]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/handwired/xealous/matrix.c
Make quantum/split_common/serial.[ch] configurable (#4419)
[qmk_firmware.git] / keyboards / handwired / xealous / 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 "wait.h"
25 #include "print.h"
26 #include "debug.h"
27 #include "util.h"
28 #include "matrix.h"
29 #include "split_util.h"
30 #include "pro_micro.h"
31 #include "config.h"
32 #include "timer.h"
33 #ifdef DEBUG_MATRIX_SCAN_RATE
34     #include "matrix_scanrate.h"
35 #endif
36
37
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
63 static uint8_t error_count = 0;
64
65 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
66 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
67
68 /* matrix state(1:on, 0:off) */
69 static matrix_row_t matrix[MATRIX_ROWS];
70 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
71 static matrix_row_t* debouncing_matrix_hand_offsetted; //pointer to matrix_debouncing for our hand
72 static matrix_row_t* matrix_hand_offsetted; // pointer to matrix for our hand
73
74 //Debouncing counters
75 typedef uint8_t debounce_counter_t;
76 #define DEBOUNCE_COUNTER_MODULO 250
77 #define DEBOUNCE_COUNTER_INACTIVE 251
78 static debounce_counter_t debounce_counters[MATRIX_ROWS * MATRIX_COLS];
79 static debounce_counter_t *debounce_counters_hand_offsetted;
80
81
82 #if (DIODE_DIRECTION == ROW2COL)
83     error "Only Col2Row supported";
84 #endif
85 static void init_cols(void);
86 static void unselect_rows(void);
87 static void select_row(uint8_t row);
88 static void unselect_row(uint8_t row);
89 static matrix_row_t optimized_col_reader(void);
90
91 __attribute__ ((weak))
92 void matrix_init_kb(void) {
93     matrix_init_user();
94 }
95
96 __attribute__ ((weak))
97 void matrix_scan_kb(void) {
98     matrix_scan_user();
99 }
100
101 __attribute__ ((weak))
102 void matrix_init_user(void) {
103 }
104
105 __attribute__ ((weak))
106 void matrix_scan_user(void) {
107 }
108
109 __attribute__ ((weak))
110 void matrix_slave_scan_user(void) {
111 }
112
113 inline
114 uint8_t matrix_rows(void)
115 {
116     return MATRIX_ROWS;
117 }
118
119 inline
120 uint8_t matrix_cols(void)
121 {
122     return MATRIX_COLS;
123 }
124
125 void 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))
161 void 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
180 void 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
205 uint8_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
227 int 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 {
249 i2c_error: // the cable is disconnceted, or something else went wrong
250         i2c_reset_state();
251         return err;
252     }
253
254     return 0;
255 }
256
257 uint8_t matrix_scan(void)
258 {    
259 #ifdef DEBUG_MATRIX_SCAN_RATE
260     matrix_check_scan_rate();
261     matrix_time_between_scans();
262 #endif        
263     uint8_t ret = _matrix_scan();
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
287 void 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
299 inline
300 bool matrix_is_on(uint8_t row, uint8_t col)
301 {
302     return (matrix[row] & ((matrix_row_t)1<<col));
303 }
304
305
306 inline
307 matrix_row_t matrix_get_row(uint8_t row)
308 {
309     return matrix[row];
310 }
311
312 void 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
322 uint8_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
331 static 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 }
339
340 inline 
341 static matrix_row_t optimized_col_reader(void) {
342     //MATRIX_COL_PINS { B6, B2, B3, B1, F7, F6, F5, F4 }
343     return (PINB & (1 << 6) ? 0 : (ROW_SHIFTER << 0)) |
344           (PINB & (1 << 2) ? 0 : (ROW_SHIFTER << 1)) |
345           (PINB & (1 << 3) ? 0 : (ROW_SHIFTER << 2)) |
346           (PINB & (1 << 1) ? 0 : (ROW_SHIFTER << 3)) |
347           (PINF & (1 << 7) ? 0 : (ROW_SHIFTER << 4)) |
348           (PINF & (1 << 6) ? 0 : (ROW_SHIFTER << 5)) |
349           (PINF & (1 << 5) ? 0 : (ROW_SHIFTER << 6)) |
350           (PINF & (1 << 4) ? 0 : (ROW_SHIFTER << 7));
351 }
352
353
354 static 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
361 static 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
368 static 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