Skip to content
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

[DEMO - DO NOT MERGE] Demo opening hex directly in the micro:bit app #1083

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
7 changes: 7 additions & 0 deletions src/fs/fs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,13 @@ export class FileSystem extends EventEmitter implements FlashDataSource {
return fs.getUniversalHex();
}

async toHexURI(): Promise<string> {
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<void> {
this._dirty = false;
return this.storage.clearDirty();
Expand Down
10 changes: 7 additions & 3 deletions src/project/SendButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -96,7 +100,7 @@ const SendButton = React.forwardRef(
size={size}
variant="solid"
leftIcon={<RiUsbLine />}
onClick={handleSendToMicrobit}
onClick={handleOpenInMicrobitApp}
>
<FormattedMessage id="send-action" />
</Button>
Expand Down
8 changes: 8 additions & 0 deletions src/project/project-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down