]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/ergodone/matrix.c
[Keymap] Mekberg kbd6x keymap (#7061)
[qmk_firmware.git] / keyboards / ergodone / matrix.c
1 #include <stdint.h>
2 #include <stdbool.h>
3 #include <avr/io.h>
4 #include "wait.h"
5 #include "action_layer.h"
6 #include "print.h"
7 #include "debug.h"
8 #include "util.h"
9 #include "matrix.h"
10 #include "ergodone.h"
11 #include "expander.h"
12
13 /*
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.
16  *
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.
20  *
21  * And so, there is no sense to have DEBOUNCE higher than 2.
22  */
23
24 #ifndef DEBOUNCE
25 #   define DEBOUNCE     5
26 #endif
27
28 /* matrix state(1:on, 0:off) */
29 static matrix_row_t matrix[MATRIX_ROWS];
30
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];
35
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);
40
41 __attribute__ ((weak))
42 void matrix_init_user(void) {}
43
44 __attribute__ ((weak))
45 void matrix_scan_user(void) {}
46
47 __attribute__ ((weak))
48 void matrix_init_kb(void) {
49   matrix_init_user();
50 }
51
52 __attribute__ ((weak))
53 void matrix_scan_kb(void) {
54   matrix_scan_user();
55 }
56
57 inline
58 uint8_t matrix_rows(void)
59 {
60   return MATRIX_ROWS;
61 }
62
63 inline
64 uint8_t matrix_cols(void)
65 {
66   return MATRIX_COLS;
67 }
68
69 void matrix_init(void)
70 {
71   unselect_rows();
72   init_cols();
73
74   // initialize matrix state: all keys off
75   for (uint8_t i=0; i < MATRIX_ROWS; i++) {
76     matrix[i] = 0;
77     for (uint8_t j=0; j < MATRIX_COLS; ++j) {
78       debounce_matrix[i * MATRIX_COLS + j] = 0;
79     }
80   }
81
82   matrix_init_quantum();
83 }
84
85 void matrix_power_up(void) {
86   unselect_rows();
87   init_cols();
88
89   // initialize matrix state: all keys off
90   for (uint8_t i=0; i < MATRIX_ROWS; i++) {
91     matrix[i] = 0;
92   }
93 }
94
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];
102     } else {
103       result |= (1 << j);
104     }
105   }
106   return result;
107 }
108
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;
115     }
116   }
117 }
118
119 uint8_t matrix_scan(void)
120 {
121   expander_scan();
122
123   for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
124     select_row(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);
129     matrix[i] = cols;
130
131     unselect_rows();
132   }
133
134   matrix_scan_quantum();
135
136   return 1;
137 }
138
139 inline
140 bool matrix_is_on(uint8_t row, uint8_t col)
141 {
142   return (matrix[row] & ((matrix_row_t)1<<col));
143 }
144
145 inline
146 matrix_row_t matrix_get_row(uint8_t row)
147 {
148   return matrix[row];
149 }
150
151 void matrix_print(void)
152 {
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));
157     print("\n");
158   }
159 }
160
161 uint8_t matrix_key_count(void)
162 {
163   uint8_t count = 0;
164   for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
165     count += bitpop16(matrix[i]);
166   }
167   return count;
168 }
169
170 /* Column pin configuration
171  *
172  * Pro Micro: 6    5    4    3    2    1    0
173  *            PD3  PD2  PD4  PC6  PD7  PE6  PB4
174  *
175  * Expander:  13   12   11   10   9    8    7
176  */
177 static void  init_cols(void)
178 {
179   // Pro Micro
180   DDRE  &= ~(1<<PE6);
181   PORTE |=  (1<<PE6);
182   DDRD  &= ~(1<<PD2 | 1<<PD3 | 1<<PD4 | 1<<PD7);
183   PORTD |=  (1<<PD2 | 1<<PD3 | 1<<PD4 | 1<<PD7);
184   DDRC  &= ~(1<<PC6);
185   PORTC |=  (1<<PC6);
186   DDRB  &= ~(1<<PB4);
187   PORTB |=  (1<<PB4);
188
189   // MCP23017
190   expander_init();
191 }
192
193 static matrix_row_t read_cols(uint8_t row)
194 {
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)) ;
203 }
204
205 /* Row pin configuration
206  *
207  * Pro Micro: 0   1   2   3   4   5
208  *            F4  F5  F6  F7  B1  B2
209  *
210  * Expander:  0   1   2   3   4   5
211  */
212 static void unselect_rows(void)
213 {
214   // Pro Micro
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);
219
220   // Expander
221   expander_unselect_rows();
222 }
223
224 static void select_row(uint8_t row)
225 {
226   // Pro Micro
227   switch (row) {
228   case 0:
229     DDRF  |=  (1<<PF4);
230     PORTF &= ~(1<<PF4);
231     break;
232   case 1:
233     DDRF  |=  (1<<PF5);
234     PORTF &= ~(1<<PF5);
235     break;
236   case 2:
237     DDRF  |=  (1<<PF6);
238     PORTF &= ~(1<<PF6);
239     break;
240   case 3:
241     DDRF  |=  (1<<PF7);
242     PORTF &= ~(1<<PF7);
243     break;
244   case 4:
245     DDRB  |=  (1<<PB1);
246     PORTB &= ~(1<<PB1);
247     break;
248   case 5:
249     DDRB  |=  (1<<PB2);
250     PORTB &= ~(1<<PB2);
251     break;
252   }
253
254   expander_select_row(row);
255 }
256