2 Copyright 2011 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>
31 #if (MATRIX_COLS > 16)
32 # error "MATRIX_COLS must not exceed 16"
34 #if (MATRIX_ROWS > 255)
35 # error "MATRIX_ROWS must not exceed 255"
42 static uint8_t debouncing = DEBOUNCE;
44 // matrix state buffer(1:on, 0:off)
45 #if (MATRIX_COLS <= 8)
46 static uint8_t *matrix;
47 static uint8_t *matrix_prev;
48 static uint8_t _matrix0[MATRIX_ROWS];
49 static uint8_t _matrix1[MATRIX_ROWS];
51 static uint16_t *matrix;
52 static uint16_t *matrix_prev;
53 static uint16_t _matrix0[MATRIX_ROWS];
54 static uint16_t _matrix1[MATRIX_ROWS];
57 #ifdef MATRIX_HAS_GHOST
58 static bool matrix_has_ghost_in_row(uint8_t row);
60 static uint8_t read_col(void);
61 static void unselect_rows(void);
62 static void select_row(uint8_t row);
66 uint8_t matrix_rows(void)
72 uint8_t matrix_cols(void)
77 void matrix_init(void)
79 // initialize row and col
81 // Input with pull-up(DDR:0, PORT:1)
85 // initialize matrix state: all keys off
86 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
87 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
89 matrix_prev = _matrix1;
92 uint8_t matrix_scan(void)
95 uint8_t *tmp = matrix_prev;
100 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
103 _delay_us(30); // without this wait read unstable value.
104 if (matrix[i] != (uint8_t)~read_col()) {
105 matrix[i] = (uint8_t)~read_col();
107 debug("bounce!: "); debug_hex(debouncing); print("\n");
109 debouncing = DEBOUNCE;
121 bool matrix_is_modified(void)
123 if (debouncing) return false;
124 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
125 if (matrix[i] != matrix_prev[i]) {
133 bool matrix_has_ghost(void)
135 #ifdef MATRIX_HAS_GHOST
136 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
137 if (matrix_has_ghost_in_row(i))
145 bool matrix_is_on(uint8_t row, uint8_t col)
147 return (matrix[row] & (1<<col));
151 #if (MATRIX_COLS <= 8)
152 uint8_t matrix_get_row(uint8_t row)
154 uint16_t matrix_get_row(uint8_t row)
160 void matrix_print(void)
162 print("\nr/c 01234567\n");
163 for (uint8_t row = 0; row < matrix_rows(); row++) {
164 phex(row); print(": ");
165 #if (MATRIX_COLS <= 8)
166 pbin_reverse(matrix_get_row(row));
168 pbin_reverse16(matrix_get_row(row));
170 #ifdef MATRIX_HAS_GHOST
171 if (matrix_has_ghost_in_row(row)) {
179 uint8_t matrix_key_count(void)
182 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
183 #if (MATRIX_COLS <= 8)
184 count += bitpop(matrix[i]);
186 count += bitpop16(matrix[i]);
192 #ifdef MATRIX_HAS_GHOST
194 static bool matrix_has_ghost_in_row(uint8_t row)
196 // no ghost exists in case less than 2 keys on
197 if (((matrix[row] - 1) & matrix[row]) == 0)
200 // ghost exists in case same state as other row
201 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
202 if (i != row && (matrix[i] & matrix[row]) == matrix[row])
210 static uint8_t read_col(void)
216 static void unselect_rows(void)
218 // Hi-Z(DDR:0, PORT:0) to unselect
219 DDRC &= ~0b01000000; // PC: 6
220 PORTC &= ~0b01000000;
221 DDRD &= ~0b11100111; // PD: 7,6,5,2,1,0
222 PORTD &= ~0b11100111;
223 DDRF &= ~0b11000000; // PF: 7,6
224 PORTF &= ~0b11000000;
228 static void select_row(uint8_t row)
230 // Output low(DDR:1, PORT:0) to select
231 // row: 0 1 2 3 4 5 6 7 8
232 // pin: PD0, PD5, PD7, PF6, PD6, PD1, PD2, PC6, PF7