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.

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