]> git.gir.st - tmk_keyboard.git/blob - keyboard/phantom/matrix.c
Fix debouncing on 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
31 #define _DDRA (uint8_t *const)&DDRA
32 #define _DDRB (uint8_t *const)&DDRB
33 #define _DDRC (uint8_t *const)&DDRC
34 #define _DDRD (uint8_t *const)&DDRD
35 #define _DDRE (uint8_t *const)&DDRE
36 #define _DDRF (uint8_t *const)&DDRF
37
38 #define _PINA (uint8_t *const)&PINA
39 #define _PINB (uint8_t *const)&PINB
40 #define _PINC (uint8_t *const)&PINC
41 #define _PIND (uint8_t *const)&PIND
42 #define _PINE (uint8_t *const)&PINE
43 #define _PINF (uint8_t *const)&PINF
44
45 #define _PORTA (uint8_t *const)&PORTA
46 #define _PORTB (uint8_t *const)&PORTB
47 #define _PORTC (uint8_t *const)&PORTC
48 #define _PORTD (uint8_t *const)&PORTD
49 #define _PORTE (uint8_t *const)&PORTE
50 #define _PORTF (uint8_t *const)&PORTF
51
52 #define _BIT0 0x01
53 #define _BIT1 0x02
54 #define _BIT2 0x04
55 #define _BIT3 0x08
56 #define _BIT4 0x10
57 #define _BIT5 0x20
58 #define _BIT6 0x40
59 #define _BIT7 0x80
60
61 /* Specifies the ports and pin numbers for the rows */
62 static
63 uint8_t *const row_ddr[MATRIX_ROWS] = {_DDRB, _DDRB, _DDRB, _DDRB, _DDRB, _DDRB};
64
65 static
66 uint8_t *const row_port[MATRIX_ROWS] = {_PORTB, _PORTB, _PORTB, _PORTB, _PORTB, _PORTB};
67
68 static
69 uint8_t *const row_pin[MATRIX_ROWS] = {_PINB, _PINB, _PINB, _PINB, _PINB, _PINB};
70
71 static
72 const uint8_t row_bit[MATRIX_ROWS] = { _BIT0, _BIT1, _BIT2, _BIT3, _BIT4, _BIT5};
73
74 /* Specifies the ports and pin numbers for the columns */
75 static
76 uint8_t *const col_ddr[MATRIX_COLS] = { _DDRD, _DDRC, _DDRC, _DDRD, _DDRD, _DDRE,
77 _DDRF, _DDRF, _DDRF, _DDRF, _DDRF, _DDRF,
78 _DDRD, _DDRD, _DDRD, _DDRD, _DDRD};
79
80 static
81 uint8_t *const col_port[MATRIX_COLS] = {_PORTD, _PORTC, _PORTC, _PORTD, _PORTD, _PORTE,
82 _PORTF, _PORTF, _PORTF, _PORTF, _PORTF, _PORTF,
83 _PORTD, _PORTD, _PORTD, _PORTD, _PORTD};
84
85 static
86 const uint8_t col_bit[MATRIX_COLS] = { _BIT5, _BIT7, _BIT6, _BIT4, _BIT0, _BIT6,
87 _BIT0, _BIT1, _BIT4, _BIT5, _BIT6, _BIT7,
88 _BIT7, _BIT6, _BIT1, _BIT2, _BIT3};
89
90 static
91 inline void pull_column(int col) {
92 *col_port[col] &= ~col_bit[col];
93 }
94
95 static
96 inline void release_column(int col) {
97 *col_port[col] |= col_bit[col];
98 }
99
100 /* PORTB is set as input with pull-up resistors
101 PORTC,D,E,F are set to high output */
102
103 static
104 void setup_io_pins(void) {
105 uint8_t row, col;
106 for(row = 0; row < MATRIX_ROWS; row++) {
107 *row_ddr[row] &= ~row_bit[row];
108 *row_port[row] |= row_bit[row];
109 }
110 for(col = 0; col < MATRIX_COLS; col++) {
111 *col_ddr[col] |= col_bit[col];
112 *col_port[col] |= col_bit[col];
113 }
114 }
115
116 /* LEDs are on output compare pins OC1B OC1C
117 This activates fast PWM mode on them.
118 Prescaler 256 and 8-bit counter results in
119 16000000/256/256 = 244 Hz blink frequency.
120 LED_A: Caps Lock
121 LED_B: Scroll Lock */
122 /* Output on PWM pins are turned off when the timer
123 reaches the value in the output compare register,
124 and are turned on when it reaches TOP (=256). */
125 static
126 void setup_leds(void) {
127 TCCR1A |= // Timer control register 1A
128 (1<<WGM10) | // Fast PWM 8-bit
129 (1<<COM1B1)| // Clear OC1B on match, set at TOP
130 (1<<COM1C1); // Clear OC1C on match, set at TOP
131 TCCR1B |= // Timer control register 1B
132 (1<<WGM12) | // Fast PWM 8-bit
133 (1<<CS12); // Prescaler 256
134 OCR1B = 250; // Output compare register 1B
135 OCR1C = 250; // Output compare register 1C
136 // LEDs: LED_A -> PORTB6, LED_B -> PORTB7
137 DDRB &= 0x3F;
138 PORTB &= 0x3F;
139 }
140
141
142 inline
143 uint8_t matrix_rows(void)
144 {
145 return MATRIX_ROWS;
146 }
147
148 inline
149 uint8_t matrix_cols(void)
150 {
151 return MATRIX_COLS;
152 }
153
154 void matrix_init(void)
155 {
156 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
157 MCUCR |= (1<<JTD);
158 MCUCR |= (1<<JTD);
159
160 // initialize row and col
161 setup_io_pins();
162 setup_leds();
163
164 // initialize matrix state: all keys off
165 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
166 matrix[i] = 0;
167 matrix_debouncing[i] = 0;
168 }
169 }
170
171 uint8_t matrix_scan(void)
172 {
173 for (uint8_t col = 0; col < MATRIX_COLS; col++) { // 0-16
174 pull_column(col); // output hi on theline
175 _delay_us(3); // without this wait it won't read stable value.
176 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { // 0-5
177 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
178 bool curr_bit = !(*row_pin[row] & row_bit[row]);
179 if (prev_bit != curr_bit) {
180 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
181 if (debouncing) {
182 debug("bounce!: "); debug_hex(debouncing); print("\n");
183 }
184 debouncing = DEBOUNCE;
185 }
186 }
187 release_column(col);
188 }
189
190 if (debouncing) {
191 if (--debouncing) {
192 _delay_ms(1);
193 } else {
194 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
195 matrix[i] = matrix_debouncing[i];
196 }
197 }
198 }
199
200 return 1;
201 }
202
203 bool matrix_is_modified(void)
204 {
205 // NOTE: no longer used
206 return true;
207 }
208
209 inline
210 bool matrix_is_on(uint8_t row, uint8_t col)
211 {
212 return (matrix[row] & ((matrix_row_t)1<<col));
213 }
214
215 inline
216 matrix_row_t matrix_get_row(uint8_t row)
217 {
218 return matrix[row];
219 }
220
221 void matrix_print(void)
222 {
223 print("\nr/c 01234567\n");
224 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
225 phex(row); print(": ");
226 pbin_reverse(matrix_get_row(row));
227 print("\n");
228 }
229 }
230
231 uint8_t matrix_key_count(void)
232 {
233 uint8_t count = 0;
234 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
235 for (uint8_t j = 0; j < MATRIX_COLS; j++) {
236 if (matrix_is_on(i, j))
237 count++;
238 }
239 }
240 return count;
241 }
Imprint / Impressum