matrix.c 11 KB

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