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 static matrix_row_t *matrix;
46 static matrix_row_t *matrix_debouncing;
47 static matrix_row_t matrix0[MATRIX_ROWS];
48 static matrix_row_t matrix1[MATRIX_ROWS];
50 #ifdef MATRIX_HAS_GHOST
51 static bool matrix_has_ghost_in_row(uint8_t row);
53 static matrix_row_t read_col(void);
54 static void unselect_rows(void);
55 static void select_row(uint8_t row);
59 uint8_t matrix_rows(void)
65 uint8_t matrix_cols(void)
70 void matrix_init(void)
72 // initialize row and col
74 // Input with pull-up(DDR:0, PORT:1)
78 // initialize matrix state: all keys off
79 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
84 matrix_debouncing = matrix1;
87 uint8_t matrix_scan(void)
89 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
92 _delay_us(30); // without this wait read unstable value.
93 if (matrix[i] != read_col()) {
94 matrix[i] = read_col();
96 debug("bounce!: "); debug_hex(debouncing); print("\n");
98 debouncing = DEBOUNCE;
107 matrix_row_t *tmp = matrix;
108 matrix = matrix_debouncing;
109 matrix_debouncing = tmp;
117 bool matrix_is_modified(void)
119 if (debouncing) return false;
124 bool matrix_is_on(uint8_t row, uint8_t col)
126 return (matrix[row] & (1<<col));
130 matrix_row_t matrix_get_row(uint8_t row)
135 void matrix_print(void)
137 print("\nr/c 01234567\n");
138 for (uint8_t row = 0; row < matrix_rows(); row++) {
139 phex(row); print(": ");
140 #if (MATRIX_COLS <= 8)
141 pbin_reverse(matrix_get_row(row));
143 pbin_reverse16(matrix_get_row(row));
145 #ifdef MATRIX_HAS_GHOST
146 if (matrix_has_ghost_in_row(row)) {
154 uint8_t matrix_key_count(void)
157 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
158 #if (MATRIX_COLS <= 8)
159 count += bitpop(matrix[i]);
161 count += bitpop16(matrix[i]);
167 #ifdef MATRIX_HAS_GHOST
169 static bool matrix_has_ghost_in_row(uint8_t row)
171 // no ghost exists in case less than 2 keys on
172 if (((matrix[row] - 1) & matrix[row]) == 0)
175 // ghost exists in case same state as other row
176 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
177 if (i != row && (matrix[i] & matrix[row]) == matrix[row])
185 static matrix_row_t read_col(void)
191 static void unselect_rows(void)
193 // Hi-Z(DDR:0, PORT:0) to unselect
194 DDRC &= ~0b01000000; // PC: 6
195 PORTC &= ~0b01000000;
196 DDRD &= ~0b11100111; // PD: 7,6,5,2,1,0
197 PORTD &= ~0b11100111;
198 DDRF &= ~0b11000000; // PF: 7,6
199 PORTF &= ~0b11000000;
203 static void select_row(uint8_t row)
205 // Output low(DDR:1, PORT:0) to select
206 // row: 0 1 2 3 4 5 6 7 8
207 // pin: PD0, PD5, PD7, PF6, PD6, PD1, PD2, PC6, PF7