matrix.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  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. phex(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. static void select_row(uint8_t row)
  144. {
  145. setPinOutput(row_pins[row]);
  146. writePinLow(row_pins[row]);
  147. }
  148. static void unselect_row(uint8_t row)
  149. {
  150. setPinInputHigh(row_pins[row]);
  151. }
  152. static void unselect_rows(void)
  153. {
  154. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  155. setPinInputHigh(row_pins[x]);
  156. }
  157. }
  158. static void init_pins(void) {
  159. unselect_rows();
  160. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  161. setPinInputHigh(col_pins[x]);
  162. }
  163. }
  164. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  165. {
  166. // Store last value of row prior to reading
  167. matrix_row_t last_row_value = current_matrix[current_row];
  168. // Clear data in matrix row
  169. current_matrix[current_row] = 0;
  170. // Select row and wait for row selecton to stabilize
  171. select_row(current_row);
  172. wait_us(30);
  173. // For each col...
  174. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  175. // Select the col pin to read (active low)
  176. uint8_t pin_state = readPin(col_pins[col_index]);
  177. // Populate the matrix row with the state of the col pin
  178. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  179. }
  180. // Unselect row
  181. unselect_row(current_row);
  182. return (last_row_value != current_matrix[current_row]);
  183. }
  184. #elif (DIODE_DIRECTION == ROW2COL)
  185. /* Cols 0 - 15
  186. * col 0: C7
  187. * col 1: B6
  188. * col 2: C6
  189. * col 3: B4
  190. * col 4: B5
  191. * col 5: D7
  192. * These columns use a 74HC237D 3 to 8 bit demultiplexer.
  193. * A0 A1 A2
  194. * col / pin: PD2 PD1 PD0
  195. * 6: 1 1 1
  196. * 7: 0 1 1
  197. * 8: 1 0 1
  198. * 9: 0 0 1
  199. * 10: 1 1 0
  200. * 11: 0 1 0
  201. * 12: 1 0 0
  202. * col 13: D3
  203. * col 14: B7
  204. * col 15: B3
  205. */
  206. static void select_col(uint8_t col)
  207. {
  208. switch (col) {
  209. case 0:
  210. writePinLow(C7);
  211. break;
  212. case 1:
  213. writePinLow(B6);
  214. break;
  215. case 2:
  216. writePinLow(C6);
  217. break;
  218. case 3:
  219. writePinLow(B4);
  220. break;
  221. case 4:
  222. writePinLow(B5);
  223. break;
  224. case 5:
  225. writePinLow(D7);
  226. break;
  227. case 6:
  228. writePinHigh(D0);
  229. writePinHigh(D1);
  230. writePinHigh(D2);
  231. break;
  232. case 7:
  233. writePinHigh(D0);
  234. writePinHigh(D1);
  235. break;
  236. case 8:
  237. writePinHigh(D0);
  238. writePinHigh(D2);
  239. break;
  240. case 9:
  241. writePinHigh(D0);
  242. break;
  243. case 10:
  244. writePinHigh(D1);
  245. writePinHigh(D2);
  246. break;
  247. case 11:
  248. writePinHigh(D1);
  249. break;
  250. case 12:
  251. writePinHigh(D2);
  252. break;
  253. case 13:
  254. writePinLow(D3);
  255. break;
  256. case 14:
  257. writePinLow(B7);
  258. break;
  259. case 15:
  260. writePinLow(B3);
  261. break;
  262. }
  263. }
  264. static void unselect_col(uint8_t col)
  265. {
  266. switch (col) {
  267. case 0:
  268. writePinHigh(C7);
  269. break;
  270. case 1:
  271. writePinHigh(B6);
  272. break;
  273. case 2:
  274. writePinHigh(C6);
  275. break;
  276. case 3:
  277. writePinHigh(B4);
  278. break;
  279. case 4:
  280. writePinHigh(B5);
  281. break;
  282. case 5:
  283. writePinHigh(D7);
  284. break;
  285. case 6:
  286. writePinLow(D0);
  287. writePinLow(D1);
  288. writePinLow(D2);
  289. break;
  290. case 7:
  291. writePinLow(D0);
  292. writePinLow(D1);
  293. break;
  294. case 8:
  295. writePinLow(D0);
  296. writePinLow(D2);
  297. break;
  298. case 9:
  299. writePinLow(D0);
  300. break;
  301. case 10:
  302. writePinLow(D1);
  303. writePinLow(D2);
  304. break;
  305. case 11:
  306. writePinLow(D1);
  307. break;
  308. case 12:
  309. writePinLow(D2);
  310. break;
  311. case 13:
  312. writePinHigh(D3);
  313. break;
  314. case 14:
  315. writePinHigh(B7);
  316. break;
  317. case 15:
  318. writePinHigh(B3);
  319. break;
  320. }
  321. }
  322. static void unselect_cols(void)
  323. {
  324. //Native
  325. setPinOutput(D3);
  326. setPinOutput(D7);
  327. writePinHigh(D3);
  328. writePinHigh(D7);
  329. setPinOutput(C6);
  330. setPinOutput(C7);
  331. writePinHigh(C6);
  332. writePinHigh(C7);
  333. setPinOutput(B3);
  334. setPinOutput(B4);
  335. setPinOutput(B5);
  336. setPinOutput(B6);
  337. setPinOutput(B7);
  338. writePinHigh(B3);
  339. writePinHigh(B4);
  340. writePinHigh(B5);
  341. writePinHigh(B6);
  342. writePinHigh(B7);
  343. //Demultiplexer
  344. setPinOutput(D0);
  345. setPinOutput(D1);
  346. setPinOutput(D2);
  347. writePinLow(D0);
  348. writePinLow(D1);
  349. writePinLow(D2);
  350. }
  351. static void init_pins(void) {
  352. unselect_cols();
  353. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  354. setPinInputHigh(row_pins[x]);
  355. }
  356. }
  357. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  358. {
  359. bool matrix_changed = false;
  360. // Select col and wait for col selecton to stabilize
  361. select_col(current_col);
  362. wait_us(30);
  363. // For each row...
  364. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  365. {
  366. // Store last value of row prior to reading
  367. matrix_row_t last_row_value = current_matrix[row_index];
  368. // Check row pin state
  369. if (readPin(row_pins[row_index]) == 0)
  370. {
  371. // Pin LO, set col bit
  372. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  373. }
  374. else
  375. {
  376. // Pin HI, clear col bit
  377. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  378. }
  379. // Determine if the matrix changed state
  380. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  381. {
  382. matrix_changed = true;
  383. }
  384. }
  385. // Unselect col
  386. unselect_col(current_col);
  387. return matrix_changed;
  388. }
  389. #endif
  390. void matrix_init(void) {
  391. // initialize key pins
  392. init_pins();
  393. // initialize matrix state: all keys off
  394. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  395. raw_matrix[i] = 0;
  396. matrix[i] = 0;
  397. }
  398. debounce_init(MATRIX_ROWS);
  399. matrix_init_quantum();
  400. }
  401. uint8_t matrix_scan(void)
  402. {
  403. bool changed = false;
  404. #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
  405. // Set row, read cols
  406. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  407. changed |= read_cols_on_row(raw_matrix, current_row);
  408. }
  409. #elif (DIODE_DIRECTION == ROW2COL)
  410. // Set col, read rows
  411. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  412. changed |= read_rows_on_col(raw_matrix, current_col);
  413. }
  414. #endif
  415. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  416. matrix_scan_quantum();
  417. return (uint8_t)changed;
  418. }