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>
30 #if (MATRIX_COLS > 16)
31 # error "MATRIX_COLS must not exceed 16"
33 #if (MATRIX_ROWS > 255)
34 # error "MATRIX_ROWS must not exceed 255"
38 // matrix state buffer(1:on, 0:off)
39 #if (MATRIX_COLS <= 8)
40 static uint8_t *matrix;
41 static uint8_t *matrix_prev;
42 static uint8_t _matrix0[MATRIX_ROWS];
43 static uint8_t _matrix1[MATRIX_ROWS];
45 static uint16_t *matrix;
46 static uint16_t *matrix_prev;
47 static uint16_t _matrix0[MATRIX_ROWS];
48 static uint16_t _matrix1[MATRIX_ROWS];
51 #ifdef MATRIX_HAS_GHOST
52 static bool matrix_has_ghost_in_row(uint8_t row);
54 static uint8_t read_col(void);
55 static void unselect_rows(void);
56 static void select_row(uint8_t row);
60 uint8_t matrix_rows(void)
66 uint8_t matrix_cols(void)
71 void matrix_init(void)
73 // initialize row and col
75 // Input with pull-up(DDR:0, PORT:1)
79 // initialize matrix state: all keys off
80 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
81 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
83 matrix_prev = _matrix1;
86 uint8_t matrix_scan(void)
94 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
97 _delay_us(30); // without this wait read unstable value.
98 matrix[i] = ~read_col();
104 bool matrix_is_modified(void)
106 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
107 if (matrix[i] != matrix_prev[i])
114 bool matrix_has_ghost(void)
116 #ifdef MATRIX_HAS_GHOST
117 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
118 if (matrix_has_ghost_in_row(i))
126 bool matrix_is_on(uint8_t row, uint8_t col)
128 return (matrix[row] & (1<<col));
132 #if (MATRIX_COLS <= 8)
133 uint8_t matrix_get_row(uint8_t row)
135 uint16_t matrix_get_row(uint8_t row)
141 void matrix_print(void)
143 print("\nr/c 01234567\n");
144 for (uint8_t row = 0; row < matrix_rows(); row++) {
145 phex(row); print(": ");
146 #if (MATRIX_COLS <= 8)
147 pbin_reverse(matrix_get_row(row));
149 pbin_reverse16(matrix_get_row(row));
151 #ifdef MATRIX_HAS_GHOST
152 if (matrix_has_ghost_in_row(row)) {
160 uint8_t matrix_key_count(void)
163 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
164 #if (MATRIX_COLS <= 8)
165 count += bitpop(matrix[i]);
167 count += bitpop16(matrix[i]);
173 #ifdef MATRIX_HAS_GHOST
175 static bool matrix_has_ghost_in_row(uint8_t row)
177 // no ghost exists in case less than 2 keys on
178 if (((matrix[row] - 1) & matrix[row]) == 0)
181 // ghost exists in case same state as other row
182 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
183 if (i != row && (matrix[i] & matrix[row]) == matrix[row])
191 static uint8_t read_col(void)
197 static void unselect_rows(void)
199 // Hi-Z(DDR:0, PORT:0) to unselect
200 DDRC &= ~0b11000000; // PC: 7,6
201 PORTC &= ~0b11000000;
202 DDRD &= ~0b11000111; // PD: 7,6,2,1,0
203 PORTD &= ~0b11000111;
204 DDRF &= ~0b11000000; // PF: 7,6
205 PORTF &= ~0b11000000;
209 static void select_row(uint8_t row)
211 // Output low(DDR:1, PORT:0) to select
212 // row: 0 1 2 3 4 5 6 7 8
213 // pin: PD0, PC7, PD7, PF6, PD6, PD1, PD2, PC6, PF7