From 33e6defbd1d1a811c1e534c2a0e8c4c6d653e176 Mon Sep 17 00:00:00 2001 From: Robert Knight Date: Thu, 15 Dec 2022 16:53:41 +0000 Subject: [PATCH] Demo opening the hex in the micro:bit app --- src/fs/fs.ts | 7 +++++++ src/project/SendButton.tsx | 10 +++++++--- src/project/project-actions.tsx | 8 ++++++++ 3 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/fs/fs.ts b/src/fs/fs.ts index 2a109d74f..4e8c3352f 100644 --- a/src/fs/fs.ts +++ b/src/fs/fs.ts @@ -436,6 +436,13 @@ export class FileSystem extends EventEmitter implements FlashDataSource { return fs.getUniversalHex(); } + async toHexURI(): Promise { + const fs = await this.initialize(); + const universalHex = fs.getUniversalHex(); + const b64EncodedHex = btoa(universalHex); + return `microbithex://?data:microbit-${this.project.name}.hex;base64,${b64EncodedHex}`; + } + async clearDirty(): Promise { this._dirty = false; return this.storage.clearDirty(); diff --git a/src/project/SendButton.tsx b/src/project/SendButton.tsx index a0a8f83db..c69406685 100644 --- a/src/project/SendButton.tsx +++ b/src/project/SendButton.tsx @@ -48,7 +48,11 @@ const SendButton = React.forwardRef( flashing: false, lastCompleteFlash: 0, }); - const handleSendToMicrobit = useCallback(async () => { + const handleOpenInMicrobitApp = useCallback(async () => { + await actions.openInMicrobitApp(); + }, [actions]); + // Temporarily commented out for demo purposes. + /* const handleSendToMicrobit = useCallback(async () => { if (flashing.current.flashing) { // Ignore repeated clicks. return; @@ -65,7 +69,7 @@ const SendButton = React.forwardRef( lastCompleteFlash: new Date().getTime(), }; } - }, [flashing, actions, sendButtonRef]); + }, [flashing, actions, sendButtonRef]); */ const handleFocus = useCallback( (e) => { const inProgress = flashing.current.flashing; @@ -96,7 +100,7 @@ const SendButton = React.forwardRef( size={size} variant="solid" leftIcon={} - onClick={handleSendToMicrobit} + onClick={handleOpenInMicrobitApp} > diff --git a/src/project/project-actions.tsx b/src/project/project-actions.tsx index e4b2fb75d..f729a8653 100644 --- a/src/project/project-actions.tsx +++ b/src/project/project-actions.tsx @@ -544,6 +544,14 @@ export class ProjectActions { } }; + /** + * Open hex file as base64 in the micro:bit app. + */ + openInMicrobitApp = async () => { + const dataURI = await this.fs.toHexURI(); + window.open(dataURI, "_self"); + }; + /** * Trigger a browser download with a universal hex file. */