You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

361 lines
8.8 KiB

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