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"
28 #include "keymap_steno.h"
29 #include QMK_KEYBOARD_H
30 #ifdef DEBUG_MATRIX_SCAN_RATE
67 #define BMASK (COL7 | COL8 | COL9 | COL10)
69 #define DMASK (COL11 | COL12)
70 #define FMASK (ROW1 | ROW2 | ROW3 | ROW4)
71 #define RROWMASK (RROW1 | RROW2 | RROW3 | RROW4)
72 #define MCPMASK (COL0 | COL1 | COL2 | COL3 | COL4 | COL5 | COL6)
74 /* matrix state(1:on, 0:off) */
75 static matrix_row_t matrix[MATRIX_ROWS];
77 * matrix state(1:on, 0:off)
78 * contains the raw values without debounce filtering of the last read cycle.
80 static matrix_row_t raw_matrix[MATRIX_ROWS];
82 // Debouncing: store for each key the number of scans until it's eligible to
83 // change. When scanning the matrix, ignore any changes in keys that have
84 // already changed in the last DEBOUNCE scans.
85 static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
87 static matrix_row_t read_cols(uint8_t row);
88 static void init_cols(void);
89 static void unselect_rows(void);
90 static void select_row(uint8_t row);
92 static uint8_t mcp23018_reset_loop;
93 // static uint16_t mcp23018_reset_loop;
95 #ifdef DEBUG_MATRIX_SCAN_RATE
96 uint32_t matrix_timer;
97 uint32_t matrix_scan_count;
101 __attribute__ ((weak))
102 void matrix_init_user(void) {}
104 __attribute__ ((weak))
105 void matrix_scan_user(void) {}
107 __attribute__ ((weak))
108 void matrix_init_kb(void) {
112 __attribute__ ((weak))
113 void matrix_scan_kb(void) {
118 uint8_t matrix_rows(void)
124 uint8_t matrix_cols(void)
130 void matrix_init(void)
132 // initialize row and col
133 mcp23018_status = init_mcp23018();
137 // initialize matrix state: all keys off
138 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
141 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
142 debounce_matrix[i * MATRIX_COLS + j] = 0;
146 #ifdef DEBUG_MATRIX_SCAN_RATE
147 matrix_timer = timer_read32();
148 matrix_scan_count = 0;
150 matrix_init_quantum();
153 void matrix_power_up(void) {
154 mcp23018_status = init_mcp23018();
159 // initialize matrix state: all keys off
160 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;
171 // Returns a matrix_row_t whose bits are set if the corresponding key should be
172 // eligible to change in this scan.
173 matrix_row_t debounce_mask(matrix_row_t rawcols, uint8_t row) {
174 matrix_row_t result = 0;
175 matrix_row_t change = rawcols ^ raw_matrix[row];
176 raw_matrix[row] = rawcols;
177 for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
178 if (debounce_matrix[row * MATRIX_COLS + i]) {
179 --debounce_matrix[row * MATRIX_COLS + i];
183 if (change & (1 << i)) {
184 debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
190 matrix_row_t debounce_read_cols(uint8_t row) {
191 // Read the row without debouncing filtering and store it for later usage.
192 matrix_row_t cols = read_cols(row);
193 // Get the Debounce mask.
194 matrix_row_t mask = debounce_mask(cols, row);
195 // debounce the row and return the result.
196 return (cols & mask) | (matrix[row] & ~mask);;
199 uint8_t matrix_scan(void)
202 if (mcp23018_status) { // if there was an error
203 if (++mcp23018_reset_loop == 0) {
204 // if (++mcp23018_reset_loop >= 1300) {
205 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
206 // this will be approx bit more frequent than once per second
207 print("trying to reset mcp23018\n");
208 mcp23018_status = init_mcp23018();
209 if (mcp23018_status) {
210 print("left side not responding\n");
212 print("left side attached\n");
217 #ifdef DEBUG_MATRIX_SCAN_RATE
219 uint32_t timer_now = timer_read32();
220 if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) {
221 print("matrix scan frequency: ");
222 pdec(matrix_scan_count);
225 matrix_timer = timer_now;
226 matrix_scan_count = 0;
229 for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
231 // and select on left hand
232 select_row(i + MATRIX_ROWS_PER_SIDE);
233 // we don't need a 30us delay anymore, because selecting a
234 // left-hand row requires more than 30us for i2c.
236 // grab cols from left hand
237 matrix[i] = debounce_read_cols(i);
238 // grab cols from right hand
239 matrix[i + MATRIX_ROWS_PER_SIDE] = debounce_read_cols(i + MATRIX_ROWS_PER_SIDE);
244 matrix_scan_quantum();
247 for (uint8_t c = 0; c < MATRIX_COLS; c++)
248 for (uint8_t r = 0; r < MATRIX_ROWS; r++)
249 if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
255 bool matrix_is_modified(void) // deprecated and evidently not called.
261 bool matrix_is_on(uint8_t row, uint8_t col)
263 return (matrix[row] & ((matrix_row_t)1<<col));
267 matrix_row_t matrix_get_row(uint8_t row)
272 void matrix_print(void)
274 print("\nr/c 0123456789ABCDEF\n");
275 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
276 phex(row); print(": ");
277 pbin_reverse16(matrix_get_row(row));
282 uint8_t matrix_key_count(void)
285 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
286 count += bitpop16(matrix[i]);
291 // Remember this means ROWS
292 static void init_cols(void)
295 // not needed, already done as part of init_mcp23018()
297 // Input with pull-up(DDR:0, PORT:1)
302 static matrix_row_t read_cols(uint8_t row)
305 if (mcp23018_status) { // if there was an error
309 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
310 mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
311 mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
312 mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
313 data = ~((uint8_t)mcp23018_status);
314 mcp23018_status = I2C_STATUS_SUCCESS;
319 if (data != 0x00) xprintf("I2C: %d\n", data);
325 * bitmask is 0b0111001, but we want the lower four
326 * we'll return 1s for the top two, but that's harmless.
328 // So I need to confuckulate all this
329 //return ~(((PIND & DMASK) >> 1 | ((PINC & CMASK) >> 6) | (PIN)));
330 //return ~((PINF & 0x03) | ((PINF & 0xF0) >> 2));
332 (((PINF & ROW4) >> 1)
333 | ((PINF & (ROW1 | ROW2 | ROW3)) >> 3))
338 // Row pin configuration
339 static void unselect_rows(void)
341 // no need to unselect on mcp23018, because the select step sets all
342 // the other row bits high, and it's not changing to a different
344 // Hi-Z(DDR:0, PORT:0) to unselect
353 static void select_row(uint8_t row)
356 // select on mcp23018
357 if (mcp23018_status) { // do nothing on error
358 } else { // set active row low : 0 // set other rows hi-Z : 1
359 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
360 mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
361 mcp23018_status = i2c_write(0xFF & ~(1<<row), ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
366 // Output low(DDR:1, PORT:0) to select