Skip to content

Commit df3a520

Browse files
authored
Merge pull request #124 from adafruit/update-hid-boot
add hid boot examples
2 parents 8f4420f + 616a65a commit df3a520

File tree

4 files changed

+49
-46
lines changed

4 files changed

+49
-46
lines changed

examples/Composite/mouse_ramdisk/mouse_ramdisk.ino

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,11 @@ Adafruit_USBD_MSC usb_msc;
3434
#if defined ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS || defined ARDUINO_NRF52840_CIRCUITPLAY
3535
const int pin = 4; // Left Button
3636
bool activeState = true;
37+
3738
#elif defined PIN_BUTTON1
3839
const int pin = PIN_BUTTON1;
3940
bool activeState = false;
41+
4042
#else
4143
const int pin = 12;
4244
bool activeState = false;

examples/HID/hid_keyboard/hid_keyboard.ino renamed to examples/HID/hid_boot_keyboard/hid_boot_keyboard.ino

Lines changed: 32 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,20 @@
2121
// Single Report (no ID) descriptor
2222
uint8_t const desc_hid_report[] =
2323
{
24-
TUD_HID_REPORT_DESC_KEYBOARD(),
24+
TUD_HID_REPORT_DESC_KEYBOARD()
2525
};
2626

2727
Adafruit_USBD_HID usb_hid;
2828

2929
// Array of pins and its keycode
3030
// For keycode definition see BLEHidGeneric.h
3131
#ifdef ARDUINO_ARCH_RP2040
32-
uint8_t pins[] = { D0, D1, D2, D3, D4, D5 };
32+
uint8_t pins[] = { D0, D1, D2, D3, D4, D5 };
3333
#else
34-
uint8_t pins[] = { A0, A1, A2, A3, A4, A5 };
34+
uint8_t pins[] = { A0, A1, A2, A3, A4, A5 };
3535
#endif
36-
uint8_t hidcode[] = { HID_KEY_0, HID_KEY_1, HID_KEY_2, HID_KEY_3 , HID_KEY_4, HID_KEY_5 };
36+
37+
uint8_t hidcode[] = { HID_KEY_ARROW_RIGHT, HID_KEY_ARROW_LEFT, HID_KEY_ARROW_DOWN, HID_KEY_ARROW_UP , HID_KEY_4, HID_KEY_5 };
3738

3839
uint8_t pincount = sizeof(pins)/sizeof(pins[0]);
3940

@@ -45,6 +46,7 @@ void setup()
4546
TinyUSB_Device_Init(0);
4647
#endif
4748

49+
usb_hid.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD);
4850
usb_hid.setPollInterval(2);
4951
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
5052
usb_hid.setReportCallback(NULL, hid_report_callback);
@@ -72,18 +74,8 @@ void loop()
7274
// poll gpio once each 2 ms
7375
delay(2);
7476

75-
// // Remote wakeup
76-
// if ( TinyUSBDevice.suspended() && btn )
77-
// {
78-
// // Wake up host if we are in suspend mode
79-
// // and REMOTE_WAKEUP feature is enabled by host
80-
// TinyUSBDevice.remoteWakeup();
81-
// }
82-
83-
if ( !usb_hid.ready() ) return;
84-
77+
// used to avoid send multiple consecutive zero report for keyboard
8578
static bool keyPressedPreviously = false;
86-
bool anyKeyPressed = false;
8779

8880
uint8_t count=0;
8981
uint8_t keycode[6] = { 0 };
@@ -97,35 +89,38 @@ void loop()
9789
keycode[count++] = hidcode[i];
9890

9991
// 6 is max keycode per report
100-
if (count == 6)
101-
{
102-
usb_hid.keyboardReport(0, 0, keycode);
103-
delay(2); // delay for report to send out
104-
105-
// reset report
106-
count = 0;
107-
memset(keycode, 0, 6);
108-
}
109-
110-
// used later
111-
anyKeyPressed = true;
112-
keyPressedPreviously = true;
92+
if (count == 6) break;
11393
}
11494
}
11595

116-
// Send any remaining keys (not accumulated up to 6)
117-
if ( count )
96+
if ( TinyUSBDevice.suspended() && count )
11897
{
119-
usb_hid.keyboardReport(0, 0, keycode);
98+
// Wake up host if we are in suspend mode
99+
// and REMOTE_WAKEUP feature is enabled by host
100+
TinyUSBDevice.remoteWakeup();
120101
}
121102

122-
// Send All-zero report to indicate there is no keys pressed
123-
// Most of the time, it is, though we don't need to send zero report
124-
// every loop(), only a key is pressed in previous loop()
125-
if ( !anyKeyPressed && keyPressedPreviously )
103+
// skip if hid is not ready e.g still transferring previous report
104+
if ( !usb_hid.ready() ) return;
105+
106+
if ( count )
107+
{
108+
// Send report if there is key pressed
109+
uint8_t const report_id = 0;
110+
uint8_t const modifier = 0;
111+
112+
keyPressedPreviously = true;
113+
usb_hid.keyboardReport(report_id, modifier, keycode);
114+
}else
126115
{
127-
keyPressedPreviously = false;
128-
usb_hid.keyboardRelease(0);
116+
// Send All-zero report to indicate there is no keys pressed
117+
// Most of the time, it is, though we don't need to send zero report
118+
// every loop(), only a key is pressed in previous loop()
119+
if ( keyPressedPreviously )
120+
{
121+
keyPressedPreviously = false;
122+
usb_hid.keyboardRelease(0);
123+
}
129124
}
130125
}
131126

examples/HID/hid_mouse/hid_mouse.ino renamed to examples/HID/hid_boot_mouse/hid_boot_mouse.ino

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,23 @@
1414
/* This sketch demonstrates USB HID mouse
1515
* Press button pin will move
1616
* - mouse toward bottom right of monitor
17-
*
17+
*
1818
* Depending on the board, the button pin
1919
* and its active state (when pressed) are different
2020
*/
21-
#if defined ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS
21+
#if defined ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS || defined ARDUINO_NRF52840_CIRCUITPLAY
2222
const int pin = 4; // Left Button
2323
bool activeState = true;
24-
#elif defined ARDUINO_NRF52840_FEATHER
25-
const int pin = 7; // UserSw
24+
25+
#elif defined PIN_BUTTON1
26+
const int pin = PIN_BUTTON1;
2627
bool activeState = false;
28+
2729
#else
2830
const int pin = 12;
2931
bool activeState = false;
3032
#endif
31-
33+
3234

3335
// HID report descriptor using TinyUSB's template
3436
// Single Report (no ID) descriptor
@@ -51,6 +53,7 @@ void setup()
5153
// Set up button, pullup opposite to active state
5254
pinMode(pin, activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
5355

56+
usb_hid.setBootProtocol(HID_ITF_PROTOCOL_MOUSE);
5457
usb_hid.setPollInterval(2);
5558
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
5659
//usb_hid.setStringDescriptor("TinyUSB Mouse");
@@ -86,7 +89,8 @@ void loop()
8689

8790
if ( usb_hid.ready() )
8891
{
92+
uint8_t const report_id = 0; // no ID
8993
int8_t const delta = 5;
90-
usb_hid.mouseMove(0, delta, delta); // no ID: right + down
94+
usb_hid.mouseMove(report_id, delta, delta); // right + down
9195
}
9296
}

examples/HID/hid_composite/hid_composite.ino

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,14 @@
1919
* Depending on the board, the button pin
2020
* and its active state (when pressed) are different
2121
*/
22-
#if defined ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS
22+
#if defined ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS || defined ARDUINO_NRF52840_CIRCUITPLAY
2323
const int pin = 4; // Left Button
2424
bool activeState = true;
25-
#elif defined ARDUINO_NRF52840_FEATHER
26-
const int pin = 7; // UserSw
25+
26+
#elif defined PIN_BUTTON1
27+
const int pin = PIN_BUTTON1;
2728
bool activeState = false;
29+
2830
#else
2931
const int pin = 12;
3032
bool activeState = false;

0 commit comments

Comments
 (0)