Skip to content

Commit

Permalink
Fix hard-resets
Browse files Browse the repository at this point in the history
Signed-off-by: paulober <[email protected]>
  • Loading branch information
paulober committed Nov 17, 2024
1 parent 7c53ea4 commit 00e7956
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
43 changes: 42 additions & 1 deletion src/picoMpyCom.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ export class PicoMpyCom extends EventEmitter {
private resetResolve?: (
value: OperationResult | PromiseLike<OperationResult>
) => 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
Expand Down Expand Up @@ -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
})
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 0 additions & 4 deletions src/tests/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -848,9 +848,6 @@ async function handleCommand(command: string): Promise<void> {
{
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(
Expand Down Expand Up @@ -921,7 +918,6 @@ handleCommand("help")

return;
} else if (interupOnInput) {
interupOnInput = false;
serialCom.interruptExecution();

return;
Expand Down

0 comments on commit 00e7956

Please sign in to comment.