Skip to content

Commit e1a0350

Browse files
facchinmcmaglie
authored andcommitted
allow HID submodules to create runtime descriptors
with this PR you can add \#include Keyboard.h \#include Mouse.h \#include HID.h in the top of the sketch and you will expose a Mouse+Keyboard From the library pow, simply add static HID_Descriptor cb = { .length = sizeof(_hidReportDescriptor), .descriptor = _hidReportDescriptor, }; static HIDDescriptorListNode node(&cb); HID.AppendDescriptor(&node); in the class' constructor and you are done!
1 parent 6a9568d commit e1a0350

File tree

6 files changed

+83
-52
lines changed

6 files changed

+83
-52
lines changed

hardware/arduino/avr/libraries/HID/HID.cpp

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ static u8 HID_INTERFACE;
4444

4545
HIDDescriptor _hidInterface;
4646

47+
static HIDDescriptorListNode* rootNode = NULL;
48+
static uint8_t sizeof_hidReportDescriptor = 0;
49+
static uint8_t modules_count = 0;
4750
//================================================================================
4851
//================================================================================
4952
// Driver
@@ -54,18 +57,45 @@ u8 _hid_idle = 1;
5457
int HID_GetInterface(u8* interfaceNum)
5558
{
5659
interfaceNum[0] += 1; // uses 1
60+
_hidInterface =
61+
{
62+
D_INTERFACE(HID_INTERFACE,1,3,0,0),
63+
D_HIDREPORT(sizeof_hidReportDescriptor),
64+
D_ENDPOINT(USB_ENDPOINT_IN (HID_ENDPOINT_INT),USB_ENDPOINT_TYPE_INTERRUPT,0x40,0x01)
65+
};
5766
return USB_SendControl(0,&_hidInterface,sizeof(_hidInterface));
5867
}
5968

6069
int HID_GetDescriptor(int8_t t)
6170
{
6271
if (HID_REPORT_DESCRIPTOR_TYPE == t) {
63-
return USB_SendControl(TRANSFER_PGM,_hidReportDescriptor,getsizeof_hidReportDescriptor());
72+
HIDDescriptorListNode* current = rootNode;
73+
int total = 0;
74+
while(current != NULL) {
75+
total += USB_SendControl(TRANSFER_PGM,current->cb->descriptor,current->cb->length);
76+
current = current->next;
77+
}
78+
return total;
6479
} else {
6580
return 0;
6681
}
6782
}
6883

84+
void HID_::AppendDescriptor(HIDDescriptorListNode *node)
85+
{
86+
if (modules_count == 0) {
87+
rootNode = node;
88+
} else {
89+
HIDDescriptorListNode *current = rootNode;
90+
while(current->next != NULL) {
91+
current = current->next;
92+
}
93+
current->next = node;
94+
}
95+
modules_count++;
96+
sizeof_hidReportDescriptor += node->cb->length;
97+
}
98+
6999
void HID_::SendReport(u8 id, const void* data, int len)
70100
{
71101
USB_Send(HID_TX, &id, 1);
@@ -129,13 +159,6 @@ HID_::HID_(void)
129159
static PUSBListNode node(&cb);
130160

131161
HID_ENDPOINT_INT = PUSB_AddFunction(&node, &HID_INTERFACE);
132-
133-
_hidInterface =
134-
{
135-
D_INTERFACE(HID_INTERFACE,1,3,0,0),
136-
D_HIDREPORT(getsizeof_hidReportDescriptor()),
137-
D_ENDPOINT(USB_ENDPOINT_IN (HID_ENDPOINT_INT),USB_ENDPOINT_TYPE_INTERRUPT,0x40,0x01)
138-
};
139162
}
140163

141164
int HID_::begin(void)

hardware/arduino/avr/libraries/HID/HID.h

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,27 @@
4242
#define HID_REPORT_DESCRIPTOR_TYPE 0x22
4343
#define HID_PHYSICAL_DESCRIPTOR_TYPE 0x23
4444

45+
typedef struct __attribute__((packed)) {
46+
u8 length;
47+
const void* descriptor;
48+
} HID_Descriptor;
49+
50+
class HIDDescriptorListNode {
51+
public:
52+
HIDDescriptorListNode *next = NULL;
53+
const HID_Descriptor * cb;
54+
HIDDescriptorListNode(const HID_Descriptor *ncb) {cb = ncb;}
55+
};
56+
4557
class HID_
4658
{
4759
public:
4860
HID_(void);
4961
int begin(void);
5062
void SendReport(uint8_t id, const void* data, int len);
63+
void AppendDescriptor(HIDDescriptorListNode* node);
5164
};
5265

53-
extern HID_ HID;
54-
5566
typedef struct
5667
{
5768
u8 len; // 9
@@ -77,11 +88,6 @@ typedef struct
7788
#define D_HIDREPORT(_descriptorLength) \
7889
{ 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 }
7990

80-
extern const u8 _hidReportDescriptor[] PROGMEM;
81-
82-
// MUST be declared by the module
83-
size_t getsizeof_hidReportDescriptor();
84-
8591
#define WEAK __attribute__ ((weak))
8692

8793
#endif

libraries/Keyboard/Keyboard.cpp

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -21,33 +21,13 @@
2121

2222
#if 1
2323

24-
#include "HID.h"
2524
#include "Keyboard.h"
2625

2726
//================================================================================
2827
//================================================================================
2928
// Keyboard
3029

31-
Keyboard_ Keyboard;
32-
33-
Keyboard_::Keyboard_(void)
34-
{
35-
}
36-
37-
void Keyboard_::begin(void)
38-
{
39-
}
40-
41-
void Keyboard_::end(void)
42-
{
43-
}
44-
45-
void Keyboard_::sendReport(KeyReport* keys)
46-
{
47-
HID.SendReport(2,keys,sizeof(KeyReport));
48-
}
49-
50-
const u8 _hidReportDescriptor[] PROGMEM = {
30+
static const u8 _hidReportDescriptor[] PROGMEM = {
5131

5232
// Keyboard
5333
0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 47
@@ -80,6 +60,29 @@ const u8 _hidReportDescriptor[] PROGMEM = {
8060
0xc0, // END_COLLECTION
8161
};
8262

63+
Keyboard_::Keyboard_(void)
64+
{
65+
static HID_Descriptor cb = {
66+
.length = sizeof(_hidReportDescriptor),
67+
.descriptor = _hidReportDescriptor,
68+
};
69+
static HIDDescriptorListNode node(&cb);
70+
HID.AppendDescriptor(&node);
71+
}
72+
73+
void Keyboard_::begin(void)
74+
{
75+
}
76+
77+
void Keyboard_::end(void)
78+
{
79+
}
80+
81+
void Keyboard_::sendReport(KeyReport* keys)
82+
{
83+
HID.SendReport(2,keys,sizeof(KeyReport));
84+
}
85+
8386
extern
8487
const uint8_t _asciimap[128] PROGMEM;
8588

@@ -217,9 +220,6 @@ const uint8_t _asciimap[128] =
217220
0 // DEL
218221
};
219222

220-
size_t getsizeof_hidReportDescriptor() {
221-
return sizeof(_hidReportDescriptor);
222-
}
223223

224224
uint8_t USBPutChar(uint8_t c);
225225

libraries/Keyboard/Keyboard.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#ifndef KEYBOARD_h
2323
#define KEYBOARD_h
2424

25-
#if defined(_USING_HID)
25+
#if 0 //defined(_USING_HID)
2626

2727
#error "Can only attach one submodule to HID module"
2828

@@ -90,10 +90,10 @@ class Keyboard_ : public Print
9090
Keyboard_(void);
9191
void begin(void);
9292
void end(void);
93-
virtual size_t write(uint8_t k);
94-
virtual size_t press(uint8_t k);
95-
virtual size_t release(uint8_t k);
96-
virtual void releaseAll(void);
93+
size_t write(uint8_t k);
94+
size_t press(uint8_t k);
95+
size_t release(uint8_t k);
96+
void releaseAll(void);
9797
};
9898
extern Keyboard_ Keyboard;
9999
extern HID_ HID;

libraries/Mouse/Mouse.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
#include "Mouse.h"
2525

26-
const u8 _hidReportDescriptor[] PROGMEM = {
26+
static const u8 _hidReportDescriptor[] PROGMEM = {
2727

2828
// Mouse
2929
0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 54
@@ -56,18 +56,18 @@ const u8 _hidReportDescriptor[] PROGMEM = {
5656
0xc0, // END_COLLECTION
5757
};
5858

59-
size_t getsizeof_hidReportDescriptor() {
60-
return sizeof(_hidReportDescriptor);
61-
}
62-
63-
Mouse_ Mouse;
64-
6559
//================================================================================
6660
//================================================================================
6761
// Mouse
6862

6963
Mouse_::Mouse_(void) : _buttons(0)
7064
{
65+
const static HID_Descriptor cb = {
66+
.length = sizeof(_hidReportDescriptor),
67+
.descriptor = _hidReportDescriptor,
68+
};
69+
static HIDDescriptorListNode node(&cb);
70+
HID.AppendDescriptor(&node);
7171
}
7272

7373
void Mouse_::begin(void)
@@ -122,4 +122,6 @@ bool Mouse_::isPressed(uint8_t b)
122122
return false;
123123
}
124124

125+
Mouse_ Mouse;
126+
125127
#endif

libraries/Mouse/Mouse.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#ifndef MOUSE_h
2323
#define MOUSE_h
2424

25-
#if defined(_USING_HID)
25+
#if 0 //defined(_USING_HID)
2626

2727
#error "Can only attach one submodule to HID module"
2828

0 commit comments

Comments
 (0)