You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

matrix.c 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*
  2. Copyright 2014 Ralf Schmitt <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include <avr/io.h>
  17. #include <util/delay.h>
  18. #include "print.h"
  19. #include "debug.h"
  20. #include "util.h"
  21. #include "matrix.h"
  22. #include "eeconfig.h"
  23. #include "action_layer.h"
  24. #include "backlight.h"
  25. #ifndef DEBOUNCE
  26. # define DEBOUNCE 0
  27. #endif
  28. static uint8_t debouncing = DEBOUNCE;
  29. static matrix_row_t matrix[MATRIX_ROWS];
  30. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  31. static uint8_t read_rows(void);
  32. static uint8_t read_fwkey(void);
  33. static void init_rows(void);
  34. static void unselect_cols(void);
  35. static void select_col(uint8_t col);
  36. inline
  37. uint8_t matrix_rows(void)
  38. {
  39. return MATRIX_ROWS;
  40. }
  41. inline
  42. uint8_t matrix_cols(void)
  43. {
  44. return MATRIX_COLS;
  45. }
  46. void misc_init(void) {
  47. }
  48. void matrix_init(void)
  49. {
  50. backlight_init_ports();
  51. unselect_cols();
  52. init_rows();
  53. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  54. matrix[i] = 0;
  55. matrix_debouncing[i] = 0;
  56. }
  57. }
  58. uint8_t matrix_scan(void)
  59. {
  60. for (uint8_t col = 0; col < MATRIX_COLS; col++) {
  61. select_col(col);
  62. _delay_us(3);
  63. uint8_t rows = read_rows();
  64. // Use the otherwise unused col: 0 row: 0 for firmware key
  65. if(col == 0) {
  66. rows |= read_fwkey();
  67. }
  68. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  69. bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
  70. bool curr_bit = rows & (1<<row);
  71. if (prev_bit != curr_bit) {
  72. matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
  73. if (debouncing) {
  74. dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
  75. }
  76. debouncing = DEBOUNCE;
  77. }
  78. }
  79. unselect_cols();
  80. }
  81. if (debouncing) {
  82. if (--debouncing) {
  83. _delay_ms(1);
  84. } else {
  85. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  86. matrix[i] = matrix_debouncing[i];
  87. }
  88. }
  89. }
  90. return 1;
  91. }
  92. bool matrix_is_modified(void)
  93. {
  94. if (debouncing) return false;
  95. return true;
  96. }
  97. inline
  98. bool matrix_is_on(uint8_t row, uint8_t col)
  99. {
  100. return (matrix[row] & ((matrix_row_t)1<<col));
  101. }
  102. inline
  103. matrix_row_t matrix_get_row(uint8_t row)
  104. {
  105. return matrix[row];
  106. }
  107. void matrix_print(void)
  108. {
  109. print("\nr/c 0123456789ABCDEF\n");
  110. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  111. xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
  112. }
  113. }
  114. uint8_t matrix_key_count(void)
  115. {
  116. uint8_t count = 0;
  117. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  118. count += bitpop32(matrix[i]);
  119. }
  120. return count;
  121. }
  122. /* Row configuration
  123. *
  124. * row: 0 1 2 3 4 5
  125. * pin: PD0 PD1 PD2 PD3 PD5 PB7
  126. *
  127. * Firmware uses pin PE2
  128. */
  129. static void init_rows(void)
  130. {
  131. DDRD &= ~0b00101111;
  132. PORTD |= 0b00101111;
  133. DDRB &= ~0b10000000;
  134. PORTB |= 0b10000000;
  135. DDRE &= ~0b00000100;
  136. PORTE |= 0b00000100;
  137. }
  138. static uint8_t read_rows(void)
  139. {
  140. return (PIND&(1<<0) ? (1<<0) : 0) |
  141. (PIND&(1<<1) ? (1<<1) : 0) |
  142. (PIND&(1<<2) ? (1<<2) : 0) |
  143. (PIND&(1<<3) ? (1<<3) : 0) |
  144. (PIND&(1<<5) ? (1<<4) : 0) |
  145. (PINB&(1<<7) ? (1<<5) : 0);
  146. }
  147. static uint8_t read_fwkey(void)
  148. {
  149. return PINE&(1<<2) ? 0 : (1<<0);
  150. }
  151. /* Column configuration
  152. *
  153. * col: 0 1 2 3
  154. * pin: PF0 PF1 PC7 PC6
  155. */
  156. static void unselect_cols(void)
  157. {
  158. DDRF |= 0b00000011;
  159. PORTF &= ~0b00000011;
  160. DDRC |= 0b11000000;
  161. PORTC &= ~0b11000000;
  162. }
  163. static void select_col(uint8_t col)
  164. {
  165. switch (col) {
  166. case 0:
  167. PORTF |= (1<<0);
  168. break;
  169. case 1:
  170. PORTF |= (1<<1);
  171. break;
  172. case 2:
  173. PORTC |= (1<<7);
  174. break;
  175. case 3:
  176. PORTC |= (1<<6);
  177. break;
  178. }
  179. }