]> git.gir.st - tmk_keyboard.git/blob - keyboard/nerd/matrix.c
Merge commit '20b787fc1284176834cbe7ca2134e4b36bec5828'
[tmk_keyboard.git] / keyboard / nerd / 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 "backlight.h"
27
28
29 #ifndef DEBOUNCE
30 # define DEBOUNCE 5
31 #endif
32 static uint8_t debouncing = DEBOUNCE;
33
34 /* matrix state(1:on, 0:off) */
35 static matrix_row_t matrix[MATRIX_ROWS];
36 static matrix_row_t matrix_debouncing[MATRIX_ROWS];
37
38 static uint16_t read_inputs(void);
39 static void init_inputs(void);
40 static void init_outputs(void);
41 static void reset_inputs(void);
42 static void reset_outputs(void);
43 static void select_output(uint8_t col);
44
45 inline
46 uint8_t matrix_rows(void)
47 {
48 return MATRIX_ROWS;
49 }
50
51 inline
52 uint8_t matrix_cols(void)
53 {
54 return MATRIX_COLS;
55 }
56
57 void matrix_init(void)
58 {
59 backlight_init_ports();
60 init_inputs();
61 init_outputs();
62
63 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
64 matrix[i] = 0;
65 matrix_debouncing[i] = 0;
66 }
67 }
68
69 uint8_t matrix_scan(void)
70 {
71 for (uint8_t col = 0; col < MATRIX_COLS; col++) {
72 reset_inputs();
73 reset_outputs();
74 select_output(col);
75 _delay_us(3);
76 uint16_t rows = read_inputs();
77 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
78 bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<<col);
79 bool curr_bit = rows & (1<<row);
80 if (prev_bit != curr_bit) {
81 matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
82 if (debouncing) {
83 dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
84 }
85 debouncing = DEBOUNCE;
86 }
87 }
88 }
89
90 if (debouncing) {
91 if (--debouncing) {
92 _delay_ms(1);
93 } else {
94 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
95 matrix[i] = matrix_debouncing[i];
96 }
97 }
98 }
99
100 return 1;
101 }
102
103 bool matrix_is_modified(void)
104 {
105 if (debouncing) return false;
106 return true;
107 }
108
109 inline
110 bool matrix_is_on(uint8_t row, uint8_t col)
111 {
112 return (matrix[row] & ((matrix_row_t)1<<col));
113 }
114
115 inline
116 matrix_row_t matrix_get_row(uint8_t row)
117 {
118 return matrix[row];
119 }
120
121 void matrix_print(void)
122 {
123 print("\nr/c 0123456789ABCDEF\n");
124 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
125 phex(row); print(": ");
126 pbin_reverse16(matrix_get_row(row));
127 print("\n");
128 }
129 }
130
131 uint8_t matrix_key_count(void)
132 {
133 uint8_t count = 0;
134 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
135 count += bitpop16(matrix[i]);
136 }
137 return count;
138 }
139
140 static void init_inputs(void)
141 {
142 DDRE &= ~0b01000000; // PE6 (Col 0)
143 DDRB &= ~0b00001111; // PB0 (Col 1), PB1 (Col 2), PB2 (Col 3), PB3 (Col 4)
144 DDRF &= ~0b00000001; // PF0 (Col 5)
145 DDRD &= ~0b00100011; // PD0 (Col 6), PD1 (Col 8 TKL), PD5 (Col 7)
146 }
147
148 static uint16_t read_inputs(void)
149 {
150 return (PINE&(1<<6) ? 0 : (1<<0)) | // PE6 (Col 0)
151 (PINB&(1<<0) ? 0 : (1<<1)) | // PB0 (Col 1)
152 (PINB&(1<<1) ? 0 : (1<<2)) | // PB1 (Col 2)
153 (PINB&(1<<2) ? 0 : (1<<3)) | // PB2 (Col 3)
154 (PINB&(1<<3) ? 0 : (1<<4)) | // PB3 (Col 4)
155 (PINF&(1<<0) ? 0 : (1<<5)) | // PF0 (Col 5)
156 (PIND&(1<<0) ? 0 : (1<<6)) | // PD0 (Col 6)
157 (PIND&(1<<5) ? 0 : (1<<7)) | // PD5 (Col 7)
158 (PIND&(1<<1) ? 0 : (1<<8)); // PD1 (Col 8 TKL)
159 }
160
161 static void reset_inputs(void)
162 {
163 PORTE |= 0b01000000; // PE6 (Col 0)
164 PORTB |= 0b00001111; // PB0 (Col 1), PB1 (Col 2), PB2 (Col 3), PB3 (Col 4)
165 PORTF |= 0b00000001; // PF0 (Col 5)
166 PORTD |= 0b00100011; // PD0 (Col 6), PD1 (Col 8 TKL), PD5 (Col 7)
167 }
168
169 static void init_outputs(void)
170 {
171 DDRB |= 0b00010000; // PB4 (Row 0)
172 DDRE |= 0b00000100; // PE2 (Row 1)
173 DDRF |= 0b11110010; // PF4 (Row 2), PF7 (Row 3), PF1 (Row 4), PF6 (Row 5), PF5 (Row 7)
174 DDRC |= 0b11000000; // PC6 (Row 6), PC7 (Row 9)
175 DDRD |= 0b10000000; // PD7 (Row 8)
176 }
177
178 static void reset_outputs(void)
179 {
180 PORTB |= 0b00010000; // PB4 (Row 0)
181 PORTE |= 0b00000100; // PE2 (Row 1)
182 PORTF |= 0b11110010; // PF4 (Row 2), PF7 (Row 3), PF1 (Row 4), PF6 (Row 5), PF5 (Row 7)
183 PORTC |= 0b11000000; // PC6 (Row 6), PC7 (Row 9)
184 PORTD |= 0b10000000; // PD7 (Row 8)
185 }
186
187 static void select_output(uint8_t col)
188 {
189 switch (col) {
190 case 0:
191 PORTB &= ~(1<<4);
192 break;
193 case 1:
194 PORTE &= ~(1<<2);
195 break;
196 case 2:
197 PORTF &= ~(1<<4);
198 break;
199 case 3:
200 PORTF &= ~(1<<7);
201 break;
202 case 4:
203 PORTF &= ~(1<<1);
204 break;
205 case 5:
206 PORTF &= ~(1<<6);
207 break;
208 case 6:
209 PORTC &= ~(1<<6);
210 break;
211 case 7:
212 PORTF &= ~(1<<5);
213 break;
214 case 8:
215 PORTD &= ~(1<<7);
216 break;
217 case 9:
218 PORTC &= ~(1<<7);
219 break;
220 }
221 }
Imprint / Impressum