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