logo

qmk_firmware

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

matrix.c (9229B)


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