2 Copyright 2016 Jun Wako <wakojun@gmail.com>
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.
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.
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/>.
41 * HID keycode(1 byte):
42 * Higher 5 bits indicates ROW and lower 3 bits COL.
49 * Matrix space(16 * 16):
51 * 0 +----------------+
54 * 16 +----------------+
58 #define CODE(row, col) (((row) << 4) | (col))
59 #define ROW(code) (((code) & ROW_MASK) >> 4)
60 #define COL(code) ((code) & COL_MASK)
61 #define ROW_BITS(code) (1 << COL(code))
64 // Integrated key state of all keyboards
65 static report_keyboard_t keyboard_report;
67 static bool matrix_is_mod =false;
70 * USB Host Shield HID keyboards
71 * This supports two cascaded hubs and four keyboards
74 USBHub hub1(&usb_host);
75 USBHub hub2(&usb_host);
76 HIDBoot<USB_HID_PROTOCOL_KEYBOARD> kbd1(&usb_host);
77 HIDBoot<USB_HID_PROTOCOL_KEYBOARD> kbd2(&usb_host);
78 HIDBoot<USB_HID_PROTOCOL_KEYBOARD> kbd3(&usb_host);
79 HIDBoot<USB_HID_PROTOCOL_KEYBOARD> kbd4(&usb_host);
80 KBDReportParser kbd_parser1;
81 KBDReportParser kbd_parser2;
82 KBDReportParser kbd_parser3;
83 KBDReportParser kbd_parser4;
86 uint8_t matrix_rows(void) { return MATRIX_ROWS; }
87 uint8_t matrix_cols(void) { return MATRIX_COLS; }
88 bool matrix_has_ghost(void) { return false; }
89 void matrix_init(void) {
91 // USB Host Shield setup
93 kbd1.SetReportParser(0, (HIDReportParser*)&kbd_parser1);
94 kbd2.SetReportParser(0, (HIDReportParser*)&kbd_parser2);
95 kbd3.SetReportParser(0, (HIDReportParser*)&kbd_parser3);
96 kbd4.SetReportParser(0, (HIDReportParser*)&kbd_parser4);
99 static void or_report(report_keyboard_t report) {
100 // integrate reports into keyboard_report
101 keyboard_report.mods |= report.mods;
102 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
103 if (IS_ANY(report.keys[i])) {
104 for (uint8_t j = 0; j < KEYBOARD_REPORT_KEYS; j++) {
105 if (! keyboard_report.keys[j]) {
106 keyboard_report.keys[j] = report.keys[i];
114 uint8_t matrix_scan(void) {
115 static uint16_t last_time_stamp1 = 0;
116 static uint16_t last_time_stamp2 = 0;
117 static uint16_t last_time_stamp3 = 0;
118 static uint16_t last_time_stamp4 = 0;
120 // check report came from keyboards
121 if (kbd_parser1.time_stamp != last_time_stamp1 ||
122 kbd_parser2.time_stamp != last_time_stamp2 ||
123 kbd_parser3.time_stamp != last_time_stamp3 ||
124 kbd_parser4.time_stamp != last_time_stamp4) {
126 last_time_stamp1 = kbd_parser1.time_stamp;
127 last_time_stamp2 = kbd_parser2.time_stamp;
128 last_time_stamp3 = kbd_parser3.time_stamp;
129 last_time_stamp4 = kbd_parser4.time_stamp;
131 // clear and integrate all reports
132 keyboard_report = {};
133 or_report(kbd_parser1.report);
134 or_report(kbd_parser2.report);
135 or_report(kbd_parser3.report);
136 or_report(kbd_parser4.report);
138 matrix_is_mod = true;
140 dprintf("state: %02X %02X", keyboard_report.mods, keyboard_report.reserved);
141 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
142 dprintf(" %02X", keyboard_report.keys[i]);
146 matrix_is_mod = false;
150 timer = timer_read();
152 timer = timer_elapsed(timer);
154 dprintf("host.Task: %d\n", timer);
157 static uint8_t usb_state = 0;
158 if (usb_state != usb_host.getUsbTaskState()) {
159 usb_state = usb_host.getUsbTaskState();
160 dprintf("usb_state: %02X\n", usb_state);
162 // restore LED state when keyboard comes up
163 if (usb_state == USB_STATE_RUNNING) {
164 dprintf("speed: %s\n", usb_host.getVbusState()==FSHOST ? "full" : "low");
165 keyboard_set_leds(host_keyboard_leds());
171 bool matrix_is_modified(void) {
172 return matrix_is_mod;
175 bool matrix_is_on(uint8_t row, uint8_t col) {
176 uint8_t code = CODE(row, col);
179 if (keyboard_report.mods & ROW_BITS(code)) {
183 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
184 if (keyboard_report.keys[i] == code) {
191 matrix_row_t matrix_get_row(uint8_t row) {
192 uint16_t row_bits = 0;
194 if (IS_MOD(CODE(row, 0)) && keyboard_report.mods) {
195 row_bits |= keyboard_report.mods;
198 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
199 if (IS_ANY(keyboard_report.keys[i])) {
200 if (row == ROW(keyboard_report.keys[i])) {
201 row_bits |= ROW_BITS(keyboard_report.keys[i]);
208 uint8_t matrix_key_count(void) {
211 count += bitpop(keyboard_report.mods);
212 for (uint8_t i = 0; i < KEYBOARD_REPORT_KEYS; i++) {
213 if (IS_ANY(keyboard_report.keys[i])) {
220 void matrix_print(void) {
221 print("\nr/c 0123456789ABCDEF\n");
222 for (uint8_t row = 0; row < matrix_rows(); row++) {
223 xprintf("%02d: ", row);
224 print_bin_reverse16(matrix_get_row(row));
229 void led_set(uint8_t usb_led)
231 if (kbd1.isReady()) kbd1.SetReport(0, 0, 2, 0, 1, &usb_led);
232 if (kbd2.isReady()) kbd2.SetReport(0, 0, 2, 0, 1, &usb_led);
233 if (kbd3.isReady()) kbd3.SetReport(0, 0, 2, 0, 1, &usb_led);
234 if (kbd4.isReady()) kbd4.SetReport(0, 0, 2, 0, 1, &usb_led);