Skip to content

Added a rudimentary mechanism for reacting to messages from the host to the HID device. #6391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions hardware/arduino/avr/libraries/HID/src/HID.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,12 @@ int HID_::SendReport(uint8_t id, const void* data, int len)
return ret + ret2;
}

int HID_::RegisterReportReceiver(uint8_t id, void(*reportReceiver)(const void *data, int len))
{
this->receiverId = id;
this->reportReceiver = reportReceiver;
}

bool HID_::setup(USBSetup& setup)
{
if (pluggedInterface != setup.wIndex) {
Expand Down Expand Up @@ -133,13 +139,23 @@ bool HID_::setup(USBSetup& setup)
}
if (request == HID_SET_REPORT)
{
//uint8_t reportID = setup.wValueL;
//uint16_t length = setup.wLength;
//uint8_t data[length];
if (this->reportReceiver == NULL)
{
return false;
}
uint8_t reportID = setup.wValueL;
uint16_t length = setup.wLength;
uint8_t data[16];
// Make sure to not read more data than USB_EP_SIZE.
// You can read multiple times through a loop.
// The first byte (may!) contain the reportID on a multreport.
//USB_RecvControl(data, length);
int numRead = USB_RecvControl(data, length);
if (numRead < 1 || data[0] != this->receiverId )
{
return false;
}

(*this->reportReceiver)(data + 1, length - 1);
}
}

Expand All @@ -148,7 +164,8 @@ bool HID_::setup(USBSetup& setup)

HID_::HID_(void) : PluggableUSBModule(1, 1, epType),
rootNode(NULL), descriptorSize(0),
protocol(HID_REPORT_PROTOCOL), idle(1)
protocol(HID_REPORT_PROTOCOL), idle(1),
receiverId(0), reportReceiver(NULL)
{
epType[0] = EP_TYPE_INTERRUPT_IN;
PluggableUSB().plug(this);
Expand Down
7 changes: 7 additions & 0 deletions hardware/arduino/avr/libraries/HID/src/HID.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ class HID_ : public PluggableUSBModule
int SendReport(uint8_t id, const void* data, int len);
void AppendDescriptor(HIDSubDescriptor* node);

// Registers a callback for when a report with the given id occurs. You can only have
// one callback registered. If you need to un-register a callback, supply 0 and NULL
// to this method.
int RegisterReportReceiver(uint8_t id, void(*reportReceiver)(const void *data, int len));

protected:
// Implementation of the PluggableUSBModule
int getInterface(uint8_t* interfaceCount);
Expand All @@ -111,6 +116,8 @@ class HID_ : public PluggableUSBModule

uint8_t protocol;
uint8_t idle;
uint8_t receiverId;
void(*reportReceiver)(const void *data, int len);
};

// Replacement for global singleton.
Expand Down