Joystick report descriptor; USB device class

input_events
NepEgor 3 years ago
parent aaa60c6778
commit df99a0c180

@ -1,20 +1,37 @@
#ifndef USB_DEVICE_H
#define USB_DEVICE_H
#include <Arduino.h>
#include <stdint.h>
#include "usbd_hid_composite_if.h"
#include "usbd_report.h"
class USB_Device
{
private:
USBD_HID_Joystick_Report report;
public:
USB_Device();
void begin();
void end();
static const int16_t usb_joystick_x = 511;
static const int16_t usb_joystick_y = 511;
static const int16_t usb_joystick_r = 511;
USB_Device() {}
void begin();
void end();
void button(uint8_t button, bool val);
void dpad(uint8_t dir);
void joystick_left(uint16_t x, uint16_t y);
void joystick_right(uint16_t x, uint16_t y);
void trigger_left(uint16_t val);
void trigger_right(uint16_t val);
void sendReport();
};
#endif

@ -6,9 +6,6 @@
#define USB_HID_CUSTOM_CONFIG_DESC_SIZ 34U
#define USB_HID_CUSTOM_DESC_SIZ 9U
#define USB_LEN_DEV_QUALIFIER_DESC 0x0AU
#define HID_CUSTOM_REPORT_DESC_SIZE 34U
//#define HID_KEYBOARD_REPORT_DESC_SIZE 45U
#define HID_DESCRIPTOR_TYPE 0x21
@ -24,10 +21,6 @@
#define HID_CUSTOM_EPIN_ADDR 0x81U
#define HID_CUSTOM_EPIN_SIZE 0x08U
//#define HID_KEYBOARD_INTERFACE 0x01U
//#define HID_KEYBOARD_EPIN_ADDR 0x82U
//#define HID_KEYBOARD_EPIN_SIZE 0x08U
// USB HID device FS Configuration Descriptor
extern uint8_t USBD_HID_CUSTOM_CfgFSDesc[USB_HID_CUSTOM_CONFIG_DESC_SIZ];
@ -40,13 +33,7 @@ extern uint8_t USBD_HID_CUSTOM_OtherSpeedCfgDesc[USB_HID_CUSTOM_CONFIG_DESC_SIZ]
// USB HID device Configuration Descriptor
extern uint8_t USBD_HID_CUSTOM_Desc[USB_HID_CUSTOM_DESC_SIZ];
// USB HID device Configuration Descriptor
//extern uint8_t USBD_KEYBOARD_HID_Desc[USB_HID_DESC_SIZ];
// USB Standard Device Descriptor
extern uint8_t USBD_HID_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC];
// USB Custom Report Descriptor
extern uint8_t HID_CUSTOM_ReportDesc[HID_CUSTOM_REPORT_DESC_SIZE];
#endif

@ -0,0 +1,26 @@
#ifndef USBD_REPORT_H
#define USBD_REPORT_H
#include <stdint.h>
//#define HID_CUSTOM_REPORT_DESC_SIZE 34U
#define HID_JOYSTICK_REPORT_DESC_SIZE 89U
// USB Custom Report Descriptor
//extern uint8_t USBD_HID_CUSTOM_ReportDesc[HID_CUSTOM_REPORT_DESC_SIZE];
extern uint8_t USBD_HID_Joystick_ReportDesc[HID_JOYSTICK_REPORT_DESC_SIZE];
struct __attribute__((packed)) USBD_HID_Joystick_Report
{
uint8_t reportID = 20;
uint32_t buttons;
unsigned hat:4;
unsigned x:10;
unsigned y:10;
unsigned rx:10;
unsigned ry:10;
unsigned trigger_left:10;
unsigned trigger_right:10;
};
#endif

@ -2,17 +2,65 @@
#include "usbd_hid_custom_if.h"
USB_Device::USB_Device(void)
void USB_Device::begin()
{
HID_Custom_Init();
report.buttons = 0;
report.hat = 15;
report.x = usb_joystick_x;
report.y = usb_joystick_y;
report.rx = usb_joystick_x;
report.ry = usb_joystick_y;
report.trigger_left = 0;
report.trigger_right = 0;
}
void USB_Device::begin(void)
void USB_Device::end()
{
HID_Custom_Init();
HID_Custom_DeInit();
}
void USB_Device::end(void)
void USB_Device::button(uint8_t button, bool val)
{
HID_Custom_DeInit();
if (val)
{
report.buttons |= (1u << button);
}
else
{
report.buttons &= ~(1u << button);
}
}
void USB_Device::dpad(uint8_t dir)
{
report.hat = dir;
}
void USB_Device::joystick_left(uint16_t x, uint16_t y)
{
report.x = x & 0x3FF;
report.y = y & 0x3FF;
}
void USB_Device::joystick_right(uint16_t x, uint16_t y)
{
report.rx = x & 0x3FF;
report.ry = y & 0x3FF;
}
void USB_Device::trigger_left(uint16_t val)
{
report.trigger_left = val & 0x3FF;;
}
void USB_Device::trigger_right(uint16_t val)
{
report.trigger_right = val & 0x3FF;;
}
void USB_Device::sendReport()
{
HID_Custom_sendReport((uint8_t*)(&report), sizeof(report));
}

@ -1,8 +1,8 @@
#include "usbd_descriptors.h"
#include "usbd_def.h"
#include "usbd_report.h"
#include "hid_def.h"
#include "usbd_def.h"
// USB HID device FS Configuration Descriptor
__ALIGN_BEGIN uint8_t USBD_HID_CUSTOM_CfgFSDesc[USB_HID_CUSTOM_CONFIG_DESC_SIZ] __ALIGN_END = {
@ -36,7 +36,7 @@ __ALIGN_BEGIN uint8_t USBD_HID_CUSTOM_CfgFSDesc[USB_HID_CUSTOM_CONFIG_DESC_SIZ]
0x00, //bCountryCode: Hardware target country
0x01, //bNumDescriptors: Number of HID class descriptors to follow
0x22, //bDescriptorType
HID_CUSTOM_REPORT_DESC_SIZE,//wItemLength: Total length of Report descriptor
HID_JOYSTICK_REPORT_DESC_SIZE,//wItemLength: Total length of Report descriptor
0x00,
//******************* Descriptor of Mouse endpoint *******************
// 27
@ -82,7 +82,7 @@ __ALIGN_BEGIN uint8_t USBD_HID_CUSTOM_CfgHSDesc[USB_HID_CUSTOM_CONFIG_DESC_SIZ]
0x00, //bCountryCode: Hardware target country
0x01, //bNumDescriptors: Number of HID class descriptors to follow
0x22, //bDescriptorType
HID_CUSTOM_REPORT_DESC_SIZE,//wItemLength: Total length of Report descriptor
HID_JOYSTICK_REPORT_DESC_SIZE,//wItemLength: Total length of Report descriptor
0x00,
//******************* Descriptor of Mouse endpoint *******************
// 27
@ -128,7 +128,7 @@ __ALIGN_BEGIN uint8_t USBD_HID_CUSTOM_OtherSpeedCfgDesc[USB_HID_CUSTOM_CONFIG_DE
0x00, //bCountryCode: Hardware target country
0x01, //bNumDescriptors: Number of HID class descriptors to follow
0x22, //bDescriptorType
HID_CUSTOM_REPORT_DESC_SIZE,//wItemLength: Total length of Report descriptor
HID_JOYSTICK_REPORT_DESC_SIZE,//wItemLength: Total length of Report descriptor
0x00,
//******************* Descriptor of Mouse endpoint *******************
// 27
@ -151,7 +151,7 @@ __ALIGN_BEGIN uint8_t USBD_HID_CUSTOM_Desc[USB_HID_CUSTOM_DESC_SIZ] __ALIGN_END
0x00, //bCountryCode: Hardware target country
0x01, //bNumDescriptors: Number of HID class descriptors to follow
0x22, //bDescriptorType
HID_CUSTOM_REPORT_DESC_SIZE,//wItemLength: Total length of Report descriptor
HID_JOYSTICK_REPORT_DESC_SIZE,//wItemLength: Total length of Report descriptor
0x00
};
@ -168,23 +168,3 @@ __ALIGN_BEGIN uint8_t USBD_HID_DeviceQualifierDesc[USB_LEN_DEV_QUALIFIER_DESC]
0x01,
0x00,
};
__ALIGN_BEGIN uint8_t HID_CUSTOM_ReportDesc[HID_CUSTOM_REPORT_DESC_SIZE] __ALIGN_END = {
HID_USAGE_PAGE(GENERIC_DESKTOP),
HID_USAGE(JOYSTICK),
HID_COLLECTION(APPLICATION),
HID_COLLECTION(PHYSICAL),
HID_USAGE_PAGE(GENERIC_DESKTOP),
HID_USAGE(X),
HID_USAGE(Y),
HID_USAGE(Z),
HID_LOGICAL_MINIMUM(1, 0),
HID_LOGICAL_MAXIMUM(2, 1024),
HID_PHYSICAL_MINIMUM(1, 0),
HID_PHYSICAL_MAXIMUM(2, 1024),
HID_REPORT_SIZE(16),
HID_REPORT_COUNT(3),
HID_INPUT(DATA, VARIABLE, ABSOLUTE),
HID_END_COLLECTION(PHYSICAL),
HID_END_COLLECTION(APPLICATION)
};

@ -2,12 +2,13 @@
#include "usbd_ctlreq.h"
#include "usbd_descriptors.h"
#include "usbd_report.h"
static uint8_t USBD_HID_Init(USBD_HandleTypeDef* pdev, uint8_t cfgidx);
static uint8_t USBD_HID_DeInit(USBD_HandleTypeDef* pdev, uint8_t cfgidx);
static uint8_t USBD_CUSTOM_HID_Setup(USBD_HandleTypeDef* pdev, USBD_SetupReqTypedef* req);
static uint8_t USBD_HID_MOUSE_Setup(USBD_HandleTypeDef* pdev, USBD_SetupReqTypedef* req);
static uint8_t USBD_HID_JOYSTICK_Setup(USBD_HandleTypeDef* pdev, USBD_SetupReqTypedef* req);
static uint8_t* USBD_HID_GetFSCfgDesc(uint16_t* length);
static uint8_t* USBD_HID_GetHSCfgDesc(uint16_t* length);
@ -93,11 +94,11 @@ static uint8_t USBD_CUSTOM_HID_Setup(USBD_HandleTypeDef* pdev, USBD_SetupReqType
//}
//else
//{
return USBD_HID_MOUSE_Setup(pdev, req);
return USBD_HID_JOYSTICK_Setup(pdev, req);
//}
}
static uint8_t USBD_HID_MOUSE_Setup(USBD_HandleTypeDef* pdev, USBD_SetupReqTypedef* req)
static uint8_t USBD_HID_JOYSTICK_Setup(USBD_HandleTypeDef* pdev, USBD_SetupReqTypedef* req)
{
USBD_HID_HandleTypeDef* hhid = (USBD_HID_HandleTypeDef*)pdev->pClassData;
uint16_t len = 0U;
@ -151,8 +152,8 @@ static uint8_t USBD_HID_MOUSE_Setup(USBD_HandleTypeDef* pdev, USBD_SetupReqTyped
case USB_REQ_GET_DESCRIPTOR:
if (req->wValue >> 8 == HID_REPORT_DESC)
{
len = MIN(HID_CUSTOM_REPORT_DESC_SIZE, req->wLength);
pbuf = HID_CUSTOM_ReportDesc;
len = MIN(HID_JOYSTICK_REPORT_DESC_SIZE, req->wLength);
pbuf = USBD_HID_Joystick_ReportDesc;
}
else if (req->wValue >> 8 == HID_DESCRIPTOR_TYPE)
{

@ -0,0 +1,80 @@
#include "usbd_report.h"
#include "usbd_descriptors.h"
#include "usbd_def.h"
#include "hid_def.h"
/*
__ALIGN_BEGIN uint8_t USBD_HID_CUSTOM_ReportDesc[] __ALIGN_END = {
HID_USAGE_PAGE(GENERIC_DESKTOP),
HID_USAGE(JOYSTICK),
HID_COLLECTION(APPLICATION),
HID_COLLECTION(PHYSICAL),
HID_USAGE_PAGE(GENERIC_DESKTOP),
HID_USAGE(X),
HID_USAGE(Y),
HID_USAGE(Z),
HID_LOGICAL_MINIMUM(1, 0),
HID_LOGICAL_MAXIMUM(2, 1024),
HID_PHYSICAL_MINIMUM(1, 0),
HID_PHYSICAL_MAXIMUM(2, 1024),
HID_REPORT_SIZE(16),
HID_REPORT_COUNT(3),
HID_INPUT(DATA, VARIABLE, ABSOLUTE),
HID_END_COLLECTION(PHYSICAL),
HID_END_COLLECTION(APPLICATION)
};
*/
__ALIGN_BEGIN uint8_t USBD_HID_Joystick_ReportDesc[] __ALIGN_END = {
HID_USAGE_PAGE(GENERIC_DESKTOP),
HID_USAGE(JOYSTICK),
HID_COLLECTION(APPLICATION),
HID_REPORT_ID(20), // HID Joystick Report ID
HID_LOGICAL_MINIMUM(1, 0),
HID_LOGICAL_MAXIMUM(1, 1),
HID_REPORT_SIZE(1),
HID_REPORT_COUNT(32),
HID_USAGE_PAGE(BUTTON),
HID_USAGE_MINIMUM(1, 0),
HID_USAGE_MAXIMUM(1, 32),
HID_REPORT_SIZE(1),
HID_REPORT_COUNT(32),
HID_INPUT(DATA, VARIABLE, ABSOLUTE),
HID_USAGE_PAGE(GENERIC_DESKTOP),
HID_LOGICAL_MINIMUM(1, 0),
HID_LOGICAL_MAXIMUM(1, 7),
HID_PHYSICAL_MINIMUM(1, 0),
HID_PHYSICAL_MAXIMUM(2, 315),
HID_REPORT_SIZE(4),
HID_REPORT_COUNT(1),
HID_USAGE(HAT_SWITCH),
HID_INPUT(DATA, VARIABLE, ABSOLUTE),
HID_USAGE_PAGE(GENERIC_DESKTOP),
HID_USAGE(POINTER),
HID_COLLECTION(PHYSICAL),
HID_USAGE(X),
HID_USAGE(Y),
HID_USAGE(RX),
HID_USAGE(RY),
HID_LOGICAL_MINIMUM(1, 0),
HID_LOGICAL_MAXIMUM(2, 1022),
HID_REPORT_SIZE(10),
HID_REPORT_COUNT(4),
HID_INPUT(DATA, VARIABLE, ABSOLUTE),
HID_END_COLLECTION(PHYSICAL),
HID_USAGE(Z),
HID_USAGE(RZ),
HID_LOGICAL_MINIMUM(1, 0),
HID_LOGICAL_MAXIMUM(2, 1022),
HID_REPORT_SIZE(10),
HID_REPORT_COUNT(2),
HID_INPUT(DATA, VARIABLE, ABSOLUTE),
HID_END_COLLECTION(APPLICATION)
};

@ -25,20 +25,6 @@ TouchJoystick tjoystick_left;
TouchDpad tdpad_right;
TouchDpad tdpad_left;
const int16_t usb_x = 512;
const int16_t usb_y = 512;
const int16_t usb_r = 512;
/*
typedef struct
{
int16_t x:16;
int16_t y:16;
int16_t r:16;
} __packed TestReport_t;
TestReport_t testReport;
*/
void setup()
{
// Turn on LED
@ -49,14 +35,14 @@ void setup()
pinMode(TRIGGER_RIGHT_PIN, INPUT_ANALOG);
pinMode(TRACKPAD_CLICK_RIGHT_PIN, INPUT_PULLDOWN);
attachInterrupt(CLOCK_PIN_right, int_touchpad_right, FALLING);
trackpad_right.initialize(CLOCK_PIN_right, DATA_PIN_right);
attachInterrupt(CLOCK_PIN_left, int_touchpad_left, FALLING);
trackpad_left.initialize(CLOCK_PIN_left, DATA_PIN_left);
/*
//attachInterrupt(CLOCK_PIN_left, int_touchpad_left, FALLING);
//trackpad_left.initialize(CLOCK_PIN_left, DATA_PIN_left);
float ppmX = trackpad_right.getMaxY() / 62.5;
float ppmY = trackpad_right.getMaxX() / 103.9;
int32_t pos_x = 31.25 * ppmX;
@ -64,16 +50,16 @@ void setup()
int32_t pos_r = 70 * ppmX / 2;
int32_t dead_zone_inner = 3 * ppmX;
int32_t dead_zone_outer = 13 * ppmX;
tjoystick.init(pos_x, pos_y, pos_r, usb_x, usb_y, usb_r);
tjoystick.setDeadZoneInner(dead_zone_inner);
tjoystick.setDeadZoneOuter(dead_zone_outer);
tjoystick.setInvertY(true);
testReport = { 0, 0, 0 };
tjoystick_right.init(pos_x, pos_y, pos_r, USB_Device::usb_joystick_x, USB_Device::usb_joystick_y, USB_Device::usb_joystick_r);
tjoystick_right.setDeadZoneInner(dead_zone_inner);
tjoystick_right.setDeadZoneOuter(dead_zone_outer);
tjoystick_right.setInvertX();
tjoystick_right.setInvertY();
HID_Custom_Init();
*/
//tjoystick_left.init(pos_x, pos_y, pos_r, USB_Device::usb_joystick_x, USB_Device::usb_joystick_y, USB_Device::usb_joystick_r);
//tjoystick_left.setDeadZoneInner(dead_zone_inner);
//tjoystick_left.setDeadZoneOuter(dead_zone_outer);
device.begin();
@ -83,50 +69,61 @@ void setup()
void loop()
{
/*
uint32_t right_trigger = analogRead(TRIGGER_RIGHT_PIN);
uint8_t right_tp_click = digitalRead(TRACKPAD_CLICK_RIGHT_PIN);
//Serial.printf("RT: %u RTPC: %u\n", right_trigger, right_tp_click);
testReport.r = (int16_t)(right_trigger % 1024);
FingerPosition* fp;
int8_t fingers_touching = trackpad_right.poll(&fp);
if (fingers_touching > 0)
{
if (fp != NULL)
{
int16_t x, y;
for (int8_t id = 0; id < TrackPad::fingers_num; ++id)
for (uint8_t id = 0; id < TrackPad::fingers_num; ++id)
{
if (fingers_touching & (1 << id))
{
if (tjoystick.touch(fp[id].y, fp[id].x, &x, &y))
int8_t res = tjoystick_right.touch(fp[id].y, fp[id].x);
if (res > 0)
{
break;
switch(tjoystick_right.getControlType())
{
case TouchControl::CT_NONE:
break;
case TouchControl::CT_JOYSTICK:
int16_t x = tjoystick_right.getX();
int16_t y = tjoystick_right.getY();
device.joystick_left(x, y);
Serial.printf("(%i, %i) (%i, %i)\n", fp[id].x, fp[id].y, x, y);
break;
}
}
else
if (res < 0)
{
Serial.printf("Impossible Error\n");
}
else
{
device.joystick_left(USB_Device::usb_joystick_x, USB_Device::usb_joystick_y);
}
}
}
testReport.x = x;
testReport.y = y;
}
}
else
if (fingers_touching == 0)
{
testReport.x = usb_x;
testReport.y = usb_y;
device.joystick_left(USB_Device::usb_joystick_x, USB_Device::usb_joystick_y);
}
//Serial.printf("%u %u %u\n", testReport.x, testReport.y, testReport.r);
//device.move(testReport.x, testReport.y);
device.trigger_right(right_trigger);
device.button(0, right_tp_click);
HID_Custom_sendReport((uint8_t*)(&testReport), sizeof(testReport));
*/
//delay(100);
device.sendReport();
}

Loading…
Cancel
Save