Nessuna descrizione
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.

UsbJoystick.h 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Based on Obdev's AVRUSB code and under the same license.
  3. *
  4. * UsbJoystick.h is a library for Arduino that wraps around the AVRUSB driver from ObDev
  5. *
  6. * Adapted from the UsbKeyboard.h library from Phil Lindsay
  7. *
  8. * by Michel Gutlich
  9. *
  10. */
  11. #ifndef __UsbJoystick_h__
  12. #define __UsbJoystick_h__
  13. #include <avr/pgmspace.h>
  14. #include "usbdrv.h"
  15. #include <avr/interrupt.h>
  16. #include <string.h>
  17. // typedef uint8_t byte; // avoid int() issue
  18. static uchar reportBuffer[8]; /* buffer for HID reports */
  19. static uchar idleRate; // in 4 ms units
  20. PROGMEM const char usbHidReportDescriptor[USB_CFG_HID_REPORT_DESCRIPTOR_LENGTH] = { // USB report descriptor for a 6-axis, 4 button joystick
  21. 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
  22. 0x09, 0x04, // USAGE (Joystick)
  23. 0xa1, 0x01, // COLLECTION (Application)
  24. 0x09, 0x30, // USAGE (X)
  25. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  26. 0x26, 0xff, 0x03, // Logical Maximum (1024)
  27. 0x75, 0x0a, // REPORT_SIZE (10)
  28. 0x95, 0x01, // REPORT_COUNT (1)
  29. 0x81, 0x02, // INPUT (Data,Var,Abs)
  30. 0x09, 0x31, // USAGE (Y)
  31. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  32. 0x26, 0xff, 0x03, // Logical Maximum (1024)
  33. 0x75, 0x0a, // REPORT_SIZE (10)
  34. 0x95, 0x01, // REPORT_COUNT (1)
  35. 0x81, 0x02, // INPUT (Data,Var,Abs)
  36. 0x09, 0x32, // USAGE (Z)
  37. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  38. 0x26, 0xff, 0x03, // Logical Maximum (1024)
  39. 0x75, 0x0a, // REPORT_SIZE (10)
  40. 0x95, 0x01, // REPORT_COUNT (1)
  41. 0x81, 0x02, // INPUT (Data,Var,Abs)
  42. 0x09, 0x33, // USAGE (Rotate X)
  43. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  44. 0x26, 0xff, 0x03, // Logical Maximum (1024)
  45. 0x75, 0x0a, // REPORT_SIZE (10)
  46. 0x95, 0x01, // REPORT_COUNT (1)
  47. 0x81, 0x02, // INPUT (Data,Var,Abs)
  48. 0x09, 0x34, // USAGE (Rotate Y)
  49. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  50. 0x26, 0xff, 0x03, // Logical Maximum (1024)
  51. 0x75, 0x0a, // REPORT_SIZE (10)
  52. 0x95, 0x01, // REPORT_COUNT (1)
  53. 0x81, 0x02, // INPUT (Data,Var,Abs)
  54. 0x09, 0x35, // USAGE (Rotate Z)
  55. 0x15, 0x00, // LOGICAL_MINIMUM (0)
  56. 0x26, 0xff, 0x03, // Logical Maximum (1024)
  57. 0x75, 0x0a, // REPORT_SIZE (10)
  58. 0x95, 0x01, // REPORT_COUNT (1)
  59. 0x81, 0x02, // INPUT (Data,Var,Abs)
  60. 0x05, 0x09, // Usage_Page (Button)
  61. 0x19, 0x01, // Usage_Minimum (Button 1)
  62. 0x29, 0x04, // Usage_Maximum (Button 4)
  63. 0x15, 0x00, // Logical_Minimum (0)
  64. 0x25, 0x01, // Logical_Maximum (1)
  65. 0x75, 0x01, // Report_Size (1)
  66. 0x95, 0x04, // Report_Count (4)
  67. 0x55, 0x00, // Unit_Exponent (0)
  68. 0x65, 0x00, // Unit (None)
  69. 0x81, 0x02, // Input (Data, Const, Abs)
  70. 0xc0 // END_COLLECTION
  71. };
  72. class UsbJoystickDevice {
  73. public:
  74. UsbJoystickDevice () {
  75. usbInit();
  76. sei();
  77. }
  78. void refresh() {
  79. usbPoll();
  80. }
  81. void sendJoystick(){
  82. sendJoystick(reportBuffer[0],reportBuffer[1],reportBuffer[2],reportBuffer[3],reportBuffer[4],reportBuffer[5],reportBuffer[6],reportBuffer[7]);
  83. }
  84. void sendJoystick(uchar val0, uchar val1, uchar val2, uchar val3, uchar val4 , uchar val5 , uchar val6, uchar val7) {
  85. reportBuffer[0] = val0;
  86. reportBuffer[1] = val1;
  87. reportBuffer[2] = val2;
  88. reportBuffer[3] = val3;
  89. reportBuffer[4] = val4;
  90. reportBuffer[5] = val5;
  91. reportBuffer[6] = val6;
  92. reportBuffer[7] = val7;
  93. if(usbInterruptIsReady()) {
  94. usbSetInterrupt(reportBuffer, sizeof(reportBuffer));
  95. }
  96. }
  97. //private: TODO: Make friend?
  98. uchar reportBuffer[8]; // buffer for HID reports
  99. };
  100. UsbJoystickDevice UsbJoystick = UsbJoystickDevice();
  101. #ifdef __cplusplus
  102. extern "C"{
  103. #endif
  104. // USB_PUBLIC uchar usbFunctionSetup
  105. uchar usbFunctionSetup(uchar data[8])
  106. {
  107. usbRequest_t *rq = (usbRequest_t *)((void *)data);
  108. usbMsgPtr = UsbJoystick.reportBuffer; //
  109. if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
  110. if(rq->bRequest == USBRQ_HID_GET_REPORT){ /* wValue: ReportType (highbyte), ReportID (lowbyte) */
  111. reportBuffer[0]= 0;
  112. reportBuffer[1]= 0;
  113. reportBuffer[2]= 0;
  114. reportBuffer[3]= 0;
  115. reportBuffer[4]= 0;
  116. reportBuffer[5]= 0;
  117. reportBuffer[6]= 0;
  118. reportBuffer[7]= 0;
  119. return sizeof(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. }else{
  126. /* no vendor specific requests implemented */
  127. }
  128. return 0;
  129. }
  130. }
  131. #ifdef __cplusplus
  132. } // extern "C"
  133. #endif
  134. #endif // __UsbJoystick_h__