Skip to content

Move to interface #32

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 20 additions & 17 deletions lib/device.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,25 @@ export enum ConnectionStatus {
RECONNECTING = "RECONNECTING",
}

export interface FlashOptions {
/**
* True to use a partial flash where possible, false to force a full flash.
*/
partial: boolean;
/**
* A progress callback. Called with undefined when the process is complete or has failed.
*
* Requesting a partial flash doesn't guarantee one is performed. Partial flashes are avoided
* if too many blocks have changed and failed partial flashes are retried as full flashes.
* The partial parameter reports the flash type currently in progress.
*/
progress: (percentage: number | undefined, partial: boolean) => void;
/**
* Smallest possible progress increment to limit callback rate.
*/
minimumProgressIncrement?: number;
}

export class FlashDataError extends Error {}

export type FlashDataSource = (
Expand Down Expand Up @@ -201,23 +220,7 @@ export interface DeviceConnection
* @param dataSource The data to use.
* @param options Flash options and progress callback.
*/
flash?(
dataSource: FlashDataSource,
options: {
/**
* True to use a partial flash where possible, false to force a full flash.
*/
partial: boolean;
/**
* A progress callback. Called with undefined when the process is complete or has failed.
*
* Requesting a partial flash doesn't guarantee one is performed. Partial flashes are avoided
* if too many blocks have changed and failed partial flashes are retried as full flashes.
* The partial parameter reports the flash type currently in progress.
*/
progress: (percentage: number | undefined, partial: boolean) => void;
},
): Promise<void>;
flash?(dataSource: FlashDataSource, options: {}): Promise<void>;

/**
* Disconnect from the device.
Expand Down
19 changes: 6 additions & 13 deletions lib/usb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {
FlashDataError,
FlashDataSource,
FlashEvent,
FlashOptions,
SerialDataEvent,
SerialErrorEvent,
SerialResetEvent,
Expand Down Expand Up @@ -201,11 +202,7 @@ export class MicrobitWebUSBConnection

async flash(
dataSource: FlashDataSource,
options: {
partial: boolean;
progress: (percentage: number | undefined) => void;
miniumProgressIncrement: number;
},
options: FlashOptions,
): Promise<void> {
this.flashing = true;
try {
Expand All @@ -230,11 +227,7 @@ export class MicrobitWebUSBConnection

private async flashInternal(
dataSource: FlashDataSource,
options: {
partial: boolean;
progress: (percentage: number | undefined, partial: boolean) => void;
miniumProgressIncrement: number;
},
options: FlashOptions,
): Promise<void> {
this.log("Stopping serial before flash");
await this.stopSerialInternal();
Expand All @@ -246,7 +239,7 @@ export class MicrobitWebUSBConnection

const partial = options.partial;
const progress = rateLimitProgress(
options.miniumProgressIncrement ?? 0.0025,
options.minimumProgressIncrement ?? 0.0025,
options.progress || (() => {}),
);

Expand Down Expand Up @@ -523,7 +516,7 @@ const enrichedError = (err: any): DeviceError => {
};

const rateLimitProgress = (
miniumProgressIncrement: number,
minimumProgressIncrement: number,
callback: (value: number | undefined, partial: boolean) => void,
) => {
let lastCallValue = -1;
Expand All @@ -532,7 +525,7 @@ const rateLimitProgress = (
value === undefined ||
value === 0 ||
value === 1 ||
value >= lastCallValue + miniumProgressIncrement
value >= lastCallValue + minimumProgressIncrement
) {
lastCallValue = value ?? -1;
callback(value, partial);
Expand Down