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);
50 __attribute__ ((weak))
51 void matrix_init_kb(void) {
55 __attribute__ ((weak))
56 void matrix_scan_kb(void) {
61 uint8_t matrix_rows(void)
67 uint8_t matrix_cols(void)
72 void matrix_init(void)
74 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
79 // initialize row and col
83 // initialize matrix state: all keys off
84 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
86 matrix_debouncing[i] = 0;
93 uint8_t matrix_scan(void)
96 #if DIODE_DIRECTION == COL2ROW
97 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
99 _delay_us(30); // without this wait read unstable value.
100 matrix_row_t cols = read_cols();
101 if (matrix_debouncing[i] != cols) {
102 matrix_debouncing[i] = cols;
104 debug("bounce!: "); debug_hex(debouncing); debug("\n");
106 debouncing = DEBOUNCE;
115 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
116 matrix[i] = matrix_debouncing[i];
121 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
123 _delay_us(30); // without this wait read unstable value.
124 matrix_row_t rows = read_cols();
125 if (matrix_reversed_debouncing[i] != rows) {
126 matrix_reversed_debouncing[i] = rows;
128 debug("bounce!: "); debug_hex(debouncing); debug("\n");
130 debouncing = DEBOUNCE;
139 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
140 matrix_reversed[i] = matrix_reversed_debouncing[i];
144 for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
145 matrix_row_t row = 0;
146 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
147 row |= ((matrix_reversed[x] & (1<<y)) >> y) << x;
158 bool matrix_is_modified(void)
160 if (debouncing) return false;
165 bool matrix_is_on(uint8_t row, uint8_t col)
167 return (matrix[row] & ((matrix_row_t)1<col));
171 matrix_row_t matrix_get_row(uint8_t row)
176 void matrix_print(void)
178 print("\nr/c 0123456789ABCDEF\n");
179 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
180 phex(row); print(": ");
181 pbin_reverse16(matrix_get_row(row));
186 uint8_t matrix_key_count(void)
189 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
190 count += bitpop16(matrix[i]);
195 static void init_cols(void)
197 int B = 0, C = 0, D = 0, E = 0, F = 0;
199 #if DIODE_DIRECTION == COL2ROW
200 for(int x = 0; x < MATRIX_COLS; x++) {
203 for(int x = 0; x < MATRIX_ROWS; x++) {
206 if ((col & 0xF0) == 0x20) {
207 B |= (1<<(col & 0x0F));
208 } else if ((col & 0xF0) == 0x30) {
209 C |= (1<<(col & 0x0F));
210 } else if ((col & 0xF0) == 0x40) {
211 D |= (1<<(col & 0x0F));
212 } else if ((col & 0xF0) == 0x50) {
213 E |= (1<<(col & 0x0F));
214 } else if ((col & 0xF0) == 0x60) {
215 F |= (1<<(col & 0x0F));
218 DDRB &= ~(B); PORTB |= (B);
219 DDRC &= ~(C); PORTC |= (C);
220 DDRD &= ~(D); PORTD |= (D);
221 DDRE &= ~(E); PORTE |= (E);
222 DDRF &= ~(F); PORTF |= (F);
225 static matrix_row_t read_cols(void)
227 matrix_row_t result = 0;
229 #if DIODE_DIRECTION == COL2ROW
230 for(int x = 0; x < MATRIX_COLS; x++) {
233 for(int x = 0; x < MATRIX_ROWS; x++) {
237 if ((col & 0xF0) == 0x20) {
238 result |= (PINB&(1<<(col & 0x0F)) ? 0 : (1<<x));
239 } else if ((col & 0xF0) == 0x30) {
240 result |= (PINC&(1<<(col & 0x0F)) ? 0 : (1<<x));
241 } else if ((col & 0xF0) == 0x40) {
242 result |= (PIND&(1<<(col & 0x0F)) ? 0 : (1<<x));
243 } else if ((col & 0xF0) == 0x50) {
244 result |= (PINE&(1<<(col & 0x0F)) ? 0 : (1<<x));
245 } else if ((col & 0xF0) == 0x60) {
246 result |= (PINF&(1<<(col & 0x0F)) ? 0 : (1<<x));
252 static void unselect_rows(void)
254 int B = 0, C = 0, D = 0, E = 0, F = 0;
256 #if DIODE_DIRECTION == COL2ROW
257 for(int x = 0; x < MATRIX_ROWS; x++) {
260 for(int x = 0; x < MATRIX_COLS; x++) {
263 if ((row & 0xF0) == 0x20) {
264 B |= (1<<(row & 0x0F));
265 } else if ((row & 0xF0) == 0x30) {
266 C |= (1<<(row & 0x0F));
267 } else if ((row & 0xF0) == 0x40) {
268 D |= (1<<(row & 0x0F));
269 } else if ((row & 0xF0) == 0x50) {
270 E |= (1<<(row & 0x0F));
271 } else if ((row & 0xF0) == 0x60) {
272 F |= (1<<(row & 0x0F));
275 DDRB &= ~(B); PORTB |= (B);
276 DDRC &= ~(C); PORTC |= (C);
277 DDRD &= ~(D); PORTD |= (D);
278 DDRE &= ~(E); PORTE |= (E);
279 DDRF &= ~(F); PORTF |= (F);
282 static void select_row(uint8_t row)
285 #if DIODE_DIRECTION == COL2ROW
286 int row_pin = ROWS[row];
288 int row_pin = COLS[row];
291 if ((row_pin & 0xF0) == 0x20) {
292 DDRB |= (1<<(row_pin & 0x0F));
293 PORTB &= ~(1<<(row_pin & 0x0F));
294 } else if ((row_pin & 0xF0) == 0x30) {
295 DDRC |= (1<<(row_pin & 0x0F));
296 PORTC &= ~(1<<(row_pin & 0x0F));
297 } else if ((row_pin & 0xF0) == 0x40) {
298 DDRD |= (1<<(row_pin & 0x0F));
299 PORTD &= ~(1<<(row_pin & 0x0F));
300 } else if ((row_pin & 0xF0) == 0x50) {
301 DDRE |= (1<<(row_pin & 0x0F));
302 PORTE &= ~(1<<(row_pin & 0x0F));
303 } else if ((row_pin & 0xF0) == 0x60) {
304 DDRF |= (1<<(row_pin & 0x0F));
305 PORTF &= ~(1<<(row_pin & 0x0F));