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.

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