2 Copyright 2012 Jun Wako <wakojun@gmail.com>
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>
34 static uint8_t debouncing = DEBOUNCE;
36 /* matrix state(1:on, 0:off) */
37 static uint16_t *matrix;
38 static uint16_t *matrix_debouncing;
39 static uint16_t matrix0[MATRIX_ROWS];
40 static uint16_t matrix1[MATRIX_ROWS];
41 static bool is_modified;
43 static uint16_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
69 matrix_debouncing = matrix1;
70 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
72 matrix_debouncing[i] = 0;
77 uint8_t matrix_scan(void)
79 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
82 _delay_us(30); // without this wait read unstable value.
83 uint16_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;
100 uint16_t *tmp = matrix;
101 matrix = matrix_debouncing;
102 matrix_debouncing = tmp;
109 bool matrix_is_modified(void)
115 bool matrix_has_ghost(void)
121 bool matrix_is_on(uint8_t row, uint8_t col)
123 return (matrix[row] & (1<<col));
127 uint16_t matrix_get_row(uint8_t row)
132 void matrix_print(void)
134 print("\nr/c 0123456789ABCDEF\n");
135 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
136 phex(row); print(": ");
137 pbin_reverse16(matrix_get_row(row));
142 uint8_t matrix_key_count(void)
145 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
146 count += bitpop16(matrix[i]);
151 /* Column pin configuration
152 * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13
153 * pin: F0 F1 E6 C7 C6 B6 D4 B1 B0 B5 B4 D7 D6 B3
155 static void init_cols(void)
157 // Input with pull-up(DDR:0, PORT:1)
158 DDRF &= ~(1<<0 | 1<<1);
159 PORTF |= (1<<0 | 1<<1);
162 DDRD &= ~(1<<7 | 1<<6 | 1<<4);
163 PORTD |= (1<<7 | 1<<6 | 1<<4);
164 DDRC &= ~(1<<7 | 1<<6);
165 PORTC |= (1<<7 | 1<<6);
166 DDRB &= ~(1<<6 | 1<< 5 | 1<<4 | 1<<3 | 1<<1 | 1<<0);
167 PORTB |= (1<<6 | 1<< 5 | 1<<4 | 1<<3 | 1<<1 | 1<<0);
170 static uint16_t read_cols(void)
172 return (PINF&(1<<0) ? 0 : (1<<0)) |
173 (PINF&(1<<1) ? 0 : (1<<1)) |
174 (PINE&(1<<6) ? 0 : (1<<2)) |
175 (PINC&(1<<7) ? 0 : (1<<3)) |
176 (PINC&(1<<6) ? 0 : (1<<4)) |
177 (PINB&(1<<6) ? 0 : (1<<5)) |
178 (PIND&(1<<4) ? 0 : (1<<6)) |
179 (PINB&(1<<1) ? 0 : (1<<7)) |
180 (PINB&(1<<0) ? 0 : (1<<8)) |
181 (PINB&(1<<5) ? 0 : (1<<9)) |
182 (PINB&(1<<4) ? 0 : (1<<10)) |
183 (PIND&(1<<7) ? 0 : (1<<11)) |
184 (PIND&(1<<6) ? 0 : (1<<12)) |
185 (PINB&(1<<3) ? 0 : (1<<13));
188 /* Row pin configuration
190 * pin: D0 D1 D2 D3 D5
192 static void unselect_rows(void)
194 // Hi-Z(DDR:0, PORT:0) to unselect
196 PORTD &= ~0b00101111;
199 static void select_row(uint8_t row)
201 // Output low(DDR:1, PORT:0) to select