aboutsummaryrefslogtreecommitdiff
path: root/keyboards/minidox/matrix.c
diff options
context:
space:
mode:
Diffstat (limited to 'keyboards/minidox/matrix.c')
-rw-r--r--keyboards/minidox/matrix.c307
1 files changed, 0 insertions, 307 deletions
diff --git a/keyboards/minidox/matrix.c b/keyboards/minidox/matrix.c
deleted file mode 100644
index 27a38dca0..000000000
--- a/keyboards/minidox/matrix.c
+++ /dev/null
@@ -1,307 +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#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_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;
50
51static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
52static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
53
54/* matrix state(1:on, 0:off) */
55static matrix_row_t matrix[MATRIX_ROWS];
56static matrix_row_t matrix_debouncing[MATRIX_ROWS];
57
58static matrix_row_t read_cols(void);
59static void init_cols(void);
60static void unselect_rows(void);
61static void select_row(uint8_t row);
62
63
64__attribute__ ((weak))
65void matrix_init_kb(void) {
66 matrix_init_user();
67}
68
69__attribute__ ((weak))
70void matrix_scan_kb(void) {
71 matrix_scan_user();
72}
73
74__attribute__ ((weak))
75void matrix_init_user(void) {
76}
77
78__attribute__ ((weak))
79void matrix_scan_user(void) {
80}
81
82inline
83uint8_t matrix_rows(void)
84{
85 return MATRIX_ROWS;
86}
87
88inline
89uint8_t matrix_cols(void)
90{
91 return MATRIX_COLS;
92}
93
94void 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 TX_RX_LED_INIT;
104
105 // initialize matrix state: all keys off
106 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
107 matrix[i] = 0;
108 matrix_debouncing[i] = 0;
109 }
110
111 matrix_init_quantum();
112}
113
114uint8_t _matrix_scan(void)
115{
116 // Right hand is stored after the left in the matirx so, we need to offset it
117 int offset = isLeftHand ? 0 : (ROWS_PER_HAND);
118
119 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
120 select_row(i);
121 _delay_us(30); // without this wait read unstable value.
122 matrix_row_t cols = read_cols();
123 if (matrix_debouncing[i+offset] != cols) {
124 matrix_debouncing[i+offset] = cols;
125 debouncing = DEBOUNCE;
126 }
127 unselect_rows();
128 }
129
130 if (debouncing) {
131 if (--debouncing) {
132 _delay_ms(1);
133 } else {
134 for (uint8_t i = 0; i < ROWS_PER_HAND; i++) {
135 matrix[i+offset] = matrix_debouncing[i+offset];
136 }
137 }
138 }
139
140 return 1;
141}
142
143#ifdef USE_I2C
144
145// Get rows from other half over i2c
146int i2c_transaction(void) {
147 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
148
149 int err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_WRITE);
150 if (err) goto i2c_error;
151
152 // start of matrix stored at 0x00
153 err = i2c_master_write(0x00);
154 if (err) goto i2c_error;
155
156 // Start read
157 err = i2c_master_start(SLAVE_I2C_ADDRESS + I2C_READ);
158 if (err) goto i2c_error;
159
160 if (!err) {
161 int i;
162 for (i = 0; i < ROWS_PER_HAND-1; ++i) {
163 matrix[slaveOffset+i] = i2c_master_read(I2C_ACK);
164 }
165 matrix[slaveOffset+i] = i2c_master_read(I2C_NACK);
166 i2c_master_stop();
167 } else {
168i2c_error: // the cable is disconnceted, or something else went wrong
169 i2c_reset_state();
170 return err;
171 }
172
173 return 0;
174}
175
176#else // USE_SERIAL
177
178int serial_transaction(void) {
179 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
180
181 if (serial_update_buffers()) {
182 return 1;
183 }
184
185 for (int i = 0; i < ROWS_PER_HAND; ++i) {
186 matrix[slaveOffset+i] = serial_slave_buffer[i];
187 }
188 return 0;
189}
190#endif
191
192uint8_t matrix_scan(void)
193{
194 int ret = _matrix_scan();
195
196
197
198#ifdef USE_I2C
199 if( i2c_transaction() ) {
200#else // USE_SERIAL
201 if( serial_transaction() ) {
202#endif
203 // turn on the indicator led when halves are disconnected
204 TXLED1;
205
206 error_count++;
207
208 if (error_count > ERROR_DISCONNECT_COUNT) {
209 // reset other half if disconnected
210 int slaveOffset = (isLeftHand) ? (ROWS_PER_HAND) : 0;
211 for (int i = 0; i < ROWS_PER_HAND; ++i) {
212 matrix[slaveOffset+i] = 0;
213 }
214 }
215 } else {
216 // turn off the indicator led on no error
217 TXLED0;
218 error_count = 0;
219 }
220 matrix_scan_quantum();
221 return ret;
222}
223
224void matrix_slave_scan(void) {
225 _matrix_scan();
226
227 int offset = (isLeftHand) ? 0 : (MATRIX_ROWS / 2);
228
229#ifdef USE_I2C
230 for (int i = 0; i < ROWS_PER_HAND; ++i) {
231 /* i2c_slave_buffer[i] = matrix[offset+i]; */
232 i2c_slave_buffer[i] = matrix[offset+i];
233 }
234#else // USE_SERIAL
235 for (int i = 0; i < ROWS_PER_HAND; ++i) {
236 serial_slave_buffer[i] = matrix[offset+i];
237 }
238#endif
239}
240
241bool matrix_is_modified(void)
242{
243 if (debouncing) return false;
244 return true;
245}
246
247inline
248bool matrix_is_on(uint8_t row, uint8_t col)
249{
250 return (matrix[row] & ((matrix_row_t)1<<col));
251}
252
253inline
254matrix_row_t matrix_get_row(uint8_t row)
255{
256 return matrix[row];
257}
258
259void matrix_print(void)
260{
261 print("\nr/c 0123456789ABCDEF\n");
262 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
263 phex(row); print(": ");
264 pbin_reverse16(matrix_get_row(row));
265 print("\n");
266 }
267}
268
269uint8_t matrix_key_count(void)
270{
271 uint8_t count = 0;
272 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
273 count += bitpop16(matrix[i]);
274 }
275 return count;
276}
277
278static void init_cols(void)
279{
280 for(int x = 0; x < MATRIX_COLS; x++) {
281 _SFR_IO8((col_pins[x] >> 4) + 1) &= ~_BV(col_pins[x] & 0xF);
282 _SFR_IO8((col_pins[x] >> 4) + 2) |= _BV(col_pins[x] & 0xF);
283 }
284}
285
286static matrix_row_t read_cols(void)
287{
288 matrix_row_t result = 0;
289 for(int x = 0; x < MATRIX_COLS; x++) {
290 result |= (_SFR_IO8(col_pins[x] >> 4) & _BV(col_pins[x] & 0xF)) ? 0 : (1 << x);
291 }
292 return result;
293}
294
295static void unselect_rows(void)
296{
297 for(int x = 0; x < ROWS_PER_HAND; x++) {
298 _SFR_IO8((row_pins[x] >> 4) + 1) &= ~_BV(row_pins[x] & 0xF);
299 _SFR_IO8((row_pins[x] >> 4) + 2) |= _BV(row_pins[x] & 0xF);
300 }
301}
302
303static void select_row(uint8_t row)
304{
305 _SFR_IO8((row_pins[row] >> 4) + 1) |= _BV(row_pins[row] & 0xF);
306 _SFR_IO8((row_pins[row] >> 4) + 2) &= ~_BV(row_pins[row] & 0xF);
307}