config.h 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. // ---- USB presentation modes (g_usbMode). RF poll/relay is identical across all; only USB enumeration +
  22. // report mapping differ. ----
  23. #define MODE_STEAM 0 // Valve puck; auto-lizard when Steam closed
  24. #define MODE_XBOX 1 // XInput + right-pad mouse
  25. #define MODE_SW_HORI 2 // HORIPAD (Switch console whitelist)
  26. #define MODE_LIZARD \
  27. 3 // Puck HID; always keyboard+mouse (ignores Steam heartbeat)
  28. #define MODE_SW_PRO 4 // Nintendo Switch Pro Controller (057E:2009) + gyro
  29. // Sony DualSense (054C:0CE6) + gyro + split trackpad (+ wake mouse + WebUSB panel)
  30. #define MODE_PS5 5
  31. // DS4-layout generic HID gamepad + gyro (+ wake mouse + WebUSB panel)
  32. #define MODE_HIDGYRO 6
  33. // DualSense, CLEAN single-HID (no wake/WebUSB) so PC games classify it as PlayStation (Fortnite)
  34. #define MODE_PS5_GAME 7
  35. // DS4, CLEAN single-HID (no wake/WebUSB) for game PlayStation classification
  36. #define MODE_DS4_GAME 8
  37. #define MODE_MAX 8
  38. // The two "game" personalities drop the wake-mouse + WebUSB interfaces so the device is a genuine single-HID PS
  39. // controller (some PC games -- e.g. Fortnite/UE GameInput -- refuse PS classification when extra interfaces are
  40. // present). Cost: no config panel / host-wake while in these modes; chord back to Steam (back4 + A) for the panel.
  41. static inline bool modeIsCleanPS(uint8_t m)
  42. {
  43. return m == MODE_PS5_GAME || m == MODE_DS4_GAME;
  44. }
  45. static inline bool modeIsPuck(uint8_t m)
  46. {
  47. return m == MODE_STEAM || m == MODE_LIZARD;
  48. }
  49. static inline bool modeValid(uint8_t m)
  50. {
  51. return m <= MODE_MAX;
  52. }
  53. // ---- Emulated controller TYPES. Button-remap config (back paddles / QAM / A-B swap / trackpad haptics) is kept
  54. // per-type, not per-mode: the right mapping depends on what controller we present, and the variant modes of
  55. // one controller share it. Steam + Lizard are native puck presentations -- they forward raw input to Steam,
  56. // which does its own remapping -- so they are NOT emulated types (ET_NONE) and ignore this config. ----
  57. #define ET_XBOX 0
  58. #define ET_SWITCH 1
  59. #define ET_DS4 2
  60. #define ET_DS5 3
  61. #define ET_COUNT 4
  62. #define ET_NONE 0xFF
  63. static inline uint8_t etypeForMode(uint8_t m)
  64. {
  65. switch (m) {
  66. case MODE_XBOX:
  67. return ET_XBOX;
  68. case MODE_SW_HORI:
  69. case MODE_SW_PRO:
  70. return ET_SWITCH;
  71. case MODE_HIDGYRO:
  72. case MODE_DS4_GAME:
  73. return ET_DS4;
  74. case MODE_PS5:
  75. case MODE_PS5_GAME:
  76. return ET_DS5;
  77. default:
  78. return ET_NONE; // Steam / Lizard
  79. }
  80. }
  81. extern uint8_t g_usbMode; // loaded from flash at boot
  82. // true for all non-puck presentations (cached !modeIsPuck(g_usbMode))
  83. extern bool g_xbox;
  84. extern uint8_t g_chordBtn[3]; // back4+B/X/Y -> these modes (A always STEAM)
  85. // Mode persistence policy: by DEFAULT every fresh power-on/reconnect lands in STEAM mode (0). An explicit
  86. // mode switch still works for the session via a ONE-SHOT bootMode (honored once, then cleared). g_persistMode
  87. // instead remembers the last selected mode across reboots.
  88. // false (default) = always boot Steam; true = boot into last mode
  89. extern bool g_persistMode;
  90. // one-shot: boot into this mode once then clear (!persistMode + explicit switch)
  91. extern uint8_t g_bootMode;
  92. // One-shot debug CDC. Puck mode normally DROPS the CDC serial console to free the USB endpoint its wake-mouse
  93. // interface needs (to wake a sleeping Windows host). Arming this keeps CDC for the NEXT boot only -- dropping
  94. // the wake mouse that boot -- to attach the serial debugger; the boot after reverts automatically.
  95. // decision for THIS boot: true => keep CDC, skip the wake interface
  96. extern bool g_debugCdcThisBoot;
  97. void armDebugCdcNextBoot(); // persist the one-shot (caller reboots)
  98. // persisted, runtime-tunable config:
  99. extern int g_mDiv, g_mFric; // xbox/lizard mouse sensitivity divisor / friction%
  100. // Per-emulated-type button config. One entry per ET_* type. back[] = paddle L4,R4,L5,R5 -> button codes
  101. // (0..15 standard, 16=PS Touch Click, 17=PS5 Mute, 18=Switch Capture/Screenshot). qamMap = QAM (3 dots)
  102. // physical button -> same code space (0 = default/unmapped). abSwap = swap A/B and X/Y (Nintendo layout).
  103. // padHaptics = 1 keeps the controller's autonomous trackpad haptics, 0 disables them for this type.
  104. // ledBright = LED brightness sent to the controller on connect: 0 = no override (controller default),
  105. // 1-100 = brightness %. Steam sets brightness each session; emulated modes never do, so the controller
  106. // comes up at full brightness. Setting a value here preserves the preferred brightness across mode switches.
  107. struct TypeCfg {
  108. uint8_t back[4];
  109. uint8_t qamMap;
  110. uint8_t abSwap;
  111. uint8_t padHaptics;
  112. uint8_t ledBright;
  113. };
  114. extern TypeCfg g_type[ET_COUNT];
  115. extern uint8_t
  116. g_etype; // etypeForMode(g_usbMode), resolved at boot (ET_NONE for puck modes)
  117. // Live, resolved-active-type mirrors of g_type[g_etype], read by the hot-path mode builders. Refreshed by
  118. // applyActiveType() at boot and after any edit to the active type.
  119. extern uint8_t g_abSwap; // 1 = swap A/B and X/Y (Nintendo face-button layout)
  120. extern uint8_t g_back[4];
  121. extern uint8_t g_qamMap;
  122. extern uint8_t
  123. g_padHaptics; // 1 = trackpad haptics on (default), 0 = disabled for the active type
  124. // LED brightness for the active emulated type (0 = no override, 1-100 = brightness %)
  125. extern uint8_t g_ledBright;
  126. // Copy g_type[g_etype] into the live mirrors above (safe defaults when g_etype == ET_NONE).
  127. void applyActiveType();
  128. // rumble strength, percent of decoded amplitude (100 = 1x, 200 = 2x default), all modes
  129. extern uint8_t g_rumbleScale;
  130. // Switch Pro motion settings. Persisted in their OWN flash file (mode_switch_pro.cpp), NOT in Cfg -- so changing
  131. // them never resets the rest of the config. Set from the WebUSB panel.
  132. // Switch Pro report cadence: 0 = 66Hz (15ms, compat), 1 = 120Hz (8ms, DEFAULT), 2 = full (~250Hz)
  133. extern uint8_t g_swProRate;
  134. // Switch Pro gyro sensitivity x10 (10 = 1.0x default; 5/15/20/25/30 = 0.5..3.0x)
  135. extern uint8_t g_swGyroScale10;
  136. // persist g_swProRate + g_swGyroScale10 to their flash file
  137. void swProSaveCfg();
  138. // 250 Hz -- matches SC2 input report rate (1000000/250 = 4000 us)
  139. #define POLL_US_DEFAULT 4000u
  140. // host-side HID stream cadence for translated modes (~250 Hz)
  141. #define USB_STREAM_MS 4u
  142. // How long a USB suspend must PERSIST before we power the controllers off. A brief selective-suspend
  143. // (host idle power-management) resumes in <1s and must not trigger a self-inflicted power-off -> the
  144. // resulting disconnect/reconnect churn looked like random controller drops. Real host sleep persists.
  145. #define SUSPEND_OFF_MS 4000u
  146. // RF poll cadence (us). FIXED -- not configurable (see loadCfg).
  147. extern const uint32_t g_pollUs;
  148. // loop-timing diagnostics (defined in OpenPuck.ino) -- surfaced in the WebUSB status blob to find what caps
  149. // the poll rate: avg loop period, slowest section index, and that section's avg us/iteration.
  150. extern uint16_t g_loopPeriodUs;
  151. extern uint8_t g_loopWorst;
  152. extern uint16_t g_loopWorstUs;
  153. void loadCfg();
  154. void saveCfg();
  155. // FULL factory wipe: reformat the internal LittleFS, erasing cfg.bin (modes/tunables/chords) AND bonds.bin
  156. // (paired controller). Irreversible; the caller reboots so the next boot comes up on clean defaults and the
  157. // controller must be re-paired. Gated behind explicit confirmation at every call site.
  158. void factoryErase();
  159. // One-time factory reset for the -DOPK_FACTORY_RESET recovery build: wipe ONCE on the first boot after flashing
  160. // (tracked by a git-hash tag file so it doesn't wipe on every boot), then persist normally. buildTag = OPK_GIT_HASH.
  161. void factoryResetOnce(const char *buildTag);
  162. // Mode switch (chord / WebUI): persist mode if the toggle is on, else arm a one-shot so this reboot lands in
  163. // the new mode but the next cold boot returns to Steam. Either way saveCfg + caller reboots.
  164. void saveMode(uint8_t m);