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 * This constant define not debouncing time in msecs, but amount of matrix
32 * scan loops which should be made to get stable debounced results.
34 * On Ergodox matrix scan rate is relatively low, because of slow I2C.
35 * Now it's only 317 scans/second, or about 3.15 msec/scan.
36 * According to Cherry specs, debouncing time is 5 msec.
38 * And so, there is no sense to have DEBOUNCE higher than 2.
41 #ifndef DEBOUNCING_DELAY
42 # define DEBOUNCING_DELAY 5
44 static uint8_t debouncing = DEBOUNCING_DELAY;
46 static const uint8_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
47 static const uint8_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
49 /* matrix state(1:on, 0:off) */
50 static matrix_row_t matrix[MATRIX_ROWS];
51 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
53 #if DIODE_DIRECTION == ROW2COL
54 static matrix_row_t matrix_reversed[MATRIX_COLS];
55 static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS];
64 static matrix_row_t read_cols(void);
65 static void init_cols(void);
66 static void unselect_rows(void);
67 static void select_row(uint8_t row);
69 __attribute__ ((weak))
70 void matrix_init_quantum(void) {
74 __attribute__ ((weak))
75 void matrix_scan_quantum(void) {
79 __attribute__ ((weak))
80 void matrix_init_kb(void) {
84 __attribute__ ((weak))
85 void matrix_scan_kb(void) {
89 __attribute__ ((weak))
90 void matrix_init_user(void) {
93 __attribute__ ((weak))
94 void matrix_scan_user(void) {
98 uint8_t matrix_rows(void) {
103 uint8_t matrix_cols(void) {
107 // void matrix_power_up(void) {
108 // #if DIODE_DIRECTION == COL2ROW
109 // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
111 // _SFR_IO8((row_pins[r] >> 4) + 1) |= _BV(row_pins[r] & 0xF);
114 // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
116 // _SFR_IO8((col_pins[c] >> 4) + 2) |= _BV(col_pins[c] & 0xF);
119 // for (int8_t c = MATRIX_COLS - 1; c >= 0; --c) {
121 // _SFR_IO8((col_pins[c] >> 4) + 1) |= _BV(col_pins[c] & 0xF);
124 // for (int8_t r = MATRIX_ROWS - 1; r >= 0; --r) {
126 // _SFR_IO8((row_pins[r] >> 4) + 2) |= _BV(row_pins[r] & 0xF);
131 void matrix_init(void) {
132 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
133 #ifdef __AVR_ATmega32U4__
138 // initialize row and col
142 // initialize matrix state: all keys off
143 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
145 matrix_debouncing[i] = 0;
148 matrix_init_quantum();
151 uint8_t matrix_scan(void)
154 #if DIODE_DIRECTION == COL2ROW
155 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
157 wait_us(30); // without this wait read unstable value.
158 matrix_row_t cols = read_cols();
159 if (matrix_debouncing[i] != cols) {
160 matrix_debouncing[i] = cols;
162 debug("bounce!: "); debug_hex(debouncing); debug("\n");
164 debouncing = DEBOUNCING_DELAY;
173 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
174 matrix[i] = matrix_debouncing[i];
179 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
181 wait_us(30); // without this wait read unstable value.
182 matrix_row_t rows = read_cols();
183 if (matrix_reversed_debouncing[i] != rows) {
184 matrix_reversed_debouncing[i] = rows;
186 debug("bounce!: "); debug_hex(debouncing); debug("\n");
188 debouncing = DEBOUNCING_DELAY;
197 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
198 matrix_reversed[i] = matrix_reversed_debouncing[i];
202 for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
203 matrix_row_t row = 0;
204 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
205 row |= ((matrix_reversed[x] & (1<<y)) >> y) << x;
211 matrix_scan_quantum();
216 bool matrix_is_modified(void)
218 if (debouncing) return false;
223 bool matrix_is_on(uint8_t row, uint8_t col)
225 return (matrix[row] & ((matrix_row_t)1<col));
229 matrix_row_t matrix_get_row(uint8_t row)
234 void matrix_print(void)
236 print("\nr/c 0123456789ABCDEF\n");
237 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
238 phex(row); print(": ");
239 pbin_reverse16(matrix_get_row(row));
244 uint8_t matrix_key_count(void)
247 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
248 count += bitpop16(matrix[i]);
253 static void init_cols(void)
255 #if DIODE_DIRECTION == COL2ROW
256 for(int x = 0; x < MATRIX_COLS; x++) {
257 int pin = col_pins[x];
259 for(int x = 0; x < MATRIX_ROWS; x++) {
260 int pin = row_pins[x];
262 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF);
263 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF);
267 static matrix_row_t read_cols(void)
269 matrix_row_t result = 0;
271 #if DIODE_DIRECTION == COL2ROW
272 for(int x = 0; x < MATRIX_COLS; x++) {
273 int pin = col_pins[x];
275 for(int x = 0; x < MATRIX_ROWS; x++) {
276 int pin = row_pins[x];
278 result |= (_SFR_IO8(pin >> 4) & _BV(pin & 0xF)) ? 0 : (SHIFTER << x);
283 static void unselect_rows(void)
285 #if DIODE_DIRECTION == COL2ROW
286 for(int x = 0; x < MATRIX_ROWS; x++) {
287 int pin = row_pins[x];
289 for(int x = 0; x < MATRIX_COLS; x++) {
290 int pin = col_pins[x];
292 _SFR_IO8((pin >> 4) + 1) &= ~_BV(pin & 0xF);
293 _SFR_IO8((pin >> 4) + 2) |= _BV(pin & 0xF);
297 static void select_row(uint8_t row)
300 #if DIODE_DIRECTION == COL2ROW
301 int pin = row_pins[row];
303 int pin = col_pins[row];
305 _SFR_IO8((pin >> 4) + 1) |= _BV(pin & 0xF);
306 _SFR_IO8((pin >> 4) + 2) &= ~_BV(pin & 0xF);