10
10
*********************************************************************/
11
11
12
12
#include " Adafruit_TinyUSB.h"
13
+ #include < Adafruit_NeoPixel.h>
13
14
14
15
/* This sketch demonstrates USB HID keyboard.
15
16
* - PIN A0-A5 is used to send digit '0' to '5' respectively
16
17
* (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
18
19
*/
19
20
20
21
// HID report descriptor using TinyUSB's template
@@ -24,20 +25,47 @@ uint8_t const desc_hid_report[] =
24
25
TUD_HID_REPORT_DESC_KEYBOARD ()
25
26
};
26
27
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);
28
32
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()
31
36
#ifdef ARDUINO_ARCH_RP2040
32
- uint8_t pins[] = { D0, D1, D2, D3, D4, D5 };
37
+ uint8_t pins[] = { D0, D1, D2, D3 };
33
38
#else
34
- uint8_t pins[] = { A0, A1, A2, A3, A4, A5 };
39
+ uint8_t pins[] = { A0, A1, A2, A3 };
35
40
#endif
36
41
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
39
43
uint8_t pincount = sizeof (pins)/sizeof (pins[0 ]);
40
44
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
+
41
69
// the setup function runs once when you press reset or power the board
42
70
void setup ()
43
71
{
@@ -46,22 +74,53 @@ void setup()
46
74
TinyUSB_Device_Init (0 );
47
75
#endif
48
76
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
52
84
usb_hid.setReportCallback (NULL , hid_report_callback);
53
- // usb_hid.setStringDescriptor("TinyUSB Keyboard");
54
85
55
86
usb_hid.begin ();
56
87
57
88
// led pin
58
89
pinMode (LED_BUILTIN, OUTPUT);
59
90
digitalWrite (LED_BUILTIN, LOW);
60
91
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
+
61
120
// Set up pin as input
62
121
for (uint8_t i=0 ; i<pincount; i++)
63
122
{
64
- pinMode (pins[i], INPUT_PULLUP);
123
+ pinMode (pins[i], activeState ? INPUT_PULLDOWN : INPUT_PULLUP);
65
124
}
66
125
67
126
// wait until device mounted
@@ -83,7 +142,7 @@ void loop()
83
142
// scan normal key and send report
84
143
for (uint8_t i=0 ; i < pincount; i++)
85
144
{
86
- if ( 0 == digitalRead (pins[i]) )
145
+ if ( activeState == digitalRead (pins[i]) )
87
146
{
88
147
// if pin is active (low), add its hid code to key report
89
148
keycode[count++] = hidcode[i];
@@ -129,13 +188,19 @@ void hid_report_callback(uint8_t report_id, hid_report_type_t report_type, uint8
129
188
{
130
189
(void ) report_id;
131
190
(void ) bufsize;
191
+
132
192
// LED indicator is output report with only 1 byte length
133
193
if ( report_type != HID_REPORT_TYPE_OUTPUT ) return ;
134
194
135
195
// The LED bit map is as follows: (also defined by KEYBOARD_LED_* )
136
196
// Kana (4) | Compose (3) | ScrollLock (2) | CapsLock (1) | Numlock (0)
137
197
uint8_t ledIndicator = buffer[0 ];
138
198
139
- // turn on LED if caplock is set
199
+ // turn on LED if capslock is set
140
200
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
141
206
}
0 commit comments