-
Notifications
You must be signed in to change notification settings - Fork 42
/
ps2x2pico.c
247 lines (218 loc) · 7.94 KB
/
ps2x2pico.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
* The MIT License (MIT)
*
* Copyright (c) 2024 No0ne (https://github.com/No0ne)
* (c) 2023 Dustin Hoffman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "hardware/watchdog.h"
#include "hardware/gpio.h"
#include "bsp/board_api.h"
#include "tusb.h"
#include "ps2x2pico.h"
static void print_utf16(uint16_t *temp_buf, size_t buf_len);
void print_device_descriptor(tuh_xfer_t* xfer);
u8 kb_addr = 0;
u8 kb_inst = 0;
u8 kb_leds = 0;
char device_str[50];
char manufacturer_str[50];
void tuh_kb_set_leds(u8 leds) {
if(kb_addr) {
kb_leds = leds;
printf("HID(%d,%d): LEDs = %d\n", kb_addr, kb_inst, kb_leds);
tuh_hid_set_report(kb_addr, kb_inst, 0, HID_REPORT_TYPE_OUTPUT, &kb_leds, sizeof(kb_leds));
}
}
#define LANGUAGE_ID 0x0409 // English
void tuh_hid_mount_cb(u8 dev_addr, u8 instance, u8 const* desc_report, u16 desc_len) {
// This happens if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE.
// Consider increasing #define CFG_TUH_ENUMERATION_BUFSIZE 256 in tusb_config.h
if (desc_report == NULL && desc_len == 0) {
printf("WARNING: HID(%d,%d) skipped!\n",dev_addr, instance);
return;
}
hid_interface_protocol_enum_t hid_if_proto = tuh_hid_interface_protocol(dev_addr, instance);
uint16_t vid, pid;
tuh_vid_pid_get(dev_addr, &vid, &pid);
char* hidprotostr;
switch (hid_if_proto) {
case HID_ITF_PROTOCOL_NONE:
hidprotostr = "NONE";
break;
case HID_ITF_PROTOCOL_KEYBOARD:
hidprotostr = "KEYBOARD";
break;
case HID_ITF_PROTOCOL_MOUSE:
hidprotostr = "MOUSE";
//tuh_hid_set_protocol(dev_addr, instance, HID_PROTOCOL_REPORT);
break;
default:
hidprotostr = "UNKNOWN";
break;
};
printf("HID(%d,%d,%s) mounted\n", dev_addr, instance, hidprotostr);
printf(" ID: %04x:%04x\n", vid, pid);
uint16_t temp_buf[128];
printf(" Manufacturer: ");
if (XFER_RESULT_SUCCESS == tuh_descriptor_get_manufacturer_string_sync(dev_addr, LANGUAGE_ID, temp_buf, sizeof(temp_buf)) )
{
print_utf16(temp_buf, TU_ARRAY_SIZE(temp_buf));
}
printf("\n");
printf(" Product: ");
if (XFER_RESULT_SUCCESS == tuh_descriptor_get_product_string_sync(dev_addr, LANGUAGE_ID, temp_buf, sizeof(temp_buf)))
{
print_utf16(temp_buf, TU_ARRAY_SIZE(temp_buf));
}
printf("\n\n");
if (hid_if_proto == HID_ITF_PROTOCOL_KEYBOARD || hid_if_proto == HID_ITF_PROTOCOL_MOUSE) {
if (!tuh_hid_receive_report(dev_addr, instance)) {
printf("ERROR: Could not register for HID(%d,%d,%s)!\n", dev_addr, instance, hidprotostr);
} else {
printf("HID(%d,%d,%s) registered for reports\n", dev_addr, instance, hidprotostr);
if (hid_if_proto == HID_ITF_PROTOCOL_KEYBOARD) {
// TODO: This needs to be addressed if we want to have multiple connected kbds working correctly!
// Only relevant for KB LEDS though.
// Could be a list of all connected kbds, so we could set the LEDs on each.
kb_addr = dev_addr;
kb_inst = instance;
}
board_led_write(1);
}
}
}
void tuh_hid_umount_cb(u8 dev_addr, u8 instance) {
printf("HID(%d,%d) unmounted\n", dev_addr, instance);
board_led_write(0);
if(dev_addr == kb_addr && instance == kb_inst) {
kb_addr = 0;
kb_inst = 0;
}
//tuh_deinit(TUH_OPT_RHPORT);
//printf("deinit(%d)\n", TUH_OPT_RHPORT);
//tusb_init();
//printf("init()\n");
}
void tuh_hid_report_received_cb(u8 dev_addr, u8 instance, u8 const* report, u16 len) {
switch(tuh_hid_interface_protocol(dev_addr, instance)) {
case HID_ITF_PROTOCOL_KEYBOARD:
#ifdef TRACE
printf("HID_KB(%d,%d): r[2..7]={0x%x,0x%x,0x%x,0x%x,0x%x,0x%x},r[0]=0x%x,l=%d\n",
dev_addr, instance,
report[2], report[3], report[4], report[5], report[6], report[7],
report[0], len);
#else
#ifdef KB_DEBUG
printf("HID_KB(%d,%d): r[2]=0x%x,r[0]=0x%x,l=%d\n", dev_addr, instance, report[2], report[0], len);
#endif
#endif
kb_usb_receive(report, len);
tuh_hid_receive_report(dev_addr, instance);
break;
case HID_ITF_PROTOCOL_MOUSE:
#ifdef TRACE
printf("HID_MS(%d,%d): r[2..7]={0x%x,0x%x,0x%x,0x%x,0x%x,0x%x},r[0]=0x%x,l=%d\n",
dev_addr, instance,
report[2], report[3], report[4], report[5], report[6], report[7],
report[0], len);
#else
#ifdef MS_DEBUG
printf("HID_MS(%d,%d)\n", dev_addr, instance);
#endif
#endif
ms_usb_receive(report);
tuh_hid_receive_report(dev_addr, instance);
break;
}
}
void main() {
board_init();
printf("\n%s-%s\n", PICO_PROGRAM_NAME, PICO_PROGRAM_VERSION_STRING);
gpio_init(LVOUT);
gpio_init(LVIN);
gpio_set_dir(LVOUT, GPIO_OUT);
gpio_set_dir(LVIN, GPIO_OUT);
gpio_put(LVOUT, 1);
gpio_put(LVIN, 1);
tusb_init();
kb_init(KBOUT, KBIN);
ms_init(MSOUT, MSIN);
while(1) {
tuh_task();
kb_task();
ms_task();
}
}
void reset() {
printf("\n\n *** PANIC via tinyusb: watchdog reset!\n\n");
watchdog_enable(100, false);
}
//--------------------------------------------------------------------+
// String Descriptor Helper
//--------------------------------------------------------------------+
static void _convert_utf16le_to_utf8(const uint16_t *utf16, size_t utf16_len, uint8_t *utf8, size_t utf8_len) {
// TODO: Check for runover.
(void)utf8_len;
// Get the UTF-16 length out of the data itself.
for (size_t i = 0; i < utf16_len; i++) {
uint16_t chr = utf16[i];
if (chr < 0x80) {
*utf8++ = chr & 0xffu;
} else if (chr < 0x800) {
*utf8++ = (uint8_t)(0xC0 | (chr >> 6 & 0x1F));
*utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F));
} else {
// TODO: Verify surrogate.
*utf8++ = (uint8_t)(0xE0 | (chr >> 12 & 0x0F));
*utf8++ = (uint8_t)(0x80 | (chr >> 6 & 0x3F));
*utf8++ = (uint8_t)(0x80 | (chr >> 0 & 0x3F));
}
// TODO: Handle UTF-16 code points that take two entries.
}
}
// Count how many bytes a utf-16-le encoded string will take in utf-8.
static int _count_utf8_bytes(const uint16_t *buf, size_t len) {
size_t total_bytes = 0;
for (size_t i = 0; i < len; i++) {
uint16_t chr = buf[i];
if (chr < 0x80) {
total_bytes += 1;
} else if (chr < 0x800) {
total_bytes += 2;
} else {
total_bytes += 3;
}
// TODO: Handle UTF-16 code points that take two entries.
}
return (int) total_bytes;
}
static void print_utf16(uint16_t *temp_buf, size_t buf_len) {
if ((temp_buf[0] & 0xff) == 0) return; // empty
size_t utf16_len = ((temp_buf[0] & 0xff) - 2) / sizeof(uint16_t);
size_t utf8_len = (size_t) _count_utf8_bytes(temp_buf + 1, utf16_len);
_convert_utf16le_to_utf8(temp_buf + 1, utf16_len, (uint8_t *) temp_buf, sizeof(uint16_t) * buf_len);
((uint8_t*) temp_buf)[utf8_len] = '\0';
printf("%s", (char*)temp_buf);
}