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
+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