]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/hotdox/matrix.c
Remove superfluous JTAG disable code (#6445)
[qmk_firmware.git] / keyboards / hotdox / 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 "hotdox.h"
11 #include "left.h"
12 #ifdef DEBUG_MATRIX_SCAN_RATE
13 #include  "timer.h"
14 #endif
15
16 /*
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.
19  *
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.
23  *
24  * And so, there is no sense to have DEBOUNCE higher than 2.
25  */
26
27 #ifndef DEBOUNCE
28 #   define DEBOUNCE     5
29 #endif
30
31 /* matrix state(1:on, 0:off) */
32 static matrix_row_t matrix[MATRIX_ROWS];
33
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];
38
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);
43
44 #ifdef DEBUG_MATRIX_SCAN_RATE
45 uint32_t matrix_timer;
46 uint32_t matrix_scan_count;
47 #endif
48
49
50 __attribute__ ((weak))
51 void matrix_init_user(void) {}
52
53 __attribute__ ((weak))
54 void matrix_scan_user(void) {}
55
56 __attribute__ ((weak))
57 void matrix_init_kb(void) {
58   matrix_init_user();
59 }
60
61 __attribute__ ((weak))
62 void matrix_scan_kb(void) {
63   matrix_scan_user();
64 }
65
66 inline
67 uint8_t matrix_rows(void)
68 {
69   return MATRIX_ROWS;
70 }
71
72 inline
73 uint8_t matrix_cols(void)
74 {
75   return MATRIX_COLS;
76 }
77
78 void matrix_init(void)
79 {
80   unselect_rows();
81   init_cols();
82
83   //eeprom_update_word(EECONFIG_MAGIC, 0x0000);
84
85   // initialize matrix state: all keys off
86   for (uint8_t i=0; i < MATRIX_ROWS; i++) {
87     matrix[i] = 0;
88     for (uint8_t j=0; j < MATRIX_COLS; ++j) {
89       debounce_matrix[i * MATRIX_COLS + j] = 0;
90     }
91   }
92
93 #ifdef DEBUG_MATRIX_SCAN_RATE
94   matrix_timer = timer_read32();
95   matrix_scan_count = 0;
96 #endif
97
98   matrix_init_quantum();
99
100 }
101
102 void matrix_power_up(void) {
103   unselect_rows();
104   init_cols();
105
106   // initialize matrix state: all keys off
107   for (uint8_t i=0; i < MATRIX_ROWS; i++) {
108     matrix[i] = 0;
109   }
110
111 #ifdef DEBUG_MATRIX_SCAN_RATE
112   matrix_timer = timer_read32();
113   matrix_scan_count = 0;
114 #endif
115 }
116
117 // Returns a matrix_row_t whose bits are set if the corresponding key should be
118 // eligible to change in this scan.
119 matrix_row_t debounce_mask(uint8_t row) {
120   matrix_row_t result = 0;
121   for (uint8_t j=0; j < MATRIX_COLS; ++j) {
122     if (debounce_matrix[row * MATRIX_COLS + j]) {
123       --debounce_matrix[row * MATRIX_COLS + j];
124     } else {
125       result |= (1 << j);
126     }
127   }
128   return result;
129 }
130
131 // Report changed keys in the given row.  Resets the debounce countdowns
132 // corresponding to each set bit in 'change' to DEBOUNCE.
133 void debounce_report(matrix_row_t change, uint8_t row) {
134   for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
135     if (change & (1 << i)) {
136       debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
137     }
138   }
139 }
140
141 uint8_t matrix_scan(void)
142 {
143   left_scan();
144
145 #ifdef DEBUG_MATRIX_SCAN_RATE
146   matrix_scan_count++;
147
148   uint32_t timer_now = timer_read32();
149   if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
150     print("matrix scan frequency: ");
151     pdec(matrix_scan_count);
152     print("\n");
153     matrix_print();
154
155     matrix_timer = timer_now;
156     matrix_scan_count = 0;
157   }
158 #endif
159   for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
160     select_row(i);
161     wait_us(30);  // without this wait read unstable value.
162     matrix_row_t mask = debounce_mask(i);
163     matrix_row_t cols = (read_cols(i) & mask) | (matrix[i] & ~mask);
164     debounce_report(cols ^ matrix[i], i);
165     matrix[i] = cols;
166
167     unselect_rows();
168   }
169
170   matrix_scan_quantum();
171
172   return 1;
173 }
174
175 inline
176 bool matrix_is_on(uint8_t row, uint8_t col)
177 {
178   return (matrix[row] & ((matrix_row_t)1<<col));
179 }
180
181 inline
182 matrix_row_t matrix_get_row(uint8_t row)
183 {
184   return matrix[row];
185 }
186
187 void matrix_print(void)
188 {
189   print("\nr/c 0123456789ABCDEF\n");
190   for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
191     phex(row); print(": ");
192     pbin_reverse16(matrix_get_row(row));
193     print("\n");
194   }
195 }
196
197 uint8_t matrix_key_count(void)
198 {
199   uint8_t count = 0;
200   for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
201     count += bitpop16(matrix[i]);
202   }
203   return count;
204 }
205
206 static void init_cols(void)
207 {
208   // Pro Micro
209   DDRB  &= ~(1<<PB0 | 1<<PB1 | 1<<PB2 | 1<<PB3);
210   PORTB |=  (1<<PB0 | 1<<PB1 | 1<<PB2 | 1<<PB3);
211
212   DDRD  &= ~(1<<PD2 | 1<<PD3);
213   PORTD |=  (1<<PD2 | 1<<PD3);
214
215   DDRC  &= ~(1<<PC6);
216   PORTC |=  (1<<PC6);
217
218   left_init();
219 }
220
221 static matrix_row_t read_cols(uint8_t row)
222 {
223   matrix_row_t cols0 = 0x00, cols1 = 0x00;
224
225   cols0 = left_read_cols();
226
227   cols1 = (PINC&(1<<PC6) ? 0 : (1<<(0+7))) |
228           (PIND&(1<<PD3) ? 0 : (1<<(1+7))) |
229           (PIND&(1<<PD2) ? 0 : (1<<(2+7))) |
230           (PINB&(1<<PB3) ? 0 : (1<<(3+7))) |
231           (PINB&(1<<PB2) ? 0 : (1<<(4+7))) |
232           (PINB&(1<<PB1) ? 0 : (1<<(5+7))) |
233           (PINB&(1<<PB0) ? 0 : (1<<(6+7))) ;
234
235   return (cols0 | cols1);
236 }
237
238 static void unselect_rows(void)
239 {
240   // Pro Micro
241   DDRF  &= ~(1<<PF7 | 1<< PF6 | 1<<PF5 | 1<<PF4 | 1<<PF1 | 1<<PF0);
242   PORTF &= ~(1<<PF7 | 1<< PF6 | 1<<PF5 | 1<<PF4 | 1<<PF1 | 1<<PF0);
243
244   left_unselect_rows();
245 }
246
247 static void select_row(uint8_t row)
248 {
249   // Pro Micro
250   switch (row) {
251   case 5:
252     DDRF  |=  (1<<PF0);
253     PORTF &= ~(1<<PF0);
254     break;
255   case 4:
256     DDRF  |=  (1<<PF1);
257     PORTF &= ~(1<<PF1);
258     break;
259   case 3:
260     DDRF  |=  (1<<PF4);
261     PORTF &= ~(1<<PF4);
262     break;
263   case 2:
264     DDRF  |=  (1<<PF5);
265     PORTF &= ~(1<<PF5);
266     break;
267   case 1:
268     DDRF  |=  (1<<PF6);
269     PORTF &= ~(1<<PF6);
270     break;
271   case 0:
272     DDRF  |=  (1<<PF7);
273     PORTF &= ~(1<<PF7);
274     break;
275   }
276
277   left_select_row(row);
278 }
279