matrix.c 8.9 KB

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