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>
34 static uint8_t debouncing = DEBOUNCE;
36 /* matrix state(1:on, 0:off) */
37 static matrix_row_t matrix[MATRIX_ROWS];
38 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
40 #ifdef MATRIX_HAS_GHOST
41 static bool matrix_has_ghost_in_row(uint8_t row);
43 static matrix_row_t read_cols(void);
44 static void unselect_rows(void);
45 static void select_row(uint8_t row);
49 uint8_t matrix_rows(void)
55 uint8_t matrix_cols(void)
60 void matrix_init(void)
62 // initialize row and col
64 // Input with pull-up(DDR:0, PORT:1)
68 // initialize matrix state: all keys off
69 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
71 matrix_debouncing[i] = 0;
75 uint8_t matrix_scan(void)
77 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
79 _delay_us(30); // without this wait read unstable value.
80 matrix_row_t cols = read_cols();
81 if (matrix_debouncing[i] != cols) {
82 matrix_debouncing[i] = cols;
84 debug("bounce!: "); debug_hex(debouncing); debug("\n");
86 debouncing = DEBOUNCE;
95 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
96 matrix[i] = matrix_debouncing[i];
105 bool matrix_is_modified(void)
107 if (debouncing) return false;
112 bool matrix_is_on(uint8_t row, uint8_t col)
114 return (matrix[row] & ((matrix_row_t)1<<col));
118 matrix_row_t matrix_get_row(uint8_t row)
123 void matrix_print(void)
125 print("\nr/c 01234567\n");
126 for (uint8_t row = 0; row < matrix_rows(); row++) {
127 phex(row); print(": ");
128 pbin_reverse(matrix_get_row(row));
129 #ifdef MATRIX_HAS_GHOST
130 if (matrix_has_ghost_in_row(row)) {
138 #ifdef MATRIX_HAS_GHOST
140 static bool matrix_has_ghost_in_row(uint8_t row)
142 // no ghost exists in case less than 2 keys on
143 if (((matrix[row] - 1) & matrix[row]) == 0)
146 // ghost exists in case same state as other row
147 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
148 if (i != row && (matrix[i] & matrix[row]))
156 static matrix_row_t read_cols(void)
162 static void unselect_rows(void)
164 // Hi-Z(DDR:0, PORT:0) to unselect
165 DDRC &= ~0b01000000; // PC: 6
166 PORTC &= ~0b01000000;
167 DDRD &= ~0b11100111; // PD: 7,6,5,2,1,0
168 PORTD &= ~0b11100111;
169 DDRF &= ~0b11000000; // PF: 7,6
170 PORTF &= ~0b11000000;
174 static void select_row(uint8_t row)
176 // Output low(DDR:1, PORT:0) to select
177 // row: 0 1 2 3 4 5 6 7 8
178 // pin: PD0, PD5, PD7, PF6, PD6, PD1, PD2, PC6, PF7