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 QMK_KEYBOARD_H
45 #define BMASK (COL7 | COL8 | COL9 | COL6)
46 #define DMASK (COL10 | COL11)
49 /* matrix state(1:on, 0:off) */
50 static matrix_row_t matrix[MATRIX_ROWS];
52 * matrix state(1:on, 0:off)
53 * contains the raw values without debounce filtering of the last read cycle.
55 static matrix_row_t raw_matrix[MATRIX_ROWS];
57 // Debouncing: store for each key the number of scans until it's eligible to
58 // change. When scanning the matrix, ignore any changes in keys that have
59 // already changed in the last DEBOUNCE scans.
60 static uint8_t debounce_matrix[MATRIX_ROWS * MATRIX_COLS];
62 static matrix_row_t read_cols(uint8_t row);
63 static void init_cols(void);
64 static void unselect_rows(void);
65 static void select_row(uint8_t row);
67 static uint8_t mcp23018_reset_loop;
68 // static uint16_t mcp23018_reset_loop;
70 __attribute__ ((weak))
71 void matrix_init_user(void) {}
73 __attribute__ ((weak))
74 void matrix_scan_user(void) {}
76 __attribute__ ((weak))
77 void matrix_init_kb(void) {
81 __attribute__ ((weak))
82 void matrix_scan_kb(void) {
87 uint8_t matrix_rows(void)
93 uint8_t matrix_cols(void)
99 void matrix_init(void)
101 // initialize row and col
102 mcp23018_status = init_mcp23018();
106 // initialize matrix state: all keys off
107 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
110 for (uint8_t j=0; j < MATRIX_COLS; ++j) {
111 debounce_matrix[i * MATRIX_COLS + j] = 0;
115 matrix_init_quantum();
118 void matrix_power_up(void) {
119 mcp23018_status = init_mcp23018();
124 // initialize matrix state: all keys off
125 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
130 // Returns a matrix_row_t whose bits are set if the corresponding key should be
131 // eligible to change in this scan.
132 matrix_row_t debounce_mask(matrix_row_t rawcols, uint8_t row) {
133 matrix_row_t result = 0;
134 matrix_row_t change = rawcols ^ raw_matrix[row];
135 raw_matrix[row] = rawcols;
136 for (uint8_t i = 0; i < MATRIX_COLS; ++i) {
137 if (debounce_matrix[row * MATRIX_COLS + i]) {
138 --debounce_matrix[row * MATRIX_COLS + i];
142 if (change & (1 << i)) {
143 debounce_matrix[row * MATRIX_COLS + i] = DEBOUNCE;
149 matrix_row_t debounce_read_cols(uint8_t row) {
150 // Read the row without debouncing filtering and store it for later usage.
151 matrix_row_t cols = read_cols(row);
152 // Get the Debounce mask.
153 matrix_row_t mask = debounce_mask(cols, row);
154 // debounce the row and return the result.
155 return (cols & mask) | (matrix[row] & ~mask);;
158 uint8_t matrix_scan(void)
161 if (mcp23018_status) { // if there was an error
162 if (++mcp23018_reset_loop == 0) {
163 // if (++mcp23018_reset_loop >= 1300) {
164 // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
165 // this will be approx bit more frequent than once per second
166 print("trying to reset mcp23018\n");
167 mcp23018_status = init_mcp23018();
168 if (mcp23018_status) {
169 print("left side not responding\n");
171 print("left side attached\n");
176 for (uint8_t i = 0; i < MATRIX_ROWS_PER_SIDE; i++) {
178 // and select on left hand
179 select_row(i + MATRIX_ROWS_PER_SIDE);
180 // we don't need a 30us delay anymore, because selecting a
181 // left-hand row requires more than 30us for i2c.
183 // grab cols from left hand
184 matrix[i] = debounce_read_cols(i);
185 // grab cols from right hand
186 matrix[i + MATRIX_ROWS_PER_SIDE] = debounce_read_cols(i + MATRIX_ROWS_PER_SIDE);
191 matrix_scan_quantum();
194 for (uint8_t c = 0; c < MATRIX_COLS; c++)
195 for (uint8_t r = 0; r < MATRIX_ROWS; r++)
196 if (matrix_is_on(r, c)) xprintf("r:%d c:%d \n", r, c);
202 bool matrix_is_modified(void) // deprecated and evidently not called.
208 bool matrix_is_on(uint8_t row, uint8_t col)
210 return (matrix[row] & ((matrix_row_t)1<<col));
214 matrix_row_t matrix_get_row(uint8_t row)
219 void matrix_print(void)
221 print("\nr/c 0123456789ABCDEF\n");
222 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
223 phex(row); print(": ");
224 pbin_reverse16(matrix_get_row(row));
229 uint8_t matrix_key_count(void)
232 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
233 count += bitpop16(matrix[i]);
238 // Remember this means ROWS
239 static void init_cols(void)
242 // not needed, already done as part of init_mcp23018()
244 // Input with pull-up(DDR:0, PORT:1)
249 static matrix_row_t read_cols(uint8_t row)
252 if (mcp23018_status) { // if there was an error
256 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
257 mcp23018_status = i2c_write(GPIOB, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
258 mcp23018_status = i2c_start(I2C_ADDR_READ, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
259 mcp23018_status = i2c_read_nack(ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status < 0) goto out;
260 data = (~((uint8_t)mcp23018_status) >> 2) & 0x01 ;
261 mcp23018_status = I2C_STATUS_SUCCESS;
266 if (data != 0x00) xprintf("I2C: %d\n", data);
271 // Read using bitmask
272 return ~((((PINF & ROW1) >> 5)) & 0x1);
276 // Row pin configuration
277 static void unselect_rows(void)
279 // no need to unselect on mcp23018, because the select step sets all
280 // the other row bits high, and it's not changing to a different
282 // Hi-Z(DDR:0, PORT:0) to unselect
289 static void select_row(uint8_t row)
292 // select on mcp23018
293 if (mcp23018_status) { // do nothing on error
294 // Read using bitmask
295 } else { // set active row low : 0 // set other rows hi-Z : 1
296 mcp23018_status = i2c_start(I2C_ADDR_WRITE, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
297 mcp23018_status = i2c_write(GPIOA, ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
298 mcp23018_status = i2c_write(~(1<<row), ERGODOX_EZ_I2C_TIMEOUT); if (mcp23018_status) goto out;
303 // Output low(DDR:1, PORT:0) to select