config.cpp 6.6 KB

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