- Dual-Core Architecture: Dedicated 1000Hz USB gaming pipeline on Core 1 and BLE/Wi-Fi/Web telemetry on Core 0. - Multi-Input BLE Host: Parsers for Xbox Wireless, Google Stadia, 8BitDo (Ultimate/Pro2/SN30), and Generic HOGP gamepads. - Multi-Console USB Emulation: Nintendo Switch, PC DirectInput, PS3, PS4, Xbox 360 (libxsm3 cryptographic challenge-response), Original Xbox, and Couch Mouse/Keyboard. - Onboard Web Portal & Captive Portal: Real-time SVG gamepad visualizer, stick deadzone calibration, button combo remapping, and Wi-Fi manager. - Low-Latency Radio Management: Interactive SoftAP toggle to drop to 0-latency gaming mode. - Dual OTA Firmware Flashing: Browser-based wireless firmware updater and automated Python OTA script. - Strict Build Quality: Clean build under -Wall -Wextra with zero warnings or errors. - Versioning & Licensing: Semantic Versioning v1.0.0 and GNU GPLv3 with Commercial Restrictions.
570 lines
23 KiB
C
570 lines
23 KiB
C
#include "usb_gamepad.h"
|
|
#include "switch_descriptors.h"
|
|
#include "dinput_descriptors.h"
|
|
#include "ogx_descriptors.h"
|
|
#include "ps4_descriptors.h"
|
|
#include "mouse_kb_descriptors.h"
|
|
#include "xinput_device.h"
|
|
#include "ble_hid_host.h"
|
|
#include "status_led.h"
|
|
#include "tinyusb.h"
|
|
#include "tusb.h"
|
|
#include "esp_log.h"
|
|
#include "freertos/FreeRTOS.h"
|
|
#include "freertos/task.h"
|
|
#include <string.h>
|
|
|
|
static const char *TAG = "USB_GAMEPAD";
|
|
static console_mode_t s_active_mode = CONSOLE_MODE_SWITCH;
|
|
static bool s_driver_installed = false;
|
|
|
|
static controller_state_t s_last_state;
|
|
static portMUX_TYPE s_state_mutex = portMUX_INITIALIZER_UNLOCKED;
|
|
|
|
void usb_gamepad_get_last_state(controller_state_t *st)
|
|
{
|
|
if (!st) return;
|
|
portENTER_CRITICAL(&s_state_mutex);
|
|
memcpy(st, &s_last_state, sizeof(controller_state_t));
|
|
portEXIT_CRITICAL(&s_state_mutex);
|
|
}
|
|
|
|
/* Device Descriptors */
|
|
static tusb_desc_device_t s_device_desc = {
|
|
.bLength = sizeof(tusb_desc_device_t),
|
|
.bDescriptorType = TUSB_DESC_DEVICE,
|
|
.bcdUSB = 0x0200,
|
|
.bDeviceClass = 0x00,
|
|
.bDeviceSubClass = 0x00,
|
|
.bDeviceProtocol = 0x00,
|
|
.bMaxPacketSize0 = 64,
|
|
.idVendor = SWITCH_VID,
|
|
.idProduct = SWITCH_PID,
|
|
.bcdDevice = 0x0100,
|
|
.iManufacturer = 0x01,
|
|
.iProduct = 0x02,
|
|
.iSerialNumber = 0x03,
|
|
.bNumConfigurations = 0x01
|
|
};
|
|
|
|
/* String Descriptors */
|
|
static const char *s_string_desc_switch[] = {
|
|
(char[]){0x09, 0x04}, /* Supported language: English (0x0409) */
|
|
"HORI CO.,LTD.", /* Manufacturer */
|
|
"POKKEN TOURNAMENT DX Pro Pad", /* Product */
|
|
"000000000001" /* Serial */
|
|
};
|
|
|
|
static const char *s_string_desc_pc[] = {
|
|
(char[]){0x09, 0x04},
|
|
"Generic USB",
|
|
"PC Game Controller",
|
|
"000000000002"
|
|
};
|
|
|
|
static const char *s_string_desc_ps3[] = {
|
|
(char[]){0x09, 0x04},
|
|
"Sony Computer Entertainment",
|
|
"PS3 Game Controller",
|
|
"000000000003"
|
|
};
|
|
|
|
static const char *s_string_desc_ps4[] = {
|
|
(char[]){0x09, 0x04},
|
|
"Sony Interactive Entertainment",
|
|
"Wireless Controller",
|
|
"000000000004"
|
|
};
|
|
|
|
/* Full Speed Configuration Descriptors (34 / 41 bytes) */
|
|
static uint8_t s_config_desc_switch[34] = {
|
|
/* Configuration Descriptor (9 bytes) */
|
|
0x09, TUSB_DESC_CONFIGURATION, 34, 0x00, 1, 1, 0, 0x80, 50,
|
|
/* Interface Descriptor (9 bytes) */
|
|
0x09, TUSB_DESC_INTERFACE, 0, 0, 1, TUSB_CLASS_HID, 0, 0, 0,
|
|
/* HID Descriptor (9 bytes) */
|
|
0x09, 0x21, 0x11, 0x01, 0, 1, 0x22, (sizeof(switch_hid_report_descriptor) & 0xFF), ((sizeof(switch_hid_report_descriptor) >> 8) & 0xFF),
|
|
/* Endpoint IN Descriptor (7 bytes) */
|
|
0x07, TUSB_DESC_ENDPOINT, 0x81, 0x03, 64, 0x00, 8
|
|
};
|
|
|
|
static uint8_t s_config_desc_pc[34] = {
|
|
0x09, TUSB_DESC_CONFIGURATION, 34, 0x00, 1, 1, 0, 0x80, 50,
|
|
0x09, TUSB_DESC_INTERFACE, 0, 0, 1, TUSB_CLASS_HID, 0, 0, 0,
|
|
0x09, 0x21, 0x11, 0x01, 0, 1, 0x22, (sizeof(dinput_hid_report_descriptor) & 0xFF), ((sizeof(dinput_hid_report_descriptor) >> 8) & 0xFF),
|
|
0x07, TUSB_DESC_ENDPOINT, 0x81, 0x03, 64, 0x00, 8
|
|
};
|
|
|
|
static uint8_t s_config_desc_ps4[41] = {
|
|
/* Configuration Descriptor (9 bytes) */
|
|
0x09, TUSB_DESC_CONFIGURATION, 41, 0x00, 1, 1, 0, 0xC0, 250,
|
|
/* Interface Descriptor (9 bytes) */
|
|
0x09, TUSB_DESC_INTERFACE, 0, 0, 2, TUSB_CLASS_HID, 0, 0, 0,
|
|
/* HID Descriptor (9 bytes) */
|
|
0x09, 0x21, 0x11, 0x01, 0, 1, 0x22, (sizeof(ps4_hid_report_descriptor) & 0xFF), ((sizeof(ps4_hid_report_descriptor) >> 8) & 0xFF),
|
|
/* Endpoint IN Descriptor (7 bytes) */
|
|
0x07, TUSB_DESC_ENDPOINT, 0x81, 0x03, 64, 0x00, 4,
|
|
/* Endpoint OUT Descriptor (7 bytes) */
|
|
0x07, TUSB_DESC_ENDPOINT, 0x01, 0x03, 64, 0x00, 4
|
|
};
|
|
|
|
static uint8_t s_config_desc_mouse_kb[34] = {
|
|
0x09, TUSB_DESC_CONFIGURATION, 34, 0x00, 1, 1, 0, 0xA0, 50,
|
|
0x09, TUSB_DESC_INTERFACE, 0, 0, 1, TUSB_CLASS_HID, 0, 0, 0,
|
|
0x09, 0x21, 0x11, 0x01, 0, 1, 0x22, (sizeof(mouse_kb_hid_report_descriptor) & 0xFF), ((sizeof(mouse_kb_hid_report_descriptor) >> 8) & 0xFF),
|
|
0x07, TUSB_DESC_ENDPOINT, 0x81, 0x03, 16, 0x00, 8
|
|
};
|
|
|
|
static char const *s_string_desc_mouse_kb[] = {
|
|
(const char[]){ 0x09, 0x04 }, /* 0: Language English */
|
|
"Antigravity", /* 1: Manufacturer */
|
|
"Couch Mouse & Keyboard", /* 2: Product */
|
|
"1.0.0", /* 3: Serial */
|
|
};
|
|
|
|
/* --- TinyUSB Application Callbacks --- */
|
|
|
|
uint8_t const * tud_hid_descriptor_report_cb(uint8_t instance)
|
|
{
|
|
(void)instance;
|
|
if (s_active_mode == CONSOLE_MODE_SWITCH) {
|
|
return switch_hid_report_descriptor;
|
|
} else if (s_active_mode == CONSOLE_MODE_PS4) {
|
|
return ps4_hid_report_descriptor;
|
|
} else if (s_active_mode == CONSOLE_MODE_MOUSE_KB) {
|
|
return mouse_kb_hid_report_descriptor;
|
|
} else if (s_active_mode == CONSOLE_MODE_XBOX360 || s_active_mode == CONSOLE_MODE_XBOXOG) {
|
|
return NULL;
|
|
} else {
|
|
return dinput_hid_report_descriptor;
|
|
}
|
|
}
|
|
|
|
static void build_switch_report(const controller_state_t *state, switch_report_t *rep)
|
|
{
|
|
memset(rep, 0, sizeof(switch_report_t));
|
|
|
|
/* Buttons */
|
|
if (state->btn_y) rep->buttons |= SWITCH_BTN_Y;
|
|
if (state->btn_b) rep->buttons |= SWITCH_BTN_B;
|
|
if (state->btn_a) rep->buttons |= SWITCH_BTN_A;
|
|
if (state->btn_x) rep->buttons |= SWITCH_BTN_X;
|
|
if (state->btn_lb) rep->buttons |= SWITCH_BTN_L;
|
|
if (state->btn_rb) rep->buttons |= SWITCH_BTN_R;
|
|
if (state->btn_lt) rep->buttons |= SWITCH_BTN_ZL;
|
|
if (state->btn_rt) rep->buttons |= SWITCH_BTN_ZR;
|
|
if (state->btn_select) rep->buttons |= SWITCH_BTN_MINUS;
|
|
if (state->btn_start) rep->buttons |= SWITCH_BTN_PLUS;
|
|
if (state->btn_l3) rep->buttons |= SWITCH_BTN_LCLICK;
|
|
if (state->btn_r3) rep->buttons |= SWITCH_BTN_RCLICK;
|
|
if (state->btn_system) rep->buttons |= SWITCH_BTN_HOME;
|
|
if (state->btn_share) rep->buttons |= SWITCH_BTN_CAPTURE;
|
|
|
|
/* Hat Switch (D-Pad) */
|
|
if (state->dpad_up && state->dpad_right) rep->hat = SWITCH_HAT_UP_RIGHT;
|
|
else if (state->dpad_down && state->dpad_right) rep->hat = SWITCH_HAT_DOWN_RIGHT;
|
|
else if (state->dpad_down && state->dpad_left) rep->hat = SWITCH_HAT_DOWN_LEFT;
|
|
else if (state->dpad_up && state->dpad_left) rep->hat = SWITCH_HAT_UP_LEFT;
|
|
else if (state->dpad_up) rep->hat = SWITCH_HAT_UP;
|
|
else if (state->dpad_right) rep->hat = SWITCH_HAT_RIGHT;
|
|
else if (state->dpad_down) rep->hat = SWITCH_HAT_DOWN;
|
|
else if (state->dpad_left) rep->hat = SWITCH_HAT_LEFT;
|
|
else rep->hat = SWITCH_HAT_CENTER;
|
|
|
|
/* Sticks: 0..255, 128 center, Y axis inverted: 0 is top */
|
|
rep->lx = (uint8_t)(((int32_t)state->lx + 32768) >> 8);
|
|
rep->ly = (uint8_t)(((int32_t)32767 - state->ly) >> 8);
|
|
rep->rx = (uint8_t)(((int32_t)state->rx + 32768) >> 8);
|
|
rep->ry = (uint8_t)(((int32_t)32767 - state->ry) >> 8);
|
|
rep->vendor = 0x00;
|
|
}
|
|
|
|
static void build_dinput_report(const controller_state_t *state, dinput_report_t *rep)
|
|
{
|
|
memset(rep, 0, sizeof(dinput_report_t));
|
|
|
|
/* Sticks: X (lx), Y (ly), Rx (rx), Ry (ry) */
|
|
rep->lx = (uint8_t)(((int32_t)state->lx + 32768) >> 8);
|
|
rep->ly = (uint8_t)(((int32_t)32767 - state->ly) >> 8);
|
|
rep->rx = (uint8_t)(((int32_t)state->rx + 32768) >> 8);
|
|
rep->ry = (uint8_t)(((int32_t)32767 - state->ry) >> 8);
|
|
|
|
/* Triggers: Z (lt), Rz (rt) */
|
|
rep->lt = state->lt;
|
|
rep->rt = state->rt;
|
|
|
|
/* Hat: 0..7 directions, 8 neutral */
|
|
if (state->dpad_up && state->dpad_right) rep->hat = 1;
|
|
else if (state->dpad_right && state->dpad_down) rep->hat = 3;
|
|
else if (state->dpad_down && state->dpad_left) rep->hat = 5;
|
|
else if (state->dpad_left && state->dpad_up) rep->hat = 7;
|
|
else if (state->dpad_up) rep->hat = 0;
|
|
else if (state->dpad_right) rep->hat = 2;
|
|
else if (state->dpad_down) rep->hat = 4;
|
|
else if (state->dpad_left) rep->hat = 6;
|
|
else rep->hat = 8;
|
|
|
|
/* Buttons bitmask: Standard Gamepad API ordering */
|
|
if (state->btn_a) rep->buttons |= (1UL << 0);
|
|
if (state->btn_b) rep->buttons |= (1UL << 1);
|
|
if (state->btn_x) rep->buttons |= (1UL << 2);
|
|
if (state->btn_y) rep->buttons |= (1UL << 3);
|
|
if (state->btn_lb) rep->buttons |= (1UL << 4);
|
|
if (state->btn_rb) rep->buttons |= (1UL << 5);
|
|
if (state->btn_lt) rep->buttons |= (1UL << 6);
|
|
if (state->btn_rt) rep->buttons |= (1UL << 7);
|
|
if (state->btn_select) rep->buttons |= (1UL << 8);
|
|
if (state->btn_start) rep->buttons |= (1UL << 9);
|
|
if (state->btn_l3) rep->buttons |= (1UL << 10);
|
|
if (state->btn_r3) rep->buttons |= (1UL << 11);
|
|
/* Buttons 12-15: Standard D-Pad buttons */
|
|
if (state->dpad_up) rep->buttons |= (1UL << 12);
|
|
if (state->dpad_down) rep->buttons |= (1UL << 13);
|
|
if (state->dpad_left) rep->buttons |= (1UL << 14);
|
|
if (state->dpad_right) rep->buttons |= (1UL << 15);
|
|
/* Buttons 16-17: System / Guide / Home and Share / Capture */
|
|
if (state->btn_system) rep->buttons |= (1UL << 16);
|
|
if (state->btn_share) rep->buttons |= (1UL << 17);
|
|
}
|
|
|
|
static void build_ps4_report(const controller_state_t *state, ps4_report_t *rep)
|
|
{
|
|
static uint16_t timer_tick = 0;
|
|
static uint8_t packet_counter = 0;
|
|
|
|
memset(rep, 0, sizeof(ps4_report_t));
|
|
rep->report_id = 0x01;
|
|
|
|
/* Sticks: 0..255, 128 center, Y axis: 0 is top */
|
|
rep->lx = (uint8_t)(((int32_t)state->lx + 32768) >> 8);
|
|
rep->ly = (uint8_t)(((int32_t)32767 - state->ly) >> 8);
|
|
rep->rx = (uint8_t)(((int32_t)state->rx + 32768) >> 8);
|
|
rep->ry = (uint8_t)(((int32_t)32767 - state->ry) >> 8);
|
|
|
|
/* Hat Switch (D-Pad) */
|
|
uint8_t hat = PS4_HAT_CENTER;
|
|
if (state->dpad_up && state->dpad_right) hat = PS4_HAT_UP_RIGHT;
|
|
else if (state->dpad_down && state->dpad_right) hat = PS4_HAT_DOWN_RIGHT;
|
|
else if (state->dpad_down && state->dpad_left) hat = PS4_HAT_DOWN_LEFT;
|
|
else if (state->dpad_up && state->dpad_left) hat = PS4_HAT_UP_LEFT;
|
|
else if (state->dpad_up) hat = PS4_HAT_UP;
|
|
else if (state->dpad_right) hat = PS4_HAT_RIGHT;
|
|
else if (state->dpad_down) hat = PS4_HAT_DOWN;
|
|
else if (state->dpad_left) hat = PS4_HAT_LEFT;
|
|
|
|
rep->dpad_and_buttons1 = hat & 0x0F;
|
|
|
|
/* Face buttons (Square, Cross, Circle, Triangle) */
|
|
if (state->btn_x) rep->dpad_and_buttons1 |= PS4_BTN_SQUARE;
|
|
if (state->btn_a) rep->dpad_and_buttons1 |= PS4_BTN_CROSS;
|
|
if (state->btn_b) rep->dpad_and_buttons1 |= PS4_BTN_CIRCLE;
|
|
if (state->btn_y) rep->dpad_and_buttons1 |= PS4_BTN_TRIANGLE;
|
|
|
|
/* Shoulder buttons & Menu */
|
|
if (state->btn_lb) rep->buttons2 |= PS4_BTN_L1;
|
|
if (state->btn_rb) rep->buttons2 |= PS4_BTN_R1;
|
|
if (state->btn_lt) rep->buttons2 |= PS4_BTN_L2;
|
|
if (state->btn_rt) rep->buttons2 |= PS4_BTN_R2;
|
|
if (state->btn_select) rep->buttons2 |= PS4_BTN_SHARE;
|
|
if (state->btn_start) rep->buttons2 |= PS4_BTN_OPTIONS;
|
|
if (state->btn_l3) rep->buttons2 |= PS4_BTN_L3;
|
|
if (state->btn_r3) rep->buttons2 |= PS4_BTN_R3;
|
|
|
|
/* System & Touchpad buttons */
|
|
if (state->btn_system) rep->buttons3 |= PS4_BTN_PS;
|
|
/* Map Share / Capture button to Touchpad Click */
|
|
if (state->btn_share) rep->buttons3 |= PS4_BTN_TOUCHPAD;
|
|
|
|
/* Rolling packet counter */
|
|
rep->buttons3 |= ((packet_counter++ & 0x3F) << 2);
|
|
|
|
/* Analog triggers (0..255) */
|
|
rep->lt = state->lt;
|
|
rep->rt = state->rt;
|
|
|
|
/* Timestamp and battery */
|
|
rep->timestamp = timer_tick++;
|
|
rep->battery = 0x0B; /* 100% full, cable connected */
|
|
|
|
/* Touchpad packet (finger not touching: bit 7 = 1) */
|
|
rep->reserved[10] = 0x01;
|
|
rep->reserved[11] = 0x80;
|
|
rep->reserved[15] = 0x80;
|
|
}
|
|
|
|
uint16_t tud_hid_get_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen)
|
|
{
|
|
(void)instance;
|
|
(void)report_id;
|
|
(void)report_type;
|
|
|
|
controller_state_t current;
|
|
portENTER_CRITICAL(&s_state_mutex);
|
|
memcpy(¤t, &s_last_state, sizeof(controller_state_t));
|
|
portEXIT_CRITICAL(&s_state_mutex);
|
|
|
|
if (s_active_mode == CONSOLE_MODE_SWITCH) {
|
|
switch_report_t rep;
|
|
build_switch_report(¤t, &rep);
|
|
uint16_t copy_len = (reqlen < sizeof(rep)) ? reqlen : sizeof(rep);
|
|
memcpy(buffer, &rep, copy_len);
|
|
return copy_len;
|
|
} else if (s_active_mode == CONSOLE_MODE_PS4) {
|
|
ps4_report_t rep;
|
|
build_ps4_report(¤t, &rep);
|
|
uint16_t copy_len = (reqlen < sizeof(rep)) ? reqlen : sizeof(rep);
|
|
memcpy(buffer, &rep, copy_len);
|
|
return copy_len;
|
|
} else {
|
|
dinput_report_t rep;
|
|
build_dinput_report(¤t, &rep);
|
|
uint16_t copy_len = (reqlen < sizeof(rep)) ? reqlen : sizeof(rep);
|
|
memcpy(buffer, &rep, copy_len);
|
|
return copy_len;
|
|
}
|
|
}
|
|
|
|
void tud_hid_set_report_cb(uint8_t instance, uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize)
|
|
{
|
|
(void)instance;
|
|
(void)report_id;
|
|
if (report_type == HID_REPORT_TYPE_OUTPUT && buffer && bufsize > 0) {
|
|
if (s_active_mode == CONSOLE_MODE_PS3) {
|
|
/* PS3 DualShock 3 Output Report:
|
|
* Byte 2 = Right small motor (0 or 1 on/off)
|
|
* Byte 4 = Left large motor (0-255)
|
|
*/
|
|
if (bufsize >= 5) {
|
|
uint8_t light = buffer[2] ? 255 : 0;
|
|
uint8_t heavy = buffer[4];
|
|
ble_hid_host_send_rumble(heavy, light);
|
|
}
|
|
} else if (s_active_mode == CONSOLE_MODE_PC_DINPUT) {
|
|
/* PC Generic Output Report: [heavy, light] */
|
|
if (bufsize >= 2) {
|
|
ble_hid_host_send_rumble(buffer[0], buffer[1]);
|
|
}
|
|
} else if (s_active_mode == CONSOLE_MODE_PS4) {
|
|
/* PS4 DualShock 4 Output Report (Report ID 0x05):
|
|
* If buffer[0] == 0x05:
|
|
* buffer[4] = Light / small motor (0-255)
|
|
* buffer[5] = Heavy / large motor (0-255)
|
|
* buffer[6] = Lightbar Red (0-255)
|
|
* buffer[7] = Lightbar Green (0-255)
|
|
* buffer[8] = Lightbar Blue (0-255)
|
|
*/
|
|
size_t off = (buffer[0] == 0x05) ? 1 : 0;
|
|
if (bufsize >= off + 8) {
|
|
uint8_t light = buffer[off + 3];
|
|
uint8_t heavy = buffer[off + 4];
|
|
ble_hid_host_send_rumble(heavy, light);
|
|
|
|
uint8_t r = buffer[off + 5];
|
|
uint8_t g = buffer[off + 6];
|
|
uint8_t b = buffer[off + 7];
|
|
if (r > 0 || g > 0 || b > 0) {
|
|
status_led_flash_color(r, g, b, 2000);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/* Background task ensuring reports are constantly streamed to prevent uninitialized host reads */
|
|
static void usb_stream_task(void *pvParameters)
|
|
{
|
|
while (1) {
|
|
if (tud_mounted()) {
|
|
controller_state_t current;
|
|
portENTER_CRITICAL(&s_state_mutex);
|
|
memcpy(¤t, &s_last_state, sizeof(controller_state_t));
|
|
portEXIT_CRITICAL(&s_state_mutex);
|
|
|
|
if (s_active_mode == CONSOLE_MODE_XBOX360) {
|
|
xinput_in_report_t rep;
|
|
xinput_device_build_report(¤t, &rep);
|
|
xinput_device_send_report(&rep);
|
|
} else if (s_active_mode == CONSOLE_MODE_XBOXOG) {
|
|
ogx_report_t rep;
|
|
ogx_build_report(¤t, &rep);
|
|
ogx_device_send_report(&rep, sizeof(rep));
|
|
} else if (tud_hid_ready()) {
|
|
if (s_active_mode == CONSOLE_MODE_SWITCH) {
|
|
switch_report_t rep;
|
|
build_switch_report(¤t, &rep);
|
|
tud_hid_report(0, &rep, sizeof(rep));
|
|
} else if (s_active_mode == CONSOLE_MODE_PS4) {
|
|
ps4_report_t rep;
|
|
build_ps4_report(¤t, &rep);
|
|
tud_hid_report(0, &rep, sizeof(rep));
|
|
} else if (s_active_mode == CONSOLE_MODE_MOUSE_KB) {
|
|
/* Mouse movement calculation */
|
|
int16_t lx = current.lx;
|
|
int16_t ly = current.ly;
|
|
int8_t dx = 0, dy = 0;
|
|
if (abs(lx) > 3000) {
|
|
int val = (lx > 0 ? (lx - 3000) : (lx + 3000)) / 2200;
|
|
if (val > 120) val = 120;
|
|
if (val < -120) val = -120;
|
|
dx = (int8_t)val;
|
|
}
|
|
if (abs(ly) > 3000) {
|
|
int val = -(ly > 0 ? (ly - 3000) : (ly + 3000)) / 2200;
|
|
if (val > 120) val = 120;
|
|
if (val < -120) val = -120;
|
|
dy = (int8_t)val;
|
|
}
|
|
|
|
int8_t wheel = 0;
|
|
int16_t ry = current.ry;
|
|
if (abs(ry) > 6000) {
|
|
wheel = (ry > 0 ? 1 : -1) * (abs(ry) > 18000 ? 2 : 1);
|
|
}
|
|
|
|
uint8_t btns = 0;
|
|
if (current.btn_a || current.btn_lt || current.btn_lb) btns |= 0x01; /* Left Click */
|
|
if (current.btn_b || current.btn_rt || current.btn_rb) btns |= 0x02; /* Right Click */
|
|
if (current.btn_l3 || current.btn_x) btns |= 0x04; /* Middle Click */
|
|
|
|
mouse_report_payload_t m_rep = {
|
|
.buttons = btns,
|
|
.x = dx,
|
|
.y = dy,
|
|
.wheel = wheel
|
|
};
|
|
tud_hid_report(1, &m_rep, sizeof(m_rep));
|
|
|
|
/* Keyboard / Navigation keys */
|
|
keyboard_report_payload_t k_rep = {0};
|
|
uint8_t k_idx = 0;
|
|
if (current.dpad_up && k_idx < 6) k_rep.keycodes[k_idx++] = 0x52; /* Up Arrow */
|
|
if (current.dpad_down && k_idx < 6) k_rep.keycodes[k_idx++] = 0x51; /* Down Arrow */
|
|
if (current.dpad_left && k_idx < 6) k_rep.keycodes[k_idx++] = 0x50; /* Left Arrow */
|
|
if (current.dpad_right && k_idx < 6) k_rep.keycodes[k_idx++] = 0x4F; /* Right Arrow */
|
|
if (current.btn_y && k_idx < 6) k_rep.keycodes[k_idx++] = 0x28; /* Enter */
|
|
if (current.btn_select && k_idx < 6) k_rep.keycodes[k_idx++] = 0x2C; /* Space */
|
|
if (current.btn_start && k_idx < 6) k_rep.keycodes[k_idx++] = 0x29; /* Escape */
|
|
|
|
static uint8_t s_last_k_keys[6] = {0};
|
|
if (memcmp(s_last_k_keys, k_rep.keycodes, 6) != 0) {
|
|
memcpy(s_last_k_keys, k_rep.keycodes, 6);
|
|
tud_hid_report(2, &k_rep, sizeof(k_rep));
|
|
}
|
|
} else {
|
|
dinput_report_t rep;
|
|
build_dinput_report(¤t, &rep);
|
|
tud_hid_report(0, &rep, sizeof(rep));
|
|
}
|
|
}
|
|
}
|
|
vTaskDelay(pdMS_TO_TICKS(10));
|
|
}
|
|
}
|
|
|
|
/* --- Public API --- */
|
|
|
|
esp_err_t usb_gamepad_init(console_mode_t initial_mode)
|
|
{
|
|
s_active_mode = initial_mode;
|
|
|
|
/* Initialize last state to neutral */
|
|
portENTER_CRITICAL(&s_state_mutex);
|
|
controller_state_reset(&s_last_state);
|
|
portEXIT_CRITICAL(&s_state_mutex);
|
|
|
|
tinyusb_config_t tusb_cfg = {
|
|
.device_descriptor = &s_device_desc,
|
|
.string_descriptor = s_string_desc_pc,
|
|
.string_descriptor_count = 4,
|
|
.external_phy = false,
|
|
.configuration_descriptor = s_config_desc_pc,
|
|
};
|
|
|
|
if (s_active_mode == CONSOLE_MODE_SWITCH) {
|
|
s_device_desc.idVendor = SWITCH_VID;
|
|
s_device_desc.idProduct = SWITCH_PID;
|
|
tusb_cfg.string_descriptor = s_string_desc_switch;
|
|
tusb_cfg.configuration_descriptor = s_config_desc_switch;
|
|
} else if (s_active_mode == CONSOLE_MODE_PS4) {
|
|
s_device_desc.idVendor = PS4_VID;
|
|
s_device_desc.idProduct = PS4_PID;
|
|
tusb_cfg.string_descriptor = s_string_desc_ps4;
|
|
tusb_cfg.configuration_descriptor = s_config_desc_ps4;
|
|
} else if (s_active_mode == CONSOLE_MODE_MOUSE_KB) {
|
|
s_device_desc.idVendor = 0x046D; /* Logitech */
|
|
s_device_desc.idProduct = 0xC52B; /* Wireless Receiver */
|
|
tusb_cfg.string_descriptor = s_string_desc_mouse_kb;
|
|
tusb_cfg.configuration_descriptor = s_config_desc_mouse_kb;
|
|
} else if (s_active_mode == CONSOLE_MODE_PS3) {
|
|
s_device_desc.idVendor = 0x054C; /* Sony PS3 */
|
|
s_device_desc.idProduct = 0x0268;
|
|
tusb_cfg.string_descriptor = s_string_desc_ps3;
|
|
tusb_cfg.configuration_descriptor = s_config_desc_pc;
|
|
} else if (s_active_mode == CONSOLE_MODE_XBOX360) {
|
|
xinput_device_init();
|
|
tusb_cfg.device_descriptor = &xinput_device_desc;
|
|
tusb_cfg.configuration_descriptor = xinput_config_desc;
|
|
tusb_cfg.string_descriptor = xinput_string_desc;
|
|
tusb_cfg.string_descriptor_count = 5;
|
|
} else if (s_active_mode == CONSOLE_MODE_XBOXOG) {
|
|
ogx_device_init();
|
|
tusb_cfg.device_descriptor = &ogx_device_desc;
|
|
tusb_cfg.configuration_descriptor = ogx_config_desc;
|
|
tusb_cfg.string_descriptor = ogx_string_desc;
|
|
tusb_cfg.string_descriptor_count = 4;
|
|
} else {
|
|
s_device_desc.idVendor = DINPUT_VID;
|
|
s_device_desc.idProduct = DINPUT_PID;
|
|
tusb_cfg.string_descriptor = s_string_desc_pc;
|
|
tusb_cfg.configuration_descriptor = s_config_desc_pc;
|
|
}
|
|
|
|
esp_err_t err = tinyusb_driver_install(&tusb_cfg);
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "TinyUSB install failed: %s", esp_err_to_name(err));
|
|
return err;
|
|
}
|
|
|
|
s_driver_installed = true;
|
|
ESP_LOGI(TAG, "USB Gamepad initialized in mode: %s (VID:0x%04X PID:0x%04X)",
|
|
profile_manager_mode_name(s_active_mode),
|
|
tusb_cfg.device_descriptor->idVendor,
|
|
tusb_cfg.device_descriptor->idProduct);
|
|
|
|
static bool task_created = false;
|
|
if (!task_created) {
|
|
/* Pin real-time USB transmission to Core 1 (APP CPU) with high priority */
|
|
xTaskCreatePinnedToCore(usb_stream_task, "usb_stream_task", 4096, NULL, 6, NULL, 1);
|
|
task_created = true;
|
|
}
|
|
|
|
return ESP_OK;
|
|
}
|
|
|
|
esp_err_t usb_gamepad_change_mode(console_mode_t new_mode)
|
|
{
|
|
if (s_driver_installed) {
|
|
ESP_LOGI(TAG, "Re-enumerating USB for new console profile...");
|
|
tinyusb_driver_uninstall();
|
|
s_driver_installed = false;
|
|
vTaskDelay(pdMS_TO_TICKS(200));
|
|
}
|
|
return usb_gamepad_init(new_mode);
|
|
}
|
|
|
|
bool usb_gamepad_is_mounted(void)
|
|
{
|
|
return tud_mounted();
|
|
}
|
|
|
|
esp_err_t usb_gamepad_send_state(const controller_state_t *state)
|
|
{
|
|
if (!state) return ESP_ERR_INVALID_ARG;
|
|
|
|
portENTER_CRITICAL(&s_state_mutex);
|
|
memcpy(&s_last_state, state, sizeof(controller_state_t));
|
|
portEXIT_CRITICAL(&s_state_mutex);
|
|
|
|
return ESP_OK;
|
|
}
|