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