]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/vitamins_included/matrix.c
fix DZ60 info.json (#7000)
[qmk_firmware.git] / keyboards / vitamins_included / matrix.c
1 /*
2 Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 /*
19  * scan matrix
20  */
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <avr/io.h>
24 #include <avr/interrupt.h>
25 #include <util/delay.h>
26 #include "wait.h"
27 #include "print.h"
28 #include "debug.h"
29 #include "util.h"
30 #include "matrix.h"
31 #include "split_util.h"
32 #include "pro_micro.h"
33 #include "config.h"
34 #include "timer.h"
35 #include <print.h>
36
37 #if (defined(RGB_MIDI) | defined(RGBLIGHT_ANIMATIONS)) & defined(RGBLIGHT_ENABLE)
38     #include "rgblight.h"
39 #endif
40
41
42 #ifdef USE_I2C
43 #  include "i2c.h"
44 #else // USE_SERIAL
45 #  include "serial.h"
46 #endif
47
48 #ifndef DEBOUNCE
49 #   define DEBOUNCE 5
50 #endif
51
52 #if (DEBOUNCE > 0)
53     static uint16_t debouncing_time;
54     static bool debouncing = false;
55 #endif
56
57 #if (MATRIX_COLS <= 8)
58 #    define print_matrix_header()  print("\nr/c 01234567\n")
59 #    define print_matrix_row(row)  print_bin_reverse8(matrix_get_row(row))
60 #    define matrix_bitpop(i)       bitpop(matrix[i])
61 #    define ROW_SHIFTER ((uint8_t)1)
62 #else
63 #    error "Currently only supports 8 COLS"
64 #endif
65 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
66
67 #define ERROR_DISCONNECT_COUNT 5
68
69 #define ROWS_PER_HAND (MATRIX_ROWS/2)
70
71 static uint8_t error_count = 0;
72
73 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
74 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
75
76 /* matrix state(1:on, 0:off) */
77 static matrix_row_t matrix[MATRIX_ROWS];
78 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
79
80 #if (DIODE_DIRECTION == COL2ROW)
81     static void init_cols(void);
82     static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
83     static void unselect_rows(void);
84     static void select_row(uint8_t row);
85     static void unselect_row(uint8_t row);
86 #elif (DIODE_DIRECTION == ROW2COL)
87     static void init_rows(void);
88     static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
89     static void unselect_cols(void);
90     static void unselect_col(uint8_t col);
91     static void select_col(uint8_t col);
92 #endif
93
94
95 __attribute__ ((weak))
96 void matrix_init_quantum(void) {
97     matrix_init_kb();
98 }
99
100 __attribute__ ((weak))
101 void matrix_scan_quantum(void) {
102     matrix_scan_kb();
103 }
104
105 __attribute__ ((weak))
106 void matrix_init_kb(void) {
107     matrix_init_user();
108 }
109
110 __attribute__ ((weak))
111 void matrix_scan_kb(void) {
112     matrix_scan_user();
113 }
114
115 __attribute__ ((weak))
116 void matrix_init_user(void) {
117 }
118
119 __attribute__ ((weak))
120 void matrix_scan_user(void) {
121 }
122
123 inline
124 uint8_t matrix_rows(void) {
125     return MATRIX_ROWS;
126 }
127
128 inline
129 uint8_t matrix_cols(void) {
130     return MATRIX_COLS;
131 }
132
133 bool has_usb(void) {
134     return UDADDR & _BV(ADDEN); // This will return true if a USB connection has been established
135 }
136
137 void matrix_init(void)
138 {
139     // initialize row and col
140 #if (DIODE_DIRECTION == COL2ROW)
141     unselect_rows();
142     init_cols();
143 #elif (DIODE_DIRECTION == ROW2COL)
144     unselect_cols();
145     init_rows();
146 #endif
147
148   TX_RX_LED_INIT;
149
150   // initialize matrix state: all keys off
151   for (uint8_t i=0; i < MATRIX_ROWS; i++) {
152     matrix[i] = 0;
153     matrix_debouncing[i] = 0;
154   }
155
156   #ifdef RGBLIGHT_ENABLE
157     rgblight_init();
158   #endif
159
160   timer_init();
161   #ifdef USE_I2C
162     i2c_slave_init(SLAVE_I2C_ADDRESS);
163   #else
164     serial_slave_init();
165   #endif
166
167   sei();
168
169   matrix_init_quantum();
170   while(!has_usb() || contacted_by_master){
171     matrix_slave_scan();
172   }
173
174   // Set up as master
175   #ifdef USE_I2C
176     i2c_reset_state();
177     i2c_master_init();
178   #else
179     serial_master_init();
180   #endif
181 }
182
183 uint8_t _matrix_scan(void)
184 {
185     int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
186 #if (DIODE_DIRECTION == COL2ROW)
187     // Set row, read cols
188     for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
189 #       if (DEBOUNCE > 0)
190             bool matrix_changed = read_cols_on_row(matrix_debouncing+offset, current_row);
191
192             if (matrix_changed) {
193                 debouncing = true;
194                 debouncing_time = timer_read();
195             }
196
197 #       else
198             read_cols_on_row(matrix+offset, current_row);
199 #       endif
200
201     }
202
203 #elif (DIODE_DIRECTION == ROW2COL)
204     // Set col, read rows
205     for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
206 #       if (DEBOUNCE > 0)
207             bool matrix_changed = read_rows_on_col(matrix_debouncing+offset, current_col);
208             if (matrix_changed) {
209                 debouncing = true;
210                 debouncing_time = timer_read();
211             }
212 #       else
213              read_rows_on_col(matrix+offset, current_col);
214 #       endif
215
216     }
217 #endif
218
219 #   if (DEBOUNCE > 0)
220         if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
221             for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
222                 matrix[i+offset] = matrix_debouncing[i+offset];
223             }
224             debouncing = false;
225         }
226 #   endif
227
228     return 1;
229 }
230
231 #ifdef USE_I2C
232
233 // Get rows from other half over i2c
234 int i2c_transaction(void) {
235     int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
236
237     int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
238     if (err) goto i2c_error;
239
240     // start of matrix stored at 0x00
241     err = i2c_master_write(0x00);
242     if (err) goto i2c_error;
243
244     // Start read
245     err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
246     if (err) goto i2c_error;
247
248     if (!err) {
249         int i;
250         for (i = 0; i < ROWS_PER_HAND-1; ++i) {
251             matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
252         }
253         matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
254         i2c_master_stop();
255     } else {
256 i2c_error: // the cable is disconnceted, or something else went wrong
257         i2c_reset_state();
258         return err;
259     }
260
261     return 0;
262 }
263
264 #else // USE_SERIAL
265
266 int serial_transaction(void) {
267     int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
268
269     if (serial_update_buffers()) {
270         return 1;
271     }
272
273     for (int i = 0; i < ROWS_PER_HAND; ++i) {
274         matrix[slaveOffset+i] = serial_slave_buffer[i];
275     }
276     return 0;
277 }
278 #endif
279
280 uint8_t matrix_scan(void)
281 {
282     uint8_t ret = _matrix_scan();
283
284 #ifdef USE_I2C
285     if( i2c_transaction() ) {
286 #else // USE_SERIAL
287     if( serial_transaction() ) {
288 #endif
289         // turn on the indicator led when halves are disconnected
290         TXLED1;
291
292         error_count++;
293
294         if (error_count > ERROR_DISCONNECT_COUNT) {
295             // reset other half if disconnected
296             int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
297             for (int i = 0; i < ROWS_PER_HAND; ++i) {
298                 matrix[slaveOffset+i] = 0;
299             }
300         }
301     } else {
302         // turn off the indicator led on no error
303         TXLED0;
304         error_count = 0;
305     }
306     matrix_scan_quantum();
307     return ret;
308 }
309
310 void matrix_slave_scan(void) {
311   #if defined(RGBLIGHT_ANIMATIONS) & defined(RGBLIGHT_ENABLE)
312     rgblight_task();
313   #endif
314   _matrix_scan();
315
316   int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
317
318 #ifdef USE_I2C
319     for (int i = 0; i < ROWS_PER_HAND; ++i) {
320         i2c_slave_buffer[i] = matrix[offset+i];
321     }
322 #else // USE_SERIAL
323     for (int i = 0; i < ROWS_PER_HAND; ++i) {
324         serial_slave_buffer[i] = matrix[offset+i];
325     }
326 #endif
327 }
328
329 bool matrix_is_modified(void)
330 {
331     if (debouncing) return false;
332     return true;
333 }
334
335 inline
336 bool matrix_is_on(uint8_t row, uint8_t col)
337 {
338     return (matrix[row] & ((matrix_row_t)1<<col));
339 }
340
341 inline
342 matrix_row_t matrix_get_row(uint8_t row)
343 {
344     return matrix[row];
345 }
346
347 void matrix_print(void)
348 {
349     print("\nr/c 0123456789ABCDEF\n");
350     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
351         phex(row); print(": ");
352         pbin_reverse16(matrix_get_row(row));
353         print("\n");
354     }
355 }
356
357 uint8_t matrix_key_count(void)
358 {
359     uint8_t count = 0;
360     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
361         count += bitpop16(matrix[i]);
362     }
363     return count;
364 }
365
366 #if (DIODE_DIRECTION == COL2ROW)
367
368 static void init_cols(void)
369 {
370     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
371         uint8_t pin = col_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
377 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
378 {
379     // Store last value of row prior to reading
380     matrix_row_t last_row_value = current_matrix[current_row];
381
382     // Clear data in matrix row
383     current_matrix[current_row] = 0;
384
385     // Select row and wait for row selecton to stabilize
386     select_row(current_row);
387     wait_us(30);
388
389     // For each col...
390     for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
391
392         // Select the col pin to read (active low)
393         uint8_t pin = col_pins[col_index];
394         uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
395
396         // Populate the matrix row with the state of the col pin
397         current_matrix[current_row] |=  pin_state ? 0 : (ROW_SHIFTER << col_index);
398     }
399
400     // Unselect row
401     unselect_row(current_row);
402
403     return (last_row_value != current_matrix[current_row]);
404 }
405
406 static void select_row(uint8_t row)
407 {
408     uint8_t pin = row_pins[row];
409     _SFR_IO8((pin >> 4) + 1) |=  _BV(pin & 0xF); // OUT
410     _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
411 }
412
413 static void unselect_row(uint8_t row)
414 {
415     uint8_t pin = row_pins[row];
416     _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
417     _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
418 }
419
420 static void unselect_rows(void)
421 {
422     for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
423         uint8_t pin = row_pins[x];
424         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
425         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
426     }
427 }
428
429 #elif (DIODE_DIRECTION == ROW2COL)
430
431 static void init_rows(void)
432 {
433     for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
434         uint8_t pin = row_pins[x];
435         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
436         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
437     }
438 }
439
440 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
441 {
442     bool matrix_changed = false;
443
444     // Select col and wait for col selecton to stabilize
445     select_col(current_col);
446     wait_us(30);
447
448     // For each row...
449     for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++)
450     {
451
452         // Store last value of row prior to reading
453         matrix_row_t last_row_value = current_matrix[row_index];
454
455         // Check row pin state
456         if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
457         {
458             // Pin LO, set col bit
459             current_matrix[row_index] |= (ROW_SHIFTER << current_col);
460         }
461         else
462         {
463             // Pin HI, clear col bit
464             current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
465         }
466
467         // Determine if the matrix changed state
468         if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
469         {
470             matrix_changed = true;
471         }
472     }
473
474     // Unselect col
475     unselect_col(current_col);
476
477     return matrix_changed;
478 }
479
480 static void select_col(uint8_t col)
481 {
482     uint8_t pin = col_pins[col];
483     _SFR_IO8((pin >> 4) + 1) |=  _BV(pin & 0xF); // OUT
484     _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
485 }
486
487 static void unselect_col(uint8_t col)
488 {
489     uint8_t pin = col_pins[col];
490     _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
491     _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
492 }
493
494 static void unselect_cols(void)
495 {
496     for(uint8_t x = 0; x < MATRIX_COLS; x++) {
497         uint8_t pin = col_pins[x];
498         _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
499         _SFR_IO8((pin >> 4) + 2) |=  _BV(pin & 0xF); // HI
500     }
501 }
502
503 #endif