2 Copyright 2016 Ralf Schmitt <ralf@bunkertor.net>
3 This program is free software: you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation, either version 2 of the License, or
6 (at your option) any later version.
7 This program is distributed in the hope that it will be useful,
8 but WITHOUT ANY WARRANTY; without even the implied warranty of
9 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 GNU General Public License for more details.
11 You should have received a copy of the GNU General Public License
12 along with this program. If not, see <http://www.gnu.org/licenses/>.
18 #include <util/delay.h>
24 static uint8_t debouncing = DEBOUNCING_DELAY;
26 /* matrix state(1:on, 0:off) */
27 static matrix_row_t matrix[MATRIX_ROWS];
28 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
30 static uint8_t read_rows(uint8_t col);
31 static void init_ports(void);
32 static void unselect_cols(void);
33 static void select_col(uint8_t col);
35 __attribute__ ((weak))
36 void matrix_init_kb(void) {
40 __attribute__ ((weak))
41 void matrix_scan_kb(void) {
45 __attribute__ ((weak))
46 void matrix_init_user(void) {
49 __attribute__ ((weak))
50 void matrix_scan_user(void) {
54 uint8_t matrix_rows(void)
60 uint8_t matrix_cols(void)
65 void backlight_init_ports(void)
75 void matrix_init(void)
77 backlight_init_ports();
80 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
82 matrix_debouncing[i] = 0;
86 uint8_t matrix_scan(void)
88 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
91 uint8_t rows = read_rows(col);
92 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
93 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
94 bool curr_bit = rows & (1<<row);
95 if (prev_bit != curr_bit) {
96 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
98 dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
100 debouncing = DEBOUNCING_DELAY;
110 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
111 matrix[i] = matrix_debouncing[i];
119 bool matrix_is_modified(void)
121 if (debouncing) return false;
126 bool matrix_is_on(uint8_t row, uint8_t col)
128 return (matrix[row] & ((matrix_row_t)1<<col));
132 matrix_row_t matrix_get_row(uint8_t row)
137 void matrix_print(void)
139 print("\nr/c 0123456789ABCDEF\n");
140 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
141 xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
145 uint8_t matrix_key_count(void)
148 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
149 count += bitpop32(matrix[i]);
154 static void init_ports(void)
156 // Rows are inputs (inputs are 0)
161 // Columns are outputs (outputs are high)
168 static uint8_t read_rows(uint8_t col)
171 return (PINE&(1<<2) ? 0 : (1<<0)) |
172 (PIND&(1<<0) ? (1<<1) : 0) |
173 (PIND&(1<<1) ? (1<<2) : 0) |
174 (PIND&(1<<2) ? (1<<3) : 0) |
175 (PIND&(1<<3) ? (1<<4) : 0) |
176 (PIND&(1<<5) ? (1<<5) : 0);
179 return (PINB&(1<<7) ? (1<<0) : 0) |
180 (PIND&(1<<0) ? (1<<1) : 0) |
181 (PIND&(1<<1) ? (1<<2) : 0) |
182 (PIND&(1<<2) ? (1<<3) : 0) |
183 (PIND&(1<<3) ? (1<<4) : 0) |
184 (PIND&(1<<5) ? (1<<5) : 0);
189 static void unselect_cols(void)
197 static void select_col(uint8_t col)