]> git.gir.st - tmk_keyboard.git/blob - keyboard/gh60/matrix.c
Fix debouncing code.(gh60)
[tmk_keyboard.git] / keyboard / gh60 / matrix.c
1 /*
2 Copyright 2012 Jun Wako <wakojun@gmail.com>
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 /*
19 * scan matrix
20 */
21 #include <stdint.h>
22 #include <stdbool.h>
23 #include <avr/io.h>
24 #include <util/delay.h>
25 #include "print.h"
26 #include "debug.h"
27 #include "util.h"
28 #include "matrix.h"
29
30
31 #ifndef DEBOUNCE
32 # define DEBOUNCE 5
33 #endif
34 static uint8_t debouncing = DEBOUNCE;
35
36 /* matrix state(1:on, 0:off) */
37 static uint16_t *matrix;
38 static uint16_t *matrix_debouncing;
39 static uint16_t matrix0[MATRIX_ROWS];
40 static uint16_t matrix1[MATRIX_ROWS];
41 static bool is_modified;
42
43 static uint16_t read_cols(void);
44 static void init_cols(void);
45 static void unselect_rows(void);
46 static void select_row(uint8_t row);
47
48
49 inline
50 uint8_t matrix_rows(void)
51 {
52 return MATRIX_ROWS;
53 }
54
55 inline
56 uint8_t matrix_cols(void)
57 {
58 return MATRIX_COLS;
59 }
60
61 void matrix_init(void)
62 {
63 // initialize row and col
64 unselect_rows();
65 init_cols();
66
67 // initialize matrix state: all keys off
68 matrix = matrix0;
69 matrix_debouncing = matrix1;
70 for (uint8_t i=0; i < MATRIX_ROWS; i++) {
71 matrix[i] = 0;
72 matrix_debouncing[i] = 0;
73 }
74 is_modified = false;
75 }
76
77 uint8_t matrix_scan(void)
78 {
79 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
80 //unselect_rows();
81 select_row(i);
82 _delay_us(30); // without this wait read unstable value.
83 uint16_t cols = read_cols();
84 if (matrix_debouncing[i] != cols) {
85 matrix_debouncing[i] = cols;
86 if (debouncing) {
87 debug("bounce!: "); debug_hex(debouncing); debug("\n");
88 }
89 debouncing = DEBOUNCE;
90 is_modified = false;
91 }
92 unselect_rows();
93 }
94 //unselect_rows();
95
96 if (debouncing) {
97 debouncing--;
98 _delay_ms(1);
99 } else {
100 uint16_t *tmp = matrix;
101 matrix = matrix_debouncing;
102 matrix_debouncing = tmp;
103 is_modified = true;
104 }
105
106 return 1;
107 }
108
109 bool matrix_is_modified(void)
110 {
111 return is_modified;
112 }
113
114 inline
115 bool matrix_has_ghost(void)
116 {
117 return false;
118 }
119
120 inline
121 bool matrix_is_on(uint8_t row, uint8_t col)
122 {
123 return (matrix[row] & (1<<col));
124 }
125
126 inline
127 uint16_t matrix_get_row(uint8_t row)
128 {
129 return matrix[row];
130 }
131
132 void matrix_print(void)
133 {
134 print("\nr/c 01234567890ABCDEF\n");
135 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
136 phex(row); print(": ");
137 pbin_reverse16(matrix_get_row(row));
138 print("\n");
139 }
140 }
141
142 uint8_t matrix_key_count(void)
143 {
144 uint8_t count = 0;
145 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
146 count += bitpop16(matrix[i]);
147 }
148 return count;
149 }
150
151 /* Column pin configuration
152 * col: 0 1 2 3 4 5 6 7 8 9 10 11 12 13
153 * pin: F0 F1 E6 C7 C6 B6 D4 B1 B0 B5 B4 D7 D6 B3
154 */
155 static void init_cols(void)
156 {
157 // Input with pull-up(DDR:0, PORT:1)
158 DDRF &= ~(1<<0 | 1<<1);
159 PORTF |= (1<<0 | 1<<1);
160 DDRE &= ~(1<<6);
161 PORTE |= (1<<6);
162 DDRD &= ~(1<<7 | 1<<6 | 1<<4);
163 PORTD |= (1<<7 | 1<<6 | 1<<4);
164 DDRC &= ~(1<<7 | 1<<6);
165 PORTC |= (1<<7 | 1<<6);
166 DDRB &= ~(1<<6 | 1<< 5 | 1<<4 | 1<<3 | 1<<1 | 1<<0);
167 PORTB |= (1<<6 | 1<< 5 | 1<<4 | 1<<3 | 1<<1 | 1<<0);
168 }
169
170 static uint16_t read_cols(void)
171 {
172 return (PINF&(1<<0) ? 0 : (1<<0)) |
173 (PINF&(1<<1) ? 0 : (1<<1)) |
174 (PINE&(1<<6) ? 0 : (1<<2)) |
175 (PINC&(1<<7) ? 0 : (1<<3)) |
176 (PINC&(1<<6) ? 0 : (1<<4)) |
177 (PINB&(1<<6) ? 0 : (1<<5)) |
178 (PIND&(1<<4) ? 0 : (1<<6)) |
179 (PINB&(1<<1) ? 0 : (1<<7)) |
180 (PINB&(1<<0) ? 0 : (1<<8)) |
181 (PINB&(1<<5) ? 0 : (1<<9)) |
182 (PINB&(1<<4) ? 0 : (1<<10)) |
183 (PIND&(1<<7) ? 0 : (1<<11)) |
184 (PIND&(1<<6) ? 0 : (1<<12)) |
185 (PINB&(1<<3) ? 0 : (1<<13));
186 }
187
188 /* Row pin configuration
189 * row: 0 1 2 3 4
190 * pin: D0 D1 D2 D3 D5
191 */
192 static void unselect_rows(void)
193 {
194 // Hi-Z(DDR:0, PORT:0) to unselect
195 DDRD &= ~0b00101111;
196 PORTD &= ~0b00101111;
197 }
198
199 static void select_row(uint8_t row)
200 {
201 // Output low(DDR:1, PORT:0) to select
202 switch (row) {
203 case 0:
204 DDRD |= (1<<0);
205 PORTD &= ~(1<<0);
206 break;
207 case 1:
208 DDRD |= (1<<1);
209 PORTD &= ~(1<<1);
210 break;
211 case 2:
212 DDRD |= (1<<2);
213 PORTD &= ~(1<<2);
214 break;
215 case 3:
216 DDRD |= (1<<3);
217 PORTD &= ~(1<<3);
218 break;
219 case 4:
220 DDRD |= (1<<5);
221 PORTD &= ~(1<<5);
222 break;
223 }
224 }
Imprint / Impressum