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 // TODO fix this dependency
66 backlight_init_ports();
68 // initialize row and col
72 // initialize matrix state: all keys off
73 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
75 matrix_debouncing[i] = 0;
79 uint8_t matrix_scan(void)
81 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
83 _delay_us(30); // without this wait read unstable value.
84 matrix_row_t cols = read_cols();
85 if (matrix_debouncing[i] != cols) {
86 matrix_debouncing[i] = cols;
88 debug("bounce!: "); debug_hex(debouncing); debug("\n");
90 debouncing = DEBOUNCE;
99 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
100 matrix[i] = matrix_debouncing[i];
108 bool matrix_is_modified(void)
110 if (debouncing) return false;
115 bool matrix_is_on(uint8_t row, uint8_t col)
117 return (matrix[row] & ((matrix_row_t)1<col));
121 matrix_row_t matrix_get_row(uint8_t row)
126 void matrix_print(void)
128 print("\nr/c 0123456789ABCDEF\n");
129 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
130 phex(row); print(": ");
131 pbin_reverse16(matrix_get_row(row));
136 uint8_t matrix_key_count(void)
139 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
140 count += bitpop16(matrix[i]);
146 // Planck PCB Rev 1 Pin Assignments
148 // Column: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11
149 // Pin: F1, F0, B0, C7, F4, F5, F6, F7, D4, D6, B4, D7
152 static void init_cols(void)
154 DDRB &= ~(1<<4 | 1<<0);
155 PORTB |= (1<<4 | 1<<0);
158 DDRD &= ~(1<<7 | 1<<6 | 1<<4);
159 PORTD |= (1<<7 | 1<<6 | 1<<4);
160 DDRF &= ~(1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
161 PORTF |= (1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
165 static matrix_row_t read_cols(void)
167 return (PINF&(1<<1) ? 0 : (1<<0)) |
168 (PINF&(1<<0) ? 0 : (1<<1)) |
169 (PINB&(1<<0) ? 0 : (1<<2)) |
170 (PINC&(1<<7) ? 0 : (1<<3)) |
171 (PINF&(1<<4) ? 0 : (1<<4)) |
172 (PINF&(1<<5) ? 0 : (1<<5)) |
173 (PINF&(1<<6) ? 0 : (1<<6)) |
174 (PINF&(1<<7) ? 0 : (1<<7)) |
175 (PIND&(1<<4) ? 0 : (1<<8)) |
176 (PIND&(1<<6) ? 0 : (1<<9)) |
177 (PINB&(1<<4) ? 0 : (1<<10)) |
178 (PIND&(1<<7) ? 0 : (1<<11));
182 static void unselect_rows(void)
184 DDRB &= ~(1<<5 | 1<<6);
185 PORTB |= (1<<5 | 1<<6);
186 DDRD &= ~(1<<0 | 1<<5);
187 PORTD |= (1<<0 | 1<<5);
192 // Planck PCB Rev 1 Pin Assignments
195 // Pin: D0, D5, B5, B6
198 static void select_row(uint8_t row)