matrix.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar
  3. Copyright 2019 Evy Dekkers
  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 <stdint.h>
  16. #include <stdbool.h>
  17. #include "wait.h"
  18. #include "util.h"
  19. #include "matrix.h"
  20. #include "debounce.h"
  21. #include "quantum.h"
  22. #ifdef DIRECT_PINS
  23. static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
  24. #elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
  25. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  26. //static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  27. #endif
  28. // matrix code
  29. #ifdef DIRECT_PINS
  30. static void init_pins(void) {
  31. for (int row = 0; row < MATRIX_ROWS; row++) {
  32. for (int col = 0; col < MATRIX_COLS; col++) {
  33. pin_t pin = direct_pins[row][col];
  34. if (pin != NO_PIN) {
  35. setPinInputHigh(pin);
  36. }
  37. }
  38. }
  39. }
  40. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  41. matrix_row_t last_row_value = current_matrix[current_row];
  42. current_matrix[current_row] = 0;
  43. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  44. pin_t pin = direct_pins[current_row][col_index];
  45. if (pin != NO_PIN) {
  46. current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  47. }
  48. }
  49. return (last_row_value != current_matrix[current_row]);
  50. }
  51. #elif (DIODE_DIRECTION == COL2ROW)
  52. static void select_row(uint8_t row) {
  53. setPinOutput(row_pins[row]);
  54. writePinLow(row_pins[row]);
  55. }
  56. static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); }
  57. static void unselect_rows(void) {
  58. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  59. setPinInputHigh(row_pins[x]);
  60. }
  61. }
  62. static void init_pins(void) {
  63. unselect_rows();
  64. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  65. setPinInputHigh(col_pins[x]);
  66. }
  67. }
  68. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  69. // Store last value of row prior to reading
  70. matrix_row_t last_row_value = current_matrix[current_row];
  71. // Clear data in matrix row
  72. current_matrix[current_row] = 0;
  73. // Select row and wait for row selecton to stabilize
  74. select_row(current_row);
  75. wait_us(30);
  76. // For each col...
  77. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  78. // Select the col pin to read (active low)
  79. uint8_t pin_state = readPin(col_pins[col_index]);
  80. // Populate the matrix row with the state of the col pin
  81. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  82. }
  83. // Unselect row
  84. unselect_row(current_row);
  85. return (last_row_value != current_matrix[current_row]);
  86. }
  87. #elif (DIODE_DIRECTION == ROW2COL)
  88. /* Cols 0 - 16
  89. * These columns use two 74HC138 3 to 8 bit demultiplexer. D6, D7 is the enable pin, must be set high (1) to use it.
  90. *
  91. * col / pin: PA0 PA1 PA2 PD6 PD7 PC4
  92. * 0: 0 ── 0 ── 0 1 ── 0 0
  93. * ────────────────────────────────────────────
  94. * 1: 0 ── 0 ── 1 1 ── 0 0
  95. * ────────────────────────────────────────────
  96. * 2: 0 ── 1 ── 0 1 ── 0 0
  97. * ────────────────────────────────────────────
  98. * 3: 0 ── 1 ── 1 1 ── 0 0
  99. * ────────────────────────────────────────────
  100. * 4: 1 ── 0 ── 0 1 ── 0 0
  101. * ────────────────────────────────────────────
  102. * 5: 1 ── 0 ── 1 1 ── 0 0
  103. * ────────────────────────────────────────────
  104. * 6: 1 ── 1 ── 0 1 ── 0 0
  105. * ────────────────────────────────────────────
  106. * 7: 1 ── 1 ── 1 1 ── 0 0
  107. * ────────────────────────────────────────────
  108. * 8: 0 ── 0 ── 0 0 ── 1 0
  109. * ────────────────────────────────────────────
  110. * 9: 0 ── 0 ── 1 0 ── 1 0
  111. * ────────────────────────────────────────────
  112. *10: 0 ── 1 ── 0 0 ── 1 0
  113. * ────────────────────────────────────────────
  114. *11: 0 ── 1 ── 1 0 ── 1 0
  115. * ────────────────────────────────────────────
  116. *12: 1 ── 0 ── 0 0 ── 1 0
  117. * ────────────────────────────────────────────
  118. *13: 1 ── 0 ── 1 0 ── 1 0
  119. * ────────────────────────────────────────────
  120. *14: 1 ── 1 ── 1 0 ── 1 0
  121. * ────────────────────────────────────────────
  122. *15: 1 ── 1 ── 0 0 ── 1 0
  123. * ────────────────────────────────────────────
  124. *16: 0 ── 0 ── 0 0 ── 0 1
  125. *
  126. */
  127. static void select_col(uint8_t col) {
  128. switch (col) {
  129. case 0:
  130. writePinLow(A0);
  131. writePinLow(A1);
  132. writePinLow(A2);
  133. writePinHigh(D6);
  134. break;
  135. case 1:
  136. writePinLow(A0);
  137. writePinLow(A1);
  138. writePinHigh(A2);
  139. writePinHigh(D6);
  140. break;
  141. case 2:
  142. writePinLow(A0);
  143. writePinHigh(A1);
  144. writePinLow(A2);
  145. writePinHigh(D6);
  146. break;
  147. case 3:
  148. writePinLow(A0);
  149. writePinHigh(A1);
  150. writePinHigh(A2);
  151. writePinHigh(D6);
  152. break;
  153. case 4:
  154. writePinHigh(A0);
  155. writePinLow(A1);
  156. writePinLow(A2);
  157. writePinHigh(D6);
  158. break;
  159. case 5:
  160. writePinHigh(A0);
  161. writePinLow(A1);
  162. writePinHigh(A2);
  163. writePinHigh(D6);
  164. break;
  165. case 6:
  166. writePinHigh(A0);
  167. writePinHigh(A1);
  168. writePinLow(A2);
  169. writePinHigh(D6);
  170. break;
  171. case 7:
  172. writePinHigh(A0);
  173. writePinHigh(A1);
  174. writePinHigh(A2);
  175. writePinHigh(D6);
  176. break;
  177. case 8:
  178. writePinLow(A0);
  179. writePinLow(A1);
  180. writePinLow(A2);
  181. writePinHigh(D7);
  182. break;
  183. case 9:
  184. writePinLow(A0);
  185. writePinLow(A1);
  186. writePinHigh(A2);
  187. writePinHigh(D7);
  188. break;
  189. case 10:
  190. writePinLow(A0);
  191. writePinHigh(A1);
  192. writePinLow(A2);
  193. writePinHigh(D7);
  194. break;
  195. case 11:
  196. writePinLow(A0);
  197. writePinHigh(A1);
  198. writePinHigh(A2);
  199. writePinHigh(D7);
  200. break;
  201. case 12:
  202. writePinHigh(A0);
  203. writePinLow(A1);
  204. writePinLow(A2);
  205. writePinHigh(D7);
  206. break;
  207. case 13:
  208. writePinHigh(A0);
  209. writePinLow(A1);
  210. writePinHigh(A2);
  211. writePinHigh(D7);
  212. break;
  213. case 14:
  214. writePinHigh(A0);
  215. writePinHigh(A1);
  216. writePinHigh(A2);
  217. writePinHigh(D7);
  218. break;
  219. case 15:
  220. writePinHigh(A0);
  221. writePinHigh(A1);
  222. writePinLow(A2);
  223. writePinHigh(D7);
  224. break;
  225. case 16:
  226. writePinLow(C4);
  227. break;
  228. }
  229. }
  230. static void unselect_col(uint8_t col) {
  231. switch (col) {
  232. case 0:
  233. writePinHigh(A0);
  234. writePinHigh(A1);
  235. writePinHigh(A2);
  236. writePinLow(D6);
  237. break;
  238. case 1:
  239. writePinHigh(A0);
  240. writePinHigh(A1);
  241. writePinLow(A2);
  242. writePinLow(D6);
  243. break;
  244. case 2:
  245. writePinHigh(A0);
  246. writePinLow(A1);
  247. writePinHigh(A2);
  248. writePinLow(D6);
  249. break;
  250. case 3:
  251. writePinHigh(A0);
  252. writePinLow(A1);
  253. writePinLow(A2);
  254. writePinLow(D6);
  255. break;
  256. case 4:
  257. writePinLow(A0);
  258. writePinHigh(A1);
  259. writePinHigh(A2);
  260. writePinLow(D6);
  261. break;
  262. case 5:
  263. writePinLow(A0);
  264. writePinHigh(A1);
  265. writePinLow(A2);
  266. writePinLow(D6);
  267. break;
  268. case 6:
  269. writePinLow(A0);
  270. writePinLow(A1);
  271. writePinHigh(A2);
  272. writePinLow(D6);
  273. break;
  274. case 7:
  275. writePinLow(A0);
  276. writePinLow(A1);
  277. writePinLow(A2);
  278. writePinLow(D6);
  279. break;
  280. case 8:
  281. writePinHigh(A0);
  282. writePinHigh(A1);
  283. writePinHigh(A2);
  284. writePinLow(D7);
  285. break;
  286. case 9:
  287. writePinHigh(A0);
  288. writePinHigh(A1);
  289. writePinLow(A2);
  290. writePinLow(D7);
  291. break;
  292. case 10:
  293. writePinHigh(A0);
  294. writePinLow(A1);
  295. writePinHigh(A2);
  296. writePinLow(D7);
  297. break;
  298. case 11:
  299. writePinHigh(A0);
  300. writePinLow(A1);
  301. writePinLow(A2);
  302. writePinLow(D7);
  303. break;
  304. case 12:
  305. writePinLow(A0);
  306. writePinHigh(A1);
  307. writePinHigh(A2);
  308. writePinLow(D7);
  309. break;
  310. case 13:
  311. writePinLow(A0);
  312. writePinHigh(A1);
  313. writePinLow(A2);
  314. writePinLow(D7);
  315. break;
  316. case 14:
  317. writePinLow(A0);
  318. writePinLow(A1);
  319. writePinLow(A2);
  320. writePinLow(D7);
  321. break;
  322. case 15:
  323. writePinLow(A0);
  324. writePinLow(A1);
  325. writePinHigh(A2);
  326. writePinLow(D7);
  327. break;
  328. case 16:
  329. writePinHigh(C4);
  330. break;
  331. }
  332. }
  333. static void unselect_cols(void) {
  334. //Native
  335. writePinHigh(C4);
  336. //Demultiplexer
  337. writePinLow(D6);
  338. writePinLow(D7);
  339. writePinHigh(A0);
  340. writePinHigh(A1);
  341. writePinHigh(A2);
  342. }
  343. static void init_pins(void) {
  344. unselect_cols();
  345. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  346. setPinInputHigh(row_pins[x]);
  347. }
  348. setPinOutput(A0);
  349. setPinOutput(A1);
  350. setPinOutput(A2);
  351. setPinOutput(D6);
  352. setPinOutput(D7);
  353. setPinOutput(C4);
  354. }
  355. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  356. bool matrix_changed = false;
  357. // Select col and wait for col selecton to stabilize
  358. select_col(current_col);
  359. wait_us(30);
  360. // For each row...
  361. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  362. // Store last value of row prior to reading
  363. matrix_row_t last_row_value = current_matrix[row_index];
  364. // Check row pin state
  365. if (readPin(row_pins[row_index]) == 0) {
  366. // Pin LO, set col bit
  367. current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
  368. } else {
  369. // Pin HI, clear col bit
  370. current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
  371. }
  372. // Determine if the matrix changed state
  373. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
  374. matrix_changed = true;
  375. }
  376. }
  377. // Unselect col
  378. unselect_col(current_col);
  379. return matrix_changed;
  380. }
  381. #endif
  382. void matrix_init_custom(void) {
  383. // initialize key pins
  384. init_pins();
  385. }
  386. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  387. bool changed = false;
  388. #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
  389. // Set row, read cols
  390. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  391. changed |= read_cols_on_row(current_matrix, current_row);
  392. }
  393. #elif (DIODE_DIRECTION == ROW2COL)
  394. // Set col, read rows
  395. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  396. changed |= read_rows_on_col(current_matrix, current_col);
  397. }
  398. #endif
  399. return changed;
  400. }