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];
45 static matrix_row_t read_cols(void);
46 static void init_cols(void);
47 static void unselect_rows(void);
48 static void select_row(uint8_t row);
51 uint8_t matrix_rows(void)
57 uint8_t matrix_cols(void)
62 void matrix_init(void)
64 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
69 // initialize row and col
73 // initialize matrix state: all keys off
74 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
76 matrix_debouncing[i] = 0;
85 uint8_t matrix_scan(void)
88 #if DIODE_DIRECTION == COL2ROW
89 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
91 _delay_us(30); // without this wait read unstable value.
92 matrix_row_t cols = read_cols();
93 if (matrix_debouncing[i] != cols) {
94 matrix_debouncing[i] = cols;
96 debug("bounce!: "); debug_hex(debouncing); debug("\n");
98 debouncing = DEBOUNCE;
107 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
108 matrix[i] = matrix_debouncing[i];
113 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
115 _delay_us(30); // without this wait read unstable value.
116 matrix_row_t rows = read_cols();
117 if (matrix_reversed_debouncing[i] != rows) {
118 matrix_reversed_debouncing[i] = rows;
120 debug("bounce!: "); debug_hex(debouncing); debug("\n");
122 debouncing = DEBOUNCE;
131 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
132 matrix_reversed[i] = matrix_reversed_debouncing[i];
136 for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
137 matrix_row_t row = 0;
138 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
139 row |= ((matrix_reversed[x] & (1<<y)) >> y) << x;
145 if (matrix_scan_kb) {
152 bool matrix_is_modified(void)
154 if (debouncing) return false;
159 bool matrix_is_on(uint8_t row, uint8_t col)
161 return (matrix[row] & ((matrix_row_t)1<col));
165 matrix_row_t matrix_get_row(uint8_t row)
170 void matrix_print(void)
172 print("\nr/c 0123456789ABCDEF\n");
173 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
174 phex(row); print(": ");
175 pbin_reverse16(matrix_get_row(row));
180 uint8_t matrix_key_count(void)
183 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
184 count += bitpop16(matrix[i]);
189 static void init_cols(void)
191 int B = 0, C = 0, D = 0, E = 0, F = 0;
193 #if DIODE_DIRECTION == COL2ROW
194 for(int x = 0; x < MATRIX_COLS; x++) {
197 for(int x = 0; x < MATRIX_ROWS; x++) {
200 if ((col & 0xF0) == 0x20) {
201 B |= (1<<(col & 0x0F));
202 } else if ((col & 0xF0) == 0x30) {
203 C |= (1<<(col & 0x0F));
204 } else if ((col & 0xF0) == 0x40) {
205 D |= (1<<(col & 0x0F));
206 } else if ((col & 0xF0) == 0x50) {
207 E |= (1<<(col & 0x0F));
208 } else if ((col & 0xF0) == 0x60) {
209 F |= (1<<(col & 0x0F));
212 DDRB &= ~(B); PORTB |= (B);
213 DDRC &= ~(C); PORTC |= (C);
214 DDRD &= ~(D); PORTD |= (D);
215 DDRE &= ~(E); PORTE |= (E);
216 DDRF &= ~(F); PORTF |= (F);
219 static matrix_row_t read_cols(void)
221 matrix_row_t result = 0;
223 #if DIODE_DIRECTION == COL2ROW
224 for(int x = 0; x < MATRIX_COLS; x++) {
227 for(int x = 0; x < MATRIX_ROWS; x++) {
231 if ((col & 0xF0) == 0x20) {
232 result |= (PINB&(1<<(col & 0x0F)) ? 0 : (1<<x));
233 } else if ((col & 0xF0) == 0x30) {
234 result |= (PINC&(1<<(col & 0x0F)) ? 0 : (1<<x));
235 } else if ((col & 0xF0) == 0x40) {
236 result |= (PIND&(1<<(col & 0x0F)) ? 0 : (1<<x));
237 } else if ((col & 0xF0) == 0x50) {
238 result |= (PINE&(1<<(col & 0x0F)) ? 0 : (1<<x));
239 } else if ((col & 0xF0) == 0x60) {
240 result |= (PINF&(1<<(col & 0x0F)) ? 0 : (1<<x));
246 static void unselect_rows(void)
248 int B = 0, C = 0, D = 0, E = 0, F = 0;
250 #if DIODE_DIRECTION == COL2ROW
251 for(int x = 0; x < MATRIX_ROWS; x++) {
254 for(int x = 0; x < MATRIX_COLS; x++) {
257 if ((row & 0xF0) == 0x20) {
258 B |= (1<<(row & 0x0F));
259 } else if ((row & 0xF0) == 0x30) {
260 C |= (1<<(row & 0x0F));
261 } else if ((row & 0xF0) == 0x40) {
262 D |= (1<<(row & 0x0F));
263 } else if ((row & 0xF0) == 0x50) {
264 E |= (1<<(row & 0x0F));
265 } else if ((row & 0xF0) == 0x60) {
266 F |= (1<<(row & 0x0F));
269 DDRB &= ~(B); PORTB |= (B);
270 DDRC &= ~(C); PORTC |= (C);
271 DDRD &= ~(D); PORTD |= (D);
272 DDRE &= ~(E); PORTE |= (E);
273 DDRF &= ~(F); PORTF |= (F);
276 static void select_row(uint8_t row)
279 #if DIODE_DIRECTION == COL2ROW
280 int row_pin = ROWS[row];
282 int row_pin = COLS[row];
285 if ((row_pin & 0xF0) == 0x20) {
286 DDRB |= (1<<(row_pin & 0x0F));
287 PORTB &= ~(1<<(row_pin & 0x0F));
288 } else if ((row_pin & 0xF0) == 0x30) {
289 DDRC |= (1<<(row_pin & 0x0F));
290 PORTC &= ~(1<<(row_pin & 0x0F));
291 } else if ((row_pin & 0xF0) == 0x40) {
292 DDRD |= (1<<(row_pin & 0x0F));
293 PORTD &= ~(1<<(row_pin & 0x0F));
294 } else if ((row_pin & 0xF0) == 0x50) {
295 DDRE |= (1<<(row_pin & 0x0F));
296 PORTE &= ~(1<<(row_pin & 0x0F));
297 } else if ((row_pin & 0xF0) == 0x60) {
298 DDRF |= (1<<(row_pin & 0x0F));
299 PORTF &= ~(1<<(row_pin & 0x0F));