aboutsummaryrefslogtreecommitdiff
path: root/keyboards/ergotaco/matrix.c
diff options
context:
space:
mode:
authorJeremy Bernhardt <jeremythegeek@gmail.com>2019-03-29 11:39:28 -0600
committerDrashna Jaelre <drashna@live.com>2019-03-29 10:39:28 -0700
commit8fa9f67256772984110a6e21f19d6fb8594b804d (patch)
tree53c5ac04dd72031800e3aaba707ce66d5748cd3d /keyboards/ergotaco/matrix.c
parent3f4d706c98fc3a7f5fcec6ed0b62291b4159f097 (diff)
downloadqmk_firmware-8fa9f67256772984110a6e21f19d6fb8594b804d.tar.gz
qmk_firmware-8fa9f67256772984110a6e21f19d6fb8594b804d.zip
[Keyboard] ErgoTaco Support (#5504)
* ErgoTaco support * Update keyboards/ergotaco/ergotaco.h Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/ergotaco/keymaps/default/keymap.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/ergotaco/keymaps/default/keymap.c Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/ergotaco/readme.md Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/ergotaco/readme.md Co-Authored-By: germ <jeremythegeek@gmail.com> * Update keyboards/ergotaco/rules.mk Co-Authored-By: germ <jeremythegeek@gmail.com> * juggling rules.mk * Update keyboards/ergotaco/rules.mk Co-Authored-By: germ <jeremythegeek@gmail.com> * updating IS_COMMAND * learning2english Meme-tastic --Drashna
Diffstat (limited to 'keyboards/ergotaco/matrix.c')
-rw-r--r--keyboards/ergotaco/matrix.c366
1 files changed, 366 insertions, 0 deletions
diff --git a/keyboards/ergotaco/matrix.c b/keyboards/ergotaco/matrix.c
new file mode 100644
index 000000000..e4fd6902f
--- /dev/null
+++ b/keyboards/ergotaco/matrix.c
@@ -0,0 +1,366 @@
1/*
2Note for ErgoDox EZ customizers: Here be dragons!
3This is not a file you want to be messing with.
4All of the interesting stuff for you is under keymaps/ :)
5Love, Erez
6
7Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
8
9This program is free software: you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation, either version 2 of the License, or
12(at your option) any later version.
13
14This program is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with this program. If not, see <http://www.gnu.org/licenses/>.
21*/
22
23#include "matrix.h"
24#include <stdint.h>
25#include <stdbool.h>
26#include <avr/io.h>
27#include "wait.h"
28#include "action_layer.h"
29#include "print.h"
30#include "debug.h"
31#include "util.h"
32#include QMK_KEYBOARD_H
33#ifdef DEBUG_MATRIX_SCAN_RATE
34#include "timer.h"
35#endif
36
37#ifndef DEBOUNCE
38# define DEBOUNCE 5
39#endif
40
41// ATmega pin defs
42#define ROW1 (1<<5)
43#define COL6 (1<<0)
44#define COL7 (1<<1)
45#define COL8 (1<<2)
46#define COL9 (1<<3)
47#define COL10 (1<<2)
48#define COL11 (1<<3)
49
50
51// bit masks
52#define BMASK (COL7 | COL8 | COL9 | COL6)
53#define DMASK (COL10 | COL11)
54#define FMASK (ROW1)
55
56/* matrix state(1:on, 0:off) */
57static matrix_row_t matrix[MATRIX_ROWS];
58/*
59 * matrix state(1:on, 0:off)
60 * contains the raw values without debounce filtering of the last read cycle.
61 */
62static matrix_row_t raw_matrix[MATRIX_ROWS];
63
64// Debouncing: store for each key the number of scans until it's eligible to
65// change. When scanning the matrix, ignore any changes in keys that have
66// already changed in the last DEBOUNCE scans.
67static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
68
69static matrix_row_t read_cols(uint8_t row);
70static void init_cols(void);
71static void unselect_rows(void);
72static void select_row(uint8_t row);
73
74static uint8_t mcp23018_reset_loop;
75// static uint16_t mcp23018_reset_loop;
76
77#ifdef DEBUG_MATRIX_SCAN_RATE
78uint32_t matrix_timer;
79uint32_t matrix_scan_count;
80#endif
81
82
83__attribute__ ((weak))
84void matrix_init_user(void) {}
85
86__attribute__ ((weak))
87void matrix_scan_user(void) {}
88
89__attribute__ ((weak))
90void matrix_init_kb(void) {
91 matrix_init_user();
92}
93
94__attribute__ ((weak))
95void matrix_scan_kb(void) {
96 matrix_scan_user();
97}
98
99inline
100uint8_t matrix_rows(void)
101{
102 return MATRIX_ROWS;
103}
104
105inline
106uint8_t matrix_cols(void)
107{
108 return MATRIX_COLS;
109}
110
111
112void matrix_init(void)
113{
114 // initialize row and col
115 mcp23018_status = init_mcp23018();
116 unselect_rows();
117 init_cols();
118
119 // initialize matrix state: all keys off
120 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
121 matrix[i] = 0;
122 raw_matrix[i] = 0;
123 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
124 debounce_matrix[i * MATRIX_COLS + j] = 0;
125 }
126 }
127
128#ifdef DEBUG_MATRIX_SCAN_RATE
129 matrix_timer = timer_read32();
130 matrix_scan_count = 0;
131#endif
132 matrix_init_quantum();
133}
134
135void matrix_power_up(void) {
136 mcp23018_status = init_mcp23018();
137
138 unselect_rows();
139 init_cols();
140
141 // initialize matrix state: all keys off
142 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
143 matrix[i] = 0;
144 }
145
146#ifdef DEBUG_MATRIX_SCAN_RATE
147 matrix_timer = timer_read32();
148 matrix_scan_count = 0;
149#endif
150
151}
152
153// Returns a matrix_row_t whose bits are set if the corresponding key should be
154// eligible to change in this scan.
155matrix_row_t debounce_mask(matrix_row_t rawcols, uint8_t row) {
156 matrix_row_t result = 0;
157 matrix_row_t change = rawcols ^ raw_matrix[row];
158 raw_matrix[row] = rawcols;
159 for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
160 if (debounce_matrix[row * MATRIX_COLS + i]) {
161 --debounce_matrix[row * MATRIX_COLS + i];
162 } else {
163 result |= (1 << i);
164 }
165 if (change & (1 << i)) {
166 debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
167 }
168 }
169 return result;
170}
171
172matrix_row_t debounce_read_cols(uint8_t row) {
173 // Read the row without debouncing filtering and store it for later usage.
174 matrix_row_t cols = read_cols(row);
175 // Get the Debounce mask.
176 matrix_row_t mask = debounce_mask(cols, row);
177 // debounce the row and return the result.
178 return (cols & mask) | (matrix[row] & ~mask);;
179}
180
181uint8_t matrix_scan(void)
182{
183 // Then the keyboard
184 if (mcp23018_status) { // if there was an error
185 if (++mcp23018_reset_loop == 0) {
186 // if (++mcp23018_reset_loop >= 1300) {
187 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
188 // this will be approx bit more frequent than once per second
189 print("trying to reset mcp23018\n");
190 mcp23018_status = init_mcp23018();
191 if (mcp23018_status) {
192 print("left side not responding\n");
193 } else {
194 print("left side attached\n");
195 }
196 }
197 }
198
199#ifdef DEBUG_MATRIX_SCAN_RATE
200 matrix_scan_count++;
201 uint32_t timer_now = timer_read32();
202 if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
203 print("matrix scan frequency: ");
204 pdec(matrix_scan_count);
205 print("\n");
206
207 matrix_timer = timer_now;
208 matrix_scan_count = 0;
209 }
210#endif
211 for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
212 select_row(i);
213 // and select on left hand
214 select_row(i + MATRIX_ROWS_PER_SIDE);
215 // we don't need a 30us delay anymore, because selecting a
216 // left-hand row requires more than 30us for i2c.
217
218 // grab cols from left hand
219 matrix[i] = debounce_read_cols(i);
220 // grab cols from right hand
221 matrix[i + MATRIX_ROWS_PER_SIDE] = debounce_read_cols(i + MATRIX_ROWS_PER_SIDE);
222
223 unselect_rows();
224 }
225
226 matrix_scan_quantum();
227
228#ifdef DEBUG_MATRIX
229 for (uint8_t c = 0; c < MATRIX_COLS; c++)
230 for (uint8_t r = 0; r < MATRIX_ROWS; r++)
231 if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
232#endif
233
234 return 1;
235}
236
237bool matrix_is_modified(void) // deprecated and evidently not called.
238{
239 return true;
240}
241
242inline
243bool matrix_is_on(uint8_t row, uint8_t col)
244{
245 return (matrix[row] & ((matrix_row_t)1<<col));
246}
247
248inline
249matrix_row_t matrix_get_row(uint8_t row)
250{
251 return matrix[row];
252}
253
254void matrix_print(void)
255{
256 print("\nr/c 0123456789ABCDEF\n");
257 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
258 phex(row); print(": ");
259 pbin_reverse16(matrix_get_row(row));
260 print("\n");
261 }
262}
263
264uint8_t matrix_key_count(void)
265{
266 uint8_t count = 0;
267 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
268 count += bitpop16(matrix[i]);
269 }
270 return count;
271}
272
273// Remember this means ROWS
274static void init_cols(void)
275{
276 // init on mcp23018
277 // not needed, already done as part of init_mcp23018()
278
279 // Input with pull-up(DDR:0, PORT:1)
280 DDRF &= ~FMASK;
281 PORTF |= FMASK;
282}
283
284static matrix_row_t read_cols(uint8_t row)
285{
286 if (row < 6) {
287 if (mcp23018_status) { // if there was an error
288 return 0;
289 } else {
290 uint8_t data = 0;
291 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
292 mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
293 mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
294 mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
295 data = (~((uint8_t)mcp23018_status) >> 2) & 0x01 ;
296 mcp23018_status = I2C_STATUS_SUCCESS;
297 out:
298 i2c_stop();
299
300#ifdef DEBUG_MATRIX
301 if (data != 0x00) xprintf("I2C: %d\n", data);
302#endif
303 return data;
304 }
305 } else {
306 // Read using bitmask
307 return ~((((PINF & ROW1) >> 5)) & 0x1);
308 }
309}
310
311// Row pin configuration
312static void unselect_rows(void)
313{
314 // no need to unselect on mcp23018, because the select step sets all
315 // the other row bits high, and it's not changing to a different
316 // direction
317 // Hi-Z(DDR:0, PORT:0) to unselect
318 DDRB &= ~BMASK;
319 PORTB &= ~BMASK;
320 DDRD &= ~DMASK;
321 PORTD &= ~DMASK;
322}
323
324static void select_row(uint8_t row)
325{
326 if (row < 6) {
327 // select on mcp23018
328 if (mcp23018_status) { // do nothing on error
329 // Read using bitmask
330 } else { // set active row low : 0 // set other rows hi-Z : 1
331 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
332 mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
333 mcp23018_status = i2c_write(~(1<<row), ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
334 out:
335 i2c_stop();
336 }
337 } else {
338 // Output low(DDR:1, PORT:0) to select
339 switch (row) {
340 case 6:
341 DDRB |= COL6;
342 PORTB &= ~COL6;
343 break;
344 case 7:
345 DDRB |= COL7;
346 PORTB &= ~COL7;
347 break;
348 case 8:
349 DDRB |= COL8;
350 PORTB &= ~COL8;
351 break;
352 case 9:
353 DDRB |= COL9;
354 PORTB &= ~COL9;
355 break;
356 case 10:
357 DDRD |= COL10;
358 PORTD &= ~COL10;
359 break;
360 case 11:
361 DDRD |= COL11;
362 PORTD &= ~COL11;
363 break;
364 }
365 }
366}