config.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "config.h"
  2. #include <Adafruit_LittleFS.h>
  3. #include <InternalFileSystem.h>
  4. using namespace Adafruit_LittleFS_Namespace;
  5. uint8_t g_usbMode = 0;
  6. bool g_xbox = false;
  7. uint8_t g_chordBtn[3] = { MODE_LIZARD, MODE_XBOX, MODE_SW_HORI }; // back4+B/X/Y -> these modes (A always STEAM)
  8. bool g_persistMode = false;
  9. uint8_t g_bootMode = 0xFF;
  10. int g_mDiv = 64, g_mFric = 94;
  11. uint8_t g_abSwap = 0;
  12. uint8_t g_back[4] = {5,6,7,8}; // L4->LB R4->RB L5->L3 R5->R3 (see codeToXB)
  13. // poll rate is fixed. Faster than the controller can refresh wastes airtime; slower adds latency. Any rate
  14. // persisted by an older build is ignored and overwritten with the default on boot (see loadCfg).
  15. const uint32_t g_pollUs = POLL_US_DEFAULT;
  16. #define CFG_FILE "/cfg.bin"
  17. #define CFG_MAGIC 0xC7 // bumped (chordBtn[3]): old cfg ignored -> clean defaults on first boot
  18. struct Cfg { uint8_t magic, mode, mDiv, mFric, rsvd0, abSwap, back[4], pollU100, persistMode, bootMode, chordBtn[3]; }; // rsvd0 = ex-padSmooth (kept for layout)
  19. void saveCfg(){
  20. Cfg c={CFG_MAGIC,g_usbMode,(uint8_t)g_mDiv,(uint8_t)g_mFric,0,g_abSwap,
  21. {g_back[0],g_back[1],g_back[2],g_back[3]},(uint8_t)(g_pollUs/100),(uint8_t)(g_persistMode?1:0),g_bootMode,
  22. {g_chordBtn[0],g_chordBtn[1],g_chordBtn[2]}};
  23. InternalFS.remove(CFG_FILE); File f(InternalFS);
  24. if(f.open(CFG_FILE,FILE_O_WRITE)){ f.write((uint8_t*)&c,sizeof c); f.close(); }
  25. }
  26. void loadCfg(){
  27. Cfg c; File f(InternalFS); bool consume=false;
  28. if(f.open(CFG_FILE,FILE_O_READ)){
  29. if(f.read((uint8_t*)&c,sizeof c)==(int)sizeof c && c.magic==CFG_MAGIC){
  30. g_mDiv=c.mDiv?c.mDiv:64; g_mFric=c.mFric;
  31. g_abSwap=c.abSwap; for(int i=0;i<4;i++) g_back[i]=c.back[i];
  32. g_persistMode = c.persistMode?true:false;
  33. // poll rate is fixed (g_pollUs const = POLL_US_DEFAULT). A stale rate from an older build is never
  34. // applied; rewrite cfg so the persisted byte also matches the new default.
  35. if(c.pollU100 != (uint8_t)(POLL_US_DEFAULT/100)) consume=true;
  36. // boot-mode policy: a one-shot bootMode (set by an explicit switch when !persist) wins once and is then
  37. // cleared; otherwise persist->last mode, else->Steam. (poll rate stays POLL_US_DEFAULT -- never restored from cfg.)
  38. if(c.bootMode!=0xFF){ g_usbMode=modeValid(c.bootMode)?c.bootMode:0; consume=true; }
  39. else g_usbMode = g_persistMode ? (modeValid(c.mode)?c.mode:0) : 0;
  40. static const uint8_t CHORD_DEF[3]={MODE_LIZARD,MODE_XBOX,MODE_SW_HORI};
  41. for(int i=0;i<3;i++) g_chordBtn[i]=modeValid(c.chordBtn[i])?c.chordBtn[i]:CHORD_DEF[i];
  42. }
  43. f.close();
  44. }
  45. if(consume){ g_bootMode=0xFF; saveCfg(); } // clear the one-shot so the NEXT cold boot reverts to the default/persist policy
  46. }
  47. void saveMode(uint8_t m){
  48. if(g_persistMode){ g_usbMode=m; g_bootMode=0xFF; }
  49. else { g_bootMode=m; }
  50. saveCfg();
  51. }