matrix.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. /*
  2. Copyright 2012-2019 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. #include "i2c_master.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 matrix_bitpop(i) bitpop(matrix[i])
  28. # define ROW_SHIFTER ((uint8_t)1)
  29. #elif (MATRIX_COLS <= 16)
  30. # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
  31. # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
  32. # define matrix_bitpop(i) bitpop16(matrix[i])
  33. # define ROW_SHIFTER ((uint16_t)1)
  34. #elif (MATRIX_COLS <= 32)
  35. # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
  36. # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
  37. # define matrix_bitpop(i) bitpop32(matrix[i])
  38. # define ROW_SHIFTER ((uint32_t)1)
  39. #endif
  40. #ifdef MATRIX_MASKED
  41. extern const matrix_row_t matrix_mask[];
  42. #endif
  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. /* matrix state(1:on, 0:off) */
  46. static matrix_row_t raw_matrix[MATRIX_ROWS]; //raw values
  47. static matrix_row_t matrix[MATRIX_ROWS]; //debounced values
  48. __attribute__ ((weak))
  49. void matrix_init_quantum(void) {
  50. matrix_init_kb();
  51. }
  52. __attribute__ ((weak))
  53. void matrix_scan_quantum(void) {
  54. matrix_scan_kb();
  55. }
  56. __attribute__ ((weak))
  57. void matrix_init_kb(void) {
  58. matrix_init_user();
  59. }
  60. __attribute__ ((weak))
  61. void matrix_scan_kb(void) {
  62. matrix_scan_user();
  63. }
  64. __attribute__ ((weak))
  65. void matrix_init_user(void) {
  66. }
  67. __attribute__ ((weak))
  68. void matrix_scan_user(void) {
  69. }
  70. inline
  71. uint8_t matrix_rows(void) {
  72. return MATRIX_ROWS;
  73. }
  74. inline
  75. uint8_t matrix_cols(void) {
  76. return MATRIX_COLS;
  77. }
  78. //Deprecated.
  79. bool matrix_is_modified(void)
  80. {
  81. if (debounce_active()) return false;
  82. return true;
  83. }
  84. inline
  85. bool matrix_is_on(uint8_t row, uint8_t col)
  86. {
  87. return (matrix[row] & ((matrix_row_t)1<<col));
  88. }
  89. inline
  90. matrix_row_t matrix_get_row(uint8_t row)
  91. {
  92. // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
  93. // switch blocker installed and the switch is always pressed.
  94. #ifdef MATRIX_MASKED
  95. return matrix[row] & matrix_mask[row];
  96. #else
  97. return matrix[row];
  98. #endif
  99. }
  100. void matrix_print(void)
  101. {
  102. print_matrix_header();
  103. for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
  104. phex(row); print(": ");
  105. print_matrix_row(row);
  106. print("\n");
  107. }
  108. }
  109. uint8_t matrix_key_count(void)
  110. {
  111. uint8_t count = 0;
  112. for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
  113. count += matrix_bitpop(i);
  114. }
  115. return count;
  116. }
  117. static void select_row(uint8_t row)
  118. {
  119. setPinOutput(row_pins[row]);
  120. writePinLow(row_pins[row]);
  121. }
  122. static void unselect_row(uint8_t row)
  123. {
  124. setPinInputHigh(row_pins[row]);
  125. }
  126. static void unselect_rows(void)
  127. {
  128. for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
  129. setPinInput(row_pins[x]);
  130. }
  131. }
  132. static void init_pins(void) {
  133. unselect_rows();
  134. // Set I/O
  135. uint8_t send_data = 0x07;
  136. i2c_writeReg((PORT_EXPANDER_ADDRESS << 1), 0x00, &send_data, 1, 20);
  137. // // Set Pull-up
  138. i2c_writeReg((PORT_EXPANDER_ADDRESS << 1), 0x06, &send_data, 1, 20);
  139. for (uint8_t x = 0; x < MATRIX_COLS; x++) {
  140. if ( (x > 0) && (x < 12) ) {
  141. setPinInputHigh(col_pins[x]);
  142. }
  143. }
  144. }
  145. static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
  146. {
  147. // Store last value of row prior to reading
  148. matrix_row_t last_row_value = current_matrix[current_row];
  149. // Clear data in matrix row
  150. current_matrix[current_row] = 0;
  151. // Select row and wait for row selecton to stabilize
  152. select_row(current_row);
  153. wait_us(30);
  154. // For each col...
  155. for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
  156. uint8_t pin_state;
  157. // Select the col pin to read (active low)
  158. switch (col_index) {
  159. case 0 :
  160. i2c_readReg((PORT_EXPANDER_ADDRESS << 1), 0x09, &pin_state, 1, 20);
  161. pin_state = pin_state & 0x01;
  162. break;
  163. case 12 :
  164. i2c_readReg((PORT_EXPANDER_ADDRESS << 1), 0x09, &pin_state, 1, 20);
  165. pin_state = pin_state & (1 << 2);
  166. break;
  167. case 13 :
  168. i2c_readReg((PORT_EXPANDER_ADDRESS << 1), 0x09, &pin_state, 1, 20);
  169. pin_state = pin_state & (1 << 1);
  170. break;
  171. default :
  172. pin_state = readPin(col_pins[col_index]);
  173. }
  174. // Populate the matrix row with the state of the col pin
  175. current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
  176. }
  177. // Unselect row
  178. unselect_row(current_row);
  179. return (last_row_value != current_matrix[current_row]);
  180. }
  181. void matrix_init(void) {
  182. // Initialize I2C
  183. i2c_init();
  184. // initialize key pins
  185. init_pins();
  186. // initialize matrix state: all keys off
  187. for (uint8_t i=0; i < MATRIX_ROWS; i++) {
  188. raw_matrix[i] = 0;
  189. matrix[i] = 0;
  190. }
  191. debounce_init(MATRIX_ROWS);
  192. matrix_init_quantum();
  193. }
  194. uint8_t matrix_scan(void)
  195. {
  196. bool changed = false;
  197. // Set row, read cols
  198. for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
  199. changed |= read_cols_on_row(raw_matrix, current_row);
  200. }
  201. debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
  202. matrix_scan_quantum();
  203. return (uint8_t)changed;
  204. }