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.

344 lines
12 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 "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. gpio_write_pin_low(B5);
  61. gpio_write_pin_low(B7);
  62. gpio_write_pin_low(F0);
  63. gpio_write_pin_high(B0);
  64. break;
  65. case 1:
  66. gpio_write_pin_low(B5);
  67. gpio_write_pin_low(B7);
  68. gpio_write_pin_high(F0);
  69. gpio_write_pin_high(B0);
  70. break;
  71. case 2:
  72. gpio_write_pin_low(B5);
  73. gpio_write_pin_high(B7);
  74. gpio_write_pin_low(F0);
  75. gpio_write_pin_high(B0);
  76. break;
  77. case 3:
  78. gpio_write_pin_low(B5);
  79. gpio_write_pin_high(B7);
  80. gpio_write_pin_high(F0);
  81. gpio_write_pin_high(B0);
  82. break;
  83. case 4:
  84. gpio_write_pin_high(B5);
  85. gpio_write_pin_low(B7);
  86. gpio_write_pin_low(F0);
  87. gpio_write_pin_high(B0);
  88. break;
  89. case 5:
  90. gpio_write_pin_high(B5);
  91. gpio_write_pin_low(B7);
  92. gpio_write_pin_high(F0);
  93. gpio_write_pin_high(B0);
  94. break;
  95. case 6:
  96. gpio_write_pin_high(B5);
  97. gpio_write_pin_high(B7);
  98. gpio_write_pin_low(F0);
  99. gpio_write_pin_high(B0);
  100. break;
  101. case 7:
  102. gpio_write_pin_high(B5);
  103. gpio_write_pin_high(B7);
  104. gpio_write_pin_high(F0);
  105. gpio_write_pin_high(B0);
  106. break;
  107. case 8:
  108. gpio_write_pin_low(B5);
  109. gpio_write_pin_low(B7);
  110. gpio_write_pin_low(F0);
  111. gpio_write_pin_high(F1);
  112. break;
  113. case 9:
  114. gpio_write_pin_low(B5);
  115. gpio_write_pin_low(B7);
  116. gpio_write_pin_high(F0);
  117. gpio_write_pin_high(F1);
  118. break;
  119. case 10:
  120. gpio_write_pin_low(B5);
  121. gpio_write_pin_high(B7);
  122. gpio_write_pin_low(F0);
  123. gpio_write_pin_high(F1);
  124. break;
  125. case 11:
  126. gpio_write_pin_low(B5);
  127. gpio_write_pin_high(B7);
  128. gpio_write_pin_high(F0);
  129. gpio_write_pin_high(F1);
  130. break;
  131. case 12:
  132. gpio_write_pin_high(B5);
  133. gpio_write_pin_low(B7);
  134. gpio_write_pin_low(F0);
  135. gpio_write_pin_high(F1);
  136. break;
  137. case 13:
  138. gpio_write_pin_high(B5);
  139. gpio_write_pin_low(B7);
  140. gpio_write_pin_high(F0);
  141. gpio_write_pin_high(F1);
  142. break;
  143. case 14:
  144. gpio_write_pin_high(B5);
  145. gpio_write_pin_high(B7);
  146. gpio_write_pin_high(F0);
  147. gpio_write_pin_high(F1);
  148. break;
  149. case 15:
  150. gpio_write_pin_high(B5);
  151. gpio_write_pin_high(B7);
  152. gpio_write_pin_low(F0);
  153. gpio_write_pin_high(F1);
  154. break;
  155. case 16:
  156. gpio_write_pin_low(E6);
  157. break;
  158. }
  159. }
  160. static void unselect_col(uint8_t col) {
  161. switch (col) {
  162. case 0:
  163. gpio_write_pin_high(B5);
  164. gpio_write_pin_high(B7);
  165. gpio_write_pin_high(F0);
  166. gpio_write_pin_low(B0);
  167. break;
  168. case 1:
  169. gpio_write_pin_high(B5);
  170. gpio_write_pin_high(B7);
  171. gpio_write_pin_low(F0);
  172. gpio_write_pin_low(B0);
  173. break;
  174. case 2:
  175. gpio_write_pin_high(B5);
  176. gpio_write_pin_low(B7);
  177. gpio_write_pin_high(F0);
  178. gpio_write_pin_low(B0);
  179. break;
  180. case 3:
  181. gpio_write_pin_high(B5);
  182. gpio_write_pin_low(B7);
  183. gpio_write_pin_low(F0);
  184. gpio_write_pin_low(B0);
  185. break;
  186. case 4:
  187. gpio_write_pin_low(B5);
  188. gpio_write_pin_high(B7);
  189. gpio_write_pin_high(F0);
  190. gpio_write_pin_low(B0);
  191. break;
  192. case 5:
  193. gpio_write_pin_low(B5);
  194. gpio_write_pin_high(B7);
  195. gpio_write_pin_low(F0);
  196. gpio_write_pin_low(B0);
  197. break;
  198. case 6:
  199. gpio_write_pin_low(B5);
  200. gpio_write_pin_low(B7);
  201. gpio_write_pin_high(F0);
  202. gpio_write_pin_low(B0);
  203. break;
  204. case 7:
  205. gpio_write_pin_low(B5);
  206. gpio_write_pin_low(B7);
  207. gpio_write_pin_low(F0);
  208. gpio_write_pin_low(B0);
  209. break;
  210. case 8:
  211. gpio_write_pin_high(B5);
  212. gpio_write_pin_high(B7);
  213. gpio_write_pin_high(F0);
  214. gpio_write_pin_low(F1);
  215. break;
  216. case 9:
  217. gpio_write_pin_high(B5);
  218. gpio_write_pin_high(B7);
  219. gpio_write_pin_low(F0);
  220. gpio_write_pin_low(F1);
  221. break;
  222. case 10:
  223. gpio_write_pin_high(B5);
  224. gpio_write_pin_low(B7);
  225. gpio_write_pin_high(F0);
  226. gpio_write_pin_low(F1);
  227. break;
  228. case 11:
  229. gpio_write_pin_high(B5);
  230. gpio_write_pin_low(B7);
  231. gpio_write_pin_low(F0);
  232. gpio_write_pin_low(F1);
  233. break;
  234. case 12:
  235. gpio_write_pin_low(B5);
  236. gpio_write_pin_high(B7);
  237. gpio_write_pin_high(F0);
  238. gpio_write_pin_low(F1);
  239. break;
  240. case 13:
  241. gpio_write_pin_low(B5);
  242. gpio_write_pin_high(B7);
  243. gpio_write_pin_low(F0);
  244. gpio_write_pin_low(F1);
  245. break;
  246. case 14:
  247. gpio_write_pin_low(B5);
  248. gpio_write_pin_low(B7);
  249. gpio_write_pin_low(F0);
  250. gpio_write_pin_low(F1);
  251. break;
  252. case 15:
  253. gpio_write_pin_low(B5);
  254. gpio_write_pin_low(B7);
  255. gpio_write_pin_high(F0);
  256. gpio_write_pin_low(F1);
  257. break;
  258. case 16:
  259. gpio_write_pin_high(E6);
  260. break;
  261. }
  262. }
  263. static void unselect_cols(void) {
  264. //Native
  265. gpio_write_pin_high(E6);
  266. //Demultiplexer
  267. gpio_write_pin_low(B0);
  268. gpio_write_pin_low(F1);
  269. gpio_write_pin_high(B5);
  270. gpio_write_pin_high(B7);
  271. gpio_write_pin_high(F0);
  272. }
  273. static void init_pins(void) {
  274. unselect_cols();
  275. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  276. gpio_set_pin_input_high(row_pins[x]);
  277. }
  278. gpio_set_pin_output(B5);
  279. gpio_set_pin_output(B7);
  280. gpio_set_pin_output(F0);
  281. gpio_set_pin_output(B0);
  282. gpio_set_pin_output(F1);
  283. gpio_set_pin_output(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 (gpio_read_pin(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. }