Skip to content

Commit 00e7956

Browse files
committed
Fix hard-resets
Signed-off-by: paulober <44974737+paulober@users.noreply.github.com>
1 parent 7c53ea4 commit 00e7956

File tree

2 files changed

+42
-5
lines changed

2 files changed

+42
-5
lines changed

src/picoMpyCom.ts

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,9 @@ export class PicoMpyCom extends EventEmitter {
3535
private resetResolve?: (
3636
value: OperationResult | PromiseLike<OperationResult>
3737
) => void;
38+
// flag to indicate if main.py exists on the board
39+
// so the follow op can account for it
40+
private mainPyExists = false;
3841

3942
private constructor() {
4043
// TODO: maybe set option to auto capture rejections
@@ -118,7 +121,13 @@ export class PicoMpyCom extends EventEmitter {
118121
}
119122
};
120123
this.once(PicoSerialEvents.interrupt, onInter);
121-
readUntil(this.serialPort, 2, ">OK", null, this.followReset)
124+
readUntil(
125+
this.serialPort,
126+
2,
127+
this.mainPyExists ? "\n>>> " : ">OK",
128+
null,
129+
this.followReset
130+
)
122131
.catch(() => {
123132
// Do nothing
124133
})
@@ -288,6 +297,38 @@ export class PicoMpyCom extends EventEmitter {
288297
this.resetInProgress = true;
289298
this.followReset = receiver;
290299
this.resetResolve = resolve;
300+
301+
// check if main.py is present
302+
const result = await executeAnyCommand(
303+
this.serialPort,
304+
this,
305+
{
306+
type: CommandType.getItemStat,
307+
args: { item: "main.py" },
308+
},
309+
undefined,
310+
pythonInterpreterPath,
311+
undefined
312+
);
313+
314+
if (result.type !== OperationResultType.getItemStat) {
315+
// reset state
316+
this.resetInProgress = false;
317+
this.followReset = undefined;
318+
this.resetResolve = undefined;
319+
320+
// continue processing of the queue
321+
this.executeNextOperation();
322+
323+
resolve({
324+
type: OperationResultType.commandResult,
325+
result: false,
326+
});
327+
328+
return;
329+
}
330+
331+
this.mainPyExists = result.stat !== null && !result.stat.isDir;
291332
}
292333

293334
readyStateCb?.(true);

src/tests/index.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -848,9 +848,6 @@ async function handleCommand(command: string): Promise<void> {
848848
{
849849
rl.question("Do you want to follow hard reset? (y/n): ", answer => {
850850
const follow = answer.trim().toLowerCase() === "y";
851-
if (!follow) {
852-
interupOnInput = true;
853-
}
854851
rl.pause();
855852
serialCom
856853
.hardReset(
@@ -921,7 +918,6 @@ handleCommand("help")
921918

922919
return;
923920
} else if (interupOnInput) {
924-
interupOnInput = false;
925921
serialCom.interruptExecution();
926922

927923
return;

0 commit comments

Comments
 (0)