matrix.c 9.0 KB

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