2 Copyright 2012-2017 Jun Wako, Jack Humbert
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
31 /* Set 0 if debouncing isn't needed */
38 static uint16_t debouncing_time;
39 static bool debouncing = false;
42 #if (MATRIX_COLS <= 8)
43 # define print_matrix_header() print("\nr/c 01234567\n")
44 # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
45 # define matrix_bitpop(i) bitpop(matrix[i])
46 # define ROW_SHIFTER ((uint8_t)1)
47 #elif (MATRIX_COLS <= 16)
48 # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
49 # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
50 # define matrix_bitpop(i) bitpop16(matrix[i])
51 # define ROW_SHIFTER ((uint16_t)1)
52 #elif (MATRIX_COLS <= 32)
53 # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
54 # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
55 # define matrix_bitpop(i) bitpop32(matrix[i])
56 # define ROW_SHIFTER ((uint32_t)1)
60 extern const matrix_row_t matrix_mask[];
63 static const uint8_t col_pins[ATMEGA_COLS] = MATRIX_COL_PINS;
64 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
66 /* matrix state(1:on, 0:off) */
67 static matrix_row_t matrix[MATRIX_ROWS];
68 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
69 static uint8_t mcp23018_reset_loop;
72 static void init_cols(void);
73 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
74 static void unselect_rows(void);
75 static void select_row(uint8_t row);
77 __attribute__ ((weak))
78 void matrix_init_quantum(void) {
82 __attribute__ ((weak))
83 void matrix_scan_quantum(void) {
87 __attribute__ ((weak))
88 void matrix_init_kb(void) {
92 __attribute__ ((weak))
93 void matrix_scan_kb(void) {
97 __attribute__ ((weak))
98 void matrix_init_user(void) {
101 __attribute__ ((weak))
102 void matrix_scan_user(void) {
106 uint8_t matrix_rows(void) {
111 uint8_t matrix_cols(void) {
115 void matrix_init(void) {
116 mcp23018_status = true;
118 /* initialize row and col */
122 /* initialize matrix state: all keys off */
123 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
125 matrix_debouncing[i] = 0;
128 matrix_init_quantum();
131 uint8_t matrix_scan(void)
133 if (mcp23018_status) {
134 /* if there was an error */
135 if (++mcp23018_reset_loop == 0) {
136 /* since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
137 this will be approx bit more frequent than once per second */
138 print("trying to reset mcp23018\n");
139 mcp23018_status = init_mcp23018();
140 if (mcp23018_status) {
141 print("left side not responding\n");
143 print("left side attached\n");
148 /* Set row, read cols */
149 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
151 bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
153 if (matrix_changed) {
155 debouncing_time = timer_read();
158 read_cols_on_row(matrix, current_row);
163 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
164 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
165 matrix[i] = matrix_debouncing[i];
171 matrix_scan_quantum();
175 bool matrix_is_modified(void)
178 if (debouncing) return false;
184 bool matrix_is_on(uint8_t row, uint8_t col)
186 return (matrix[row] & ((matrix_row_t)1<<col));
190 matrix_row_t matrix_get_row(uint8_t row)
192 /* Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
193 switch blocker installed and the switch is always pressed. */
195 return matrix[row] & matrix_mask[row];
201 void matrix_print(void)
203 print_matrix_header();
205 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
206 phex(row); print(": ");
207 print_matrix_row(row);
212 uint8_t matrix_key_count(void)
215 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
216 count += matrix_bitpop(i);
221 static void init_cols(void)
223 for(uint8_t x = 0; x < ATMEGA_COLS; x++) {
224 uint8_t pin = col_pins[x];
225 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); /* IN */
226 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); /* HI */
230 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
232 /* Store last value of row prior to reading */
233 matrix_row_t last_row_value = current_matrix[current_row];
235 /* Clear data in matrix row */
236 current_matrix[current_row] = 0;
238 /* Select row and wait for row selecton to stabilize */
239 select_row(current_row);
242 if (mcp23018_status) {
243 /* if there was an error */
247 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
248 mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
249 mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out;
250 data = i2c_readNak();
254 current_matrix[current_row] |= (data << 8);
257 /* For each col... */
258 for(uint8_t col_index = 0; col_index < ATMEGA_COLS; col_index++) {
259 /* Select the col pin to read (active low) */
260 uint8_t pin = col_pins[col_index];
261 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
263 /* Populate the matrix row with the state of the col pin */
264 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
270 return (last_row_value != current_matrix[current_row]);
273 static void select_row(uint8_t row)
275 if (mcp23018_status) {
276 /* if there was an error do nothing */
278 /* set active row low : 0
279 set active row output : 1
280 set other rows hi-Z : 1 */
281 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
282 mcp23018_status = i2c_write(GPIOB); if (mcp23018_status) goto out;
283 mcp23018_status = i2c_write(0xFF & ~(1<<abs(row-4))); if (mcp23018_status) goto out;
288 uint8_t pin = row_pins[row];
289 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); /* OUT */
290 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); /* LOW */
293 static void unselect_rows(void)
295 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
296 uint8_t pin = row_pins[x];
297 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); /* IN */
298 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); /* HI */