-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.c
467 lines (383 loc) · 13.1 KB
/
main.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Ha Thach (tinyusb.org)
* sekigon-gonnoc
*
* 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.
*
*/
// This example runs both host and device concurrently. The USB host receive
// reports from HID device and print it out over USB Device CDC interface.
// For TinyUSB roothub port0 is native usb controller, roothub port1 is
// pico-pio-usb.
#define __STDC_FORMAT_MACROS
#include <inttypes.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "pico/stdlib.h"
#include "pico/multicore.h"
#include "pico/bootrom.h"
#include <pico/time.h>
#include "pio_usb.h"
#include "tusb.h"
#include "xinput_host.h"
//--------------------------------------------------------------------+
// MACRO CONSTANT TYPEDEF PROTYPES
//--------------------------------------------------------------------+
// uncomment if you are using colemak layout
// #define KEYBOARD_COLEMAK
#ifdef KEYBOARD_COLEMAK
const uint8_t colemak[128] = {
0 , 0, 0, 0, 0, 0, 0, 22,
9 , 23, 7, 0, 24, 17, 8, 12,
0 , 14, 28, 51, 0, 19, 21, 10,
15 , 0, 0, 0, 13, 0, 0, 0,
0 , 0, 0, 0, 0, 0, 0, 0,
0 , 0, 0, 0, 0, 0, 0, 0,
0 , 0, 0, 18, 0, 0, 0, 0,
0 , 0, 0, 0, 0, 0, 0, 0,
0 , 0, 0, 0, 0, 0, 0, 0,
0 , 0, 0, 0, 0, 0, 0, 0
};
#endif
static uint8_t const keycode2ascii[128][2] = { HID_KEYCODE_TO_ASCII };
uint64_t getTime()
{
return time_us_64();
}
uint64_t INPUT_SENT;
uint64_t LAST_FALL_TIME = 0x00;
uint64_t LAST_RISE_TIME = 0x00;
int NUM_TESTS = 10;
uint64_t PIN_DELAY = 100; // ms to wait after setting a pin high/low
float TEST_DATA[10];
int CURR_TEST;
void sendInput() {
gpio_put(3, false);
}
float arrmax(float a[], int num_elements)
{
int i;
float max;
max = a[0];
for (i=1; i<num_elements; i++)
{
if (a[i]>max)
{
max=a[i];
}
}
return(max);
}
float arrmin(float a[], int num_elements)
{
int i;
float min;
min = a[0];
for (i=1; i<num_elements; i++)
{
if (a[i]<min)
{
min=a[i];
}
}
return(min);
}
float arravg(float a[], int num_elements)
{
float sum;
int i;
float avg;
sum=0;
avg=0;
for (i=0; i<num_elements;i++)
{
sum=sum+a[i];
avg=(float)sum/(i+1);
}
return(avg);
}
/*------------- MAIN -------------*/
// core1: handle host events
void core1_main() {
sleep_ms(10);
// Use tuh_configure() to pass pio configuration to the host stack
// Note: tuh_configure() must be called before
pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG;
tuh_configure(1, TUH_CFGID_RPI_PIO_USB_CONFIGURATION, &pio_cfg);
// To run USB SOF interrupt in core1, init host stack for pio_usb (roothub
// port1) on core1
tuh_init(1);
while (true) {
tuh_task(); // tinyusb host task
}
}
// core0: handle device events
int main(void) {
// default 125MHz is not appropreate. Sysclock should be multiple of 12MHz.
set_sys_clock_khz(120000, true);
sleep_ms(10);
multicore_reset_core1();
// all USB task run in core1
multicore_launch_core1(core1_main);
gpio_init(3); // Initialize pin
gpio_set_dir(3, GPIO_OUT); // Set as INPUT
gpio_pull_up(3); // Set as PULLUP
gpio_put(3, true);
// init device stack on native usb (roothub port0)
tud_init(0);
while (true) {
tud_task(); // tinyusb device task
tud_cdc_write_flush();
}
return 0;
}
//--------------------------------------------------------------------+
// Device CDC
//--------------------------------------------------------------------+
// Invoked when CDC interface received data from host
void tud_cdc_rx_cb(uint8_t itf)
{
(void) itf;
char buf[64];
uint32_t count = tud_cdc_read(buf, sizeof(buf));
// TODO control LED on keyboard of host stack
(void) count;
}
//--------------------------------------------------------------------+
// Host HID
//--------------------------------------------------------------------+
// Invoked when device with hid interface is mounted
// Report descriptor is also available for use. tuh_hid_parse_report_descriptor()
// can be used to parse common/simple enough descriptor.
// Note: if report descriptor length > CFG_TUH_ENUMERATION_BUFSIZE, it will be skipped
// therefore report_desc = NULL, desc_len = 0
void tuh_hid_mount_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* desc_report, uint16_t desc_len)
{
(void)desc_report;
(void)desc_len;
// Interface protocol (hid_interface_protocol_enum_t)
const char* protocol_str[] = { "None", "Keyboard", "Mouse" };
uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance);
uint16_t vid, pid;
tuh_vid_pid_get(dev_addr, &vid, &pid);
char tempbuf[256];
int count = sprintf(tempbuf, "[%04x:%04x][%u] HID Interface%u, Protocol = %s\r\n", vid, pid, dev_addr, instance, protocol_str[itf_protocol]);
tud_cdc_write(tempbuf, count);
tud_cdc_write_flush();
// Receive report from boot keyboard & mouse only
// tuh_hid_report_received_cb() will be invoked when report is available
if (itf_protocol == HID_ITF_PROTOCOL_KEYBOARD || itf_protocol == HID_ITF_PROTOCOL_MOUSE)
{
if ( !tuh_hid_receive_report(dev_addr, instance) )
{
tud_cdc_write_str("Error: cannot request report\r\n");
}
}
}
// Invoked when device with hid interface is un-mounted
void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance)
{
char tempbuf[256];
int count = sprintf(tempbuf, "[%u] HID Interface%u is unmounted\r\n", dev_addr, instance);
tud_cdc_write(tempbuf, count);
tud_cdc_write_flush();
}
// look up new key in previous keys
static inline bool find_key_in_report(hid_keyboard_report_t const *report, uint8_t keycode)
{
for(uint8_t i=0; i<6; i++)
{
if (report->keycode[i] == keycode) return true;
}
return false;
}
// convert hid keycode to ascii and print via usb device CDC (ignore non-printable)
static void process_kbd_report(uint8_t dev_addr, hid_keyboard_report_t const *report)
{
(void) dev_addr;
static hid_keyboard_report_t prev_report = { 0, 0, {0} }; // previous report to check key released
bool flush = false;
for(uint8_t i=0; i<6; i++)
{
uint8_t keycode = report->keycode[i];
if ( keycode )
{
if ( find_key_in_report(&prev_report, keycode) )
{
// exist in previous report means the current key is holding
}else
{
// not existed in previous report means the current key is pressed
// remap the key code for Colemak layout
#ifdef KEYBOARD_COLEMAK
uint8_t colemak_key_code = colemak[keycode];
if (colemak_key_code != 0) keycode = colemak_key_code;
#endif
bool const is_shift = report->modifier & (KEYBOARD_MODIFIER_LEFTSHIFT | KEYBOARD_MODIFIER_RIGHTSHIFT);
uint8_t ch = keycode2ascii[keycode][is_shift ? 1 : 0];
if (ch)
{
if (ch == '\n') tud_cdc_write("\r", 1);
tud_cdc_write(&ch, 1);
flush = true;
}
}
}
// TODO example skips key released
}
if (flush) tud_cdc_write_flush();
prev_report = *report;
}
// Invoked when received report from device via interrupt endpoint
void tuh_hid_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const* report, uint16_t len)
{
(void) len;
uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance);
switch(itf_protocol)
{
case HID_ITF_PROTOCOL_KEYBOARD:
process_kbd_report(dev_addr, (hid_keyboard_report_t const*) report );
break;
default: break;
}
// continue to request to receive report
if ( !tuh_hid_receive_report(dev_addr, instance) )
{
tud_cdc_write_str("Error: cannot request report\r\n");
}
}
void tuh_xinput_report_received_cb(uint8_t dev_addr, uint8_t instance, uint8_t const *report, uint16_t len)
{
char tempbuf[256];
int count;
uint64_t currTime = time_us_64();
uint64_t tmpTestElapsed = currTime - INPUT_SENT;
uint64_t tmpEdgeToEdgeTime;
float edgeToEdgeTime;
// These should probably go in a define...
//
// count = sprintf(tempbuf, "Previous: %lluusec Current: %lluusec Elapsed: %lluusec\n", INPUT_SENT, currTime, tmpElapsed);
// tud_cdc_write(tempbuf, count);
// tud_cdc_write_flush();
// count = sprintf(tempbuf, "XInput Report:\n");
// tud_cdc_write(tempbuf, count);
// tud_cdc_write_flush();
// for(uint16_t i=0; i < len; i++)
// {
// count = sprintf(tempbuf, "0x%02x ", report[i]);
// tud_cdc_write(tempbuf, count);
// tud_cdc_write_flush();
// if ((i + 1) % 8 == 0) {
// tud_cdc_write(" ", 3);
// tud_cdc_write_flush();
// }
// if ((i + 1) % 16 == 0) {
// tud_cdc_write("\n", 1);
// tud_cdc_write_flush();
// }
// }
float elapsedTime = (float)tmpTestElapsed / 1000.0;
if (CURR_TEST < NUM_TESTS) {
count = sprintf(tempbuf, "\nCurrent test #%d\nElapsed time: %.2fms\n", CURR_TEST + 1, elapsedTime);
tud_cdc_write(tempbuf, count);
tud_cdc_write_flush();
}
xinputh_interface_t *xid_itf = (xinputh_interface_t *)report;
xinput_gamepad_t *p = &xid_itf->pad;
if (xid_itf->connected && xid_itf->new_pad_data) {
count = sprintf(tempbuf, "Pad data received: 0x%04x\n", p->wButtons);
tud_cdc_write(tempbuf, count);
tud_cdc_write_flush();
if (p->wButtons == 0x0000) {
LAST_FALL_TIME = currTime;
} else {
LAST_RISE_TIME = currTime;
}
}
if (LAST_FALL_TIME > LAST_RISE_TIME) {
tmpEdgeToEdgeTime = LAST_FALL_TIME - LAST_RISE_TIME;
edgeToEdgeTime = ((float)tmpEdgeToEdgeTime / 1000.0);
count = sprintf(tempbuf, "Fall detected: %.2fms\n", edgeToEdgeTime);
tud_cdc_write(tempbuf, count);
tud_cdc_write_flush();
}
if (LAST_RISE_TIME > LAST_FALL_TIME) {
tmpEdgeToEdgeTime = LAST_RISE_TIME - LAST_FALL_TIME;
edgeToEdgeTime = ((float)tmpEdgeToEdgeTime / 1000.0);
count = sprintf(tempbuf, "Rise detected: %.2fms\n", edgeToEdgeTime);
tud_cdc_write(tempbuf, count);
tud_cdc_write_flush();
TEST_DATA[CURR_TEST] = edgeToEdgeTime;
sleep_ms(PIN_DELAY);
LAST_FALL_TIME += PIN_DELAY * 1000;
LAST_RISE_TIME += PIN_DELAY * 1000;
gpio_put(3, true);
CURR_TEST++;
if (CURR_TEST < NUM_TESTS) {
sleep_ms(PIN_DELAY);
LAST_FALL_TIME += PIN_DELAY * 1000;
LAST_RISE_TIME += PIN_DELAY * 1000;
INPUT_SENT = time_us_64();
gpio_put(3, false);
} else {
float min = arrmin(TEST_DATA, 10);
float max = arrmax(TEST_DATA, 10);
float avg = arravg(TEST_DATA, 10);
int count = sprintf(tempbuf, "\n\nTest Results:\nMin:%.2f ms, Max:%.2f ms, Avg: %.2f ms\n\n\n", min, max, avg);
tud_cdc_write(tempbuf, count);
tud_cdc_write_flush();
}
}
tuh_xinput_receive_report(dev_addr, instance);
}
void tuh_xinput_mount_cb(uint8_t dev_addr, uint8_t instance, const xinputh_interface_t *xinput_itf)
{
char tempbuf[256];
int count = sprintf(tempbuf, "XINPUT MOUNTED %02x %d\n", dev_addr, instance);
tud_cdc_write(tempbuf, count);
tud_cdc_write_flush();
// If this is a Xbox 360 Wireless controller we need to wait for a connection packet
// on the in pipe before setting LEDs etc. So just start getting data until a controller is connected.
if (xinput_itf->type == XBOX360_WIRELESS && xinput_itf->connected == false)
{
tuh_xinput_receive_report(dev_addr, instance);
return;
}
tuh_xinput_set_led(dev_addr, instance, 0, true);
tuh_xinput_set_led(dev_addr, instance, 1, true);
tuh_xinput_set_rumble(dev_addr, instance, 0, 0, true);
tuh_xinput_receive_report(dev_addr, instance);
tud_cdc_write("Beginning automatic testing...\n", 32);
tud_cdc_write_flush();
CURR_TEST = 0;
INPUT_SENT = time_us_64();
gpio_put(3, false);
}
void tuh_xinput_umount_cb(uint8_t dev_addr, uint8_t instance)
{
char tempbuf[256];
int count = sprintf(tempbuf, "XINPUT UNMOUNTED %02x %d\n", dev_addr, instance);
tud_cdc_write(tempbuf, count);
tud_cdc_write_flush();
CURR_TEST = 0;
memset(TEST_DATA, 0, sizeof(TEST_DATA));
}