You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UsbRawHid.hxx 3.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. //
  2. // Created by robin on 1/8/16.
  3. //
  4. UsbRawHidDevice UsbRawHid = UsbRawHidDevice();
  5. /* We use a simplifed keyboard report descriptor which does not support the
  6. * boot protocol. We don't allow setting status LEDs and but we do allow
  7. * simultaneous key presses.
  8. * The report descriptor has been created with usb.org's "HID Descriptor Tool"
  9. * which can be downloaded from http://www.usb.org/developers/hidpage/.
  10. * Redundant entries (such as LOGICAL_MINIMUM and USAGE_PAGE) have been omitted
  11. * for the second INPUT item.
  12. */
  13. PROGMEM const char usbHidReportDescriptor[52] = { /* USB report descriptor, size must match usbconfig.h */
  14. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  15. 0x09, 0x02, // USAGE (Mouse)
  16. 0xa1, 0x01, // COLLECTION (Application)
  17. 0x09, 0x01, // USAGE (Pointer)
  18. 0xA1, 0x00, // COLLECTION (Physical)
  19. 0x05, 0x09, // USAGE_PAGE (Button)
  20. 0x19, 0x01, // USAGE_MINIMUM
  21. 0x29, 0x03, // USAGE_MAXIMUM
  22. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  23. 0x25, 0x01, // LOGICAL_MAXIMUM (1)
  24. 0x95, 0x03, // REPORT_COUNT (3)
  25. 0x75, 0x01, // REPORT_SIZE (1)
  26. 0x81, 0x02, // INPUT (Data,Var,Abs)
  27. 0x95, 0x01, // REPORT_COUNT (1)
  28. 0x75, 0x05, // REPORT_SIZE (5)
  29. 0x81, 0x03, // INPUT (Const,Var,Abs)
  30. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  31. 0x09, 0x30, // USAGE (X)
  32. 0x09, 0x31, // USAGE (Y)
  33. 0x09, 0x38, // USAGE (Wheel)
  34. 0x15, 0x81, // LOGICAL_MINIMUM (-127)
  35. 0x25, 0x7F, // LOGICAL_MAXIMUM (127)
  36. 0x75, 0x08, // REPORT_SIZE (8)
  37. 0x95, 0x03, // REPORT_COUNT (3)
  38. 0x81, 0x06, // INPUT (Data,Var,Rel)
  39. 0xC0, // END_COLLECTION
  40. 0xC0, // END COLLECTION
  41. };
  42. UsbRawHidDevice::UsbRawHidDevice()
  43. {
  44. PORTD = 0; // TODO: Only for USB pins?
  45. DDRD |= ~USBMASK;
  46. cli();
  47. usbDeviceDisconnect();
  48. usbDeviceConnect();
  49. usbInit();
  50. sei();
  51. }
  52. bool UsbRawHidDevice::isUsbReady()
  53. {
  54. update();
  55. return usbInterruptIsReady();
  56. }
  57. usbMsgLen_t usbFunctionSetup(uchar data[8])
  58. {
  59. usbRequest_t *rq = (usbRequest_t*)(void*)data;
  60. /* The following requests are never used. But since they are required by
  61. * the specification, we implement them in this example.
  62. */
  63. if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
  64. if(rq->bRequest == USBRQ_HID_GET_REPORT){ /* wValue: ReportType (highbyte), ReportID (lowbyte) */
  65. /* we only have one report type, so don't look at wValue */
  66. usbMsgPtr = (unsigned char*)(void *)&UsbRawHid.reportBuffer;
  67. return sizeof(UsbRawHid.reportBuffer);
  68. }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
  69. usbMsgPtr = &idleRate;
  70. return 1;
  71. }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
  72. idleRate = rq->wValue.bytes[1];
  73. }
  74. }else{
  75. /* no vendor specific requests implemented */
  76. }
  77. return 0; /* default for not implemented requests: return no data back to host */
  78. }