matrix.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. Copyright 2012 Jun Wako
  3. Copyright 2014 Jack Humbert
  4. Copyright 2017 Priyadi Iman Nurcahyo
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. */
  16. #include <stdint.h>
  17. #include <stdbool.h>
  18. #if defined(__AVR__)
  19. #include <avr/io.h>
  20. #endif
  21. #include "wait.h"
  22. #include "print.h"
  23. #include "debug.h"
  24. #include "util.h"
  25. #include "matrix.h"
  26. #include "timer.h"
  27. /* Set 0 if debouncing isn't needed */
  28. #ifndef DEBOUNCING_DELAY
  29. # define DEBOUNCING_DELAY 5
  30. #endif
  31. #if (DEBOUNCING_DELAY > 0)
  32. static uint16_t debouncing_time;
  33. static bool debouncing = false;
  34. #endif
  35. #if (MATRIX_COLS <= 8)
  36. # define print_matrix_header() print("\nr/c 01234567\n")
  37. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  38. # define matrix_bitpop(i) bitpop(matrix[i])
  39. # define ROW_SHIFTER ((uint8_t)1)
  40. #elif (MATRIX_COLS <= 16)
  41. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  42. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  43. # define matrix_bitpop(i) bitpop16(matrix[i])
  44. # define ROW_SHIFTER ((uint16_t)1)
  45. #elif (MATRIX_COLS <= 32)
  46. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  47. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  48. # define matrix_bitpop(i) bitpop32(matrix[i])
  49. # define ROW_SHIFTER ((uint32_t)1)
  50. #endif
  51. #ifdef MATRIX_MASKED
  52. extern const matrix_row_t matrix_mask[];
  53. #endif
  54. static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  55. static const uint8_t tp_pins[3] = TRACKPOINT_PINS;
  56. /* matrix state(1:on, 0:off) */
  57. static matrix_row_t matrix[MATRIX_ROWS];
  58. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  59. static void init_cols(void);
  60. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  61. static void unselect_rows(void);
  62. static void select_row(uint8_t row);
  63. static void unselect_row(uint8_t row);
  64. __attribute__ ((weak))
  65. void matrix_init_kb(void) {
  66. matrix_init_user();
  67. }
  68. __attribute__ ((weak))
  69. void matrix_scan_kb(void) {
  70. matrix_scan_user();
  71. }
  72. __attribute__ ((weak))
  73. void matrix_init_user(void) {
  74. }
  75. __attribute__ ((weak))
  76. void matrix_scan_user(void) {
  77. }
  78. inline
  79. uint8_t matrix_rows(void) {
  80. return MATRIX_ROWS;
  81. }
  82. inline
  83. uint8_t matrix_cols(void) {
  84. return MATRIX_COLS;
  85. }
  86. void matrix_init(void) {
  87. // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
  88. #if (defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega32U4__))
  89. MCUCR |= _BV(JTD);
  90. MCUCR |= _BV(JTD);
  91. #endif
  92. // initialize row and col
  93. unselect_rows();
  94. init_cols();
  95. // initialize matrix state: all keys off
  96. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  97. matrix[i] = 0;
  98. matrix_debouncing[i] = 0;
  99. }
  100. matrix_init_quantum();
  101. }
  102. uint8_t matrix_scan(void)
  103. {
  104. // Set row, read cols
  105. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  106. # if (DEBOUNCING_DELAY > 0)
  107. bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
  108. if (matrix_changed) {
  109. debouncing = true;
  110. debouncing_time = timer_read();
  111. }
  112. # else
  113. read_cols_on_row(matrix, current_row);
  114. # endif
  115. }
  116. # if (DEBOUNCING_DELAY > 0)
  117. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
  118. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  119. matrix[i] = matrix_debouncing[i];
  120. }
  121. debouncing = false;
  122. }
  123. # endif
  124. matrix_scan_quantum();
  125. return 1;
  126. }
  127. bool matrix_is_modified(void)
  128. {
  129. #if (DEBOUNCING_DELAY > 0)
  130. if (debouncing) return false;
  131. #endif
  132. return true;
  133. }
  134. inline
  135. bool matrix_is_on(uint8_t row, uint8_t col)
  136. {
  137. return (matrix[row] & ((matrix_row_t)1<col));
  138. }
  139. inline
  140. matrix_row_t matrix_get_row(uint8_t row)
  141. {
  142. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  143. // switch blocker installed and the switch is always pressed.
  144. #ifdef MATRIX_MASKED
  145. return matrix[row] & matrix_mask[row];
  146. #else
  147. return matrix[row];
  148. #endif
  149. }
  150. void matrix_print(void)
  151. {
  152. print_matrix_header();
  153. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  154. phex(row); print(": ");
  155. print_matrix_row(row);
  156. print("\n");
  157. }
  158. }
  159. uint8_t matrix_key_count(void)
  160. {
  161. uint8_t count = 0;
  162. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  163. count += matrix_bitpop(i);
  164. }
  165. return count;
  166. }
  167. #define ROW_MASK 0b11100000
  168. static const uint8_t row_bit[MATRIX_ROWS] = {
  169. // 76543210
  170. 0b00000000,
  171. 0b00100000,
  172. 0b01000000,
  173. 0b01100000,
  174. 0b10000000,
  175. 0b10100000,
  176. 0b11000000,
  177. 0b11100000,
  178. };
  179. static void init_cols(void)
  180. {
  181. // columns
  182. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  183. uint8_t pin = col_pins[x];
  184. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  185. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  186. }
  187. // rows
  188. DDRF |= ROW_MASK;
  189. PORTF &= ~ROW_MASK;
  190. // trackpoint
  191. for(uint8_t x = 0; x < 3; x++) {
  192. uint8_t pin = tp_pins[x];
  193. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  194. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  195. }
  196. }
  197. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  198. {
  199. // Store last value of row prior to reading
  200. matrix_row_t last_row_value = current_matrix[current_row];
  201. // Clear data in matrix row
  202. current_matrix[current_row] = 0;
  203. // special case for trackpoint
  204. if (current_row == 8) {
  205. for(uint8_t tp_index = 0; tp_index < 3; tp_index++) {
  206. // Select the TP pin to read (active low)
  207. uint8_t pin = tp_pins[tp_index];
  208. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  209. // Populate the matrix row with the state of the col pin
  210. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << tp_index);
  211. }
  212. return (last_row_value != current_matrix[current_row]);
  213. }
  214. // Select row and wait for row selecton to stabilize
  215. select_row(current_row);
  216. _delay_us(5); // without this wait it won't read stable value.
  217. // wait_us(50);
  218. // For each col...
  219. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  220. // Select the col pin to read (active low)
  221. uint8_t pin = col_pins[col_index];
  222. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  223. // Populate the matrix row with the state of the col pin
  224. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  225. }
  226. // Unselect row
  227. unselect_row(current_row);
  228. return (last_row_value != current_matrix[current_row]);
  229. }
  230. static void select_row(uint8_t row)
  231. {
  232. PORTF = row_bit[row] | (PORTF & ~ROW_MASK);
  233. }
  234. static void unselect_row(uint8_t row)
  235. {
  236. }
  237. static void unselect_rows(void)
  238. {
  239. }