From 00e795679cc9068c237748a1a4925308b7206688 Mon Sep 17 00:00:00 2001 From: paulober <44974737+paulober@users.noreply.github.com> Date: Sun, 17 Nov 2024 02:36:13 +0100 Subject: [PATCH] Fix hard-resets Signed-off-by: paulober <44974737+paulober@users.noreply.github.com> --- src/picoMpyCom.ts | 43 ++++++++++++++++++++++++++++++++++++++++++- src/tests/index.ts | 4 ---- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/src/picoMpyCom.ts b/src/picoMpyCom.ts index 65a6aa9..d1ed05a 100644 --- a/src/picoMpyCom.ts +++ b/src/picoMpyCom.ts @@ -35,6 +35,9 @@ export class PicoMpyCom extends EventEmitter { private resetResolve?: ( value: OperationResult | PromiseLike ) => void; + // flag to indicate if main.py exists on the board + // so the follow op can account for it + private mainPyExists = false; private constructor() { // TODO: maybe set option to auto capture rejections @@ -118,7 +121,13 @@ export class PicoMpyCom extends EventEmitter { } }; this.once(PicoSerialEvents.interrupt, onInter); - readUntil(this.serialPort, 2, ">OK", null, this.followReset) + readUntil( + this.serialPort, + 2, + this.mainPyExists ? "\n>>> " : ">OK", + null, + this.followReset + ) .catch(() => { // Do nothing }) @@ -288,6 +297,38 @@ export class PicoMpyCom extends EventEmitter { this.resetInProgress = true; this.followReset = receiver; this.resetResolve = resolve; + + // check if main.py is present + const result = await executeAnyCommand( + this.serialPort, + this, + { + type: CommandType.getItemStat, + args: { item: "main.py" }, + }, + undefined, + pythonInterpreterPath, + undefined + ); + + if (result.type !== OperationResultType.getItemStat) { + // reset state + this.resetInProgress = false; + this.followReset = undefined; + this.resetResolve = undefined; + + // continue processing of the queue + this.executeNextOperation(); + + resolve({ + type: OperationResultType.commandResult, + result: false, + }); + + return; + } + + this.mainPyExists = result.stat !== null && !result.stat.isDir; } readyStateCb?.(true); diff --git a/src/tests/index.ts b/src/tests/index.ts index ef764ef..6f975d7 100644 --- a/src/tests/index.ts +++ b/src/tests/index.ts @@ -848,9 +848,6 @@ async function handleCommand(command: string): Promise { { rl.question("Do you want to follow hard reset? (y/n): ", answer => { const follow = answer.trim().toLowerCase() === "y"; - if (!follow) { - interupOnInput = true; - } rl.pause(); serialCom .hardReset( @@ -921,7 +918,6 @@ handleCommand("help") return; } else if (interupOnInput) { - interupOnInput = false; serialCom.interruptExecution(); return;