Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for Adafruit TinyUSB devices #120

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions library.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"name": "ArduinoJoystickLibrary",
"description": "Arduino library that allows an Arduino Leonardo, Arduino Micro, or Arudino Due to appear as a Joystick or Gamepad.",
"repository": "https://github.com/nsamala/ArduinoJoystickLibrary.git",
"srcDir": "src",
"includeDir": "src"
}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

#if defined(USBCON)

#ifdef _VARIANT_ARDUINO_DUE_X_
#if defined(_VARIANT_ARDUINO_DUE_X_) || defined(_VARIANT_MACCHINA_M2_)
#define USB_SendControl USBD_SendControl
#define USB_Send USBD_Send
#endif
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#include <stdint.h>
#include <Arduino.h>

#ifdef _VARIANT_ARDUINO_DUE_X_
#if defined(_VARIANT_ARDUINO_DUE_X_) || defined(_VARIANT_MACCHINA_M2_)
// The following values are the same as AVR's USBAPI.h
// Reproduced here because SAM doesn't have these in
// its own USBAPI.H
Expand Down Expand Up @@ -119,7 +119,7 @@ class DynamicHID_ : public PluggableUSBModule
uint8_t getShortName(char* name);

private:
#ifdef _VARIANT_ARDUINO_DUE_X_
#if defined(_VARIANT_ARDUINO_DUE_X_) || defined(_VARIANT_MACCHINA_M2_)
uint32_t epType[1];
#else
uint8_t epType[1];
Expand Down
22 changes: 21 additions & 1 deletion Joystick/src/Joystick.cpp → src/Joystick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,8 +435,12 @@ Joystick_::Joystick_(
memcpy(customHidReportDescriptor, tempHidReportDescriptor, hidReportDescriptorSize);

// Register HID Report Description
#ifdef USE_TINYUSB
_usb_hid.setReportDescriptor(customHidReportDescriptor, hidReportDescriptorSize);
#else
DynamicHIDSubDescriptor *node = new DynamicHIDSubDescriptor(customHidReportDescriptor, hidReportDescriptorSize, false);
DynamicHID().AppendDescriptor(node);
#endif

// Setup Joystick State
if (buttonCount > 0) {
Expand Down Expand Up @@ -475,8 +479,15 @@ Joystick_::Joystick_(
}
}

void Joystick_::begin(bool initAutoSendState)
void Joystick_::begin(bool initAutoSendState, uint8_t intervalMs)
{
#ifdef USE_TINYUSB
_usb_hid.setPollInterval(intervalMs);
_usb_hid.begin();

while(!USBDevice.mounted()) delay(1);
#endif

_autoSendState = initAutoSendState;
sendState();
}
Expand Down Expand Up @@ -674,7 +685,16 @@ void Joystick_::sendState()
index += buildAndSetSimulationValue(_includeSimulatorFlags & JOYSTICK_INCLUDE_BRAKE, _brake, _brakeMinimum, _brakeMaximum, &(data[index]));
index += buildAndSetSimulationValue(_includeSimulatorFlags & JOYSTICK_INCLUDE_STEERING, _steering, _steeringMinimum, _steeringMaximum, &(data[index]));

#ifdef USE_TINYUSB
if (_usb_hid.ready()) {
_usb_hid.sendReport(_hidReportId, data, _hidReportSize);
}

if (USBDevice.suspended())
USBDevice.remoteWakeup();
#else
DynamicHID().SendReport(_hidReportId, data, _hidReportSize);
#endif
}

#endif
10 changes: 9 additions & 1 deletion Joystick/src/Joystick.h → src/Joystick.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@
#ifndef JOYSTICK_h
#define JOYSTICK_h

#ifdef USE_TINYUSB
#include <Adafruit_TinyUSB.h>
#else
#include <DynamicHID/DynamicHID.h>
#endif

#if ARDUINO < 10606
#error The Joystick library requires Arduino IDE 1.6.6 or greater. Please update your IDE.
Expand Down Expand Up @@ -107,6 +111,10 @@ class Joystick_
uint8_t _hidReportId;
uint8_t _hidReportSize;

#ifdef USE_TINYUSB
Adafruit_USBD_HID _usb_hid;
#endif

protected:
int buildAndSet16BitValue(bool includeValue, int16_t value, int16_t valueMinimum, int16_t valueMaximum, int16_t actualMinimum, int16_t actualMaximum, uint8_t dataLocation[]);
int buildAndSetAxisValue(bool includeAxis, int16_t axisValue, int16_t axisMinimum, int16_t axisMaximum, uint8_t dataLocation[]);
Expand All @@ -130,7 +138,7 @@ class Joystick_
bool includeBrake = true,
bool includeSteering = true);

void begin(bool initAutoSendState = true);
void begin(bool initAutoSendState = true, uint8_t interval_ms = 2);
void end();

// Set Range Functions
Expand Down