web serial cleanup (#928)

* updated ticks

* fix enable and compile

* add debug mode

* don't try to change UI while downloading

* cleanup

* don't double open

* use cu

* add delay before reconnecting

* support for 200 ragne in field turn ratio

* updated docs

* fix docs
This commit is contained in:
Peli de Halleux
2019-09-30 11:13:28 -07:00
committed by GitHub
parent d85b5c5129
commit 8ca4558fc2
6 changed files with 43 additions and 25 deletions

View File

@ -64,7 +64,6 @@ class WebSerialPackageIO implements pxt.HF2.PacketIO {
private _writer: any;
constructor(private port: SerialPort, private options: SerialOptions) {
this.openAsync();
}
async readSerialAsync() {
@ -173,7 +172,7 @@ export function initAsync(): Promise<void> {
}
if (WebSerialPackageIO.isSupported())
pxt.tickEvent("bluetooth.supported");
pxt.tickEvent("webserial.supported");
return Promise.resolve();
}
@ -274,7 +273,7 @@ export function deployCoreAsync(resp: pxtc.CompileResult) {
if (!useHID) return saveUF2Async()
pxt.tickEvent("bluetooth.flash");
pxt.tickEvent("webserial.flash");
let w: Ev3Wrapper;
return initHidAsync()
.then(w_ => {
@ -291,12 +290,14 @@ export function deployCoreAsync(resp: pxtc.CompileResult) {
`<ul>
<li>${lf("Make sure to stop your program or exit portview on the EV3.")}</li>
<li>${lf("Check your battery level.")}</li>
<li>${lf("Close EV3 LabView or other MakeCode editor tabs.")}
</ul>`,
hasCloseIcon: true,
hideCancel: true,
hideAgree: false,
agreeLbl: lf("Try again"),
}).then(() => w.disconnectAsync())
.then(() => Promise.delay(1000))
.then(() => w.reconnectAsync());
// nothing we can do
@ -309,11 +310,11 @@ export function deployCoreAsync(resp: pxtc.CompileResult) {
.then(() => w.flashAsync(rbfPath, rbfBIN))
.then(() => w.runAsync(rbfPath))
.then(() => {
pxt.tickEvent("bluetooth.success");
pxt.tickEvent("webserial.success");
return w.disconnectAsync()
//return Promise.delay(1000).then(() => w.dmesgAsync())
}).catch(e => {
pxt.tickEvent("bluetooth.fail");
pxt.tickEvent("webserial.fail");
useHID = false;
useWebSerial = false;
// if we failed to initalize, tell the user to retry

View File

@ -7,6 +7,13 @@ let bluetoothDialogShown = false;
pxt.editor.initExtensionsAsync = function (opts: pxt.editor.ExtensionOptions): Promise<pxt.editor.ExtensionResult> {
const projectView = opts.projectView;
pxt.debug('loading pxt-ev3 target extensions...')
function enableWebSerialAndCompileAsync() {
return enableWebSerialAsync()
.then(() => Promise.delay(500))
.then(() => projectView.compile());
}
const res: pxt.editor.ExtensionResult = {
deployCoreAsync,
showUploadInstructionsAsync: (fn: string, url: string, confirmAsync: (options: any) => Promise<number>) => {
@ -84,7 +91,7 @@ pxt.editor.initExtensionsAsync = function (opts: pxt.editor.ExtensionOptions): P
onclick: () => {
pxt.tickEvent("bluetooth.enable");
if (bluetoothDialogShown) {
enableWebSerialAsync().done();
enableWebSerialAndCompileAsync().done();
} else {
bluetoothDialogShown = true;
confirmAsync({
@ -99,13 +106,13 @@ pxt.editor.initExtensionsAsync = function (opts: pxt.editor.ExtensionOptions): P
}],
htmlBody: `<p>
${lf("You will be prompted to select a serial port.")}
${pxt.BrowserUtils.isWindows() ? lf("Look for 'Standard Serial over Bluetooth link'.") : lf("Loop for 'tty.EV3-SerialPort' or 'cu.EV3-SerialPort'")}
${pxt.BrowserUtils.isWindows()
? lf("Look for 'Standard Serial over Bluetooth link'.")
: lf("Loop for 'cu.EV3-SerialPort'.")}
${lf("If you have paired multiple EV3, you might have to try out multiple ports until you find the correct one.")}
</p>
`
}).then(() => enableWebSerialAsync())
.then(() => Promise.delay(500))
.then(() => projectView.compile())
}).then(() => enableWebSerialAndCompileAsync())
}
}
} : undefined, downloadAgain ? {

View File

@ -1,8 +1,12 @@
/**
* See https://www.lego.com/cdn/cs/set/assets/blt6879b00ae6951482/LEGO_MINDSTORMS_EV3_Communication_Developer_Kit.pdf
* https://github.com/mindboards/ev3sources/blob/master/lms2012/lms2012/source/bytecodes.h#L146
*/
import HF2 = pxt.HF2
import U = pxt.U
function log(msg: string) {
pxt.debug("EWRAP: " + msg)
pxt.log("serial: " + msg)
}
export interface DirEntry {
@ -19,7 +23,7 @@ export class Ev3Wrapper {
private cmdSeq = U.randomUint32() & 0xffff;
private lock = new U.PromiseQueue();
isStreaming = false;
dataDump = false;
dataDump = /talkdbg=1/.test(window.location.href);
constructor(public io: pxt.HF2.PacketIO) {
io.onData = buf => {