matrix.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 == ROW2COL)
  52. /* Cols 0 - 16
  53. * These columns use two 74HC138 3 to 8 bit demultiplexer. B0, F1 is the enable pin, must be set high (1) to use it.
  54. *
  55. * col / pin: PB5 PB7 PF0 PB0 PF1 PE6
  56. * 0: 0 ── 0 ── 0 1 ── 0 0
  57. * ────────────────────────────────────────────
  58. * 1: 0 ── 0 ── 1 1 ── 0 0
  59. * ────────────────────────────────────────────
  60. * 2: 0 ── 1 ── 0 1 ── 0 0
  61. * ────────────────────────────────────────────
  62. * 3: 0 ── 1 ── 1 1 ── 0 0
  63. * ────────────────────────────────────────────
  64. * 4: 1 ── 0 ── 0 1 ── 0 0
  65. * ────────────────────────────────────────────
  66. * 5: 1 ── 0 ── 1 1 ── 0 0
  67. * ────────────────────────────────────────────
  68. * 6: 1 ── 1 ── 0 1 ── 0 0
  69. * ────────────────────────────────────────────
  70. * 7: 1 ── 1 ── 1 1 ── 0 0
  71. * ────────────────────────────────────────────
  72. * 8: 0 ── 0 ── 0 0 ── 1 0
  73. * ────────────────────────────────────────────
  74. * 9: 0 ── 0 ── 1 0 ── 1 0
  75. * ────────────────────────────────────────────
  76. *10: 0 ── 1 ── 0 0 ── 1 0
  77. * ────────────────────────────────────────────
  78. *11: 0 ── 1 ── 1 0 ── 1 0
  79. * ────────────────────────────────────────────
  80. *12: 1 ── 0 ── 0 0 ── 1 0
  81. * ────────────────────────────────────────────
  82. *13: 1 ── 0 ── 1 0 ── 1 0
  83. * ────────────────────────────────────────────
  84. *14: 1 ── 1 ── 1 0 ── 1 0
  85. * ────────────────────────────────────────────
  86. *15: 1 ── 1 ── 0 0 ── 1 0
  87. * ────────────────────────────────────────────
  88. *16: 0 ── 0 ── 0 0 ── 0 1
  89. *
  90. */
  91. static void select_col(uint8_t col) {
  92. switch (col) {
  93. case 0:
  94. writePinLow(B5);
  95. writePinLow(B7);
  96. writePinLow(F0);
  97. writePinHigh(B0);
  98. break;
  99. case 1:
  100. writePinLow(B5);
  101. writePinLow(B7);
  102. writePinHigh(F0);
  103. writePinHigh(B0);
  104. break;
  105. case 2:
  106. writePinLow(B5);
  107. writePinHigh(B7);
  108. writePinLow(F0);
  109. writePinHigh(B0);
  110. break;
  111. case 3:
  112. writePinLow(B5);
  113. writePinHigh(B7);
  114. writePinHigh(F0);
  115. writePinHigh(B0);
  116. break;
  117. case 4:
  118. writePinHigh(B5);
  119. writePinLow(B7);
  120. writePinLow(F0);
  121. writePinHigh(B0);
  122. break;
  123. case 5:
  124. writePinHigh(B5);
  125. writePinLow(B7);
  126. writePinHigh(F0);
  127. writePinHigh(B0);
  128. break;
  129. case 6:
  130. writePinHigh(B5);
  131. writePinHigh(B7);
  132. writePinLow(F0);
  133. writePinHigh(B0);
  134. break;
  135. case 7:
  136. writePinHigh(B5);
  137. writePinHigh(B7);
  138. writePinHigh(F0);
  139. writePinHigh(B0);
  140. break;
  141. case 8:
  142. writePinLow(B5);
  143. writePinLow(B7);
  144. writePinLow(F0);
  145. writePinHigh(F1);
  146. break;
  147. case 9:
  148. writePinLow(B5);
  149. writePinLow(B7);
  150. writePinHigh(F0);
  151. writePinHigh(F1);
  152. break;
  153. case 10:
  154. writePinLow(B5);
  155. writePinHigh(B7);
  156. writePinLow(F0);
  157. writePinHigh(F1);
  158. break;
  159. case 11:
  160. writePinLow(B5);
  161. writePinHigh(B7);
  162. writePinHigh(F0);
  163. writePinHigh(F1);
  164. break;
  165. case 12:
  166. writePinHigh(B5);
  167. writePinLow(B7);
  168. writePinLow(F0);
  169. writePinHigh(F1);
  170. break;
  171. case 13:
  172. writePinHigh(B5);
  173. writePinLow(B7);
  174. writePinHigh(F0);
  175. writePinHigh(F1);
  176. break;
  177. case 14:
  178. writePinHigh(B5);
  179. writePinHigh(B7);
  180. writePinHigh(F0);
  181. writePinHigh(F1);
  182. break;
  183. case 15:
  184. writePinHigh(B5);
  185. writePinHigh(B7);
  186. writePinLow(F0);
  187. writePinHigh(F1);
  188. break;
  189. case 16:
  190. writePinLow(E6);
  191. break;
  192. }
  193. }
  194. static void unselect_col(uint8_t col) {
  195. switch (col) {
  196. case 0:
  197. writePinHigh(B5);
  198. writePinHigh(B7);
  199. writePinHigh(F0);
  200. writePinLow(B0);
  201. break;
  202. case 1:
  203. writePinHigh(B5);
  204. writePinHigh(B7);
  205. writePinLow(F0);
  206. writePinLow(B0);
  207. break;
  208. case 2:
  209. writePinHigh(B5);
  210. writePinLow(B7);
  211. writePinHigh(F0);
  212. writePinLow(B0);
  213. break;
  214. case 3:
  215. writePinHigh(B5);
  216. writePinLow(B7);
  217. writePinLow(F0);
  218. writePinLow(B0);
  219. break;
  220. case 4:
  221. writePinLow(B5);
  222. writePinHigh(B7);
  223. writePinHigh(F0);
  224. writePinLow(B0);
  225. break;
  226. case 5:
  227. writePinLow(B5);
  228. writePinHigh(B7);
  229. writePinLow(F0);
  230. writePinLow(B0);
  231. break;
  232. case 6:
  233. writePinLow(B5);
  234. writePinLow(B7);
  235. writePinHigh(F0);
  236. writePinLow(B0);
  237. break;
  238. case 7:
  239. writePinLow(B5);
  240. writePinLow(B7);
  241. writePinLow(F0);
  242. writePinLow(B0);
  243. break;
  244. case 8:
  245. writePinHigh(B5);
  246. writePinHigh(B7);
  247. writePinHigh(F0);
  248. writePinLow(F1);
  249. break;
  250. case 9:
  251. writePinHigh(B5);
  252. writePinHigh(B7);
  253. writePinLow(F0);
  254. writePinLow(F1);
  255. break;
  256. case 10:
  257. writePinHigh(B5);
  258. writePinLow(B7);
  259. writePinHigh(F0);
  260. writePinLow(F1);
  261. break;
  262. case 11:
  263. writePinHigh(B5);
  264. writePinLow(B7);
  265. writePinLow(F0);
  266. writePinLow(F1);
  267. break;
  268. case 12:
  269. writePinLow(B5);
  270. writePinHigh(B7);
  271. writePinHigh(F0);
  272. writePinLow(F1);
  273. break;
  274. case 13:
  275. writePinLow(B5);
  276. writePinHigh(B7);
  277. writePinLow(F0);
  278. writePinLow(F1);
  279. break;
  280. case 14:
  281. writePinLow(B5);
  282. writePinLow(B7);
  283. writePinLow(F0);
  284. writePinLow(F1);
  285. break;
  286. case 15:
  287. writePinLow(B5);
  288. writePinLow(B7);
  289. writePinHigh(F0);
  290. writePinLow(F1);
  291. break;
  292. case 16:
  293. writePinHigh(E6);
  294. break;
  295. }
  296. }
  297. static void unselect_cols(void) {
  298. //Native
  299. writePinHigh(E6);
  300. //Demultiplexer
  301. writePinLow(B0);
  302. writePinLow(F1);
  303. writePinHigh(B5);
  304. writePinHigh(B7);
  305. writePinHigh(F0);
  306. }
  307. static void init_pins(void) {
  308. unselect_cols();
  309. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  310. setPinInputHigh(row_pins[x]);
  311. }
  312. setPinOutput(B5);
  313. setPinOutput(B7);
  314. setPinOutput(F0);
  315. setPinOutput(B0);
  316. setPinOutput(F1);
  317. setPinOutput(E6);
  318. }
  319. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  320. bool matrix_changed = false;
  321. // Select col and wait for col selecton to stabilize
  322. select_col(current_col);
  323. wait_us(30);
  324. // For each row...
  325. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  326. // Store last value of row prior to reading
  327. matrix_row_t last_row_value = current_matrix[row_index];
  328. // Check row pin state
  329. if (readPin(row_pins[row_index]) == 0) {
  330. // Pin LO, set col bit
  331. current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
  332. } else {
  333. // Pin HI, clear col bit
  334. current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
  335. }
  336. // Determine if the matrix changed state
  337. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
  338. matrix_changed = true;
  339. }
  340. }
  341. // Unselect col
  342. unselect_col(current_col);
  343. return matrix_changed;
  344. }
  345. #endif
  346. void matrix_init_custom(void) {
  347. // initialize key pins
  348. init_pins();
  349. }
  350. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  351. bool changed = false;
  352. #if defined(DIRECT_PINS) || (DIODE_DIRECTION == ROW2COL)
  353. // Set col, read rows
  354. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  355. changed |= read_rows_on_col(current_matrix, current_col);
  356. }
  357. #endif
  358. return changed;
  359. }