Skip to content

Commit

Permalink
Make stop actually stop the simulator.
Browse files Browse the repository at this point in the history
It's behaviour now matches how it's used for restart and flash.
  • Loading branch information
microbit-matt-hillsdon committed Nov 2, 2022
1 parent 08c365e commit 556c80b
Showing 1 changed file with 32 additions and 8 deletions.
40 changes: 32 additions & 8 deletions src/board/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,12 +123,16 @@ export class Board {
return result ?? id;
};

/**
* Defined for the duration of start().
*/
private runningPromise: Promise<void> | undefined;
/**
* Defined during start().
*/
private modulePromise: Promise<ModuleWrapper> | undefined;
/**
* Defined by start but async.
* Defined during start().
*/
private module: ModuleWrapper | undefined;
/**
Expand Down Expand Up @@ -385,7 +389,19 @@ export class Board {
this.stoppedOverlay.style.display = "flex";
}

private async start() {
/**
* Start the simulator.
*
* @returns a promise that resolves when the simulator has stopped.
*/
private start(): void {
if (this.runningPromise) {
throw new Error("Already running!");
}
this.runningPromise = this.createRunningPromise();
}

private async createRunningPromise() {
if (this.modulePromise || this.module) {
throw new Error("Module already exists!");
}
Expand Down Expand Up @@ -453,8 +469,17 @@ export class Board {
}
}
this.stopKind = StopKind.Default;
this.runningPromise = undefined;
}

/**
* Stop the simulator.
*
* This cancels any pending restart or panic requested by the program.
*
* @param brief If true the stopped UI is not shown.
* @returns A promise that resolves when the simulator is stopped.
*/
async stop(brief: boolean = false): Promise<void> {
if (this.panicTimeout) {
clearTimeout(this.panicTimeout);
Expand All @@ -481,14 +506,15 @@ export class Board {
// Ctrl-C, Ctrl-D to interrupt the main loop.
this.writeSerialInput("\x03\x04");
}
return this.runningPromise;
}

/**
* An external reset.
*/
async reset(): Promise<void> {
await this.stop(true);
return this.start();
this.start();
}

async flash(filesystem: Record<string, Uint8Array>): Promise<void> {
Expand All @@ -500,10 +526,8 @@ export class Board {
});
this.dataLogging.delete();
};
if (this.modulePromise) {
// If it's running then we need to stop before flash.
await this.stop(true);
}
// Ensure it's stopped before flash.
await this.stop(true);
flashFileSystem();
return this.start();
}
Expand Down Expand Up @@ -648,7 +672,7 @@ export class Board {

writeRadioRxBuffer(packet: Uint8Array): number {
if (!this.module) {
throw new Error("Must be running");
throw new Error("Must be running as called via HAL");
}
return this.module.writeRadioRxBuffer(packet);
}
Expand Down

0 comments on commit 556c80b

Please sign in to comment.