matrix.c 9.4 KB

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