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

Fix Joystick HID detection, add ADC Joystick mode, fix build on MacOS #408

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
49 changes: 22 additions & 27 deletions radio/src/targets/common/arm/stm32/usb_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ extern "C" {
#endif

#include "opentx.h"
#include "board.h"
#include "debug.h"

static bool usbDriverStarted = false;
Expand Down Expand Up @@ -162,6 +163,8 @@ bool usbStarted()
}

#if !defined(BOOT)

#define PACKET_SIZE 8
/*
Prepare and send new USB data packet

Expand All @@ -171,7 +174,7 @@ bool usbStarted()
*/
void usbJoystickUpdate()
{
static uint8_t HID_Buffer[HID_IN_PACKET];
static uint8_t HID_Buffer[PACKET_SIZE];

// test to see if TX buffer is free
#if defined(STM32F0)
Expand All @@ -181,36 +184,28 @@ void usbJoystickUpdate()
#endif

// 4 axes
for (int i = 0; i < 8; ++i) {
int16_t value = channelOutputs[i] + 1024;
if ( value > 2047 ) value = 2047;
else if ( value < 0 ) value = 0;
HID_Buffer[i*2 + 0] = uint8_t(value);
HID_Buffer[i*2 + 1] = uint8_t(value >> 8);
}
// 2 sliders
// 4 switches
HID_Buffer[0] = uint8_t(adcValues[STICK1] >> 4) - 0x80;
dim13 marked this conversation as resolved.
Show resolved Hide resolved
HID_Buffer[1] = uint8_t(adcValues[STICK2] >> 4) - 0x80;
HID_Buffer[2] = uint8_t(adcValues[STICK3] >> 4) - 0x80;
HID_Buffer[3] = uint8_t(adcValues[STICK4] >> 4) - 0x80;

// buttons
HID_Buffer[8*2+1] = 0;
HID_Buffer[8*2+2] = 0;
HID_Buffer[8*2+3] = 0;
for (int i = 0; i < 8; ++i) {
if (channelOutputs[i+8] > 0) {
HID_Buffer[8*2+1] |= 1 << i;
}
if (channelOutputs[i+16] > 0) {
HID_Buffer[8*2+2] |= 1 << i;
}
if (channelOutputs[i+24] > 0) {
HID_Buffer[8*2+3] |= 1 << i;
}
}
// 2 pots
HID_Buffer[4] = uint8_t(adcValues[POT1] >> 4) - 0x80;
HID_Buffer[5] = uint8_t(adcValues[POT2] >> 4) - 0x80;

// 4 switches
// up: 10
// mid: 00
// dn: 01
HID_Buffer[6] = (~(uint8_t(adcValues[SW_A] >> 10) - 2) & 0x03)
| (~(uint8_t(adcValues[SW_B] >> 10) - 2) & 0x03) << 2
| (~(uint8_t(adcValues[SW_C] >> 10) - 2) & 0x03) << 4
| (~(uint8_t(adcValues[SW_D] >> 10) - 2) & 0x03) << 6;

#if defined(STM32F0)
USBD_HID_SendReport(&USB_Device_dev, HID_Buffer, HID_IN_PACKET);
USBD_HID_SendReport(&USB_Device_dev, HID_Buffer, PACKET_SIZE);
#else
USBD_HID_SendReport(&USB_OTG_dev, HID_Buffer, HID_IN_PACKET);
USBD_HID_SendReport(&USB_OTG_dev, HID_Buffer, PACKET_SIZE);
#endif
}
}
Expand Down
23 changes: 8 additions & 15 deletions radio/src/targets/common/arm/stm32/usbd_hid_joystick.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,34 +112,27 @@ __ALIGN_BEGIN static const uint8_t HID_JOYSTICK_ReportDesc[] __ALIGN_END =
0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x04, // USAGE (Joystick)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is 0x05 in EdgeTX, I'm not against the change but is there a specific reason to change it?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Value from original firmware, IMHO more suitable for the use. Doesn't do much however.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As this is port of OpenTX/EdgeTX and there is no specific reason to change that, it should stay as it was.

0xa1, 0x01, // COLLECTION (Application)
0xa1, 0x00, // COLLECTION (Physical)

0x05, 0x01, // USAGE_PAGE (Generic Desktop)
0x09, 0x30, // USAGE (X)
0x09, 0x31, // USAGE (Y)
0x09, 0x32, // USAGE (Z)
0x09, 0x33, // USAGE (Rx)
0x09, 0x34, // USAGE (Ry)
0x09, 0x35, // USAGE (Rz)
0x09, 0x36, // USAGE (Slider)
0x09, 0x37, // USAGE (Slider)
0x16, 0x00, 0x00, // LOGICAL_MINIMUM (0)
0x26, 0xFF, 0x07, // LOGICAL_MAXIMUM (2047)
0x75, 0x10, // REPORT_SIZE (16)
0x95, 0x08, // REPORT_COUNT (8)
0x15, 0x81, // LOGICAL_MINIMUM (-127)
0x25, 0x7F, // LOGICAL_MAXIMUM (127)
0x75, 0x08, // REPORT_SIZE (8)
0x95, 0x06, // REPORT_COUNT (6)
0x81, 0x02, // INPUT (Data,Var,Abs)

0x05, 0x09, // USAGE_PAGE (Button)
0x19, 0x01, // USAGE_MINIMUM (Button 1)
0x29, 0x10, // USAGE_MAXIMUM (Button 16)
0x29, 0x08, // USAGE_MAXIMUM (Button 8)
0x15, 0x00, // LOGICAL_MINIMUM (0)
0x25, 0x01, // LOGICAL_MAXIMUM (1)
0x95, 0x10, // REPORT_COUNT (16)
0x75, 0x01, // REPORT_SIZE (1)
0x95, 0x08, // REPORT_COUNT (8)
0x81, 0x02, // INPUT (Data,Var,Abs)

0xc0, // END_COLLECTION
0xc0 // END_COLLECTION
0xc0, // END_COLLECTION
};


Expand Down Expand Up @@ -248,7 +241,7 @@ __ALIGN_BEGIN static const uint8_t USBD_HID_CfgDesc[USB_HID_CONFIG_DESC_SIZ] __A

HID_IN_EP, /*bEndpointAddress: Endpoint Address (IN)*/
0x03, /*bmAttributes: Interrupt endpoint*/
HID_IN_PACKET, /*wMaxPacketSize: 4 Byte max */
0x40, /*wMaxPacketSize: 4 Byte max */
0x00,
0x07, /*bInterval: Polling Interval (7 ms)*/
ajjjjjjjj marked this conversation as resolved.
Show resolved Hide resolved
/* 34 */
Expand Down
4 changes: 2 additions & 2 deletions radio/src/targets/flysky/board.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,12 +335,12 @@ enum Analogs {
STICK4,
SW_A,
SW_B,
SW_C,
SW_D,
POT_FIRST,
POT1 = POT_FIRST,
POT2,
POT_LAST = POT2,
SW_C,
ajjjjjjjj marked this conversation as resolved.
Show resolved Hide resolved
SW_D,
TX_VOLTAGE,
NUM_ANALOGS
};
Expand Down