Skip to content

Commit c919886

Browse files
committed
Add custom HID device example
1 parent 8ed0d12 commit c919886

File tree

3 files changed

+79
-0
lines changed

3 files changed

+79
-0
lines changed

libraries/USB/examples/CustomHIDDevice/.skip.esp32

Whitespace-only changes.

libraries/USB/examples/CustomHIDDevice/.skip.esp32c3

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
#include "USB.h"
2+
#include "USBHID.h"
3+
USBHID HID;
4+
5+
static const uint8_t report_descriptor[] = { // 8 axis
6+
0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
7+
0x09, 0x04, // Usage (Joystick)
8+
0xa1, 0x01, // Collection (Application)
9+
0xa1, 0x00, // Collection (Physical)
10+
0x05, 0x01, // Usage Page (Generic Desktop Ctrls)
11+
0x09, 0x30, // Usage (X)
12+
0x09, 0x31, // Usage (Y)
13+
0x09, 0x32, // Usage (Z)
14+
0x09, 0x33, // Usage (Rx)
15+
0x09, 0x34, // Usage (Ry)
16+
0x09, 0x35, // Usage (Rz)
17+
0x09, 0x36, // Usage (Slider)
18+
0x09, 0x36, // Usage (Slider)
19+
0x15, 0x81, // Logical Minimum (-127)
20+
0x25, 0x7f, // Logical Maximum (127)
21+
0x75, 0x08, // Report Size (8)
22+
0x95, 0x08, // Report Count (8)
23+
0x81, 0x02, // Input (Data,Var,Abs,No Wrap,Linear,Preferred State,No Null Position)
24+
0xC0, // End Collection
25+
0xC0, // End Collection
26+
};
27+
28+
class CustomHIDDevice: public USBHIDDevice {
29+
public:
30+
CustomHIDDevice(void){
31+
static bool initialized = false;
32+
if(!initialized){
33+
initialized = true;
34+
HID.addDevice(this, sizeof(report_descriptor));
35+
}
36+
}
37+
38+
void begin(void){
39+
HID.begin();
40+
}
41+
42+
uint16_t _onGetDescriptor(uint8_t* buffer){
43+
memcpy(buffer, report_descriptor, sizeof(report_descriptor));
44+
return sizeof(report_descriptor);
45+
}
46+
47+
bool send(uint8_t * value){
48+
return HID.SendReport(0, value, 8);
49+
}
50+
};
51+
52+
CustomHIDDevice Device;
53+
54+
const int buttonPin = 0;
55+
int previousButtonState = HIGH;
56+
uint8_t axis[8];
57+
58+
void setup() {
59+
Serial.begin(115200);
60+
Serial.setDebugOutput(true);
61+
pinMode(buttonPin, INPUT_PULLUP);
62+
Device.begin();
63+
USB.begin();
64+
}
65+
66+
void loop() {
67+
int buttonState = digitalRead(buttonPin);
68+
if (HID.ready() && buttonState != previousButtonState) {
69+
previousButtonState = buttonState;
70+
if (buttonState == LOW) {
71+
Serial.println("Button Pressed");
72+
axis[0] = random() & 0xFF;
73+
Device.send(axis);
74+
} else {
75+
Serial.println("Button Released");
76+
}
77+
delay(100);
78+
}
79+
}

0 commit comments

Comments
 (0)