diff --git a/README.adoc b/README.adoc index de7e68e..4e27fac 100644 --- a/README.adoc +++ b/README.adoc @@ -1,6 +1,8 @@ = USB Host Library for Arduino = -The USBHost library allows an Arduino Due board to appear as a USB host, enabling it to communicate with peripherals like USB mice, keyboards and HTC Vive trackers. +The USBHost library allows an Arduino Due board to appear as a USB host, enabling it to communicate with peripherals like USB mice, keyboards and HTC Vive trackers (requires at least tracker firmware v.20). + +For an USBHost library supporting Vive tracker for Arduino SAMD boards see [here](https://github.com/matzman666/USBHost-samd). For more information about this library please visit us at http://www.arduino.cc/en/Reference/USBHost diff --git a/examples/ViveTracker/ViveTracker.ino b/examples/ViveTracker/ViveTracker.ino index b3d0ae7..606558a 100644 --- a/examples/ViveTracker/ViveTracker.ino +++ b/examples/ViveTracker/ViveTracker.ino @@ -32,5 +32,5 @@ void loop() { uint16_t batteryLevel = 0; tracker.setTrackerStatus(buttons, padX, padY, trigger, batteryLevel); } - delay(10); // According to HTC's documentation the interval to send data should be at least 10 ms. + delay(300); } diff --git a/library.properties b/library.properties index 755bffd..0d21c90 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=USBHost -version=1.0.5 +version=1.0.6 author=Arduino maintainer=Arduino sentence=Allows the communication with USB peripherals like mice, keyboards, and thumbdrives. diff --git a/src/Usb.cpp b/src/Usb.cpp index ce74b48..7b35f11 100644 --- a/src/Usb.cpp +++ b/src/Usb.cpp @@ -723,23 +723,28 @@ void USBHost::Task(void) { case UHD_STATE_ERROR: // Illegal state + //Serial.write("UHD_STATE_ERROR\n"); usb_task_state = USB_DETACHED_SUBSTATE_ILLEGAL; lowspeed = 0; break; case UHD_STATE_DISCONNECTED: + //Serial.write("UHD_STATE_DISCONNECTED\n"); // Disconnected state if ((usb_task_state & USB_STATE_MASK) != USB_STATE_DETACHED) { + //Serial.write("UHD_STATE_DISCONNECTED2\n"); usb_task_state = USB_DETACHED_SUBSTATE_INITIALIZE; lowspeed = 0; } break; case UHD_STATE_CONNECTED: + //Serial.write("UHD_STATE_CONNECTED\n"); // Attached state if ((usb_task_state & USB_STATE_MASK) == USB_STATE_DETACHED) { + //Serial.write("UHD_STATE_CONNECTED2\n"); delay = millis() + USB_SETTLE_DELAY; usb_task_state = USB_ATTACHED_SUBSTATE_SETTLE; //FIXME TODO: lowspeed = 0 ou 1; already done by hardware? diff --git a/src/ViveTrackerController.h b/src/ViveTrackerController.h index fa7bb66..cdc05b3 100644 --- a/src/ViveTrackerController.h +++ b/src/ViveTrackerController.h @@ -19,6 +19,12 @@ #define VIVETRACKER_BUTTON_PADTRIGGERED (1 << 4) #define VIVETRACKER_BUTTON_PADTOUCHED (1 << 5) +#define VIVETRACKER_LPF_DEFAULT 0 +#define VIVETRACKER_LPF_184HZ 0 +#define VIVETRACKER_LPF_5HZ 1 +#define VIVETRACKER_LPF_10HZ 2 +#define VIVETRACKER_LPF_20HZ 3 + /* * The format of the USB HID feature reports is unfortunately not correct in HTC's documentation. @@ -32,12 +38,13 @@ */ struct __attribute__((__packed__)) ViveTrackerFeatureReportB3 { uint8_t address = 0xB3; - uint8_t payloadSize = 3; + uint8_t payloadSize = 4; uint8_t hostType; uint8_t chargeEnable; uint8_t osType; - ViveTrackerFeatureReportB3(uint8_t hostType = 3, uint8_t chargeEnable = 0, uint8_t osType = 0) - : hostType(hostType), chargeEnable(chargeEnable), osType(osType) {} + uint8_t lpfConfig; + ViveTrackerFeatureReportB3(uint8_t hostType = 3, uint8_t chargeEnable = 0, uint8_t osType = 0, uint8_t lpfConfig = VIVETRACKER_LPF_DEFAULT) + : hostType(hostType), chargeEnable(chargeEnable), osType(osType), lpfConfig(lpfConfig) {} }; /** @@ -99,7 +106,8 @@ class ViveTrackerBoot : public HIDBoot { */ class ViveTrackerController { public: - ViveTrackerController(USBHost &usb) : usb(usb), hostTracker(&usb) {} + ViveTrackerController(USBHost &usb, uint8_t hostType = 3, uint8_t chargeEnabled = 0, uint8_t osType = 0) + : usb(usb), hostTracker(&usb), _hostType(hostType), _chargeEnabled(chargeEnabled), _osType(osType) {} /** * Returns whether a vive tracker is connected and initialized. @@ -131,6 +139,19 @@ class ViveTrackerController { return -1; } + uint32_t setChargeEnabled(uint8_t enabled, bool sendUsbReport = true) { + _chargeEnabled = enabled; + if (sendUsbReport) { + return sendUsbReportB3(_hostType, _chargeEnabled, _osType, VIVETRACKER_LPF_DEFAULT); + } else { + return 0; + } + } + + uint32_t setLpfConfig(uint8_t lpfConfig) { + return sendUsbReportB3(_hostType, _chargeEnabled, _osType, lpfConfig); + } + /** * Process usb and vive tracker controller tasks. * @@ -149,10 +170,18 @@ class ViveTrackerController { USBHost &usb; ViveTrackerBoot hostTracker; bool _isInitialized = false; + uint8_t _hostType; + uint8_t _chargeEnabled; + uint8_t _osType; + + uint32_t _initConnection() { + return sendUsbReportB3(_hostType, _chargeEnabled, _osType, VIVETRACKER_LPF_DEFAULT); + } - uint32_t _initConnection(uint8_t hostType = 3, uint8_t chargeEnable = 0, uint8_t osType = 0) { - ViveTrackerFeatureReportB3 report(hostType, chargeEnable, osType); + uint32_t sendUsbReportB3(uint8_t hostType, uint8_t chargeEnabled, uint8_t osType, uint8_t lpfConfig) { + ViveTrackerFeatureReportB3 report(hostType, chargeEnabled, osType, lpfConfig); return hostTracker.SetReport(0, 2, 3, 0, sizeof(ViveTrackerFeatureReportB3), (uint8_t*)&report); + } };