4 #include <util/delay.h>
10 #ifndef DEBOUNCING_DELAY
11 # define DEBOUNCING_DELAY 5
13 static uint8_t debouncing = DEBOUNCING_DELAY;
15 static matrix_row_t matrix[MATRIX_ROWS];
16 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
18 static uint8_t read_rows(void);
19 static void init_rows(void);
20 static void unselect_cols(void);
21 static void select_col(uint8_t col);
23 inline uint8_t matrix_rows(void) {
27 inline uint8_t matrix_cols(void) {
31 __attribute__ ((weak))
32 void matrix_init_kb(void) {
36 __attribute__ ((weak))
37 void matrix_scan_kb(void) {
41 __attribute__ ((weak))
42 void matrix_init_user(void) {
45 __attribute__ ((weak))
46 void matrix_scan_user(void) {
49 void matrix_init(void) {
50 // initialize row and col
54 // initialize matrix state: all keys off
55 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
57 matrix_debouncing[i] = 0;
59 matrix_init_quantum();
62 uint8_t matrix_scan(void) {
63 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
66 uint8_t rows = read_rows();
67 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
68 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
69 bool curr_bit = rows & (1<<row);
70 if (prev_bit != curr_bit) {
71 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
72 debouncing = DEBOUNCING_DELAY;
82 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
83 matrix[i] = matrix_debouncing[i];
88 matrix_scan_quantum();
92 bool matrix_is_modified(void) {
99 inline bool matrix_is_on(uint8_t row, uint8_t col) {
100 return (matrix[row] & ((matrix_row_t)1<<col));
103 inline matrix_row_t matrix_get_row(uint8_t row) {
107 void matrix_print(void) {
108 print("\nr/c 0123456789ABCDEF\n");
109 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
110 xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
114 uint8_t matrix_key_count(void) {
116 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
117 count += bitpop32(matrix[i]);
122 /* Row pin configuration
125 * pin: C7 B1 B2 C6 B4 B5
128 static void init_rows(void)
136 static uint8_t read_rows(void) {
137 return (PINC&(1<<PC7) ? 0 : (1<<0)) |
138 (PINB&(1<<PB1) ? 0 : (1<<1)) |
139 (PINB&(1<<PB2) ? 0 : (1<<2)) |
140 (PINC&(1<<PC6) ? 0 : (1<<3)) |
141 (PINB&(1<<PB4) ? 0 : (1<<4)) |
142 (PINB&(1<<PB5) ? 0 : (1<<5));
145 /* Row pin configuration
146 * pin: D3 D7 D6 D5 D4
165 static void unselect_cols(void)
167 // output high(DDR:1, PORT:1) to unselect
172 static void select_col(uint8_t col) {
173 DDRD |= (1<<PD3 | 1<<PD4 | 1<<PD5 | 1<<PD6 | 1<<PD7);