logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

matrix.c (9184B)


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