]> git.donarmstrong.com Git - tmk_firmware.git/blob - keyboard/macway/matrix.c
Merge branch 'debouncing_fix'
[tmk_firmware.git] / keyboard / macway / matrix.c
1 /*
2 Copyright 2011 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 #if (MATRIX_COLS > 16)
32 #   error "MATRIX_COLS must not exceed 16"
33 #endif
34 #if (MATRIX_ROWS > 255)
35 #   error "MATRIX_ROWS must not exceed 255"
36 #endif
37
38
39 #ifndef DEBOUNCE
40 #   define DEBOUNCE     5
41 #endif
42 static uint8_t debouncing = DEBOUNCE;
43
44 // matrix state buffer(1:on, 0:off)
45 static matrix_row_t *matrix;
46 static matrix_row_t *matrix_debouncing;
47 static matrix_row_t matrix0[MATRIX_ROWS];
48 static matrix_row_t matrix1[MATRIX_ROWS];
49
50 #ifdef MATRIX_HAS_GHOST
51 static bool matrix_has_ghost_in_row(uint8_t row);
52 #endif
53 static matrix_row_t read_col(void);
54 static void unselect_rows(void);
55 static void select_row(uint8_t row);
56
57
58 inline
59 uint8_t matrix_rows(void)
60 {
61     return MATRIX_ROWS;
62 }
63
64 inline
65 uint8_t matrix_cols(void)
66 {
67     return MATRIX_COLS;
68 }
69
70 void matrix_init(void)
71 {
72     // initialize row and col
73     unselect_rows();
74     // Input with pull-up(DDR:0, PORT:1)
75     DDRB = 0x00;
76     PORTB = 0xFF;
77
78     // initialize matrix state: all keys off
79     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
80         matrix0[i] = 0;
81         matrix1[i] = 0;
82     }
83     matrix = matrix0;
84     matrix_debouncing = matrix1;
85 }
86
87 uint8_t matrix_scan(void)
88 {
89     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
90         unselect_rows();
91         select_row(i);
92         _delay_us(30);  // without this wait read unstable value.
93         if (matrix[i] != read_col()) {
94             matrix[i] = read_col();
95             if (debouncing) {
96                 debug("bounce!: "); debug_hex(debouncing); print("\n");
97             }
98             debouncing = DEBOUNCE;
99         }
100     }
101     unselect_rows();
102
103     if (debouncing) {
104         if (--debouncing) {
105             _delay_ms(1);
106         } else {
107             matrix_row_t *tmp = matrix;
108             matrix = matrix_debouncing;
109             matrix_debouncing = tmp;
110         }
111
112     }
113
114     return 1;
115 }
116
117 bool matrix_is_modified(void)
118 {
119     if (debouncing) return false;
120     return true;
121 }
122
123 inline
124 bool matrix_is_on(uint8_t row, uint8_t col)
125 {
126     return (matrix[row] & (1<<col));
127 }
128
129 inline
130 matrix_row_t matrix_get_row(uint8_t row)
131 {
132     return matrix[row];
133 }
134
135 void matrix_print(void)
136 {
137     print("\nr/c 01234567\n");
138     for (uint8_t row = 0; row < matrix_rows(); row++) {
139         phex(row); print(": ");
140 #if (MATRIX_COLS <= 8)
141         pbin_reverse(matrix_get_row(row));
142 #else
143         pbin_reverse16(matrix_get_row(row));
144 #endif
145 #ifdef MATRIX_HAS_GHOST
146         if (matrix_has_ghost_in_row(row)) {
147             print(" <ghost");
148         }
149 #endif
150         print("\n");
151     }
152 }
153
154 uint8_t matrix_key_count(void)
155 {
156     uint8_t count = 0;
157     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
158 #if (MATRIX_COLS <= 8)
159         count += bitpop(matrix[i]);
160 #else
161         count += bitpop16(matrix[i]);
162 #endif
163     }
164     return count;
165 }
166
167 #ifdef MATRIX_HAS_GHOST
168 inline
169 static bool matrix_has_ghost_in_row(uint8_t row)
170 {
171     // no ghost exists in case less than 2 keys on
172     if (((matrix[row] - 1) & matrix[row]) == 0)
173         return false;
174
175     // ghost exists in case same state as other row
176     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
177         if (i != row && (matrix[i] & matrix[row]) == matrix[row])
178             return true;
179     }
180     return false;
181 }
182 #endif
183
184 inline
185 static matrix_row_t read_col(void)
186 {
187     return ~PINB;
188 }
189
190 inline
191 static void unselect_rows(void)
192 {
193     // Hi-Z(DDR:0, PORT:0) to unselect
194     DDRC  &= ~0b01000000; // PC: 6
195     PORTC &= ~0b01000000;
196     DDRD  &= ~0b11100111; // PD: 7,6,5,2,1,0
197     PORTD &= ~0b11100111;
198     DDRF  &= ~0b11000000; // PF: 7,6
199     PORTF &= ~0b11000000;
200 }
201
202 inline
203 static void select_row(uint8_t row)
204 {
205     // Output low(DDR:1, PORT:0) to select
206     // row: 0    1    2    3    4    5    6    7    8
207     // pin: PD0, PD5, PD7, PF6, PD6, PD1, PD2, PC6, PF7
208     switch (row) {
209         case 0:
210             DDRD  |= (1<<0);
211             PORTD &= ~(1<<0);
212             break;
213         case 1:
214             DDRD  |= (1<<5);
215             PORTD &= ~(1<<5);
216             break;
217         case 2:
218             DDRD  |= (1<<7);
219             PORTD &= ~(1<<7);
220             break;
221         case 3:
222             DDRF  |= (1<<6);
223             PORTF &= ~(1<<6);
224             break;
225         case 4:
226             DDRD  |= (1<<6);
227             PORTD &= ~(1<<6);
228             break;
229         case 5:
230             DDRD  |= (1<<1);
231             PORTD &= ~(1<<1);
232             break;
233         case 6:
234             DDRD  |= (1<<2);
235             PORTD &= ~(1<<2);
236             break;
237         case 7:
238             DDRC  |= (1<<6);
239             PORTC &= ~(1<<6);
240             break;
241         case 8:
242             DDRF  |= (1<<7);
243             PORTF &= ~(1<<7);
244             break;
245     }
246 }