]> git.donarmstrong.com Git - tmk_firmware.git/blob - converter/adb_usb/matrix.c
18e71aa49786aca2c06ce4d848995b87675d867f
[tmk_firmware.git] / converter / adb_usb / 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 "util.h"
27 #include "debug.h"
28 #include "host.h"
29 #include "led.h"
30 #include "adb.h"
31 #include "matrix.h"
32
33
34 #if (MATRIX_COLS > 16)
35 #   error "MATRIX_COLS must not exceed 16"
36 #endif
37 #if (MATRIX_ROWS > 255)
38 #   error "MATRIX_ROWS must not exceed 255"
39 #endif
40
41 #define ADB_CAPS_UP     (ADB_CAPS | 0x80)
42
43
44 static bool is_modified = false;
45
46 // matrix state buffer(1:on, 0:off)
47 #if (MATRIX_COLS <= 8)
48 static uint8_t matrix[MATRIX_ROWS];
49 #else
50 static uint16_t matrix[MATRIX_ROWS];
51 #endif
52
53 #ifdef MATRIX_HAS_GHOST
54 static bool matrix_has_ghost_in_row(uint8_t row);
55 #endif
56 static void register_key(uint8_t key);
57
58
59 inline
60 uint8_t matrix_rows(void)
61 {
62     return MATRIX_ROWS;
63 }
64
65 inline
66 uint8_t matrix_cols(void)
67 {
68     return MATRIX_COLS;
69 }
70
71 void matrix_init(void)
72 {
73     adb_host_init();
74
75     // initialize matrix state: all keys off
76     for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
77
78     print_enable = true;
79     debug_enable = true;
80     debug_matrix = true;
81     debug_keyboard = true;
82     debug_mouse = true;
83     print("debug enabled.\n");
84     return;
85 }
86
87 uint8_t matrix_scan(void)
88 {
89     uint16_t codes;
90     uint8_t key0, key1;
91
92     is_modified = false;
93     codes = adb_host_kbd_recv();
94     key0 = codes>>8;
95     key1 = codes&0xFF;
96
97     if (debug_matrix && codes) {
98         print("adb_host_kbd_recv: "); phex16(codes); print("\n");
99     }
100
101 #ifdef MATRIX_HAS_LOCKING_CAPS
102     // Send Caps key up event
103     if (matrix_is_on(MATRIX_ROW(ADB_CAPS), MATRIX_COL(ADB_CAPS))) {
104         register_key(ADB_CAPS_UP);
105     }
106 #endif
107     if (codes == 0) {                           // no keys
108         return 0;
109     } else if (codes == 0x7F7F) {   // power key press
110         register_key(0x7F);
111     } else if (codes == 0xFFFF) {   // power key release
112         register_key(0xFF);
113     } else if (key0 == 0xFF) {      // error
114         if (debug_matrix) print("adb_host_kbd_recv: ERROR(matrix cleared.)\n");
115         // clear matrix to unregister all keys
116         for (uint8_t i=0; i < MATRIX_ROWS; i++) matrix[i] = 0x00;
117         return key1;
118     } else {
119 #ifdef MATRIX_HAS_LOCKING_CAPS    
120         if (host_keyboard_leds() & (1<<USB_LED_CAPS_LOCK)) {
121             // Ignore LockingCaps key down event when CAPS LOCK is on
122             if (key0 == ADB_CAPS && (key1 == ADB_CAPS || key1 == 0xFF)) return 0;
123             if (key0 == ADB_CAPS) key0 = key1;
124             if (key1 == ADB_CAPS) key1 = 0xFF;
125             // Convert LockingCaps key up event into down event
126             if (key0 == ADB_CAPS_UP) key0 = ADB_CAPS;
127             if (key1 == ADB_CAPS_UP) key1 = ADB_CAPS;
128         } else {
129             // ADB_CAPS LOCK off:
130             // Ignore LockingCaps key up event when ADB_CAPS LOCK is off
131             if (key0 == ADB_CAPS_UP && (key1 == ADB_CAPS_UP || key1 == 0xFF)) return 0;
132             if (key0 == ADB_CAPS_UP) key0 = key1;
133             if (key1 == ADB_CAPS_UP) key1 = 0xFF;
134         }
135 #endif        
136         register_key(key0);
137         if (key1 != 0xFF)       // key1 is 0xFF when no second key.
138             register_key(key1);
139     }
140
141     return 1;
142 }
143
144 bool matrix_is_modified(void)
145 {
146     return is_modified;
147 }
148
149 inline
150 bool matrix_has_ghost(void)
151 {
152 #ifdef MATRIX_HAS_GHOST
153     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
154         if (matrix_has_ghost_in_row(i))
155             return true;
156     }
157 #endif
158     return false;
159 }
160
161 inline
162 bool matrix_is_on(uint8_t row, uint8_t col)
163 {
164     return (matrix[row] & (1<<col));
165 }
166
167 inline
168 #if (MATRIX_COLS <= 8)
169 uint8_t matrix_get_row(uint8_t row)
170 #else
171 uint16_t matrix_get_row(uint8_t row)
172 #endif
173 {
174     return matrix[row];
175 }
176
177 void matrix_print(void)
178 {
179     if (!debug_matrix) return;
180 #if (MATRIX_COLS <= 8)
181     print("r/c 01234567\n");
182 #else
183     print("r/c 0123456789ABCDEF\n");
184 #endif
185     for (uint8_t row = 0; row < matrix_rows(); row++) {
186         phex(row); print(": ");
187 #if (MATRIX_COLS <= 8)
188         pbin_reverse(matrix_get_row(row));
189 #else
190         pbin_reverse16(matrix_get_row(row));
191 #endif
192 #ifdef MATRIX_HAS_GHOST
193         if (matrix_has_ghost_in_row(row)) {
194             print(" <ghost");
195         }
196 #endif
197         print("\n");
198     }
199 }
200
201 uint8_t matrix_key_count(void)
202 {
203     uint8_t count = 0;
204     for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
205 #if (MATRIX_COLS <= 8)
206         count += bitpop(matrix[i]);
207 #else
208         count += bitpop16(matrix[i]);
209 #endif
210     }
211     return count;
212 }
213
214 #ifdef MATRIX_HAS_GHOST
215 inline
216 static bool matrix_has_ghost_in_row(uint8_t row)
217 {
218     // no ghost exists in case less than 2 keys on
219     if (((matrix[row] - 1) & matrix[row]) == 0)
220         return false;
221
222     // ghost exists in case same state as other row
223     for (uint8_t i=0; i < MATRIX_ROWS; i++) {
224         if (i != row && (matrix[i] & matrix[row]) == matrix[row])
225             return true;
226     }
227     return false;
228 }
229 #endif
230
231 inline
232 static void register_key(uint8_t key)
233 {
234     uint8_t col, row;
235     col = key&0x07;
236     row = (key>>3)&0x0F;
237     if (key&0x80) {
238         matrix[row] &= ~(1<<col);
239     } else {
240         matrix[row] |=  (1<<col);
241     }
242     is_modified = true;
243 }