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 2.7KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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[32] = {
  14. 0x06, 0x42, 0xff,
  15. 0x0A, 0x42, 0xff,
  16. 0xA1, 0x01, // Collection 0x01
  17. 0x75, 0x08, // report size = 8 bits
  18. 0x15, 0x00, // logical minimum = 0
  19. 0x26, 0xFF, 0x00, // logical maximum = 255
  20. 0x95, RAWHID_TX_SIZE, // report count
  21. 0x09, 0x01, // usage
  22. 0x81, 0x02, // Input (array)
  23. 0x95, RAWHID_RX_SIZE, // report count
  24. 0x09, 0x02, // usage
  25. 0x91, 0x02, // Output (array)
  26. 0xC0 // end collection
  27. };
  28. UsbRawHidDevice::UsbRawHidDevice()
  29. {
  30. PORTD = 0; // TODO: Only for USB pins?
  31. DDRD |= ~USBMASK;
  32. cli();
  33. usbDeviceDisconnect();
  34. usbDeviceConnect();
  35. usbInit();
  36. sei();
  37. //sendData("", 0);
  38. }
  39. bool UsbRawHidDevice::isUsbReady()
  40. {
  41. update();
  42. return usbInterruptIsReady();
  43. }
  44. void UsbRawHidDevice::sendData(const char* data, unsigned size)
  45. {
  46. if (size > RAWHID_TX_SIZE || size == 0)
  47. {
  48. size = RAWHID_TX_SIZE;
  49. }
  50. memcpy(reportBuffer, data, size);
  51. memset(reportBuffer + size, 0, RAWHID_TX_SIZE - size);
  52. usbSetInterrupt((unsigned char*)(void *)&reportBuffer, sizeof(reportBuffer));
  53. }
  54. usbMsgLen_t usbFunctionSetup(uchar data[8])
  55. {
  56. usbRequest_t *rq = (usbRequest_t*)(void*)data;
  57. /* The following requests are never used. But since they are required by
  58. * the specification, we implement them in this example.
  59. */
  60. if((rq->bmRequestType & USBRQ_TYPE_MASK) == USBRQ_TYPE_CLASS){ /* class request type */
  61. if(rq->bRequest == USBRQ_HID_GET_REPORT){ /* wValue: ReportType (highbyte), ReportID (lowbyte) */
  62. /* we only have one report type, so don't look at wValue */
  63. usbMsgPtr = (unsigned char*)(void *)&UsbRawHid.reportBuffer;
  64. return sizeof(UsbRawHid.reportBuffer);
  65. }else if(rq->bRequest == USBRQ_HID_GET_IDLE){
  66. usbMsgPtr = &idleRate;
  67. return 1;
  68. }else if(rq->bRequest == USBRQ_HID_SET_IDLE){
  69. idleRate = rq->wValue.bytes[1];
  70. }
  71. }else{
  72. /* no vendor specific requests implemented */
  73. }
  74. return 0; /* default for not implemented requests: return no data back to host */
  75. }