Skip to content

Commit 335e90e

Browse files
committed
update hid_boot_keyboard to support neopixel with power pin, button active high boards such as cpx, cpb and funhouse
also use built-in buttons if defined
1 parent 1b22237 commit 335e90e

File tree

1 file changed

+80
-15
lines changed

1 file changed

+80
-15
lines changed

examples/HID/hid_boot_keyboard/hid_boot_keyboard.ino

Lines changed: 80 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
*********************************************************************/
1111

1212
#include "Adafruit_TinyUSB.h"
13+
#include <Adafruit_NeoPixel.h>
1314

1415
/* This sketch demonstrates USB HID keyboard.
1516
* - PIN A0-A5 is used to send digit '0' to '5' respectively
1617
* (On the RP2040, pins D0-D5 used)
17-
* - LED will be used as Caplock indicator
18+
* - LED and/or Neopixels will be used as Capslock indicator
1819
*/
1920

2021
// HID report descriptor using TinyUSB's template
@@ -24,20 +25,47 @@ uint8_t const desc_hid_report[] =
2425
TUD_HID_REPORT_DESC_KEYBOARD()
2526
};
2627

27-
Adafruit_USBD_HID usb_hid;
28+
// USB HID object
29+
// Note: For ESP32 these values cannot be changed after this declaration
30+
// desc report, desc len, protocol, interval, use out endpoint
31+
Adafruit_USBD_HID usb_hid(desc_hid_report, sizeof(desc_hid_report), HID_ITF_PROTOCOL_KEYBOARD, 2, false);
2832

29-
// Array of pins and its keycode
30-
// For keycode definition see BLEHidGeneric.h
33+
//------------- Input Pins -------------//
34+
// Array of pins and its keycode.
35+
// Notes: these pins can be replaced by PIN_BUTTONn if defined in setup()
3136
#ifdef ARDUINO_ARCH_RP2040
32-
uint8_t pins[] = { D0, D1, D2, D3, D4, D5 };
37+
uint8_t pins[] = { D0, D1, D2, D3 };
3338
#else
34-
uint8_t pins[] = { A0, A1, A2, A3, A4, A5 };
39+
uint8_t pins[] = { A0, A1, A2, A3 };
3540
#endif
3641

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 };
38-
42+
// number of pins
3943
uint8_t pincount = sizeof(pins)/sizeof(pins[0]);
4044

45+
// For keycode definition check out https://github.com/hathach/tinyusb/blob/master/src/class/hid/hid.h
46+
uint8_t hidcode[] = { HID_KEY_ARROW_RIGHT, HID_KEY_ARROW_LEFT, HID_KEY_ARROW_DOWN, HID_KEY_ARROW_UP };
47+
48+
#if defined(ARDUINO_SAMD_CIRCUITPLAYGROUND_EXPRESS) || defined(ARDUINO_NRF52840_CIRCUITPLAY) || defined(ARDUINO_FUNHOUSE_ESP32S2)
49+
bool activeState = true;
50+
#else
51+
bool activeState = false;
52+
#endif
53+
54+
//------------- Neopixel -------------//
55+
// #define PIN_NEOPIXEL 8
56+
#ifdef PIN_NEOPIXEL
57+
58+
// How many NeoPixels are attached to the Arduino?
59+
// use on-board defined NEOPIXEL_NUM if existed
60+
#ifndef NEOPIXEL_NUM
61+
#define NEOPIXEL_NUM 10
62+
#endif
63+
64+
Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NEOPIXEL_NUM, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
65+
66+
#endif
67+
68+
4169
// the setup function runs once when you press reset or power the board
4270
void setup()
4371
{
@@ -46,22 +74,53 @@ void setup()
4674
TinyUSB_Device_Init(0);
4775
#endif
4876

49-
usb_hid.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD);
50-
usb_hid.setPollInterval(2);
51-
usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
77+
// Notes: following commented-out functions has no affect on ESP32
78+
// usb_hid.setBootProtocol(HID_ITF_PROTOCOL_KEYBOARD);
79+
// usb_hid.setPollInterval(2);
80+
// usb_hid.setReportDescriptor(desc_hid_report, sizeof(desc_hid_report));
81+
// usb_hid.setStringDescriptor("TinyUSB Keyboard");
82+
83+
// Set up output report (on control endpoint) for Capslock indicator
5284
usb_hid.setReportCallback(NULL, hid_report_callback);
53-
//usb_hid.setStringDescriptor("TinyUSB Keyboard");
5485

5586
usb_hid.begin();
5687

5788
// led pin
5889
pinMode(LED_BUILTIN, OUTPUT);
5990
digitalWrite(LED_BUILTIN, LOW);
6091

92+
// neopixel if existed
93+
#ifdef PIN_NEOPIXEL
94+
pixels.begin();
95+
pixels.setBrightness(50);
96+
97+
#ifdef NEOPIXEL_POWER
98+
pinMode(NEOPIXEL_POWER, OUTPUT);
99+
digitalWrite(NEOPIXEL_POWER, NEOPIXEL_POWER_ON);
100+
#endif
101+
#endif
102+
103+
// overwrite input pin with PIN_BUTTONx
104+
#ifdef PIN_BUTTON1
105+
pins[0] = PIN_BUTTON1;
106+
#endif
107+
108+
#ifdef PIN_BUTTON2
109+
pins[1] = PIN_BUTTON2;
110+
#endif
111+
112+
#ifdef PIN_BUTTON3
113+
pins[2] = PIN_BUTTON3;
114+
#endif
115+
116+
#ifdef PIN_BUTTON4
117+
pins[3] = PIN_BUTTON4;
118+
#endif
119+
61120
// Set up pin as input
62121
for (uint8_t i=0; i<pincount; i++)
63122
{
64-
pinMode(pins[i], INPUT_PULLUP);
123+
pinMode(pins[i], activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
65124
}
66125

67126
// wait until device mounted
@@ -83,7 +142,7 @@ void loop()
83142
// scan normal key and send report
84143
for(uint8_t i=0; i < pincount; i++)
85144
{
86-
if ( 0 == digitalRead(pins[i]) )
145+
if ( activeState == digitalRead(pins[i]) )
87146
{
88147
// if pin is active (low), add its hid code to key report
89148
keycode[count++] = hidcode[i];
@@ -129,13 +188,19 @@ void hid_report_callback(uint8_t report_id, hid_report_type_t report_type, uint8
129188
{
130189
(void) report_id;
131190
(void) bufsize;
191+
132192
// LED indicator is output report with only 1 byte length
133193
if ( report_type != HID_REPORT_TYPE_OUTPUT ) return;
134194

135195
// The LED bit map is as follows: (also defined by KEYBOARD_LED_* )
136196
// Kana (4) | Compose (3) | ScrollLock (2) | CapsLock (1) | Numlock (0)
137197
uint8_t ledIndicator = buffer[0];
138198

139-
// turn on LED if caplock is set
199+
// turn on LED if capslock is set
140200
digitalWrite(LED_BUILTIN, ledIndicator & KEYBOARD_LED_CAPSLOCK);
201+
202+
#ifdef PIN_NEOPIXEL
203+
pixels.fill(ledIndicator & KEYBOARD_LED_CAPSLOCK ? 0xff0000 : 0x000000);
204+
pixels.show();
205+
#endif
141206
}

0 commit comments

Comments
 (0)