📄 hiddfunctiondriver.c
字号:
// Restart transfer
USBD_Read(HIDD_Descriptors_INTERRUPTOUT,
&(hiddKeyboardDriver.outputReport),
sizeof(HIDDKeyboardOutputReport),
(TransferCallback) HIDD_ReportReceived,
0); // No argument for callback function
}
//-----------------------------------------------------------------------------
/// Retrieves the new value of a report from the host and saves it.
/// \param type Report type.
/// \param length Report length.
//-----------------------------------------------------------------------------
static void HIDD_SetReport(unsigned char type,
unsigned short length)
{
TRACE_INFO_WP("sReport ");
// Check report type
switch (type) {
case HIDReportRequest_INPUT:
// SET_REPORT requests on input reports are ignored
USBD_Stall(0);
break;
case HIDReportRequest_OUTPUT:
// Check report length
if (length != sizeof(HIDDKeyboardOutputReport)) {
USBD_Stall(0);
}
else {
USBD_Read(0, // Endpoint #0
&(hiddKeyboardDriver.outputReport),
length,
(TransferCallback) HIDD_ReportReceived,
0); // No argument to the callback function
}
break;
default:
USBD_Stall(0);
}
}
//-----------------------------------------------------------------------------
// Exported functions
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
/// Initializes an USB HID keyboard function driver.
/// \param pUsbdDriver Pointer to the USB driver instance.
//-----------------------------------------------------------------------------
void HIDDFunctionDriver_Initialize(USBDDriver * pUsbdDriver)
{
hiddKeyboardDriver.inputReportIdleRate = 0;
HIDDKeyboardInputReport_Initialize(&(hiddKeyboardDriver.inputReport));
HIDDKeyboardOutputReport_Initialize(&(hiddKeyboardDriver.outputReport));
hiddKeyboardDriver.pUsbdDriver = pUsbdDriver;
}
//-----------------------------------------------------------------------------
/// Handles HID-specific SETUP request sent by the host.
/// \param request Pointer to a USBGenericRequest instance.
/// \return 0 if the request is Unsupported, 1 if the request handled.
//-----------------------------------------------------------------------------
unsigned char HIDDFunctionDriver_RequestHandler(
const USBGenericRequest *request)
{
TRACE_INFO_WP("NewReq ");
// Check if this is a standard request
if (USBGenericRequest_GetType(request) == USBGenericRequest_STANDARD) {
// This is a standard request
switch (USBGenericRequest_GetRequest(request)) {
case USBGenericRequest_GETDESCRIPTOR:
// Check if this is a HID descriptor, otherwise forward it to
// the standard driver
if (!HIDD_GetDescriptor(
USBGetDescriptorRequest_GetDescriptorType(request),
USBGenericRequest_GetLength(request))) {
USBDDriver_RequestHandler(hiddKeyboardDriver.pUsbdDriver,
request);
}
break;
default:
return 0;
}
}
// Check if this is a class request
else if (USBGenericRequest_GetType(request) == USBGenericRequest_CLASS) {
// This is a class-specific request
switch (USBGenericRequest_GetRequest(request)) {
case HIDGenericRequest_GETIDLE:
HIDD_GetIdle();
break;
case HIDGenericRequest_SETIDLE:
HIDD_SetIdle(HIDIdleRequest_GetIdleRate(request));
break;
case HIDGenericRequest_GETREPORT:
HIDD_GetReport(
HIDReportRequest_GetReportType(request),
USBGenericRequest_GetLength(request));
break;
case HIDGenericRequest_SETREPORT:
HIDD_SetReport(
HIDReportRequest_GetReportType(request),
USBGenericRequest_GetLength(request));
break;
default:
return 0;
}
}
return 1;
}
//-----------------------------------------------------------------------------
/// Invoked whenever the configuration of the device is changed by the host.
/// \param cfgnum Newly configuration number.
//-----------------------------------------------------------------------------
void HIDDFunctionCallbacks_ConfigurationChanged(unsigned char cfgnum)
{
if (cfgnum > 0) {
// Start receiving output reports
USBD_Read(HIDD_Descriptors_INTERRUPTOUT,
&(hiddKeyboardDriver.outputReport),
sizeof(HIDDKeyboardOutputReport),
(TransferCallback) HIDD_ReportReceived,
0); // No argument for callback function
}
}
//-----------------------------------------------------------------------------
/// Reports a change in which keys are currently pressed or release to the
/// host.
/// \param pressedKeys Pointer to an array of key codes indicating keys that
/// have been pressed since the last call to
/// <HIDDKeyboardDriver_ChangeKeys>.
/// \param pressedKeysSize Number of key codes in the pressedKeys array.
/// \param releasedKeys Pointer to an array of key codes indicates keys that
/// have been released since the last call to
/// <HIDDKeyboardDriver_ChangeKeys>.
/// \param releasedKeysSize Number of key codes in the releasedKeys array.
/// \return <USBD_STATUS_SUCCESS> if the report has been sent to the host;
/// otherwise an error code.
//-----------------------------------------------------------------------------
unsigned char HIDDKeyboardDriver_ChangeKeys(unsigned char *pressedKeys,
unsigned char pressedKeysSize,
unsigned char *releasedKeys,
unsigned char releasedKeysSize)
{
// Press keys
while (pressedKeysSize > 0) {
// Check if this is a standard or modifier key
if (HIDKeypad_IsModifierKey(*pressedKeys)) {
// Set the corresponding bit in the input report
HIDDKeyboardInputReport_PressModifierKey(
&(hiddKeyboardDriver.inputReport),
*pressedKeys);
}
else {
HIDDKeyboardInputReport_PressStandardKey(
&(hiddKeyboardDriver.inputReport),
*pressedKeys);
}
pressedKeysSize--;
pressedKeys++;
}
// Release keys
while (releasedKeysSize > 0) {
// Check if this is a standard or modifier key
if (HIDKeypad_IsModifierKey(*releasedKeys)) {
// Set the corresponding bit in the input report
HIDDKeyboardInputReport_ReleaseModifierKey(
&(hiddKeyboardDriver.inputReport),
*releasedKeys);
}
else {
HIDDKeyboardInputReport_ReleaseStandardKey(
&(hiddKeyboardDriver.inputReport),
*releasedKeys);
}
releasedKeysSize--;
releasedKeys++;
}
// Send input report through the interrupt IN endpoint
return USBD_Write(HIDD_Descriptors_INTERRUPTIN,
&(hiddKeyboardDriver.inputReport),
sizeof(HIDDKeyboardInputReport),
0,
0);
}
//-----------------------------------------------------------------------------
/// Starts a remote wake-up sequence if the host has explicitely enabled it
/// by sending the appropriate SET_FEATURE request.
//-----------------------------------------------------------------------------
void HIDDKeyboardDriver_RemoteWakeUp(void)
{
// Remote wake-up has been enabled
if (USBDDriver_IsRemoteWakeUpEnabled(hiddKeyboardDriver.pUsbdDriver)) {
USBD_RemoteWakeUp();
}
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -