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 matrix_row_t matrix[MATRIX_ROWS];
38 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
40 static matrix_row_t read_cols(void);
41 static void init_cols(void);
42 static void unselect_rows(void);
43 static void select_row(uint8_t row);
46 void matrix_init(void)
51 // initialize row and col
55 // initialize matrix state: all keys off
56 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
58 matrix_debouncing[i] = 0;
62 uint8_t matrix_scan(void)
64 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
66 _delay_us(30); // without this wait read unstable value.
67 matrix_row_t cols = read_cols();
68 if (matrix_debouncing[i] != cols) {
69 matrix_debouncing[i] = cols;
71 debug("bounce!: "); debug_hex(debouncing); debug("\n");
73 debouncing = DEBOUNCE;
82 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
83 matrix[i] = matrix_debouncing[i];
92 matrix_row_t matrix_get_row(uint8_t row)
97 /* Column pin configuration
101 static void init_cols(void)
103 // Input with pull-up(DDR:0, PORT:1)
108 static matrix_row_t read_cols(void)
110 return (PINB&(1<<0) ? 0 : (1<<0));
113 /* Row pin configuration
117 static void unselect_rows(void)
119 // Hi-Z(DDR:0, PORT:0) to unselect
121 PORTB &= ~0b00000010;
124 static void select_row(uint8_t row)
126 // Output low(DDR:1, PORT:0) to select