// TITLE: USB HID Mouse device class driver
#include "usbdhidmouse.h"
#include <usb.h>
namespace usb{
// report descriptor
static const uint8_t ReportDesc[] = {
0x05,1, // UsagePage(GENERIC_DESKTOP)
0x09,2, // Usage(MOUSE),
0xA1,1, // Collection(APPLICATION)
0x09,1, // Usage(POINTER)
0xA1,0, // Collection(Physical)
// buttons
0x05,9, // UsagePage(BUTTONS)
0x19,1, // UsageMinimum(1)
0x29,3, // UsageMaximum(3)
0x15,0, // LogicalMinimum(0)
0x25,1, // LogicalMaximum(1)
0x75,1, // ReportSize(1)
0x95,3, // ReportCount(3)
0x81,2, // Input(VAR,ABS)
0x75,5, // ReportSize(5)
0x95,1, // ReportCount(1)
0x81,3, // Input(CONST|ARY|ABS)
// X and Y axis
0x05,1, // UsagePage(GENERIC_DESKTOP)
0x09,0x30, // Usage(X),
0x09,0x31, // Usage(Y),
0x15,(uint8_t)-127, // LogicalMinimum(-127)
0x25,127, // LogicalMaximum(127)
0x75,8, // ReportSize(8)
0x95,2, // ReportCount(2)
0x81,6, // Input(DATA|VAR|REL)
0xC0, // EndCollection
0xC0, // EndCollection
};
// Device Descriptor
static const uint8_t DeviceDesc[] = {
18, // Size of this structure.
1, // Type of this structure = DTYPE_DEVICE
USB2(0x110), // USB version 1.1
0, // USB Device Class
0, // USB Device Sub-class
0, // USB Device protocol
64, // Maximum packet size for default pipe.
USB2(0x1CBE), // Vendor ID = Texas Instruments
USB2(0), // Product ID = TI Mouse
USB2(0x100), // Device Version BCD.
1, // Manufacturer string identifier.
2, // Product string identifier.
0, // Product serial number.
1, // Number of configurations.
};
// configuration descriptor.
static const uint8_t ConfigDesc[] = {
9, // bLength
2, // USB_DTYPE_CONFIGURATION
USB2(9+9+9+7), // The total size of this full structure.
1, // bNumInterfaces
1, // bConfigurationValue
0, // iConfiguration
0xC0, // Bus Powered, Self Powered, remote wake up.
50, // The maximum power in 2mA increments.
// Interface Descriptor.
9, // Size of the interface descriptor.
4, //USB_DTYPE_INTERFACE, // Type of this descriptor.
0, // The index for this interface.
0, // The alternate setting for this interface.
1, // The number of endpoints used by this interface.
3, // USB_CLASS_HID, // interface class
0, // interface sub-class.
0, // interface protocol for the sub-class specified above.
0, // The string index for this interface.
// HID descriptor
9, // bLength
0x21, // bDescriptorType = DTYPE_HID
USB2(0x111), // bcdHID (version 1.11 compliant)
0, // bCountryCode (not localized)
1, // bNumDescriptors
0x22, // Report descriptor = DTYPE_REPORT
USB2(sizeof ReportDesc), // Size of report descriptor
// Interrupt IN endpoint descriptor
7, // The size of the endpoint descriptor.
5, //USB_DTYPE_ENDPOINT, // Descriptor type is an endpoint.
0x81, // EP1IN
0x03, // Endpoint is an interrupt endpoint.
USB2(64), // The maximum packet size.
16, // The polling interval for this endpoint.
};
bool tHIDMouseDevice::cbRequestHandler(const tRequest&rq) {
if (rq.wRequest() == 0x01A1 // GET_REPORT, class-specific, target=interface
&& rq.wIndexL == 0 // Interface 0
&& rq.wValueH == 0x01 // report_type = INPUT
&& rq.wValueL == 0) { // report_id
USBDevEndpointDataAck(USB_BASE, USB_EP_0, true);
SendDataEP0(inrep,3,rq);
return true;
}
return tHIDDevice::cbRequestHandler(rq); // chain down
}
bool tHIDMouseDevice::Init() {
desc.device = DeviceDesc;
desc.config = ConfigDesc;
desc.hid = ConfigDesc + 9+9;
desc.hidrep = ReportDesc;
return tHIDDevice::Init();
}
bool tHIDMouseDevice::StateChange(uint8_t Buttons, int8_t DeltaX, int8_t DeltaY) {
inrep[0]=Buttons;
inrep[1]=(uint8_t)DeltaX;
inrep[2]=(uint8_t)DeltaY;
return SendReport(inrep,3);
}
}
Vorgefundene Kodierung: UTF-8 | 0
|