Skip to content

Commit 0c6fd22

Browse files
nekunekohathach
authored andcommitted
Added HID Gamepad example
1 parent 596d9c0 commit 0c6fd22

File tree

1 file changed

+226
-0
lines changed

1 file changed

+226
-0
lines changed
Lines changed: 226 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,226 @@
1+
/*********************************************************************
2+
Adafruit invests time and resources providing this open source code,
3+
please support Adafruit and open-source hardware by purchasing
4+
products from Adafruit!
5+
6+
MIT license, check LICENSE for more information
7+
Copyright (c) 2021 NeKuNeKo for Adafruit Industries
8+
All text above, and the splash screen below must be included in
9+
any redistribution
10+
*********************************************************************/
11+
12+
#include "Adafruit_TinyUSB.h"
13+
14+
/* This sketch demonstrates USB HID gamepad use.
15+
* This sketch is only valid on boards which have native USB support
16+
* and compatibility with Adafruit TinyUSB library.
17+
* For example SAMD21, SAMD51, nRF52840.
18+
*
19+
* Make sure you select the TinyUSB USB stack if you have a SAMD board.
20+
* You can test the gamepad on a Windows system by pressing WIN+R, writing Joy.cpl and pressing Enter.
21+
*/
22+
23+
// HID report descriptor using TinyUSB's template
24+
// Single Report (no ID) descriptor
25+
uint8_t const desc_hid_report[] =
26+
{
27+
TUD_HID_REPORT_DESC_GAMEPAD()
28+
};
29+
30+
// USB HID object
31+
Adafruit_USBD_HID usb_hid;
32+
33+
hid_gamepad_report_t gp; // defined in hid.h from Adafruit_TinyUSB_ArduinoCore
34+
// For Gamepad Button Bit Mask see hid_gamepad_button_bm_t typedef defined in hid.h from Adafruit_TinyUSB_ArduinoCore
35+
// For Gamepad Hat Bit Mask see hid_gamepad_hat_bm_t typedef defined in hid.h from Adafruit_TinyUSB_ArduinoCore
36+
37+
void setup()
38+
{
39+
Serial.begin(115200);
40+
41+
usb_hid.setPollInterval(2);
42+
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
43+
44+
usb_hid.begin();
45+
46+
// wait until device mounted
47+
while( !USBDevice.mounted() ) delay(1);
48+
49+
Serial.println("Adafruit TinyUSB HID Gamepad example");
50+
}
51+
52+
void loop()
53+
{
54+
// // Remote wakeup
55+
// if ( USBDevice.suspended() && btn )
56+
// {
57+
// // Wake up host if we are in suspend mode
58+
// // and REMOTE_WAKEUP feature is enabled by host
59+
// USBDevice.remoteWakeup();
60+
// }
61+
62+
if ( !usb_hid.ready() ) return;
63+
64+
65+
// Reset buttons
66+
Serial.println("No pressing buttons");
67+
gp.buttons = 0;
68+
gp.x = 0;
69+
gp.y = 0;
70+
gp.z = 0;
71+
gp.r_z = 0;
72+
gp.hat = 0;
73+
usb_hid.sendReport(0, &gp, sizeof(gp));
74+
delay(2000);
75+
76+
77+
// Hat/DPAD UP
78+
Serial.println("Hat/DPAD UP");
79+
gp.hat = 1; // GAMEPAD_HAT_UP;
80+
usb_hid.sendReport(0, &gp, sizeof(gp));
81+
delay(2000);
82+
83+
// Hat/DPAD UP RIGHT
84+
Serial.println("Hat/DPAD UP RIGHT");
85+
gp.hat = 2; // GAMEPAD_HAT_UP_RIGHT;
86+
usb_hid.sendReport(0, &gp, sizeof(gp));
87+
delay(2000);
88+
89+
// Hat/DPAD RIGHT
90+
Serial.println("Hat/DPAD RIGHT");
91+
gp.hat = 3; // GAMEPAD_HAT_RIGHT;
92+
usb_hid.sendReport(0, &gp, sizeof(gp));
93+
delay(2000);
94+
95+
// Hat/DPAD DOWN RIGHT
96+
Serial.println("Hat/DPAD DOWN RIGHT");
97+
gp.hat = 4; // GAMEPAD_HAT_DOWN_RIGHT;
98+
usb_hid.sendReport(0, &gp, sizeof(gp));
99+
delay(2000);
100+
101+
// Hat/DPAD DOWN
102+
Serial.println("Hat/DPAD DOWN");
103+
gp.hat = 5; // GAMEPAD_HAT_DOWN;
104+
usb_hid.sendReport(0, &gp, sizeof(gp));
105+
delay(2000);
106+
107+
// Hat/DPAD DOWN LEFT
108+
Serial.println("Hat/DPAD DOWN LEFT");
109+
gp.hat = 6; // GAMEPAD_HAT_DOWN_LEFT;
110+
usb_hid.sendReport(0, &gp, sizeof(gp));
111+
delay(2000);
112+
113+
// Hat/DPAD LEFT
114+
Serial.println("Hat/DPAD LEFT");
115+
gp.hat = 7; // GAMEPAD_HAT_LEFT;
116+
usb_hid.sendReport(0, &gp, sizeof(gp));
117+
delay(2000);
118+
119+
// Hat/DPAD UP LEFT
120+
Serial.println("Hat/DPAD UP LEFT");
121+
gp.hat = 8; // GAMEPAD_HAT_UP_LEFT;
122+
usb_hid.sendReport(0, &gp, sizeof(gp));
123+
delay(2000);
124+
125+
// Hat/DPAD CENTER
126+
Serial.println("Hat/DPAD CENTER");
127+
gp.hat = 0; // GAMEPAD_HAT_CENTERED;
128+
usb_hid.sendReport(0, &gp, sizeof(gp));
129+
delay(2000);
130+
131+
132+
// Joystick 1 UP
133+
Serial.println("Joystick 1 UP");
134+
gp.x = 0;
135+
gp.y = -127;
136+
usb_hid.sendReport(0, &gp, sizeof(gp));
137+
delay(2000);
138+
139+
// Joystick 1 DOWN
140+
Serial.println("Joystick 1 DOWN");
141+
gp.x = 0;
142+
gp.y = 127;
143+
usb_hid.sendReport(0, &gp, sizeof(gp));
144+
delay(2000);
145+
146+
// Joystick 1 LEFT
147+
Serial.println("Joystick 1 LEFT");
148+
gp.x = -127;
149+
gp.y = 0;
150+
usb_hid.sendReport(0, &gp, sizeof(gp));
151+
delay(2000);
152+
153+
// Joystick 1 RIGHT
154+
Serial.println("Joystick 1 RIGHT");
155+
gp.x = 127;
156+
gp.y = 0;
157+
usb_hid.sendReport(0, &gp, sizeof(gp));
158+
delay(2000);
159+
160+
// Joystick 1 CENTER
161+
Serial.println("Joystick 1 CENTER");
162+
gp.x = 0;
163+
gp.y = 0;
164+
usb_hid.sendReport(0, &gp, sizeof(gp));
165+
delay(2000);
166+
167+
168+
// Joystick 2 UP
169+
Serial.println("Joystick 2 UP");
170+
gp.z = 0;
171+
gp.r_z = 127;
172+
usb_hid.sendReport(0, &gp, sizeof(gp));
173+
delay(2000);
174+
175+
// Joystick 2 DOWN
176+
Serial.println("Joystick 2 DOWN");
177+
gp.z = 0;
178+
gp.r_z = -127;
179+
usb_hid.sendReport(0, &gp, sizeof(gp));
180+
delay(2000);
181+
182+
// Joystick 2 LEFT
183+
Serial.println("Joystick 2 LEFT");
184+
gp.z = -127;
185+
gp.r_z = 0;
186+
usb_hid.sendReport(0, &gp, sizeof(gp));
187+
delay(2000);
188+
189+
// Joystick 2 RIGHT
190+
Serial.println("Joystick 2 RIGHT");
191+
gp.z = 127;
192+
gp.r_z = 0;
193+
usb_hid.sendReport(0, &gp, sizeof(gp));
194+
delay(2000);
195+
196+
// Joystick 2 CENTER
197+
Serial.println("Joystick 2 CENTER");
198+
gp.z = 0;
199+
gp.r_z = 0;
200+
usb_hid.sendReport(0, &gp, sizeof(gp));
201+
delay(2000);
202+
203+
204+
// Test buttons
205+
for (int i=0; i<=15; ++i)
206+
{
207+
Serial.print("Pressing button "); Serial.println(i+1);
208+
gp.buttons = (0x00 | TU_BIT(i));
209+
usb_hid.sendReport(0, &gp, sizeof(gp));
210+
delay(1000);
211+
}
212+
213+
214+
// Random touch
215+
Serial.println("Random touch");
216+
gp.buttons = random(0, 0xffff);
217+
gp.x = random(-127, 128);
218+
gp.y = random(-127, 128);
219+
gp.z = random(-127, 128);
220+
gp.r_z = random(-127, 128);
221+
gp.hat = random(0, 9);
222+
usb_hid.sendReport(0, &gp, sizeof(gp));
223+
delay(2000);
224+
225+
// */
226+
}

0 commit comments

Comments
 (0)