-
Notifications
You must be signed in to change notification settings - Fork 1
/
gamepad_tinyusb.ino
200 lines (157 loc) · 4.49 KB
/
gamepad_tinyusb.ino
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
/************************************************************************
MIT License
Copyright (c) 2021 gdsports625@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.
*************************************************************************/
#include "Adafruit_TinyUSB.h"
#define ATTRIBUTE_PACKED __attribute__((packed, aligned(1)))
typedef struct ATTRIBUTE_PACKED {
uint16_t buttons;
int8_t leftXAxis;
int8_t leftYAxis;
int8_t rightXAxis;
int8_t rightYAxis;
} HID_GamepadReport_Data_t;
// HID report descriptor using TinyUSB's template
// Single Report (no ID) descriptor
uint8_t const desc_hid_report[] =
{
TUD_HID_REPORT_DESC_GAMEPAD()
};
class TUGamepad {
public:
inline TUGamepad(void);
inline void begin(void);
inline void end(void);
inline void loop(void);
inline void write(void);
inline void write(void *report);
inline void press(uint8_t b);
inline void release(uint8_t b);
inline void releaseAll(void);
inline void buttons(uint16_t b);
inline void leftXAxis(int8_t a);
inline void leftYAxis(int8_t a);
inline void rightXAxis(int8_t a);
inline void rightYAxis(int8_t a);
inline bool ready(void) { return this->usb_hid.ready(); };
// Sending is public for advanced users.
inline bool SendReport(void* data, size_t length) {
return this->usb_hid.sendReport(0, data, (uint8_t)length);
};
protected:
HID_GamepadReport_Data_t _report;
uint32_t startMillis;
Adafruit_USBD_HID usb_hid;
};
TUGamepad::TUGamepad(void)
{
this->usb_hid.setPollInterval(1);
this->usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
// setStringDescriptor is undefined on nRF52840
// this->usb_hid.setStringDescriptor("TinyUSB Gamepad");
}
void TUGamepad::begin(void)
{
this->usb_hid.begin();
// release all buttons, center all sticks, etc.
end();
startMillis = millis();
}
void TUGamepad::loop(void)
{
if (startMillis != millis()) {
write();
startMillis = millis();
}
}
void TUGamepad::end(void)
{
memset(&_report, 0x00, sizeof(_report));
SendReport(&_report, sizeof(_report));
}
void TUGamepad::write(void)
{
SendReport(&_report, sizeof(_report));
}
void TUGamepad::write(void *report)
{
memcpy(&_report, report, sizeof(_report));
SendReport(&_report, sizeof(_report));
}
void TUGamepad::press(uint8_t b)
{
b &= 0xF; // Limit value between 0..15
_report.buttons |= (uint16_t)1 << b;
}
void TUGamepad::release(uint8_t b)
{
b &= 0xF; // Limit value between 0..15
_report.buttons &= ~((uint16_t)1 << b);
}
void TUGamepad::releaseAll(void)
{
_report.buttons = 0;
}
void TUGamepad::buttons(uint16_t b)
{
_report.buttons = b;
}
void TUGamepad::leftXAxis(int8_t a)
{
_report.leftXAxis = a;
}
void TUGamepad::leftYAxis(int8_t a)
{
_report.leftYAxis = a;
}
void TUGamepad::rightXAxis(int8_t a)
{
_report.rightXAxis = a;
}
void TUGamepad::rightYAxis(int8_t a)
{
_report.rightYAxis = a;
}
TUGamepad Gamepad;
void setup()
{
Gamepad.begin();
// wait until device mounted
while( !USBDevice.mounted() ) delay(1);
}
void loop()
{
if ( !Gamepad.ready() ) return;
static uint8_t count = 0;
// 16 buttons
if (count > 15) {
Gamepad.releaseAll();
count = 0;
}
Gamepad.press(count);
count++;
// Move x/y Axis to a random position
Gamepad.leftXAxis(random(256));
Gamepad.leftYAxis(random(256));
Gamepad.rightXAxis(random(256));
Gamepad.rightYAxis(random(256));
// Functions above only set the values.
// This writes the report to the host.
Gamepad.loop();
delay(100);
}