Skip to content

Commit 3d61f6e

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 b0bdb47 commit 3d61f6e

File tree

2 files changed

+44
-15
lines changed

2 files changed

+44
-15
lines changed

libraries/HID/HID.cpp

+31-8
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)

libraries/HID/HID.h

+13-7
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

0 commit comments

Comments
 (0)