]> git.gir.st - tmk_keyboard.git/blob - keyboard/lightpad/matrix.c
Merge commit '20b787fc1284176834cbe7ca2134e4b36bec5828'
[tmk_keyboard.git] / keyboard / lightpad / matrix.c
1 /*
2 Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>
3
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <stdint.h>
19 #include <stdbool.h>
20 #include <avr/io.h>
21 #include <util/delay.h>
22 #include "print.h"
23 #include "debug.h"
24 #include "util.h"
25 #include "matrix.h"
26 #include "eeconfig.h"
27 #include "action_layer.h"
28 #include "backlight.h"
29
30 #ifndef DEBOUNCE
31 # define DEBOUNCE 0
32 #endif
33 static uint8_t debouncing = DEBOUNCE;
34
35 static matrix_row_t matrix[MATRIX_ROWS];
36 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
37
38 static uint8_t read_rows(void);
39 static uint8_t read_fwkey(void);
40 static void init_rows(void);
41 static void unselect_cols(void);
42 static void select_col(uint8_t col);
43
44 inline
45 uint8_t matrix_rows(void)
46 {
47 return MATRIX_ROWS;
48 }
49
50 inline
51 uint8_t matrix_cols(void)
52 {
53 return MATRIX_COLS;
54 }
55
56 void misc_init(void) {
57 }
58
59 void matrix_init(void)
60 {
61 backlight_init_ports();
62 unselect_cols();
63 init_rows();
64 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
65 matrix[i] = 0;
66 matrix_debouncing[i] = 0;
67 }
68 }
69
70 uint8_t matrix_scan(void)
71 {
72 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
73 select_col(col);
74 _delay_us(3);
75 uint8_t rows = read_rows();
76 // Use the otherwise unused col: 0 row: 0 for firmware key
77 if(col == 0) {
78 rows |= read_fwkey();
79 }
80 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
81 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
82 bool curr_bit = rows & (1<<row);
83 if (prev_bit != curr_bit) {
84 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
85 if (debouncing) {
86 dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
87 }
88 debouncing = DEBOUNCE;
89 }
90 }
91 unselect_cols();
92 }
93
94 if (debouncing) {
95 if (--debouncing) {
96 _delay_ms(1);
97 } else {
98 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
99 matrix[i] = matrix_debouncing[i];
100 }
101 }
102 }
103
104 return 1;
105 }
106
107 bool matrix_is_modified(void)
108 {
109 if (debouncing) return false;
110 return true;
111 }
112
113 inline
114 bool matrix_is_on(uint8_t row, uint8_t col)
115 {
116 return (matrix[row] & ((matrix_row_t)1<<col));
117 }
118
119 inline
120 matrix_row_t matrix_get_row(uint8_t row)
121 {
122 return matrix[row];
123 }
124
125 void matrix_print(void)
126 {
127 print("\nr/c 0123456789ABCDEF\n");
128 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
129 xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
130 }
131 }
132
133 uint8_t matrix_key_count(void)
134 {
135 uint8_t count = 0;
136 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
137 count += bitpop32(matrix[i]);
138 }
139 return count;
140 }
141
142 /* Row configuration
143 *
144 * row: 0 1 2 3 4 5
145 * pin: PD0 PD1 PD2 PD3 PD5 PB7
146 *
147 * Firmware uses pin PE2
148 */
149 static void init_rows(void)
150 {
151 DDRD &= ~0b00101111;
152 PORTD |= 0b00101111;
153
154 DDRB &= ~0b10000000;
155 PORTB |= 0b10000000;
156
157 DDRE &= ~0b00000100;
158 PORTE |= 0b00000100;
159 }
160
161 static uint8_t read_rows(void)
162 {
163 return (PIND&(1<<0) ? (1<<0) : 0) |
164 (PIND&(1<<1) ? (1<<1) : 0) |
165 (PIND&(1<<2) ? (1<<2) : 0) |
166 (PIND&(1<<3) ? (1<<3) : 0) |
167 (PIND&(1<<5) ? (1<<4) : 0) |
168 (PINB&(1<<7) ? (1<<5) : 0);
169 }
170
171 static uint8_t read_fwkey(void)
172 {
173 return PINE&(1<<2) ? 0 : (1<<0);
174 }
175
176 /* Column configuration
177 *
178 * col: 0 1 2 3
179 * pin: PF0 PF1 PC7 PC6
180 */
181 static void unselect_cols(void)
182 {
183 DDRF |= 0b00000011;
184 PORTF &= ~0b00000011;
185 DDRC |= 0b11000000;
186 PORTC &= ~0b11000000;
187 }
188
189 static void select_col(uint8_t col)
190 {
191 switch (col) {
192 case 0:
193 PORTF |= (1<<0);
194 break;
195 case 1:
196 PORTF |= (1<<1);
197 break;
198 case 2:
199 PORTC |= (1<<7);
200 break;
201 case 3:
202 PORTC |= (1<<6);
203 break;
204 }
205 }
Imprint / Impressum