matrix.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. Copyright 2012 Jun Wako
  3. Copyright 2014 Jack Humbert
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include <stdint.h>
  16. #include <stdbool.h>
  17. #if defined(__AVR__)
  18. #include <avr/io.h>
  19. #endif
  20. #include "wait.h"
  21. #include "print.h"
  22. #include "debug.h"
  23. #include "util.h"
  24. #include "matrix.h"
  25. #include "timer.h"
  26. /* Set 0 if debouncing isn't needed */
  27. #ifndef DEBOUNCING_DELAY
  28. # define DEBOUNCING_DELAY 5
  29. #endif
  30. #if (DEBOUNCING_DELAY > 0)
  31. static uint16_t debouncing_time;
  32. static bool debouncing = false;
  33. #endif
  34. #if (MATRIX_COLS <= 8)
  35. # define print_matrix_header() print("\nr/c 01234567\n")
  36. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  37. # define matrix_bitpop(i) bitpop(matrix[i])
  38. # define ROW_SHIFTER ((uint8_t)1)
  39. #elif (MATRIX_COLS <= 16)
  40. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  41. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  42. # define matrix_bitpop(i) bitpop16(matrix[i])
  43. # define ROW_SHIFTER ((uint16_t)1)
  44. #elif (MATRIX_COLS <= 32)
  45. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  46. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  47. # define matrix_bitpop(i) bitpop32(matrix[i])
  48. # define ROW_SHIFTER ((uint32_t)1)
  49. #endif
  50. #ifdef MATRIX_MASKED
  51. extern const matrix_row_t matrix_mask[];
  52. #endif
  53. static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  54. static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  55. /* matrix state(1:on, 0:off) */
  56. static matrix_row_t matrix[MATRIX_ROWS];
  57. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  58. #if (DIODE_DIRECTION == COL2ROW)
  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. #else // ROW2COL
  65. static void init_rows(void);
  66. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  67. static void unselect_cols(void);
  68. static void unselect_col(uint8_t col);
  69. static void select_col(uint8_t col);
  70. #endif
  71. __attribute__ ((weak))
  72. void matrix_init_quantum(void) {
  73. matrix_init_kb();
  74. }
  75. __attribute__ ((weak))
  76. void matrix_scan_quantum(void) {
  77. matrix_scan_kb();
  78. }
  79. __attribute__ ((weak))
  80. void matrix_init_kb(void) {
  81. matrix_init_user();
  82. }
  83. __attribute__ ((weak))
  84. void matrix_scan_kb(void) {
  85. matrix_scan_user();
  86. }
  87. __attribute__ ((weak))
  88. void matrix_init_user(void) {
  89. }
  90. __attribute__ ((weak))
  91. void matrix_scan_user(void) {
  92. }
  93. inline
  94. uint8_t matrix_rows(void) {
  95. return MATRIX_ROWS;
  96. }
  97. inline
  98. uint8_t matrix_cols(void) {
  99. return MATRIX_COLS;
  100. }
  101. // void matrix_power_up(void) {
  102. // #if (DIODE_DIRECTION == COL2ROW)
  103. // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
  104. // /* DDRxn */
  105. // _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
  106. // toggle_row(r);
  107. // }
  108. // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
  109. // /* PORTxn */
  110. // _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
  111. // }
  112. // #else
  113. // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
  114. // /* DDRxn */
  115. // _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
  116. // toggle_col(c);
  117. // }
  118. // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
  119. // /* PORTxn */
  120. // _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
  121. // }
  122. // #endif
  123. // }
  124. void matrix_init(void) {
  125. // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
  126. #if (defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega32U4__))
  127. MCUCR |= _BV(JTD);
  128. MCUCR |= _BV(JTD);
  129. #endif
  130. // initialize row and col
  131. #if (DIODE_DIRECTION == COL2ROW)
  132. unselect_rows();
  133. init_cols();
  134. #else // ROW2COL
  135. unselect_cols();
  136. init_rows();
  137. #endif
  138. // initialize matrix state: all keys off
  139. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  140. matrix[i] = 0;
  141. matrix_debouncing[i] = 0;
  142. }
  143. matrix_init_quantum();
  144. }
  145. uint8_t matrix_scan(void)
  146. {
  147. #if (DIODE_DIRECTION == COL2ROW)
  148. // Set row, read cols
  149. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  150. # if (DEBOUNCING_DELAY > 0)
  151. bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
  152. if (matrix_changed) {
  153. debouncing = true;
  154. debouncing_time = timer_read();
  155. }
  156. # else
  157. read_cols_on_row(matrix, current_row);
  158. # endif
  159. }
  160. #else // ROW2COL
  161. // Set col, read rows
  162. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  163. # if (DEBOUNCING_DELAY > 0)
  164. bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
  165. if (matrix_changed) {
  166. debouncing = true;
  167. debouncing_time = timer_read();
  168. }
  169. # else
  170. read_rows_on_col(matrix, current_col);
  171. # endif
  172. }
  173. #endif
  174. # if (DEBOUNCING_DELAY > 0)
  175. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
  176. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  177. matrix[i] = matrix_debouncing[i];
  178. }
  179. debouncing = false;
  180. }
  181. # endif
  182. matrix_scan_quantum();
  183. return 1;
  184. }
  185. bool matrix_is_modified(void)
  186. {
  187. #if (DEBOUNCING_DELAY > 0)
  188. if (debouncing) return false;
  189. #endif
  190. return true;
  191. }
  192. inline
  193. bool matrix_is_on(uint8_t row, uint8_t col)
  194. {
  195. return (matrix[row] & ((matrix_row_t)1<col));
  196. }
  197. inline
  198. matrix_row_t matrix_get_row(uint8_t row)
  199. {
  200. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  201. // switch blocker installed and the switch is always pressed.
  202. #ifdef MATRIX_MASKED
  203. return matrix[row] & matrix_mask[row];
  204. #else
  205. return matrix[row];
  206. #endif
  207. }
  208. void matrix_print(void)
  209. {
  210. print_matrix_header();
  211. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  212. phex(row); print(": ");
  213. print_matrix_row(row);
  214. print("\n");
  215. }
  216. }
  217. uint8_t matrix_key_count(void)
  218. {
  219. uint8_t count = 0;
  220. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  221. count += matrix_bitpop(i);
  222. }
  223. return count;
  224. }
  225. #if (DIODE_DIRECTION == COL2ROW)
  226. static void init_cols(void)
  227. {
  228. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  229. uint8_t pin = col_pins[x];
  230. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  231. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  232. }
  233. }
  234. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  235. {
  236. // Store last value of row prior to reading
  237. matrix_row_t last_row_value = current_matrix[current_row];
  238. // Clear data in matrix row
  239. current_matrix[current_row] = 0;
  240. // Select row and wait for row selecton to stabilize
  241. select_row(current_row);
  242. wait_us(30);
  243. // For each col...
  244. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  245. // Select the col pin to read (active low)
  246. uint8_t pin = col_pins[col_index];
  247. uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
  248. // Populate the matrix row with the state of the col pin
  249. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  250. }
  251. // Unselect row
  252. unselect_row(current_row);
  253. return (last_row_value != current_matrix[current_row]);
  254. }
  255. static void select_row(uint8_t row)
  256. {
  257. uint8_t pin = row_pins[row];
  258. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  259. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  260. }
  261. static void unselect_row(uint8_t row)
  262. {
  263. uint8_t pin = row_pins[row];
  264. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  265. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  266. }
  267. static void unselect_rows(void)
  268. {
  269. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  270. uint8_t pin = row_pins[x];
  271. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  272. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  273. }
  274. }
  275. #else // ROW2COL
  276. static void init_rows(void)
  277. {
  278. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  279. uint8_t pin = row_pins[x];
  280. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  281. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  282. }
  283. }
  284. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  285. {
  286. bool matrix_changed = false;
  287. // Select col and wait for col selecton to stabilize
  288. select_col(current_col);
  289. wait_us(30);
  290. // For each row...
  291. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  292. {
  293. // Store last value of row prior to reading
  294. matrix_row_t last_row_value = current_matrix[row_index];
  295. // Check row pin state
  296. if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
  297. {
  298. // Pin LO, set col bit
  299. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  300. }
  301. else
  302. {
  303. // Pin HI, clear col bit
  304. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  305. }
  306. // Determine if the matrix changed state
  307. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  308. {
  309. matrix_changed = true;
  310. }
  311. }
  312. // Unselect col
  313. unselect_col(current_col);
  314. return matrix_changed;
  315. }
  316. static void select_col(uint8_t col)
  317. {
  318. uint8_t pin = col_pins[col];
  319. _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
  320. _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
  321. }
  322. static void unselect_col(uint8_t col)
  323. {
  324. uint8_t pin = col_pins[col];
  325. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  326. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  327. }
  328. static void unselect_cols(void)
  329. {
  330. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  331. uint8_t pin = col_pins[x];
  332. _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
  333. _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
  334. }
  335. }
  336. #endif