Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

UsbMouse.hxx 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. resetStatus();
  52. }
  53. bool UsbMouseDevice::isUsbReady()
  54. {
  55. UsbMouse.update();
  56. return usbInterruptIsReady();
  57. }
  58. void UsbMouseDevice::move(char dx, char dy)
  59. {
  60. reportBuffer.dx += dx;
  61. reportBuffer.dy += dy;
  62. updateStatus();
  63. }
  64. void UsbMouseDevice::moveWheel(char dy)
  65. {
  66. reportBuffer.dWheel += dy;
  67. updateStatus();
  68. }
  69. void UsbMouseDevice::pressButton(char button)
  70. {
  71. reportBuffer.buttonMask |= button;
  72. updateStatus();
  73. }
  74. void UsbMouseDevice::releaseButton(char button)
  75. {
  76. reportBuffer.buttonMask &= ~button;
  77. updateStatus();
  78. }
  79. void UsbMouseDevice::setButtonState(char button, bool pressed)
  80. {
  81. if (pressed)
  82. {
  83. pressButton(button);
  84. }
  85. else
  86. {
  87. releaseButton(button);
  88. }
  89. }
  90. /*void UsbMouseDevice::clickButton(char button)
  91. {
  92. pressButton(button);
  93. while (!usbInterruptIsReady()) {
  94. }
  95. releaseButton(button);
  96. }*/
  97. void UsbMouseDevice::updateStatus()
  98. {
  99. usbSetInterrupt((unsigned char*)(void *)&reportBuffer, sizeof(reportBuffer));
  100. resetStatus();
  101. }
  102. void UsbMouseDevice::resetStatus()
  103. {
  104. reportBuffer.buttonMask = 0;
  105. reportBuffer.dx = 0;
  106. reportBuffer.dy = 0;
  107. reportBuffer.dWheel = 0;
  108. }
  109. usbMsgLen_t usbFunctionSetup(uchar data[8])
  110. {
  111. usbRequest_t *rq = (usbRequest_t*)(void*)data;
  112. /* The following requests are never used. But since they are required by
  113. * the specification, we implement them in this example.
  114. */
  115. if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
  116. if(rq->bRequest == USBRQ_HID_GET_REPORT){ /* wValue: ReportType (highbyte), ReportID (lowbyte) */
  117. /* we only have one report type, so don't look at wValue */
  118. usbMsgPtr = (unsigned char*)(void *)&UsbMouse.reportBuffer;
  119. return sizeof(UsbMouse.reportBuffer);
  120. }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
  121. usbMsgPtr = &idleRate;
  122. return 1;
  123. }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
  124. idleRate = rq->wValue.bytes[1];
  125. }
  126. }else{
  127. /* no vendor specific requests implemented */
  128. }
  129. return 0; /* default for not implemented requests: return no data back to host */
  130. }