Skip to content

Commit 407ea86

Browse files
Working initial flash progress
It needs to become richer to deal with the multi-stage process and indeterminate parts but we'll revisit that later. NPM script for hot reload
1 parent a18f007 commit 407ea86

File tree

5 files changed

+30
-10
lines changed

5 files changed

+30
-10
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"ci": "npm run test && npm run lint && npm run build && npx prettier --check src",
1111
"deploy": "website-deploy-aws",
1212
"dev": "cross-env VITE_VERSION=$npm_package_version vite",
13+
"dev:apps": "cross-env VITE_BUILD_MODE=apps VITE_VERSION=$npm_package_version vite --host",
1314
"invalidate": "invalidate-cloudfront-distribution",
1415
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0",
1516
"postinstall": "npm run theme",

src/connect-actions.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,16 @@ export class ConnectActions {
128128
: createUniversalHexFlashDataSource(hex);
129129

130130
try {
131-
await connection.flash(data, {
131+
const options = {
132+
// TODO: not supported by BLE (it's always partial first)
132133
partial: true,
133134
// If we could improve the re-rendering due to progress further we can remove this and accept the
134135
// default which updates 4x as often.
136+
// TODO: Needed for BLE?
135137
minimumProgressIncrement: 0.01,
136138
progress: (v: number | undefined) => progress(v ?? 1),
137-
});
139+
};
140+
await connection.flash(data, options);
138141
return ConnectResult.Success;
139142
} catch (e) {
140143
this.logging.error(`USB flashing failed: ${JSON.stringify(e)}`);

src/device/capacitor-ble/index.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ import { DeviceInformationService } from "./device-information-service";
2424
import MemoryMap from "nrf-intel-hex";
2525
import partialFlash, { PartialFlashResult } from "./flashing-partial";
2626
import { fullFlash } from "./flashing-full";
27-
import { FlashResult } from "./model";
27+
import { FlashProgressStage, FlashResult, Progress } from "./model";
2828
import { AccelerometerService } from "./acceleromoeter-service";
2929

3030
export class MicrobitCapacitorBluetoothConnection
@@ -190,13 +190,25 @@ export class MicrobitCapacitorBluetoothConnection
190190
* @param dataSource The data to use.
191191
* @param options Flash options and progress callback.
192192
*/
193-
async flash(dataSource: FlashDataSource): Promise<void> {
194-
// We'll disconnect/reconnect multiple times but reporting this is unhelpful.
193+
async flash(
194+
dataSource: FlashDataSource,
195+
options: { progress?: (v: number | undefined) => void }
196+
): Promise<void> {
197+
// TODO: deal with the need for richer progress for BLE
198+
const externalProgress = options.progress ?? (() => {});
199+
const progress: Progress = (stage, v) => {
200+
if (
201+
(stage === FlashProgressStage.Partial ||
202+
stage === FlashProgressStage.Full) &&
203+
v !== undefined
204+
) {
205+
externalProgress(v);
206+
}
207+
};
208+
209+
// We'll disconnect/reconnect multiple times due to device resets, but reporting this is unhelpful.
195210
this.deferStatusUpdates = true;
196211
try {
197-
const progress = () => {
198-
// TODO!
199-
};
200212
if (this.status !== ConnectionStatus.CONNECTED) {
201213
const status = await this.connect();
202214
if (status !== ConnectionStatus.CONNECTED) {

src/device/capacitor-ble/nordic-dfu.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ export async function flashDfu(
9191
({ state, data }) => {
9292
switch (state) {
9393
case DfuState.DFU_COMPLETED: {
94+
progress(FlashProgressStage.Full, 1);
9495
resolve(FlashResult.Success);
9596
break;
9697
}
@@ -99,7 +100,9 @@ export async function flashDfu(
99100
break;
100101
}
101102
case DfuState.DFU_PROGRESS: {
102-
progress(FlashProgressStage.Full, data.percent);
103+
if (typeof data.percent === "number") {
104+
progress(FlashProgressStage.Full, data.percent / 100);
105+
}
103106
break;
104107
}
105108
case DfuState.DFU_FAILED: {

src/hooks/download-hooks.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { Settings } from "../settings";
2727
import { useSettings, useStore } from "../store";
2828
import { downloadHex } from "../utils/fs-util";
2929

30+
// TODO: extract this for consistency with connection actions
3031
export class DownloadProjectActions {
3132
private flashingProgressCallback: (value: number) => void;
3233
constructor(
@@ -58,7 +59,7 @@ export class DownloadProjectActions {
5859
(this.state.connection.status === DeviceConnectionStatus.CONNECTED ||
5960
isNativeBluetoothConnection(this.state.connection))
6061
) {
61-
// Reuse connection from last time.
62+
// Reuse connection from last time if still connected or native Bluetooth.
6263
const newState: DownloadState = {
6364
...this.state,
6465
step: DownloadStep.FlashingInProgress,

0 commit comments

Comments
 (0)