3 Copyright 2013 Oleg Kostyuk <cub.uanic@gmail.com>
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.
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.
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/>.
24 #include "action_layer.h"
29 #include QMK_KEYBOARD_H
30 #ifdef DEBUG_MATRIX_SCAN_RATE
35 #include <avr/interrupt.h>
36 #include "pointing_device.h"
77 // Multiple for mouse moves
82 // multiple for mouse scroll
88 #define BMASK (COL7 | COL8 | COL9 | COL10)
90 #define DMASK (COL11 | COL12)
91 #define FMASK (ROW1 | ROW2 | ROW3 | ROW4)
92 #define RROWMASK (RROW1 | RROW2 | RROW3 | RROW4)
93 #define MCPMASK (COL0 | COL1 | COL2 | COL3 | COL4 | COL5 | COL6)
94 #define TRKMASK (TRKUP | TRKDN | TRKRT | TRKLT)
96 // Trackball interrupts accumulate over here. Processed on scan
97 // Stores prev state of mouse, high bits store direction
99 uint8_t trkBtnState = 0;
101 volatile uint8_t tbUpCnt = 0;
102 volatile uint8_t tbDnCnt = 0;
103 volatile uint8_t tbLtCnt = 0;
104 volatile uint8_t tbRtCnt = 0;
106 /* matrix state(1:on, 0:off) */
107 static matrix_row_t matrix[MATRIX_ROWS];
109 * matrix state(1:on, 0:off)
110 * contains the raw values without debounce filtering of the last read cycle.
112 static matrix_row_t raw_matrix[MATRIX_ROWS];
114 // Debouncing: store for each key the number of scans until it's eligible to
115 // change. When scanning the matrix, ignore any changes in keys that have
116 // already changed in the last DEBOUNCE scans.
118 static matrix_row_t read_cols(uint8_t row);
119 static void init_cols(void);
120 static void unselect_rows(void);
121 static void select_row(uint8_t row);
122 static void enableInterrupts(void);
124 static uint8_t mcp23018_reset_loop;
125 // static uint16_t mcp23018_reset_loop;
127 #ifdef DEBUG_MATRIX_SCAN_RATE
128 uint32_t matrix_timer;
129 uint32_t matrix_scan_count;
133 __attribute__ ((weak)) void matrix_init_user(void) {}
135 __attribute__ ((weak)) void matrix_scan_user(void) {}
137 __attribute__ ((weak))
138 void matrix_init_kb(void) {
142 __attribute__ ((weak))
143 void matrix_scan_kb(void) {
147 inline uint8_t matrix_rows(void) { return MATRIX_ROWS; }
149 inline uint8_t matrix_cols(void) { return MATRIX_COLS; }
152 void matrix_init(void) {
153 // initialize row and col
154 mcp23018_status = init_mcp23018();
158 // initialize matrix state: all keys off
159 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
164 #ifdef DEBUG_MATRIX_SCAN_RATE
165 matrix_timer = timer_read32();
166 matrix_scan_count = 0;
168 debounce_init(MATRIX_ROWS);
169 matrix_init_quantum();
172 void matrix_power_up(void) {
173 mcp23018_status = init_mcp23018();
178 // initialize matrix state: all keys off
179 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
183 #ifdef DEBUG_MATRIX_SCAN_RATE
184 matrix_timer = timer_read32();
185 matrix_scan_count = 0;
190 // Reads and stores a row, returning
191 // whether a change occurred.
192 static inline bool store_raw_matrix_row(uint8_t index) {
193 matrix_row_t temp = read_cols(index);
194 if (raw_matrix[index] != temp) {
195 raw_matrix[index] = temp;
203 uint8_t matrix_scan(void) {
204 // TODO: Find what is trashing interrupts
207 // First we handle the mouse inputs
209 uint8_t pBtn = PINE & TRKBTN;
212 // Compare to previous, mod report
213 if (tbUpCnt + tbDnCnt + tbLtCnt + tbRtCnt != 0)
214 xprintf("U: %d D: %d L: %d R: %d B: %d\n", tbUpCnt, tbDnCnt, tbLtCnt, tbRtCnt, (trkBtnState >> 6));
218 report_mouse_t pRprt = pointing_device_get_report();
220 // Scroll by default, move on layer
221 if (layer_state == 0) {
222 pRprt.h += tbLtCnt * SCROLLSTEP; tbLtCnt = 0;
223 pRprt.h -= tbRtCnt * SCROLLSTEP; tbRtCnt = 0;
224 pRprt.v -= tbUpCnt * SCROLLSTEP; tbUpCnt = 0;
225 pRprt.v += tbDnCnt * SCROLLSTEP; tbDnCnt = 0;
227 pRprt.x -= tbLtCnt * TRKSTEP * (layer_state - 1); tbLtCnt = 0;
228 pRprt.x += tbRtCnt * TRKSTEP * (layer_state - 1); tbRtCnt = 0;
229 pRprt.y -= tbUpCnt * TRKSTEP * (layer_state - 1); tbUpCnt = 0;
230 pRprt.y += tbDnCnt * TRKSTEP * (layer_state - 1); tbDnCnt = 0;
234 if (pRprt.x != 0 || pRprt.y != 0)
235 xprintf("X: %d Y: %d\n", pRprt.x, pRprt.y);
238 if ((pBtn != trkBtnState) && ((pBtn >> 6) == 0)) pRprt.buttons |= MOUSE_BTN1;
239 if ((pBtn != trkBtnState) && ((pBtn >> 6) == 1)) pRprt.buttons &= ~MOUSE_BTN1;
241 // Save state, push update
242 if (pRprt.x != 0 || pRprt.y != 0 || pRprt.h != 0 || pRprt.v != 0 || (trkBtnState != pBtn))
243 pointing_device_set_report(pRprt);
249 if (mcp23018_status) { // if there was an error
250 if (++mcp23018_reset_loop == 0) {
251 // if (++mcp23018_reset_loop >= 1300) {
252 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
253 // this will be approx bit more frequent than once per second
254 print("trying to reset mcp23018\n");
255 mcp23018_status = init_mcp23018();
256 if (mcp23018_status) {
257 print("left side not responding\n");
259 print("left side attached\n");
264 #ifdef DEBUG_MATRIX_SCAN_RATE
267 uint32_t timer_now = timer_read32();
268 if (TIMER_DIFF_32(timer_now, matrix_timer) > 1000) {
269 print("matrix scan frequency: ");
270 pdec(matrix_scan_count);
273 matrix_timer = timer_now;
274 matrix_scan_count = 0;
278 bool changed = false;
279 for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
280 // select rows from left and right hands
281 uint8_t left_index = i;
282 uint8_t right_index = i + MATRIX_ROWS_PER_SIDE;
283 select_row(left_index);
284 select_row(right_index);
286 // we don't need a 30us delay anymore, because selecting a
287 // left-hand row requires more than 30us for i2c.
289 changed |= store_raw_matrix_row(left_index);
290 changed |= store_raw_matrix_row(right_index);
295 debounce(raw_matrix, matrix, MATRIX_ROWS, changed);
296 matrix_scan_quantum();
301 for (uint8_t c = 0; c < MATRIX_COLS; c++)
302 for (uint8_t r = 0; r < MATRIX_ROWS; r++)
303 if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
309 bool matrix_is_modified(void) // deprecated and evidently not called.
314 inline bool matrix_is_on(uint8_t row, uint8_t col) { return (matrix[row] & ((matrix_row_t)1 << col)); }
316 inline matrix_row_t matrix_get_row(uint8_t row) { return matrix[row]; }
318 void matrix_print(void) {
319 print("\nr/c 0123456789ABCDEF\n");
320 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
321 phex(row); print(": ");
322 pbin_reverse16(matrix_get_row(row));
327 uint8_t matrix_key_count(void) {
329 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
330 count += bitpop16(matrix[i]);
335 // Remember this means ROWS
336 static void init_cols(void) {
338 // not needed, already done as part of init_mcp23018()
340 // Input with pull-up(DDR:0, PORT:1)
345 static matrix_row_t read_cols(uint8_t row) {
347 if (mcp23018_status) { // if there was an error
351 mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
352 mcp23018_status = i2c_write(GPIOB, I2C_TIMEOUT); if (mcp23018_status) goto out;
353 mcp23018_status = i2c_start(I2C_ADDR_READ, I2C_TIMEOUT); if (mcp23018_status) goto out;
354 mcp23018_status = i2c_read_nack(I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
355 data = ~((uint8_t)mcp23018_status);
356 mcp23018_status = I2C_STATUS_SUCCESS;
361 if (data != 0x00) xprintf("I2C: %d\n", data);
367 * bitmask is 0b0111001, but we want the lower four
368 * we'll return 1s for the top two, but that's harmless.
370 // So I need to confuckulate all this
371 //return ~(((PIND & DMASK) >> 1 | ((PINC & CMASK) >> 6) | (PIN)));
372 //return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
374 (((PINF & ROW4) >> 1)
375 | ((PINF & (ROW1 | ROW2 | ROW3)) >> 3))
380 // Row pin configuration
381 static void unselect_rows(void)
383 // no need to unselect on mcp23018, because the select step sets all
384 // the other row bits high, and it's not changing to a different
386 // Hi-Z(DDR:0, PORT:0) to unselect
387 DDRB &= ~(BMASK | TRKMASK);
394 // Fix trashing of DDRB for TB
398 static void select_row(uint8_t row)
401 // select on mcp23018
402 if (mcp23018_status) { // do nothing on error
403 } else { // set active row low : 0 // set other rows hi-Z : 1
404 mcp23018_status = i2c_start(I2C_ADDR_WRITE, I2C_TIMEOUT); if (mcp23018_status) goto out;
405 mcp23018_status = i2c_write(GPIOA, I2C_TIMEOUT); if (mcp23018_status) goto out;
406 mcp23018_status = i2c_write(0xFF & ~(1<<row), I2C_TIMEOUT); if (mcp23018_status) goto out;
411 // Output low(DDR:1, PORT:0) to select
446 // Trackball Interrupts
447 static void enableInterrupts(void) {
449 // Set interrupt mask
456 // Interrupt shenanigans
457 //EIMSK |= (1 << PCIE0);
459 PCICR |= (1 << PCIE0);
467 // Don't get fancy, we're in a interrupt here
468 // PCINT reports a interrupt for a change on the bus
469 // We hand the button at scantime for debounce
470 volatile uint8_t pState = PINB & TRKMASK;
471 if ((pState & TRKUP) != (trkState & TRKUP)) tbUpCnt++;
472 if ((pState & TRKDN) != (trkState & TRKDN)) tbDnCnt++;
473 if ((pState & TRKLT) != (trkState & TRKLT)) tbLtCnt++;
474 if ((pState & TRKRT) != (trkState & TRKRT)) tbRtCnt++;