5 #include "action_layer.h"
14 * This constant define not debouncing time in msecs, but amount of matrix
15 * scan loops which should be made to get stable debounced results.
17 * On Ergodox matrix scan rate is relatively low, because of slow I2C.
18 * Now it's only 317 scans/second, or about 3.15 msec/scan.
19 * According to Cherry specs, debouncing time is 5 msec.
21 * And so, there is no sense to have DEBOUNCE higher than 2.
28 /* matrix state(1:on, 0:off) */
29 static matrix_row_t matrix[MATRIX_ROWS];
31 // Debouncing: store for each key the number of scans until it's eligible to
32 // change. When scanning the matrix, ignore any changes in keys that have
33 // already changed in the last DEBOUNCE scans.
34 static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
36 static matrix_row_t read_cols(uint8_t row);
37 static void init_cols(void);
38 static void unselect_rows(void);
39 static void select_row(uint8_t row);
41 __attribute__ ((weak))
42 void matrix_init_user(void) {}
44 __attribute__ ((weak))
45 void matrix_scan_user(void) {}
47 __attribute__ ((weak))
48 void matrix_init_kb(void) {
52 __attribute__ ((weak))
53 void matrix_scan_kb(void) {
58 uint8_t matrix_rows(void)
64 uint8_t matrix_cols(void)
69 void matrix_init(void)
74 // initialize matrix state: all keys off
75 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
77 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
78 debounce_matrix[i * MATRIX_COLS + j] = 0;
82 matrix_init_quantum();
85 void matrix_power_up(void) {
89 // initialize matrix state: all keys off
90 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
95 // Returns a matrix_row_t whose bits are set if the corresponding key should be
96 // eligible to change in this scan.
97 matrix_row_t debounce_mask(uint8_t row) {
98 matrix_row_t result = 0;
99 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
100 if (debounce_matrix[row * MATRIX_COLS + j]) {
101 --debounce_matrix[row * MATRIX_COLS + j];
109 // Report changed keys in the given row. Resets the debounce countdowns
110 // corresponding to each set bit in 'change' to DEBOUNCE.
111 void debounce_report(matrix_row_t change, uint8_t row) {
112 for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
113 if (change & (1 << i)) {
114 debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
119 uint8_t matrix_scan(void)
123 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
125 wait_us(30); // without this wait read unstable value.
126 matrix_row_t mask = debounce_mask(i);
127 matrix_row_t cols = (read_cols(i) & mask) | (matrix[i] & ~mask);
128 debounce_report(cols ^ matrix[i], i);
134 matrix_scan_quantum();
140 bool matrix_is_on(uint8_t row, uint8_t col)
142 return (matrix[row] & ((matrix_row_t)1<<col));
146 matrix_row_t matrix_get_row(uint8_t row)
151 void matrix_print(void)
153 print("\nr/c 0123456789ABCDEF\n");
154 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
155 phex(row); print(": ");
156 pbin_reverse16(matrix_get_row(row));
161 uint8_t matrix_key_count(void)
164 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
165 count += bitpop16(matrix[i]);
170 /* Column pin configuration
172 * Pro Micro: 6 5 4 3 2 1 0
173 * PD3 PD2 PD4 PC6 PD7 PE6 PB4
175 * Expander: 13 12 11 10 9 8 7
177 static void init_cols(void)
182 DDRD &= ~(1<<PD2 | 1<<PD3 | 1<<PD4 | 1<<PD7);
183 PORTD |= (1<<PD2 | 1<<PD3 | 1<<PD4 | 1<<PD7);
193 static matrix_row_t read_cols(uint8_t row)
195 return expander_read_row() |
196 (PIND&(1<<PD3) ? 0 : (1<<6)) |
197 (PIND&(1<<PD2) ? 0 : (1<<5)) |
198 (PIND&(1<<PD4) ? 0 : (1<<4)) |
199 (PINC&(1<<PC6) ? 0 : (1<<3)) |
200 (PIND&(1<<PD7) ? 0 : (1<<2)) |
201 (PINE&(1<<PE6) ? 0 : (1<<1)) |
202 (PINB&(1<<PB4) ? 0 : (1<<0)) ;
205 /* Row pin configuration
207 * Pro Micro: 0 1 2 3 4 5
210 * Expander: 0 1 2 3 4 5
212 static void unselect_rows(void)
215 DDRF &= ~(1<<PF4 | 1<<PF5 | 1<<PF6 | 1<<PF7);
216 PORTF &= ~(1<<PF4 | 1<<PF5 | 1<<PF6 | 1<<PF7);
217 DDRB &= ~(1<<PB1 | 1<<PB2);
218 PORTB &= ~(1<<PB1 | 1<<PB2);
221 expander_unselect_rows();
224 static void select_row(uint8_t row)
254 expander_select_row(row);