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/>.
29 #if (MATRIX_COLS <= 8)
30 # define print_matrix_header() print("\nr/c 01234567\n")
31 # define print_matrix_row(row) print_bin_reverse8(matrix_get_row(row))
32 # define matrix_bitpop(i) bitpop(matrix[i])
33 # define ROW_SHIFTER ((uint8_t)1)
34 #elif (MATRIX_COLS <= 16)
35 # define print_matrix_header() print("\nr/c 0123456789ABCDEF\n")
36 # define print_matrix_row(row) print_bin_reverse16(matrix_get_row(row))
37 # define matrix_bitpop(i) bitpop16(matrix[i])
38 # define ROW_SHIFTER ((uint16_t)1)
39 #elif (MATRIX_COLS <= 32)
40 # define print_matrix_header() print("\nr/c 0123456789ABCDEF0123456789ABCDEF\n")
41 # define print_matrix_row(row) print_bin_reverse32(matrix_get_row(row))
42 # define matrix_bitpop(i) bitpop32(matrix[i])
43 # define ROW_SHIFTER ((uint32_t)1)
46 #if (MATRIX_ROWS <= 8)
47 # define COL_SHIFTER ((uint8_t)1)
48 #elif (MATRIX_ROWS <= 16)
49 # define COL_SHIFTER ((uint16_t)1)
50 #elif (MATRIX_ROWS <= 32)
51 # define COL_SHIFTER ((uint32_t)1)
57 extern const matrix_row_t matrix_mask[];
60 /* Set 0 if debouncing isn't needed */
62 #ifndef DEBOUNCING_DELAY
63 # define DEBOUNCING_DELAY 5
65 static uint8_t debouncing = DEBOUNCING_DELAY;
67 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
68 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
70 /* matrix state(1:on, 0:off) */
71 static matrix_row_t matrix[MATRIX_ROWS];
73 #if DIODE_DIRECTION == COL2ROW
74 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
76 static matrix_col_t matrix_transposed[MATRIX_COLS];
77 static matrix_col_t matrix_transposed_debouncing[MATRIX_COLS];
80 #if (DIODE_DIRECTION == COL2ROW)
81 static void init_cols(void);
82 static matrix_row_t read_cols(void);
83 static void unselect_rows(void);
84 static void select_row(uint8_t row);
85 static void unselect_row(uint8_t row);
87 static void init_rows(void);
88 static matrix_col_t read_rows(void);
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_power_up(void) {
133 // #if (DIODE_DIRECTION == COL2ROW)
134 // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
136 // _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
139 // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
141 // _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
144 // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
146 // _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
149 // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
151 // _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
156 void matrix_init(void) {
158 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
159 #if (defined(__AVR_AT90USB1286__) || defined(__AVR_AT90USB1287__) || defined(__AVR_ATmega32U4__))
164 // initialize row and col
165 #if (DIODE_DIRECTION == COL2ROW)
169 // initialize matrix state: all keys off
170 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
172 matrix_debouncing[i] = 0;
179 // initialize matrix state: all keys off
180 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
184 // initialize matrix state: all keys off
185 for (uint8_t i=0; i < MATRIX_COLS; i++) {
186 matrix_transposed_debouncing[i] = 0;
190 matrix_init_quantum();
193 uint8_t matrix_scan(void)
196 #if (DIODE_DIRECTION == COL2ROW)
198 // Set row, read cols
200 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
202 wait_us(30); // without this wait read unstable value.
203 matrix_row_t current_row = read_cols();
204 if (matrix_debouncing[i] != current_row) {
205 matrix_debouncing[i] = current_row;
207 debug("bounce!: "); debug_hex(debouncing); debug("\n");
209 debouncing = DEBOUNCING_DELAY;
218 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
219 matrix[i] = matrix_debouncing[i];
226 // Set col, read rows
228 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
230 wait_us(30); // without this wait read unstable value.
231 matrix_col_t current_col = read_rows();
232 if (matrix_transposed_debouncing[i] != current_col) {
233 matrix_transposed_debouncing[i] = current_col;
235 debug("bounce!: "); debug_hex(debouncing); debug("\n");
237 debouncing = DEBOUNCING_DELAY;
246 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
247 matrix_transposed[i] = matrix_transposed_debouncing[i];
252 // Untranspose matrix
253 for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
254 matrix_row_t row = 0;
255 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
256 row |= ((matrix_transposed[x] & (1<<y)) >> y) << x;
263 matrix_scan_quantum();
268 bool matrix_is_modified(void)
270 if (debouncing) return false;
275 bool matrix_is_on(uint8_t row, uint8_t col)
277 return (matrix[row] & ((matrix_row_t)1<col));
281 matrix_row_t matrix_get_row(uint8_t row)
283 // Matrix mask lets you disable switches in the returned matrix data. For example, if you have a
284 // switch blocker installed and the switch is always pressed.
286 return matrix[row] & matrix_mask[row];
292 void matrix_print(void)
294 print_matrix_header();
296 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
297 phex(row); print(": ");
298 print_matrix_row(row);
303 uint8_t matrix_key_count(void)
306 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
307 count += matrix_bitpop(i);
314 #if (DIODE_DIRECTION == COL2ROW)
316 static void init_cols(void)
318 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
319 uint8_t pin = col_pins[x];
320 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
321 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
325 static matrix_row_t read_cols(void)
327 matrix_row_t result = 0;
329 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
330 uint8_t pin = col_pins[x];
331 result |= (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)) ? 0 : (ROW_SHIFTER << x);
337 static void select_row(uint8_t row)
339 uint8_t pin = row_pins[row];
340 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
341 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
344 static void unselect_row(uint8_t row)
346 uint8_t pin = row_pins[row];
347 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
348 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
351 static void unselect_rows(void)
353 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
354 uint8_t pin = row_pins[x];
355 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
356 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
362 static void init_rows(void)
364 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
365 uint8_t pin = row_pins[x];
366 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
367 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
371 static matrix_col_t read_rows(void)
373 matrix_col_t result = 0;
375 for(uint8_t x = 0; x < MATRIX_ROWS; x++) {
376 uint8_t pin = row_pins[x];
377 result |= (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)) ? 0 : (COL_SHIFTER << x);
383 static void select_col(uint8_t col)
385 uint8_t pin = col_pins[col];
386 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF); // OUT
387 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF); // LOW
390 static void unselect_col(uint8_t col)
392 uint8_t pin = col_pins[col];
393 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
394 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI
397 static void unselect_cols(void)
399 for(uint8_t x = 0; x < MATRIX_COLS; x++) {
400 uint8_t pin = col_pins[x];
401 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF); // IN
402 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF); // HI