2 Copyright 2018 Carlos Filoteo
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <util/delay.h>
30 #include "protocol/serial.h"
37 static uint8_t debouncing = DEBOUNCE;
39 /* matrix state(1:on, 0:off) */
40 static matrix_row_t matrix[MATRIX_ROWS];
41 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
43 static matrix_row_t read_cols(void);
44 static void init_cols(void);
45 static void unselect_rows(void);
46 static void select_row(uint8_t row);
50 uint8_t matrix_rows(void)
56 uint8_t matrix_cols(void)
61 void matrix_init(void)
63 // initialize row and col
67 // initialize matrix state: all keys off
68 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
70 matrix_debouncing[i] = 0;
78 uint8_t matrix_scan(void)
80 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
82 _delay_us(30); // without this wait read unstable value.
83 matrix_row_t cols = read_cols();
84 if (matrix_debouncing[i] != cols) {
85 matrix_debouncing[i] = cols;
87 debug("bounce!: "); debug_hex(debouncing); debug("\n");
89 debouncing = DEBOUNCE;
98 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
99 matrix[i] = matrix_debouncing[i];
107 bool matrix_is_modified(void)
109 if (debouncing) return false;
114 bool matrix_is_on(uint8_t row, uint8_t col)
116 return (matrix[row] & ((matrix_row_t)1<<col));
120 matrix_row_t matrix_get_row(uint8_t row)
125 void matrix_print(void)
127 print("\nr/c 0123456789ABCDEF\n");
128 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
129 phex(row); print(": ");
130 pbin_reverse16(matrix_get_row(row));
135 uint8_t matrix_key_count(void)
138 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
139 count += bitpop16(matrix[i]);
144 /* Column pin configuration
145 * col: 0 1 2 3 4 5 6 7 8 9 10 11
146 * pin: D7 E6 B4 B5 B6 B2 B3 B1 F7 F6 F5 F4
149 static void init_cols(void)
151 // Input with pull-up(DDR:0, PORT:1)
152 DDRF &= ~(1<<4 | 1<<5 | 1<<6 | 1<<7);
153 PORTF |= (1<<4 | 1<<5 | 1<<6 | 1<<7);
158 DDRB &= ~(1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6);
159 PORTB |= (1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6);
162 static matrix_row_t read_cols(void)
164 return (PIND&(1<<7) ? 0 : (1<<0)) |
165 (PINE&(1<<6) ? 0 : (1<<1)) |
166 (PINB&(1<<4) ? 0 : (1<<2)) |
167 (PINB&(1<<5) ? 0 : (1<<3)) |
168 (PINB&(1<<6) ? 0 : (1<<4)) |
169 (PINB&(1<<2) ? 0 : (1<<5)) |
170 (PINB&(1<<3) ? 0 : (1<<6)) |
171 (PINB&(1<<1) ? 0 : (1<<7)) |
172 (PINF&(1<<7) ? 0 : (1<<8)) |
173 (PINF&(1<<6) ? 0 : (1<<9)) |
174 (PINF&(1<<5) ? 0 : (1<<10)) |
175 (PINF&(1<<4) ? 0 : (1<<11));
178 /* Row pin configuration
183 static void unselect_rows(void)
185 // Hi-Z(DDR:0, PORT:0) to unselect
187 PORTD &= ~0b00010011;
189 PORTC &= ~0b01000000;
192 static void select_row(uint8_t row)
194 // Output low(DDR:1, PORT:0) to select