Skip to content

Commit b576368

Browse files
authored
Merge pull request #287 from adafruit/add-mul-hid-example
add multiple hid interfaces example
2 parents 008de7f + d6dffe1 commit b576368

File tree

2 files changed

+125
-0
lines changed

2 files changed

+125
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
/examples/**/build/
22
/.development
3+
.idea
4+
platformio.ini
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
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) 2019 Ha Thach 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 multiple USB HID interfaces. Pressing the button will
15+
* - mouse toward bottom right of monitor
16+
* - send 'a' key
17+
*
18+
* Depending on the board, the button pin
19+
* and its active state (when pressed) are different
20+
*/
21+
#if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY)
22+
const int pin = 4; // Left Button
23+
bool activeState = true;
24+
25+
#elif defined(ARDUINO_FUNHOUSE_ESP32S2)
26+
const int pin = BUTTON_DOWN;
27+
bool activeState = true;
28+
29+
#elif defined PIN_BUTTON1
30+
const int pin = PIN_BUTTON1;
31+
bool activeState = false;
32+
33+
#elif defined PIN_BUTTON
34+
const int pin = PIN_BUTTON;
35+
bool activeState = false;
36+
37+
#else
38+
const int pin = 12;
39+
bool activeState = false;
40+
#endif
41+
42+
// HID report descriptor using TinyUSB's template
43+
uint8_t const desc_keyboard_report[] =
44+
{
45+
TUD_HID_REPORT_DESC_KEYBOARD()
46+
};
47+
48+
uint8_t const desc_mouse_report[] =
49+
{
50+
TUD_HID_REPORT_DESC_MOUSE()
51+
};
52+
53+
// USB HID object. For ESP32 these values cannot be changed after this declaration
54+
// desc report, desc len, protocol, interval, use out endpoint
55+
Adafruit_USBD_HID usb_keyboard(desc_keyboard_report, sizeof(desc_keyboard_report), HID_ITF_PROTOCOL_KEYBOARD, 2, false);
56+
Adafruit_USBD_HID usb_mouse(desc_mouse_report, sizeof(desc_mouse_report), HID_ITF_PROTOCOL_MOUSE, 2, false);
57+
58+
// the setup function runs once when you press reset or power the board
59+
void setup()
60+
{
61+
// Notes: following commented-out functions has no affect on ESP32
62+
// usb_keyboard.setPollInterval(2);
63+
// usb_keyboard.setReportDescriptor();
64+
// usb_keyboard.setStringDescriptor("TinyUSB HID Composite");
65+
66+
usb_keyboard.begin();
67+
usb_mouse.begin();
68+
69+
// Set up button, pullup opposite to active state
70+
pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
71+
72+
Serial.begin(115200);
73+
74+
// wait until device mounted
75+
//while( !TinyUSBDevice.mounted() ) delay(1);
76+
Serial.println("Adafruit TinyUSB HID Composite example");
77+
}
78+
79+
void loop()
80+
{
81+
// poll gpio once each 10 ms
82+
delay(10);
83+
84+
// Whether button is pressed
85+
bool btn_pressed = (digitalRead(pin) == activeState);
86+
87+
// Remote wakeup
88+
if ( TinyUSBDevice.suspended() && btn_pressed )
89+
{
90+
// Wake up host if we are in suspend mode
91+
// and REMOTE_WAKEUP feature is enabled by host
92+
TinyUSBDevice.remoteWakeup();
93+
}
94+
95+
/*------------- Mouse -------------*/
96+
if (usb_mouse.ready() && btn_pressed )
97+
{
98+
int8_t const delta = 5;
99+
usb_mouse.mouseMove(0, delta, delta); // right + down
100+
}
101+
102+
/*------------- Keyboard -------------*/
103+
if ( usb_keyboard.ready() )
104+
{
105+
// use to send key release report
106+
static bool has_key = false;
107+
108+
if ( btn_pressed )
109+
{
110+
uint8_t keycode[6] = { 0 };
111+
keycode[0] = HID_KEY_A;
112+
113+
usb_keyboard.keyboardReport(0, 0, keycode);
114+
115+
has_key = true;
116+
}else
117+
{
118+
// send empty key report if previously has key pressed
119+
if (has_key) usb_keyboard.keyboardRelease(0);
120+
has_key = false;
121+
}
122+
}
123+
}

0 commit comments

Comments
 (0)