aboutsummaryrefslogtreecommitdiff
path: root/keyboards/lets_split/matrix.c
diff options
context:
space:
mode:
authorJack Humbert <jack.humb@gmail.com>2016-07-05 23:27:10 -0400
committerJack Humbert <jack.humb@gmail.com>2016-07-05 23:27:10 -0400
commitd707738616c140f8d9c8eded7b64e5fc806f4b24 (patch)
treef8aaebebb526903bb49283aa9fa53242c4bf793b /keyboards/lets_split/matrix.c
parent96f44e120295e677d21d3dbb9dc4bf642ba2af09 (diff)
downloadqmk_firmware-d707738616c140f8d9c8eded7b64e5fc806f4b24.tar.gz
qmk_firmware-d707738616c140f8d9c8eded7b64e5fc806f4b24.zip
i2c working
Diffstat (limited to 'keyboards/lets_split/matrix.c')
-rw-r--r--keyboards/lets_split/matrix.c310
1 files changed, 310 insertions, 0 deletions
diff --git a/keyboards/lets_split/matrix.c b/keyboards/lets_split/matrix.c
new file mode 100644
index 000000000..16c2ba0ba
--- /dev/null
+++ b/keyboards/lets_split/matrix.c
@@ -0,0 +1,310 @@
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 "i2c.h"
32#include "split_util.h"
33#include "pro_micro.h"
34#include "config.h"
35
36#ifndef DEBOUNCE
37# define DEBOUNCE 5
38#endif
39
40#define ERROR_DISCONNECT_COUNT 5
41
42static uint8_t debouncing = DEBOUNCE;
43static const int ROWS_PER_HAND = MATRIX_ROWS/2;
44static uint8_t error_count = 0;
45
46static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
47static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
48
49/* matrix state(1:on, 0:off) */
50static matrix_row_t matrix[MATRIX_ROWS];
51static matrix_row_t matrix_debouncing[MATRIX_ROWS];
52
53static matrix_row_t read_cols(void);
54static void init_cols(void);
55static void unselect_rows(void);
56static void select_row(uint8_t row);
57
58__attribute__ ((weak))
59void matrix_init_quantum(void) {
60 matrix_init_kb();
61}
62
63__attribute__ ((weak))
64void matrix_scan_quantum(void) {
65 matrix_scan_kb();
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 matirx 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// Get rows from other half over i2c
148int i2c_transaction(void) {
149 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
150
151 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
152 if (err) goto i2c_error;
153
154 // start of matrix stored at 0x00
155 err = i2c_master_write(0x00);
156 if (err) goto i2c_error;
157
158 // Start read
159 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
160 if (err) goto i2c_error;
161
162 if (!err) {
163 int i;
164 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
165 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
166 }
167 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
168 i2c_master_stop();
169 } else {
170i2c_error: // the cable is disconnceted, or something else went wrong
171 i2c_reset_state();
172 return err;
173 }
174
175 return 0;
176}
177
178#ifndef USE_I2C
179int serial_transaction(void) {
180 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
181
182 if (serial_update_buffers()) {
183 return 1;
184 }
185
186 for (int i = 0; i < ROWS_PER_HAND; ++i) {
187 matrix[slaveOffset+i] = serial_slave_buffer[i];
188 }
189 return 0;
190}
191#endif
192
193uint8_t matrix_scan(void)
194{
195 int ret = _matrix_scan();
196
197
198
199#ifdef USE_I2C
200 if( i2c_transaction() ) {
201#else
202 if( serial_transaction() ) {
203#endif
204 // turn on the indicator led when halves are disconnected
205 TXLED1;
206
207 error_count++;
208
209 if (error_count > ERROR_DISCONNECT_COUNT) {
210 // reset other half if disconnected
211 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
212 for (int i = 0; i < ROWS_PER_HAND; ++i) {
213 matrix[slaveOffset+i] = 0;
214 }
215 }
216 } else {
217 // turn off the indicator led on no error
218 TXLED0;
219 error_count = 0;
220 }
221
222 matrix_scan_quantum();
223
224 return ret;
225}
226
227void matrix_slave_scan(void) {
228 _matrix_scan();
229
230 int offset = (isLeftHand) ? 0 : (MATRIX_ROWS / 2);
231
232#ifdef USE_I2C
233 for (int i = 0; i < ROWS_PER_HAND; ++i) {
234 /* i2c_slave_buffer[i] = matrix[offset+i]; */
235 i2c_slave_buffer[i] = matrix[offset+i];
236 }
237#else
238 for (int i = 0; i < ROWS_PER_HAND; ++i) {
239 serial_slave_buffer[i] = matrix[offset+i];
240 }
241#endif
242}
243
244bool matrix_is_modified(void)
245{
246 if (debouncing) return false;
247 return true;
248}
249
250inline
251bool matrix_is_on(uint8_t row, uint8_t col)
252{
253 return (matrix[row] & ((matrix_row_t)1<<col));
254}
255
256inline
257matrix_row_t matrix_get_row(uint8_t row)
258{
259 return matrix[row];
260}
261
262void matrix_print(void)
263{
264 print("\nr/c 0123456789ABCDEF\n");
265 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
266 phex(row); print(": ");
267 pbin_reverse16(matrix_get_row(row));
268 print("\n");
269 }
270}
271
272uint8_t matrix_key_count(void)
273{
274 uint8_t count = 0;
275 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
276 count += bitpop16(matrix[i]);
277 }
278 return count;
279}
280
281static void init_cols(void)
282{
283 for(int x = 0; x < MATRIX_COLS; x++) {
284 _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF);
285 _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
286 }
287}
288
289static matrix_row_t read_cols(void)
290{
291 matrix_row_t result = 0;
292 for(int x = 0; x < MATRIX_COLS; x++) {
293 result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
294 }
295 return result;
296}
297
298static void unselect_rows(void)
299{
300 for(int x = 0; x < ROWS_PER_HAND; x++) {
301 _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF);
302 _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
303 }
304}
305
306static void select_row(uint8_t row)
307{
308 _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF);
309 _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);
310}