logo

qmk_firmware

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

matrix.c (6454B)


  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. static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
  37. static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
  38. /* matrix state(1:on, 0:off) */
  39. static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values
  40. static matrix_row_t matrix[MATRIX_ROWS]; //debounced values
  41. __attribute__ ((weak))
  42. void matrix_init_kb(void) {
  43. matrix_init_user();
  44. }
  45. __attribute__ ((weak))
  46. void matrix_scan_kb(void) {
  47. matrix_scan_user();
  48. }
  49. __attribute__ ((weak))
  50. void matrix_init_user(void) {
  51. }
  52. __attribute__ ((weak))
  53. void matrix_scan_user(void) {
  54. }
  55. inline
  56. uint8_t matrix_rows(void) {
  57. return MATRIX_ROWS;
  58. }
  59. inline
  60. uint8_t matrix_cols(void) {
  61. return MATRIX_COLS;
  62. }
  63. inline
  64. bool matrix_is_on(uint8_t row, uint8_t col)
  65. {
  66. return (matrix[row] & ((matrix_row_t)1<<col));
  67. }
  68. inline
  69. matrix_row_t matrix_get_row(uint8_t row)
  70. {
  71. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  72. // switch blocker installed and the switch is always pressed.
  73. #ifdef MATRIX_MASKED
  74. return matrix[row] & matrix_mask[row];
  75. #else
  76. return matrix[row];
  77. #endif
  78. }
  79. void matrix_print(void)
  80. {
  81. print_matrix_header();
  82. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  83. print_hex8(row); print(": ");
  84. print_matrix_row(row);
  85. print("\n");
  86. }
  87. }
  88. static void select_row(uint8_t row)
  89. {
  90. gpio_set_pin_output(row_pins[row]);
  91. gpio_write_pin_low(row_pins[row]);
  92. }
  93. static void unselect_row(uint8_t row)
  94. {
  95. gpio_set_pin_input_high(row_pins[row]);
  96. }
  97. static void unselect_rows(void)
  98. {
  99. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  100. gpio_set_pin_input_high(row_pins[x]);
  101. }
  102. }
  103. static void select_col(uint8_t col)
  104. {
  105. gpio_set_pin_output(col_pins[col]);
  106. gpio_write_pin_low(col_pins[col]);
  107. }
  108. static void unselect_col(uint8_t col)
  109. {
  110. gpio_set_pin_input_high(col_pins[col]);
  111. }
  112. static void unselect_cols(void)
  113. {
  114. for(uint8_t x = 0; x < MATRIX_COLS; x++) {
  115. gpio_set_pin_input_high(col_pins[x]);
  116. }
  117. }
  118. static void init_pins(void) {
  119. unselect_rows();
  120. unselect_cols();
  121. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  122. gpio_set_pin_input_high(col_pins[x]);
  123. }
  124. for (uint8_t x = 0; x < MATRIX_ROWS; x++) {
  125. gpio_set_pin_input_high(row_pins[x]);
  126. }
  127. }
  128. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  129. {
  130. // Store last value of row prior to reading
  131. matrix_row_t last_row_value = current_matrix[current_row];
  132. // Clear data in matrix row
  133. current_matrix[current_row] = 0;
  134. // Select row and wait for row selecton to stabilize
  135. select_row(current_row);
  136. wait_us(30);
  137. // For each col...
  138. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  139. // Select the col pin to read (active low)
  140. uint8_t pin_state = gpio_read_pin(col_pins[col_index]);
  141. // Populate the matrix row with the state of the col pin
  142. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  143. }
  144. // Unselect row
  145. unselect_row(current_row);
  146. return (last_row_value != current_matrix[current_row]);
  147. }
  148. static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
  149. {
  150. bool matrix_changed = false;
  151. // Select col and wait for col selecton to stabilize
  152. select_col(current_col);
  153. wait_us(30);
  154. // For each row...
  155. for(uint8_t row_index = 0; row_index < MATRIX_ROWS/2; row_index++)
  156. {
  157. uint8_t tmp = row_index + MATRIX_ROWS/2;
  158. // Store last value of row prior to reading
  159. matrix_row_t last_row_value = current_matrix[tmp];
  160. // Check row pin state
  161. if (gpio_read_pin(row_pins[row_index]) == 0)
  162. {
  163. // Pin LO, set col bit
  164. current_matrix[tmp] |= (ROW_SHIFTER << current_col);
  165. }
  166. else
  167. {
  168. // Pin HI, clear col bit
  169. current_matrix[tmp] &= ~(ROW_SHIFTER << current_col);
  170. }
  171. // Determine if the matrix changed state
  172. if ((last_row_value != current_matrix[tmp]) && !(matrix_changed))
  173. {
  174. matrix_changed = true;
  175. }
  176. }
  177. // Unselect col
  178. unselect_col(current_col);
  179. return matrix_changed;
  180. }
  181. void matrix_init(void) {
  182. // initialize key pins
  183. init_pins();
  184. // initialize matrix state: all keys off
  185. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  186. raw_matrix[i] = 0;
  187. matrix[i] = 0;
  188. }
  189. debounce_init(MATRIX_ROWS);
  190. matrix_init_kb();
  191. }
  192. uint8_t matrix_scan(void)
  193. {
  194. bool changed = false;
  195. // Set row, read cols
  196. for (uint8_t current_row = 0; current_row < MATRIX_ROWS / 2; current_row++) {
  197. changed |= read_cols_on_row(raw_matrix, current_row);
  198. }
  199. //else
  200. // Set col, read rows
  201. for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
  202. changed |= read_rows_on_col(raw_matrix, current_col);
  203. }
  204. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  205. matrix_scan_kb();
  206. return (uint8_t)changed;
  207. }