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>
25 #include "action_layer.h"
35 static uint8_t debouncing = DEBOUNCE;
37 /* matrix state(1:on, 0:off) */
38 static matrix_row_t matrix[MATRIX_ROWS];
39 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
41 static matrix_row_t read_cols(void);
42 static void init_cols(void);
43 static void unselect_rows(void);
44 static void select_row(uint8_t row);
48 uint8_t matrix_rows(void)
54 uint8_t matrix_cols(void)
60 void matrix_init(void)
62 // initialize row and col
66 // initialize matrix state: all keys off
67 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
69 matrix_debouncing[i] = 0;
73 uint8_t matrix_scan(void)
75 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
77 _delay_us(30); // without this wait read unstable value.
78 matrix_row_t cols = read_cols();
79 if (matrix_debouncing[i] != cols) {
80 matrix_debouncing[i] = cols;
82 debug("bounce!: "); debug_hex(debouncing); debug("\n");
84 debouncing = DEBOUNCE;
93 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
94 matrix[i] = matrix_debouncing[i];
103 bool matrix_is_modified(void)
105 if (debouncing) return false;
110 bool matrix_is_on(uint8_t row, uint8_t col)
112 return (matrix[row] & ((matrix_row_t)1<<col));
116 matrix_row_t matrix_get_row(uint8_t row)
121 void matrix_print(void)
123 print("\nr/c 0123456789ABCDEF\n");
124 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
125 phex(row); print(": ");
126 pbin_reverse16(matrix_get_row(row));
131 uint8_t matrix_key_count(void)
134 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
135 count += bitpop16(matrix[i]);
140 /* Column pin configuration
141 * col: 0 1 2 3 4 5 6 7 8 9 10 11
142 * pin: F0 F1 F4 F5 F6 F7 B6 B5 B4 D7 D5 D4
145 static void init_cols(void)
147 DDRC &= ~(1<<6 | 1<<7);
148 PORTC |= (1<<6 | 1<<7);
149 DDRD &= ~(1<<4 | 1<<5 | 1<<6 | 1<<7);
150 PORTD |= (1<<4 | 1<<5 | 1<<6 | 1<<7);
151 DDRB &= ~(1<<4 | 1<<5 | 1<<6);
152 PORTB |= (1<<4 | 1<<5 | 1<<6);
153 DDRF &= ~(1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
154 PORTF |= (1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
157 static matrix_row_t read_cols(void)
159 return (PINC&(1<<6) ? 0 : (1<< 0)) |
160 (PINC&(1<<7) ? 0 : (1<< 1)) |
161 (PIND&(1<<5) ? 0 : (1<< 2)) |
162 (PIND&(1<<4) ? 0 : (1<< 3)) |
163 (PIND&(1<<6) ? 0 : (1<< 4)) |
164 (PIND&(1<<7) ? 0 : (1<< 5)) |
165 (PINB&(1<<4) ? 0 : (1<< 6)) |
166 (PINB&(1<<5) ? 0 : (1<< 7)) |
167 (PINB&(1<<6) ? 0 : (1<< 8)) |
168 (PINF&(1<<7) ? 0 : (1<< 9)) |
169 (PINF&(1<<6) ? 0 : (1<<10)) |
170 (PINF&(1<<5) ? 0 : (1<<11)) |
171 (PINF&(1<<4) ? 0 : (1<<12)) |
172 (PINF&(1<<1) ? 0 : (1<<13)) |
173 (PINF&(1<<0) ? 0 : (1<<14));
176 /* Row pin configuration
180 static void unselect_rows(void)
182 // Hi-Z(DDR:0, PORT:0) to unselect
183 DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<7);
184 PORTB |= (1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<7);
187 static void select_row(uint8_t row)