Skip to content

Commit

Permalink
handle simple cases initiated by app side; refresh / download / expli…
Browse files Browse the repository at this point in the history
…cit pair
  • Loading branch information
jwunderl committed Sep 5, 2024
1 parent 4742c98 commit 6a35f4c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
1 change: 1 addition & 0 deletions localtypings/pxtarget.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,7 @@ declare namespace pxt {

dragFileImage?: string;
connectDeviceImage?: string;
disconnectDeviceImage?: string;
selectDeviceImage?: string;
connectionSuccessImage?: string;
incompatibleHardwareImage?: string;
Expand Down
38 changes: 37 additions & 1 deletion webapp/src/cmds.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,35 @@ function showUploadInstructionsAsync(
}).then(() => { });
}

export function showReconnectDeviceInstructionsAsync(
confirmAsync: (options: core.PromptOptions) => Promise<number>
): Promise<void> {
const boardName = pxt.appTarget.appTheme.boardName || lf("device");
const helpUrl = pxt.appTarget.appTheme.usbDocs;
const jsx = webusb.renderDisconnectDeviceDialog();
const body = lf("Your {0} appears to have stalled; please disconnect any battery and usb connection, and try again.", boardName);
return confirmAsync({
header: lf("{0} Connection failed...", boardName),
body,
jsx,
hasCloseIcon: true,
hideAgree: true,
helpUrl,
bigHelpButton: true,
className: 'downloaddialog',
buttons: [
{
label: lf("Done"),
className: "primary",
onclick: () => {
pxt.tickEvent('downloaddialog.done')
core.hideDialog();
}
},
]
}).then(() => { });
}

export function nativeHostPostMessageFunction(): (msg: NativeHostMessage) => void {
const webkit = (<any>window).webkit;
if (webkit
Expand Down Expand Up @@ -261,6 +290,9 @@ export async function hidDeployCoreAsync(resp: pxtc.CompileResult, d?: pxt.comma
// device is locked or used by another tab
pxt.tickEvent("hid.flash.devicelocked");
log(`error: device locked`);
} else if (e.type == "inittimeout") {
pxt.tickEvent("hid.flash.inittimeout");
await showReconnectDeviceInstructionsAsync(core.confirmAsync);
} else {
pxt.tickEvent("hid.flash.error");
log(`hid error ${e.message}`)
Expand Down Expand Up @@ -461,8 +493,12 @@ export async function maybeReconnectAsync(pairIfDeviceNotFound = false, skipIfCo
await wrapper.reconnectAsync();
return true;
} catch (e) {
if (e.type == "devicenotfound")
if (e.type == "devicenotfound") {
return !!pairIfDeviceNotFound && pairAsync();
} else if (e.type == "inittimeout") {
pxt.tickEvent("hid.flash.inittimeout");
await showReconnectDeviceInstructionsAsync(core.confirmAsync);
}
throw e;
}
} finally {
Expand Down
19 changes: 19 additions & 0 deletions webapp/src/webusb.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,25 @@ export function renderUnpairDialog() {
return { header, jsx, helpUrl };
}

export function renderDisconnectDeviceDialog() {
const boardName = getBoardName();
const disconnectImage = theme().disconnectDeviceImage;

return <>
{disconnectImage && <img
className="ui image centered medium"
src={disconnectImage}
alt={lf("Image of {0} being disconnected", boardName)}
/>}
<div>
{lf("Your {0} appears to have stalled", boardName)}
<br />
<br />
{lf("Please disconnect any battery and usb connection, and try again.")}
</div>
</>;
}

export async function showDeviceForgottenDialog(confirmAsync: ConfirmAsync) {
const boardName = getBoardName();
const deviceForgottenImage = theme().usbDeviceForgottenImage;
Expand Down

0 comments on commit 6a35f4c

Please sign in to comment.