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"
33 #include QMK_KEYBOARD_H
34 #ifdef DEBUG_MATRIX_SCAN_RATE
39 #include <avr/interrupt.h>
40 #include "pointing_device.h"
81 // Multiple for mouse moves
86 // multiple for mouse scroll
92 #define BMASK (COL7 | COL8 | COL9 | COL10)
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)
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;
105 volatile uint8_t tbUpCnt = 0;
106 volatile uint8_t tbDnCnt = 0;
107 volatile uint8_t tbLtCnt = 0;
108 volatile uint8_t tbRtCnt = 0;
110 /* matrix state(1:on, 0:off) */
111 static matrix_row_t matrix[MATRIX_ROWS];
113 * matrix state(1:on, 0:off)
114 * contains the raw values without debounce filtering of the last read cycle.
116 static matrix_row_t raw_matrix[MATRIX_ROWS];
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.
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)) void matrix_init_user(void) {}
139 __attribute__ ((weak)) void matrix_scan_user(void) {}
141 __attribute__ ((weak))
142 void matrix_init_kb(void) {
146 __attribute__ ((weak))
147 void matrix_scan_kb(void) {
151 inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
153 inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
156 void matrix_init(void) {
157 // initialize row and col
158 mcp23018_status = init_mcp23018();
162 // initialize matrix state: all keys off
163 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
168 #ifdef DEBUG_MATRIX_SCAN_RATE
169 matrix_timer = timer_read32();
170 matrix_scan_count = 0;
172 debounce_init(MATRIX_ROWS);
173 matrix_init_quantum();
176 void matrix_power_up(void) {
177 mcp23018_status = init_mcp23018();
182 // initialize matrix state: all keys off
183 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
187 #ifdef DEBUG_MATRIX_SCAN_RATE
188 matrix_timer = timer_read32();
189 matrix_scan_count = 0;
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;
207 uint8_t matrix_scan(void) {
208 // TODO: Find what is trashing interrupts
211 // First we handle the mouse inputs
213 uint8_t pBtn = PINE & TRKBTN;
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));
222 report_mouse_t pRprt = pointing_device_get_report();
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;
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;
238 if (pRprt.x != 0 || pRprt.y != 0)
239 xprintf("X: %d Y: %d\n", pRprt.x, pRprt.y);
242 if ((pBtn != trkBtnState) && ((pBtn >> 6) == 0)) pRprt.buttons |= MOUSE_BTN1;
243 if ((pBtn != trkBtnState) && ((pBtn >> 6) == 1)) pRprt.buttons &= ~MOUSE_BTN1;
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);
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");
263 print("left side attached\n");
268 #ifdef DEBUG_MATRIX_SCAN_RATE
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);
277 matrix_timer = timer_now;
278 matrix_scan_count = 0;
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);
290 // we don't need a 30us delay anymore, because selecting a
291 // left-hand row requires more than 30us for i2c.
293 changed |= store_raw_matrix_row(left_index);
294 changed |= store_raw_matrix_row(right_index);
299 debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
300 matrix_scan_quantum();
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);
313 bool matrix_is_modified(void) // deprecated and evidently not called.
318 inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
320 inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
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));
331 uint8_t matrix_key_count(void) {
333 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
334 count += bitpop16(matrix[i]);
339 // Remember this means ROWS
340 static void init_cols(void) {
342 // not needed, already done as part of init_mcp23018()
344 // Input with pull-up(DDR:0, PORT:1)
349 static matrix_row_t read_cols(uint8_t row) {
351 if (mcp23018_status) { // if there was an error
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;
365 if (data != 0x00) xprintf("I2C: %d\n", data);
371 * bitmask is 0b0111001, but we want the lower four
372 * we'll return 1s for the top two, but that's harmless.
374 // So I need to confuckulate all this
375 //return ~(((PIND & DMASK) >> 1 | ((PINC & CMASK) >> 6) | (PIN)));
376 //return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
378 (((PINF & ROW4) >> 1)
379 | ((PINF & (ROW1 | ROW2 | ROW3)) >> 3))
384 // Row pin configuration
385 static void unselect_rows(void)
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
390 // Hi-Z(DDR:0, PORT:0) to unselect
391 DDRB &= ~(BMASK | TRKMASK);
398 // Fix trashing of DDRB for TB
402 static void select_row(uint8_t row)
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;
415 // Output low(DDR:1, PORT:0) to select
450 // Trackball Interrupts
451 static void enableInterrupts(void) {
453 // Set interrupt mask
460 // Interrupt shenanigans
461 //EIMSK |= (1 << PCIE0);
463 PCICR |= (1 << PCIE0);
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++;