aboutsummaryrefslogtreecommitdiff
path: root/keyboards/helix/pico/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/helix/pico/matrix.c')
-rw-r--r--keyboards/helix/pico/matrix.c351
1 files changed, 351 insertions, 0 deletions
diff --git a/keyboards/helix/pico/matrix.c b/keyboards/helix/pico/matrix.c
new file mode 100644
index 000000000..aba55da7c
--- /dev/null
+++ b/keyboards/helix/pico/matrix.c
@@ -0,0 +1,351 @@
1/*
2Copyright 2012 Jun Wako <wakojun@gmail.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 <avr/wdt.h>
25#include <avr/interrupt.h>
26#include <util/delay.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
34#ifdef USE_MATRIX_I2C
35# include "i2c.h"
36#else // USE_SERIAL
37# include "serial.h"
38#endif
39
40#ifndef DEBOUNCE
41# define DEBOUNCE 5
42#endif
43
44#define ERROR_DISCONNECT_COUNT 5
45
46static uint8_t debouncing = DEBOUNCE;
47static const int ROWS_PER_HAND = MATRIX_ROWS/2;
48static uint8_t error_count = 0;
49uint8_t is_master = 0 ;
50
51static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
52static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
53
54/* matrix state(1:on, 0:off) */
55static matrix_row_t matrix[MATRIX_ROWS];
56static matrix_row_t matrix_debouncing[MATRIX_ROWS];
57
58static matrix_row_t read_cols(void);
59static void init_cols(void);
60static void unselect_rows(void);
61static void select_row(uint8_t row);
62static uint8_t matrix_master_scan(void);
63
64
65__attribute__ ((weak))
66void matrix_init_kb(void) {
67 matrix_init_user();
68}
69
70__attribute__ ((weak))
71void matrix_scan_kb(void) {
72 matrix_scan_user();
73}
74
75__attribute__ ((weak))
76void matrix_init_user(void) {
77}
78
79__attribute__ ((weak))
80void matrix_scan_user(void) {
81}
82
83inline
84uint8_t matrix_rows(void)
85{
86 return MATRIX_ROWS;
87}
88
89inline
90uint8_t matrix_cols(void)
91{
92 return MATRIX_COLS;
93}
94
95void matrix_init(void)
96{
97 debug_enable = true;
98 debug_matrix = true;
99 debug_mouse = true;
100 // initialize row and col
101 unselect_rows();
102 init_cols();
103
104 TX_RX_LED_INIT;
105 TXLED0;
106 RXLED0;
107
108 // initialize matrix state: all keys off
109 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
110 matrix[i] = 0;
111 matrix_debouncing[i] = 0;
112 }
113
114 is_master = has_usb();
115
116 matrix_init_quantum();
117}
118
119uint8_t _matrix_scan(void)
120{
121 // Right hand is stored after the left in the matirx so, we need to offset it
122 int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
123
124 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
125 select_row(i);
126 _delay_us(30); // without this wait read unstable value.
127 matrix_row_t cols = read_cols();
128 if (matrix_debouncing[i+offset] != cols) {
129 matrix_debouncing[i+offset] = cols;
130 debouncing = DEBOUNCE;
131 }
132 unselect_rows();
133 }
134
135 if (debouncing) {
136 if (--debouncing) {
137 _delay_ms(1);
138 } else {
139 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
140 matrix[i+offset] = matrix_debouncing[i+offset];
141 }
142 }
143 }
144
145 return 1;
146}
147
148#ifdef USE_MATRIX_I2C
149
150// Get rows from other half over i2c
151int i2c_transaction(void) {
152 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
153
154 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
155 if (err) goto i2c_error;
156
157 // start of matrix stored at 0x00
158 err = i2c_master_write(0x00);
159 if (err) goto i2c_error;
160
161 // Start read
162 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
163 if (err) goto i2c_error;
164
165 if (!err) {
166 int i;
167 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
168 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
169 }
170 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
171 i2c_master_stop();
172 } else {
173i2c_error: // the cable is disconnceted, or something else went wrong
174 i2c_reset_state();
175 return err;
176 }
177
178 return 0;
179}
180
181#else // USE_SERIAL
182
183int serial_transaction(void) {
184 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
185 int ret=serial_update_buffers();
186 if (ret ) {
187 if(ret==2)RXLED1;
188 return 1;
189 }
190 RXLED0;
191 for (int i = 0; i < ROWS_PER_HAND; ++i) {
192 matrix[slaveOffset+i] = serial_slave_buffer[i];
193 }
194 return 0;
195}
196#endif
197
198uint8_t matrix_scan(void)
199{
200 if (is_master) {
201 matrix_master_scan();
202 }else{
203 matrix_slave_scan();
204
205// if(serial_slave_DATA_CORRUPT()){
206// TXLED0;
207 int offset = (isLeftHand) ? ROWS_PER_HAND : 0;
208
209 for (int i = 0; i < ROWS_PER_HAND; ++i) {
210 matrix[offset+i] = serial_master_buffer[i];
211 }
212
213// }else{
214// TXLED1;
215// }
216
217 matrix_scan_quantum();
218 }
219 return 1;
220}
221
222
223uint8_t matrix_master_scan(void) {
224
225 int ret = _matrix_scan();
226
227#ifndef KEYBOARD_helix_rev1
228 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
229
230#ifdef USE_MATRIX_I2C
231// for (int i = 0; i < ROWS_PER_HAND; ++i) {
232 /* i2c_slave_buffer[i] = matrix[offset+i]; */
233// i2c_slave_buffer[i] = matrix[offset+i];
234// }
235#else // USE_SERIAL
236 for (int i = 0; i < ROWS_PER_HAND; ++i) {
237 serial_master_buffer[i] = matrix[offset+i];
238 }
239#endif
240#endif
241
242#ifdef USE_MATRIX_I2C
243 if( i2c_transaction() ) {
244#else // USE_SERIAL
245 if( serial_transaction() ) {
246#endif
247 // turn on the indicator led when halves are disconnected
248 TXLED1;
249
250 error_count++;
251
252 if (error_count > ERROR_DISCONNECT_COUNT) {
253 // reset other half if disconnected
254 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
255 for (int i = 0; i < ROWS_PER_HAND; ++i) {
256 matrix[slaveOffset+i] = 0;
257 }
258 }
259 } else {
260 // turn off the indicator led on no error
261 TXLED0;
262 error_count = 0;
263 }
264 matrix_scan_quantum();
265 return ret;
266}
267
268void matrix_slave_scan(void) {
269 _matrix_scan();
270
271 int offset = (isLeftHand) ? 0 : ROWS_PER_HAND;
272
273#ifdef USE_MATRIX_I2C
274 for (int i = 0; i < ROWS_PER_HAND; ++i) {
275 /* i2c_slave_buffer[i] = matrix[offset+i]; */
276 i2c_slave_buffer[i] = matrix[offset+i];
277 }
278#else // USE_SERIAL
279 for (int i = 0; i < ROWS_PER_HAND; ++i) {
280 serial_slave_buffer[i] = matrix[offset+i];
281 }
282#endif
283}
284
285bool matrix_is_modified(void)
286{
287 if (debouncing) return false;
288 return true;
289}
290
291inline
292bool matrix_is_on(uint8_t row, uint8_t col)
293{
294 return (matrix[row] & ((matrix_row_t)1<<col));
295}
296
297inline
298matrix_row_t matrix_get_row(uint8_t row)
299{
300 return matrix[row];
301}
302
303void matrix_print(void)
304{
305 print("\nr/c 0123456789ABCDEF\n");
306 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
307 phex(row); print(": ");
308 pbin_reverse16(matrix_get_row(row));
309 print("\n");
310 }
311}
312
313uint8_t matrix_key_count(void)
314{
315 uint8_t count = 0;
316 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
317 count += bitpop16(matrix[i]);
318 }
319 return count;
320}
321
322static void init_cols(void)
323{
324 for(int x = 0; x < MATRIX_COLS; x++) {
325 _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF);
326 _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
327 }
328}
329
330static matrix_row_t read_cols(void)
331{
332 matrix_row_t result = 0;
333 for(int x = 0; x < MATRIX_COLS; x++) {
334 result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
335 }
336 return result;
337}
338
339static void unselect_rows(void)
340{
341 for(int x = 0; x < ROWS_PER_HAND; x++) {
342 _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF);
343 _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
344 }
345}
346
347static void select_row(uint8_t row)
348{
349 _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF);
350 _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);
351}