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