Skip to content

Commit 6046d88

Browse files
committed
[PUSB] callbacks are now pure virtual methods
This change allows the compiler to handle callbacks resolution. Callbacks now must be implemented on the class that extends PUSBListNode and this is forced by compiler by means of pure virtual methods. Also the calls to HID.interface() and HID.endpoint() can now be simplified to interface() and endpoint() respectively since the methods are no more static.
1 parent 597cd8d commit 6046d88

File tree

3 files changed

+18
-18
lines changed

3 files changed

+18
-18
lines changed

hardware/arduino/avr/cores/arduino/PluggableUSB.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
class PUSBListNode {
2929
public:
3030
PUSBListNode() { }
31-
bool (*setup)(USBSetup& setup, uint8_t i);
32-
int (*getInterface)(uint8_t* interfaceNum);
33-
int (*getDescriptor)(int8_t t);
3431
int8_t numEndpoints;
3532
int8_t numInterfaces;
3633
uint8_t *endpointType;
@@ -39,6 +36,10 @@ class PUSBListNode {
3936
inline int8_t endpoint() const { return pluggedEndpoint; }
4037

4138
protected:
39+
virtual bool setup(USBSetup& setup, uint8_t i) = 0;
40+
virtual int getInterface(uint8_t* interfaceNum) = 0;
41+
virtual int getDescriptor(int8_t t) = 0;
42+
4243
uint8_t pluggedInterface;
4344
int8_t pluggedEndpoint;
4445

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

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,19 +41,19 @@ uint8_t HID_::epType[] = { EP_TYPE_INTERRUPT_IN };
4141
uint8_t HID_::protocol = 1;
4242
uint8_t HID_::idle = 1;
4343

44-
int HID_::GetInterface(uint8_t* interfaceNum)
44+
int HID_::getInterface(uint8_t* interfaceNum)
4545
{
4646
interfaceNum[0] += 1; // uses 1
4747
hidInterface =
4848
{
49-
D_INTERFACE(HID.interface(), 1, 3, 0, 0),
49+
D_INTERFACE(interface(), 1, 3, 0, 0),
5050
D_HIDREPORT(sizeof_hidReportDescriptor),
51-
D_ENDPOINT(USB_ENDPOINT_IN(HID.endpoint()), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x01)
51+
D_ENDPOINT(USB_ENDPOINT_IN(endpoint()), USB_ENDPOINT_TYPE_INTERRUPT, USB_EP_SIZE, 0x01)
5252
};
5353
return USB_SendControl(0, &hidInterface, sizeof(hidInterface));
5454
}
5555

56-
int HID_::GetDescriptor(int8_t t)
56+
int HID_::getDescriptor(int8_t t)
5757
{
5858
if (HID_REPORT_DESCRIPTOR_TYPE == t) {
5959
HIDDescriptorListNode* current = rootNode;
@@ -85,13 +85,13 @@ void HID_::AppendDescriptor(HIDDescriptorListNode *node)
8585

8686
void HID_::SendReport(uint8_t id, const void* data, int len)
8787
{
88-
USB_Send(HID.endpoint(), &id, 1);
89-
USB_Send(HID.endpoint() | TRANSFER_RELEASE,data,len);
88+
USB_Send(endpoint(), &id, 1);
89+
USB_Send(endpoint() | TRANSFER_RELEASE,data,len);
9090
}
9191

92-
bool HID_::Setup(USBSetup& setup, uint8_t i)
92+
bool HID_::setup(USBSetup& setup, uint8_t i)
9393
{
94-
if (HID.interface() != i) {
94+
if (interface() != i) {
9595
return false;
9696
} else {
9797
uint8_t r = setup.bRequest;
@@ -130,9 +130,6 @@ bool HID_::Setup(USBSetup& setup, uint8_t i)
130130

131131
HID_::HID_(void)
132132
{
133-
setup = HID_::Setup;
134-
getInterface = HID_::GetInterface;
135-
getDescriptor = HID_::GetDescriptor;
136133
numEndpoints = 1;
137134
numInterfaces = 1;
138135
endpointType = epType;

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -80,11 +80,13 @@ class HID_ : public PUSBListNode
8080
void SendReport(uint8_t id, const void* data, int len);
8181
void AppendDescriptor(HIDDescriptorListNode* node);
8282

83-
private:
84-
static int GetInterface(uint8_t* interfaceNum);
85-
static int GetDescriptor(int8_t t);
86-
static bool Setup(USBSetup& setup, uint8_t i);
83+
protected:
84+
// Implementation of the PUSBListNode
85+
int getInterface(uint8_t* interfaceNum);
86+
int getDescriptor(int8_t t);
87+
bool setup(USBSetup& setup, uint8_t i);
8788

89+
private:
8890
static HIDDescriptor hidInterface;
8991

9092
static HIDDescriptorListNode* rootNode;

0 commit comments

Comments
 (0)