You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

287 lines
6.9 KiB

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