config.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // config.h -- USB-presentation modes + persisted, runtime-tunable settings.
  2. //
  3. // One puck speaks the SAME RF protocol no matter which USB face it wears; only the USB enumeration and the
  4. // input-report mapping change. The active face is g_usbMode, persisted to flash (cfg.bin). Tunables here are
  5. // set over the CDC console (serial_console.cpp) and the WebUSB panel (webusb_config.cpp) and likewise persist.
  6. #pragma once
  7. #include <stdint.h>
  8. // Compile-time logging / diagnostics. 0 (default) = PRODUCTION build: no host->controller capture ring
  9. // (reclaims ~80KB RAM), no per-event logging, no main-loop section timing, no WebUSB capture channel.
  10. // Functional bits (haptic reconnect block, buzz-clear re-init, rate stats) stay. -DOPK_LOG=1 for diagnostics.
  11. #ifndef OPK_LOG
  12. #define OPK_LOG 0
  13. #endif
  14. // Build-time FACTORY RESET (recovery build). 0 (default) = normal. -DOPK_FACTORY_RESET=1 wipes ALL persistent
  15. // storage (cfg.bin + bonds.bin) ONCE -- on the first boot after flashing -- then persists normally. A git-hash
  16. // tag file (written after the wipe) records that this build already reset, so subsequent boots skip it. Flashing
  17. // a DIFFERENT build re-triggers the one-time wipe. Re-pair the controller after the reset. See factoryResetOnce().
  18. #ifndef OPK_FACTORY_RESET
  19. #define OPK_FACTORY_RESET 0
  20. #endif
  21. // Optional GPIO trigger wired to the HOST motherboard's front-panel power-switch header: fires a
  22. // momentary pulse when the paired Steam Controller's STEAM button is short-pressed WHILE the host
  23. // is off (USB not enumerated). 0 (default) = feature compiled out entirely -- no pin driven, no
  24. // behavior change for anyone without the extra wiring. -DOPK_PWR_SWITCH=1 to enable. See
  25. // pwr_switch.h for the pin/timing knobs.
  26. #ifndef OPK_PWR_SWITCH
  27. #define OPK_PWR_SWITCH 0
  28. #endif
  29. // ---- USB presentation modes (g_usbMode). RF poll/relay is identical across all; only USB enumeration +
  30. // report mapping differ. ----
  31. #define MODE_STEAM 0 // Valve puck; auto-lizard when Steam closed
  32. #define MODE_XBOX 1 // XInput + right-pad mouse
  33. #define MODE_SW_HORI 2 // HORIPAD (Switch console whitelist)
  34. #define MODE_LIZARD \
  35. 3 // Puck HID; always keyboard+mouse (ignores Steam heartbeat)
  36. #define MODE_SW_PRO 4 // Nintendo Switch Pro Controller (057E:2009) + gyro
  37. // Sony DualSense (054C:0CE6) + gyro + split trackpad (+ wake mouse + WebUSB panel)
  38. #define MODE_PS5 5
  39. // DS4-layout generic HID gamepad + gyro (+ wake mouse + WebUSB panel)
  40. #define MODE_HIDGYRO 6
  41. // DualSense, CLEAN single-HID (no wake/WebUSB) so PC games classify it as PlayStation (Fortnite)
  42. #define MODE_PS5_GAME 7
  43. // DS4, CLEAN single-HID (no wake/WebUSB) for game PlayStation classification
  44. #define MODE_DS4_GAME 8
  45. // Sony DualShock 3 / Sixaxis (054C:0268) -- CLEAN single-HID so it enumerates on a REAL PS3 console (the
  46. // console wants a bare Sixaxis HID, not a composite). Answers the PS3's GET_REPORT(0xF2/0xF5/0xEF/0x01)
  47. // enable handshake. + gyro/accel + rumble.
  48. #define MODE_PS3 9
  49. // Microsoft Original Xbox Controller S (045E:0289)
  50. #define MODE_XBOX_OG 10
  51. // Generic DirectInput joystick -- presents EVERY analog input at once (sticks, triggers, both trackpads, gyro)
  52. // as two DirectInput devices, for flight/space sims that bind axes through DirectInput rather than XInput.
  53. #define MODE_DINPUT 11
  54. // SInput: the open SDL-native gamepad protocol (docs.handheldlegend.com/s/sinput). Sticks + analog triggers +
  55. // gyro/accel + BOTH trackpads + battery, all bound natively by SDL3 / Steam Input with no impersonation.
  56. #define MODE_SINPUT 12
  57. #define MODE_MAX 12
  58. // The two "game" personalities drop the wake-mouse + WebUSB interfaces so the device is a genuine single-HID PS
  59. // controller (some PC games -- e.g. Fortnite/UE GameInput -- refuse PS classification when extra interfaces are
  60. // present). Cost: no config panel / host-wake while in these modes; chord back to Steam (back4 + A) for the panel.
  61. static inline bool modeIsCleanPS(uint8_t m)
  62. {
  63. return m == MODE_PS5_GAME || m == MODE_DS4_GAME || m == MODE_PS3;
  64. }
  65. static inline bool modeIsPuck(uint8_t m)
  66. {
  67. return m == MODE_STEAM || m == MODE_LIZARD;
  68. }
  69. static inline bool modeValid(uint8_t m)
  70. {
  71. return m <= MODE_MAX;
  72. }
  73. // ---- Emulated controller TYPES. Button-remap config (back paddles / QAM / A-B swap / trackpad haptics) is kept
  74. // per-type, not per-mode: the right mapping depends on what controller we present, and the variant modes of
  75. // one controller share it. Steam + Lizard are native puck presentations -- they forward raw input to Steam,
  76. // which does its own remapping -- so they are NOT emulated types (ET_NONE) and ignore this config. ----
  77. #define ET_XBOX 0
  78. #define ET_SWITCH 1
  79. #define ET_DS4 2
  80. #define ET_DS5 3
  81. #define ET_COUNT 4
  82. #define ET_NONE 0xFF
  83. static inline uint8_t etypeForMode(uint8_t m)
  84. {
  85. switch (m) {
  86. case MODE_XBOX:
  87. // OG Xbox Controller S shares the Xbox layout / button config minus guide button
  88. case MODE_XBOX_OG:
  89. return ET_XBOX;
  90. case MODE_SW_HORI:
  91. case MODE_SW_PRO:
  92. return ET_SWITCH;
  93. case MODE_HIDGYRO:
  94. case MODE_DS4_GAME:
  95. case MODE_PS3: // DS3 shares the PlayStation face layout / button config
  96. return ET_DS4;
  97. case MODE_PS5:
  98. case MODE_PS5_GAME:
  99. return ET_DS5;
  100. default:
  101. // Steam / Lizard forward raw input for the host to remap. DirectInput and SInput expose every
  102. // physical button as its own bindable button (paddles included), so they have nothing to remap
  103. // either -- binding happens in the sim / in SDL.
  104. return ET_NONE;
  105. }
  106. }
  107. extern uint8_t g_usbMode; // loaded from flash at boot
  108. // true for all non-puck presentations (cached !modeIsPuck(g_usbMode))
  109. extern bool g_xbox;
  110. extern uint8_t g_chordBtn[3]; // back4+B/X/Y -> these modes (A always STEAM)
  111. // back4+D-pad -> these modes, indexed CHD_LEFT/UP/RIGHT/DOWN. Same deal as g_chordBtn: configurable from the
  112. // panel, defaults reach the console modes that have no config interface (so a controller-only route in exists).
  113. #define CHD_LEFT 0
  114. #define CHD_UP 1
  115. #define CHD_RIGHT 2
  116. #define CHD_DOWN 3
  117. extern uint8_t g_chordDpad[4];
  118. // Mode persistence policy: by DEFAULT every fresh power-on/reconnect lands in STEAM mode (0). An explicit
  119. // mode switch still works for the session via a ONE-SHOT bootMode (honored once, then cleared). g_persistMode
  120. // instead remembers the last selected mode across reboots.
  121. // false (default) = always boot Steam; true = boot into last mode
  122. extern bool g_persistMode;
  123. // one-shot: boot into this mode once then clear (!persistMode + explicit switch)
  124. extern uint8_t g_bootMode;
  125. extern bool g_isMachineInternal;
  126. // One-shot debug CDC. Puck mode normally DROPS the CDC serial console to free the USB endpoint its wake-mouse
  127. // interface needs (to wake a sleeping Windows host). Arming this keeps CDC for the NEXT boot only -- dropping
  128. // the wake mouse that boot -- to attach the serial debugger; the boot after reverts automatically.
  129. // decision for THIS boot: true => keep CDC, skip the wake interface
  130. extern bool g_debugCdcThisBoot;
  131. void armDebugCdcNextBoot(); // persist the one-shot (caller reboots)
  132. // persisted, runtime-tunable config:
  133. extern int g_mDiv, g_mFric; // xbox/lizard mouse sensitivity divisor / friction%
  134. // Per-emulated-type button config. One entry per ET_* type. back[] = paddle L4,R4,L5,R5 -> button codes
  135. // (0..15 standard, 16=PS Touch Click, 17=PS5 Mute, 18=Switch Capture/Screenshot). qamMap = QAM (3 dots)
  136. // physical button -> same code space (0 = default/unmapped). abSwap = swap A/B and X/Y (Nintendo layout).
  137. // padHaptics = 1 keeps the controller's autonomous trackpad haptics, 0 disables them for this type.
  138. // rumble = 1 enables host rumble relay (default), 0 silences it for this type.
  139. // ledBright = LED brightness sent to the controller on connect: 0 = no override (controller default),
  140. // 1-100 = brightness %. Steam sets brightness each session; emulated modes never do, so the controller
  141. // comes up at full brightness. Setting a value here preserves the preferred brightness across mode switches.
  142. struct TypeCfg {
  143. uint8_t back[4];
  144. uint8_t qamMap;
  145. uint8_t abSwap;
  146. uint8_t padHaptics;
  147. uint8_t ledBright;
  148. uint8_t rumble;
  149. };
  150. extern TypeCfg g_type[ET_COUNT];
  151. // Trackpad -> analog stick mapping, per emulated type: {left pad, right pad}, values PS_OFF/PS_LEFT/PS_RIGHT.
  152. // While the mapped pad is touched its coordinates drive that stick; releasing it re-centers the stick (the
  153. // physical stick still drives it whenever the pad is untouched). Kept OUTSIDE TypeCfg so the on-flash Cfg
  154. // layout only grows in its tail -- an existing cfg.bin still loads and keeps every other setting.
  155. #define PS_OFF 0
  156. #define PS_LEFT 1
  157. #define PS_RIGHT 2
  158. #define PS_MAX 2
  159. extern uint8_t g_padStickCfg[ET_COUNT][2];
  160. extern uint8_t
  161. g_etype; // etypeForMode(g_usbMode), resolved at boot (ET_NONE for puck modes)
  162. // Live, resolved-active-type mirrors of g_type[g_etype], read by the hot-path mode builders. Refreshed by
  163. // applyActiveType() at boot and after any edit to the active type.
  164. extern uint8_t g_abSwap; // 1 = swap A/B and X/Y (Nintendo face-button layout)
  165. extern uint8_t g_back[4];
  166. extern uint8_t g_qamMap;
  167. extern uint8_t
  168. g_padHaptics; // 1 = trackpad haptics on (default), 0 = disabled for the active type
  169. // 1 = host rumble relay on (default), 0 = silenced for the active emulated type
  170. extern uint8_t g_rumble;
  171. // LED brightness for the active emulated type (0 = no override, 1-100 = brightness %)
  172. extern uint8_t g_ledBright;
  173. // Live mirror of g_padStickCfg[g_etype]: {left pad, right pad} -> stick (PS_*).
  174. extern uint8_t g_padStick[2];
  175. // Copy g_type[g_etype] into the live mirrors above (safe defaults when g_etype == ET_NONE).
  176. void applyActiveType();
  177. // Switch Pro motion setting. Persisted in its OWN flash file (mode_switch_pro.cpp), NOT in Cfg -- so changing
  178. // it never resets the rest of the config. Set from the WebUSB panel.
  179. // Switch Pro gyro mapping: 0 = corrected (default, PR #189 sensitivity trim), 1 = legacy (raw, pre-#189)
  180. extern uint8_t g_swGyroLegacy;
  181. // persist g_swGyroLegacy to its flash file
  182. void swProSaveCfg();
  183. // RF poll cadence: 250 Hz (4000 us) -- matches the real Valve puck's rate. The controller does NOT stream
  184. // on its own; the puck POLLS (short frame) and the controller auto-ACKs one input frame per poll (RE air
  185. // capture, nrf-sniffer HANDOFF.md), and it stuffs fresh IMU into every report, so the DELIVERED report
  186. // rate simply equals the poll rate. The real puck polls ~230-250 Hz, so we match it here. (The genuine
  187. // smoothness bug was never the rate -- it was onReport45 dropping ~1/3 of captured reports via a bogus
  188. // hid.ready() gate; see puck_hid.cpp. Poll rate is live-tunable via console "PR<hz>" for sweeps.)
  189. #define POLL_US_DEFAULT 4000u
  190. // host-side HID stream cadence for translated modes (~250 Hz)
  191. #define USB_STREAM_MS 4u
  192. // How long a USB suspend must PERSIST before we power the controllers off. A brief selective-suspend
  193. // (host idle power-management) resumes in <1s and must not trigger a self-inflicted power-off -> the
  194. // resulting disconnect/reconnect churn looked like random controller drops. Real host sleep persists.
  195. #define SUSPEND_OFF_MS 4000u
  196. // RF poll cadence (us). Defaults to POLL_US_DEFAULT; live-adjustable via the console "PR<hz>" command
  197. // (session-only -- NOT persisted; loadCfg always forces the default on boot) so the sweet spot can be
  198. // swept on hardware while watching the delivered report rate.
  199. extern uint32_t g_pollUs;
  200. // loop-timing diagnostics (defined in OpenPuck.ino) -- surfaced in the WebUSB status blob to find what caps
  201. // the poll rate: avg loop period, slowest section index, and that section's avg us/iteration.
  202. extern uint16_t g_loopPeriodUs;
  203. extern uint8_t g_loopWorst;
  204. extern uint16_t g_loopWorstUs;
  205. #define CFG_EXT_STORAGE_BYTES 10u
  206. uint8_t cfgExtRead(uint8_t index);
  207. void cfgExtWrite(uint8_t index, uint8_t value);
  208. void loadCfg();
  209. void saveCfg();
  210. // FULL factory wipe: reformat the internal LittleFS, erasing cfg.bin (modes/tunables/chords) AND bonds.bin
  211. // (paired controller). Irreversible; the caller reboots so the next boot comes up on clean defaults and the
  212. // controller must be re-paired. Gated behind explicit confirmation at every call site.
  213. void factoryErase();
  214. // One-time factory reset for the -DOPK_FACTORY_RESET recovery build: wipe ONCE on the first boot after flashing
  215. // (tracked by a git-hash tag file so it doesn't wipe on every boot), then persist normally. buildTag = OPK_GIT_HASH.
  216. void factoryResetOnce(const char *buildTag);
  217. // Mode switch (chord / WebUI): persist mode if the toggle is on, else arm a one-shot so this reboot lands in
  218. // the new mode but the next cold boot returns to Steam. Either way saveCfg + caller reboots.
  219. void saveMode(uint8_t m);