Skip to content

Commit

Permalink
Take input direct from ADC instead of Mixers
Browse files Browse the repository at this point in the history
  • Loading branch information
dim13 committed Jul 7, 2024
1 parent 08dfe4a commit 3e1ed05
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 44 deletions.
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 7
/*
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;
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_C] >> 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)
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)*/
/* 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,
SW_D,
TX_VOLTAGE,
NUM_ANALOGS
};
Expand Down

0 comments on commit 3e1ed05

Please sign in to comment.