Skip to content

Commit

Permalink
Cancel running program prior to attempting to run teardown executers
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianPugh committed Feb 15, 2023
1 parent e9e66d1 commit 81cd5f6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 1 deletion.
2 changes: 2 additions & 0 deletions belay/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,8 @@ def close(self) -> None:
# Invoke all teardown executers prior to closing out connection.
atexit.unregister(self.close)

self._board.cancel_running_program()

for executer in _sort_executers(self._belay_teardown._belay_executers):
executer()

Expand Down
6 changes: 5 additions & 1 deletion belay/pyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -402,13 +402,17 @@ def read_until(self, min_num_bytes, ending, timeout=10, data_consumer=None):
time.sleep(0.01)
return data

def cancel_running_program(self):
"""Interrupts any running program."""
self.serial.write(b"\r\x03\x03") # ctrl-C twice: interrupt any running program

def enter_raw_repl(self, soft_reset=True):
# flush input (without relying on serial.flushInput())
n = self.serial.inWaiting()
while n > 0:
self.serial.read(n)
n = self.serial.inWaiting()
self.serial.write(b"\r\x03\x03") # ctrl-C twice: interrupt any running program
self.cancel_running_program()
self.exit_raw_repl() # if device is already in raw_repl, b'>>>' won't be printed.
self.read_until(1, b">>>")
self.serial.write(b"\r\x01") # ctrl-A: enter raw REPL
Expand Down

0 comments on commit 81cd5f6

Please sign in to comment.