From 356cfac1545dcb977cc0a98614ad8a36bcd24c26 Mon Sep 17 00:00:00 2001 From: Nikunj Kotecha Date: Fri, 30 Oct 2026 16:52:32 -0700 Subject: [PATCH] fix(app): wait out a board's first start of a model it copies to SRAM A BrainBoard1500 that runs a model from its AKD1500 SRAM copies the model there from flash over the 2 MHz bridge on its first load, and only then acknowledges the start. For Vision Human Detection that copy takes 3.24 s, longer than the 3 s the app waited, so the first Run after power up reported a start error although the board had started. The start now waits 10 s. The stop keeps its 3 s wait, because a stop on the board only clears the running flags and loads nothing. --- __tests__/appTransition.test.ts | 37 +++++++++++++++++++++++++++-- src/app/store/useBleCommandStore.ts | 17 +++++++++---- 2 files changed, 48 insertions(+), 6 deletions(-) diff --git a/__tests__/appTransition.test.ts b/__tests__/appTransition.test.ts index f5cd4c4..5384e00 100644 --- a/__tests__/appTransition.test.ts +++ b/__tests__/appTransition.test.ts @@ -94,19 +94,52 @@ describe('what the store says while the board changes application', () => { expect(useBleCommandStore.getState().activeApp).toBeNull(); }); + it('waits out a first start that copies the model into SRAM', async () => { + useBleCommandStore.setState({ activeApp: null }); + const started = useBleCommandStore.getState().deployApp('vision'); + await Promise.resolve(); + + await jest.advanceTimersByTimeAsync(3500); + expect(useBleCommandStore.getState().appTransition).toEqual({ + appId: 'vision', + kind: 'starting', + }); + + notify({ type: 'DEPLOYSTART_ACK' }); + await started; + + expect(useBleCommandStore.getState().appTransition).toBeNull(); + expect(useBleCommandStore.getState().activeApp).toBe('vision'); + expect(useBleCommandStore.getState().isInferenceRunning).toBe(true); + }); + it('gives up the wait, and says so, when the board never answers', async () => { useBleCommandStore.setState({ activeApp: null }); const started = useBleCommandStore.getState().deployApp('keyword'); await Promise.resolve(); expect(useBleCommandStore.getState().appTransition).not.toBeNull(); - jest.advanceTimersByTime(3000); + const outcome = expect(started).rejects.toThrow('DEPLOYSTART ACK timeout'); + await jest.advanceTimersByTimeAsync(9999); + expect(useBleCommandStore.getState().appTransition).not.toBeNull(); + + await jest.advanceTimersByTimeAsync(1); - await expect(started).rejects.toThrow('DEPLOYSTART ACK timeout'); + await outcome; expect(useBleCommandStore.getState().appTransition).toBeNull(); expect(useBleCommandStore.getState().activeApp).toBeNull(); }); + it('gives up a stop the board never confirms after three seconds', async () => { + const stopped = useBleCommandStore.getState().stopApp('vision'); + await Promise.resolve(); + + jest.advanceTimersByTime(3000); + + await expect(stopped).rejects.toThrow('DEPLOYSTOP ACK timeout'); + expect(useBleCommandStore.getState().appTransition).toBeNull(); + }); + it('gives up the wait when the command itself cannot be sent', async () => { mockSendCommand.mockRejectedValueOnce(new Error('Device disconnected')); diff --git a/src/app/store/useBleCommandStore.ts b/src/app/store/useBleCommandStore.ts index e967337..27ea710 100644 --- a/src/app/store/useBleCommandStore.ts +++ b/src/app/store/useBleCommandStore.ts @@ -74,8 +74,17 @@ export interface AppTransition { kind: 'starting' | 'stopping'; } -/** How long to wait for the board to confirm a start or stop. */ -const DEPLOY_ACK_TIMEOUT_MS = 3000; +/** + * How long to wait for the board to confirm a start. The first start of a + * model a BrainBoard1500 runs from its SRAM copies the model there from flash + * over a 2 MHz bridge, 3.24 s for human detection, and only then confirms. + * This covers that copy, even one filling the whole SRAM, plus the Bluetooth + * round trip and the board's own start work on a slow phone or busy link. + */ +const DEPLOY_START_ACK_TIMEOUT_MS = 10000; + +/** How long to wait for the board to confirm a stop, which loads nothing. */ +const DEPLOY_STOP_ACK_TIMEOUT_MS = 3000; // Preview chunks are put together here, outside the store's state, because a // half-built image is nothing the UI should ever see. @@ -652,7 +661,7 @@ export const useBleCommandStore = create((set, get) => ({ const timeout = new Promise((_, reject) => { timer = setTimeout( () => reject(new Error('DEPLOYSTART ACK timeout')), - DEPLOY_ACK_TIMEOUT_MS, + DEPLOY_START_ACK_TIMEOUT_MS, ); }); @@ -698,7 +707,7 @@ export const useBleCommandStore = create((set, get) => ({ const timeout = new Promise((_, reject) => { timer = setTimeout( () => reject(new Error('DEPLOYSTOP ACK timeout')), - DEPLOY_ACK_TIMEOUT_MS, + DEPLOY_STOP_ACK_TIMEOUT_MS, ); });