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