From 99789bcd4ddb6d5ac47255ac1efa00d3956427cf Mon Sep 17 00:00:00 2001 From: Benjamin Maurin Date: Fri, 16 Dec 2022 14:49:33 +0100 Subject: [PATCH 1/7] Add support for Arduino Zero (SAMD base) by including right HID definitions thanks to NicoHood HID library. Warning, the Arduino Zero's BSP package has a driver bug which force you to delete the automatic CDC driver in the windows device manager. --- src/DynamicHID/DynamicHID.cpp | 5 -- src/DynamicHID/DynamicHID.h | 105 +++++++++++++++++++++++++++++----- src/Joystick.cpp | 26 ++++----- 3 files changed, 104 insertions(+), 32 deletions(-) diff --git a/src/DynamicHID/DynamicHID.cpp b/src/DynamicHID/DynamicHID.cpp index 6c46a2c..17be852 100644 --- a/src/DynamicHID/DynamicHID.cpp +++ b/src/DynamicHID/DynamicHID.cpp @@ -23,11 +23,6 @@ #if defined(USBCON) -#ifdef _VARIANT_ARDUINO_DUE_X_ -#define USB_SendControl USBD_SendControl -#define USB_Send USBD_Send -#endif - DynamicHID_& DynamicHID() { static DynamicHID_ obj; diff --git a/src/DynamicHID/DynamicHID.h b/src/DynamicHID/DynamicHID.h index 0a9a6ef..dc8c34c 100644 --- a/src/DynamicHID/DynamicHID.h +++ b/src/DynamicHID/DynamicHID.h @@ -25,18 +25,99 @@ #include #include -#ifdef _VARIANT_ARDUINO_DUE_X_ - // The following values are the same as AVR's USBAPI.h - // Reproduced here because SAM doesn't have these in - // its own USBAPI.H - #define USB_EP_SIZE 64 - #define TRANSFER_PGM 0x80 - - #include "USB/PluggableUSB.h" + + +// See https://github.com/NicoHood/HID/blob/master/src/HID-Settings.h + + +#if defined(ARDUINO_ARCH_AVR) + +// Use default alignment for AVR +#define ATTRIBUTE_PACKED + +#include "PluggableUSB.h" + +#define EPTYPE_DESCRIPTOR_SIZE uint8_t + +#elif defined(ARDUINO_ARCH_SAM) + +#define ATTRIBUTE_PACKED __attribute__((packed, aligned(1))) + +#include "USB/PluggableUSB.h" + +#define EPTYPE_DESCRIPTOR_SIZE uint32_t +/* +#define EP_TYPE_INTERRUPT_IN (UOTGHS_DEVEPTCFG_EPSIZE_512_BYTE | \ + UOTGHS_DEVEPTCFG_EPDIR_IN | \ + UOTGHS_DEVEPTCFG_EPTYPE_BLK | \ + UOTGHS_DEVEPTCFG_EPBK_1_BANK | \ + UOTGHS_DEVEPTCFG_NBTRANS_1_TRANS | \ + UOTGHS_DEVEPTCFG_ALLOC) +#define EP_TYPE_INTERRUPT_OUT (UOTGHS_DEVEPTCFG_EPSIZE_512_BYTE | \ + UOTGHS_DEVEPTCFG_EPTYPE_BLK | \ + UOTGHS_DEVEPTCFG_EPBK_1_BANK | \ + UOTGHS_DEVEPTCFG_NBTRANS_1_TRANS | \ + UOTGHS_DEVEPTCFG_ALLOC) +*/ +#define USB_EP_SIZE EPX_SIZE +#define USB_SendControl USBD_SendControl +#define USB_Available USBD_Available +#define USB_Recv USBD_Recv +#define USB_Send USBD_Send +#define USB_RecvControl USBD_RecvControl +#define USB_Available USBD_Available +#define USB_Flush USBD_Flush +#define TRANSFER_PGM 0x80 + +#elif defined(ARDUINO_ARCH_SAMD) + +#define ATTRIBUTE_PACKED __attribute__((packed, aligned(1))) + +#define USB_EP_SIZE EPX_SIZE +#define EP_TYPE_INTERRUPT_IN USB_ENDPOINT_TYPE_INTERRUPT | USB_ENDPOINT_IN(0); +#define EP_TYPE_INTERRUPT_OUT USB_ENDPOINT_TYPE_INTERRUPT | USB_ENDPOINT_OUT(0); + +#if defined(ARDUINO_API_VERSION) + +#include "api/PluggableUSB.h" +#define EPTYPE_DESCRIPTOR_SIZE unsigned int +#define USB_Available USBDevice.available +#define USB_SendControl USBDevice.sendControl +#define USB_Send USBDevice.send + #else - #include "PluggableUSB.h" + +#include "USB/PluggableUSB.h" + +#define EPTYPE_DESCRIPTOR_SIZE uint32_t +//#define USB_SendControl USBDevice.sendControl -> real C++ functions to take care of PGM overloading +#define USB_Available USBDevice.available +#define USB_Recv USBDevice.recv +#define USB_RecvControl USBDevice.recvControl +#define USB_Send USBDevice.send +#define USB_Flush USBDevice.flush + +int USB_SendControl(void* y, uint8_t z); +int USB_SendControl(uint8_t x, const void* y, uint8_t z); + #endif +#define TRANSFER_PGM 0 +#define TRANSFER_RELEASE 0 + +#define HID_REPORT_TYPE_INPUT 1 +#define HID_REPORT_TYPE_OUTPUT 2 +#define HID_REPORT_TYPE_FEATURE 3 + +#else + +#error "Unsupported architecture" + +#endif + + + + #if defined(USBCON) #define _USING_DYNAMIC_HID @@ -119,11 +200,7 @@ class DynamicHID_ : public PluggableUSBModule uint8_t getShortName(char* name); private: - #ifdef _VARIANT_ARDUINO_DUE_X_ - uint32_t epType[1]; - #else - uint8_t epType[1]; - #endif + EPTYPE_DESCRIPTOR_SIZE epType[1]; DynamicHIDSubDescriptor* rootNode; uint16_t descriptorSize; diff --git a/src/Joystick.cpp b/src/Joystick.cpp index c91841c..18cea97 100644 --- a/src/Joystick.cpp +++ b/src/Joystick.cpp @@ -28,18 +28,18 @@ #define JOYSTICK_SIMULATOR_MINIMUM 0 #define JOYSTICK_SIMULATOR_MAXIMUM 65535 -#define JOYSTICK_INCLUDE_X_AXIS B00000001 -#define JOYSTICK_INCLUDE_Y_AXIS B00000010 -#define JOYSTICK_INCLUDE_Z_AXIS B00000100 -#define JOYSTICK_INCLUDE_RX_AXIS B00001000 -#define JOYSTICK_INCLUDE_RY_AXIS B00010000 -#define JOYSTICK_INCLUDE_RZ_AXIS B00100000 - -#define JOYSTICK_INCLUDE_RUDDER B00000001 -#define JOYSTICK_INCLUDE_THROTTLE B00000010 -#define JOYSTICK_INCLUDE_ACCELERATOR B00000100 -#define JOYSTICK_INCLUDE_BRAKE B00001000 -#define JOYSTICK_INCLUDE_STEERING B00010000 +#define JOYSTICK_INCLUDE_X_AXIS 0b00000001 +#define JOYSTICK_INCLUDE_Y_AXIS 0b00000010 +#define JOYSTICK_INCLUDE_Z_AXIS 0b00000100 +#define JOYSTICK_INCLUDE_RX_AXIS 0b00001000 +#define JOYSTICK_INCLUDE_RY_AXIS 0b00010000 +#define JOYSTICK_INCLUDE_RZ_AXIS 0b00100000 + +#define JOYSTICK_INCLUDE_RUDDER 0b00000001 +#define JOYSTICK_INCLUDE_THROTTLE 0b00000010 +#define JOYSTICK_INCLUDE_ACCELERATOR 0b00000100 +#define JOYSTICK_INCLUDE_BRAKE 0b00001000 +#define JOYSTICK_INCLUDE_STEERING 0b00010000 Joystick_::Joystick_( uint8_t hidReportId, @@ -657,7 +657,7 @@ void Joystick_::sendState() } // Pack hat-switch states into a single byte - data[index++] = (convertedHatSwitch[1] << 4) | (B00001111 & convertedHatSwitch[0]); + data[index++] = (convertedHatSwitch[1] << 4) | (0b00001111 & convertedHatSwitch[0]); } // Hat Switches From 0f23e158ffbb1baabfed667353f2031c39569cc0 Mon Sep 17 00:00:00 2001 From: Benjamin Maurin Date: Wed, 7 Feb 2024 14:50:32 +0100 Subject: [PATCH 2/7] Test for HID name when using joystick --- src/DynamicHID/DynamicHID.cpp | 11 ++++++++++- src/DynamicHID/DynamicHID.h | 2 ++ src/Joystick.cpp | 3 +++ 3 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/DynamicHID/DynamicHID.cpp b/src/DynamicHID/DynamicHID.cpp index 17be852..2b09a78 100644 --- a/src/DynamicHID/DynamicHID.cpp +++ b/src/DynamicHID/DynamicHID.cpp @@ -65,14 +65,23 @@ int DynamicHID_::getDescriptor(USBSetup& setup) return total; } +void DynamicHID_::setShortName(char* name) { + strlcpy(serialname, name, ISERIAL_MAX_LEN); +} + uint8_t DynamicHID_::getShortName(char *name) { + // Up to 20 char (null char included) + // Example from Arduino: + /* name[0] = 'H'; name[1] = 'I'; name[2] = 'D'; name[3] = 'A' + (descriptorSize & 0x0F); name[4] = 'A' + ((descriptorSize >> 4) & 0x0F); - return 5; + */ + int len = strlcpy(name, serialname, ISERIAL_MAX_LEN); + return len; } void DynamicHID_::AppendDescriptor(DynamicHIDSubDescriptor *node) diff --git a/src/DynamicHID/DynamicHID.h b/src/DynamicHID/DynamicHID.h index dc8c34c..9cbe416 100644 --- a/src/DynamicHID/DynamicHID.h +++ b/src/DynamicHID/DynamicHID.h @@ -191,6 +191,7 @@ class DynamicHID_ : public PluggableUSBModule int begin(void); int SendReport(uint8_t id, const void* data, int len); void AppendDescriptor(DynamicHIDSubDescriptor* node); + void setShortName(char* name); protected: // Implementation of the PluggableUSBModule @@ -207,6 +208,7 @@ class DynamicHID_ : public PluggableUSBModule uint8_t protocol; uint8_t idle; + char serialname[ISERIAL_MAX_LEN]; }; // Replacement for global singleton. diff --git a/src/Joystick.cpp b/src/Joystick.cpp index 18cea97..8b35371 100644 --- a/src/Joystick.cpp +++ b/src/Joystick.cpp @@ -438,6 +438,9 @@ Joystick_::Joystick_( // Register HID Report Description DynamicHIDSubDescriptor *node = new DynamicHIDSubDescriptor(customHidReportDescriptor, hidReportDescriptorSize, false); + char name[] = "JOYSTICK_0"; + name[strlen(name)-1] = '0' + (hidReportId-2); + DynamicHID().setShortName(name); DynamicHID().AppendDescriptor(node); // Setup Joystick State From eff8626592a34a9ead0588d54b9c5019787736d7 Mon Sep 17 00:00:00 2001 From: Benjamin Maurin Date: Tue, 19 Nov 2024 16:39:25 +0100 Subject: [PATCH 3/7] Update with latest official version --- CONTRIBUTING.md | 2 +- src/Joystick.cpp | 26 +++++++++++++------------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 6cb08fa..f0e8c9d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -4,7 +4,7 @@ * **Check to see if the bug has already reported** by searching on GitHub under [Issues](https://github.com/MHeironimus/ArduinoJoystickLibrary/issues). -* If you're unable to find an open issue addressing the problem, [open a new one](https://github.com/MHeironimus/ArduinoJoystickLibrary/issues/new). Be sure to include a **title and clear description**, as much relevant information as possible (e.g. host OS, Arduino IDE version, etc.), any important hardware information (e.g. name of Arduino board being used, specific pin wirings if they are relavant, etc.), and a **sample sketch** file demonstrating the issue. +* If you're unable to find an open issue addressing the problem, [open a new one](https://github.com/MHeironimus/ArduinoJoystickLibrary/issues/new). Be sure to include a **title and clear description**, as much relevant information as possible (e.g. host OS, Arduino IDE version, etc.), any important hardware information (e.g. name of Arduino board being used, specific pin wirings if they are relevant, etc.), and a **sample sketch** file demonstrating the issue. ### Fix a bug diff --git a/src/Joystick.cpp b/src/Joystick.cpp index 8b35371..e570cd7 100644 --- a/src/Joystick.cpp +++ b/src/Joystick.cpp @@ -28,18 +28,18 @@ #define JOYSTICK_SIMULATOR_MINIMUM 0 #define JOYSTICK_SIMULATOR_MAXIMUM 65535 -#define JOYSTICK_INCLUDE_X_AXIS 0b00000001 -#define JOYSTICK_INCLUDE_Y_AXIS 0b00000010 -#define JOYSTICK_INCLUDE_Z_AXIS 0b00000100 -#define JOYSTICK_INCLUDE_RX_AXIS 0b00001000 -#define JOYSTICK_INCLUDE_RY_AXIS 0b00010000 -#define JOYSTICK_INCLUDE_RZ_AXIS 0b00100000 - -#define JOYSTICK_INCLUDE_RUDDER 0b00000001 -#define JOYSTICK_INCLUDE_THROTTLE 0b00000010 -#define JOYSTICK_INCLUDE_ACCELERATOR 0b00000100 -#define JOYSTICK_INCLUDE_BRAKE 0b00001000 -#define JOYSTICK_INCLUDE_STEERING 0b00010000 +#define JOYSTICK_INCLUDE_X_AXIS B00000001 +#define JOYSTICK_INCLUDE_Y_AXIS B00000010 +#define JOYSTICK_INCLUDE_Z_AXIS B00000100 +#define JOYSTICK_INCLUDE_RX_AXIS B00001000 +#define JOYSTICK_INCLUDE_RY_AXIS B00010000 +#define JOYSTICK_INCLUDE_RZ_AXIS B00100000 + +#define JOYSTICK_INCLUDE_RUDDER B00000001 +#define JOYSTICK_INCLUDE_THROTTLE B00000010 +#define JOYSTICK_INCLUDE_ACCELERATOR B00000100 +#define JOYSTICK_INCLUDE_BRAKE B00001000 +#define JOYSTICK_INCLUDE_STEERING B00010000 Joystick_::Joystick_( uint8_t hidReportId, @@ -660,7 +660,7 @@ void Joystick_::sendState() } // Pack hat-switch states into a single byte - data[index++] = (convertedHatSwitch[1] << 4) | (0b00001111 & convertedHatSwitch[0]); + data[index++] = (convertedHatSwitch[1] << 4) | (B00001111 & convertedHatSwitch[0]); } // Hat Switches From 2d809ef456ce5b1a21c82eea8db8bd0746f9f024 Mon Sep 17 00:00:00 2001 From: Benjamin Maurin Date: Mon, 28 Apr 2025 17:02:08 +0200 Subject: [PATCH 4/7] Add R4 minima to zero --- src/DynamicHID/DynamicHID.h | 143 +++++++++++++++++------------------- src/Joystick.cpp | 30 ++++---- src/Joystick.h | 8 +- 3 files changed, 91 insertions(+), 90 deletions(-) diff --git a/src/DynamicHID/DynamicHID.h b/src/DynamicHID/DynamicHID.h index 9cbe416..b53879e 100644 --- a/src/DynamicHID/DynamicHID.h +++ b/src/DynamicHID/DynamicHID.h @@ -32,92 +32,85 @@ #if defined(ARDUINO_ARCH_AVR) -// Use default alignment for AVR -#define ATTRIBUTE_PACKED - -#include "PluggableUSB.h" - -#define EPTYPE_DESCRIPTOR_SIZE uint8_t - + // Use default alignment for AVR + #define ATTRIBUTE_PACKED + + #include "PluggableUSB.h" + + #define EPTYPE_DESCRIPTOR_SIZE uint8_t + #elif defined(ARDUINO_ARCH_SAM) -#define ATTRIBUTE_PACKED __attribute__((packed, aligned(1))) - -#include "USB/PluggableUSB.h" - -#define EPTYPE_DESCRIPTOR_SIZE uint32_t -/* -#define EP_TYPE_INTERRUPT_IN (UOTGHS_DEVEPTCFG_EPSIZE_512_BYTE | \ - UOTGHS_DEVEPTCFG_EPDIR_IN | \ - UOTGHS_DEVEPTCFG_EPTYPE_BLK | \ - UOTGHS_DEVEPTCFG_EPBK_1_BANK | \ - UOTGHS_DEVEPTCFG_NBTRANS_1_TRANS | \ - UOTGHS_DEVEPTCFG_ALLOC) -#define EP_TYPE_INTERRUPT_OUT (UOTGHS_DEVEPTCFG_EPSIZE_512_BYTE | \ - UOTGHS_DEVEPTCFG_EPTYPE_BLK | \ - UOTGHS_DEVEPTCFG_EPBK_1_BANK | \ - UOTGHS_DEVEPTCFG_NBTRANS_1_TRANS | \ - UOTGHS_DEVEPTCFG_ALLOC) -*/ -#define USB_EP_SIZE EPX_SIZE -#define USB_SendControl USBD_SendControl -#define USB_Available USBD_Available -#define USB_Recv USBD_Recv -#define USB_Send USBD_Send -#define USB_RecvControl USBD_RecvControl -#define USB_Available USBD_Available -#define USB_Flush USBD_Flush -#define TRANSFER_PGM 0x80 - + // Use byte alignment for SAM - Arduino Zero + #define ATTRIBUTE_PACKED __attribute__((packed, aligned(1))) + + #include "USB/PluggableUSB.h" + + #define EPTYPE_DESCRIPTOR_SIZE uint32_t + + #define USB_EP_SIZE EPX_SIZE + #define USB_SendControl USBD_SendControl + #define USB_Available USBD_Available + #define USB_Recv USBD_Recv + #define USB_Send USBD_Send + #define USB_RecvControl USBD_RecvControl + #define USB_Available USBD_Available + #define USB_Flush USBD_Flush + #define TRANSFER_PGM 0x80 + +#elif defined(ARDUINO_UNOR4_MINIMA) + + #include "HID.h" + #elif defined(ARDUINO_ARCH_SAMD) -#define ATTRIBUTE_PACKED __attribute__((packed, aligned(1))) - -#define USB_EP_SIZE EPX_SIZE -#define EP_TYPE_INTERRUPT_IN USB_ENDPOINT_TYPE_INTERRUPT | USB_ENDPOINT_IN(0); -#define EP_TYPE_INTERRUPT_OUT USB_ENDPOINT_TYPE_INTERRUPT | USB_ENDPOINT_OUT(0); - -#if defined(ARDUINO_API_VERSION) - -#include "api/PluggableUSB.h" -#define EPTYPE_DESCRIPTOR_SIZE unsigned int -#define USB_Available USBDevice.available -#define USB_SendControl USBDevice.sendControl -#define USB_Send USBDevice.send + // Use byte alignment for SAMD - Arduino Due + #define ATTRIBUTE_PACKED __attribute__((packed, aligned(1))) + + #define USB_EP_SIZE EPX_SIZE + #define EP_TYPE_INTERRUPT_IN USB_ENDPOINT_TYPE_INTERRUPT | USB_ENDPOINT_IN(0); + #define EP_TYPE_INTERRUPT_OUT USB_ENDPOINT_TYPE_INTERRUPT | USB_ENDPOINT_OUT(0); + + #if defined(ARDUINO_API_VERSION) + + #include "api/PluggableUSB.h" + + #define EPTYPE_DESCRIPTOR_SIZE unsigned int + #define USB_Available USBDevice.available + #define USB_SendControl USBDevice.sendControl + #define USB_Send USBDevice.send + + #else + + #include "USB/PluggableUSB.h" + + #define EPTYPE_DESCRIPTOR_SIZE uint32_t + //#define USB_SendControl USBDevice.sendControl -> real C++ functions to take care of PGM overloading + #define USB_Available USBDevice.available + #define USB_Recv USBDevice.recv + #define USB_RecvControl USBDevice.recvControl + #define USB_Send USBDevice.send + #define USB_Flush USBDevice.flush + + int USB_SendControl(void* y, uint8_t z); + int USB_SendControl(uint8_t x, const void* y, uint8_t z); + + #endif + + #define TRANSFER_PGM 0 + #define TRANSFER_RELEASE 0 + + #define HID_REPORT_TYPE_INPUT 1 + #define HID_REPORT_TYPE_OUTPUT 2 + #define HID_REPORT_TYPE_FEATURE 3 #else -#include "USB/PluggableUSB.h" - -#define EPTYPE_DESCRIPTOR_SIZE uint32_t -//#define USB_SendControl USBDevice.sendControl -> real C++ functions to take care of PGM overloading -#define USB_Available USBDevice.available -#define USB_Recv USBDevice.recv -#define USB_RecvControl USBDevice.recvControl -#define USB_Send USBDevice.send -#define USB_Flush USBDevice.flush - -int USB_SendControl(void* y, uint8_t z); -int USB_SendControl(uint8_t x, const void* y, uint8_t z); - -#endif - -#define TRANSFER_PGM 0 -#define TRANSFER_RELEASE 0 - -#define HID_REPORT_TYPE_INPUT 1 -#define HID_REPORT_TYPE_OUTPUT 2 -#define HID_REPORT_TYPE_FEATURE 3 - -#else - -#error "Unsupported architecture" + #error "Unsupported architecture" #endif - - #if defined(USBCON) #define _USING_DYNAMIC_HID diff --git a/src/Joystick.cpp b/src/Joystick.cpp index e570cd7..2b78d92 100644 --- a/src/Joystick.cpp +++ b/src/Joystick.cpp @@ -28,18 +28,18 @@ #define JOYSTICK_SIMULATOR_MINIMUM 0 #define JOYSTICK_SIMULATOR_MAXIMUM 65535 -#define JOYSTICK_INCLUDE_X_AXIS B00000001 -#define JOYSTICK_INCLUDE_Y_AXIS B00000010 -#define JOYSTICK_INCLUDE_Z_AXIS B00000100 -#define JOYSTICK_INCLUDE_RX_AXIS B00001000 -#define JOYSTICK_INCLUDE_RY_AXIS B00010000 -#define JOYSTICK_INCLUDE_RZ_AXIS B00100000 - -#define JOYSTICK_INCLUDE_RUDDER B00000001 -#define JOYSTICK_INCLUDE_THROTTLE B00000010 -#define JOYSTICK_INCLUDE_ACCELERATOR B00000100 -#define JOYSTICK_INCLUDE_BRAKE B00001000 -#define JOYSTICK_INCLUDE_STEERING B00010000 +#define JOYSTICK_INCLUDE_X_AXIS 0b00000001 +#define JOYSTICK_INCLUDE_Y_AXIS 0b00000010 +#define JOYSTICK_INCLUDE_Z_AXIS 0b00000100 +#define JOYSTICK_INCLUDE_RX_AXIS 0b00001000 +#define JOYSTICK_INCLUDE_RY_AXIS 0b00010000 +#define JOYSTICK_INCLUDE_RZ_AXIS 0b00100000 + +#define JOYSTICK_INCLUDE_RUDDER 0b00000001 +#define JOYSTICK_INCLUDE_THROTTLE 0b00000010 +#define JOYSTICK_INCLUDE_ACCELERATOR 0b00000100 +#define JOYSTICK_INCLUDE_BRAKE 0b00001000 +#define JOYSTICK_INCLUDE_STEERING 0b00010000 Joystick_::Joystick_( uint8_t hidReportId, @@ -437,10 +437,14 @@ Joystick_::Joystick_( memcpy(customHidReportDescriptor, tempHidReportDescriptor, hidReportDescriptorSize); // Register HID Report Description + #ifdef ARDUINO_UNOR4_MINIMA + HIDSubDescriptor *node = new HIDSubDescriptor(customHidReportDescriptor, hidReportDescriptorSize); + #else DynamicHIDSubDescriptor *node = new DynamicHIDSubDescriptor(customHidReportDescriptor, hidReportDescriptorSize, false); char name[] = "JOYSTICK_0"; name[strlen(name)-1] = '0' + (hidReportId-2); DynamicHID().setShortName(name); + #endif DynamicHID().AppendDescriptor(node); // Setup Joystick State @@ -660,7 +664,7 @@ void Joystick_::sendState() } // Pack hat-switch states into a single byte - data[index++] = (convertedHatSwitch[1] << 4) | (B00001111 & convertedHatSwitch[0]); + data[index++] = (convertedHatSwitch[1] << 4) | (0b00001111 & convertedHatSwitch[0]); } // Hat Switches diff --git a/src/Joystick.h b/src/Joystick.h index de4900b..e75bcfe 100644 --- a/src/Joystick.h +++ b/src/Joystick.h @@ -21,16 +21,20 @@ #ifndef JOYSTICK_h #define JOYSTICK_h -#include "DynamicHID/DynamicHID.h" - #if ARDUINO < 10606 #error The Joystick library requires Arduino IDE 1.6.6 or greater. Please update your IDE. #endif // ARDUINO < 10606 #if ARDUINO > 10606 + #include "DynamicHID/DynamicHID.h" + #ifdef ARDUINO_UNOR4_MINIMA + #define DynamicHID HID + #define _USING_DYNAMIC_HID + #else #if !defined(USBCON) #error The Joystick library can only be used with a USB MCU (e.g. Arduino Leonardo, Arduino Micro, etc.). #endif // !defined(USBCON) + #endif #endif // ARDUINO > 10606 #if !defined(_USING_DYNAMIC_HID) From 6a6ff2e2f80e418c58865858333835890245b70c Mon Sep 17 00:00:00 2001 From: Benjamin Maurin Date: Tue, 29 Apr 2025 14:11:55 +0200 Subject: [PATCH 5/7] clean up code and merged zero and R4 branches --- library.properties | 4 +- src/DynamicHID/DynamicHID.cpp | 9 ++++ src/DynamicHID/DynamicHID.h | 95 +++++------------------------------ src/Joystick.cpp | 15 ++++-- src/Joystick.h | 27 +++++----- 5 files changed, 49 insertions(+), 101 deletions(-) diff --git a/library.properties b/library.properties index 3e3fe88..2f95bc0 100644 --- a/library.properties +++ b/library.properties @@ -2,8 +2,8 @@ name=Joystick version=2.1.1 author=Matthew Heironimus maintainer=Matthew Heironimus -sentence=Allows an Arduino board with USB capabilities (e.g. Leonardo, Arduino Micro, Arudino Due, etc.) to appear as a Joystick or Gamepad. +sentence=Allows an Arduino board with USB capabilities (e.g. Leonardo, Arduino Micro, Arudino Due, R4 minima etc.) to appear as a Joystick or Gamepad. paragraph=This library is built on the PluggableUSB library. It can be used with or without other HID-based libraries (Mouse, Keyboard, etc.). category=Device Control url=https://github.com/MHeironimus/ArduinoJoystickLibrary -architectures=avr,sam +architectures=avr,sam,samd,renesas diff --git a/src/DynamicHID/DynamicHID.cpp b/src/DynamicHID/DynamicHID.cpp index 2b09a78..d530fcc 100644 --- a/src/DynamicHID/DynamicHID.cpp +++ b/src/DynamicHID/DynamicHID.cpp @@ -21,8 +21,15 @@ #include "DynamicHID.h" +#if !defined(ARDUINO_SAMD_VARIANT_COMPLIANCE) + #if defined(USBCON) +#ifdef _VARIANT_ARDUINO_DUE_X_ +#define USB_SendControl USBD_SendControl +#define USB_Send USBD_Send +#endif + DynamicHID_& DynamicHID() { static DynamicHID_ obj; @@ -171,3 +178,5 @@ int DynamicHID_::begin(void) } #endif /* if defined(USBCON) */ + +#endif // !defined(!defined(ARDUINO_SAMD_VARIANT_COMPLIANCE)) diff --git a/src/DynamicHID/DynamicHID.h b/src/DynamicHID/DynamicHID.h index b53879e..4847044 100644 --- a/src/DynamicHID/DynamicHID.h +++ b/src/DynamicHID/DynamicHID.h @@ -26,91 +26,20 @@ #include - -// See https://github.com/NicoHood/HID/blob/master/src/HID-Settings.h - - -#if defined(ARDUINO_ARCH_AVR) - - // Use default alignment for AVR - #define ATTRIBUTE_PACKED - - #include "PluggableUSB.h" - - #define EPTYPE_DESCRIPTOR_SIZE uint8_t - -#elif defined(ARDUINO_ARCH_SAM) - - // Use byte alignment for SAM - Arduino Zero - #define ATTRIBUTE_PACKED __attribute__((packed, aligned(1))) - - #include "USB/PluggableUSB.h" - - #define EPTYPE_DESCRIPTOR_SIZE uint32_t - - #define USB_EP_SIZE EPX_SIZE - #define USB_SendControl USBD_SendControl - #define USB_Available USBD_Available - #define USB_Recv USBD_Recv - #define USB_Send USBD_Send - #define USB_RecvControl USBD_RecvControl - #define USB_Available USBD_Available - #define USB_Flush USBD_Flush - #define TRANSFER_PGM 0x80 - -#elif defined(ARDUINO_UNOR4_MINIMA) - - #include "HID.h" - -#elif defined(ARDUINO_ARCH_SAMD) - - // Use byte alignment for SAMD - Arduino Due - #define ATTRIBUTE_PACKED __attribute__((packed, aligned(1))) - - #define USB_EP_SIZE EPX_SIZE - #define EP_TYPE_INTERRUPT_IN USB_ENDPOINT_TYPE_INTERRUPT | USB_ENDPOINT_IN(0); - #define EP_TYPE_INTERRUPT_OUT USB_ENDPOINT_TYPE_INTERRUPT | USB_ENDPOINT_OUT(0); - - #if defined(ARDUINO_API_VERSION) - - #include "api/PluggableUSB.h" - - #define EPTYPE_DESCRIPTOR_SIZE unsigned int - #define USB_Available USBDevice.available - #define USB_SendControl USBDevice.sendControl - #define USB_Send USBDevice.send - - #else - - #include "USB/PluggableUSB.h" - - #define EPTYPE_DESCRIPTOR_SIZE uint32_t - //#define USB_SendControl USBDevice.sendControl -> real C++ functions to take care of PGM overloading - #define USB_Available USBDevice.available - #define USB_Recv USBDevice.recv - #define USB_RecvControl USBDevice.recvControl - #define USB_Send USBDevice.send - #define USB_Flush USBDevice.flush - - int USB_SendControl(void* y, uint8_t z); - int USB_SendControl(uint8_t x, const void* y, uint8_t z); - - #endif - - #define TRANSFER_PGM 0 - #define TRANSFER_RELEASE 0 - - #define HID_REPORT_TYPE_INPUT 1 - #define HID_REPORT_TYPE_OUTPUT 2 - #define HID_REPORT_TYPE_FEATURE 3 - +#if !defined(ARDUINO_SAMD_VARIANT_COMPLIANCE) && !defined(ARDUINO_ARCH_RENESAS) + +#ifdef _VARIANT_ARDUINO_DUE_X_ + // The following values are the same as AVR's USBAPI.h + // Reproduced here because SAM doesn't have these in + // its own USBAPI.H + #define USB_EP_SIZE 64 + #define TRANSFER_PGM 0x80 + #define EPTYPE_DESCRIPTOR_SIZE uint32_t + #include "USB/PluggableUSB.h" #else - - #error "Unsupported architecture" - + #include "PluggableUSB.h" #endif - #if defined(USBCON) #define _USING_DYNAMIC_HID @@ -214,3 +143,5 @@ DynamicHID_& DynamicHID(); #endif // USBCON #endif // DYNAMIC_HID_h + +#endif // !defined(ARDUINO_SAMD_VARIANT_COMPLIANCE) && !defined(ARDUINO_ARCH_RENESAS) diff --git a/src/Joystick.cpp b/src/Joystick.cpp index 2b78d92..97fc59c 100644 --- a/src/Joystick.cpp +++ b/src/Joystick.cpp @@ -20,7 +20,7 @@ #include "Joystick.h" -#if defined(_USING_DYNAMIC_HID) +#if defined(_USING_DYNAMIC_HID) || defined(_USING_HID) #define JOYSTICK_REPORT_ID_INDEX 7 #define JOYSTICK_AXIS_MINIMUM 0 @@ -437,15 +437,16 @@ Joystick_::Joystick_( memcpy(customHidReportDescriptor, tempHidReportDescriptor, hidReportDescriptorSize); // Register HID Report Description - #ifdef ARDUINO_UNOR4_MINIMA - HIDSubDescriptor *node = new HIDSubDescriptor(customHidReportDescriptor, hidReportDescriptorSize); - #else + #if defined(_USING_DYNAMIC_HID) DynamicHIDSubDescriptor *node = new DynamicHIDSubDescriptor(customHidReportDescriptor, hidReportDescriptorSize, false); + DynamicHID().AppendDescriptor(node); char name[] = "JOYSTICK_0"; name[strlen(name)-1] = '0' + (hidReportId-2); DynamicHID().setShortName(name); + #else + HIDSubDescriptor *node = new HIDSubDescriptor(customHidReportDescriptor, hidReportDescriptorSize); + HID().AppendDescriptor(node); #endif - DynamicHID().AppendDescriptor(node); // Setup Joystick State if (buttonCount > 0) { @@ -683,7 +684,11 @@ 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])); + #if defined(_USING_DYNAMIC_HID) DynamicHID().SendReport(_hidReportId, data, _hidReportSize); + #else + HID().SendReport(_hidReportId, data, _hidReportSize); + #endif } #endif diff --git a/src/Joystick.h b/src/Joystick.h index e75bcfe..9ac9280 100644 --- a/src/Joystick.h +++ b/src/Joystick.h @@ -21,27 +21,29 @@ #ifndef JOYSTICK_h #define JOYSTICK_h +#include + +#if defined(ARDUINO_SAMD_VARIANT_COMPLIANCE) || defined(ARDUINO_ARCH_RENESAS) + #include +#else + #include +#endif // defined(ARDUINO_SAMD_VARIANT_COMPLIANCE) || defined(ARDUINO_ARCH_RENESAS) + #if ARDUINO < 10606 #error The Joystick library requires Arduino IDE 1.6.6 or greater. Please update your IDE. #endif // ARDUINO < 10606 #if ARDUINO > 10606 - #include "DynamicHID/DynamicHID.h" - #ifdef ARDUINO_UNOR4_MINIMA - #define DynamicHID HID - #define _USING_DYNAMIC_HID - #else -#if !defined(USBCON) -#error The Joystick library can only be used with a USB MCU (e.g. Arduino Leonardo, Arduino Micro, etc.). -#endif // !defined(USBCON) - #endif + #if !defined(USBCON) && !defined(_USING_HID) + #error The Joystick library can only be used with a USB MCU (e.g. Arduino Leonardo, Arduino Micro, etc.). + #endif // !defined(USBCON) #endif // ARDUINO > 10606 -#if !defined(_USING_DYNAMIC_HID) +#if !defined(_USING_DYNAMIC_HID) && !defined(_USING_HID) #warning "Using legacy HID core (non pluggable)" -#else // !defined(_USING_DYNAMIC_HID) +#else // !defined(_USING_DYNAMIC_HID) && !defined(_USING_HID) //================================================================================ // Joystick (Gamepad) @@ -218,5 +220,6 @@ class Joystick_ void sendState(); }; -#endif // !defined(_USING_DYNAMIC_HID) +#endif // !defined(_USING_DYNAMIC_HID) && !defined(_USING_HID) + #endif // JOYSTICK_h From e2003a4d70e9d9505f7c47ab09fe2212ec21932b Mon Sep 17 00:00:00 2001 From: Benjamin Maurin Date: Tue, 29 Apr 2025 14:26:22 +0200 Subject: [PATCH 6/7] fix compiling for atmega32u4 --- src/DynamicHID/DynamicHID.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/DynamicHID/DynamicHID.h b/src/DynamicHID/DynamicHID.h index 4847044..3ca4800 100644 --- a/src/DynamicHID/DynamicHID.h +++ b/src/DynamicHID/DynamicHID.h @@ -37,6 +37,7 @@ #define EPTYPE_DESCRIPTOR_SIZE uint32_t #include "USB/PluggableUSB.h" #else + #define EPTYPE_DESCRIPTOR_SIZE uint8_t #include "PluggableUSB.h" #endif From b4d8c8c365717c8984be26b8d93b8fb8f0b8ce5e Mon Sep 17 00:00:00 2001 From: Benjamin Maurin Date: Tue, 29 Apr 2025 15:22:30 +0200 Subject: [PATCH 7/7] fix setting of hid name --- src/DynamicHID/DynamicHID.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/DynamicHID/DynamicHID.cpp b/src/DynamicHID/DynamicHID.cpp index d530fcc..74f3f8a 100644 --- a/src/DynamicHID/DynamicHID.cpp +++ b/src/DynamicHID/DynamicHID.cpp @@ -174,6 +174,11 @@ DynamicHID_::DynamicHID_(void) : PluggableUSBModule(1, 1, epType), int DynamicHID_::begin(void) { + serialname[0] = 'H'; + serialname[1] = 'I'; + serialname[2] = 'D'; + serialname[3] = 'A' + (descriptorSize & 0x0F); + serialname[4] = 'A' + ((descriptorSize >> 4) & 0x0F); return 0; }