]> git.gir.st - tmk_keyboard.git/blob - keyboard/phantom/matrix.c
Code style fixes for Phantom matrix.
[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 debug("bounce!: "); debug_hex(debouncing); print("\n");
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 phex(row); print(": ");
147 print_bin_reverse32(matrix_get_row(row));
148 print("\n");
149 }
150 }
151
152 uint8_t matrix_key_count(void)
153 {
154 uint8_t count = 0;
155 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
156 count += bitpop32(matrix[i]);
157 }
158 return count;
159 }
160
161 /* Row pin configuration
162 * row: 0 1 2 3 4 5
163 * pin: B0 B1 B2 B3 B4 B5
164 */
165 static void init_rows(void)
166 {
167 // Input with pull-up(DDR:0, PORT:1)
168 DDRB &= ~0b00111111;
169 PORTB |= 0b00111111;
170 }
171
172 static uint8_t read_rows(void)
173 {
174 return (PINB&(1<<0) ? 0 : (1<<0)) |
175 (PINB&(1<<1) ? 0 : (1<<1)) |
176 (PINB&(1<<2) ? 0 : (1<<2)) |
177 (PINB&(1<<3) ? 0 : (1<<3)) |
178 (PINB&(1<<4) ? 0 : (1<<4)) |
179 (PINB&(1<<5) ? 0 : (1<<5));
180 }
181
182 /* Column pin configuration
183 * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
184 * pin: D5 C7 C6 D4 D0 E6 F0 F1 F4 F5 F6 F7 D7 D6 D1 D2 D3
185 */
186 static void unselect_cols(void)
187 {
188 // Hi-Z(DDR:0, PORT:0) to unselect
189 DDRC |= 0b11000000; // PC: 7 6
190 PORTC |= 0b11000000;
191 DDRD |= 0b11111111; // PD: 7 6 5 4 3 2 1 0
192 PORTD |= 0b11111111;
193 DDRE |= 0b01000000; // PE: 6
194 PORTE |= 0b01000000;
195 DDRF |= 0b11110011; // PF: 7 6 5 4 1 0
196 PORTF |= 0b11110011;
197 }
198
199 static void select_col(uint8_t col)
200 {
201 // Output low(DDR:1, PORT:0) to select
202 switch (col) {
203 case 0:
204 DDRD |= (1<<5);
205 PORTD &= ~(1<<5);
206 break;
207 case 1:
208 DDRC |= (1<<7);
209 PORTC &= ~(1<<7);
210 break;
211 case 2:
212 DDRC |= (1<<6);
213 PORTC &= ~(1<<6);
214 break;
215 case 3:
216 DDRD |= (1<<4);
217 PORTD &= ~(1<<4);
218 break;
219 case 4:
220 DDRD |= (1<<0);
221 PORTD &= ~(1<<0);
222 break;
223 case 5:
224 DDRE |= (1<<6);
225 PORTE &= ~(1<<6);
226 break;
227 case 6:
228 DDRF |= (1<<0);
229 PORTF &= ~(1<<0);
230 break;
231 case 7:
232 DDRF |= (1<<1);
233 PORTF &= ~(1<<1);
234 break;
235 case 8:
236 DDRF |= (1<<4);
237 PORTF &= ~(1<<4);
238 break;
239 case 9:
240 DDRF |= (1<<5);
241 PORTF &= ~(1<<5);
242 break;
243 case 10:
244 DDRF |= (1<<6);
245 PORTF &= ~(1<<6);
246 break;
247 case 11:
248 DDRF |= (1<<7);
249 PORTF &= ~(1<<7);
250 break;
251 case 12:
252 DDRD |= (1<<7);
253 PORTD &= ~(1<<7);
254 break;
255 case 13:
256 DDRD |= (1<<6);
257 PORTD &= ~(1<<6);
258 break;
259 case 14:
260 DDRD |= (1<<1);
261 PORTD &= ~(1<<1);
262 break;
263 case 15:
264 DDRD |= (1<<2);
265 PORTD &= ~(1<<2);
266 break;
267 case 16:
268 DDRD |= (1<<3);
269 PORTD &= ~(1<<3);
270 break;
271 }
272 }
Imprint / Impressum