matrix.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. #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 matrix[MATRIX_ROWS];
  48. #if (DIODE_DIRECTION == COL2ROW)
  49. static void init_cols(void);
  50. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
  51. static void unselect_rows(void);
  52. static void select_row(uint8_t row);
  53. static void unselect_row(uint8_t row);
  54. #elif (DIODE_DIRECTION == ROW2COL)
  55. static void init_rows(void);
  56. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
  57. static void unselect_cols(void);
  58. static void unselect_col(uint8_t col);
  59. static void select_col(uint8_t col);
  60. #endif
  61. __attribute__ ((weak))
  62. void matrix_init_quantum(void) {
  63. matrix_init_kb();
  64. }
  65. __attribute__ ((weak))
  66. void matrix_scan_quantum(void) {
  67. matrix_scan_kb();
  68. }
  69. __attribute__ ((weak))
  70. void matrix_init_kb(void) {
  71. matrix_init_user();
  72. }
  73. __attribute__ ((weak))
  74. void matrix_scan_kb(void) {
  75. matrix_scan_user();
  76. }
  77. __attribute__ ((weak))
  78. void matrix_init_user(void) {
  79. }
  80. __attribute__ ((weak))
  81. void matrix_scan_user(void) {
  82. }
  83. inline
  84. uint8_t matrix_rows(void) {
  85. return MATRIX_ROWS;
  86. }
  87. inline
  88. uint8_t matrix_cols(void) {
  89. return MATRIX_COLS;
  90. }
  91. void matrix_init(void) {
  92. // initialize row and col
  93. #if (DIODE_DIRECTION == COL2ROW)
  94. unselect_rows();
  95. init_cols();
  96. #elif (DIODE_DIRECTION == ROW2COL)
  97. unselect_cols();
  98. init_rows();
  99. #endif
  100. // initialize matrix state: all keys off
  101. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  102. matrix[i] = 0;
  103. }
  104. matrix_init_quantum();
  105. }
  106. uint8_t matrix_scan(void)
  107. {
  108. #if (DIODE_DIRECTION == COL2ROW)
  109. // Set row, read cols
  110. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  111. read_cols_on_row(matrix, current_row);
  112. }
  113. #elif (DIODE_DIRECTION == ROW2COL)
  114. // Set col, read rows
  115. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  116. read_rows_on_col(matrix, current_col);
  117. }
  118. #endif
  119. matrix_scan_quantum();
  120. return 1;
  121. }
  122. //Deprecated.
  123. bool matrix_is_modified(void)
  124. {
  125. return true;
  126. }
  127. inline
  128. bool matrix_is_on(uint8_t row, uint8_t col)
  129. {
  130. return (matrix[row] & ((matrix_row_t)1<col));
  131. }
  132. inline
  133. matrix_row_t matrix_get_row(uint8_t row)
  134. {
  135. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  136. // switch blocker installed and the switch is always pressed.
  137. #ifdef MATRIX_MASKED
  138. return matrix[row] & matrix_mask[row];
  139. #else
  140. return matrix[row];
  141. #endif
  142. }
  143. void matrix_print(void)
  144. {
  145. print_matrix_header();
  146. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  147. phex(row); print(": ");
  148. print_matrix_row(row);
  149. print("\n");
  150. }
  151. }
  152. uint8_t matrix_key_count(void)
  153. {
  154. uint8_t count = 0;
  155. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  156. count += matrix_bitpop(i);
  157. }
  158. return count;
  159. }
  160. #if (DIODE_DIRECTION == COL2ROW)
  161. static void init_cols(void)
  162. {
  163. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  164. setPinInputHigh(col_pins[x]);
  165. }
  166. }
  167. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  168. {
  169. // Store last value of row prior to reading
  170. matrix_row_t last_row_value = current_matrix[current_row];
  171. // Clear data in matrix row
  172. current_matrix[current_row] = 0;
  173. // Select row and wait for row selecton to stabilize
  174. select_row(current_row);
  175. wait_us(30);
  176. // For each col...
  177. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  178. // Select the col pin to read (active low)
  179. uint8_t pin_state = readPin(col_pins[col_index]);
  180. // Populate the matrix row with the state of the col pin
  181. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  182. }
  183. // Unselect row
  184. unselect_row(current_row);
  185. return (last_row_value != current_matrix[current_row]);
  186. }
  187. static void select_row(uint8_t row)
  188. {
  189. setPinOutput(row_pins[row]);
  190. writePinLow(row_pins[row]);
  191. }
  192. static void unselect_row(uint8_t row)
  193. {
  194. setPinInputHigh(row_pins[row]);
  195. }
  196. static void unselect_rows(void)
  197. {
  198. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  199. setPinInput(row_pins[x]);
  200. }
  201. }
  202. #elif (DIODE_DIRECTION == ROW2COL)
  203. static void init_rows(void)
  204. {
  205. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  206. setPinInputHigh(row_pins[x]);
  207. }
  208. }
  209. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  210. {
  211. bool matrix_changed = false;
  212. // Select col and wait for col selecton to stabilize
  213. select_col(current_col);
  214. wait_us(30);
  215. // For each row...
  216. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  217. {
  218. // Store last value of row prior to reading
  219. matrix_row_t last_row_value = current_matrix[row_index];
  220. // Check row pin state
  221. if (readPin(row_pins[row_index]) == 0)
  222. {
  223. // Pin LO, set col bit
  224. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  225. }
  226. else
  227. {
  228. // Pin HI, clear col bit
  229. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  230. }
  231. // Determine if the matrix changed state
  232. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  233. {
  234. matrix_changed = true;
  235. }
  236. }
  237. // Unselect col
  238. unselect_col(current_col);
  239. return matrix_changed;
  240. }
  241. static void select_col(uint8_t col)
  242. {
  243. setPinOutput(col_pins[col]);
  244. writePinLow(col_pins[col]);
  245. }
  246. static void unselect_col(uint8_t col)
  247. {
  248. setPinInputHigh(col_pins[col]);
  249. }
  250. static void unselect_cols(void)
  251. {
  252. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  253. setPinInputHigh(col_pins[x]);
  254. }
  255. }
  256. #endif