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"
36 static uint8_t debouncing = DEBOUNCE;
38 /* matrix state(1:on, 0:off) */
39 static matrix_row_t matrix[MATRIX_ROWS];
40 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
42 static matrix_row_t read_cols(void);
43 static void init_cols(void);
44 static void init_encoder(void);
45 static void init_pot(void);
46 static void unselect_rows(void);
47 static void select_row(uint8_t row);
48 int16_t analogRead(uint8_t pin);
54 uint8_t matrix_rows(void)
60 uint8_t matrix_cols(void)
65 void matrix_init(void)
67 // initialize row and col
73 // initialize matrix state: all keys off
74 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
76 matrix_debouncing[i] = 0;
80 static void init_encoder(void)
82 DDRC &= ~(1<<6 | 1<<7);
83 PORTC |= (1<<6 | 1<<7);
87 if (PINC&(1<<6)) s |= 1;
88 if (PINC&(1<<7)) s |= 2;
93 void read_encoder(void)
95 uint8_t s = state & 3;
96 if (PINC&(1<<6)) s |= 4;
97 if (PINC&(1<<7)) s |= 8;
100 case 1: case 7: case 8: case 14:
103 case 2: case 4: case 11: case 13:
115 #define HEX(n) (((n) < 10) ? ((n) + '0') : ((n) + 'A' - 10))
117 static void init_pot(void)
124 uint8_t matrix_scan(void)
126 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
128 _delay_us(30); // without this wait read unstable value.
129 matrix_row_t cols = read_cols();
130 if (matrix_debouncing[i] != cols) {
131 matrix_debouncing[i] = cols;
133 debug("bounce!: "); debug_hex(debouncing); debug("\n");
135 debouncing = DEBOUNCE;
144 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
145 matrix[i] = matrix_debouncing[i];
152 register_code(KC_AUDIO_VOL_UP);
153 unregister_code(KC_AUDIO_VOL_UP);
155 } else if (position <= -2) {
156 register_code(KC_AUDIO_VOL_DOWN);
157 unregister_code(KC_AUDIO_VOL_DOWN);
161 // uint16_t val = analogRead(11);
163 // debug("analogRead: "); debug_hex(val); debug("\n");
168 bool matrix_is_modified(void)
170 if (debouncing) return false;
175 bool matrix_is_on(uint8_t row, uint8_t col)
177 return (matrix[row] & ((matrix_row_t)1<<col));
181 matrix_row_t matrix_get_row(uint8_t row)
186 void matrix_print(void)
188 print("\nr/c 0123456789ABCDEF\n");
189 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
190 phex(row); print(": ");
191 pbin_reverse16(matrix_get_row(row));
196 uint8_t matrix_key_count(void)
199 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
200 count += bitpop16(matrix[i]);
205 /* Column pin configuration
206 * col: 0 1 2 3 4 5 6 7 8 9 10 11
207 * pin: F0 F1 F4 F5 F6 F7 B6 B5 B4 D7 D5 D4
210 static void init_cols(void)
212 DDRF &= ~(1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
213 PORTF |= (1<<0 | 1<<1 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
216 DDRB &= ~(1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<7);
217 PORTB |= (1<<0 | 1<<1 | 1<<2 | 1<<3 | 1<<7);
220 static matrix_row_t read_cols(void)
222 return (PINB&(1<<0) ? 0 : (1<< 0)) |
223 (PINB&(1<<1) ? 0 : (1<< 1)) |
224 (PINB&(1<<2) ? 0 : (1<< 2)) |
225 (PINB&(1<<3) ? 0 : (1<< 3)) |
226 (PINB&(1<<7) ? 0 : (1<< 4)) |
227 (PIND&(1<<0) ? 0 : (1<< 5)) |
228 (PINF&(1<<7) ? 0 : (1<< 6)) |
229 (PINF&(1<<6) ? 0 : (1<< 7)) |
230 (PINF&(1<<5) ? 0 : (1<< 8)) |
231 (PINF&(1<<4) ? 0 : (1<< 9)) |
232 (PINF&(1<<1) ? 0 : (1<<10)) |
233 (PINF&(1<<0) ? 0 : (1<<11));
236 /* Row pin configuration
240 static void unselect_rows(void)
242 // Hi-Z(DDR:0, PORT:0) to unselect
244 PORTB &= ~0b01110000;
246 PORTD &= ~0b10000000;
249 static void select_row(uint8_t row)