Skip to content

Commit

Permalink
Refractored how usb message fields are handled.
Browse files Browse the repository at this point in the history
Added support for the low-pass filter added in tracker firmware v.20.
  • Loading branch information
matzman666 committed Nov 26, 2017
1 parent 8fc7d4b commit 0686cef
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 9 deletions.
4 changes: 3 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/ViveTracker/ViveTracker.ino
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=USBHost
version=1.0.5
version=1.0.6
author=Arduino
maintainer=Arduino <info@arduino.cc>
sentence=Allows the communication with USB peripherals like mice, keyboards, and thumbdrives.
Expand Down
5 changes: 5 additions & 0 deletions src/Usb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
41 changes: 35 additions & 6 deletions src/ViveTrackerController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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) {}
};

/**
Expand Down Expand Up @@ -99,7 +106,8 @@ class ViveTrackerBoot : public HIDBoot<HID_PROTOCOL_NONE, HID_NONE_SUBCLASS> {
*/
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.
Expand Down Expand Up @@ -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.
*
Expand All @@ -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);

}
};

Expand Down

0 comments on commit 0686cef

Please sign in to comment.