-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmouse2xac.ino
More file actions
260 lines (228 loc) · 9.16 KB
/
Copy pathmouse2xac.ino
File metadata and controls
260 lines (228 loc) · 9.16 KB
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
/*********************************************************************
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
MIT license, check LICENSE for more information
Copyright (c) 2019 Ha Thach for Adafruit Industriesi
All text above, and the splash screen below must be included in
any redistribution
*********************************************************************/
/*
* Stripped down version of hid_mouse_tremor_filter.ino that removes
* everything except for a USB mouse pass through. Then replace hid mouse
* device to XAC compatible joystick.
*/
/*
MIT License
Copyright (c) 2023 touchgadgetdev@gmail.com
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 demonstrates use of both device and host, where
* - Device run on native usb controller (controller0)
* - Host run on bit-banging 2 GPIOs with the help of Pico-PIO-USB library (controller1)
*
* Requirements:
* - [Pico-PIO-USB](https://github.com/sekigon-gonnoc/Pico-PIO-USB) library
* - 2 consecutive GPIOs: D+ is defined by PIN_PIO_USB_HOST_DP, D- = D+ +1
* - Provide VBus (5v) and GND for peripheral
* - CPU Speed must be either 120 or 240 Mhz. Selected via "Menu -> CPU Speed"
*/
// Set to 0 to remove the CDC ACM serial port but XAC seems to tolerate it
// so it is on by default. Set to 0 if it causes problems. Disabling the
// CDC port means you must press button(s) on the RP2040 to put in bootloader
// upload mode before using the IDE to upload.
#define USB_DEBUG 0
// pio-usb is required for rp2040 host
#include "pio_usb.h"
#include "pio-usb-host-pins.h"
#include "Adafruit_TinyUSB.h"
#include "flight_stick_tinyusb.h"
Adafruit_USBD_HID G_usb_hid;
FSJoystick FSJoy(&G_usb_hid);
// USB Host object
Adafruit_USBH_Host USBHost;
//--------------------------------------------------------------------+
// Setup and Loop on Core0
//--------------------------------------------------------------------+
void setup()
{
#if USB_DEBUG
Serial.begin(115200);
#else
Serial.end();
#endif
FSJoy.begin();
// wait until device mounted
while( !FSJoy.ready() ) delay(1);
FSJoy.write();
#if USB_DEBUG
// while ( !Serial ) delay(10); // wait for native usb
#endif
Serial.println("USB Boot Mouse pass through");
}
typedef struct {
uint8_t report[16];
uint32_t report_count = 0;
uint32_t available_count = 0;
uint32_t last_millis = 0;
uint8_t len;
bool available = false;
bool skip_report_id;
bool hid_mouse;
} Mouse_state_t;
volatile Mouse_state_t Mouse_Report;
void loop()
{
FSJoystick_Report_t joyRpt = {0};
if (Mouse_Report.available) {
if (!FSJoy.ready()) {
delay(1);
return;
}
if (Mouse_Report.len > 2) {
// Comment out this line to disable buttons. This eliminates accidental
// button clicks when using using a trackball operated by foot.
joyRpt.buttons_a = Mouse_Report.report[0];
joyRpt.x = map(Mouse_Report.report[1], SCHAR_MIN, SCHAR_MAX, 0, 1023);
joyRpt.y = map(Mouse_Report.report[2], SCHAR_MIN, SCHAR_MAX, 0, 1023);
G_usb_hid.sendReport(0, (void *)&joyRpt, sizeof(joyRpt));
}
Mouse_Report.available = false;
} else {
if ((millis() - Mouse_Report.last_millis) > 31) {
// Center x,y if no HID report for 32 ms. Preserve the buttons state.
joyRpt.buttons_a = Mouse_Report.report[0];
joyRpt.x = 511;
joyRpt.y = 511;
G_usb_hid.sendReport(0, (void *)&joyRpt, sizeof(joyRpt));
Mouse_Report.last_millis = millis();
}
}
}
//--------------------------------------------------------------------+
// Setup and Loop on Core1
//--------------------------------------------------------------------+
void setup1() {
#if USB_DEBUG
// while ( !Serial ) delay(10); // wait for native usb
#endif
Serial.println("Core1 setup to run TinyUSB host with pio-usb");
// Check for CPU frequency, must be multiple of 120Mhz for bit-banging USB
uint32_t cpu_hz = clock_get_hz(clk_sys);
if ( cpu_hz != 120000000UL && cpu_hz != 240000000UL ) {
// while ( !Serial ) delay(10); // wait for native usb
Serial.printf("Error: CPU Clock = %lu, PIO USB require CPU clock must be multiple of 120 Mhz\r\n", cpu_hz);
Serial.println("Change your CPU Clock to either 120 or 240 Mhz in Menu->CPU Speed");
while(1) delay(1);
}
#ifdef PIN_PIO_USB_HOST_VBUSEN
pinMode(PIN_PIO_USB_HOST_VBUSEN, OUTPUT);
digitalWrite(PIN_PIO_USB_HOST_VBUSEN, PIN_PIO_USB_HOST_VBUSEN_STATE);
#endif
pio_usb_configuration_t pio_cfg = PIO_USB_DEFAULT_CONFIG;
pio_cfg.pin_dp = PIN_PIO_USB_HOST_DP;
USBHost.configure_pio_usb(1, &pio_cfg);
// run host stack on controller (rhport) 1
// Note: For rp2040 pico-pio-usb, calling USBHost.begin() on core1 will have most of the
// host bit-banging processing works done in core1 to free up core0 for other works
USBHost.begin(1);
}
void loop1()
{
USBHost.task();
}
// 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;
uint16_t vid, pid;
tuh_vid_pid_get(dev_addr, &vid, &pid);
Serial.printf("HID device address = %d, instance = %d is mounted\r\n", dev_addr, instance);
Serial.printf("VID = %04x, PID = %04x\r\n", vid, pid);
uint8_t const protocol_mode = tuh_hid_get_protocol(dev_addr, instance);
uint8_t const itf_protocol = tuh_hid_interface_protocol(dev_addr, instance);
Serial.printf("protocol_mode=%d,itf_protocol=%d\r\n",
protocol_mode, itf_protocol);
const size_t REPORT_INFO_MAX = 8;
tuh_hid_report_info_t report_info[REPORT_INFO_MAX];
uint8_t report_num = tuh_hid_parse_report_descriptor(report_info,
REPORT_INFO_MAX, desc_report, desc_len);
Serial.printf("HID descriptor reports:%d\r\n", report_num);
for (size_t i = 0; i < report_num; i++) {
Serial.printf("%d,%d,%d\r\n", report_info[i].report_id, report_info[i].usage,
report_info[i].usage_page);
Mouse_Report.skip_report_id = false;
Mouse_Report.hid_mouse = false;
if ((report_info[i].usage_page == 1) && (report_info[i].usage == 2)) {
// Mouse report
Mouse_Report.hid_mouse = true;
if (itf_protocol == HID_ITF_PROTOCOL_NONE)
Mouse_Report.skip_report_id = report_info[i].report_id != 0;
else if (protocol_mode == HID_PROTOCOL_BOOT)
Mouse_Report.skip_report_id = false;
else
Mouse_Report.skip_report_id = report_info[i].report_id != 0;
break;
}
}
if (desc_report && desc_len) {
for (size_t i = 0; i < desc_len; i++) {
Serial.printf("%x,", desc_report[i]);
}
Serial.println();
}
Mouse_Report.report_count = 0;
Mouse_Report.available_count = 0;
if ((itf_protocol == HID_ITF_PROTOCOL_MOUSE) || Mouse_Report.hid_mouse){
Serial.println("HID Pointer");
if (!tuh_hid_receive_report(dev_addr, instance)) {
Serial.println("Error: cannot request to receive report");
}
}
}
// Invoked when device with hid interface is un-mounted
void tuh_hid_umount_cb(uint8_t dev_addr, uint8_t instance) {
Serial.printf("HID device address = %d, instance = %d is unmounted\r\n", dev_addr, instance);
}
// 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) {
Mouse_Report.report_count++;
if (Mouse_Report.available) {
static uint32_t dropped = 0;
Serial.printf("drops=%lu\r\n", ++dropped);
} else {
if (Mouse_Report.skip_report_id) {
// Skip first byte which is report ID.
report++;
len--;
}
memcpy((void *)Mouse_Report.report, report, min(len, sizeof(Mouse_Report.report)));
Mouse_Report.len = len;
Mouse_Report.available = true;
Mouse_Report.available_count++;
Mouse_Report.last_millis = millis();
}
// continue to request to receive report
if (!tuh_hid_receive_report(dev_addr, instance)) {
Serial.println("Error: cannot request to receive report");
}
}