Something went wrong. Try again.
Opensource firmware for NRF52840 Pro Micro that copycats the Steam Controller 2 Puck and adds an Xbox and Switch mode
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113#include "mode_ps5.h"#include "triton.h"#include "gamepad_util.h"#include "config.h"#include <Adafruit_TinyUSB.h>#include <Arduino.h>#include <string.h>
Ps5Controller g_ps5Ctl;
// DS5 calibration blob for feature report 0x05 (40 data bytes; TinyUSB prepends the 0x05 report id, so the// host receives the 41 bytes hid-playstation expects). hid-playstation/SDL read this to scale the raw IMU.// Layout (signed-LE16, kernel buf[1..] == blob[0..]): pitch/yaw/roll bias, pitch+/-, yaw+/-, roll+/-, speed+/-,// accel x/y/z +/-. Kernel math (all locals are `short`):// gyro: out = raw * (speed_2x * 1024) / (|pitch_plus| + |pitch_minus|), speed_2x = speed_plus + speed_minus// accel: out = (raw - bias) * (2*8192) / (acc_plus - acc_minus), bias = acc_plus - range/2// CRITICAL: speed_plus AND speed_minus are SPEED MAGNITUDES and must BOTH be POSITIVE -- they're summed. The// old blob stored speed_minus as -1024 (0xFC00), so speed_2x = 1024 + (-1024) = 0 -> sens_numer = 0 -> EVERY// gyro axis multiplied by zero -> dead gyro. Only the per-axis pitch/yaw/roll_minus are negative (they feed an// abs() range). Scale assumes the controller's raw IMU is the common 16-bit default ±2000 deg/s (16.384 LSB per// deg/s) and ±2 g (16384 LSB/g): gyro ratio = speed_2x*1024/range = 250*1024/4096 = 62.5 = 1024/16.384 (-> out// in the kernel's 1024-per-deg/s units); accel ratio = 16384/32768 = 0.5 (16384 LSB/g raw -> 8192 LSB/g out).// If the gyro/accel magnitude reads too hot or too cold, the controller's true LSB-per-unit differs -- scale// the pitch/yaw/roll +/- (gyro) or the accel +/- (accel) by the same factor. See PROTOCOL.md §8.static const uint8_t DS5_CALIB[] = { 0x00,0x00, 0x00,0x00, 0x00,0x00, // gyro pitch/yaw/roll bias = 0 0x00,0x08, 0x00,0xF8, // gyro pitch plus=+2048 / minus=-2048 0x00,0x08, 0x00,0xF8, // gyro yaw plus=+2048 / minus=-2048 0x00,0x08, 0x00,0xF8, // gyro roll plus=+2048 / minus=-2048 0x7D,0x00, 0x7D,0x00, // gyro speed+ =+125 / speed- =+125 (BOTH positive -> speed_2x=250) 0x00,0x40, 0x00,0xC0, // accel X plus=+16384 / minus=-16384 (range 32768 -> /2) 0x00,0x40, 0x00,0xC0, // accel Y 0x00,0x40, 0x00,0xC0, // accel Z 0x00,0x00, 0x00,0x00, 0x00,0x00, // padding to 40};
static uint16_t ps5Get(uint8_t rid, hid_report_type_t type, uint8_t* buf, uint16_t reqlen) { if (type != HID_REPORT_TYPE_FEATURE || reqlen == 0) return 0; memset(buf, 0, reqlen); if (rid == 0x05) { uint16_t n = (uint16_t)sizeof(DS5_CALIB); if (n > reqlen) n = reqlen; memcpy(buf, DS5_CALIB, n); return n; } return reqlen;}
static const uint8_t PS5_HID_DESC[]={ 0x05,0x01,0x09,0x05,0xA1,0x01,0x85,0x01,0x09,0x30,0x09,0x31,0x09,0x32,0x09,0x35, 0x09,0x33,0x09,0x34,0x15,0x00,0x26,0xFF,0x00,0x75,0x08,0x95,0x06,0x81,0x02,0x06, 0x00,0xFF,0x09,0x20,0x95,0x01,0x81,0x02,0x05,0x01,0x09,0x39,0x15,0x00,0x25,0x07, 0x35,0x00,0x46,0x3B,0x01,0x65,0x14,0x75,0x04,0x95,0x01,0x81,0x42,0x65,0x00,0x05, 0x09,0x19,0x01,0x29,0x0F,0x15,0x00,0x25,0x01,0x75,0x01,0x95,0x0F,0x81,0x02,0x06, 0x00,0xFF,0x09,0x21,0x95,0x0D,0x81,0x02,0x06,0x00,0xFF,0x09,0x22,0x15,0x00,0x26, 0xFF,0x00,0x75,0x08,0x95,0x34,0x81,0x02,0x85,0x02,0x09,0x23,0x95,0x2F,0x91,0x02, 0x85,0x05,0x09,0x33,0x95,0x28,0xB1,0x02,0x85,0x08,0x09,0x34,0x95,0x2F,0xB1,0x02, 0x85,0x09,0x09,0x24,0x95,0x13,0xB1,0x02,0x85,0x0A,0x09,0x25,0x95,0x1A,0xB1,0x02, 0x85,0x20,0x09,0x26,0x95,0x3F,0xB1,0x02,0x85,0x21,0x09,0x27,0x95,0x04,0xB1,0x02, 0x85,0x22,0x09,0x40,0x95,0x3F,0xB1,0x02,0x85,0x80,0x09,0x28,0x95,0x3F,0xB1,0x02, 0x85,0x81,0x09,0x29,0x95,0x3F,0xB1,0x02,0x85,0x82,0x09,0x2A,0x95,0x09,0xB1,0x02, 0x85,0x83,0x09,0x2B,0x95,0x3F,0xB1,0x02,0x85,0x84,0x09,0x2C,0x95,0x3F,0xB1,0x02, 0x85,0x85,0x09,0x2D,0x95,0x02,0xB1,0x02,0x85,0xA0,0x09,0x2E,0x95,0x01,0xB1,0x02, 0x85,0xE0,0x09,0x2F,0x95,0x3F,0xB1,0x02,0x85,0xF0,0x09,0x30,0x95,0x3F,0xB1,0x02, 0x85,0xF1,0x09,0x31,0x95,0x3F,0xB1,0x02,0x85,0xF2,0x09,0x32,0x95,0x0F,0xB1,0x02, 0x85,0xF4,0x09,0x35,0x95,0x3F,0xB1,0x02,0x85,0xF5,0x09,0x36,0x95,0x03,0xB1,0x02,0xC0};#define PS5_TOUCH_H 1080#define PS5_STATUS_USB 0x1A // charging + level 10 (~100%)static unsigned long g_ps5LastMs=0;static Adafruit_USBD_HID g_ps5;
static void ps5Build(uint8_t out[63]){ uint32_t b=psButtonsFromSteam(g_in.buttons); bool lTouch=(b&TB_LPADT)||(b&TB_LPADC), rTouch=(b&TB_RPADT)||(b&TB_RPADC); memset(out,0,63); out[0]=swStick(g_in.lx,false); out[1]=swStick(g_in.ly,true); out[2]=swStick(g_in.rx,false); out[3]=swStick(g_in.ry,true); out[4]=g_in.lt; out[5]=g_in.rt; static uint8_t seq=0; out[6]=seq++; out[7]=psHatNibble(b)|psFaceNibble(b); out[8]=psShouldersByte(b); out[9]=((b&TB_STEAM)?0x01:0)|((b&TB_LPADC||b&TB_RPADC)?0x02:0); out[15]=g_in.gx&0xFF; out[16]=g_in.gx>>8; out[17]=g_in.gz&0xFF; out[18]=g_in.gz>>8; out[19]=g_in.gy&0xFF; out[20]=g_in.gy>>8; out[21]=g_in.ax&0xFF; out[22]=g_in.ax>>8; out[23]=g_in.ay&0xFF; out[24]=g_in.ay>>8; out[25]=g_in.az&0xFF; out[26]=g_in.az>>8; uint16_t lx, ly, rx, ry; steamPadsToTouch(b, PS5_TOUCH_H, g_in.lpx, g_in.lpy, g_in.rpx, g_in.rpy, &lx, &ly, &rx, &ry); touchPackPads(out+32, lTouch, rTouch, lx, ly, rx, ry); out[52]=PS5_STATUS_USB;}
void Ps5Controller::begin(){ USBDevice.setID(0x054C, 0x0CE6); USBDevice.setDeviceVersion(0x0101); // bumped from 0x0100 for the added wake-mouse interface (Windows caches config by VID:PID:bcdDevice) USBDevice.setManufacturerDescriptor("Sony Interactive Entertainment"); USBDevice.setProductDescriptor("DualSense Wireless Controller"); g_ps5.enableOutEndpoint(true); g_ps5.setReportDescriptor(PS5_HID_DESC, sizeof PS5_HID_DESC); g_ps5.setReportCallback(ps5Get, nullptr); g_ps5.setPollInterval(4); g_ps5.begin();}void Ps5Controller::task(){ if(!g_ps5.ready()) return; if(millis()-g_ps5LastMs<USB_STREAM_MS) return; g_ps5LastMs=millis(); uint8_t p[63]; ps5Build(p); g_ps5.sendReport(0x01, p, sizeof p);}