matrix.c 11 KB

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