matrix.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. #include <stdint.h>
  15. #include <stdbool.h>
  16. #include "wait.h"
  17. #include "util.h"
  18. #include "matrix.h"
  19. #include "debounce.h"
  20. #include "quantum.h"
  21. #ifdef DIRECT_PINS
  22. static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
  23. #elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
  24. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  25. //static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  26. #endif
  27. // matrix code
  28. #ifdef DIRECT_PINS
  29. static void init_pins(void) {
  30. for (int row = 0; row < MATRIX_ROWS; row++) {
  31. for (int col = 0; col < MATRIX_COLS; col++) {
  32. pin_t pin = direct_pins[row][col];
  33. if (pin != NO_PIN) {
  34. setPinInputHigh(pin);
  35. }
  36. }
  37. }
  38. }
  39. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  40. matrix_row_t last_row_value = current_matrix[current_row];
  41. current_matrix[current_row] = 0;
  42. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  43. pin_t pin = direct_pins[current_row][col_index];
  44. if (pin != NO_PIN) {
  45. current_matrix[current_row] |= readPin(pin) ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  46. }
  47. }
  48. return (last_row_value != current_matrix[current_row]);
  49. }
  50. #elif (DIODE_DIRECTION == COL2ROW)
  51. static void select_row(uint8_t row) {
  52. setPinOutput(row_pins[row]);
  53. writePinLow(row_pins[row]);
  54. }
  55. static void unselect_row(uint8_t row) { setPinInputHigh(row_pins[row]); }
  56. static void unselect_rows(void) {
  57. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  58. setPinInputHigh(row_pins[x]);
  59. }
  60. }
  61. static void init_pins(void) {
  62. unselect_rows();
  63. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  64. setPinInputHigh(col_pins[x]);
  65. }
  66. }
  67. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  68. // Store last value of row prior to reading
  69. matrix_row_t last_row_value = current_matrix[current_row];
  70. // Clear data in matrix row
  71. current_matrix[current_row] = 0;
  72. // Select row and wait for row selecton to stabilize
  73. select_row(current_row);
  74. wait_us(30);
  75. // For each col...
  76. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  77. // Select the col pin to read (active low)
  78. uint8_t pin_state = readPin(col_pins[col_index]);
  79. // Populate the matrix row with the state of the col pin
  80. current_matrix[current_row] |= pin_state ? 0 : (MATRIX_ROW_SHIFTER << col_index);
  81. }
  82. // Unselect row
  83. unselect_row(current_row);
  84. return (last_row_value != current_matrix[current_row]);
  85. }
  86. #elif (DIODE_DIRECTION == ROW2COL)
  87. /* Cols 0 - 15
  88. * col 0: F7
  89. * col 1: F5
  90. * col 2: F6
  91. * col 3: F1
  92. * col 4: F4
  93. * col 5: F0
  94. * These columns use a 74HC237D 3 to 8 bit demultiplexer. D4 is the enable pin, must be set high to use it.
  95. * A0 A1 A2
  96. * col / pin: PD2 PD1 PD0
  97. * 6: 1 1 1
  98. * col 7: D3
  99. * col 8: B7
  100. * 9: 0 1 1
  101. * 10: 1 0 1
  102. * 11: 0 0 1
  103. * 12: 1 1 0
  104. * 13: 0 1 0
  105. * 14: 1 0 0
  106. * 15: 0 0 0
  107. */
  108. static void select_col(uint8_t col) {
  109. switch (col) {
  110. case 0:
  111. writePinLow(F7);
  112. break;
  113. case 1:
  114. writePinLow(F5);
  115. break;
  116. case 2:
  117. writePinLow(F6);
  118. break;
  119. case 3:
  120. writePinLow(F1);
  121. break;
  122. case 4:
  123. writePinLow(F4);
  124. break;
  125. case 5:
  126. writePinLow(F0);
  127. break;
  128. case 6:
  129. writePinHigh(D4);
  130. writePinHigh(D2);
  131. writePinHigh(D1);
  132. writePinHigh(D0);
  133. break;
  134. case 7:
  135. writePinLow(D5);
  136. break;
  137. case 8:
  138. writePinLow(D3);
  139. break;
  140. case 9:
  141. writePinHigh(D4);
  142. writePinHigh(D1);
  143. writePinHigh(D0);
  144. break;
  145. case 10:
  146. writePinHigh(D4);
  147. writePinHigh(D2);
  148. writePinHigh(D0);
  149. break;
  150. case 11:
  151. writePinHigh(D4);
  152. writePinHigh(D0);
  153. break;
  154. case 12:
  155. writePinHigh(D4);
  156. writePinHigh(D2);
  157. writePinHigh(D1);
  158. break;
  159. case 13:
  160. writePinHigh(D4);
  161. writePinHigh(D1);
  162. break;
  163. case 14:
  164. writePinHigh(D4);
  165. writePinHigh(D2);
  166. break;
  167. case 15:
  168. writePinHigh(D4);
  169. break;
  170. }
  171. }
  172. static void unselect_col(uint8_t col) {
  173. switch (col) {
  174. case 0:
  175. writePinHigh(F7);
  176. break;
  177. case 1:
  178. writePinHigh(F5);
  179. break;
  180. case 2:
  181. writePinHigh(F6);
  182. break;
  183. case 3:
  184. writePinHigh(F1);
  185. break;
  186. case 4:
  187. writePinHigh(F4);
  188. break;
  189. case 5:
  190. writePinHigh(F0);
  191. break;
  192. case 6:
  193. writePinLow(D4);
  194. writePinLow(D2);
  195. writePinLow(D1);
  196. writePinLow(D0);
  197. break;
  198. case 7:
  199. writePinHigh(D5);
  200. break;
  201. case 8:
  202. writePinHigh(D3);
  203. break;
  204. case 9:
  205. writePinLow(D4);
  206. writePinLow(D2);
  207. writePinLow(D1);
  208. writePinLow(D0);
  209. break;
  210. case 10:
  211. writePinLow(D4);
  212. writePinLow(D2);
  213. writePinLow(D1);
  214. writePinLow(D0);
  215. break;
  216. case 11:
  217. writePinLow(D4);
  218. writePinLow(D2);
  219. writePinLow(D1);
  220. writePinLow(D0);
  221. break;
  222. case 12:
  223. writePinLow(D4);
  224. writePinLow(D2);
  225. writePinLow(D1);
  226. writePinLow(D0);
  227. break;
  228. case 13:
  229. writePinLow(D4);
  230. writePinLow(D2);
  231. writePinLow(D1);
  232. writePinLow(D0);
  233. break;
  234. case 14:
  235. writePinLow(D4);
  236. writePinLow(D2);
  237. writePinLow(D1);
  238. writePinLow(D0);
  239. break;
  240. case 15:
  241. writePinLow(D4);
  242. writePinLow(D2);
  243. writePinLow(D1);
  244. writePinLow(D0);
  245. break;
  246. }
  247. }
  248. static void unselect_cols(void) {
  249. //Native
  250. writePinHigh(F7);
  251. writePinHigh(F5);
  252. writePinHigh(F6);
  253. writePinHigh(F1);
  254. writePinHigh(F4);
  255. writePinHigh(F0);
  256. writePinHigh(D3);
  257. writePinHigh(D5);
  258. //Demultiplexer
  259. writePinLow(D4);
  260. writePinLow(D2);
  261. writePinLow(D1);
  262. writePinLow(D0);
  263. }
  264. static void init_pins(void) {
  265. unselect_cols();
  266. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  267. setPinInputHigh(row_pins[x]);
  268. }
  269. setPinOutput(D0);
  270. setPinOutput(D1);
  271. setPinOutput(D2);
  272. setPinOutput(D3);
  273. setPinOutput(F7);
  274. setPinOutput(F5);
  275. setPinOutput(F6);
  276. setPinOutput(F1);
  277. setPinOutput(F4);
  278. setPinOutput(F0);
  279. setPinOutput(D3);
  280. setPinOutput(D5);
  281. setPinOutput(D4);
  282. }
  283. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col) {
  284. bool matrix_changed = false;
  285. // Select col and wait for col selecton to stabilize
  286. select_col(current_col);
  287. wait_us(30);
  288. // For each row...
  289. for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
  290. // Store last value of row prior to reading
  291. matrix_row_t last_row_value = current_matrix[row_index];
  292. // Check row pin state
  293. if (readPin(row_pins[row_index]) == 0) {
  294. // Pin LO, set col bit
  295. current_matrix[row_index] |= (MATRIX_ROW_SHIFTER << current_col);
  296. } else {
  297. // Pin HI, clear col bit
  298. current_matrix[row_index] &= ~(MATRIX_ROW_SHIFTER << current_col);
  299. }
  300. // Determine if the matrix changed state
  301. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed)) {
  302. matrix_changed = true;
  303. }
  304. }
  305. // Unselect col
  306. unselect_col(current_col);
  307. return matrix_changed;
  308. }
  309. #endif
  310. void matrix_init_custom(void) {
  311. // initialize key pins
  312. init_pins();
  313. }
  314. bool matrix_scan_custom(matrix_row_t current_matrix[]) {
  315. bool changed = false;
  316. #if defined(DIRECT_PINS) || (DIODE_DIRECTION == COL2ROW)
  317. // Set row, read cols
  318. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  319. changed |= read_cols_on_row(current_matrix, current_row);
  320. }
  321. #elif (DIODE_DIRECTION == ROW2COL)
  322. // Set col, read rows
  323. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  324. changed |= read_rows_on_col(current_matrix, current_col);
  325. }
  326. #endif
  327. return changed;
  328. }