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/ :)
7 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
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.
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.
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/>.
28 #include "action_layer.h"
32 #include "pointing_device.h"
33 #include QMK_KEYBOARD_H
34 #ifdef DEBUG_MATRIX_SCAN_RATE
39 #include <avr/interrupt.h>
80 // Multiple for mouse moves
85 // multiple for mouse scroll
91 #define BMASK (COL7 | COL8 | COL9 | COL10)
93 #define DMASK (COL11 | COL12)
94 #define FMASK (ROW1 | ROW2 | ROW3 | ROW4)
95 #define RROWMASK (RROW1 | RROW2 | RROW3 | RROW4)
96 #define MCPMASK (COL0 | COL1 | COL2 | COL3 | COL4 | COL5 | COL6)
97 #define TRKMASK (TRKUP | TRKDN | TRKRT | TRKLT)
99 // Trackball interrupts accumulate over here. Processed on scan
100 // Stores prev state of mouse, high bits store direction
101 uint8_t trkState = 0;
102 uint8_t trkBtnState = 0;
104 volatile uint8_t tbUpCnt = 0;
105 volatile uint8_t tbDnCnt = 0;
106 volatile uint8_t tbLtCnt = 0;
107 volatile uint8_t tbRtCnt = 0;
109 /* matrix state(1:on, 0:off) */
110 static matrix_row_t matrix[MATRIX_ROWS];
112 * matrix state(1:on, 0:off)
113 * contains the raw values without debounce filtering of the last read cycle.
115 static matrix_row_t raw_matrix[MATRIX_ROWS];
117 // Debouncing: store for each key the number of scans until it's eligible to
118 // change. When scanning the matrix, ignore any changes in keys that have
119 // already changed in the last DEBOUNCE scans.
120 static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
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);
128 static uint8_t mcp23018_reset_loop;
129 // static uint16_t mcp23018_reset_loop;
131 #ifdef DEBUG_MATRIX_SCAN_RATE
132 uint32_t matrix_timer;
133 uint32_t matrix_scan_count;
137 __attribute__ ((weak))
138 void matrix_init_user(void) {}
140 __attribute__ ((weak))
141 void matrix_scan_user(void) {}
143 __attribute__ ((weak))
144 void matrix_init_kb(void) {
148 __attribute__ ((weak))
149 void matrix_scan_kb(void) {
154 uint8_t matrix_rows(void)
160 uint8_t matrix_cols(void)
166 void matrix_init(void)
168 // initialize row and col
169 mcp23018_status = init_mcp23018();
173 // initialize matrix state: all keys off
174 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
177 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
178 debounce_matrix[i * MATRIX_COLS + j] = 0;
182 #ifdef DEBUG_MATRIX_SCAN_RATE
183 matrix_timer = timer_read32();
184 matrix_scan_count = 0;
186 matrix_init_quantum();
189 void matrix_power_up(void) {
190 mcp23018_status = init_mcp23018();
195 // initialize matrix state: all keys off
196 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
200 #ifdef DEBUG_MATRIX_SCAN_RATE
201 matrix_timer = timer_read32();
202 matrix_scan_count = 0;
207 // Returns a matrix_row_t whose bits are set if the corresponding key should be
208 // eligible to change in this scan.
209 matrix_row_t debounce_mask(matrix_row_t rawcols, uint8_t row) {
210 matrix_row_t result = 0;
211 matrix_row_t change = rawcols ^ raw_matrix[row];
212 raw_matrix[row] = rawcols;
213 for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
214 if (debounce_matrix[row * MATRIX_COLS + i]) {
215 --debounce_matrix[row * MATRIX_COLS + i];
219 if (change & (1 << i)) {
220 debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
226 matrix_row_t debounce_read_cols(uint8_t row) {
227 // Read the row without debouncing filtering and store it for later usage.
228 matrix_row_t cols = read_cols(row);
229 // Get the Debounce mask.
230 matrix_row_t mask = debounce_mask(cols, row);
231 // debounce the row and return the result.
232 return (cols & mask) | (matrix[row] & ~mask);;
235 uint8_t matrix_scan(void)
237 // TODO: Find what is trashing interrupts
240 // First we handle the mouse inputs
242 uint8_t pBtn = PINE & TRKBTN;
245 // Compare to previous, mod report
246 if (tbUpCnt + tbDnCnt + tbLtCnt + tbRtCnt != 0)
247 xprintf("U: %d D: %d L: %d R: %d B: %d\n", tbUpCnt, tbDnCnt, tbLtCnt, tbRtCnt, (trkBtnState >> 6));
251 report_mouse_t pRprt = pointing_device_get_report();
253 // Scroll by default, move on layer
254 if (layer_state == 0) {
255 pRprt.h += tbLtCnt * SCROLLSTEP; tbLtCnt = 0;
256 pRprt.h -= tbRtCnt * SCROLLSTEP; tbRtCnt = 0;
257 pRprt.v -= tbUpCnt * SCROLLSTEP; tbUpCnt = 0;
258 pRprt.v += tbDnCnt * SCROLLSTEP; tbDnCnt = 0;
260 pRprt.x -= tbLtCnt * TRKSTEP * (layer_state - 1); tbLtCnt = 0;
261 pRprt.x += tbRtCnt * TRKSTEP * (layer_state - 1); tbRtCnt = 0;
262 pRprt.y -= tbUpCnt * TRKSTEP * (layer_state - 1); tbUpCnt = 0;
263 pRprt.y += tbDnCnt * TRKSTEP * (layer_state - 1); tbDnCnt = 0;
267 if (pRprt.x != 0 || pRprt.y != 0)
268 xprintf("X: %d Y: %d\n", pRprt.x, pRprt.y);
271 if ((pBtn != trkBtnState) && ((pBtn >> 6) == 0)) pRprt.buttons |= MOUSE_BTN1;
272 if ((pBtn != trkBtnState) && ((pBtn >> 6) == 1)) pRprt.buttons &= ~MOUSE_BTN1;
274 // Save state, push update
275 if (pRprt.x != 0 || pRprt.y != 0 || pRprt.h != 0 || pRprt.v != 0 || (trkBtnState != pBtn))
276 pointing_device_set_report(pRprt);
282 if (mcp23018_status) { // if there was an error
283 if (++mcp23018_reset_loop == 0) {
284 // if (++mcp23018_reset_loop >= 1300) {
285 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
286 // this will be approx bit more frequent than once per second
287 print("trying to reset mcp23018\n");
288 mcp23018_status = init_mcp23018();
289 if (mcp23018_status) {
290 print("left side not responding\n");
292 print("left side attached\n");
297 #ifdef DEBUG_MATRIX_SCAN_RATE
299 uint32_t timer_now = timer_read32();
300 if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
301 print("matrix scan frequency: ");
302 pdec(matrix_scan_count);
305 matrix_timer = timer_now;
306 matrix_scan_count = 0;
309 for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
311 // and select on left hand
312 select_row(i + MATRIX_ROWS_PER_SIDE);
313 // we don't need a 30us delay anymore, because selecting a
314 // left-hand row requires more than 30us for i2c.
316 // grab cols from left hand
317 matrix[i] = debounce_read_cols(i);
318 // grab cols from right hand
319 matrix[i + MATRIX_ROWS_PER_SIDE] = debounce_read_cols(i + MATRIX_ROWS_PER_SIDE);
324 matrix_scan_quantum();
328 for (uint8_t c = 0; c < MATRIX_COLS; c++)
329 for (uint8_t r = 0; r < MATRIX_ROWS; r++)
330 if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
336 bool matrix_is_modified(void) // deprecated and evidently not called.
342 bool matrix_is_on(uint8_t row, uint8_t col)
344 return (matrix[row] & ((matrix_row_t)1<<col));
348 matrix_row_t matrix_get_row(uint8_t row)
353 void matrix_print(void)
355 print("\nr/c 0123456789ABCDEF\n");
356 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
357 phex(row); print(": ");
358 pbin_reverse16(matrix_get_row(row));
363 uint8_t matrix_key_count(void)
366 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
367 count += bitpop16(matrix[i]);
372 // Remember this means ROWS
373 static void init_cols(void)
376 // not needed, already done as part of init_mcp23018()
378 // Input with pull-up(DDR:0, PORT:1)
383 static matrix_row_t read_cols(uint8_t row)
386 if (mcp23018_status) { // if there was an error
390 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
391 mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
392 mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
393 mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
394 data = ~((uint8_t)mcp23018_status);
395 mcp23018_status = I2C_STATUS_SUCCESS;
400 if (data != 0x00) xprintf("I2C: %d\n", data);
406 * bitmask is 0b0111001, but we want the lower four
407 * we'll return 1s for the top two, but that's harmless.
409 // So I need to confuckulate all this
410 //return ~(((PIND & DMASK) >> 1 | ((PINC & CMASK) >> 6) | (PIN)));
411 //return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
413 (((PINF & ROW4) >> 1)
414 | ((PINF & (ROW1 | ROW2 | ROW3)) >> 3))
419 // Row pin configuration
420 static void unselect_rows(void)
422 // no need to unselect on mcp23018, because the select step sets all
423 // the other row bits high, and it's not changing to a different
425 // Hi-Z(DDR:0, PORT:0) to unselect
426 DDRB &= ~(BMASK | TRKMASK);
433 // Fix trashing of DDRB for TB
437 static void select_row(uint8_t row)
440 // select on mcp23018
441 if (mcp23018_status) { // do nothing on error
442 } else { // set active row low : 0 // set other rows hi-Z : 1
443 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
444 mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
445 mcp23018_status = i2c_write(0xFF & ~(1<<row), ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
450 // Output low(DDR:1, PORT:0) to select
485 // Trackball Interrupts
486 static void enableInterrupts(void) {
488 // Set interrupt mask
495 // Interrupt shenanigans
496 //EIMSK |= (1 << PCIE0);
498 PCICR |= (1 << PCIE0);
506 // Don't get fancy, we're in a interrupt here
507 // PCINT reports a interrupt for a change on the bus
508 // We hand the button at scantime for debounce
509 volatile uint8_t pState = PINB & TRKMASK;
510 if ((pState & TRKUP) != (trkState & TRKUP)) tbUpCnt++;
511 if ((pState & TRKDN) != (trkState & TRKDN)) tbDnCnt++;
512 if ((pState & TRKLT) != (trkState & TRKLT)) tbLtCnt++;
513 if ((pState & TRKRT) != (trkState & TRKRT)) tbRtCnt++;