1
0
mirror of https://github.com/arduino/Arduino.git synced 2024-12-01 12:24:14 +01:00

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!
This commit is contained in:
Martino Facchin 2015-07-02 12:12:15 +02:00 committed by Cristian Maglie
parent 6a9568d3e1
commit e1a0350062
6 changed files with 83 additions and 52 deletions

View File

@ -44,6 +44,9 @@ static u8 HID_INTERFACE;
HIDDescriptor _hidInterface;
static HIDDescriptorListNode* rootNode = NULL;
static uint8_t sizeof_hidReportDescriptor = 0;
static uint8_t modules_count = 0;
//================================================================================
//================================================================================
// Driver
@ -54,18 +57,45 @@ u8 _hid_idle = 1;
int HID_GetInterface(u8* interfaceNum)
{
interfaceNum[0] += 1; // uses 1
_hidInterface =
{
D_INTERFACE(HID_INTERFACE,1,3,0,0),
D_HIDREPORT(sizeof_hidReportDescriptor),
D_ENDPOINT(USB_ENDPOINT_IN (HID_ENDPOINT_INT),USB_ENDPOINT_TYPE_INTERRUPT,0x40,0x01)
};
return USB_SendControl(0,&_hidInterface,sizeof(_hidInterface));
}
int HID_GetDescriptor(int8_t t)
{
if (HID_REPORT_DESCRIPTOR_TYPE == t) {
return USB_SendControl(TRANSFER_PGM,_hidReportDescriptor,getsizeof_hidReportDescriptor());
HIDDescriptorListNode* current = rootNode;
int total = 0;
while(current != NULL) {
total += USB_SendControl(TRANSFER_PGM,current->cb->descriptor,current->cb->length);
current = current->next;
}
return total;
} else {
return 0;
}
}
void HID_::AppendDescriptor(HIDDescriptorListNode *node)
{
if (modules_count == 0) {
rootNode = node;
} else {
HIDDescriptorListNode *current = rootNode;
while(current->next != NULL) {
current = current->next;
}
current->next = node;
}
modules_count++;
sizeof_hidReportDescriptor += node->cb->length;
}
void HID_::SendReport(u8 id, const void* data, int len)
{
USB_Send(HID_TX, &id, 1);
@ -129,13 +159,6 @@ HID_::HID_(void)
static PUSBListNode node(&cb);
HID_ENDPOINT_INT = PUSB_AddFunction(&node, &HID_INTERFACE);
_hidInterface =
{
D_INTERFACE(HID_INTERFACE,1,3,0,0),
D_HIDREPORT(getsizeof_hidReportDescriptor()),
D_ENDPOINT(USB_ENDPOINT_IN (HID_ENDPOINT_INT),USB_ENDPOINT_TYPE_INTERRUPT,0x40,0x01)
};
}
int HID_::begin(void)

View File

@ -42,16 +42,27 @@
#define HID_REPORT_DESCRIPTOR_TYPE 0x22
#define HID_PHYSICAL_DESCRIPTOR_TYPE 0x23
typedef struct __attribute__((packed)) {
u8 length;
const void* descriptor;
} HID_Descriptor;
class HIDDescriptorListNode {
public:
HIDDescriptorListNode *next = NULL;
const HID_Descriptor * cb;
HIDDescriptorListNode(const HID_Descriptor *ncb) {cb = ncb;}
};
class HID_
{
public:
HID_(void);
int begin(void);
void SendReport(uint8_t id, const void* data, int len);
void AppendDescriptor(HIDDescriptorListNode* node);
};
extern HID_ HID;
typedef struct
{
u8 len; // 9
@ -77,11 +88,6 @@ typedef struct
#define D_HIDREPORT(_descriptorLength) \
{ 9, 0x21, 0x1, 0x1, 0, 1, 0x22, _descriptorLength, 0 }
extern const u8 _hidReportDescriptor[] PROGMEM;
// MUST be declared by the module
size_t getsizeof_hidReportDescriptor();
#define WEAK __attribute__ ((weak))
#endif

View File

@ -21,33 +21,13 @@
#if 1
#include "HID.h"
#include "Keyboard.h"
//================================================================================
//================================================================================
// Keyboard
Keyboard_ Keyboard;
Keyboard_::Keyboard_(void)
{
}
void Keyboard_::begin(void)
{
}
void Keyboard_::end(void)
{
}
void Keyboard_::sendReport(KeyReport* keys)
{
HID.SendReport(2,keys,sizeof(KeyReport));
}
const u8 _hidReportDescriptor[] PROGMEM = {
static const u8 _hidReportDescriptor[] PROGMEM = {
// Keyboard
0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 47
@ -80,6 +60,29 @@ const u8 _hidReportDescriptor[] PROGMEM = {
0xc0, // END_COLLECTION
};
Keyboard_::Keyboard_(void)
{
static HID_Descriptor cb = {
.length = sizeof(_hidReportDescriptor),
.descriptor = _hidReportDescriptor,
};
static HIDDescriptorListNode node(&cb);
HID.AppendDescriptor(&node);
}
void Keyboard_::begin(void)
{
}
void Keyboard_::end(void)
{
}
void Keyboard_::sendReport(KeyReport* keys)
{
HID.SendReport(2,keys,sizeof(KeyReport));
}
extern
const uint8_t _asciimap[128] PROGMEM;
@ -217,9 +220,6 @@ const uint8_t _asciimap[128] =
0 // DEL
};
size_t getsizeof_hidReportDescriptor() {
return sizeof(_hidReportDescriptor);
}
uint8_t USBPutChar(uint8_t c);

View File

@ -22,7 +22,7 @@
#ifndef KEYBOARD_h
#define KEYBOARD_h
#if defined(_USING_HID)
#if 0 //defined(_USING_HID)
#error "Can only attach one submodule to HID module"
@ -90,10 +90,10 @@ public:
Keyboard_(void);
void begin(void);
void end(void);
virtual size_t write(uint8_t k);
virtual size_t press(uint8_t k);
virtual size_t release(uint8_t k);
virtual void releaseAll(void);
size_t write(uint8_t k);
size_t press(uint8_t k);
size_t release(uint8_t k);
void releaseAll(void);
};
extern Keyboard_ Keyboard;
extern HID_ HID;

View File

@ -23,7 +23,7 @@
#include "Mouse.h"
const u8 _hidReportDescriptor[] PROGMEM = {
static const u8 _hidReportDescriptor[] PROGMEM = {
// Mouse
0x05, 0x01, // USAGE_PAGE (Generic Desktop) // 54
@ -56,18 +56,18 @@ const u8 _hidReportDescriptor[] PROGMEM = {
0xc0, // END_COLLECTION
};
size_t getsizeof_hidReportDescriptor() {
return sizeof(_hidReportDescriptor);
}
Mouse_ Mouse;
//================================================================================
//================================================================================
// Mouse
Mouse_::Mouse_(void) : _buttons(0)
{
const static HID_Descriptor cb = {
.length = sizeof(_hidReportDescriptor),
.descriptor = _hidReportDescriptor,
};
static HIDDescriptorListNode node(&cb);
HID.AppendDescriptor(&node);
}
void Mouse_::begin(void)
@ -122,4 +122,6 @@ bool Mouse_::isPressed(uint8_t b)
return false;
}
Mouse_ Mouse;
#endif

View File

@ -22,7 +22,7 @@
#ifndef MOUSE_h
#define MOUSE_h
#if defined(_USING_HID)
#if 0 //defined(_USING_HID)
#error "Can only attach one submodule to HID module"