matrix.c 8.8 KB

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