Skip to content

Commit e16e021

Browse files
author
Nico
committed
Led Out Report, Minor Keyboard + Mouse changes
arduino/Arduino#2275 (comment) Added Led Out Report (with definition) Is the getLEDs() name okay? Adds 44 bytes flash if activated Added missing Keyboard + Mouse definitions Changed Mouse HID report a bit Added definitions to en/disable current reports No bigger changes to the HID Api were made
1 parent 63ea8f0 commit e16e021

File tree

3 files changed

+140
-35
lines changed

3 files changed

+140
-35
lines changed

plugins/KeyboardioHID/HID.cpp

Lines changed: 78 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -36,32 +36,40 @@ Keyboard_ Keyboard;
3636
#define LSB(_x) ((_x) & 0xFF)
3737
#define MSB(_x) ((_x) >> 8)
3838

39-
#define RAWHID_USAGE_PAGE 0xFFC0
40-
#define RAWHID_USAGE 0x0C00
41-
#define RAWHID_TX_SIZE 64
42-
#define RAWHID_RX_SIZE 64
39+
#define RAWHID_USAGE_PAGE 0xFFC0 // recommended: 0xFF00 to 0xFFFF
40+
#define RAWHID_USAGE 0x0C00 // recommended: 0x0100 to 0xFFFF
41+
#define RAWHID_TX_SIZE (USB_EP_SIZE-1)
42+
#define RAWHID_RX_SIZE (USB_EP_SIZE-1)
43+
44+
#if defined USBCON && defined(HID_KEYBOARD_ENABLED) && defined(HID_KEYBOARD_LEDS_ENABLED)
45+
// extern accessible led out report
46+
uint8_t hid_keyboard_leds = 0;
47+
#endif
4348

4449
extern const u8 _hidReportDescriptor[] PROGMEM;
4550
const u8 _hidReportDescriptor[] = {
46-
47-
// Mouse
51+
#ifdef HID_MOUSE_ENABLED
52+
// Mouse
4853
0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 54
4954
0x09, 0x02, // USAGE (Mouse)
5055
0xa1, 0x01, // COLLECTION (Application)
5156
0x09, 0x01, // USAGE (Pointer)
5257
0xa1, 0x00, // COLLECTION (Physical)
53-
0x85, 0x01, // REPORT_ID (1)
58+
0x85, HID_REPORTID_MouseReport,// REPORT_ID
59+
// 5 buttons
5460
0x05, 0x09, // USAGE_PAGE (Button)
5561
0x19, 0x01, // USAGE_MINIMUM (Button 1)
56-
0x29, 0x03, // USAGE_MAXIMUM (Button 3)
62+
0x29, 0x05, // USAGE_MAXIMUM (Button 5)
5763
0x15, 0x00, // LOGICAL_MINIMUM (0)
5864
0x25, 0x01, // LOGICAL_MAXIMUM (1)
59-
0x95, 0x03, // REPORT_COUNT (3)
65+
0x95, 0x05, // REPORT_COUNT (5)
6066
0x75, 0x01, // REPORT_SIZE (1)
6167
0x81, 0x02, // INPUT (Data,Var,Abs)
68+
// reserved
6269
0x95, 0x01, // REPORT_COUNT (1)
63-
0x75, 0x05, // REPORT_SIZE (5)
70+
0x75, 0x03, // REPORT_SIZE (3)
6471
0x81, 0x03, // INPUT (Cnst,Var,Abs)
72+
// x, y, wheel
6573
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
6674
0x09, 0x30, // USAGE (X)
6775
0x09, 0x31, // USAGE (Y)
@@ -71,58 +79,75 @@ const u8 _hidReportDescriptor[] = {
7179
0x75, 0x08, // REPORT_SIZE (8)
7280
0x95, 0x03, // REPORT_COUNT (3)
7381
0x81, 0x06, // INPUT (Data,Var,Rel)
82+
// end
7483
0xc0, // END_COLLECTION
7584
0xc0, // END_COLLECTION
85+
#endif
7686

77-
// Keyboard
78-
0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 47
87+
#ifdef HID_KEYBOARD_ENABLED
88+
// Keyboard
89+
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
7990
0x09, 0x06, // USAGE (Keyboard)
8091
0xa1, 0x01, // COLLECTION (Application)
81-
0x85, 0x02, // REPORT_ID (2)
92+
0x85, HID_REPORTID_KeyboardReport, // REPORT_ID
8293
0x05, 0x07, // USAGE_PAGE (Keyboard)
83-
94+
// modifiers
8495
0x19, 0xe0, // USAGE_MINIMUM (Keyboard LeftControl)
8596
0x29, 0xe7, // USAGE_MAXIMUM (Keyboard Right GUI)
8697
0x15, 0x00, // LOGICAL_MINIMUM (0)
8798
0x25, 0x01, // LOGICAL_MAXIMUM (1)
8899
0x75, 0x01, // REPORT_SIZE (1)
89-
90100
0x95, 0x08, // REPORT_COUNT (8)
91101
0x81, 0x02, // INPUT (Data,Var,Abs)
102+
// reserved byte
92103
0x95, 0x01, // REPORT_COUNT (1)
93104
0x75, 0x08, // REPORT_SIZE (8)
94105
0x81, 0x03, // INPUT (Cnst,Var,Abs)
95-
106+
// Key[6] Array
96107
0x95, 0x06, // REPORT_COUNT (6)
97108
0x75, 0x08, // REPORT_SIZE (8)
98109
0x15, 0x00, // LOGICAL_MINIMUM (0)
99110
0x25, 0x65, // LOGICAL_MAXIMUM (101)
100111
0x05, 0x07, // USAGE_PAGE (Keyboard)
101-
102112
0x19, 0x00, // USAGE_MINIMUM (Reserved (no event indicated))
103113
0x29, 0x65, // USAGE_MAXIMUM (Keyboard Application)
104114
0x81, 0x00, // INPUT (Data,Ary,Abs)
115+
#ifdef HID_KEYBOARD_LEDS_ENABLED
116+
// LEDs for num lock etc
117+
0x05, 0x08, /* USAGE_PAGE (LEDs) */
118+
0x19, 0x01, /* USAGE_MINIMUM (Num Lock) */
119+
0x29, 0x05, /* USAGE_MAXIMUM (Kana) */
120+
0x95, 0x05, /* REPORT_COUNT (5) */
121+
0x75, 0x01, /* REPORT_SIZE (1) */
122+
0x91, 0x02, /* OUTPUT (Data,Var,Abs) */
123+
// Reserved 3 bits
124+
0x95, 0x01, /* REPORT_COUNT (1) */
125+
0x75, 0x03, /* REPORT_SIZE (3) */
126+
0x91, 0x03, /* OUTPUT (Cnst,Var,Abs) */
127+
#endif
128+
// end
105129
0xc0, // END_COLLECTION
130+
#endif
106131

107-
#ifdef RAWHID_ENABLED
108-
// RAW HID
132+
#ifdef HID_RAWHID_ENABLED
133+
// RAW HID
109134
0x06, LSB(RAWHID_USAGE_PAGE), MSB(RAWHID_USAGE_PAGE), // 30
110135
0x0A, LSB(RAWHID_USAGE), MSB(RAWHID_USAGE),
111136

112-
0xA1, 0x01, // Collection 0x01
113-
0x85, 0x03, // REPORT_ID (3)
114-
0x75, 0x08, // report size = 8 bits
115-
0x15, 0x00, // logical minimum = 0
116-
0x26, 0xFF, 0x00, // logical maximum = 255
137+
0xA1, 0x01, // Collection 0x01
138+
0x85, HID_REPORTID_RawKeyboardReport, // REPORT_ID
139+
0x75, 0x08, // report size = 8 bits
140+
0x15, 0x00, // logical minimum = 0
141+
0x26, 0xFF, 0x00, // logical maximum = 255
117142

118-
0x95, 64, // report count TX
119-
0x09, 0x01, // usage
120-
0x81, 0x02, // Input (array)
143+
0x95, RAWHID_TX_SIZE, // report count TX
144+
0x09, 0x01, // usage
145+
0x81, 0x02, // Input (array)
121146

122-
0x95, 64, // report count RX
123-
0x09, 0x02, // usage
124-
0x91, 0x02, // Output (array)
125-
0xC0 // end collection
147+
0x95, RAWHID_RX_SIZE, // report count RX
148+
0x09, 0x02, // usage
149+
0x91, 0x02, // Output (array)
150+
0xC0, // end collection
126151
#endif
127152
};
128153

@@ -192,6 +217,20 @@ bool WEAK HID_Setup(Setup& setup)
192217
_hid_idle = setup.wValueL;
193218
return true;
194219
}
220+
221+
#if defined(HID_KEYBOARD_ENABLED) && defined(HID_KEYBOARD_LEDS_ENABLED)
222+
if (HID_SET_REPORT == r)
223+
{
224+
//TODO check correct report ID (not needed for now, no other device has an out report)
225+
if (setup.wLength == 2)
226+
{
227+
// write led out report data
228+
uint8_t data[2];
229+
if (2 == USB_RecvControl(data, 2))
230+
hid_keyboard_leds = data[1];
231+
}
232+
}
233+
#endif
195234
}
196235
return false;
197236
}
@@ -200,6 +239,8 @@ bool WEAK HID_Setup(Setup& setup)
200239
//================================================================================
201240
// Mouse
202241

242+
#ifdef HID_MOUSE_ENABLED
243+
203244
Mouse_::Mouse_(void) : _buttons(0)
204245
{
205246
}
@@ -256,10 +297,14 @@ bool Mouse_::isPressed(uint8_t b)
256297
return false;
257298
}
258299

300+
#endif
301+
259302
//================================================================================
260303
//================================================================================
261304
// Keyboard
262305

306+
#ifdef HID_KEYBOARD_ENABLED
307+
263308
Keyboard_::Keyboard_(void)
264309
{
265310
}
@@ -520,4 +565,6 @@ size_t Keyboard_::write(uint8_t c)
520565

521566
#endif
522567

568+
#endif
569+
523570
#endif /* if defined(USBCON) */

plugins/KeyboardioHID/HID_Tests/HID_Tests.ino

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ void loop() {
1515
}
1616

1717
delay(3000);
18+
Keyboard.write(KEY_CAPS_LOCK);
19+
Serial.println("Leds");
20+
Serial.println(Keyboard.getLEDs(), BIN);
21+
22+
Serial.println("Serial");
1823
Serial.println(Serial.dtr());
1924
Serial.println(Serial.rts());
2025
Serial.println(Serial.baud());

plugins/KeyboardioHID/USBAPI.h

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,29 @@ typedef unsigned long u32;
3434

3535
#if defined(USBCON)
3636

37+
// include needed HID devices
38+
// the more you activate, the more flash it will take
39+
// by default only Mouse + Keyboard (without LEDs) are activated
40+
#define HID_MOUSE_ENABLED
41+
#define HID_KEYBOARD_ENABLED
42+
//#define HID_KEYBOARD_LEDS_ENABLED
43+
44+
//#define HID_RAWHID_ENABLED (currently broken for USB-Core)
45+
//#define HID_MEDIA_ENABLED
46+
//#define HID_SYSTEM_ENABLED
47+
//#define HID_GAMEPAD_ENABLED
48+
49+
/** Enum for the HID report IDs used in the device. */
50+
typedef enum{
51+
HID_REPORTID_NotAReport = 0x00, /**< first entry is always zero for multireports */
52+
HID_REPORTID_MouseReport = 0x01, /**< Report ID for the Mouse report within the device. */
53+
HID_REPORTID_KeyboardReport = 0x02, /**< Report ID for the Keyboard report within the device. */
54+
HID_REPORTID_RawHidReport = 0x03, /**< Report ID for the Raw Hid report within the device. */
55+
HID_REPORTID_MediaReport = 0x04, /**< Report ID for the Media report within the device. */
56+
HID_REPORTID_SystemReport = 0x05, /**< Report ID for the Power report within the device. */
57+
HID_REPORTID_GamepadReport = 0x06, /**< Report ID for the Gamepad report within the device. */
58+
} HID_Report_IDs;
59+
3760
#include "USBDesc.h"
3861
#include "USBCore.h"
3962

@@ -102,10 +125,14 @@ extern Serial_ Serial;
102125
//================================================================================
103126
// Mouse
104127

105-
#define MOUSE_LEFT 1
106-
#define MOUSE_RIGHT 2
107-
#define MOUSE_MIDDLE 4
108-
#define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE)
128+
#ifdef HID_MOUSE_ENABLED
129+
130+
#define MOUSE_LEFT 0x01
131+
#define MOUSE_RIGHT 0x02
132+
#define MOUSE_MIDDLE 0x04
133+
#define MOUSE_PREV 0x08
134+
#define MOUSE_NEXT 0x10
135+
#define MOUSE_ALL (MOUSE_LEFT | MOUSE_RIGHT | MOUSE_MIDDLE | MOUSE_PREV | MOUSE_NEXT)
109136

110137
class Mouse_
111138
{
@@ -124,18 +151,29 @@ class Mouse_
124151
};
125152
extern Mouse_ Mouse;
126153

154+
#endif
155+
127156
//================================================================================
128157
//================================================================================
129158
// Keyboard
130159

160+
#ifdef HID_KEYBOARD_ENABLED
161+
162+
#define KEY_PRINT 0xCE
163+
#define KEY_NUM_LOCK 0xDB
164+
#define KEY_SCROLL_LOCK 0xCF
165+
#define KEY_PAUSE 0xD0
166+
131167
#define KEY_LEFT_CTRL 0x80
132168
#define KEY_LEFT_SHIFT 0x81
133169
#define KEY_LEFT_ALT 0x82
134170
#define KEY_LEFT_GUI 0x83
171+
#define KEY_LEFT_WINDOWS KEY_LEFT_GUI
135172
#define KEY_RIGHT_CTRL 0x84
136173
#define KEY_RIGHT_SHIFT 0x85
137174
#define KEY_RIGHT_ALT 0x86
138175
#define KEY_RIGHT_GUI 0x87
176+
#define KEY_RIGHT_WINDOWS KEY_RIGHT_GUI
139177

140178
#define KEY_UP_ARROW 0xDA
141179
#define KEY_DOWN_ARROW 0xD9
@@ -165,6 +203,15 @@ extern Mouse_ Mouse;
165203
#define KEY_F11 0xCC
166204
#define KEY_F12 0xCD
167205

206+
#define LED_NUM_LOCK 0x01
207+
#define LED_CAPS_LOCK 0x02
208+
#define LED_SCROLL_LOCK 0x04
209+
210+
#if defined USBCON && defined(HID_KEYBOARD_ENABLED) && defined(HID_KEYBOARD_LEDS_ENABLED)
211+
// extern accessible led out report
212+
extern uint8_t hid_keyboard_leds;
213+
#endif
214+
168215
// Low level key report: up to 6 keys and shift, ctrl etc at once
169216
typedef struct
170217
{
@@ -186,9 +233,15 @@ class Keyboard_ : public Print
186233
virtual size_t press(uint8_t k);
187234
virtual size_t release(uint8_t k);
188235
virtual void releaseAll(void);
236+
237+
#if defined(HID_KEYBOARD_LEDS_ENABLED)
238+
inline uint8_t getLEDs(void){ return hid_keyboard_leds; }
239+
#endif
189240
};
190241
extern Keyboard_ Keyboard;
191242

243+
#endif
244+
192245
//================================================================================
193246
//================================================================================
194247
// Low level API

0 commit comments

Comments
 (0)