]> git.gir.st - tmk_keyboard.git/blob - converter/usb_usb/usb_usb.cpp
core: Debug print for system and consumer keys
[tmk_keyboard.git] / converter / usb_usb / usb_usb.cpp
1 /*
2 Copyright 2016 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
21 // USB HID host
22 #include "Usb.h"
23 #include "usbhub.h"
24 #include "hid.h"
25 #include "hidboot.h"
26 #include "parser.h"
27
28 #include "keycode.h"
29 #include "util.h"
30 #include "print.h"
31 #include "debug.h"
32 #include "timer.h"
33 #include "matrix.h"
34 #include "led.h"
35
36
37 /* KEY CODE to Matrix
38 *
39 * HID keycode(1 byte):
40 * Higher 5 bits indicates ROW and lower 3 bits COL.
41 *
42 * 7 6 5 4 3 2 1 0
43 * +---------------+
44 * | ROW | COL |
45 * +---------------+
46 *
47 * Matrix space(16 * 16):
48 * r\c0123456789ABCDEF
49 * 0 +----------------+
50 * : | |
51 * : | |
52 * 16 +----------------+
53 */
54 #define ROW_MASK 0xF0
55 #define COL_MASK 0x0F
56 #define CODE(row, col) (((row) << 4) | (col))
57 #define ROW(code) (((code) & ROW_MASK) >> 4)
58 #define COL(code) ((code) & COL_MASK)
59 #define ROW_BITS(code) (1 << COL(code))
60
61
62 // Integrated key state of all keyboards
63 static report_keyboard_t keyboard_report;
64
65 static bool matrix_is_mod =false;
66
67 /*
68 * USB Host Shield HID keyboards
69 * This supports two cascaded hubs and four keyboards
70 */
71 USB usb_host;
72 USBHub hub1(&usb_host);
73 USBHub hub2(&usb_host);
74 HIDBoot<HID_PROTOCOL_KEYBOARD> kbd1(&usb_host);
75 HIDBoot<HID_PROTOCOL_KEYBOARD> kbd2(&usb_host);
76 HIDBoot<HID_PROTOCOL_KEYBOARD> kbd3(&usb_host);
77 HIDBoot<HID_PROTOCOL_KEYBOARD> kbd4(&usb_host);
78 KBDReportParser kbd_parser1;
79 KBDReportParser kbd_parser2;
80 KBDReportParser kbd_parser3;
81 KBDReportParser kbd_parser4;
82
83
84 uint8_t matrix_rows(void) { return MATRIX_ROWS; }
85 uint8_t matrix_cols(void) { return MATRIX_COLS; }
86 bool matrix_has_ghost(void) { return false; }
87 void matrix_init(void) {
88 // USB Host Shield setup
89 usb_host.Init();
90 kbd1.SetReportParser(0, (HIDReportParser*)&kbd_parser1);
91 kbd2.SetReportParser(0, (HIDReportParser*)&kbd_parser2);
92 kbd3.SetReportParser(0, (HIDReportParser*)&kbd_parser3);
93 kbd4.SetReportParser(0, (HIDReportParser*)&kbd_parser4);
94 }
95
96 static void or_report(report_keyboard_t report) {
97 // integrate reports into keyboard_report
98 keyboard_report.mods |= report.mods;
99 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
100 if (IS_ANY(report.keys[i])) {
101 for (uint8_t j = 0; j < KEYBOARD_REPORT_KEYS; j++) {
102 if (! keyboard_report.keys[j]) {
103 keyboard_report.keys[j] = report.keys[i];
104 break;
105 }
106 }
107 }
108 }
109 }
110
111 uint8_t matrix_scan(void) {
112 static uint16_t last_time_stamp1 = 0;
113 static uint16_t last_time_stamp2 = 0;
114 static uint16_t last_time_stamp3 = 0;
115 static uint16_t last_time_stamp4 = 0;
116
117 // check report came from keyboards
118 if (kbd_parser1.time_stamp != last_time_stamp1 ||
119 kbd_parser2.time_stamp != last_time_stamp2 ||
120 kbd_parser3.time_stamp != last_time_stamp3 ||
121 kbd_parser4.time_stamp != last_time_stamp4) {
122
123 last_time_stamp1 = kbd_parser1.time_stamp;
124 last_time_stamp2 = kbd_parser2.time_stamp;
125 last_time_stamp3 = kbd_parser3.time_stamp;
126 last_time_stamp4 = kbd_parser4.time_stamp;
127
128 // clear and integrate all reports
129 keyboard_report = {};
130 or_report(kbd_parser1.report);
131 or_report(kbd_parser2.report);
132 or_report(kbd_parser3.report);
133 or_report(kbd_parser4.report);
134
135 matrix_is_mod = true;
136
137 dprintf("state: %02X %02X", keyboard_report.mods, keyboard_report.reserved);
138 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
139 dprintf(" %02X", keyboard_report.keys[i]);
140 }
141 dprint("\r\n");
142 } else {
143 matrix_is_mod = false;
144 }
145
146 uint16_t timer;
147 timer = timer_read();
148 usb_host.Task();
149 timer = timer_elapsed(timer);
150 if (timer > 100) {
151 dprintf("host.Task: %d\n", timer);
152 }
153
154 return 1;
155 }
156
157 bool matrix_is_modified(void) {
158 return matrix_is_mod;
159 }
160
161 bool matrix_is_on(uint8_t row, uint8_t col) {
162 uint8_t code = CODE(row, col);
163
164 if (IS_MOD(code)) {
165 if (keyboard_report.mods & ROW_BITS(code)) {
166 return true;
167 }
168 }
169 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
170 if (keyboard_report.keys[i] == code) {
171 return true;
172 }
173 }
174 return false;
175 }
176
177 matrix_row_t matrix_get_row(uint8_t row) {
178 uint16_t row_bits = 0;
179
180 if (IS_MOD(CODE(row, 0)) && keyboard_report.mods) {
181 row_bits |= keyboard_report.mods;
182 }
183
184 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
185 if (IS_ANY(keyboard_report.keys[i])) {
186 if (row == ROW(keyboard_report.keys[i])) {
187 row_bits |= ROW_BITS(keyboard_report.keys[i]);
188 }
189 }
190 }
191 return row_bits;
192 }
193
194 uint8_t matrix_key_count(void) {
195 uint8_t count = 0;
196
197 count += bitpop(keyboard_report.mods);
198 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
199 if (IS_ANY(keyboard_report.keys[i])) {
200 count++;
201 }
202 }
203 return count;
204 }
205
206 void matrix_print(void) {
207 print("\nr/c 0123456789ABCDEF\n");
208 for (uint8_t row = 0; row < matrix_rows(); row++) {
209 xprintf("%02d: ", row);
210 print_bin_reverse16(matrix_get_row(row));
211 print("\n");
212 }
213 }
214
215 void led_set(uint8_t usb_led)
216 {
217 kbd1.SetReport(0, 0, 2, 0, 1, &usb_led);
218 kbd2.SetReport(0, 0, 2, 0, 1, &usb_led);
219 kbd3.SetReport(0, 0, 2, 0, 1, &usb_led);
220 kbd4.SetReport(0, 0, 2, 0, 1, &usb_led);
221 }
Imprint / Impressum