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