matrix.c 10 KB

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