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 //eeprom_update_word(EECONFIG_MAGIC, 0x0000);
89 // initialize matrix state: all keys off
90 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
92 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
93 debounce_matrix[i * MATRIX_COLS + j] = 0;
97 #ifdef DEBUG_MATRIX_SCAN_RATE
98 matrix_timer = timer_read32();
99 matrix_scan_count = 0;
102 matrix_init_quantum();
106 void matrix_power_up(void) {
110 // initialize matrix state: all keys off
111 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
115 #ifdef DEBUG_MATRIX_SCAN_RATE
116 matrix_timer = timer_read32();
117 matrix_scan_count = 0;
121 // Returns a matrix_row_t whose bits are set if the corresponding key should be
122 // eligible to change in this scan.
123 matrix_row_t debounce_mask(uint8_t row) {
124 matrix_row_t result = 0;
125 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
126 if (debounce_matrix[row * MATRIX_COLS + j]) {
127 --debounce_matrix[row * MATRIX_COLS + j];
135 // Report changed keys in the given row. Resets the debounce countdowns
136 // corresponding to each set bit in 'change' to DEBOUNCE.
137 void debounce_report(matrix_row_t change, uint8_t row) {
138 for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
139 if (change & (1 << i)) {
140 debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
145 uint8_t matrix_scan(void)
149 #ifdef DEBUG_MATRIX_SCAN_RATE
152 uint32_t timer_now = timer_read32();
153 if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
154 print("matrix scan frequency: ");
155 pdec(matrix_scan_count);
159 matrix_timer = timer_now;
160 matrix_scan_count = 0;
163 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
165 wait_us(30); // without this wait read unstable value.
166 matrix_row_t mask = debounce_mask(i);
167 matrix_row_t cols = (read_cols(i) & mask) | (matrix[i] & ~mask);
168 debounce_report(cols ^ matrix[i], i);
174 matrix_scan_quantum();
180 bool matrix_is_on(uint8_t row, uint8_t col)
182 return (matrix[row] & ((matrix_row_t)1<<col));
186 matrix_row_t matrix_get_row(uint8_t row)
191 void matrix_print(void)
193 print("\nr/c 0123456789ABCDEF\n");
194 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
195 phex(row); print(": ");
196 pbin_reverse16(matrix_get_row(row));
201 uint8_t matrix_key_count(void)
204 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
205 count += bitpop16(matrix[i]);
210 static void init_cols(void)
213 DDRB &= ~(1<<PB0 | 1<<PB1 | 1<<PB2 | 1<<PB3);
214 PORTB |= (1<<PB0 | 1<<PB1 | 1<<PB2 | 1<<PB3);
216 DDRD &= ~(1<<PD2 | 1<<PD3);
217 PORTD |= (1<<PD2 | 1<<PD3);
225 static matrix_row_t read_cols(uint8_t row)
227 matrix_row_t cols0 = 0x00, cols1 = 0x00;
229 cols0 = left_read_cols();
231 cols1 = (PINC&(1<<PC6) ? 0 : (1<<(0+7))) |
232 (PIND&(1<<PD3) ? 0 : (1<<(1+7))) |
233 (PIND&(1<<PD2) ? 0 : (1<<(2+7))) |
234 (PINB&(1<<PB3) ? 0 : (1<<(3+7))) |
235 (PINB&(1<<PB2) ? 0 : (1<<(4+7))) |
236 (PINB&(1<<PB1) ? 0 : (1<<(5+7))) |
237 (PINB&(1<<PB0) ? 0 : (1<<(6+7))) ;
239 return (cols0 | cols1);
242 static void unselect_rows(void)
245 DDRF &= ~(1<<PF7 | 1<< PF6 | 1<<PF5 | 1<<PF4 | 1<<PF1 | 1<<PF0);
246 PORTF &= ~(1<<PF7 | 1<< PF6 | 1<<PF5 | 1<<PF4 | 1<<PF1 | 1<<PF0);
248 left_unselect_rows();
251 static void select_row(uint8_t row)
281 left_select_row(row);