matrix.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. #include "quantum.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. #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
  54. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  55. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  56. #endif
  57. /* matrix state(1:on, 0:off) */
  58. static matrix_row_t matrix[MATRIX_ROWS];
  59. static matrix_row_t matrix_debouncing[MATRIX_ROWS];
  60. #if (DIODE_DIRECTION == COL2ROW)
  61. static void init_cols(void);
  62. static bool 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. #elif (DIODE_DIRECTION == ROW2COL)
  67. static void init_rows(void);
  68. static bool 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. // #elif (DIODE_DIRECTION == ROW2COL)
  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. // initialize row and col
  128. #if (DIODE_DIRECTION == COL2ROW)
  129. unselect_rows();
  130. init_cols();
  131. #elif (DIODE_DIRECTION == 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_debouncing[i] = 0;
  139. }
  140. matrix_init_quantum();
  141. }
  142. uint8_t matrix_scan(void)
  143. {
  144. #if (DIODE_DIRECTION == COL2ROW)
  145. // Set row, read cols
  146. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  147. # if (DEBOUNCING_DELAY > 0)
  148. bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
  149. if (matrix_changed) {
  150. debouncing = true;
  151. debouncing_time = timer_read();
  152. }
  153. # else
  154. read_cols_on_row(matrix, current_row);
  155. # endif
  156. }
  157. #elif (DIODE_DIRECTION == ROW2COL)
  158. // Set col, read rows
  159. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  160. # if (DEBOUNCING_DELAY > 0)
  161. bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
  162. if (matrix_changed) {
  163. debouncing = true;
  164. debouncing_time = timer_read();
  165. }
  166. # else
  167. read_rows_on_col(matrix, current_col);
  168. # endif
  169. }
  170. #endif
  171. # if (DEBOUNCING_DELAY > 0)
  172. if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
  173. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  174. matrix[i] = matrix_debouncing[i];
  175. }
  176. debouncing = false;
  177. }
  178. # endif
  179. dprintf("%x\n", B0);
  180. matrix_scan_quantum();
  181. return 1;
  182. }
  183. bool matrix_is_modified(void)
  184. {
  185. #if (DEBOUNCING_DELAY > 0)
  186. if (debouncing) return false;
  187. #endif
  188. return true;
  189. }
  190. inline
  191. bool matrix_is_on(uint8_t row, uint8_t col)
  192. {
  193. return (matrix[row] & ((matrix_row_t)1<col));
  194. }
  195. inline
  196. matrix_row_t matrix_get_row(uint8_t row)
  197. {
  198. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  199. // switch blocker installed and the switch is always pressed.
  200. #ifdef MATRIX_MASKED
  201. return matrix[row] & matrix_mask[row];
  202. #else
  203. return matrix[row];
  204. #endif
  205. }
  206. void matrix_print(void)
  207. {
  208. print_matrix_header();
  209. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  210. phex(row); print(": ");
  211. print_matrix_row(row);
  212. print("\n");
  213. }
  214. }
  215. uint8_t matrix_key_count(void)
  216. {
  217. uint8_t count = 0;
  218. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  219. count += matrix_bitpop(i);
  220. }
  221. return count;
  222. }
  223. #if (DIODE_DIRECTION == COL2ROW)
  224. static void init_cols(void)
  225. {
  226. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  227. uint8_t pin = col_pins[x];
  228. DDR_INPUT(pin); // IN
  229. PORT_HIGH(pin); // HI
  230. }
  231. }
  232. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  233. {
  234. // Store last value of row prior to reading
  235. matrix_row_t last_row_value = current_matrix[current_row];
  236. // Clear data in matrix row
  237. current_matrix[current_row] = 0;
  238. // Select row and wait for row selecton to stabilize
  239. select_row(current_row);
  240. wait_us(30);
  241. // For each col...
  242. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  243. // Select the col pin to read (active low)
  244. uint8_t pin = col_pins[col_index];
  245. uint8_t pin_state = PIN_VALUE(pin);
  246. // Populate the matrix row with the state of the col pin
  247. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  248. }
  249. // Unselect row
  250. unselect_row(current_row);
  251. return (last_row_value != current_matrix[current_row]);
  252. }
  253. static void select_row(uint8_t row)
  254. {
  255. uint8_t pin = row_pins[row];
  256. DDR_OUTPUT(pin); // OUT
  257. PORT_LOW(pin); // LOW
  258. }
  259. static void unselect_row(uint8_t row)
  260. {
  261. uint8_t pin = row_pins[row];
  262. DDR_INPUT(pin); // IN
  263. PORT_HIGH(pin); // HI
  264. }
  265. static void unselect_rows(void)
  266. {
  267. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  268. uint8_t pin = row_pins[x];
  269. DDR_INPUT(pin); // IN
  270. PORT_HIGH(pin); // HI
  271. }
  272. }
  273. #elif (DIODE_DIRECTION == ROW2COL)
  274. static void init_rows(void)
  275. {
  276. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  277. uint8_t pin = row_pins[x];
  278. DDR_INPUT(pin); // IN
  279. PORT_HIGH(pin); // HI
  280. }
  281. }
  282. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  283. {
  284. bool matrix_changed = false;
  285. // Select col and wait for col selecton to stabilize
  286. select_col(current_col);
  287. wait_us(30);
  288. // For each row...
  289. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  290. {
  291. // Store last value of row prior to reading
  292. matrix_row_t last_row_value = current_matrix[row_index];
  293. // Check row pin state
  294. if (PIN_VALUE(row_pins[row_index]) == 0)
  295. {
  296. // Pin LO, set col bit
  297. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  298. }
  299. else
  300. {
  301. // Pin HI, clear col bit
  302. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  303. }
  304. // Determine if the matrix changed state
  305. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  306. {
  307. matrix_changed = true;
  308. }
  309. }
  310. // Unselect col
  311. unselect_col(current_col);
  312. return matrix_changed;
  313. }
  314. static void select_col(uint8_t col)
  315. {
  316. uint8_t pin = col_pins[col];
  317. DDR_OUTPUT(pin); // OUT
  318. PORT_LOW(pin); // LOW
  319. }
  320. static void unselect_col(uint8_t col)
  321. {
  322. uint8_t pin = col_pins[col];
  323. DDR_INPUT(pin); // IN
  324. PORT_HIGH(pin); // HI
  325. }
  326. static void unselect_cols(void)
  327. {
  328. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  329. uint8_t pin = col_pins[x];
  330. DDR_INPUT(pin); // IN
  331. PORT_HIGH(pin); // HI
  332. }
  333. }
  334. #endif