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;
95 uint8_t matrix_scan(void)
98 #if DIODE_DIRECTION == COL2ROW
99 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
101 _delay_us(30); // without this wait read unstable value.
102 matrix_row_t cols = read_cols();
103 if (matrix_debouncing[i] != cols) {
104 matrix_debouncing[i] = cols;
106 debug("bounce!: "); debug_hex(debouncing); debug("\n");
108 debouncing = DEBOUNCE;
117 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
118 matrix[i] = matrix_debouncing[i];
123 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
125 _delay_us(30); // without this wait read unstable value.
126 matrix_row_t rows = read_cols();
127 if (matrix_reversed_debouncing[i] != rows) {
128 matrix_reversed_debouncing[i] = rows;
130 debug("bounce!: "); debug_hex(debouncing); debug("\n");
132 debouncing = DEBOUNCE;
141 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
142 matrix_reversed[i] = matrix_reversed_debouncing[i];
146 for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
147 matrix_row_t row = 0;
148 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
149 row |= ((matrix_reversed[x] & (1<<y)) >> y) << x;
155 if (matrix_scan_kb) {
162 bool matrix_is_modified(void)
164 if (debouncing) return false;
169 bool matrix_is_on(uint8_t row, uint8_t col)
171 return (matrix[row] & ((matrix_row_t)1<col));
175 matrix_row_t matrix_get_row(uint8_t row)
180 void matrix_print(void)
182 print("\nr/c 0123456789ABCDEF\n");
183 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
184 phex(row); print(": ");
185 pbin_reverse16(matrix_get_row(row));
190 uint8_t matrix_key_count(void)
193 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
194 count += bitpop16(matrix[i]);
199 static void init_cols(void)
201 int B = 0, C = 0, D = 0, E = 0, F = 0;
203 #if DIODE_DIRECTION == COL2ROW
204 for(int x = 0; x < MATRIX_COLS; x++) {
207 for(int x = 0; x < MATRIX_ROWS; x++) {
210 if ((col & 0xF0) == 0x20) {
211 B |= (1<<(col & 0x0F));
212 } else if ((col & 0xF0) == 0x30) {
213 C |= (1<<(col & 0x0F));
214 } else if ((col & 0xF0) == 0x40) {
215 D |= (1<<(col & 0x0F));
216 } else if ((col & 0xF0) == 0x50) {
217 E |= (1<<(col & 0x0F));
218 } else if ((col & 0xF0) == 0x60) {
219 F |= (1<<(col & 0x0F));
222 DDRB &= ~(B); PORTB |= (B);
223 DDRC &= ~(C); PORTC |= (C);
224 DDRD &= ~(D); PORTD |= (D);
225 DDRE &= ~(E); PORTE |= (E);
226 DDRF &= ~(F); PORTF |= (F);
229 static matrix_row_t read_cols(void)
231 matrix_row_t result = 0;
233 #if DIODE_DIRECTION == COL2ROW
234 for(int x = 0; x < MATRIX_COLS; x++) {
237 for(int x = 0; x < MATRIX_ROWS; x++) {
241 if ((col & 0xF0) == 0x20) {
242 result |= (PINB&(1<<(col & 0x0F)) ? 0 : (1<<x));
243 } else if ((col & 0xF0) == 0x30) {
244 result |= (PINC&(1<<(col & 0x0F)) ? 0 : (1<<x));
245 } else if ((col & 0xF0) == 0x40) {
246 result |= (PIND&(1<<(col & 0x0F)) ? 0 : (1<<x));
247 } else if ((col & 0xF0) == 0x50) {
248 result |= (PINE&(1<<(col & 0x0F)) ? 0 : (1<<x));
249 } else if ((col & 0xF0) == 0x60) {
250 result |= (PINF&(1<<(col & 0x0F)) ? 0 : (1<<x));
256 static void unselect_rows(void)
258 int B = 0, C = 0, D = 0, E = 0, F = 0;
260 #if DIODE_DIRECTION == COL2ROW
261 for(int x = 0; x < MATRIX_ROWS; x++) {
264 for(int x = 0; x < MATRIX_COLS; x++) {
267 if ((row & 0xF0) == 0x20) {
268 B |= (1<<(row & 0x0F));
269 } else if ((row & 0xF0) == 0x30) {
270 C |= (1<<(row & 0x0F));
271 } else if ((row & 0xF0) == 0x40) {
272 D |= (1<<(row & 0x0F));
273 } else if ((row & 0xF0) == 0x50) {
274 E |= (1<<(row & 0x0F));
275 } else if ((row & 0xF0) == 0x60) {
276 F |= (1<<(row & 0x0F));
279 DDRB &= ~(B); PORTB |= (B);
280 DDRC &= ~(C); PORTC |= (C);
281 DDRD &= ~(D); PORTD |= (D);
282 DDRE &= ~(E); PORTE |= (E);
283 DDRF &= ~(F); PORTF |= (F);
286 static void select_row(uint8_t row)
289 #if DIODE_DIRECTION == COL2ROW
290 int row_pin = ROWS[row];
292 int row_pin = COLS[row];
295 if ((row_pin & 0xF0) == 0x20) {
296 DDRB |= (1<<(row_pin & 0x0F));
297 PORTB &= ~(1<<(row_pin & 0x0F));
298 } else if ((row_pin & 0xF0) == 0x30) {
299 DDRC |= (1<<(row_pin & 0x0F));
300 PORTC &= ~(1<<(row_pin & 0x0F));
301 } else if ((row_pin & 0xF0) == 0x40) {
302 DDRD |= (1<<(row_pin & 0x0F));
303 PORTD &= ~(1<<(row_pin & 0x0F));
304 } else if ((row_pin & 0xF0) == 0x50) {
305 DDRE |= (1<<(row_pin & 0x0F));
306 PORTE &= ~(1<<(row_pin & 0x0F));
307 } else if ((row_pin & 0xF0) == 0x60) {
308 DDRF |= (1<<(row_pin & 0x0F));
309 PORTF &= ~(1<<(row_pin & 0x0F));