-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.c
143 lines (121 loc) · 4.43 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
/*
* The MIT License (MIT)
*
* Copyright (c) 2019 Ha Thach (tinyusb.org)
*
* 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 <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "pico/stdlib.h"
#include "tusb.h"
#include "pico/time.h"
#include "device/usbd_pvt.h"
#include "XInputPad.h"
static inline uint32_t board_millis(void) {
return to_ms_since_boot(get_absolute_time());
}
uint8_t endpoint_in=0;
uint8_t endpoint_out=0;
static void sendReportData(void) {
// Poll every 1ms
const uint32_t interval_ms = 1;
static uint32_t start_ms = 0;
if (board_millis() - start_ms < interval_ms) return; // not enough time
start_ms += interval_ms;
// Remote wakeup
if (tud_suspended()) {
// Wake up host if we are in suspend mode
// and REMOTE_WAKEUP feature is enabled by host
tud_remote_wakeup();
}
XboxButtonData.rsize = 20;
if ((tud_ready()) && ((endpoint_in != 0)) && (!usbd_edpt_busy(0, endpoint_in))){
usbd_edpt_claim(0, endpoint_in);
usbd_edpt_xfer(0, endpoint_in, &XboxButtonData, 20);
usbd_edpt_release(0, endpoint_in);
}
}
int main(void) {
stdio_init_all();
tusb_init();
while (1) {
sendReportData();
tud_task(); // tinyusb device task
}
return 0;
}
static void xinput_init(void) {
}
static void xinput_reset(uint8_t __unused rhport) {
}
static uint16_t xinput_open(uint8_t __unused rhport, tusb_desc_interface_t const *itf_desc, uint16_t max_len) {
//+16 is for the unknown descriptor
uint16_t const drv_len = sizeof(tusb_desc_interface_t) + itf_desc->bNumEndpoints*sizeof(tusb_desc_endpoint_t) + 16;
TU_VERIFY(max_len >= drv_len, 0);
uint8_t const * p_desc = tu_desc_next(itf_desc);
uint8_t found_endpoints = 0;
while ( (found_endpoints < itf_desc->bNumEndpoints) && (drv_len <= max_len) )
{
tusb_desc_endpoint_t const * desc_ep = (tusb_desc_endpoint_t const *) p_desc;
if ( TUSB_DESC_ENDPOINT == tu_desc_type(desc_ep) )
{
TU_ASSERT(usbd_edpt_open(rhport, desc_ep));
if ( tu_edpt_dir(desc_ep->bEndpointAddress) == TUSB_DIR_IN )
{
endpoint_in = desc_ep->bEndpointAddress;
}else
{
endpoint_out = desc_ep->bEndpointAddress;
}
found_endpoints += 1;
}
p_desc = tu_desc_next(p_desc);
}
return drv_len;
}
static bool xinput_device_control_request(uint8_t __unused rhport, tusb_control_request_t const *request) {
return true;
}
static bool xinput_control_complete_cb(uint8_t __unused rhport, tusb_control_request_t __unused const *request) {
return true;
}
//callback after xfer_transfer
static bool xinput_xfer_cb(uint8_t __unused rhport, uint8_t __unused ep_addr, xfer_result_t __unused result, uint32_t __unused xferred_bytes) {
return true;
}
static usbd_class_driver_t const xinput_driver ={
#if CFG_TUSB_DEBUG >= 2
.name = "XINPUT",
#endif
.init = xinput_init,
.reset = xinput_reset,
.open = xinput_open,
.control_request = xinput_device_control_request,
.control_complete = xinput_control_complete_cb,
.xfer_cb = xinput_xfer_cb,
.sof = NULL
};
// Implement callback to add our custom driver
usbd_class_driver_t const *usbd_app_driver_get_cb(uint8_t *driver_count) {
*driver_count = 1;
return &xinput_driver;
}