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.

UsbMouse.hxx 4.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. //
  2. // Created by robin on 1/8/16.
  3. //
  4. UsbMouseDevice UsbMouse = UsbMouseDevice();
  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. UsbMouseDevice::UsbMouseDevice()
  43. {
  44. PORTD = 0; // TODO: Only for USB pins?
  45. DDRD |= ~USBMASK;
  46. cli();
  47. usbDeviceDisconnect();
  48. usbDeviceConnect();
  49. usbInit();
  50. sei();
  51. reportBuffer.buttonMask = 0;
  52. reportBuffer.dx = 0;
  53. reportBuffer.dy = 0;
  54. reportBuffer.dWheel = 0;
  55. }
  56. bool UsbMouseDevice::isUsbReady()
  57. {
  58. UsbMouse.update();
  59. return usbInterruptIsReady();
  60. }
  61. void UsbMouseDevice::move(char dx, char dy)
  62. {
  63. reportBuffer.dx += dx;
  64. reportBuffer.dy += dy;
  65. updateStatus();
  66. }
  67. void UsbMouseDevice::updateStatus()
  68. {
  69. usbSetInterrupt((unsigned char*)(void *)&reportBuffer, sizeof(reportBuffer));
  70. reportBuffer.buttonMask = 0;
  71. reportBuffer.dx = 0;
  72. reportBuffer.dy = 0;
  73. reportBuffer.dWheel = 0;
  74. }
  75. usbMsgLen_t usbFunctionSetup(uchar data[8])
  76. {
  77. usbRequest_t *rq = (usbRequest_t*)(void*)data;
  78. /* The following requests are never used. But since they are required by
  79. * the specification, we implement them in this example.
  80. */
  81. if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
  82. if(rq->bRequest == USBRQ_HID_GET_REPORT){ /* wValue: ReportType (highbyte), ReportID (lowbyte) */
  83. /* we only have one report type, so don't look at wValue */
  84. usbMsgPtr = (unsigned char*)(void *)&UsbMouse.reportBuffer;
  85. return sizeof(UsbMouse.reportBuffer);
  86. }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
  87. usbMsgPtr = &idleRate;
  88. return 1;
  89. }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
  90. idleRate = rq->wValue.bytes[1];
  91. }
  92. }else{
  93. /* no vendor specific requests implemented */
  94. }
  95. return 0; /* default for not implemented requests: return no data back to host */
  96. }