]> git.gir.st - tmk_keyboard.git/blob - keyboard/provan/matrix.c
build with ./make and flash with ./make flash
[tmk_keyboard.git] / keyboard / provan / matrix.c
1 /*
2 Copyright 2012 Jun Wako <wakojun@gmail.com>
3
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.
8
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.
13
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/>.
16 */
17
18 /*
19 * scan matrix
20 */
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <avr/io.h>
24 #include <util/delay.h>
25 #include "print.h"
26 #include "debug.h"
27 #include "util.h"
28 #include "matrix.h"
29
30
31 #ifndef DEBOUNCE
32 # define DEBOUNCE 5
33 #endif
34 static uint8_t debouncing = DEBOUNCE;
35
36 /* matrix state(1:on, 0:off) */
37 static matrix_row_t matrix[MATRIX_ROWS];
38 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
39
40 static matrix_row_t matrix_reversed[MATRIX_COLS];
41 static matrix_row_t matrix_reversed_debouncing[MATRIX_COLS];
42
43 static matrix_row_t read_cols(void);
44 static void init_cols(void);
45 static void unselect_rows(void);
46 static void select_row(uint8_t row);
47
48
49 inline
50 uint8_t matrix_rows(void)
51 {
52 return MATRIX_ROWS;
53 }
54
55 inline
56 uint8_t matrix_cols(void)
57 {
58 return MATRIX_COLS;
59 }
60
61 void matrix_init(void)
62 {
63 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
64 MCUCR |= (1<<JTD);
65 MCUCR |= (1<<JTD);
66
67 // initialize row and col
68 unselect_rows();
69 init_cols();
70
71 // initialize matrix state: all keys off
72 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
73 matrix[i] = 0;
74 matrix_debouncing[i] = 0;
75 }
76 }
77
78 uint8_t matrix_scan(void)
79 {
80 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
81 select_row(i);
82 _delay_us(30); // without this wait read unstable value.
83 matrix_row_t rows = read_cols();
84 if (matrix_reversed_debouncing[i] != rows) {
85 matrix_reversed_debouncing[i] = rows;
86 if (debouncing) {
87 debug("bounce!: "); debug_hex(debouncing); debug("\n");
88 }
89 debouncing = DEBOUNCE;
90 }
91 unselect_rows();
92 }
93
94 if (debouncing) {
95 if (--debouncing) {
96 _delay_ms(1);
97 } else {
98 for (uint8_t i = 0; i < MATRIX_COLS; i++) {
99 matrix_reversed[i] = matrix_reversed_debouncing[i];
100 }
101 }
102 }
103
104 for (uint8_t y = 0; y < MATRIX_ROWS; y++) {
105 matrix_row_t row = 0;
106 for (uint8_t x = 0; x < MATRIX_COLS; x++) {
107 row |= ((matrix_reversed[x] & (1<<y)) >> y) << x;
108 }
109 matrix[y] = row;
110 }
111
112 return 1;
113 }
114
115 bool matrix_is_modified(void)
116 {
117 if (debouncing) return false;
118 return true;
119 }
120
121 inline
122 bool matrix_is_on(uint8_t row, uint8_t col)
123 {
124 return (matrix[row] & ((matrix_row_t)1<<col));
125 }
126
127 inline
128 matrix_row_t matrix_get_row(uint8_t row)
129 {
130 return matrix[row];
131 }
132
133 void matrix_print(void)
134 {
135 print("\nr/c 0123456789ABCDEF\n");
136 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
137 phex(row); print(": ");
138 pbin_reverse16(matrix_get_row(row));
139 print("\n");
140 }
141 }
142
143 /* Column pin configuration
144 * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13
145 * pin: F0 F1 E6 C7 C6 B6 D4 B1 B0 B5 B4 D7 D6 B3 (Rev.A)
146 * pin: B7 (Rev.B)
147 */
148 static void init_cols(void)
149 {
150 // Input with pull-up(DDR:0, PORT:1)
151 // DDRF &= ~(1<<0 | 1<<1 | 1<<4 | 1<<5);
152 // PORTF |= (1<<0 | 1<<1 | 1<<4 | 1<<5);
153 // DDRD &= ~(1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
154 // PORTD |= (1<<1 | 1<<2 | 1<<3 | 1<<4 | 1<<5 | 1<<6 | 1<<7);
155 // DDRB &= ~(1<<1 | 1<<3);
156 // PORTB |= (1<<1 | 1<<3);
157
158 // because I'm a dumbass
159 DDRB &= ~(1<<0 | 1<<2 | 1<<4 | 1<<5 | 1<<6);
160 PORTB |= (1<<0 | 1<<2 | 1<<4 | 1<<5 | 1<<6);
161 }
162
163 static matrix_row_t read_cols(void)
164 {
165 // return (PINF&(1<<5) ? 0 : (1<<0)) |
166 // (PINB&(1<<1) ? 0 : (1<<1)) |
167 // (PINF&(1<<0) ? 0 : (1<<2)) |
168 // (PINF&(1<<1) ? 0 : (1<<3)) |
169 // (PINF&(1<<4) ? 0 : (1<<4)) |
170 // (PINB&(1<<3) ? 0 : (1<<5)) |
171 // (PIND&(1<<7) ? 0 : (1<<6)) |
172 // (PIND&(1<<6) ? 0 : (1<<7)) |
173 // (PIND&(1<<4) ? 0 : (1<<8)) |
174 // (PIND&(1<<5) ? 0 : (1<<9)) |
175 // (PIND&(1<<3) ? 0 : (1<<10)) |
176 // (PIND&(1<<2) ? 0 : (1<<11)) |
177 // (PIND&(1<<1) ? 0 : (1<<12));
178
179 //because I'm a dumbass
180 return (PINB&(1<<0) ? 0 : (1<<0)) |
181 (PINB&(1<<2) ? 0 : (1<<1)) |
182 (PINB&(1<<4) ? 0 : (1<<2)) |
183 (PINB&(1<<5) ? 0 : (1<<3)) |
184 (PINB&(1<<6) ? 0 : (1<<4));
185 }
186
187 /* Row pin configuration
188 * row: 0 1 2 3 4
189 * pin: D0 D1 D2 D3 D5
190 */
191 static void unselect_rows(void)
192 {
193 // Hi-Z(DDR:0, PORT:0) to unselect
194 // DDRB &= ~0b01110101;
195 // PORTB &= ~0b01110101;
196
197 //because I'm a dumbass
198 DDRB &= ~0b00001010;
199 PORTB &= ~0b00001010;
200 DDRD &= ~0b11111110;
201 PORTD &= ~0b11111110;
202 DDRF &= ~0b00110011;
203 PORTF &= ~0b00110011;
204 }
205
206 static void select_row(uint8_t row)
207 {
208 // Output low(DDR:1, PORT:0) to select
209 // switch (row) {
210 // case 0:
211 // DDRB |= (1<<0);
212 // PORTB &= ~(1<<0);
213 // break;
214 // case 1:
215 // DDRB |= (1<<2);
216 // PORTB &= ~(1<<2);
217 // break;
218 // case 2:
219 // DDRB |= (1<<4);
220 // PORTB &= ~(1<<4);
221 // break;
222 // case 3:
223 // DDRB |= (1<<5);
224 // PORTB &= ~(1<<5);
225 // break;
226 // case 4:
227 // DDRB |= (1<<6);
228 // PORTB &= ~(1<<6);
229 // break;
230 // }
231
232 //because I'm a dumbass
233 switch (row) {
234 case 0:
235 DDRF |= (1<<5);
236 PORTF &= ~(1<<5);
237 break;
238 case 1:
239 DDRB |= (1<<1);
240 PORTB &= ~(1<<1);
241 break;
242 case 2:
243 DDRF |= (1<<0);
244 PORTF &= ~(1<<0);
245 break;
246 case 3:
247 DDRF |= (1<<1);
248 PORTF &= ~(1<<1);
249 break;
250 case 4:
251 DDRF |= (1<<4);
252 PORTF &= ~(1<<4);
253 break;
254 case 5:
255 DDRB |= (1<<3);
256 PORTB &= ~(1<<3);
257 break;
258 case 6:
259 DDRD |= (1<<7);
260 PORTD &= ~(1<<7);
261 break;
262 case 7:
263 DDRD |= (1<<6);
264 PORTD &= ~(1<<6);
265 break;
266 case 8:
267 DDRD |= (1<<4);
268 PORTD &= ~(1<<4);
269 break;
270 case 9:
271 DDRD |= (1<<5);
272 PORTD &= ~(1<<5);
273 break;
274 case 10:
275 DDRD |= (1<<3);
276 PORTD &= ~(1<<3);
277 break;
278 case 11:
279 DDRD |= (1<<2);
280 PORTD &= ~(1<<2);
281 break;
282 case 12:
283 DDRD |= (1<<1);
284 PORTD &= ~(1<<1);
285 break;
286 }
287 }
Imprint / Impressum