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>
30 #include "backlight.h" // TODO fix this dependency
36 static uint8_t debouncing = DEBOUNCE;
38 /* matrix state(1:on, 0:off) */
39 static matrix_row_t matrix[MATRIX_ROWS];
40 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
42 static matrix_row_t read_cols(void);
43 static void init_cols(void);
44 static void unselect_rows(void);
45 static void select_row(uint8_t row);
48 uint8_t matrix_rows(void)
54 uint8_t matrix_cols(void)
59 void matrix_init(void)
61 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
65 backlight_init_ports();
71 // initialize row and col
75 // initialize matrix state: all keys off
76 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
78 matrix_debouncing[i] = 0;
82 uint8_t matrix_scan(void)
84 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
86 _delay_us(30); // without this wait read unstable value.
87 matrix_row_t cols = read_cols();
88 if (matrix_debouncing[i] != cols) {
89 matrix_debouncing[i] = cols;
91 debug("bounce!: "); debug_hex(debouncing); debug("\n");
93 debouncing = DEBOUNCE;
102 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
103 matrix[i] = matrix_debouncing[i];
111 bool matrix_is_modified(void)
113 if (debouncing) return false;
118 bool matrix_is_on(uint8_t row, uint8_t col)
120 return (matrix[row] & ((matrix_row_t)1<col));
124 matrix_row_t matrix_get_row(uint8_t row)
129 void matrix_print(void)
131 print("\nr/c 0123456789ABCDEF\n");
132 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
133 phex(row); print(": ");
134 pbin_reverse16(matrix_get_row(row));
139 uint8_t matrix_key_count(void)
142 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
143 count += bitpop16(matrix[i]);
149 // Planck PCB Rev 1 Pin Assignments
151 // Column: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
152 // Pin: F1, F0, B0, C7, F4, F5, F6, F7, D4, D6, B4, D7
155 static void init_cols(void)
157 DDRB &= ~(1<<4 | 1<<0);
158 PORTB |= (1<<4 | 1<<0);
161 DDRD &= ~(1<<7 | 1<<6 | 1<<4);
162 PORTD |= (1<<7 | 1<<6 | 1<<4);
163 DDRF &= ~(1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
164 PORTF |= (1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
168 static matrix_row_t read_cols(void)
170 return (PINF&(1<<1) ? 0 : (1<<0)) |
171 (PINF&(1<<0) ? 0 : (1<<1)) |
172 (PINB&(1<<0) ? 0 : (1<<2)) |
173 (PINC&(1<<7) ? 0 : (1<<3)) |
174 (PINF&(1<<4) ? 0 : (1<<4)) |
175 (PINF&(1<<5) ? 0 : (1<<5)) |
176 (PINF&(1<<6) ? 0 : (1<<6)) |
177 (PINF&(1<<7) ? 0 : (1<<7)) |
178 (PIND&(1<<4) ? 0 : (1<<8)) |
179 (PIND&(1<<6) ? 0 : (1<<9)) |
180 (PINB&(1<<4) ? 0 : (1<<10)) |
181 (PIND&(1<<7) ? 0 : (1<<11));
185 static void unselect_rows(void)
187 DDRB &= ~(1<<5 | 1<<6);
188 PORTB |= (1<<5 | 1<<6);
189 DDRD &= ~(1<<0 | 1<<5);
190 PORTD |= (1<<0 | 1<<5);
195 // Planck PCB Rev 1 Pin Assignments
198 // Pin: D0, D5, B5, B6
201 static void select_row(uint8_t row)