]> git.donarmstrong.com Git - qmk_firmware.git/blob - keyboards/gergo/matrix.c
234160326a5212f84994ea82fb98171b2276ec7f
[qmk_firmware.git] / keyboards / gergo / matrix.c
1 /*
2 Note for ErgoDox EZ customizers: Here be dragons!
3 This is not a file you want to be messing with.
4 All of the interesting stuff for you is under keymaps/ :)
5 Love, Erez
6
7 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
8
9 This program is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 2 of the License, or
12 (at your option) any later version.
13
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along 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 "debounce.h"
33 #include QMK_KEYBOARD_H
34 #ifdef DEBUG_MATRIX_SCAN_RATE
35 #   include  "timer.h"
36 #endif
37
38 #ifdef BALLER
39 #include <avr/interrupt.h>
40 #include "pointing_device.h"
41 #endif
42
43 #ifndef DEBOUNCE
44 #   define DEBOUNCE     5
45 #endif
46
47 // MCP Pin Defs
48 #define RROW1 (1<<3)
49 #define RROW2 (1<<2)
50 #define RROW3 (1<<1)
51 #define RROW4 (1<<0)
52 #define COL0 (1<<0)
53 #define COL1 (1<<1)
54 #define COL2 (1<<2)
55 #define COL3 (1<<3)
56 #define COL4 (1<<4)
57 #define COL5 (1<<5)
58 #define COL6 (1<<6)
59
60 // ATmega pin defs
61 #define ROW1  (1<<6)
62 #define ROW2  (1<<5)
63 #define ROW3  (1<<4)
64 #define ROW4  (1<<1)
65 #define COL7 (1<<0)
66 #define COL8 (1<<1)
67 #define COL9 (1<<2)
68 #define COL10 (1<<3)
69 #define COL11 (1<<2)
70 #define COL12 (1<<3)
71 #define COL13 (1<<6)
72
73 //Trackball pin defs
74 #define TRKUP (1<<4)
75 #define TRKDN (1<<5)
76 #define TRKLT (1<<6)
77 #define TRKRT (1<<7)
78 #define TRKBTN (1<<6)
79
80
81 // Multiple for mouse moves
82 #ifndef TRKSTEP
83 #define TRKSTEP 20
84 #endif
85
86 // multiple for mouse scroll
87 #ifndef SCROLLSTEP
88 #define SCROLLSTEP 5
89 #endif
90
91 // bit masks
92 #define BMASK     (COL7 | COL8 | COL9 | COL10)
93 #define CMASK     (COL13)
94 #define DMASK     (COL11 | COL12)
95 #define FMASK     (ROW1 | ROW2 | ROW3 | ROW4)
96 #define RROWMASK  (RROW1 | RROW2 | RROW3 | RROW4)
97 #define MCPMASK   (COL0 | COL1 | COL2 | COL3 | COL4 | COL5 | COL6)
98 #define TRKMASK   (TRKUP | TRKDN | TRKRT | TRKLT)
99
100 // Trackball interrupts accumulate over here. Processed on scan
101 // Stores prev state of mouse, high bits store direction
102 uint8_t trkState    = 0;
103 uint8_t trkBtnState = 0;
104
105 volatile uint8_t tbUpCnt  = 0;
106 volatile uint8_t tbDnCnt  = 0;
107 volatile uint8_t tbLtCnt  = 0;
108 volatile uint8_t tbRtCnt  = 0;
109
110 /* matrix state(1:on, 0:off) */
111 static matrix_row_t matrix[MATRIX_ROWS];
112 /*
113  * matrix state(1:on, 0:off)
114  * contains the raw values without debounce filtering of the last read cycle.
115  */
116 static matrix_row_t raw_matrix[MATRIX_ROWS];
117
118 // Debouncing: store for each key the number of scans until it's eligible to
119 // change.  When scanning the matrix, ignore any changes in keys that have
120 // already changed in the last DEBOUNCE scans.
121
122 static matrix_row_t read_cols(uint8_t row);
123 static void         init_cols(void);
124 static void         unselect_rows(void);
125 static void         select_row(uint8_t row);
126 static void enableInterrupts(void);
127
128 static uint8_t mcp23018_reset_loop;
129 // static uint16_t mcp23018_reset_loop;
130
131 #ifdef DEBUG_MATRIX_SCAN_RATE
132 uint32_t matrix_timer;
133 uint32_t matrix_scan_count;
134 #endif
135
136
137 __attribute__ ((weak)) void matrix_init_user(void) {}
138
139 __attribute__ ((weak)) void matrix_scan_user(void) {}
140
141 __attribute__ ((weak))
142 void matrix_init_kb(void) {
143   matrix_init_user();
144 }
145
146 __attribute__ ((weak))
147 void matrix_scan_kb(void) {
148   matrix_scan_user();
149 }
150
151 inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
152
153 inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
154
155
156 void matrix_init(void) {
157     // initialize row and col
158     mcp23018_status = init_mcp23018();
159     unselect_rows();
160     init_cols();
161
162   // initialize matrix state: all keys off
163   for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
164     matrix[i]     = 0;
165     raw_matrix[i] = 0;
166   }
167
168 #ifdef DEBUG_MATRIX_SCAN_RATE
169     matrix_timer      = timer_read32();
170     matrix_scan_count = 0;
171 #endif
172     debounce_init(MATRIX_ROWS);
173     matrix_init_quantum();
174 }
175
176 void matrix_power_up(void) {
177     mcp23018_status = init_mcp23018();
178
179     unselect_rows();
180     init_cols();
181
182     // initialize matrix state: all keys off
183     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
184         matrix[i] = 0;
185     }
186
187 #ifdef DEBUG_MATRIX_SCAN_RATE
188     matrix_timer      = timer_read32();
189     matrix_scan_count = 0;
190 #endif
191
192 }
193
194 // Reads and stores a row, returning
195 // whether a change occurred.
196 static inline bool store_raw_matrix_row(uint8_t index) {
197   matrix_row_t temp = read_cols(index);
198   if (raw_matrix[index] != temp) {
199     raw_matrix[index] = temp;
200     return true;
201   }
202   return false;
203 }
204
205
206
207 uint8_t matrix_scan(void) {
208     // TODO: Find what is trashing interrupts
209     enableInterrupts();
210
211     // First we handle the mouse inputs
212 #ifdef BALLER
213     uint8_t pBtn   = PINE & TRKBTN;
214
215     #ifdef DEBUG_BALLER
216     // Compare to previous, mod report
217     if (tbUpCnt + tbDnCnt + tbLtCnt + tbRtCnt != 0)
218         xprintf("U: %d D: %d L: %d R: %d B: %d\n", tbUpCnt, tbDnCnt, tbLtCnt, tbRtCnt, (trkBtnState >> 6));
219     #endif
220
221     // Modify the report
222     report_mouse_t pRprt = pointing_device_get_report();
223
224     // Scroll by default, move on layer
225     if (layer_state == 0) {
226                   pRprt.h += tbLtCnt * SCROLLSTEP; tbLtCnt = 0;
227                   pRprt.h -= tbRtCnt * SCROLLSTEP; tbRtCnt = 0;
228                   pRprt.v -= tbUpCnt * SCROLLSTEP; tbUpCnt = 0;
229                   pRprt.v += tbDnCnt * SCROLLSTEP; tbDnCnt = 0;
230     } else {
231                   pRprt.x -= tbLtCnt * TRKSTEP * (layer_state - 1); tbLtCnt = 0;
232                   pRprt.x += tbRtCnt * TRKSTEP * (layer_state - 1); tbRtCnt = 0;
233                   pRprt.y -= tbUpCnt * TRKSTEP * (layer_state - 1); tbUpCnt = 0;
234                   pRprt.y += tbDnCnt * TRKSTEP * (layer_state - 1); tbDnCnt = 0;
235     }
236
237 #ifdef DEBUG_BALLER
238     if (pRprt.x != 0 || pRprt.y != 0)
239         xprintf("X: %d Y: %d\n", pRprt.x, pRprt.y);
240 #endif
241
242     if ((pBtn != trkBtnState) && ((pBtn >> 6) == 0))  pRprt.buttons |= MOUSE_BTN1;
243     if ((pBtn != trkBtnState) && ((pBtn >> 6) == 1))  pRprt.buttons &= ~MOUSE_BTN1;
244
245     // Save state, push update
246     if (pRprt.x != 0 || pRprt.y != 0 || pRprt.h != 0 || pRprt.v != 0 || (trkBtnState != pBtn))
247         pointing_device_set_report(pRprt);
248
249     trkBtnState = pBtn;
250 #endif
251
252     // Then the keyboard
253     if (mcp23018_status) {  // if there was an error
254         if (++mcp23018_reset_loop == 0) {
255             // if (++mcp23018_reset_loop >= 1300) {
256             // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
257             // this will be approx bit more frequent than once per second
258             print("trying to reset mcp23018\n");
259             mcp23018_status = init_mcp23018();
260             if (mcp23018_status) {
261                 print("left side not responding\n");
262             } else {
263                 print("left side attached\n");
264             }
265         }
266     }
267
268 #ifdef DEBUG_MATRIX_SCAN_RATE
269     matrix_scan_count++;
270
271     uint32_t timer_now = timer_read32();
272     if (TIMER_DIFF_32(timer_now, matrix_timer) > 1000) {
273         print("matrix scan frequency: ");
274         pdec(matrix_scan_count);
275         print("\n");
276
277         matrix_timer      = timer_now;
278         matrix_scan_count = 0;
279     }
280 #endif
281
282     bool changed = false;
283     for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
284         // select rows from left and right hands
285         uint8_t left_index = i;
286         uint8_t right_index = i + MATRIX_ROWS_PER_SIDE;
287         select_row(left_index);
288         select_row(right_index);
289
290         // we don't need a 30us delay anymore, because selecting a
291         // left-hand row requires more than 30us for i2c.
292
293         changed |= store_raw_matrix_row(left_index);
294         changed |= store_raw_matrix_row(right_index);
295
296         unselect_rows();
297     }
298
299     debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
300     matrix_scan_quantum();
301
302     enableInterrupts();
303
304 #ifdef DEBUG_MATRIX
305     for (uint8_t c = 0; c < MATRIX_COLS; c++)
306                 for (uint8_t r = 0; r < MATRIX_ROWS; r++)
307                   if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
308 #endif
309
310     return 1;
311 }
312
313 bool matrix_is_modified(void) // deprecated and evidently not called.
314 {
315     return true;
316 }
317
318 inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
319
320 inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
321
322 void matrix_print(void) {
323     print("\nr/c 0123456789ABCDEF\n");
324     for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
325         phex(row); print(": ");
326         pbin_reverse16(matrix_get_row(row));
327         print("\n");
328     }
329 }
330
331 uint8_t matrix_key_count(void) {
332     uint8_t count = 0;
333     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
334         count += bitpop16(matrix[i]);
335     }
336     return count;
337 }
338
339 // Remember this means ROWS
340 static void  init_cols(void) {
341     // init on mcp23018
342     // not needed, already done as part of init_mcp23018()
343
344     // Input with pull-up(DDR:0, PORT:1)
345     DDRF  &= ~FMASK;
346     PORTF |=  FMASK;
347 }
348
349 static matrix_row_t read_cols(uint8_t row) {
350     if (row < 7) {
351         if (mcp23018_status) { // if there was an error
352             return 0;
353         } else {
354             uint8_t data = 0;
355             mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT);    if (mcp23018_status) goto out;
356             mcp23018_status = i2c_write(GPIOB, I2C_TIMEOUT);             if (mcp23018_status) goto out;
357             mcp23018_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT);     if (mcp23018_status) goto out;
358             mcp23018_status = i2c_read_nack(I2C_TIMEOUT);                if (mcp23018_status < 0) goto out;
359             data = ~((uint8_t)mcp23018_status);
360             mcp23018_status = I2C_STATUS_SUCCESS;
361         out:
362             i2c_stop();
363
364 #ifdef DEBUG_MATRIX
365             if (data != 0x00) xprintf("I2C: %d\n", data);
366 #endif
367             return data;
368         }
369     } else {
370          /* read from teensy
371                 * bitmask is 0b0111001, but we want the lower four
372                 * we'll return 1s for the top two, but that's harmless.
373                 */
374         // So I need to confuckulate all this
375         //return ~(((PIND & DMASK) >> 1  | ((PINC & CMASK) >> 6) | (PIN)));
376         //return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
377         return ~(
378            (((PINF & ROW4) >> 1)
379           | ((PINF & (ROW1 | ROW2 | ROW3)) >> 3))
380         & 0xF);
381     }
382 }
383
384 // Row pin configuration
385 static void unselect_rows(void)
386 {
387     // no need to unselect on mcp23018, because the select step sets all
388     // the other row bits high, and it's not changing to a different
389     // direction
390     // Hi-Z(DDR:0, PORT:0) to unselect
391     DDRB  &= ~(BMASK | TRKMASK);
392     PORTB &= ~(BMASK);
393     DDRC  &= ~CMASK;
394     PORTC &= ~CMASK;
395     DDRD  &= ~DMASK;
396     PORTD &= ~DMASK;
397
398         // Fix trashing of DDRB for TB
399     PORTB |= TRKMASK;
400 }
401
402 static void select_row(uint8_t row)
403 {
404     if (row < 7) {
405         // select on mcp23018
406         if (mcp23018_status) { // do nothing on error
407         } else { // set active row low  : 0 // set other rows hi-Z : 1
408             mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT);        if (mcp23018_status) goto out;
409             mcp23018_status = i2c_write(GPIOA, I2C_TIMEOUT);                 if (mcp23018_status) goto out;
410             mcp23018_status = i2c_write(0xFF & ~(1<<row), I2C_TIMEOUT);      if (mcp23018_status) goto out;
411         out:
412             i2c_stop();
413         }
414     } else {
415         // Output low(DDR:1, PORT:0) to select
416         switch (row) {
417             case 7:
418                 DDRB  |= COL7;
419                 PORTB &= ~COL7;
420                 break;
421             case 8:
422                 DDRB  |= COL8;
423                 PORTB &= ~COL8;
424                 break;
425             case 9:
426                 DDRB  |= COL9;
427                 PORTB &= ~COL9;
428                 break;
429             case 10:
430                 DDRB  |= COL10;
431                 PORTB &= ~COL10;
432                 break;
433             case 11:
434                 DDRD  |= COL11;
435                 PORTD &= ~COL11;
436                 break;
437             case 12:
438                 DDRD  |= COL12;
439                 PORTD &= ~COL12;
440                 break;
441             case 13:
442                 DDRC  |= COL13;
443                 PORTC &= ~COL13;
444                 break;
445         }
446     }
447 }
448
449
450 // Trackball Interrupts
451 static void enableInterrupts(void) {
452     #ifdef BALLER
453       // Set interrupt mask
454       // Set port defs
455       DDRB &= ~TRKMASK;
456       PORTB |= TRKMASK;
457       DDRE &= ~TRKBTN;
458       PORTE |= TRKBTN;
459
460       // Interrupt shenanigans
461       //EIMSK |= (1 << PCIE0);
462       PCMSK0 |= TRKMASK;
463       PCICR |= (1 << PCIE0);
464       sei();
465     #endif
466
467     return;
468 }
469 #ifdef BALLER
470 ISR (PCINT0_vect) {
471   // Don't get fancy, we're in a interrupt here
472   // PCINT reports a interrupt for a change on the bus
473   // We hand the button at scantime for debounce
474   volatile uint8_t pState = PINB & TRKMASK;
475   if ((pState & TRKUP) != (trkState & TRKUP)) tbUpCnt++;
476   if ((pState & TRKDN) != (trkState & TRKDN)) tbDnCnt++;
477   if ((pState & TRKLT) != (trkState & TRKLT)) tbLtCnt++;
478   if ((pState & TRKRT) != (trkState & TRKRT)) tbRtCnt++;
479   trkState = pState;
480
481 }
482 #endif