aboutsummaryrefslogtreecommitdiff
path: root/keyboards/viterbi/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/viterbi/matrix.c')
-rw-r--r--keyboards/viterbi/matrix.c464
1 files changed, 464 insertions, 0 deletions
diff --git a/keyboards/viterbi/matrix.c b/keyboards/viterbi/matrix.c
new file mode 100644
index 000000000..21eef9456
--- /dev/null
+++ b/keyboards/viterbi/matrix.c
@@ -0,0 +1,464 @@
1/*
2Copyright 2017 Danny Nguyen <danny@hexwire.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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
34#ifdef USE_I2C
35# include "i2c.h"
36#else // USE_SERIAL
37# include "serial.h"
38#endif
39
40#ifndef DEBOUNCING_DELAY
41# define DEBOUNCING_DELAY 5
42#endif
43
44#if (DEBOUNCING_DELAY > 0)
45 static uint16_t debouncing_time;
46 static bool debouncing = false;
47#endif
48
49#if (MATRIX_COLS <= 8)
50# define print_matrix_header() print("\nr/c 01234567\n")
51# define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
52# define matrix_bitpop(i) bitpop(matrix[i])
53# define ROW_SHIFTER ((uint8_t)1)
54#else
55# error "Currently only supports 8 COLS"
56#endif
57static matrix_row_t matrix_debouncing[MATRIX_ROWS];
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];
71
72#if (DIODE_DIRECTION == COL2ROW)
73 static void init_cols(void);
74 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
75 static void unselect_rows(void);
76 static void select_row(uint8_t row);
77 static void unselect_row(uint8_t row);
78#elif (DIODE_DIRECTION == ROW2COL)
79 static void init_rows(void);
80 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
81 static void unselect_cols(void);
82 static void unselect_col(uint8_t col);
83 static void select_col(uint8_t col);
84#endif
85__attribute__ ((weak))
86void matrix_init_quantum(void) {
87 matrix_init_kb();
88}
89
90__attribute__ ((weak))
91void matrix_scan_quantum(void) {
92 matrix_scan_kb();
93}
94
95__attribute__ ((weak))
96void matrix_init_kb(void) {
97 matrix_init_user();
98}
99
100__attribute__ ((weak))
101void matrix_scan_kb(void) {
102 matrix_scan_user();
103}
104
105__attribute__ ((weak))
106void matrix_init_user(void) {
107}
108
109__attribute__ ((weak))
110void matrix_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 debug_enable = true;
128 debug_matrix = true;
129 debug_mouse = true;
130 // initialize row and col
131 unselect_rows();
132 init_cols();
133
134 TX_RX_LED_INIT;
135
136 // initialize matrix state: all keys off
137 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
138 matrix[i] = 0;
139 matrix_debouncing[i] = 0;
140 }
141
142 matrix_init_quantum();
143
144}
145
146uint8_t _matrix_scan(void)
147{
148 int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
149#if (DIODE_DIRECTION == COL2ROW)
150 // Set row, read cols
151 for (uint8_t current_row = 0; current_row < ROWS_PER_HAND; current_row++) {
152# if (DEBOUNCING_DELAY > 0)
153 bool matrix_changed = read_cols_on_row(matrix_debouncing+offset, current_row);
154
155 if (matrix_changed) {
156 debouncing = true;
157 debouncing_time = timer_read();
158 PORTD ^= (1 << 2);
159 }
160
161# else
162 read_cols_on_row(matrix+offset, current_row);
163# endif
164
165 }
166
167#elif (DIODE_DIRECTION == ROW2COL)
168 // Set col, read rows
169 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
170# if (DEBOUNCING_DELAY > 0)
171 bool matrix_changed = read_rows_on_col(matrix_debouncing+offset, current_col);
172 if (matrix_changed) {
173 debouncing = true;
174 debouncing_time = timer_read();
175 }
176# else
177 read_rows_on_col(matrix+offset, current_col);
178# endif
179
180 }
181#endif
182
183# if (DEBOUNCING_DELAY > 0)
184 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
185 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
186 matrix[i+offset] = matrix_debouncing[i+offset];
187 }
188 debouncing = false;
189 }
190# endif
191
192 return 1;
193}
194
195#ifdef USE_I2C
196
197// Get rows from other half over i2c
198int i2c_transaction(void) {
199 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
200
201 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
202 if (err) goto i2c_error;
203
204 // start of matrix stored at 0x00
205 err = i2c_master_write(0x00);
206 if (err) goto i2c_error;
207
208 // Start read
209 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
210 if (err) goto i2c_error;
211
212 if (!err) {
213 int i;
214 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
215 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
216 }
217 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
218 i2c_master_stop();
219 } else {
220i2c_error: // the cable is disconnceted, or something else went wrong
221 i2c_reset_state();
222 return err;
223 }
224
225 return 0;
226}
227
228#else // USE_SERIAL
229
230int serial_transaction(void) {
231 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
232
233 if (serial_update_buffers()) {
234 return 1;
235 }
236
237 for (int i = 0; i < ROWS_PER_HAND; ++i) {
238 matrix[slaveOffset+i] = serial_slave_buffer[i];
239 }
240 return 0;
241}
242#endif
243
244uint8_t matrix_scan(void)
245{
246 uint8_t ret = _matrix_scan();
247
248#ifdef USE_I2C
249 if( i2c_transaction() ) {
250#else // USE_SERIAL
251 if( serial_transaction() ) {
252#endif
253 // turn on the indicator led when halves are disconnected
254 TXLED1;
255
256 error_count++;
257
258 if (error_count > ERROR_DISCONNECT_COUNT) {
259 // reset other half if disconnected
260 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
261 for (int i = 0; i < ROWS_PER_HAND; ++i) {
262 matrix[slaveOffset+i] = 0;
263 }
264 }
265 } else {
266 // turn off the indicator led on no error
267 TXLED0;
268 error_count = 0;
269 }
270 matrix_scan_quantum();
271 return ret;
272}
273
274void matrix_slave_scan(void) {
275 _matrix_scan();
276
277 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
278
279#ifdef USE_I2C
280 for (int i = 0; i < ROWS_PER_HAND; ++i) {
281 i2c_slave_buffer[i] = matrix[offset+i];
282 }
283#else // USE_SERIAL
284 for (int i = 0; i < ROWS_PER_HAND; ++i) {
285 serial_slave_buffer[i] = matrix[offset+i];
286 }
287#endif
288}
289
290bool matrix_is_modified(void)
291{
292 if (debouncing) return false;
293 return true;
294}
295
296inline
297bool matrix_is_on(uint8_t row, uint8_t col)
298{
299 return (matrix[row] & ((matrix_row_t)1<<col));
300}
301
302inline
303matrix_row_t matrix_get_row(uint8_t row)
304{
305 return matrix[row];
306}
307
308void matrix_print(void)
309{
310 print("\nr/c 0123456789ABCDEF\n");
311 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
312 phex(row); print(": ");
313 pbin_reverse16(matrix_get_row(row));
314 print("\n");
315 }
316}
317
318uint8_t matrix_key_count(void)
319{
320 uint8_t count = 0;
321 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
322 count += bitpop16(matrix[i]);
323 }
324 return count;
325}
326
327#if (DIODE_DIRECTION == COL2ROW)
328
329static void init_cols(void)
330{
331 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
332 uint8_t pin = col_pins[x];
333 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
334 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
335 }
336}
337
338static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
339{
340 // Store last value of row prior to reading
341 matrix_row_t last_row_value = current_matrix[current_row];
342
343 // Clear data in matrix row
344 current_matrix[current_row] = 0;
345
346 // Select row and wait for row selecton to stabilize
347 select_row(current_row);
348 wait_us(30);
349
350 // For each col...
351 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
352
353 // Select the col pin to read (active low)
354 uint8_t pin = col_pins[col_index];
355 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
356
357 // Populate the matrix row with the state of the col pin
358 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
359 }
360
361 // Unselect row
362 unselect_row(current_row);
363
364 return (last_row_value != current_matrix[current_row]);
365}
366
367static void select_row(uint8_t row)
368{
369 uint8_t pin = row_pins[row];
370 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
371 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
372}
373
374static void unselect_row(uint8_t row)
375{
376 uint8_t pin = row_pins[row];
377 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
378 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
379}
380
381static void unselect_rows(void)
382{
383 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
384 uint8_t pin = row_pins[x];
385 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
386 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
387 }
388}
389
390#elif (DIODE_DIRECTION == ROW2COL)
391
392static void init_rows(void)
393{
394 for(uint8_t x = 0; x < ROWS_PER_HAND; x++) {
395 uint8_t pin = row_pins[x];
396 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
397 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
398 }
399}
400
401static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
402{
403 bool matrix_changed = false;
404
405 // Select col and wait for col selecton to stabilize
406 select_col(current_col);
407 wait_us(30);
408
409 // For each row...
410 for(uint8_t row_index = 0; row_index < ROWS_PER_HAND; row_index++)
411 {
412
413 // Store last value of row prior to reading
414 matrix_row_t last_row_value = current_matrix[row_index];
415
416 // Check row pin state
417 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
418 {
419 // Pin LO, set col bit
420 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
421 }
422 else
423 {
424 // Pin HI, clear col bit
425 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
426 }
427
428 // Determine if the matrix changed state
429 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
430 {
431 matrix_changed = true;
432 }
433 }
434
435 // Unselect col
436 unselect_col(current_col);
437
438 return matrix_changed;
439}
440
441static void select_col(uint8_t col)
442{
443 uint8_t pin = col_pins[col];
444 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
445 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
446}
447
448static void unselect_col(uint8_t col)
449{
450 uint8_t pin = col_pins[col];
451 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
452 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
453}
454
455static void unselect_cols(void)
456{
457 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
458 uint8_t pin = col_pins[x];
459 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
460 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
461 }
462}
463
464#endif