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