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