Skip to content

Commit

Permalink
Upgrade connection lib, fix for API change
Browse files Browse the repository at this point in the history
  • Loading branch information
microbit-matt-hillsdon committed Jul 18, 2024
1 parent b2fe2ae commit 5042497
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 40 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@codemirror/view": "^6.26.3",
"@emotion/react": "^11.11.4",
"@emotion/styled": "^11.11.5",
"@microbit/microbit-connection": "^0.0.0-alpha.3",
"@microbit/microbit-connection": "^0.0.0-alpha.4",
"@microbit/microbit-fs": "^0.9.2",
"@sanity/block-content-to-react": "^3.0.0",
"@sanity/image-url": "^1.0.1",
Expand Down
16 changes: 8 additions & 8 deletions src/fs/fs.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import * as fs from "fs";
import * as fsp from "fs/promises";
import { NullLogging } from "../deployment/default/logging";
import { BoardId } from "@microbit/microbit-connection";
import { MicroPythonSource } from "../micropython/micropython";
import {
diff,
FileSystem,
Expand All @@ -20,7 +20,6 @@ import {
} from "./fs";
import { DefaultHost } from "./host";
import { defaultInitialProject } from "./initial-project";
import { MicroPythonSource } from "../micropython/micropython";

const hexes = Promise.all([
fs.readFileSync("src/micropython/microbit-micropython-v1.hex", {
Expand All @@ -39,7 +38,7 @@ const fsMicroPythonSource: MicroPythonSource = async () => {
hex: v1,
},
{
boardId: 0x9003,
boardId: 0x9903,
hex: v2,
},
];
Expand Down Expand Up @@ -218,11 +217,12 @@ describe("Filesystem", () => {
expect(typeof data).toEqual("string");
});

it("creates board-specific data for flashing", async () => {
const boardId = BoardId.parse("9900");
const partial = await ufs.partialFlashData(boardId);
const full = await ufs.fullFlashData(boardId);
expect(partial.length).toBeLessThan(full.length);
it("creates board-specific data for flashing V1", async () => {
await ufs.asFlashDataSource()("V1");
});

it("creates board-specific data for flashing V2", async () => {
await ufs.asFlashDataSource()("V2");
});

it("gives useful stats", async () => {
Expand Down
33 changes: 12 additions & 21 deletions src/fs/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ import sortBy from "lodash.sortby";
import { lineNumFromUint8Array } from "../common/text-util";
import {
BoardId,
FlashDataSource,
FlashDataError,
BoardVersion,
} from "@microbit/microbit-connection";
import { Logging } from "../logging/logging";
import { MicroPythonSource } from "../micropython/micropython";
Expand Down Expand Up @@ -170,10 +170,7 @@ export const isNameLengthValid = (filename: string): boolean =>
* or fire any events. This plays well with uncontrolled embeddings of
* third-party text editors.
*/
export class FileSystem
extends TypedEventTarget<EventMap>
implements FlashDataSource
{
export class FileSystem extends TypedEventTarget<EventMap> {
private initializing: Promise<void> | undefined;
private storage: FSStorage;
private fileVersions: Map<string, number> = new Map();
Expand Down Expand Up @@ -463,22 +460,16 @@ export class FileSystem
return this.storage.clearDirty();
}

async fullFlashData(boardId: BoardId): Promise<string> {
try {
const fs = await this.initialize();
return fs.getIntelHex(boardId.normalize().id);
} catch (e: any) {
throw new FlashDataError(e.message);
}
}

async partialFlashData(boardId: BoardId): Promise<Uint8Array> {
try {
const fs = await this.initialize();
return fs.getIntelHexBytes(boardId.normalize().id);
} catch (e: any) {
throw new FlashDataError(e.message);
}
asFlashDataSource() {
return async (boardVersion: BoardVersion) => {
try {
const fs = await this.initialize();
const boardId = BoardId.forVersion(boardVersion).id;
return fs.getIntelHex(boardId);
} catch (e: any) {
throw new FlashDataError(e.message);
}
};
}

async files(): Promise<Record<string, Uint8Array>> {
Expand Down
11 changes: 5 additions & 6 deletions src/project/project-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,7 @@ export class ProjectActions {
this.webusbNotSupportedError(finalFocusRef);
} else {
if (await this.showConnectHelp(forceConnectHelp, finalFocusRef)) {
return this.connectInternal(
{ serial: userAction !== ConnectionAction.FLASH },
userAction,
finalFocusRef
);
return this.connectInternal({}, userAction, finalFocusRef);
}
}
};
Expand Down Expand Up @@ -543,7 +539,10 @@ export class ProjectActions {
progress: value,
});
};
await this.device.flash(this.fs, { partial: true, progress });
await this.device.flash(this.fs.asFlashDataSource(), {
partial: true,
progress,
});
} catch (e) {
if (e instanceof FlashDataError) {
this.actionFeedback.expectedError({
Expand Down

0 comments on commit 5042497

Please sign in to comment.