matrix.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. Copyright 2012-2018 Jun Wako, Jack Humbert, Yiancar
  3. Port done by Andy Lee <alee@alittlepeacemusic.com>
  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 "print.h"
  19. #include "debug.h"
  20. #include "util.h"
  21. #include "matrix.h"
  22. #include "debounce.h"
  23. #include "quantum.h"
  24. #if (MATRIX_COLS <= 8)
  25. # define print_matrix_header() print("\nr/c 01234567\n")
  26. # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
  27. # define matrix_bitpop(i) bitpop(matrix[i])
  28. # define ROW_SHIFTER ((uint8_t)1)
  29. #elif (MATRIX_COLS <= 16)
  30. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  31. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  32. # define matrix_bitpop(i) bitpop16(matrix[i])
  33. # define ROW_SHIFTER ((uint16_t)1)
  34. #elif (MATRIX_COLS <= 32)
  35. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  36. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  37. # define matrix_bitpop(i) bitpop32(matrix[i])
  38. # define ROW_SHIFTER ((uint32_t)1)
  39. #endif
  40. #ifdef MATRIX_MASKED
  41. extern const matrix_row_t matrix_mask[];
  42. #endif
  43. #ifdef DIRECT_PINS
  44. static pin_t direct_pins[MATRIX_ROWS][MATRIX_COLS] = DIRECT_PINS;
  45. #elif (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
  46. // static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  47. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  48. #endif
  49. /* matrix state(1:on, 0:off) */
  50. static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values
  51. static matrix_row_t matrix[MATRIX_ROWS]; //debounced values
  52. __attribute__ ((weak))
  53. void matrix_init_quantum(void) {
  54. matrix_init_kb();
  55. }
  56. __attribute__ ((weak))
  57. void matrix_scan_quantum(void) {
  58. matrix_scan_kb();
  59. }
  60. __attribute__ ((weak))
  61. void matrix_init_kb(void) {
  62. matrix_init_user();
  63. }
  64. __attribute__ ((weak))
  65. void matrix_scan_kb(void) {
  66. matrix_scan_user();
  67. }
  68. __attribute__ ((weak))
  69. void matrix_init_user(void) {
  70. }
  71. __attribute__ ((weak))
  72. void matrix_scan_user(void) {
  73. }
  74. inline
  75. uint8_t matrix_rows(void) {
  76. return MATRIX_ROWS;
  77. }
  78. inline
  79. uint8_t matrix_cols(void) {
  80. return MATRIX_COLS;
  81. }
  82. //Deprecated.
  83. bool matrix_is_modified(void)
  84. {
  85. if (debounce_active()) return false;
  86. return true;
  87. }
  88. inline
  89. bool matrix_is_on(uint8_t row, uint8_t col)
  90. {
  91. return (matrix[row] & ((matrix_row_t)1<<col));
  92. }
  93. inline
  94. matrix_row_t matrix_get_row(uint8_t row)
  95. {
  96. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  97. // switch blocker installed and the switch is always pressed.
  98. #ifdef MATRIX_MASKED
  99. return matrix[row] & matrix_mask[row];
  100. #else
  101. return matrix[row];
  102. #endif
  103. }
  104. void matrix_print(void)
  105. {
  106. print_matrix_header();
  107. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  108. phex(row); print(": ");
  109. print_matrix_row(row);
  110. print("\n");
  111. }
  112. }
  113. uint8_t matrix_key_count(void)
  114. {
  115. uint8_t count = 0;
  116. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  117. count += matrix_bitpop(i);
  118. }
  119. return count;
  120. }
  121. #ifdef DIRECT_PINS
  122. static void init_pins(void) {
  123. for (int row = 0; row < MATRIX_ROWS; row++) {
  124. for (int col = 0; col < MATRIX_COLS; col++) {
  125. pin_t pin = direct_pins[row][col];
  126. if (pin != NO_PIN) {
  127. setPinInputHigh(pin);
  128. }
  129. }
  130. }
  131. }
  132. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row) {
  133. matrix_row_t last_row_value = current_matrix[current_row];
  134. current_matrix[current_row] = 0;
  135. for (uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  136. pin_t pin = direct_pins[current_row][col_index];
  137. if (pin != NO_PIN) {
  138. current_matrix[current_row] |= readPin(pin) ? 0 : (ROW_SHIFTER << col_index);
  139. }
  140. }
  141. return (last_row_value != current_matrix[current_row]);
  142. }
  143. #elif (DIODE_DIRECTION == COL2ROW)
  144. /* Rows 0 - 5
  145. * These rows use a 74HC138 3-to-8 demultiplexer.
  146. * C B A
  147. * row / pin: PB0 PB1 PB2
  148. * 0: 0 0 0
  149. * 1: 0 0 1
  150. * 2: 0 1 0
  151. * 3: 0 1 1
  152. * 4: 1 0 0
  153. * 5: 1 0 1
  154. */
  155. static void select_row(uint8_t col)
  156. {
  157. switch (col) {
  158. case 0:
  159. writePinLow(B0);
  160. writePinLow(B1);
  161. writePinLow(B2);
  162. break;
  163. case 1:
  164. writePinLow(B0);
  165. writePinLow(B1);
  166. break;
  167. case 2:
  168. writePinLow(B0);
  169. writePinLow(B2);
  170. break;
  171. case 3:
  172. writePinLow(B0);
  173. break;
  174. case 4:
  175. writePinLow(B1);
  176. writePinLow(B2);
  177. break;
  178. case 5:
  179. writePinLow(B1);
  180. break;
  181. }
  182. }
  183. static void unselect_row(uint8_t col)
  184. {
  185. switch (col) {
  186. case 0:
  187. writePinHigh(B0);
  188. writePinHigh(B1);
  189. writePinHigh(B2);
  190. break;
  191. case 1:
  192. writePinHigh(B0);
  193. writePinHigh(B1);
  194. break;
  195. case 2:
  196. writePinHigh(B0);
  197. writePinHigh(B2);
  198. break;
  199. case 3:
  200. writePinHigh(B0);
  201. break;
  202. case 4:
  203. writePinHigh(B1);
  204. writePinHigh(B2);
  205. break;
  206. case 5:
  207. writePinHigh(B1);
  208. break;
  209. }
  210. }
  211. static void unselect_rows(void)
  212. {
  213. setPinOutput(B0);
  214. setPinOutput(B1);
  215. setPinOutput(B2);
  216. // make all pins high to select Y7, nothing is connected to that (otherwise the first row will act weird)
  217. writePinHigh(B0);
  218. writePinHigh(B1);
  219. writePinHigh(B2);
  220. }
  221. static void init_pins(void) {
  222. unselect_rows();
  223. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  224. setPinInputHigh(col_pins[x]);
  225. }
  226. }
  227. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  228. {
  229. // Store last value of row prior to reading
  230. matrix_row_t last_row_value = current_matrix[current_row];
  231. // Clear data in matrix row
  232. current_matrix[current_row] = 0;
  233. // Select row and wait for row selecton to stabilize
  234. select_row(current_row);
  235. wait_us(30);
  236. // For each col...
  237. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  238. // Select the col pin to read (active low)
  239. uint8_t pin_state = readPin(col_pins[col_index]);
  240. // Populate the matrix row with the state of the col pin
  241. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  242. }
  243. // Unselect row
  244. unselect_row(current_row);
  245. return (last_row_value != current_matrix[current_row]);
  246. }
  247. #elif (DIODE_DIRECTION == ROW2COL)
  248. static void select_col(uint8_t col)
  249. {
  250. setPinOutput(col_pins[col]);
  251. writePinLow(col_pins[col]);
  252. }
  253. static void unselect_col(uint8_t col)
  254. {
  255. setPinInputHigh(col_pins[col]);
  256. }
  257. static void unselect_cols(void)
  258. {
  259. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  260. setPinInputHigh(col_pins[x]);
  261. }
  262. }
  263. static void init_pins(void) {
  264. unselect_cols();
  265. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  266. setPinInputHigh(row_pins[x]);
  267. }
  268. }
  269. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  270. {
  271. bool matrix_changed = false;
  272. // Select col and wait for col selecton to stabilize
  273. select_col(current_col);
  274. wait_us(30);
  275. // For each row...
  276. for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
  277. {
  278. // Store last value of row prior to reading
  279. matrix_row_t last_row_value = current_matrix[row_index];
  280. // Check row pin state
  281. if (readPin(row_pins[row_index]) == 0)
  282. {
  283. // Pin LO, set col bit
  284. current_matrix[row_index] |= (ROW_SHIFTER << current_col);
  285. }
  286. else
  287. {
  288. // Pin HI, clear col bit
  289. current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
  290. }
  291. // Determine if the matrix changed state
  292. if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
  293. {
  294. matrix_changed = true;
  295. }
  296. }
  297. // Unselect col
  298. unselect_col(current_col);
  299. return matrix_changed;
  300. }
  301. #endif
  302. void matrix_init(void) {
  303. // initialize key pins
  304. init_pins();
  305. // initialize matrix state: all keys off
  306. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  307. raw_matrix[i] = 0;
  308. matrix[i] = 0;
  309. }
  310. debounce_init(MATRIX_ROWS);
  311. matrix_init_quantum();
  312. }
  313. uint8_t matrix_scan(void)
  314. {
  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(raw_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(raw_matrix, current_col);
  325. }
  326. #endif
  327. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  328. matrix_scan_quantum();
  329. return 1;
  330. }