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