config.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. #include "config.h"
  2. #include "rf_link.h" // g_rxWin (poll RX window persisted here)
  3. #include "haptics.h" // g_hapticBlockOn, g_hapticBlockMs
  4. #include <Adafruit_LittleFS.h>
  5. #include <InternalFileSystem.h>
  6. #include <string.h>
  7. using namespace Adafruit_LittleFS_Namespace;
  8. uint8_t g_usbMode = 0;
  9. bool g_xbox = false;
  10. uint8_t g_chordBtn[3] = {
  11. MODE_LIZARD, MODE_XBOX, MODE_SW_PRO
  12. }; // back4+B/X/Y -> these modes (A always STEAM); Y defaults to Switch Pro
  13. bool g_persistMode = false;
  14. uint8_t g_bootMode = 0xFF;
  15. bool g_debugCdcThisBoot = false;
  16. // persisted one-shot arm, stored in Cfg.rsvd0 (1 = keep CDC for the next boot)
  17. static uint8_t g_debugCdc = 0;
  18. int g_mDiv = 64, g_mFric = 94;
  19. // Per-type button config. back default {5,6,7,8} = L4->LB R4->RB L5->L3 R5->R3 (0..11 buttons, 12..15 D-pad,
  20. // 16/17 PS touch/mute, 18 Switch Capture). Switch differs: QAM defaults to Capture(18), A/B swap on, and
  21. // trackpad haptics off. qamMap 0 = unmapped (hardcoded per-mode behavior). ledBright 0 = no override.
  22. TypeCfg g_type[ET_COUNT] = {
  23. /* ET_XBOX */ { { 5, 6, 7, 8 }, 0, 0, 1, 0 },
  24. /* ET_SWITCH */ { { 5, 6, 7, 8 }, 18, 1, 0, 0 },
  25. /* ET_DS4 */ { { 5, 6, 7, 8 }, 0, 0, 1, 0 },
  26. /* ET_DS5 */ { { 5, 6, 7, 8 }, 0, 0, 1, 0 },
  27. };
  28. uint8_t g_etype = ET_NONE;
  29. // Live mirrors of the active type (puck modes use the harmless defaults below).
  30. uint8_t g_abSwap = 0;
  31. uint8_t g_back[4] = { 5, 6, 7, 8 };
  32. uint8_t g_qamMap = 0;
  33. uint8_t g_padHaptics = 1;
  34. uint8_t g_ledBright = 0;
  35. void applyActiveType()
  36. {
  37. g_etype = etypeForMode(g_usbMode);
  38. if (g_etype >=
  39. ET_COUNT) { // puck mode (Steam/Lizard): no remap, haptics on
  40. g_back[0] = 5;
  41. g_back[1] = 6;
  42. g_back[2] = 7;
  43. g_back[3] = 8;
  44. g_qamMap = 0;
  45. g_abSwap = 0;
  46. g_padHaptics = 1;
  47. g_ledBright = 0;
  48. return;
  49. }
  50. const TypeCfg &t = g_type[g_etype];
  51. for (int i = 0; i < 4; i++)
  52. g_back[i] = t.back[i];
  53. g_qamMap = t.qamMap;
  54. g_abSwap = t.abSwap;
  55. g_padHaptics = t.padHaptics;
  56. g_ledBright = t.ledBright;
  57. }
  58. // rumble strength % (200 = double); adjustable from the WebUSB panel
  59. uint8_t g_rumbleScale = 200;
  60. // poll rate is fixed. Faster than the controller can refresh wastes airtime; slower adds latency. Any rate
  61. // persisted by an older build is ignored and overwritten with the default on boot (see loadCfg).
  62. const uint32_t g_pollUs = POLL_US_DEFAULT;
  63. #define CFG_FILE "/cfg.bin"
  64. // Struct layout/semantics changed (haptic-block bytes repurposed); bump so old flash format is discarded ->
  65. // clean defaults once.
  66. #define CFG_MAGIC 0xCE
  67. struct Cfg {
  68. uint8_t magic, mode, mDiv, mFric, rsvd0, pollU100, persistMode,
  69. bootMode, chordBtn[3], rumbleScale;
  70. // rxWin10: legacy RF tunable slot (window now fixed; ignored). lizKeep: the id9=0 hold enable (see
  71. // haptics.h LIZKEEP_MS). landAll87: the verbatim-0x87-relay experiment toggle (haptics.h g_landAll87).
  72. uint8_t rxWin10, lizKeep, landAll87;
  73. TypeCfg type[ET_COUNT]; // per-emulated-type back/qam/abSwap/padHaptics
  74. }; // rsvd0 = ex-padSmooth, now the one-shot debug-CDC arm
  75. void saveCfg()
  76. {
  77. Cfg c = { CFG_MAGIC,
  78. g_usbMode,
  79. (uint8_t)g_mDiv,
  80. (uint8_t)g_mFric,
  81. g_debugCdc,
  82. (uint8_t)(g_pollUs / 100),
  83. (uint8_t)(g_persistMode ? 1 : 0),
  84. g_bootMode,
  85. { g_chordBtn[0], g_chordBtn[1], g_chordBtn[2] },
  86. g_rumbleScale,
  87. (uint8_t)(g_rxWin / 10),
  88. g_lizKeep,
  89. g_landAll87,
  90. {} };
  91. for (int i = 0; i < ET_COUNT; i++)
  92. c.type[i] = g_type[i];
  93. InternalFS.remove(CFG_FILE);
  94. File f(InternalFS);
  95. if (f.open(CFG_FILE, FILE_O_WRITE)) {
  96. f.write((uint8_t *)&c, sizeof c);
  97. f.close();
  98. }
  99. }
  100. void loadCfg()
  101. {
  102. Cfg c;
  103. File f(InternalFS);
  104. bool consume = false;
  105. if (f.open(CFG_FILE, FILE_O_READ)) {
  106. if (f.read((uint8_t *)&c, sizeof c) == (int)sizeof c &&
  107. c.magic == CFG_MAGIC) {
  108. g_mDiv = c.mDiv ? c.mDiv : 64;
  109. g_mFric = c.mFric;
  110. for (int i = 0; i < ET_COUNT; i++)
  111. g_type[i] = c.type[i];
  112. g_persistMode = c.persistMode ? true : false;
  113. // one-shot debug-CDC (Cfg.rsvd0): honor for THIS boot, then consume so the next boot reverts to normal.
  114. g_debugCdcThisBoot = c.rsvd0 ? true : false;
  115. if (c.rsvd0) {
  116. g_debugCdc = 0;
  117. consume = true;
  118. }
  119. // poll rate is fixed; rewrite cfg so the persisted byte matches the new default.
  120. if (c.pollU100 != (uint8_t)(POLL_US_DEFAULT / 100))
  121. consume = true;
  122. // boot-mode policy: a one-shot bootMode (explicit switch when !persist) wins once then clears;
  123. // otherwise persist->last mode, else->Steam.
  124. if (c.bootMode != 0xFF) {
  125. g_usbMode = modeValid(c.bootMode) ? c.bootMode :
  126. 0;
  127. consume = true;
  128. } else
  129. g_usbMode = g_persistMode ? (modeValid(c.mode) ?
  130. c.mode :
  131. 0) :
  132. 0;
  133. static const uint8_t CHORD_DEF[3] = { MODE_LIZARD,
  134. MODE_XBOX,
  135. MODE_SW_PRO };
  136. for (int i = 0; i < 3; i++)
  137. g_chordBtn[i] = modeValid(c.chordBtn[i]) ?
  138. c.chordBtn[i] :
  139. CHORD_DEF[i];
  140. // 0 is a valid setting (rumble off)
  141. g_rumbleScale = c.rumbleScale;
  142. // lizard-suppression keepalive enable (0/1; anything else = a pre-0xCE cfg leaked
  143. // through -> keep the on default)
  144. if (c.lizKeep <= 1)
  145. g_lizKeep = c.lizKeep;
  146. // verbatim-0x87-relay experiment toggle (0/1; default off)
  147. if (c.landAll87 <= 1)
  148. g_landAll87 = c.landAll87;
  149. // The poll RX window is now FIXED (g_rxWin is const) -- any persisted rxWin10 is ignored.
  150. }
  151. f.close();
  152. }
  153. // resolve the active emulated type's settings into the live mirrors the mode builders read
  154. applyActiveType();
  155. // clear the one-shot so the NEXT cold boot reverts to the default/persist policy
  156. if (consume) {
  157. g_bootMode = 0xFF;
  158. saveCfg();
  159. }
  160. }
  161. void saveMode(uint8_t m)
  162. {
  163. if (g_persistMode) {
  164. g_usbMode = m;
  165. g_bootMode = 0xFF;
  166. } else {
  167. g_bootMode = m;
  168. }
  169. saveCfg();
  170. }
  171. void armDebugCdcNextBoot()
  172. {
  173. g_debugCdc = 1;
  174. saveCfg();
  175. } // next boot keeps CDC; loadCfg() consumes it after
  176. // FULL factory wipe: reformat the internal LittleFS, erasing cfg.bin (modes/tunables/chords) AND bonds.bin
  177. // (paired-controller record). Caller reboots: next boot finds no files and falls back to clean defaults, and
  178. // the controller must be re-paired. Irreversible -- gated behind explicit confirmation at every call site.
  179. void factoryErase()
  180. {
  181. // ensure mounted before we reformat (no-op if already up)
  182. InternalFS.begin();
  183. InternalFS.format();
  184. }
  185. // One-time factory reset for the -DOPK_FACTORY_RESET recovery build: clear a bad config/bond ONCE (first boot
  186. // after flashing) then persist normally. "Already reset" is tracked by a tag file holding the build's git hash,
  187. // written AFTER the wipe (so it survives in the freshly-formatted FS):
  188. // - tag missing or != this build's hash -> wipe, then stamp the tag. Next boot persists.
  189. // - tag == this build's hash -> already reset for this build: skip, boot normally.
  190. // Keying the tag to the git hash means flashing a DIFFERENT build re-triggers the wipe. buildTag is OPK_GIT_HASH.
  191. #define RESET_TAG_FILE "/rsttag"
  192. void factoryResetOnce(const char *buildTag)
  193. {
  194. char tag[24] = { 0 };
  195. {
  196. File f(InternalFS);
  197. if (f.open(RESET_TAG_FILE, FILE_O_READ)) {
  198. int n = f.read((uint8_t *)tag, sizeof tag - 1);
  199. if (n > 0)
  200. tag[n] = 0;
  201. f.close();
  202. }
  203. }
  204. if (strncmp(tag, buildTag, sizeof tag - 1) == 0)
  205. return; // this build already did its one-time reset -> persist
  206. factoryErase(); // wipe cfg.bin + bonds.bin + the old tag
  207. InternalFS.begin(); // remount the fresh FS
  208. File g(InternalFS); // stamp the tag so subsequent boots skip the wipe
  209. if (g.open(RESET_TAG_FILE, FILE_O_WRITE)) {
  210. g.write((const uint8_t *)buildTag, strlen(buildTag));
  211. g.close();
  212. }
  213. }