From 136ef8b7216750ee13d10fe66a98f79dde859ffc Mon Sep 17 00:00:00 2001 From: osiu97 Date: Wed, 8 Jul 2026 19:45:14 +0200 Subject: [PATCH] Allow position replay on lost connection --- gemini.md | 2 +- index.html | 3 ++- src/main.cpp | 3 ++- web/js/motion.js | 18 +++++++++++++++--- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/gemini.md b/gemini.md index ecf08c4..96a985d 100644 --- a/gemini.md +++ b/gemini.md @@ -97,7 +97,7 @@ Name: WijiBoard ### Status notifications (firmware → browser) -`P:,` — current step positions for motor 1 and 2, sent every 200 ms while moving. +`P:,,[]` — current step positions for motor 1 and 2, sent every 200 ms while moving, or on request. 3rd parameter is 1 if holding, 0 if disabled. `SYS:IDLE_TIMEOUT` — Sent when motors are automatically disabled due to 30 mins of inactivity. --- diff --git a/index.html b/index.html index ce214b1..84dfcb7 100644 --- a/index.html +++ b/index.html @@ -231,8 +231,9 @@ } }); - BLE.on('connected', () => { + BLE.on('connected', async () => { btnDisable.style.display = 'inline-flex'; + await BLE.write('POS').catch(e => console.warn('POS sync failed:', e)); }); BLE.on('disconnected', () => { btnDisable.style.display = 'none'; diff --git a/src/main.cpp b/src/main.cpp index 184677b..f8d3149 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -220,7 +220,8 @@ void sendPosition() { if (!bleConnected || !pStatusChar) return; String pos = "P:" + String(stepper1.currentPosition()) + "," + - String(stepper2.currentPosition()); + String(stepper2.currentPosition()) + "," + + String(motorsDisabled ? 0 : 1); pStatusChar->setValue(pos.c_str()); pStatusChar->notify(); Serial.printf("[POS] %s\n", pos.c_str()); diff --git a/web/js/motion.js b/web/js/motion.js index 2f08a27..d269700 100644 --- a/web/js/motion.js +++ b/web/js/motion.js @@ -15,9 +15,21 @@ let steps2 = 0; document.addEventListener('ble:status', (e) => { const msg = e.detail; if (msg.startsWith('P:')) { - const [s1, s2] = msg.slice(2).split(',').map(Number); - steps1 = s1; - steps2 = s2; + const parts = msg.slice(2).split(','); + steps1 = Number(parts[0]); + steps2 = Number(parts[1]); + + // parts[2] is the holding status (1 = holding, 0 = disabled) + if (parts.length > 2) { + const isHolding = parts[2] === '1'; + if (!isHolding && localStorage.getItem('wiji_homed') === 'true') { + localStorage.setItem('wiji_homed', 'false'); + UI.log('Steppers are not holding. Rehoming required.', 'warn'); + document.dispatchEvent(new CustomEvent('wiji:unhomed')); + const warningEl = document.getElementById('home-warning'); + if (warningEl) warningEl.style.display = 'block'; + } + } } });