diff options
Diffstat (limited to 'keyboards/helix/rev1/matrix.c')
-rw-r--r-- | keyboards/helix/rev1/matrix.c | 316 |
1 files changed, 316 insertions, 0 deletions
diff --git a/keyboards/helix/rev1/matrix.c b/keyboards/helix/rev1/matrix.c new file mode 100644 index 000000000..375057a6f --- /dev/null +++ b/keyboards/helix/rev1/matrix.c | |||
@@ -0,0 +1,316 @@ | |||
1 | /* | ||
2 | Copyright 2012 Jun Wako <wakojun@gmail.com> | ||
3 | |||
4 | This program is free software: you can redistribute it and/or modify | ||
5 | it under the terms of the GNU General Public License as published by | ||
6 | the Free Software Foundation, either version 2 of the License, or | ||
7 | (at your option) any later version. | ||
8 | |||
9 | This program is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
12 | GNU General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU General Public License | ||
15 | along 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 | |||
47 | static uint8_t debouncing = DEBOUNCE; | ||
48 | static const int ROWS_PER_HAND = MATRIX_ROWS/2; | ||
49 | static uint8_t error_count = 0; | ||
50 | |||
51 | static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS; | ||
52 | static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS; | ||
53 | |||
54 | /* matrix state(1:on, 0:off) */ | ||
55 | static matrix_row_t matrix[MATRIX_ROWS]; | ||
56 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | ||
57 | |||
58 | static matrix_row_t read_cols(void); | ||
59 | static void init_cols(void); | ||
60 | static void unselect_rows(void); | ||
61 | static void select_row(uint8_t row); | ||
62 | |||
63 | __attribute__ ((weak)) | ||
64 | void matrix_init_quantum(void) { | ||
65 | matrix_init_kb(); | ||
66 | } | ||
67 | |||
68 | __attribute__ ((weak)) | ||
69 | void matrix_scan_quantum(void) { | ||
70 | matrix_scan_kb(); | ||
71 | } | ||
72 | |||
73 | __attribute__ ((weak)) | ||
74 | void matrix_init_kb(void) { | ||
75 | matrix_init_user(); | ||
76 | } | ||
77 | |||
78 | __attribute__ ((weak)) | ||
79 | void matrix_scan_kb(void) { | ||
80 | matrix_scan_user(); | ||
81 | } | ||
82 | |||
83 | __attribute__ ((weak)) | ||
84 | void matrix_init_user(void) { | ||
85 | } | ||
86 | |||
87 | __attribute__ ((weak)) | ||
88 | void matrix_scan_user(void) { | ||
89 | } | ||
90 | |||
91 | inline | ||
92 | uint8_t matrix_rows(void) | ||
93 | { | ||
94 | return MATRIX_ROWS; | ||
95 | } | ||
96 | |||
97 | inline | ||
98 | uint8_t matrix_cols(void) | ||
99 | { | ||
100 | return MATRIX_COLS; | ||
101 | } | ||
102 | |||
103 | void matrix_init(void) | ||
104 | { | ||
105 | debug_enable = true; | ||
106 | debug_matrix = true; | ||
107 | debug_mouse = true; | ||
108 | // initialize row and col | ||
109 | unselect_rows(); | ||
110 | init_cols(); | ||
111 | |||
112 | TX_RX_LED_INIT; | ||
113 | |||
114 | // initialize matrix state: all keys off | ||
115 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
116 | matrix[i] = 0; | ||
117 | matrix_debouncing[i] = 0; | ||
118 | } | ||
119 | |||
120 | matrix_init_quantum(); | ||
121 | } | ||
122 | |||
123 | uint8_t _matrix_scan(void) | ||
124 | { | ||
125 | // Right hand is stored after the left in the matirx so, we need to offset it | ||
126 | int offset = isLeftHand ? 0 : (ROWS_PER_HAND); | ||
127 | |||
128 | for (uint8_t i = 0; i < ROWS_PER_HAND; i++) { | ||
129 | select_row(i); | ||
130 | _delay_us(30); // without this wait read unstable value. | ||
131 | matrix_row_t cols = read_cols(); | ||
132 | if (matrix_debouncing[i+offset] != cols) { | ||
133 | matrix_debouncing[i+offset] = cols; | ||
134 | debouncing = DEBOUNCE; | ||
135 | } | ||
136 | unselect_rows(); | ||
137 | } | ||
138 | |||
139 | if (debouncing) { | ||
140 | if (--debouncing) { | ||
141 | _delay_ms(1); | ||
142 | } else { | ||
143 | for (uint8_t i = 0; i < ROWS_PER_HAND; i++) { | ||
144 | matrix[i+offset] = matrix_debouncing[i+offset]; | ||
145 | } | ||
146 | } | ||
147 | } | ||
148 | |||
149 | return 1; | ||
150 | } | ||
151 | |||
152 | #ifdef USE_MATRIX_I2C | ||
153 | |||
154 | // Get rows from other half over i2c | ||
155 | int i2c_transaction(void) { | ||
156 | int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; | ||
157 | |||
158 | int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE); | ||
159 | if (err) goto i2c_error; | ||
160 | |||
161 | // start of matrix stored at 0x00 | ||
162 | err = i2c_master_write(0x00); | ||
163 | if (err) goto i2c_error; | ||
164 | |||
165 | // Start read | ||
166 | err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ); | ||
167 | if (err) goto i2c_error; | ||
168 | |||
169 | if (!err) { | ||
170 | int i; | ||
171 | for (i = 0; i < ROWS_PER_HAND-1; ++i) { | ||
172 | matrix[slaveOffset+i] = i2c_master_read(I2C_ACK); | ||
173 | } | ||
174 | matrix[slaveOffset+i] = i2c_master_read(I2C_NACK); | ||
175 | i2c_master_stop(); | ||
176 | } else { | ||
177 | i2c_error: // the cable is disconnceted, or something else went wrong | ||
178 | i2c_reset_state(); | ||
179 | return err; | ||
180 | } | ||
181 | |||
182 | return 0; | ||
183 | } | ||
184 | |||
185 | #else // USE_SERIAL | ||
186 | |||
187 | int serial_transaction(void) { | ||
188 | int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; | ||
189 | |||
190 | if (serial_update_buffers()) { | ||
191 | return 1; | ||
192 | } | ||
193 | |||
194 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
195 | matrix[slaveOffset+i] = serial_slave_buffer[i]; | ||
196 | } | ||
197 | return 0; | ||
198 | } | ||
199 | #endif | ||
200 | |||
201 | uint8_t matrix_scan(void) | ||
202 | { | ||
203 | int ret = _matrix_scan(); | ||
204 | |||
205 | |||
206 | |||
207 | #ifdef USE_MATRIX_I2C | ||
208 | if( i2c_transaction() ) { | ||
209 | #else // USE_SERIAL | ||
210 | if( serial_transaction() ) { | ||
211 | #endif | ||
212 | // turn on the indicator led when halves are disconnected | ||
213 | TXLED1; | ||
214 | |||
215 | error_count++; | ||
216 | |||
217 | if (error_count > ERROR_DISCONNECT_COUNT) { | ||
218 | // reset other half if disconnected | ||
219 | int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; | ||
220 | for (int i = 0; i < ROWS_PER_HAND; ++i) { | ||
221 | matrix[slaveOffset+i] = 0; | ||
222 | } | ||
223 | } | ||
224 | } else { | ||
225 | // turn off the indicator led on no error | ||
226 | TXLED0; | ||
227 | error_count = 0; | ||
228 | } | ||
229 | matrix_scan_quantum(); | ||
230 | return ret; | ||
231 | } | ||
232 | |||
233 | void matrix_slave_scan(void) { | ||
234 | _matrix_scan(); | ||
235 | |||
236 | int offset = (isLeftHand) ? 0 : (MATRIX_ROWS / 2); | ||
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_slave_buffer[i] = matrix[offset+i]; | ||
246 | } | ||
247 | #endif | ||
248 | } | ||
249 | |||
250 | bool matrix_is_modified(void) | ||
251 | { | ||
252 | if (debouncing) return false; | ||
253 | return true; | ||
254 | } | ||
255 | |||
256 | inline | ||
257 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
258 | { | ||
259 | return (matrix[row] & ((matrix_row_t)1<<col)); | ||
260 | } | ||
261 | |||
262 | inline | ||
263 | matrix_row_t matrix_get_row(uint8_t row) | ||
264 | { | ||
265 | return matrix[row]; | ||
266 | } | ||
267 | |||
268 | void matrix_print(void) | ||
269 | { | ||
270 | print("\nr/c 0123456789ABCDEF\n"); | ||
271 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
272 | phex(row); print(": "); | ||
273 | pbin_reverse16(matrix_get_row(row)); | ||
274 | print("\n"); | ||
275 | } | ||
276 | } | ||
277 | |||
278 | uint8_t matrix_key_count(void) | ||
279 | { | ||
280 | uint8_t count = 0; | ||
281 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
282 | count += bitpop16(matrix[i]); | ||
283 | } | ||
284 | return count; | ||
285 | } | ||
286 | |||
287 | static void init_cols(void) | ||
288 | { | ||
289 | for(int x = 0; x < MATRIX_COLS; x++) { | ||
290 | _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF); | ||
291 | _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF); | ||
292 | } | ||
293 | } | ||
294 | |||
295 | static matrix_row_t read_cols(void) | ||
296 | { | ||
297 | matrix_row_t result = 0; | ||
298 | for(int x = 0; x < MATRIX_COLS; x++) { | ||
299 | result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x); | ||
300 | } | ||
301 | return result; | ||
302 | } | ||
303 | |||
304 | static void unselect_rows(void) | ||
305 | { | ||
306 | for(int x = 0; x < ROWS_PER_HAND; x++) { | ||
307 | _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF); | ||
308 | _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF); | ||
309 | } | ||
310 | } | ||
311 | |||
312 | static void select_row(uint8_t row) | ||
313 | { | ||
314 | _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF); | ||
315 | _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF); | ||
316 | } | ||