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 */
33 #ifndef DEBOUNCING_DELAY
34 # define DEBOUNCING_DELAY 5
37 #if (DEBOUNCING_DELAY > 0)
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) {
117 /* To use PORTF disable JTAG with writing JTD bit twice within four cycles. */
118 #if (defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega32U4__))
123 mcp23018_status = true;
125 /* initialize row and col */
129 /* initialize matrix state: all keys off */
130 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
132 matrix_debouncing[i] = 0;
135 matrix_init_quantum();
138 uint8_t matrix_scan(void)
140 if (mcp23018_status) {
141 /* if there was an error */
142 if (++mcp23018_reset_loop == 0) {
143 /* since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans
144 this will be approx bit more frequent than once per second */
145 print("trying to reset mcp23018\n");
146 mcp23018_status = init_mcp23018();
147 if (mcp23018_status) {
148 print("left side not responding\n");
150 print("left side attached\n");
155 /* Set row, read cols */
156 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
157 # if (DEBOUNCING_DELAY > 0)
158 bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
160 if (matrix_changed) {
162 debouncing_time = timer_read();
165 read_cols_on_row(matrix, current_row);
169 # if (DEBOUNCING_DELAY > 0)
170 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCING_DELAY)) {
171 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
172 matrix[i] = matrix_debouncing[i];
178 matrix_scan_quantum();
182 bool matrix_is_modified(void)
184 #if (DEBOUNCING_DELAY > 0)
185 if (debouncing) return false;
191 bool matrix_is_on(uint8_t row, uint8_t col)
193 return (matrix[row] & ((matrix_row_t)1<col));
197 matrix_row_t matrix_get_row(uint8_t row)
199 /* Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
200 switch blocker installed and the switch is always pressed. */
202 return matrix[row] & matrix_mask[row];
208 void matrix_print(void)
210 print_matrix_header();
212 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
213 phex(row); print(": ");
214 print_matrix_row(row);
219 uint8_t matrix_key_count(void)
222 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
223 count += matrix_bitpop(i);
228 static void init_cols(void)
230 for(uint8_t x = 0; x < ATMEGA_COLS; x++) {
231 uint8_t pin = col_pins[x];
232 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); /* IN */
233 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); /* HI */
237 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
239 /* Store last value of row prior to reading */
240 matrix_row_t last_row_value = current_matrix[current_row];
242 /* Clear data in matrix row */
243 current_matrix[current_row] = 0;
245 /* Select row and wait for row selecton to stabilize */
246 select_row(current_row);
249 if (mcp23018_status) {
250 /* if there was an error */
254 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
255 mcp23018_status = i2c_write(GPIOA); if (mcp23018_status) goto out;
256 mcp23018_status = i2c_start(I2C_ADDR_READ); if (mcp23018_status) goto out;
257 data = i2c_readNak();
261 current_matrix[current_row] |= (data << 8);
264 /* For each col... */
265 for(uint8_t col_index = 0; col_index < ATMEGA_COLS; col_index++) {
266 /* Select the col pin to read (active low) */
267 uint8_t pin = col_pins[col_index];
268 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
270 /* Populate the matrix row with the state of the col pin */
271 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
277 return (last_row_value != current_matrix[current_row]);
280 static void select_row(uint8_t row)
282 if (mcp23018_status) {
283 /* if there was an error do nothing */
285 /* set active row low : 0
286 set active row output : 1
287 set other rows hi-Z : 1 */
288 mcp23018_status = i2c_start(I2C_ADDR_WRITE); if (mcp23018_status) goto out;
289 mcp23018_status = i2c_write(GPIOB); if (mcp23018_status) goto out;
290 mcp23018_status = i2c_write(0xFF & ~(1<<abs(row-4))); if (mcp23018_status) goto out;
295 uint8_t pin = row_pins[row];
296 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); /* OUT */
297 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); /* LOW */
300 static void unselect_rows(void)
302 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
303 uint8_t pin = row_pins[x];
304 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); /* IN */
305 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); /* HI */