5 #include "action_layer.h"
12 #ifdef DEBUG_MATRIX_SCAN_RATE
17 * This constant define not debouncing time in msecs, but amount of matrix
18 * scan loops which should be made to get stable debounced results.
20 * On Ergodox matrix scan rate is relatively low, because of slow I2C.
21 * Now it's only 317 scans/second, or about 3.15 msec/scan.
22 * According to Cherry specs, debouncing time is 5 msec.
24 * And so, there is no sense to have DEBOUNCE higher than 2.
31 /* matrix state(1:on, 0:off) */
32 static matrix_row_t matrix[MATRIX_ROWS];
34 // Debouncing: store for each key the number of scans until it's eligible to
35 // change. When scanning the matrix, ignore any changes in keys that have
36 // already changed in the last DEBOUNCE scans.
37 static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
39 static matrix_row_t read_cols(uint8_t row);
40 static void init_cols(void);
41 static void unselect_rows(void);
42 static void select_row(uint8_t row);
44 #ifdef DEBUG_MATRIX_SCAN_RATE
45 uint32_t matrix_timer;
46 uint32_t matrix_scan_count;
50 __attribute__ ((weak))
51 void matrix_init_user(void) {}
53 __attribute__ ((weak))
54 void matrix_scan_user(void) {}
56 __attribute__ ((weak))
57 void matrix_init_kb(void) {
61 __attribute__ ((weak))
62 void matrix_scan_kb(void) {
67 uint8_t matrix_rows(void)
73 uint8_t matrix_cols(void)
78 void matrix_init(void)
87 // initialize matrix state: all keys off
88 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
90 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
91 debounce_matrix[i * MATRIX_COLS + j] = 0;
95 #ifdef DEBUG_MATRIX_SCAN_RATE
96 matrix_timer = timer_read32();
97 matrix_scan_count = 0;
100 matrix_init_quantum();
104 void matrix_power_up(void) {
108 // initialize matrix state: all keys off
109 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
113 #ifdef DEBUG_MATRIX_SCAN_RATE
114 matrix_timer = timer_read32();
115 matrix_scan_count = 0;
119 // Returns a matrix_row_t whose bits are set if the corresponding key should be
120 // eligible to change in this scan.
121 matrix_row_t debounce_mask(uint8_t row) {
122 matrix_row_t result = 0;
123 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
124 if (debounce_matrix[row * MATRIX_COLS + j]) {
125 --debounce_matrix[row * MATRIX_COLS + j];
133 // Report changed keys in the given row. Resets the debounce countdowns
134 // corresponding to each set bit in 'change' to DEBOUNCE.
135 void debounce_report(matrix_row_t change, uint8_t row) {
136 for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
137 if (change & (1 << i)) {
138 debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
143 uint8_t matrix_scan(void)
147 #ifdef DEBUG_MATRIX_SCAN_RATE
150 uint32_t timer_now = timer_read32();
151 if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
152 print("matrix scan frequency: ");
153 pdec(matrix_scan_count);
157 matrix_timer = timer_now;
158 matrix_scan_count = 0;
162 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
164 wait_us(30); // without this wait read unstable value.
165 matrix_row_t mask = debounce_mask(i);
166 matrix_row_t cols = (read_cols(i) & mask) | (matrix[i] & ~mask);
167 debounce_report(cols ^ matrix[i], i);
173 matrix_scan_quantum();
179 bool matrix_is_on(uint8_t row, uint8_t col)
181 return (matrix[row] & ((matrix_row_t)1<<col));
185 matrix_row_t matrix_get_row(uint8_t row)
190 void matrix_print(void)
192 print("\nr/c 0123456789ABCDEF\n");
193 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
194 phex(row); print(": ");
195 pbin_reverse16(matrix_get_row(row));
200 uint8_t matrix_key_count(void)
203 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
204 count += bitpop16(matrix[i]);
209 /* Column pin configuration
211 * Pro Micro: 6 5 4 3 2 1 0
212 * PD3 PD2 PD4 PC6 PD7 PE6 PB4
214 * Expander: 13 12 11 10 9 8 7
216 static void init_cols(void)
221 DDRD &= ~(1<<PD2 | 1<<PD3 | 1<<PD4 | 1<<PD7);
222 PORTD |= (1<<PD2 | 1<<PD3 | 1<<PD4 | 1<<PD7);
232 static matrix_row_t read_cols(uint8_t row)
234 return expander_read_row() |
235 (PIND&(1<<PD3) ? 0 : (1<<6)) |
236 (PIND&(1<<PD2) ? 0 : (1<<5)) |
237 (PIND&(1<<PD4) ? 0 : (1<<4)) |
238 (PINC&(1<<PC6) ? 0 : (1<<3)) |
239 (PIND&(1<<PD7) ? 0 : (1<<2)) |
240 (PINE&(1<<PE6) ? 0 : (1<<1)) |
241 (PINB&(1<<PB4) ? 0 : (1<<0)) ;
244 /* Row pin configuration
246 * Pro Micro: 0 1 2 3 4 5
249 * Expander: 0 1 2 3 4 5
251 static void unselect_rows(void)
254 DDRF &= ~(1<<PF4 | 1<<PF5 | 1<<PF6 | 1<<PF7);
255 PORTF &= ~(1<<PF4 | 1<<PF5 | 1<<PF6 | 1<<PF7);
256 DDRB &= ~(1<<PB1 | 1<<PB2);
257 PORTB &= ~(1<<PB1 | 1<<PB2);
260 expander_unselect_rows();
263 static void select_row(uint8_t row)
293 expander_select_row(row);