diff options
Diffstat (limited to 'keyboards/helix/rev1/matrix.c')
-rw-r--r-- | keyboards/helix/rev1/matrix.c | 308 |
1 files changed, 0 insertions, 308 deletions
diff --git a/keyboards/helix/rev1/matrix.c b/keyboards/helix/rev1/matrix.c deleted file mode 100644 index aa2cc7690..000000000 --- a/keyboards/helix/rev1/matrix.c +++ /dev/null | |||
@@ -1,308 +0,0 @@ | |||
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 "config.h" | ||
33 | #include "quantum.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 | |||
64 | __attribute__ ((weak)) | ||
65 | void matrix_init_kb(void) { | ||
66 | matrix_init_user(); | ||
67 | } | ||
68 | |||
69 | __attribute__ ((weak)) | ||
70 | void matrix_scan_kb(void) { | ||
71 | matrix_scan_user(); | ||
72 | } | ||
73 | |||
74 | __attribute__ ((weak)) | ||
75 | void matrix_init_user(void) { | ||
76 | } | ||
77 | |||
78 | __attribute__ ((weak)) | ||
79 | void matrix_scan_user(void) { | ||
80 | } | ||
81 | |||
82 | inline | ||
83 | uint8_t matrix_rows(void) | ||
84 | { | ||
85 | return MATRIX_ROWS; | ||
86 | } | ||
87 | |||
88 | inline | ||
89 | uint8_t matrix_cols(void) | ||
90 | { | ||
91 | return MATRIX_COLS; | ||
92 | } | ||
93 | |||
94 | void matrix_init(void) | ||
95 | { | ||
96 | debug_enable = true; | ||
97 | debug_matrix = true; | ||
98 | debug_mouse = true; | ||
99 | // initialize row and col | ||
100 | unselect_rows(); | ||
101 | init_cols(); | ||
102 | |||
103 | setPinOutput(B0); | ||
104 | setPinOutput(D5); | ||
105 | |||
106 | // initialize matrix state: all keys off | ||
107 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
108 | matrix[i] = 0; | ||
109 | matrix_debouncing[i] = 0; | ||
110 | } | ||
111 | |||
112 | matrix_init_quantum(); | ||
113 | } | ||
114 | |||
115 | uint8_t _matrix_scan(void) | ||
116 | { | ||
117 | // Right hand is stored after the left in the matirx so, we need to offset it | ||
118 | int offset = isLeftHand ? 0 : (ROWS_PER_HAND); | ||
119 | |||
120 | for (uint8_t i = 0; i < ROWS_PER_HAND; i++) { | ||
121 | select_row(i); | ||
122 | _delay_us(30); // without this wait read unstable value. | ||
123 | matrix_row_t cols = read_cols(); | ||
124 | if (matrix_debouncing[i+offset] != cols) { | ||
125 | matrix_debouncing[i+offset] = cols; | ||
126 | debouncing = DEBOUNCE; | ||
127 | } | ||
128 | unselect_rows(); | ||
129 | } | ||
130 | |||
131 | if (debouncing) { | ||
132 | if (--debouncing) { | ||
133 | _delay_ms(1); | ||
134 | } else { | ||
135 | for (uint8_t i = 0; i < ROWS_PER_HAND; i++) { | ||
136 | matrix[i+offset] = matrix_debouncing[i+offset]; | ||
137 | } | ||
138 | } | ||
139 | } | ||
140 | |||
141 | return 1; | ||
142 | } | ||
143 | |||
144 | #ifdef USE_MATRIX_I2C | ||
145 | |||
146 | // Get rows from other half over i2c | ||
147 | int i2c_transaction(void) { | ||
148 | int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0; | ||
149 | |||
150 | int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE); | ||
151 | if (err) goto i2c_error; | ||
152 | |||
153 | // start of matrix stored at 0x00 | ||
154 | err = i2c_master_write(0x00); | ||
155 | if (err) goto i2c_error; | ||
156 | |||
157 | // Start read | ||
158 | err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ); | ||
159 | if (err) goto i2c_error; | ||
160 | |||
161 | if (!err) { | ||
162 | int i; | ||
163 | for (i = 0; i < ROWS_PER_HAND-1; ++i) { | ||
164 | matrix[slaveOffset+i] = i2c_master_read(I2C_ACK); | ||
165 | } | ||
166 | matrix[slaveOffset+i] = i2c_master_read(I2C_NACK); | ||
167 | i2c_master_stop(); | ||
168 | } else { | ||
169 | i2c_error: // the cable is disconnceted, or something else went wrong | ||
170 | i2c_reset_state(); | ||
171 | return err; | ||
172 | } | ||
173 | |||
174 | return 0; | ||
175 | } | ||
176 | |||
177 | #else // USE_SERIAL | ||
178 | |||
179 | int 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 | |||
193 | uint8_t matrix_scan(void) | ||
194 | { | ||
195 | int ret = _matrix_scan(); | ||
196 | |||
197 | |||
198 | |||
199 | #ifdef USE_MATRIX_I2C | ||
200 | if( i2c_transaction() ) { | ||
201 | #else // USE_SERIAL | ||
202 | if( serial_transaction() ) { | ||
203 | #endif | ||
204 | // turn on the indicator led when halves are disconnected | ||
205 | writePinLow(D5); | ||
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 | writePinHigh(D5); | ||
219 | error_count = 0; | ||
220 | } | ||
221 | matrix_scan_quantum(); | ||
222 | return ret; | ||
223 | } | ||
224 | |||
225 | void matrix_slave_scan(void) { | ||
226 | _matrix_scan(); | ||
227 | |||
228 | int offset = (isLeftHand) ? 0 : (MATRIX_ROWS / 2); | ||
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_slave_buffer[i] = matrix[offset+i]; | ||
238 | } | ||
239 | #endif | ||
240 | } | ||
241 | |||
242 | bool matrix_is_modified(void) | ||
243 | { | ||
244 | if (debouncing) return false; | ||
245 | return true; | ||
246 | } | ||
247 | |||
248 | inline | ||
249 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
250 | { | ||
251 | return (matrix[row] & ((matrix_row_t)1<<col)); | ||
252 | } | ||
253 | |||
254 | inline | ||
255 | matrix_row_t matrix_get_row(uint8_t row) | ||
256 | { | ||
257 | return matrix[row]; | ||
258 | } | ||
259 | |||
260 | void matrix_print(void) | ||
261 | { | ||
262 | print("\nr/c 0123456789ABCDEF\n"); | ||
263 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
264 | print_hex8(row); print(": "); | ||
265 | print_bin_reverse16(matrix_get_row(row)); | ||
266 | print("\n"); | ||
267 | } | ||
268 | } | ||
269 | |||
270 | uint8_t matrix_key_count(void) | ||
271 | { | ||
272 | uint8_t count = 0; | ||
273 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
274 | count += bitpop16(matrix[i]); | ||
275 | } | ||
276 | return count; | ||
277 | } | ||
278 | |||
279 | static void init_cols(void) | ||
280 | { | ||
281 | for(int x = 0; x < MATRIX_COLS; x++) { | ||
282 | _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF); | ||
283 | _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF); | ||
284 | } | ||
285 | } | ||
286 | |||
287 | static matrix_row_t read_cols(void) | ||
288 | { | ||
289 | matrix_row_t result = 0; | ||
290 | for(int x = 0; x < MATRIX_COLS; x++) { | ||
291 | result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x); | ||
292 | } | ||
293 | return result; | ||
294 | } | ||
295 | |||
296 | static void unselect_rows(void) | ||
297 | { | ||
298 | for(int x = 0; x < ROWS_PER_HAND; x++) { | ||
299 | _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF); | ||
300 | _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF); | ||
301 | } | ||
302 | } | ||
303 | |||
304 | static void select_row(uint8_t row) | ||
305 | { | ||
306 | _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF); | ||
307 | _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF); | ||
308 | } | ||