matrix.c 11 KB

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