]> git.gir.st - tmk_keyboard.git/blob - keyboard/phantom/matrix.c
Correct order of rows for Phantom
[tmk_keyboard.git] / keyboard / phantom / matrix.c
1 /* Copyright 2012 Jun Wako <wakojun@gmail.com>
2 *
3 * This is heavily based on phantom/board.{c|h}.
4 * https://github.com/BathroomEpiphanies/AVR-Keyboard
5 *
6 * Copyright (c) 2012 Fredrik Atmer, Bathroom Epiphanies Inc
7 * http://bathroomepiphanies.com
8 *
9 * As for liscensing consult with the original files or its author.
10 */
11 #include <stdint.h>
12 #include <stdbool.h>
13 #include <avr/io.h>
14 #include <util/delay.h>
15 #include "print.h"
16 #include "debug.h"
17 #include "util.h"
18 #include "matrix.h"
19
20
21 #ifndef DEBOUNCE
22 # define DEBOUNCE 0
23 #endif
24 static uint8_t debouncing = DEBOUNCE;
25
26 // bit array of key state(1:on, 0:off)
27 static matrix_row_t matrix[MATRIX_ROWS];
28 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
29
30 static uint8_t read_rows(void);
31 static void init_rows(void);
32 static void unselect_cols(void);
33 static void select_col(uint8_t col);
34
35 /* LEDs are on output compare pins OC1B OC1C
36 This activates fast PWM mode on them.
37 Prescaler 256 and 8-bit counter results in
38 16000000/256/256 = 244 Hz blink frequency.
39 LED_A: Caps Lock
40 LED_B: Scroll Lock */
41 /* Output on PWM pins are turned off when the timer
42 reaches the value in the output compare register,
43 and are turned on when it reaches TOP (=256). */
44 static
45 void setup_leds(void)
46 {
47 TCCR1A |= // Timer control register 1A
48 (1<<WGM10) | // Fast PWM 8-bit
49 (1<<COM1B1)| // Clear OC1B on match, set at TOP
50 (1<<COM1C1); // Clear OC1C on match, set at TOP
51 TCCR1B |= // Timer control register 1B
52 (1<<WGM12) | // Fast PWM 8-bit
53 (1<<CS12); // Prescaler 256
54 OCR1B = 250; // Output compare register 1B
55 OCR1C = 250; // Output compare register 1C
56 // LEDs: LED_A -> PORTB6, LED_B -> PORTB7
57 DDRB &= 0x3F;
58 PORTB &= 0x3F;
59 }
60
61 inline
62 uint8_t matrix_rows(void)
63 {
64 return MATRIX_ROWS;
65 }
66
67 inline
68 uint8_t matrix_cols(void)
69 {
70 return MATRIX_COLS;
71 }
72
73 void matrix_init(void)
74 {
75 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
76 MCUCR |= (1<<JTD);
77 MCUCR |= (1<<JTD);
78
79 // initialize row and col
80 unselect_cols();
81 init_rows();
82 setup_leds();
83
84 // initialize matrix state: all keys off
85 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
86 matrix[i] = 0;
87 matrix_debouncing[i] = 0;
88 }
89 }
90
91 uint8_t matrix_scan(void)
92 {
93 for (uint8_t col = 0; col < MATRIX_COLS; col++) { // 0-16
94 select_col(col);
95 _delay_us(3); // without this wait it won't read stable value.
96 uint8_t rows = read_rows();
97 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { // 0-5
98 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
99 bool curr_bit = rows & (1<<row);
100 if (prev_bit != curr_bit) {
101 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
102 if (debouncing) {
103 dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
104 }
105 debouncing = DEBOUNCE;
106 }
107 }
108 unselect_cols();
109 }
110
111 if (debouncing) {
112 if (--debouncing) {
113 _delay_ms(1);
114 } else {
115 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
116 matrix[i] = matrix_debouncing[i];
117 }
118 }
119 }
120
121 return 1;
122 }
123
124 bool matrix_is_modified(void)
125 {
126 if (debouncing) return false;
127 return true;
128 }
129
130 inline
131 bool matrix_is_on(uint8_t row, uint8_t col)
132 {
133 return (matrix[row] & ((matrix_row_t)1<<col));
134 }
135
136 inline
137 matrix_row_t matrix_get_row(uint8_t row)
138 {
139 return matrix[row];
140 }
141
142 void matrix_print(void)
143 {
144 print("\nr/c 0123456789ABCDEF\n");
145 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
146 xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
147 }
148 }
149
150 uint8_t matrix_key_count(void)
151 {
152 uint8_t count = 0;
153 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
154 count += bitpop32(matrix[i]);
155 }
156 return count;
157 }
158
159 /* Row pin configuration
160 * row: 0 1 2 3 4 5
161 * pin: B5 B4 B3 B2 B1 B0
162 */
163 static void init_rows(void)
164 {
165 // Input with pull-up(DDR:0, PORT:1)
166 DDRB &= ~0b00111111;
167 PORTB |= 0b00111111;
168 }
169
170 static uint8_t read_rows(void)
171 {
172 return (PINB&(1<<5) ? 0 : (1<<0)) |
173 (PINB&(1<<4) ? 0 : (1<<1)) |
174 (PINB&(1<<3) ? 0 : (1<<2)) |
175 (PINB&(1<<2) ? 0 : (1<<3)) |
176 (PINB&(1<<1) ? 0 : (1<<4)) |
177 (PINB&(1<<0) ? 0 : (1<<5));
178 }
179
180 /* Column pin configuration
181 * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
182 * pin: D5 C7 C6 D4 D0 E6 F0 F1 F4 F5 F6 F7 D7 D6 D1 D2 D3
183 */
184 static void unselect_cols(void)
185 {
186 // Hi-Z(DDR:0, PORT:0) to unselect
187 DDRC |= 0b11000000; // PC: 7 6
188 PORTC |= 0b11000000;
189 DDRD |= 0b11111111; // PD: 7 6 5 4 3 2 1 0
190 PORTD |= 0b11111111;
191 DDRE |= 0b01000000; // PE: 6
192 PORTE |= 0b01000000;
193 DDRF |= 0b11110011; // PF: 7 6 5 4 1 0
194 PORTF |= 0b11110011;
195 }
196
197 static void select_col(uint8_t col)
198 {
199 // Output low(DDR:1, PORT:0) to select
200 switch (col) {
201 case 0:
202 DDRD |= (1<<5);
203 PORTD &= ~(1<<5);
204 break;
205 case 1:
206 DDRC |= (1<<7);
207 PORTC &= ~(1<<7);
208 break;
209 case 2:
210 DDRC |= (1<<6);
211 PORTC &= ~(1<<6);
212 break;
213 case 3:
214 DDRD |= (1<<4);
215 PORTD &= ~(1<<4);
216 break;
217 case 4:
218 DDRD |= (1<<0);
219 PORTD &= ~(1<<0);
220 break;
221 case 5:
222 DDRE |= (1<<6);
223 PORTE &= ~(1<<6);
224 break;
225 case 6:
226 DDRF |= (1<<0);
227 PORTF &= ~(1<<0);
228 break;
229 case 7:
230 DDRF |= (1<<1);
231 PORTF &= ~(1<<1);
232 break;
233 case 8:
234 DDRF |= (1<<4);
235 PORTF &= ~(1<<4);
236 break;
237 case 9:
238 DDRF |= (1<<5);
239 PORTF &= ~(1<<5);
240 break;
241 case 10:
242 DDRF |= (1<<6);
243 PORTF &= ~(1<<6);
244 break;
245 case 11:
246 DDRF |= (1<<7);
247 PORTF &= ~(1<<7);
248 break;
249 case 12:
250 DDRD |= (1<<7);
251 PORTD &= ~(1<<7);
252 break;
253 case 13:
254 DDRD |= (1<<6);
255 PORTD &= ~(1<<6);
256 break;
257 case 14:
258 DDRD |= (1<<1);
259 PORTD &= ~(1<<1);
260 break;
261 case 15:
262 DDRD |= (1<<2);
263 PORTD &= ~(1<<2);
264 break;
265 case 16:
266 DDRD |= (1<<3);
267 PORTD &= ~(1<<3);
268 break;
269 }
270 }
Imprint / Impressum