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 /* Set 0 if debouncing isn't needed */
31 #ifndef DEBOUNCING_DELAY
32 # define DEBOUNCING_DELAY 5
34 static uint8_t debouncing = DEBOUNCING_DELAY;
36 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
37 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
39 /* matrix state(1:on, 0:off) */
40 static matrix_row_t matrix[MATRIX_ROWS];
41 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
43 #if DIODE_DIRECTION == ROW2COL
44 static matrix_row_t matrix_reversed[MATRIX_COLS];
45 static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS];
54 static matrix_row_t read_cols(void);
55 static void init_cols(void);
56 static void unselect_rows(void);
57 static void select_row(uint8_t row);
59 __attribute__ ((weak))
60 void matrix_init_quantum(void) {
64 __attribute__ ((weak))
65 void matrix_scan_quantum(void) {
69 __attribute__ ((weak))
70 void matrix_init_kb(void) {
74 __attribute__ ((weak))
75 void matrix_scan_kb(void) {
79 __attribute__ ((weak))
80 void matrix_init_user(void) {
83 __attribute__ ((weak))
84 void matrix_scan_user(void) {
88 uint8_t matrix_rows(void) {
93 uint8_t matrix_cols(void) {
97 // void matrix_power_up(void) {
98 // #if DIODE_DIRECTION == COL2ROW
99 // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
101 // _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
104 // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
106 // _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
109 // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
111 // _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
114 // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
116 // _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
121 void matrix_init(void) {
122 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
123 #ifdef __AVR_ATmega32U4__
128 // initialize row and col
132 // initialize matrix state: all keys off
133 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
135 matrix_debouncing[i] = 0;
138 matrix_init_quantum();
141 uint8_t matrix_scan(void)
144 #if DIODE_DIRECTION == COL2ROW
145 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
147 wait_us(30); // without this wait read unstable value.
148 matrix_row_t cols = read_cols();
149 if (matrix_debouncing[i] != cols) {
150 matrix_debouncing[i] = cols;
152 debug("bounce!: "); debug_hex(debouncing); debug("\n");
154 debouncing = DEBOUNCING_DELAY;
163 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
164 matrix[i] = matrix_debouncing[i];
169 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
171 wait_us(30); // without this wait read unstable value.
172 matrix_row_t rows = read_cols();
173 if (matrix_reversed_debouncing[i] != rows) {
174 matrix_reversed_debouncing[i] = rows;
176 debug("bounce!: "); debug_hex(debouncing); debug("\n");
178 debouncing = DEBOUNCING_DELAY;
187 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
188 matrix_reversed[i] = matrix_reversed_debouncing[i];
192 for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
193 matrix_row_t row = 0;
194 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
195 row |= ((matrix_reversed[x] & (1<<y)) >> y) << x;
201 matrix_scan_quantum();
206 bool matrix_is_modified(void)
208 if (debouncing) return false;
213 bool matrix_is_on(uint8_t row, uint8_t col)
215 return (matrix[row] & ((matrix_row_t)1<col));
219 matrix_row_t matrix_get_row(uint8_t row)
224 void matrix_print(void)
226 print("\nr/c 0123456789ABCDEF\n");
227 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
228 phex(row); print(": ");
229 pbin_reverse16(matrix_get_row(row));
234 uint8_t matrix_key_count(void)
237 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
238 count += bitpop16(matrix[i]);
243 static void init_cols(void)
245 #if DIODE_DIRECTION == COL2ROW
246 for(int x = 0; x < MATRIX_COLS; x++) {
247 int pin = col_pins[x];
249 for(int x = 0; x < MATRIX_ROWS; x++) {
250 int pin = row_pins[x];
252 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF);
253 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF);
257 static matrix_row_t read_cols(void)
259 matrix_row_t result = 0;
261 #if DIODE_DIRECTION == COL2ROW
262 for(int x = 0; x < MATRIX_COLS; x++) {
263 int pin = col_pins[x];
265 for(int x = 0; x < MATRIX_ROWS; x++) {
266 int pin = row_pins[x];
268 result |= (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)) ? 0 : (SHIFTER << x);
273 static void unselect_rows(void)
275 #if DIODE_DIRECTION == COL2ROW
276 for(int x = 0; x < MATRIX_ROWS; x++) {
277 int pin = row_pins[x];
279 for(int x = 0; x < MATRIX_COLS; x++) {
280 int pin = col_pins[x];
282 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF);
283 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF);
287 static void select_row(uint8_t row)
290 #if DIODE_DIRECTION == COL2ROW
291 int pin = row_pins[row];
293 int pin = col_pins[row];
295 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF);
296 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF);