2 Copyright 2012 Jun Wako
3 Copyright 2014 Jack Humbert
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/>.
23 #include <avr/interrupt.h>
24 #include <util/delay.h>
32 #include "i2c_slave.h"
35 #define SLAVE_I2C_ADDRESS 0x21
37 /* Set 0 if debouncing isn't needed */
44 static uint16_t debouncing_time;
45 static bool debouncing = false;
48 #if (MATRIX_COLS <= 8)
49 # define print_matrix_header() print("\nr/c 01234567\n")
50 # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
51 # define matrix_bitpop(i) bitpop(matrix[i])
52 # define ROW_SHIFTER ((uint8_t)1)
53 #elif (MATRIX_COLS <= 16)
54 # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
55 # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
56 # define matrix_bitpop(i) bitpop16(matrix[i])
57 # define ROW_SHIFTER ((uint16_t)1)
58 #elif (MATRIX_COLS <= 32)
59 # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
60 # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
61 # define matrix_bitpop(i) bitpop32(matrix[i])
62 # define ROW_SHIFTER ((uint32_t)1)
66 extern const matrix_row_t matrix_mask[];
69 #if (DIODE_DIRECTION == ROW2COL) || (DIODE_DIRECTION == COL2ROW)
70 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
71 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
74 /* matrix state(1:on, 0:off) */
75 static matrix_row_t matrix[MATRIX_ROWS];
77 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
80 #if (DIODE_DIRECTION == COL2ROW)
81 static void init_cols(void);
82 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row);
83 static void unselect_rows(void);
84 static void select_row(uint8_t row);
85 static void unselect_row(uint8_t row);
86 #elif (DIODE_DIRECTION == ROW2COL)
87 static void init_rows(void);
88 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col);
89 static void unselect_cols(void);
90 static void unselect_col(uint8_t col);
91 static void select_col(uint8_t col);
94 __attribute__ ((weak))
95 void matrix_init_quantum(void) {
99 __attribute__ ((weak))
100 void matrix_scan_quantum(void) {
104 __attribute__ ((weak))
105 void matrix_init_kb(void) {
109 __attribute__ ((weak))
110 void matrix_scan_kb(void) {
114 __attribute__ ((weak))
115 void matrix_init_user(void) {
118 __attribute__ ((weak))
119 void matrix_scan_user(void) {
123 uint8_t matrix_rows(void) {
128 uint8_t matrix_cols(void) {
132 void matrix_init(void) {
134 // initialize row and col
135 #if (DIODE_DIRECTION == COL2ROW)
138 #elif (DIODE_DIRECTION == ROW2COL)
143 // initialize matrix state: all keys off
144 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
146 matrix_debouncing[i] = 0;
149 matrix_init_quantum();
152 uint8_t matrix_scan(void)
154 #if (DIODE_DIRECTION == COL2ROW)
156 // Set row, read cols
157 for (uint8_t current_row = 0; current_row < MATRIX_ROWS; current_row++) {
159 bool matrix_changed = read_cols_on_row(matrix_debouncing, current_row);
161 if (matrix_changed) {
163 debouncing_time = timer_read();
167 read_cols_on_row(matrix, current_row);
172 #elif (DIODE_DIRECTION == ROW2COL)
174 // Set col, read rows
175 for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++) {
177 bool matrix_changed = read_rows_on_col(matrix_debouncing, current_col);
178 if (matrix_changed) {
180 debouncing_time = timer_read();
183 read_rows_on_col(matrix, current_col);
191 if (debouncing && (timer_elapsed(debouncing_time) > DEBOUNCE)) {
192 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
193 matrix[i] = matrix_debouncing[i];
199 if (USB_DeviceState != DEVICE_STATE_Configured){
200 i2c_slave_reg[1] = 0x55;
201 for (uint8_t i = 0; i < MATRIX_ROWS; i++){
202 i2c_slave_reg[i+2] = matrix[i]; //send matrix over i2c
206 matrix_scan_quantum();
210 bool matrix_is_modified(void)
213 if (debouncing) return false;
219 bool matrix_is_on(uint8_t row, uint8_t col)
221 return (matrix[row] & ((matrix_row_t)1<<col));
225 matrix_row_t matrix_get_row(uint8_t row)
227 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
228 // switch blocker installed and the switch is always pressed.
230 return matrix[row] & matrix_mask[row];
236 void matrix_print(void)
238 print_matrix_header();
240 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
241 phex(row); print(": ");
242 print_matrix_row(row);
247 uint8_t matrix_key_count(void)
250 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
251 count += matrix_bitpop(i);
258 #if (DIODE_DIRECTION == COL2ROW)
260 static void init_cols(void)
262 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
263 uint8_t pin = col_pins[x];
264 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
265 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
269 static bool read_cols_on_row(matrix_row_t current_matrix[], uint8_t current_row)
271 // Store last value of row prior to reading
272 matrix_row_t last_row_value = current_matrix[current_row];
274 // Clear data in matrix row
275 current_matrix[current_row] = 0;
277 // Select row and wait for row selecton to stabilize
278 select_row(current_row);
282 for(uint8_t col_index = 0; col_index < MATRIX_COLS; col_index++) {
284 // Select the col pin to read (active low)
285 uint8_t pin = col_pins[col_index];
286 uint8_t pin_state = (_SFR_IO8(pin >> 4) & _BV(pin & 0xF));
288 // Populate the matrix row with the state of the col pin
289 current_matrix[current_row] |= pin_state ? 0 : (ROW_SHIFTER << col_index);
293 unselect_row(current_row);
295 return (last_row_value != current_matrix[current_row]);
298 static void select_row(uint8_t row)
300 uint8_t pin = row_pins[row];
301 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
302 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
305 static void unselect_row(uint8_t row)
307 uint8_t pin = row_pins[row];
308 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
309 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
312 static void unselect_rows(void)
314 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
315 uint8_t pin = row_pins[x];
316 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
317 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
321 #elif (DIODE_DIRECTION == ROW2COL)
323 static void init_rows(void)
325 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
326 uint8_t pin = row_pins[x];
327 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
328 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
332 static bool read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col)
334 bool matrix_changed = false;
336 // Select col and wait for col selecton to stabilize
337 select_col(current_col);
341 for(uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++)
344 // Store last value of row prior to reading
345 matrix_row_t last_row_value = current_matrix[row_index];
347 // Check row pin state
348 if ((_SFR_IO8(row_pins[row_index] >> 4) & _BV(row_pins[row_index] & 0xF)) == 0)
350 // Pin LO, set col bit
351 current_matrix[row_index] |= (ROW_SHIFTER << current_col);
355 // Pin HI, clear col bit
356 current_matrix[row_index] &= ~(ROW_SHIFTER << current_col);
359 // Determine if the matrix changed state
360 if ((last_row_value != current_matrix[row_index]) && !(matrix_changed))
362 matrix_changed = true;
367 unselect_col(current_col);
369 return matrix_changed;
372 static void select_col(uint8_t col)
374 uint8_t pin = col_pins[col];
375 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
376 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
379 static void unselect_col(uint8_t col)
381 uint8_t pin = col_pins[col];
382 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
383 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
386 static void unselect_cols(void)
388 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
389 uint8_t pin = col_pins[x];
390 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
391 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
397 //this replases tmk code
398 void matrix_setup(void){
400 if (USB_DeviceState != DEVICE_STATE_Configured){
401 i2c_slave_init(SLAVE_I2C_ADDRESS); //setup address of slave i2c
402 sei(); //enable interupts