aboutsummaryrefslogtreecommitdiff
path: root/keyboards/hotdox/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/hotdox/matrix.c')
-rw-r--r--keyboards/hotdox/matrix.c283
1 files changed, 283 insertions, 0 deletions
diff --git a/keyboards/hotdox/matrix.c b/keyboards/hotdox/matrix.c
new file mode 100644
index 000000000..9d3a98db2
--- /dev/null
+++ b/keyboards/hotdox/matrix.c
@@ -0,0 +1,283 @@
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) */
32static 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.
37static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
38
39static matrix_row_t read_cols(uint8_t row);
40static void init_cols(void);
41static void unselect_rows(void);
42static void select_row(uint8_t row);
43
44#ifdef DEBUG_MATRIX_SCAN_RATE
45uint32_t matrix_timer;
46uint32_t matrix_scan_count;
47#endif
48
49
50__attribute__ ((weak))
51void matrix_init_user(void) {}
52
53__attribute__ ((weak))
54void matrix_scan_user(void) {}
55
56__attribute__ ((weak))
57void matrix_init_kb(void) {
58 matrix_init_user();
59}
60
61__attribute__ ((weak))
62void matrix_scan_kb(void) {
63 matrix_scan_user();
64}
65
66inline
67uint8_t matrix_rows(void)
68{
69 return MATRIX_ROWS;
70}
71
72inline
73uint8_t matrix_cols(void)
74{
75 return MATRIX_COLS;
76}
77
78void matrix_init(void)
79{
80 // disable JTAG
81 MCUCR = (1<<JTD);
82 MCUCR = (1<<JTD);
83
84 unselect_rows();
85 init_cols();
86
87 //eeprom_update_word(EECONFIG_MAGIC, 0x0000);
88
89 // initialize matrix state: all keys off
90 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
91 matrix[i] = 0;
92 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
93 debounce_matrix[i * MATRIX_COLS + j] = 0;
94 }
95 }
96
97#ifdef DEBUG_MATRIX_SCAN_RATE
98 matrix_timer = timer_read32();
99 matrix_scan_count = 0;
100#endif
101
102 matrix_init_quantum();
103
104}
105
106void matrix_power_up(void) {
107 unselect_rows();
108 init_cols();
109
110 // initialize matrix state: all keys off
111 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
112 matrix[i] = 0;
113 }
114
115#ifdef DEBUG_MATRIX_SCAN_RATE
116 matrix_timer = timer_read32();
117 matrix_scan_count = 0;
118#endif
119}
120
121// Returns a matrix_row_t whose bits are set if the corresponding key should be
122// eligible to change in this scan.
123matrix_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];
128 } else {
129 result |= (1 << j);
130 }
131 }
132 return result;
133}
134
135// Report changed keys in the given row. Resets the debounce countdowns
136// corresponding to each set bit in 'change' to DEBOUNCE.
137void 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;
141 }
142 }
143}
144
145uint8_t matrix_scan(void)
146{
147 left_scan();
148
149#ifdef DEBUG_MATRIX_SCAN_RATE
150 matrix_scan_count++;
151
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);
156 print("\n");
157 matrix_print();
158
159 matrix_timer = timer_now;
160 matrix_scan_count = 0;
161 }
162#endif
163 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
164 select_row(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);
169 matrix[i] = cols;
170
171 unselect_rows();
172 }
173
174 matrix_scan_quantum();
175
176 return 1;
177}
178
179inline
180bool matrix_is_on(uint8_t row, uint8_t col)
181{
182 return (matrix[row] & ((matrix_row_t)1<<col));
183}
184
185inline
186matrix_row_t matrix_get_row(uint8_t row)
187{
188 return matrix[row];
189}
190
191void matrix_print(void)
192{
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));
197 print("\n");
198 }
199}
200
201uint8_t matrix_key_count(void)
202{
203 uint8_t count = 0;
204 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
205 count += bitpop16(matrix[i]);
206 }
207 return count;
208}
209
210static void init_cols(void)
211{
212 // Pro Micro
213 DDRB &= ~(1<<PB0 | 1<<PB1 | 1<<PB2 | 1<<PB3);
214 PORTB |= (1<<PB0 | 1<<PB1 | 1<<PB2 | 1<<PB3);
215
216 DDRD &= ~(1<<PD2 | 1<<PD3);
217 PORTD |= (1<<PD2 | 1<<PD3);
218
219 DDRC &= ~(1<<PC6);
220 PORTC |= (1<<PC6);
221
222 left_init();
223}
224
225static matrix_row_t read_cols(uint8_t row)
226{
227 matrix_row_t cols0 = 0x00, cols1 = 0x00;
228
229 cols0 = left_read_cols();
230
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))) ;
238
239 return (cols0 | cols1);
240}
241
242static void unselect_rows(void)
243{
244 // Pro Micro
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);
247
248 left_unselect_rows();
249}
250
251static void select_row(uint8_t row)
252{
253 // Pro Micro
254 switch (row) {
255 case 5:
256 DDRF |= (1<<PF0);
257 PORTF &= ~(1<<PF0);
258 break;
259 case 4:
260 DDRF |= (1<<PF1);
261 PORTF &= ~(1<<PF1);
262 break;
263 case 3:
264 DDRF |= (1<<PF4);
265 PORTF &= ~(1<<PF4);
266 break;
267 case 2:
268 DDRF |= (1<<PF5);
269 PORTF &= ~(1<<PF5);
270 break;
271 case 1:
272 DDRF |= (1<<PF6);
273 PORTF &= ~(1<<PF6);
274 break;
275 case 0:
276 DDRF |= (1<<PF7);
277 PORTF &= ~(1<<PF7);
278 break;
279 }
280
281 left_select_row(row);
282}
283