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
);
47 uint8_t matrix_rows(void)
53 uint8_t matrix_cols(void)
58 void matrix_init(void)
60 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
64 // initialize row and col
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
];
104 bool matrix_is_modified(void)
106 if (debouncing
) return false;
111 bool matrix_is_on(uint8_t row
, uint8_t col
)
113 return (matrix
[row
] & ((matrix_row_t
)1<<col
));
117 matrix_row_t
matrix_get_row(uint8_t row
)
122 void matrix_print(void)
124 print("\nr/c 0123456789ABCDEF\n");
125 for (uint8_t row
= 0; row
< MATRIX_ROWS
; row
++) {
126 phex(row
); print(": ");
127 pbin_reverse16(matrix_get_row(row
));
132 uint8_t matrix_key_count(void)
135 for (uint8_t i
= 0; i
< MATRIX_ROWS
; i
++) {
136 count
+= bitpop16(matrix
[i
]);
141 /* Column pin configuration
142 * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13
143 * pin: F0 F1 E6 C7 C6 B6 D4 B1 B0 B5 B4 D7 D6 B3 (Rev.A)
146 static void init_cols(void)
148 // Input with pull-up(DDR:0, PORT:1)
149 DDRF
&= ~(1<<1 | 1<<4);
150 PORTF
|= (1<<1 | 1<<4);
151 DDRD
&= ~(1<<4 | 1<<6);
152 PORTD
|= (1<<4 | 1<<6);
155 static matrix_row_t
read_cols(void)
157 return (PINF
&(1<<1) ? 0 : (1<<0)) |
158 (PINF
&(1<<4) ? 0 : (1<<1)) |
159 (PIND
&(1<<6) ? 0 : (1<<2)) |
160 (PIND
&(1<<4) ? 0 : (1<<3));
163 /* Row pin configuration
165 * pin: D0 D1 D2 D3 D5
167 static void unselect_rows(void)
169 // Hi-Z(DDR:0, PORT:0) to unselect
171 PORTB
&= ~0b00010000;
173 PORTD
&= ~0b10000000;
175 PORTF
&= ~0b00100001;
178 static void select_row(uint8_t row
)
180 // Output low(DDR:1, PORT:0) to select