matrix.c 11 KB

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