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