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.

385 lines
13 KiB

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