2 Copyright 2012 Jun Wako
3 Generated by planckkeyboard.com (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/>.
25 #include <util/delay.h>
34 static uint8_t debouncing = DEBOUNCE;
36 /* matrix state(1:on, 0:off) */
37 static matrix_row_t matrix[MATRIX_ROWS];
38 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
40 #if DIODE_DIRECTION == ROW2COL
41 static matrix_row_t matrix_reversed[MATRIX_COLS];
42 static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS];
52 static matrix_row_t read_cols(void);
53 static void init_cols(void);
54 static void unselect_rows(void);
55 static void select_row(uint8_t row);
57 __attribute__ ((weak))
58 void matrix_init_quantum(void) {
62 __attribute__ ((weak))
63 void matrix_scan_quantum(void) {
68 uint8_t matrix_rows(void)
74 uint8_t matrix_cols(void)
79 void matrix_init(void)
81 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
86 // initialize row and col
90 // initialize matrix state: all keys off
91 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
93 matrix_debouncing[i] = 0;
96 matrix_init_quantum();
100 uint8_t matrix_scan(void)
103 #if DIODE_DIRECTION == COL2ROW
104 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
106 _delay_us(30); // without this wait read unstable value.
107 matrix_row_t cols = read_cols();
108 if (matrix_debouncing[i] != cols) {
109 matrix_debouncing[i] = cols;
111 debug("bounce!: "); debug_hex(debouncing); debug("\n");
113 debouncing = DEBOUNCE;
122 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
123 matrix[i] = matrix_debouncing[i];
128 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
130 _delay_us(30); // without this wait read unstable value.
131 matrix_row_t rows = read_cols();
132 if (matrix_reversed_debouncing[i] != rows) {
133 matrix_reversed_debouncing[i] = rows;
135 debug("bounce!: "); debug_hex(debouncing); debug("\n");
137 debouncing = DEBOUNCE;
146 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
147 matrix_reversed[i] = matrix_reversed_debouncing[i];
151 for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
152 matrix_row_t row = 0;
153 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
154 row |= ((matrix_reversed[x] & (1<<y)) >> y) << x;
160 matrix_scan_quantum();
165 bool matrix_is_modified(void)
167 if (debouncing) return false;
172 bool matrix_is_on(uint8_t row, uint8_t col)
174 return (matrix[row] & ((matrix_row_t)1<col));
178 matrix_row_t matrix_get_row(uint8_t row)
183 void matrix_print(void)
185 print("\nr/c 0123456789ABCDEF\n");
186 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
187 phex(row); print(": ");
188 pbin_reverse16(matrix_get_row(row));
193 uint8_t matrix_key_count(void)
196 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
197 count += bitpop16(matrix[i]);
202 static void init_cols(void)
204 int B = 0, C = 0, D = 0, E = 0, F = 0;
206 #if DIODE_DIRECTION == COL2ROW
207 for(int x = 0; x < MATRIX_COLS; x++) {
210 for(int x = 0; x < MATRIX_ROWS; x++) {
213 if ((col & 0xF0) == 0x20) {
214 B |= (1<<(col & 0x0F));
215 } else if ((col & 0xF0) == 0x30) {
216 C |= (1<<(col & 0x0F));
217 } else if ((col & 0xF0) == 0x40) {
218 D |= (1<<(col & 0x0F));
219 } else if ((col & 0xF0) == 0x50) {
220 E |= (1<<(col & 0x0F));
221 } else if ((col & 0xF0) == 0x60) {
222 F |= (1<<(col & 0x0F));
225 DDRB &= ~(B); PORTB |= (B);
226 DDRC &= ~(C); PORTC |= (C);
227 DDRD &= ~(D); PORTD |= (D);
228 DDRE &= ~(E); PORTE |= (E);
229 DDRF &= ~(F); PORTF |= (F);
232 static matrix_row_t read_cols(void)
234 matrix_row_t result = 0;
236 #if DIODE_DIRECTION == COL2ROW
237 for(int x = 0; x < MATRIX_COLS; x++) {
240 for(int x = 0; x < MATRIX_ROWS; x++) {
244 if ((col & 0xF0) == 0x20) {
245 result |= (PINB&(1<<(col & 0x0F)) ? 0 : (SHIFTER<<x));
246 } else if ((col & 0xF0) == 0x30) {
247 result |= (PINC&(1<<(col & 0x0F)) ? 0 : (SHIFTER<<x));
248 } else if ((col & 0xF0) == 0x40) {
249 result |= (PIND&(1<<(col & 0x0F)) ? 0 : (SHIFTER<<x));
250 } else if ((col & 0xF0) == 0x50) {
251 result |= (PINE&(1<<(col & 0x0F)) ? 0 : (SHIFTER<<x));
252 } else if ((col & 0xF0) == 0x60) {
253 result |= (PINF&(1<<(col & 0x0F)) ? 0 : (SHIFTER<<x));
259 static void unselect_rows(void)
261 int B = 0, C = 0, D = 0, E = 0, F = 0;
263 #if DIODE_DIRECTION == COL2ROW
264 for(int x = 0; x < MATRIX_ROWS; x++) {
267 for(int x = 0; x < MATRIX_COLS; x++) {
270 if ((row & 0xF0) == 0x20) {
271 B |= (1<<(row & 0x0F));
272 } else if ((row & 0xF0) == 0x30) {
273 C |= (1<<(row & 0x0F));
274 } else if ((row & 0xF0) == 0x40) {
275 D |= (1<<(row & 0x0F));
276 } else if ((row & 0xF0) == 0x50) {
277 E |= (1<<(row & 0x0F));
278 } else if ((row & 0xF0) == 0x60) {
279 F |= (1<<(row & 0x0F));
282 DDRB &= ~(B); PORTB |= (B);
283 DDRC &= ~(C); PORTC |= (C);
284 DDRD &= ~(D); PORTD |= (D);
285 DDRE &= ~(E); PORTE |= (E);
286 DDRF &= ~(F); PORTF |= (F);
289 static void select_row(uint8_t row)
292 #if DIODE_DIRECTION == COL2ROW
293 int row_pin = ROWS[row];
295 int row_pin = COLS[row];
298 if ((row_pin & 0xF0) == 0x20) {
299 DDRB |= (1<<(row_pin & 0x0F));
300 PORTB &= ~(1<<(row_pin & 0x0F));
301 } else if ((row_pin & 0xF0) == 0x30) {
302 DDRC |= (1<<(row_pin & 0x0F));
303 PORTC &= ~(1<<(row_pin & 0x0F));
304 } else if ((row_pin & 0xF0) == 0x40) {
305 DDRD |= (1<<(row_pin & 0x0F));
306 PORTD &= ~(1<<(row_pin & 0x0F));
307 } else if ((row_pin & 0xF0) == 0x50) {
308 DDRE |= (1<<(row_pin & 0x0F));
309 PORTE &= ~(1<<(row_pin & 0x0F));
310 } else if ((row_pin & 0xF0) == 0x60) {
311 DDRF |= (1<<(row_pin & 0x0F));
312 PORTF &= ~(1<<(row_pin & 0x0F));