matrix.c 8.5 KB

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