|
|
@@ -33,6 +33,20 @@
|
|
|
.note{color:var(--mut);font-size:12px;margin-top:10px}
|
|
|
#log{font:12px ui-monospace,Menlo,monospace;color:var(--mut);white-space:pre-wrap;margin-top:8px;max-height:120px;overflow:auto}
|
|
|
.hide{display:none}
|
|
|
+
|
|
|
+ /* ---- controller slot tabs ---- */
|
|
|
+ .slot-tabs{display:flex;gap:6px;margin:0 0 14px;flex-wrap:wrap}
|
|
|
+ .slot-tab{display:flex;align-items:center;gap:7px;padding:5px 14px;border-radius:8px;font-size:12px;font-weight:600;background:#0e1017;border:1px solid #232a3a;color:var(--mut);cursor:pointer;transition:border-color .15s,color .15s,background .15s}
|
|
|
+ .slot-tab:hover{border-color:var(--acc2);color:var(--fg)}
|
|
|
+ .slot-tab.active{background:#111c2e;border-color:var(--acc2);color:var(--fg)}
|
|
|
+ .slot-tab .dot{width:7px;height:7px;border-radius:50%;background:var(--bad);flex-shrink:0;transition:background .3s}
|
|
|
+ .slot-tab .dot.up{background:var(--acc);animation:pulse-link 2.2s ease-in-out infinite}
|
|
|
+ @keyframes pulse-link{
|
|
|
+ 0%,100%{box-shadow:0 0 0 0 rgba(91,214,160,.5)}
|
|
|
+ 55%{box-shadow:0 0 0 5px rgba(91,214,160,0)}
|
|
|
+ }
|
|
|
+ @media(prefers-reduced-motion:reduce){.slot-tab .dot.up{animation:none}}
|
|
|
+ .per-slot-grid{margin-bottom:12px}
|
|
|
</style>
|
|
|
</head>
|
|
|
<body>
|
|
|
@@ -59,9 +73,6 @@
|
|
|
<button data-mode="4" class="modebtn">Switch Pro + gyro</button>
|
|
|
<button data-mode="5" class="modebtn">PS5 DualSense</button>
|
|
|
<button data-mode="6" class="modebtn">HID gyro (DS4)</button>
|
|
|
- <!-- modes 7/8 (PS5/DS4 "game/clean") hidden from the panel for now; still reachable via serial console -->
|
|
|
- <!-- <button data-mode="7" class="modebtn">PS5 (game/clean)</button> -->
|
|
|
- <!-- <button data-mode="8" class="modebtn">DS4 (game/clean)</button> -->
|
|
|
</div>
|
|
|
<p class="note">Switching reboots the copycat and re-enumerates USB (~2 s). WebUSB works in every mode, including Steam/Lizard with Steam running (Chrome claims the vendor interface; Steam keeps the HID slots). Switch HORIPAD + WebUSB is PC-only.</p>
|
|
|
<div class="row"><label>Persist last mode</label>
|
|
|
@@ -80,11 +91,19 @@
|
|
|
|
|
|
<div class="card">
|
|
|
<h2>Link status</h2>
|
|
|
- <div class="statgrid">
|
|
|
+
|
|
|
+ <!-- Per-controller tabs: visible only when 2+ controllers are bonded -->
|
|
|
+ <div class="slot-tabs" id="slotTabs" style="display:none"></div>
|
|
|
+
|
|
|
+ <!-- Per-slot: RF link / Battery / Signal -->
|
|
|
+ <div class="statgrid per-slot-grid">
|
|
|
<div class="stat"><div class="k">RF link</div><div class="v" id="stLink">—</div></div>
|
|
|
<div class="stat"><div class="k">Battery</div><div class="v" id="stBatt">—</div></div>
|
|
|
<div class="stat"><div class="k">Signal</div><div class="v" id="stRssi">—</div></div>
|
|
|
- <div class="stat"><div class="k">Slot</div><div class="v" id="stSlot">—</div></div>
|
|
|
+ </div>
|
|
|
+
|
|
|
+ <!-- Global stats -->
|
|
|
+ <div class="statgrid">
|
|
|
<div class="stat"><div class="k">Delivered</div><div class="v" id="stRate">—</div></div>
|
|
|
<div class="stat"><div class="k">New reports</div><div class="v" id="stNew">—</div></div>
|
|
|
<div class="stat logonly"><div class="k">Polls/s</div><div class="v" id="stPolls">—</div></div>
|
|
|
@@ -92,7 +111,10 @@
|
|
|
<div class="stat logonly"><div class="k">Slowest stage</div><div class="v" id="stWorst">—</div></div>
|
|
|
<div class="stat logonly"><div class="k">Poll period act/want</div><div class="v" id="stPollPer">—</div></div>
|
|
|
<div class="stat"><div class="k">Build (git)</div><div class="v" id="stBuild">—</div></div>
|
|
|
+ <div class="stat"><div class="k">Last reset</div><div class="v" id="stReset">—</div></div>
|
|
|
</div>
|
|
|
+ <p class="note" style="margin-top:6px"><strong>Reset history</strong> (newest first, persists across power-off): <span id="resetHist" style="font-family:ui-monospace,Menlo,monospace">—</span><br>
|
|
|
+ <em>watchdog</em>/<em>LOCKUP</em> = a fault (loop hang / crash); <em>soft reboot</em> = an intentional reset (mode change); <em>power-on</em> = normal plug-in.</p>
|
|
|
<p class="note" style="margin-top:8px"><strong>IMU (raw):</strong> <span id="stImu" style="font-family:ui-monospace,Menlo,monospace">—</span><br>
|
|
|
Raw SC2 accel (before scaling). This controller is a ±2 g sensor (~16384 = 1 g); the Switch report divides it by 4 to present the genuine ±8 g (4096 = 1 g) scale so the console's gravity-correction engages and the gyro stops drifting.</p>
|
|
|
<p class="note logonly">Polls/s ≈ 250 expected; it's capped by Loop period (≈4000 µs needed). "Slowest stage" is the loop section eating the most time per iteration.</p>
|
|
|
@@ -102,6 +124,14 @@
|
|
|
<div class="row" style="margin:10px 0 0"><button id="ctlrOff">Turn off controller</button>
|
|
|
<span class="note" style="flex:1">Fires the controller power-off (0x9F "off!", x3) — the same path Steam's "turn off controller" and host-suspend use. Watch the link status to see it disconnect.</span>
|
|
|
</div>
|
|
|
+ <div class="row" style="margin:10px 0 0;border-top:1px solid #272d3d;padding-top:14px">
|
|
|
+ <button id="dfuSerial">Reboot to Serial DFU</button>
|
|
|
+ <span class="note" style="flex:1">Reboots the puck into serial DFU mode (adafruit-nrfutil). The device drops off USB — flash with <code>adafruit-nrfutil dfu serial …</code> then replug.</span>
|
|
|
+ </div>
|
|
|
+ <div class="row" style="margin:10px 0 0">
|
|
|
+ <button id="dfuUf2">Reboot to UF2 Bootloader</button>
|
|
|
+ <span class="note" style="flex:1">Reboots the puck into the UF2 mass-storage bootloader. It mounts as a USB drive — drag the <code>.uf2</code> file onto it to flash, then it reboots automatically.</span>
|
|
|
+ </div>
|
|
|
<div class="row" style="margin:18px 0 0;border-top:1px solid #4a2030;padding-top:14px">
|
|
|
<button id="factoryErase" style="background:#3a1420;border-color:#7a2233;color:#ff9bb0;font-weight:600">⚠ Factory erase</button>
|
|
|
<span class="note" style="flex:1"><strong style="color:#ff9bb0">Danger:</strong> wipes <em>all</em> persistent storage on the copycat — the paired controller bond <em>and</em> every saved setting (mode, chords, back-paddle map, sensitivity). The copycat reboots to factory defaults and you must <strong>re-pair the controller</strong>. This cannot be undone.</span>
|
|
|
@@ -124,24 +154,15 @@
|
|
|
<div class="card">
|
|
|
<h2>Switch Pro motion</h2>
|
|
|
<div class="row"><label>Report rate</label><select id="swRate"></select></div>
|
|
|
- <p class="note" style="margin:0 0 6px">120 Hz (default) is low-latency and drift-free. 66 Hz is a genuine-cadence compatibility fallback. Full rate (~250 Hz) is lowest-latency but non-genuine — use only if your console tolerates it.</p>
|
|
|
+ <p class="note" style="margin:0 0 6px">Full rate (~250 Hz, default) is lowest-latency. 120 Hz is low-latency and drift-free. 66 Hz is a genuine-cadence compatibility fallback — drop to it only if your console rejects the faster rates.</p>
|
|
|
<div class="row"><label>Gyro scale</label><select id="swGyro"></select></div>
|
|
|
<p class="note">Gyro sensitivity multiplier for Switch Pro mode (1.0× = unchanged).</p>
|
|
|
</div>
|
|
|
|
|
|
-
|
|
|
<div class="card">
|
|
|
<h2>Button mapping</h2>
|
|
|
- <div class="row"><label>A/B + X/Y swap</label>
|
|
|
- <button id="abSwap">off</button>
|
|
|
- <span class="note" style="flex:1">Nintendo layout (affects Xbox & Switch modes)</span>
|
|
|
- </div>
|
|
|
- <div class="row"><label>L4 (back upper-left)</label><select class="back" data-i="0"></select></div>
|
|
|
- <div class="row"><label>R4 (back upper-right)</label><select class="back" data-i="1"></select></div>
|
|
|
- <div class="row"><label>L5 (back lower-left)</label><select class="back" data-i="2"></select></div>
|
|
|
- <div class="row"><label>R5 (back lower-right)</label><select class="back" data-i="3"></select></div>
|
|
|
- <div class="row"><label>QAM (3 dots)</label><select id="qamMap"></select></div>
|
|
|
- <p class="note">Back-paddle → button, applied in Xbox & Switch modes. QAM remap applies to all modes (0 = default per-mode behavior).</p>
|
|
|
+ <p class="note" style="margin-top:0">Separate mappings per emulated controller type — each only offers targets that exist on that controller. The type matching the current mode is highlighted. Steam & Lizard are native puck modes (Steam does its own remapping) and aren't configured here.</p>
|
|
|
+ <div id="typeCfgs"></div>
|
|
|
</div>
|
|
|
|
|
|
<div class="card logonly">
|
|
|
@@ -163,45 +184,135 @@
|
|
|
<script>
|
|
|
const MODE_NAMES = ["Steam (puck)","Xbox 360","Switch (HORIPAD)","Lizard (always)","Switch Pro + gyro","PS5 DualSense","HID gyro (DS4)","PS5 (game/clean)","DS4 (game/clean)"];
|
|
|
const CHORD_FIELD = [17, 18, 19];
|
|
|
-const BTN_CODES = [[0,"— none —"],[1,"A"],[2,"B"],[3,"X"],[4,"Y"],[5,"LB"],[6,"RB"],[7,"L3"],[8,"R3"],[9,"Back"],[10,"Quick Access Menu"],[11,"Guide"],[12,"D-pad Up"],[13,"D-pad Down"],[14,"D-pad Left"],[15,"D-pad Right"],[16,"PS Touch Click"],[17,"PS5 Mute"]];
|
|
|
let dev=null, epIn=0, epOut=0, ifNum=0, polling=false, capturing=false;
|
|
|
const $ = s=>document.querySelector(s);
|
|
|
function log(m){ const l=$("#log"); l.textContent=(new Date().toLocaleTimeString()+" "+m+"\n"+l.textContent).slice(0,2000); }
|
|
|
|
|
|
-// populate back-paddle + chord dropdowns
|
|
|
-for(const sel of document.querySelectorAll("select.back")){
|
|
|
- for(const [c,n] of BTN_CODES){ const o=document.createElement("option"); o.value=c; o.textContent=n; sel.appendChild(o); }
|
|
|
+// Per-emulated-type config (must match firmware ET_* order: Xbox=0, Switch=1, DS4=2, DS5=3). Each type lists
|
|
|
+// only the remap targets that exist on that controller. Field id sent to firmware = 40 + et*8 + k
|
|
|
+// (k: 0..3 back paddles, 4 QAM, 5 A/B-swap, 6 trackpad-haptics). Blob v9 reads them back at p[75 + et*7 + ...].
|
|
|
+const DPAD = {12:"D-pad Up",13:"D-pad Down",14:"D-pad Left",15:"D-pad Right"};
|
|
|
+const TYPE_DEFS = [
|
|
|
+ {key:"XBOX", name:"Xbox", labels:{1:"A",2:"B",3:"X",4:"Y",5:"LB",6:"RB",7:"L3",8:"R3",9:"Back",10:"Start",11:"Guide",19:"LT",20:"RT",...DPAD}},
|
|
|
+ {key:"SWITCH", name:"Switch", labels:{1:"A",2:"B",3:"X",4:"Y",5:"L",6:"R",7:"L-Stick",8:"R-Stick",9:"Minus",10:"Plus",11:"Home",19:"ZL",20:"ZR",...DPAD,18:"Capture / Screenshot"}},
|
|
|
+ {key:"DS4", name:"DS4", labels:{1:"Cross",2:"Circle",3:"Square",4:"Triangle",5:"L1",6:"R1",7:"L3",8:"R3",9:"Create",10:"Options",11:"PS",19:"L2",20:"R2",...DPAD,16:"Touchpad Click"}},
|
|
|
+ {key:"DS5", name:"DS5", labels:{1:"Cross",2:"Circle",3:"Square",4:"Triangle",5:"L1",6:"R1",7:"L3",8:"R3",9:"Create",10:"Options",11:"PS",19:"L2",20:"R2",...DPAD,16:"Touchpad Click",17:"Mute"}},
|
|
|
+];
|
|
|
+// Which emulated type a USB mode belongs to (mirror of firmware etypeForMode); -1 = puck mode (no type).
|
|
|
+function etypeForMode(m){ switch(m){case 1:return 0; case 2:case 4:return 1; case 6:case 8:return 2; case 5:case 7:return 3; default:return -1;} }
|
|
|
+const BACK_LABELS = ["L4 (back upper-left)","R4 (back upper-right)","L5 (back lower-left)","R5 (back lower-right)"];
|
|
|
+const typeEls = []; // per-type {sec, abSwap, pad, back[4], qam}
|
|
|
+function mkSelect(def, includeNone){
|
|
|
+ const sel=document.createElement("select");
|
|
|
+ if(includeNone){ const o=document.createElement("option"); o.value=0; o.textContent="— none —"; sel.appendChild(o); }
|
|
|
+ else { const o=document.createElement("option"); o.value=0; o.textContent="Default (per-mode)"; sel.appendChild(o); }
|
|
|
+ for(const c of Object.keys(def.labels).map(Number).sort((a,b)=>a-b)){
|
|
|
+ const o=document.createElement("option"); o.value=c; o.textContent=def.labels[c]; sel.appendChild(o); }
|
|
|
+ return sel;
|
|
|
}
|
|
|
-{ const sel = document.getElementById("qamMap");
|
|
|
- const d = document.createElement("option"); d.value=0; d.textContent="Default (varies by mode)"; sel.appendChild(d);
|
|
|
- for(const [c,n] of BTN_CODES){ if(c===0) continue; const o=document.createElement("option"); o.value=c; o.textContent=n; sel.appendChild(o); }
|
|
|
+let curTab=0, tabInited=false;
|
|
|
+function setTab(et){
|
|
|
+ curTab=et;
|
|
|
+ typeEls.forEach((rec,i)=>{
|
|
|
+ rec.sec.style.display = (i===et) ? "" : "none";
|
|
|
+ rec.tab.classList.toggle("active", i===et);
|
|
|
+ });
|
|
|
}
|
|
|
+(function buildTypeCfgs(){
|
|
|
+ const host=document.getElementById("typeCfgs");
|
|
|
+ const bar=document.createElement("div"); bar.style.cssText="display:flex;gap:6px;flex-wrap:wrap;margin-bottom:12px";
|
|
|
+ host.appendChild(bar);
|
|
|
+ TYPE_DEFS.forEach((def,et)=>{
|
|
|
+ // tab button
|
|
|
+ const tab=document.createElement("button"); tab.className="slot-tab"; tab.style.cursor="pointer";
|
|
|
+ tab.innerHTML='<span>'+def.name+'</span><span class="active-dot" style="display:none;font-size:11px;color:var(--acc)"> ●</span>';
|
|
|
+ tab.onclick=()=>setTab(et); bar.appendChild(tab);
|
|
|
+ // panel
|
|
|
+ const sec=document.createElement("div"); sec.style.cssText="background:#0e1017;border:1px solid #232a3a;border-radius:8px;padding:12px 14px;display:none";
|
|
|
+ const rec={sec,tab,activeDot:tab.querySelector(".active-dot"),back:[],qam:null,abSwap:null,pad:null};
|
|
|
+ // A/B swap + trackpad haptics toggles
|
|
|
+ const tog=document.createElement("div"); tog.className="row";
|
|
|
+ tog.innerHTML='<label>A/B + X/Y swap</label>';
|
|
|
+ const ab=document.createElement("button"); ab.textContent="off"; tog.appendChild(ab);
|
|
|
+ const padLbl=document.createElement("label"); padLbl.textContent="Trackpad haptics"; padLbl.style.marginLeft="14px"; tog.appendChild(padLbl);
|
|
|
+ const pad=document.createElement("button"); pad.textContent="on"; tog.appendChild(pad);
|
|
|
+ sec.appendChild(tog);
|
|
|
+ rec.abSwap=ab; rec.pad=pad;
|
|
|
+ ab.onclick=()=>{ const on=ab.classList.contains("active"); setField(40+et*8+5, on?0:1); };
|
|
|
+ pad.onclick=()=>{ const on=pad.classList.contains("active"); setField(40+et*8+6, on?0:1); };
|
|
|
+ // back paddles
|
|
|
+ for(let i=0;i<4;i++){
|
|
|
+ const row=document.createElement("div"); row.className="row";
|
|
|
+ const lab=document.createElement("label"); lab.textContent=BACK_LABELS[i]; row.appendChild(lab);
|
|
|
+ const sel=mkSelect(def,true); row.appendChild(sel); sec.appendChild(row);
|
|
|
+ sel.addEventListener("change",()=>setField(40+et*8+i, +sel.value));
|
|
|
+ rec.back.push(sel);
|
|
|
+ }
|
|
|
+ // QAM
|
|
|
+ { const row=document.createElement("div"); row.className="row";
|
|
|
+ const lab=document.createElement("label"); lab.textContent="QAM (3 dots)"; row.appendChild(lab);
|
|
|
+ const sel=mkSelect(def,false); row.appendChild(sel); sec.appendChild(row);
|
|
|
+ sel.addEventListener("change",()=>setField(40+et*8+4, +sel.value));
|
|
|
+ rec.qam=sel; }
|
|
|
+ host.appendChild(sec); typeEls.push(rec);
|
|
|
+ });
|
|
|
+ setTab(0);
|
|
|
+})();
|
|
|
for(const sel of document.querySelectorAll("select.chord")){
|
|
|
- MODE_NAMES.forEach((n,i)=>{ if(i===7||i===8) return; // hide PS5/DS4 "game/clean" modes from the panel for now
|
|
|
+ MODE_NAMES.forEach((n,i)=>{ if(i===7||i===8) return;
|
|
|
const o=document.createElement("option"); o.value=i; o.textContent=n; sel.appendChild(o); });
|
|
|
}
|
|
|
-// Switch Pro report-rate options (0=66Hz, 1=120Hz default, 2=full ~250Hz)
|
|
|
{ const sel=document.getElementById("swRate");
|
|
|
- for(const [lbl,v] of [["120 Hz (default)",1],["66 Hz (compatibility)",0],["Full rate (~250 Hz)",2]]){
|
|
|
+ for(const [lbl,v] of [["Full rate (~250 Hz, default)",2],["120 Hz",1],["66 Hz (compatibility)",0]]){
|
|
|
const o=document.createElement("option"); o.value=v; o.textContent=lbl; sel.appendChild(o); }
|
|
|
}
|
|
|
-// Switch Pro gyro-scale options (value = scale x10, so it fits in one byte)
|
|
|
{ const sel=document.getElementById("swGyro");
|
|
|
for(const [lbl,v] of [["0.5×",5],["1.0×",10],["1.5×",15],["2.0×",20],["2.5×",25],["3.0×",30]]){
|
|
|
const o=document.createElement("option"); o.value=v; o.textContent=lbl; sel.appendChild(o); }
|
|
|
}
|
|
|
|
|
|
+// ---- per-slot tab state ----
|
|
|
+let g_activeSlot = 0;
|
|
|
+let g_slotData = []; // [{up, battery, rssi}] indexed by slot 0..3, only set for bonded slots
|
|
|
+
|
|
|
+function updateSlotDisplay(slotIdx) {
|
|
|
+ const d = g_slotData[slotIdx];
|
|
|
+ if (!d) { $("#stLink").innerHTML="—"; $("#stBatt").textContent="—"; $("#stRssi").textContent="—"; return; }
|
|
|
+ $("#stLink").innerHTML = d.up ? '<span class="pill up">up</span>' : '<span class="pill dn">down</span>';
|
|
|
+ $("#stBatt").textContent = (d.up && d.battery) ? d.battery+"%" : "—";
|
|
|
+ $("#stRssi").textContent = (d.up && d.rssi) ? ("-"+d.rssi+" dBm") : "—";
|
|
|
+}
|
|
|
+
|
|
|
+function renderSlotTabs(bondedCount) {
|
|
|
+ const bar = $("#slotTabs");
|
|
|
+ if (bondedCount <= 1) { bar.style.display="none"; updateSlotDisplay(g_activeSlot); return; }
|
|
|
+ bar.style.display="flex";
|
|
|
+ bar.innerHTML="";
|
|
|
+ for (let s=0; s<4; s++) {
|
|
|
+ const d = g_slotData[s];
|
|
|
+ if (!d) continue;
|
|
|
+ const btn = document.createElement("button");
|
|
|
+ btn.className = "slot-tab" + (s===g_activeSlot ? " active" : "");
|
|
|
+ const dot = document.createElement("span");
|
|
|
+ dot.className = "dot" + (d.up ? " up" : "");
|
|
|
+ btn.appendChild(dot);
|
|
|
+ btn.appendChild(document.createTextNode("Controller "+(s+1)));
|
|
|
+ btn.onclick = () => { g_activeSlot=s; renderSlotTabs(bondedCount); };
|
|
|
+ bar.appendChild(btn);
|
|
|
+ }
|
|
|
+ updateSlotDisplay(g_activeSlot);
|
|
|
+}
|
|
|
+
|
|
|
async function connect(){
|
|
|
try{
|
|
|
dev = await navigator.usb.requestDevice({filters:[{vendorId:0x28DE},{vendorId:0x045E},{vendorId:0x057E},{vendorId:0x0F0D},{vendorId:0x054C}]});
|
|
|
await dev.open();
|
|
|
if(dev.configuration===null) await dev.selectConfiguration(1);
|
|
|
- // WebUSB = vendor class 0xFF with bulk IN + bulk OUT (skip XInput 0xFF/5D/01 which uses interrupt only)
|
|
|
let found=null;
|
|
|
for(const itf of dev.configuration.interfaces){
|
|
|
const a=itf.alternate;
|
|
|
if(a.interfaceClass!==0xFF) continue;
|
|
|
- if(a.interfaceSubClass===0x5D) continue; // Xbox XInput vendor
|
|
|
+ if(a.interfaceSubClass===0x5D) continue;
|
|
|
let bin=0,bout=0;
|
|
|
for(const e of a.endpoints){ if(e.type==="bulk"){ if(e.direction==="in")bin=e.endpointNumber; else bout=e.endpointNumber; } }
|
|
|
if(bin&&bout){ found={ifNum:itf.interfaceNumber, epIn:bin, epOut:bout}; break; }
|
|
|
@@ -209,20 +320,17 @@ async function connect(){
|
|
|
if(!found){ log("no WebUSB bulk vendor interface — reflash firmware with WebUSB enabled, or another app may be holding the device"); return; }
|
|
|
ifNum=found.ifNum; epIn=found.epIn; epOut=found.epOut;
|
|
|
await dev.claimInterface(ifNum);
|
|
|
- // WebUSB "connect" handshake: Adafruit_USBD_WebUSB only enables write() / connected()
|
|
|
- // after the host sends SET_CONTROL_LINE_STATE (bRequest 0x22, value=1) on this interface.
|
|
|
await dev.controlTransferOut({requestType:"class", recipient:"interface", request:0x22, value:0x01, index:ifNum});
|
|
|
$("#connState").textContent="connected"; $("#connState").className="pill up";
|
|
|
$("#connectBtn").textContent="Reconnect";
|
|
|
$("#panel").classList.remove("hide");
|
|
|
log(`connected — VID ${dev.vendorId.toString(16)} iface ${ifNum} epIn ${epIn} epOut ${epOut}`);
|
|
|
- dev.onUSBConnectionEvent;
|
|
|
navigator.usb.addEventListener("disconnect", e=>{ if(e.device===dev){ onGone(); }});
|
|
|
startPolling();
|
|
|
}catch(e){ log("connect failed: "+e.message); }
|
|
|
}
|
|
|
function onGone(){
|
|
|
- polling=false; dev=null;
|
|
|
+ polling=false; dev=null; tabInited=false; // re-default the config tab to the mode on next connect
|
|
|
$("#connState").textContent="disconnected"; $("#connState").className="pill dn";
|
|
|
$("#panel").classList.add("hide");
|
|
|
log("device disconnected (likely a mode-switch reboot — reconnect)");
|
|
|
@@ -234,10 +342,10 @@ async function send(bytes){
|
|
|
}
|
|
|
async function readBlob(){
|
|
|
try{
|
|
|
- const r = await dev.transferIn(epIn, 64);
|
|
|
+ // v8 blob is 75 bytes (two USB-FS packets); read 128 to capture both in one call.
|
|
|
+ const r = await dev.transferIn(epIn, 128);
|
|
|
if(r.status!=="ok"||r.data.byteLength<2) return null;
|
|
|
const d=new Uint8Array(r.data.buffer);
|
|
|
- // find 0xA5 frame
|
|
|
let i=0; while(i<d.length && d[i]!==0xA5) i++;
|
|
|
if(i+2>d.length) return null;
|
|
|
const len=d[i+1]; const p=d.slice(i+2, i+2+len);
|
|
|
@@ -245,52 +353,137 @@ async function readBlob(){
|
|
|
}catch(e){ if(dev) log("read err: "+e.message); return null; }
|
|
|
}
|
|
|
function applyBlob(p){
|
|
|
- // [ver][mode][mDiv][mFric][rsvd][abSwap][b0..b3][slot][up][f1lo][f1hi][pollU100(unused)][newlo][newhi]
|
|
|
- const mode=p[1], mDiv=p[2], mFric=p[3], qamMap=p[4], abSwap=p[5]; // p[4] = QAM remap code (0 = default)
|
|
|
- const back=[p[6],p[7],p[8],p[9]], slot=p[10], up=p[11], f1=(p[12]|(p[13]<<8));
|
|
|
+ const mode=p[1], mDiv=p[2], mFric=p[3]; // p[4..11] mirror the active type (per-type block below is authoritative)
|
|
|
+ const slot=p[10], up=p[11], f1=(p[12]|(p[13]<<8));
|
|
|
const newps=(p.length>16?(p[15]|(p[16]<<8)):0), persistMode=(p.length>22?p[22]:0);
|
|
|
- const chord=(p.length>25)?[p[23],p[24],p[25]]:[3,1,4]; // v2 blob: chordBtn after qos/persist (Y defaults to Switch Pro=4)
|
|
|
- const polls=(p.length>27?(p[26]|(p[27]<<8)):0); // poll TX rate
|
|
|
- const loopUs=(p.length>29?(p[28]|(p[29]<<8)):0); // loop diag
|
|
|
+ const chord=(p.length>25)?[p[23],p[24],p[25]]:[3,1,4];
|
|
|
+ const polls=(p.length>27?(p[26]|(p[27]<<8)):0);
|
|
|
+ const loopUs=(p.length>29?(p[28]|(p[29]<<8)):0);
|
|
|
const worstIdx=(p.length>30?p[30]:0), worstUs=(p.length>32?(p[31]|(p[32]<<8)):0);
|
|
|
const WORST=["webusb","ctrl.task","serial","rfdiag","rflink","haptic","led"];
|
|
|
- const pollUs=(p.length>34?(p[33]|(p[34]<<8)):0); // MEASURED poll period
|
|
|
- const pollIntended=(p.length>14?p[14]*100:0); // intended poll interval (g_pollUs)
|
|
|
- const logEnabled=(p.length>35?p[35]:0); // is this a logging build (-DOPK_LOG=1)?
|
|
|
+ const pollUs=(p.length>34?(p[33]|(p[34]<<8)):0);
|
|
|
+ const pollIntended=(p.length>14?p[14]*100:0);
|
|
|
+ const logEnabled=(p.length>35?p[35]:0);
|
|
|
for(const el of document.querySelectorAll(".logonly")) el.style.display = logEnabled ? "" : "none";
|
|
|
- // mode buttons
|
|
|
for(const b of document.querySelectorAll(".modebtn")) b.classList.toggle("active", +b.dataset.mode===mode);
|
|
|
$("#mouseCard").style.opacity = (mode===1||mode===3)?1:0.5;
|
|
|
- // status
|
|
|
- const battery=(p.length>36?p[36]:0); // controller battery % (report 0x43); 0 = unknown
|
|
|
- const rumbleScale=(p.length>51?p[51]:200); // rumble strength % (protocol v5+; older blob -> default)
|
|
|
- const swProRate=(p.length>52?p[52]:1); // Switch Pro report rate 0/1/2 (protocol v6+; default 120Hz)
|
|
|
- const swGyro10=(p.length>53?p[53]:10); // Switch Pro gyro scale x10 (protocol v6+)
|
|
|
- const rssi=(p.length>37?p[37]:0); // RAW signal strength |dBm| (0 = no sample)
|
|
|
- $("#stLink").innerHTML = up? '<span class="pill up">up</span>' : '<span class="pill dn">down</span>';
|
|
|
- $("#stBatt").textContent = (up && battery)? battery+"%" : "—";
|
|
|
- $("#stRssi").textContent = (up && rssi)? ("-"+rssi+" dBm") : "—";
|
|
|
- $("#stSlot").textContent = slot===0xFF? "—" : slot;
|
|
|
+ const battery=(p.length>36?p[36]:0);
|
|
|
+ const rumbleScale=(p.length>51?p[51]:200);
|
|
|
+ const swProRate=(p.length>52?p[52]:2);
|
|
|
+ const swGyro10=(p.length>53?p[53]:10);
|
|
|
+ const rssi=(p.length>37?p[37]:0);
|
|
|
+
|
|
|
+ // per-slot link status (protocol v8, p.length >= 73)
|
|
|
+ const hasSlotsData = p.length >= 73;
|
|
|
+ if (hasSlotsData) {
|
|
|
+ const bondedN = p[60];
|
|
|
+ g_slotData = [];
|
|
|
+ for (let s=0; s<4; s++) {
|
|
|
+ const base = 61 + s*3;
|
|
|
+ if (p.length >= base+3) {
|
|
|
+ // only include bonded slots (a slot with up=0 could be bonded but offline)
|
|
|
+ // check if the slot appears at all in the bonded map by checking if bondedN covers it;
|
|
|
+ // the firmware only sets non-zero battery/rssi for actually-bonded slots
|
|
|
+ const slotUp = !!p[base];
|
|
|
+ const slotBatt = p[base+1];
|
|
|
+ const slotRssi = p[base+2];
|
|
|
+ // slot is "bonded" if firmware included it in bondedN coverage
|
|
|
+ // we populate slotData for all 4 and renderSlotTabs skips gaps
|
|
|
+ g_slotData[s] = {up: slotUp, battery: slotBatt, rssi: slotRssi};
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // keep only bonded slots by cross-referencing bondedN
|
|
|
+ // firmware sends bondedSlotCount but not a slot bitmask; use battery>0 || rssi>0 || up as presence hint
|
|
|
+ // simpler: just count how many of the 4 slots have any non-zero data OR are marked up
|
|
|
+ // Actually the firmware always writes all 4 slots; we filter by the bond system's "used" flag via presence.
|
|
|
+ // For clean display: we'll show tabs only for slots that have ever had a reply OR are bonded (battery/rssi set).
|
|
|
+ // The firmware sets g_battery[s]=0 and g_linkRssi[s]=0 for unused slots, so 0/0/0 = not bonded.
|
|
|
+ // But a bonded offline slot also has 0/0/0 initially. We rely on bondedN for count and show the first bondedN slots
|
|
|
+ // that have g_slot[s].used — but we can't easily know which slots are used from the blob.
|
|
|
+ // Best heuristic: show all 4 slots if bondedN==4, otherwise use slot indices where battery or rssi != 0 OR up==1,
|
|
|
+ // OR fall back to showing slots 0..bondedN-1 if none have data yet (device just booted).
|
|
|
+ let bondedSlots = [];
|
|
|
+ for (let s=0; s<4; s++) {
|
|
|
+ if (g_slotData[s] && (g_slotData[s].up || g_slotData[s].battery || g_slotData[s].rssi)) bondedSlots.push(s);
|
|
|
+ }
|
|
|
+ // fallback: if no data yet (all offline, just booted), show slots 0..bondedN-1
|
|
|
+ if (bondedSlots.length === 0 && bondedN > 0) {
|
|
|
+ for (let s=0; s<bondedN; s++) bondedSlots.push(s);
|
|
|
+ }
|
|
|
+ // remove slotData for non-bonded slots so tabs don't appear for them
|
|
|
+ const bondedSet = new Set(bondedSlots);
|
|
|
+ for (let s=0; s<4; s++) { if (!bondedSet.has(s)) g_slotData[s] = null; }
|
|
|
+ // clamp active slot to a valid bonded slot
|
|
|
+ if (!g_slotData[g_activeSlot]) g_activeSlot = bondedSlots[0] || 0;
|
|
|
+ renderSlotTabs(bondedN);
|
|
|
+ } else {
|
|
|
+ // old firmware: single-slot display
|
|
|
+ $("#slotTabs").style.display="none";
|
|
|
+ $("#stLink").innerHTML = up? '<span class="pill up">up</span>' : '<span class="pill dn">down</span>';
|
|
|
+ $("#stBatt").textContent = (up && battery)? battery+"%" : "—";
|
|
|
+ $("#stRssi").textContent = (up && rssi)? ("-"+rssi+" dBm") : "—";
|
|
|
+ }
|
|
|
+
|
|
|
+ // global stats
|
|
|
$("#stRate").textContent = up? f1+" /s" : "—";
|
|
|
$("#stNew").textContent = up? newps+" /s" : "—";
|
|
|
$("#stPolls").textContent = up? polls+" /s" : "—";
|
|
|
$("#stLoop").textContent = loopUs? loopUs+" µs" : "—";
|
|
|
$("#stWorst").textContent = loopUs? (WORST[worstIdx]||worstIdx)+" "+worstUs+"µs" : "—";
|
|
|
$("#stPollPer").textContent = up? (pollUs+" / "+pollIntended+" µs") : "—";
|
|
|
- // build provenance (protocol v4+): dirty flag at p[38], 12-byte NUL-padded git hash at p[39..50]. Independent
|
|
|
- // of link state. Older firmware sends a shorter blob -> stays "—".
|
|
|
+ // reset-cause history (protocol v9): JS p[73]=count, p[74..83]=last 10 causes (newest first).
|
|
|
+ // Persisted in puck flash, so a reboot that happened on a Switch is still readable here after replugging.
|
|
|
+ function resetReasonText(rr){
|
|
|
+ if (rr === 0) return "power-on";
|
|
|
+ const r = [];
|
|
|
+ if (rr & 0x02) r.push("watchdog"); // DOG: loop wedge / hang
|
|
|
+ if (rr & 0x08) r.push("LOCKUP"); // CPU lockup = HardFault escalation (real crash)
|
|
|
+ if (rr & 0x04) r.push("soft reboot"); // SREQ: NVIC_SystemReset (mode change etc.)
|
|
|
+ if (rr & 0x01) r.push("reset pin");
|
|
|
+ return r.length ? r.join("+") : ("0x"+rr.toString(16));
|
|
|
+ }
|
|
|
+ if (p.length > 73) {
|
|
|
+ const n = Math.min(p[73], 10);
|
|
|
+ const hist = [];
|
|
|
+ for (let i=0; i<n; i++) hist.push(p[74+i]);
|
|
|
+ const fault = (rr)=>!!(rr & 0x0A); // watchdog or lockup = a real fault
|
|
|
+ if (n === 0) { $("#stReset").textContent = "—"; }
|
|
|
+ else {
|
|
|
+ const newest = hist[0];
|
|
|
+ const head = fault(newest) ? ('<span class="pill dn">'+resetReasonText(newest)+'</span>') : resetReasonText(newest);
|
|
|
+ $("#stReset").innerHTML = head;
|
|
|
+ // full history list under the tile (newest -> oldest)
|
|
|
+ const items = hist.map((rr,i)=>{
|
|
|
+ const t = resetReasonText(rr);
|
|
|
+ return (i===0?'<strong>':'')+ '#'+(n-i)+' '+(fault(rr)?('<span style="color:#ff6b6b">'+t+'</span>'):t) +(i===0?' (latest)</strong>':'');
|
|
|
+ });
|
|
|
+ const el = $("#resetHist"); if (el) el.innerHTML = items.join(' · ');
|
|
|
+ }
|
|
|
+ }
|
|
|
const gitDirty=(p.length>38?p[38]:0); let gitHash="";
|
|
|
for(let i=39;i<51 && i<p.length && p[i];i++) gitHash+=String.fromCharCode(p[i]);
|
|
|
$("#stBuild").innerHTML = gitHash
|
|
|
? (gitHash + (gitDirty ? ' <span class="pill dn">dirty</span>' : ' <span class="pill up">clean</span>'))
|
|
|
: "—";
|
|
|
- // sliders (only if not being dragged)
|
|
|
setSlider("mDiv",mDiv); setSlider("mFric",mFric); setSlider("rumble",rumbleScale);
|
|
|
- $("#abSwap").textContent = abSwap? "on":"off"; $("#abSwap").classList.toggle("active",!!abSwap);
|
|
|
$("#persistMode").textContent = persistMode? "on":"off"; $("#persistMode").classList.toggle("active",!!persistMode);
|
|
|
- for(const sel of document.querySelectorAll("select.back")){ if(document.activeElement!==sel) sel.value=back[+sel.dataset.i]; }
|
|
|
- { const sel=$("#qamMap"); if(document.activeElement!==sel) sel.value=qamMap; }
|
|
|
- // raw accelerometer diagnostic (protocol v7): 3x s16 LE at p[54..59]
|
|
|
+ // per-type button config (blob v9): 4 types x 7 bytes. readBlob() strips the 2-byte [0xA5][len] header, so
|
|
|
+ // the firmware's payload byte 75 lands at JS index 73 here.
|
|
|
+ if(p.length>=73+TYPE_DEFS.length*7){
|
|
|
+ const activeEt=etypeForMode(mode);
|
|
|
+ typeEls.forEach((rec,et)=>{
|
|
|
+ const q=73+et*7;
|
|
|
+ const back=[p[q],p[q+1],p[q+2],p[q+3]], qam=p[q+4], ab=p[q+5], pad=p[q+6];
|
|
|
+ rec.back.forEach((sel,i)=>{ if(document.activeElement!==sel) sel.value=back[i]; });
|
|
|
+ if(document.activeElement!==rec.qam) rec.qam.value=qam;
|
|
|
+ rec.abSwap.textContent=ab?"on":"off"; rec.abSwap.classList.toggle("active",!!ab);
|
|
|
+ rec.pad.textContent=pad?"on":"off"; rec.pad.classList.toggle("active",!!pad);
|
|
|
+ // dot marks the type matching the current puck mode
|
|
|
+ rec.activeDot.style.display = (et===activeEt) ? "" : "none";
|
|
|
+ });
|
|
|
+ // open the tab for the current mode's type on first load (then leave the user's choice alone)
|
|
|
+ if(!tabInited){ tabInited=true; setTab(activeEt>=0?activeEt:0); }
|
|
|
+ }
|
|
|
if(p.length>=60){ const s16=(o)=>{ let v=p[o]|(p[o+1]<<8); return v>32767?v-65536:v; };
|
|
|
const ax=s16(54),ay=s16(56),az=s16(58);
|
|
|
const amag=Math.round(Math.sqrt(ax*ax+ay*ay+az*az));
|
|
|
@@ -305,7 +498,7 @@ function setSlider(id,v){ const el=$("#"+id); if(document.activeElement!==el){ e
|
|
|
|
|
|
let inflight=false;
|
|
|
async function refresh(){
|
|
|
- if(!dev||inflight||capturing) return; inflight=true; // don't let the status poll steal capture frames
|
|
|
+ if(!dev||inflight||capturing) return; inflight=true;
|
|
|
await send([0x01]); const p=await readBlob(); if(p) applyBlob(p);
|
|
|
inflight=false;
|
|
|
}
|
|
|
@@ -314,9 +507,9 @@ async function setField(field,value){
|
|
|
}
|
|
|
function fmtCapEntry(e,maxMs){
|
|
|
const hex=e.bytes.map(x=>x.toString(16).padStart(2,"0")).join(" ");
|
|
|
- const t = `-${String(maxMs-e.ms).padStart(6," ")}ms `; // age vs the newest entry (consistent across drain batches)
|
|
|
+ const t = `-${String(maxMs-e.ms).padStart(6," ")}ms `;
|
|
|
if(e.slot===0xFD){ const k=e.bytes[0]; return `${t}════════ ${k===2?"RECONNECT (block+reinit armed)":k===1?"RF LINK UP":"RF LINK DOWN"} ════════`; }
|
|
|
- const who = (e.slot===0xFE) ? "TX→ctlr " : `if${e.slot} `; // 0xFE = frame WE sent the controller
|
|
|
+ const who = (e.slot===0xFE) ? "TX→ctlr " : `if${e.slot} `;
|
|
|
return `${t}${who} cmd=${e.rid.toString(16).padStart(2,"0")} n=${e.nb}: ${hex}`;
|
|
|
}
|
|
|
function downloadCap(){
|
|
|
@@ -325,11 +518,10 @@ function downloadCap(){
|
|
|
a.href=URL.createObjectURL(new Blob([t],{type:"text/plain"}));
|
|
|
a.download="puck-capture.txt"; a.click();
|
|
|
}
|
|
|
-// ---- live Start/Stop capture: poll the firmware's drain (op 0x06) on a timer and accumulate in the browser ----
|
|
|
-let liveTimer=null, draining=false, capLines=[]; // capLines = array of {ms,slot,rid,nb,bytes}
|
|
|
+let liveTimer=null, draining=false, capLines=[];
|
|
|
function renderCap(){
|
|
|
if(!capLines.length){ $("#capOut").textContent="(nothing captured yet)"; window._lastCap=""; return; }
|
|
|
- const maxMs=capLines[capLines.length-1].ms; // entries are chronological -> newest is last
|
|
|
+ const maxMs=capLines[capLines.length-1].ms;
|
|
|
const t=capLines.map(e=>fmtCapEntry(e,maxMs)).join("\n");
|
|
|
$("#capOut").textContent=t; window._lastCap=t;
|
|
|
}
|
|
|
@@ -364,15 +556,15 @@ async function drainTick(){
|
|
|
}
|
|
|
async function startCapture(){
|
|
|
if(!dev||capturing) return;
|
|
|
- capturing=true; capLines=[]; // capturing=true pauses the status poll so it can't steal frames
|
|
|
+ capturing=true; capLines=[];
|
|
|
$("#capOut").textContent="(dumping ring from boot, then live — press Stop when done)";
|
|
|
$("#capStart").disabled=true; $("#capStop").disabled=false;
|
|
|
- await send([0x05,1]); // rewind drain to OLDEST -> dumps the whole ring from boot
|
|
|
+ await send([0x05,1]);
|
|
|
liveTimer=setInterval(drainTick,100);
|
|
|
}
|
|
|
async function stopCapture(){
|
|
|
if(liveTimer){ clearInterval(liveTimer); liveTimer=null; }
|
|
|
- await drainTick(); // final flush
|
|
|
+ await drainTick();
|
|
|
try{ await send([0x05,0]); }catch(e){}
|
|
|
capturing=false;
|
|
|
$("#capStart").disabled=false; $("#capStop").disabled=true;
|
|
|
@@ -390,28 +582,32 @@ $("#capStart").onclick=startCapture;
|
|
|
$("#capStop").onclick=stopCapture;
|
|
|
$("#hapClear").onclick=async()=>{ if(dev){ await send([0x07]); log("haptic re-init sent (clear stuck buzz)"); } };
|
|
|
$("#ctlrOff").onclick=async()=>{ if(dev){ await send([0x08]); log("controller power-off attempt sent — watch link status"); } };
|
|
|
+$("#dfuSerial").onclick=async()=>{
|
|
|
+ if(!dev){ log("not connected"); return; }
|
|
|
+ if(!confirm("Reboot into serial DFU?\n\nThe puck will disconnect immediately. Flash with adafruit-nrfutil, then replug.")) return;
|
|
|
+ await send([0x0B]); log("serial DFU reboot sent — device disconnecting");
|
|
|
+};
|
|
|
+$("#dfuUf2").onclick=async()=>{
|
|
|
+ if(!dev){ log("not connected"); return; }
|
|
|
+ if(!confirm("Reboot into UF2 bootloader?\n\nThe puck will disconnect and mount as a USB drive. Drag the .uf2 file onto it to flash.")) return;
|
|
|
+ await send([0x0C]); log("UF2 bootloader reboot sent — device disconnecting");
|
|
|
+};
|
|
|
$("#factoryErase").onclick=async()=>{
|
|
|
if(!dev){ log("not connected"); return; }
|
|
|
- // Triple gate so this is impossible to fire accidentally: warn, warn harder, then require typing ERASE.
|
|
|
if(!confirm("Factory erase?\n\nThis wipes ALL persistent storage on the copycat:\n • the paired-controller bond (you'll have to re-pair)\n • every saved setting (mode, chords, back paddles, sensitivity)\n\nThe copycat reboots to factory defaults. This CANNOT be undone.")) return;
|
|
|
if(!confirm("Are you absolutely sure?\n\nThere is no recovery. The controller bond and all settings will be gone.")) return;
|
|
|
const typed=prompt('Final confirmation — type ERASE (all caps) to wipe everything:');
|
|
|
if(typed!=="ERASE"){ log("factory erase cancelled (confirmation text did not match)"); return; }
|
|
|
- // op 0x0A + magic "ERS" (0x45,0x52,0x53) -- firmware reformats the FS and reboots; the device drops off USB.
|
|
|
await send([0x0A,0x45,0x52,0x53]);
|
|
|
log("FACTORY ERASE sent — copycat is reformatting and rebooting to defaults. Re-pair the controller, then reconnect.");
|
|
|
};
|
|
|
-$("#abSwap").onclick=()=>{ const on=$("#abSwap").classList.contains("active"); setField(4, on?0:1); };
|
|
|
$("#persistMode").onclick=()=>{ const on=$("#persistMode").classList.contains("active"); setField(16, on?0:1); };
|
|
|
for(const id of ["mDiv","mFric","rumble"]){
|
|
|
const field={mDiv:1,mFric:2,rumble:22}[id];
|
|
|
$("#"+id).addEventListener("input", ()=>{ $("#"+id+"V").textContent=fmtSlider(id,$("#"+id).value); });
|
|
|
$("#"+id).addEventListener("change", ()=>setField(field, +$("#"+id).value));
|
|
|
}
|
|
|
-for(const sel of document.querySelectorAll("select.back")){
|
|
|
- sel.addEventListener("change", ()=>setField(5 + +sel.dataset.i, +sel.value));
|
|
|
-}
|
|
|
-$("#qamMap").addEventListener("change", ()=>setField(21, +$("#qamMap").value));
|
|
|
+// per-type back/QAM/A-B-swap/haptics handlers are wired in buildTypeCfgs()
|
|
|
$("#swRate").addEventListener("change", ()=>setField(23, +$("#swRate").value));
|
|
|
$("#swGyro").addEventListener("change", ()=>setField(24, +$("#swGyro").value));
|
|
|
for(const sel of document.querySelectorAll("select.chord")){
|