Fix BLE upload: write-with-response on CMD+DATA, on-demand advertising, wake-play-random, remove startup blink

This commit is contained in:
zyphlar
2026-07-04 03:28:14 -07:00
parent d3bd7b71b9
commit 96d0c44d11
5 changed files with 68 additions and 39 deletions
+1 -1
View File
@@ -38,7 +38,7 @@ function fmtBytes(n) {
// ---- connection state ----
dev.addEventListener('log', (e) => log(e.detail));
dev.addEventListener('connected', (e) => { setConnected(true, e.detail.name); refresh(); fetchVolume(); });
dev.addEventListener('connected', async (e) => { setConnected(true, e.detail.name); await refresh(); fetchVolume(); });
dev.addEventListener('disconnected', () => {
setConnected(false); deviceTracks = null; renderSlots();
$('volume-val').textContent = '—';
+9 -9
View File
@@ -186,7 +186,7 @@ export class BabyMobile extends EventTarget {
async _writeCmd(bytes) {
this._requireConnected();
await this.cmd.writeValueWithoutResponse(Uint8Array.from(bytes));
await this.cmd.writeValue(Uint8Array.from(bytes));
}
async play(track) {
@@ -282,21 +282,21 @@ export class BabyMobile extends EventTarget {
this._log(`Starting upload of ${total} bytes to slot ${track}`);
await this._writeCmd([CMD_UPLOAD_START, track & 0xff]);
// Send all data chunks sequentially. Chrome on Windows (WinRT BLE) only
// allows one writeValueWithoutResponse in flight at a time; concurrent calls
// fail with "GATT operation already in progress". Sequential writes are
// reliable and throughput is governed by the BLE connection interval, not
// by how many calls are in flight.
// Send all data chunks using write-with-response. Each writeValue() call
// blocks until the SoftDevice sends an ATT Write Response, which only happens
// after it has delivered the packet to the firmware's write callback. This
// provides natural per-packet flow control: CMD_UPLOAD_END cannot be sent until
// every data packet has been confirmed received, eliminating all OS TX-queue
// race conditions without any byte-counting hacks.
for (let off = 0; off < total; off += DATA_CHUNK) {
await this.data.writeValueWithoutResponse(
await this.data.writeValue(
wavBytes.subarray(off, Math.min(off + DATA_CHUNK, total)));
// Cap the optimistic local count at 99%; the final 1% is filled only when
// the device's finalize ack confirms the bytes actually landed in flash.
report(Math.min(off + DATA_CHUNK, Math.floor(total * 0.99)));
}
// Tell the firmware we're done. It will drain its ring buffer, write the
// track table, then send ONE finalize notification with the total byte count.
// All data confirmed received — finalize.
await this._writeCmd([CMD_UPLOAD_END]);
// Wait for that single finalize notification. Allow up to 60 s: at 300 ms/sector
+5 -5
View File
@@ -87,15 +87,15 @@ label { display: inline-flex; align-items: center; gap: 8px; font-size: .9rem; c
.handle:focus { outline: none; }
.handle:focus .line, .handle.dragging .line { background: var(--accent); box-shadow: 0 0 6px var(--accent); }
.handle:focus .chev, .handle.dragging .chev { filter: drop-shadow(0 0 4px var(--accent)); }
.trim-readout { display: flex; align-items: center; gap: 12px; font-size: .85rem; color: var(--muted); }
.time-fields { display: inline-flex; align-items: center; gap: 6px; }
.trim-readout { display: flex; align-items: center; flex-wrap: wrap; gap: 8px 12px; font-size: .85rem; color: var(--muted); }
.time-fields { display: inline-flex; align-items: center; gap: 6px; flex-shrink: 0; }
.time-fields .dash { color: var(--muted); }
.time-in {
width: 4.2rem; padding: 5px 7px; font-size: .85rem;
width: 3.8rem; min-width: 0; padding: 5px 7px; font-size: .85rem;
background: #1c123e; color: var(--text); border: 1px solid var(--line); border-radius: 7px;
}
.trim-readout .clip { color: var(--accent); font-weight: 600; }
.trim-readout button { margin-left: auto; }
.trim-readout .clip { margin-left: auto; color: var(--accent); font-weight: 600; }
.trim-readout button { flex-shrink: 0; }
.level { display: flex; align-items: center; gap: 10px; width: 100%; margin-top: 12px; font-size: .85rem; color: var(--muted); }
.level input[type=range] { flex: 1; min-width: 0; }
.level .unit { min-width: 3.2rem; text-align: right; color: var(--accent); font-weight: 600; }
+1 -1
View File
@@ -1,5 +1,5 @@
// sw.js — minimal offline cache so the PWA launches without a network.
const CACHE = 'babymobile-v8';
const CACHE = 'babymobile-v9';
const ASSETS = [
'./', './index.html', './styles.css',
'./app.js', './ble.js', './wav.js',