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 setup_leds(void) {
66 void matrix_init(void)
68 // initialize row and col
74 // initialize matrix state: all keys off
75 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
77 matrix_debouncing[i] = 0;
81 uint8_t matrix_scan(void)
83 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
85 _delay_us(30); // without this wait read unstable value.
86 matrix_row_t cols = read_cols();
87 if (matrix_debouncing[i] != cols) {
88 matrix_debouncing[i] = cols;
90 debug("bounce!: "); debug_hex(debouncing); debug("\n");
92 debouncing = DEBOUNCE;
101 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
102 matrix[i] = matrix_debouncing[i];
107 // uint8_t layer = biton32(default_layer_state);
108 switch (default_layer_state) {
122 bool matrix_is_modified(void)
124 if (debouncing) return false;
129 bool matrix_is_on(uint8_t row, uint8_t col)
131 return (matrix[row] & ((matrix_row_t)1<<col));
135 matrix_row_t matrix_get_row(uint8_t row)
140 void matrix_print(void)
142 print("\nr/c 0123456789ABCDEF\n");
143 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
144 phex(row); print(": ");
145 pbin_reverse16(matrix_get_row(row));
150 uint8_t matrix_key_count(void)
153 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
154 count += bitpop16(matrix[i]);
159 /* Column pin configuration
160 * col: 0 1 2 3 4 5 6 7 8 9 10 11
161 * pin: F0 F1 F4 F5 F6 F7 B6 B5 B4 D7 D5 D4
164 static void init_cols(void)
166 DDRC &= ~(1<<6 | 1<<7);
167 PORTC |= (1<<6 | 1<<7);
168 DDRD &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<5);
169 PORTD |= (1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<5);
170 DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<7);
171 PORTB |= (1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<7);
174 static matrix_row_t read_cols(void)
176 return (PIND&(1<<5) ? 0 : (1<< 0)) |
177 (PINC&(1<<7) ? 0 : (1<< 1)) |
178 (PINC&(1<<6) ? 0 : (1<< 2)) |
179 (PIND&(1<<3) ? 0 : (1<< 3)) |
180 (PIND&(1<<2) ? 0 : (1<< 4)) |
181 (PIND&(1<<1) ? 0 : (1<< 5)) |
182 (PIND&(1<<0) ? 0 : (1<< 6)) |
183 (PINB&(1<<7) ? 0 : (1<< 7)) |
184 (PINB&(1<<3) ? 0 : (1<< 8)) |
185 (PINB&(1<<2) ? 0 : (1<< 9)) |
186 (PINB&(1<<1) ? 0 : (1<<10)) |
187 (PINB&(1<<0) ? 0 : (1<<11));
190 /* Row pin configuration
194 static void unselect_rows(void)
196 // Hi-Z(DDR:0, PORT:0) to unselect
197 DDRD &= ~(1<<4 | 1<<6 | 1<<7);
198 PORTD |= (1<<4 | 1<<6 | 1<<7);
203 static void select_row(uint8_t row)