aboutsummaryrefslogtreecommitdiff
path: root/keyboards/handwired/xealous/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/handwired/xealous/matrix.c')
-rw-r--r--keyboards/handwired/xealous/matrix.c329
1 files changed, 8 insertions, 321 deletions
diff --git a/keyboards/handwired/xealous/matrix.c b/keyboards/handwired/xealous/matrix.c
index 27fad0008..e2d219939 100644
--- a/keyboards/handwired/xealous/matrix.c
+++ b/keyboards/handwired/xealous/matrix.c
@@ -34,311 +34,21 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
34 #include "matrix_scanrate.h" 34 #include "matrix_scanrate.h"
35#endif 35#endif
36 36
37 37void matrix_scan_user(void)
38
39#ifdef USE_I2C
40# include "i2c.h"
41#else // USE_SERIAL
42# error "only i2c supported"
43#endif
44
45#ifndef DEBOUNCING_DELAY
46# define DEBOUNCING_DELAY 5
47#endif
48
49
50#if (MATRIX_COLS <= 8)
51# define print_matrix_header() print("\nr/c 01234567\n")
52# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
53# define matrix_bitpop(i) bitpop(matrix[i])
54# define ROW_SHIFTER ((uint8_t)1)
55#else
56# error "Currently only supports 8 COLS"
57#endif
58
59#define ERROR_DISCONNECT_COUNT 5
60
61#define ROWS_PER_HAND (MATRIX_ROWS/2)
62
63static uint8_t error_count = 0;
64
65static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
66static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
67
68/* matrix state(1:on, 0:off) */
69static matrix_row_t matrix[MATRIX_ROWS];
70static matrix_row_t matrix_debouncing[MATRIX_ROWS];
71static matrix_row_t* debouncing_matrix_hand_offsetted; //pointer to matrix_debouncing for our hand
72static matrix_row_t* matrix_hand_offsetted; // pointer to matrix for our hand
73
74//Debouncing counters
75typedef uint8_t debounce_counter_t;
76#define DEBOUNCE_COUNTER_MODULO 250
77#define DEBOUNCE_COUNTER_INACTIVE 251
78static debounce_counter_t debounce_counters[MATRIX_ROWS * MATRIX_COLS];
79static debounce_counter_t *debounce_counters_hand_offsetted;
80
81
82#if (DIODE_DIRECTION == ROW2COL)
83 error "Only Col2Row supported";
84#endif
85static void init_cols(void);
86static void unselect_rows(void);
87static void select_row(uint8_t row);
88static void unselect_row(uint8_t row);
89static matrix_row_t optimized_col_reader(void);
90
91__attribute__ ((weak))
92void matrix_init_kb(void) {
93 matrix_init_user();
94}
95
96__attribute__ ((weak))
97void matrix_scan_kb(void) {
98 matrix_scan_user();
99}
100
101__attribute__ ((weak))
102void matrix_init_user(void) {
103}
104
105__attribute__ ((weak))
106void matrix_scan_user(void) {
107}
108
109__attribute__ ((weak))
110void matrix_slave_scan_user(void) {
111}
112
113inline
114uint8_t matrix_rows(void)
115{
116 return MATRIX_ROWS;
117}
118
119inline
120uint8_t matrix_cols(void)
121{
122 return MATRIX_COLS;
123}
124
125void matrix_init(void)
126{
127#ifdef DISABLE_JTAG
128 // JTAG disable for PORT F. write JTD bit twice within four cycles.
129 MCUCR |= (1<<JTD);
130 MCUCR |= (1<<JTD);
131#endif
132
133 debug_enable = true;
134 debug_matrix = false;
135 debug_mouse = false;
136 // initialize row and col
137 unselect_rows();
138 init_cols();
139
140 TX_RX_LED_INIT;
141
142 // initialize matrix state: all keys off
143 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
144 matrix[i] = 0;
145 matrix_debouncing[i] = 0;
146 }
147 int my_hand_offset = isLeftHand ? 0 : (ROWS_PER_HAND);
148 debouncing_matrix_hand_offsetted = matrix_debouncing + my_hand_offset;
149 matrix_hand_offsetted = matrix + my_hand_offset;
150 debounce_counters_hand_offsetted = debounce_counters + my_hand_offset;
151
152 for (uint8_t i = 0; i < MATRIX_ROWS * MATRIX_COLS; i++) {
153 debounce_counters[i] = DEBOUNCE_COUNTER_INACTIVE;
154 }
155
156 matrix_init_quantum();
157
158}
159
160//#define TIMER_DIFF(a, b, max) ((a) >= (b) ? (a) - (b) : (max) - (b) + (a))
161void update_debounce_counters(uint8_t current_time)
162{
163 debounce_counter_t *debounce_pointer = debounce_counters_hand_offsetted;
164 for (uint8_t row = 0; row < ROWS_PER_HAND; row++)
165 {
166 for (uint8_t col = 0; col < MATRIX_COLS; col++)
167 {
168 if (*debounce_pointer != DEBOUNCE_COUNTER_INACTIVE)
169 {
170 if (TIMER_DIFF(current_time, *debounce_pointer, DEBOUNCE_COUNTER_MODULO) >=
171 DEBOUNCING_DELAY) {
172 *debounce_pointer = DEBOUNCE_COUNTER_INACTIVE;
173 }
174 }
175 debounce_pointer++;
176 }
177 }
178}
179
180void transfer_matrix_values(uint8_t current_time)
181{
182 //upload from debounce_matrix to final matrix;
183 debounce_counter_t *debounce_pointer = debounce_counters_hand_offsetted;
184 for (uint8_t row = 0; row < ROWS_PER_HAND; row++)
185 {
186 matrix_row_t row_value = matrix_hand_offsetted[row];
187 matrix_row_t debounce_value = debouncing_matrix_hand_offsetted[row];
188
189 for (uint8_t col = 0; col < MATRIX_COLS; col++)
190 {
191 bool final_value = debounce_value & (1 << col);
192 bool current_value = row_value & (1 << col);
193 if (*debounce_pointer == DEBOUNCE_COUNTER_INACTIVE
194 && (current_value != final_value))
195 {
196 *debounce_pointer = current_time;
197 row_value ^= (1 << col);
198 }
199 debounce_pointer++;
200 }
201 matrix_hand_offsetted[row] = row_value;
202 }
203}
204
205uint8_t _matrix_scan(void)
206{
207 uint8_t current_time = timer_read() % DEBOUNCE_COUNTER_MODULO;
208
209 // Set row, read cols
210 for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
211 select_row(current_row);
212 asm volatile ("nop"); asm volatile("nop");
213
214 debouncing_matrix_hand_offsetted[current_row] = optimized_col_reader();
215 // Unselect row
216 unselect_row(current_row);
217 }
218
219 update_debounce_counters(current_time);
220 transfer_matrix_values(current_time);
221
222 return 1;
223}
224
225
226// Get rows from other half over i2c
227int i2c_transaction(void) {
228 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
229
230 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
231 if (err) goto i2c_error;
232
233 // start of matrix stored at 0x00
234 err = i2c_master_write(I2C_KEYMAP_START);
235 if (err) goto i2c_error;
236
237 // Start read
238 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
239 if (err) goto i2c_error;
240
241 if (!err) {
242 int i;
243 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
244 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
245 }
246 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
247 i2c_master_stop();
248 } else {
249i2c_error: // the cable is disconnceted, or something else went wrong
250 i2c_reset_state();
251 return err;
252 }
253
254 return 0;
255}
256
257uint8_t matrix_scan(void)
258{ 38{
259#ifdef DEBUG_MATRIX_SCAN_RATE 39#ifdef DEBUG_MATRIX_SCAN_RATE
260 matrix_check_scan_rate(); 40 matrix_check_scan_rate();
261 matrix_time_between_scans(); 41 matrix_time_between_scans();
262#endif 42#endif
263 uint8_t ret = _matrix_scan(); 43
264
265 if( i2c_transaction() ) {
266 // turn on the indicator led when halves are disconnected
267 TXLED1;
268
269 error_count++;
270
271 if (error_count > ERROR_DISCONNECT_COUNT) {
272 // reset other half if disconnected
273 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
274 for (int i = 0; i < ROWS_PER_HAND; ++i) {
275 matrix[slaveOffset+i] = 0;
276 }
277 }
278 } else {
279 // turn off the indicator led on no error
280 TXLED0;
281 error_count = 0;
282 }
283 matrix_scan_quantum();
284 return ret;
285}
286
287void matrix_slave_scan(void) {
288 _matrix_scan();
289
290 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
291
292 for (int i = 0; i < ROWS_PER_HAND; ++i) {
293 i2c_slave_buffer[I2C_KEYMAP_START+i] = matrix[offset+i];
294 }
295
296 matrix_slave_scan_user();
297}
298
299inline
300bool matrix_is_on(uint8_t row, uint8_t col)
301{
302 return (matrix[row] & ((matrix_row_t)1<<col));
303}
304
305
306inline
307matrix_row_t matrix_get_row(uint8_t row)
308{
309 return matrix[row];
310}
311
312void matrix_print(void)
313{
314 print("\nr/c 0123456789ABCDEF\n");
315 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
316 phex(row); print(": ");
317 pbin_reverse16(matrix_get_row(row));
318 print("\n");
319 }
320}
321
322uint8_t matrix_key_count(void)
323{
324 uint8_t count = 0;
325 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
326 count += bitpop16(matrix[i]);
327 }
328 return count;
329}
330
331static void init_cols(void)
332{
333 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
334 uint8_t pin = col_pins[x];
335 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
336 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
337 }
338} 44}
339 45
340inline 46// Copy this code to split_common/matrix.c,
341static matrix_row_t optimized_col_reader(void) { 47// and call it instead of the unoptimized col_reader. Scan-rate jumps from 1200->1920
48// Also remove the sleep_us(30), not necessary for this keyboard.
49// In usb_descriptor.c, set .PollingIntervalMS = 0x01
50#define ROW_SHIFTER ((uint8_t)1)
51inline static matrix_row_t optimized_col_reader(void) {
342 //MATRIX_COL_PINS { B6, B2, B3, B1, F7, F6, F5, F4 } 52 //MATRIX_COL_PINS { B6, B2, B3, B1, F7, F6, F5, F4 }
343 return (PINB & (1 << 6) ? 0 : (ROW_SHIFTER << 0)) | 53 return (PINB & (1 << 6) ? 0 : (ROW_SHIFTER << 0)) |
344 (PINB & (1 << 2) ? 0 : (ROW_SHIFTER << 1)) | 54 (PINB & (1 << 2) ? 0 : (ROW_SHIFTER << 1)) |
@@ -351,26 +61,3 @@ static matrix_row_t optimized_col_reader(void) {
351} 61}
352 62
353 63
354static void select_row(uint8_t row)
355{
356 uint8_t pin = row_pins[row];
357 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
358 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
359}
360
361static void unselect_row(uint8_t row)
362{
363 uint8_t pin = row_pins[row];
364 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
365 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
366}
367
368static void unselect_rows(void)
369{
370 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
371 uint8_t pin = row_pins[x];
372 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
373 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
374 }
375}
376