Skip to content

Commit

Permalink
Merge pull request #107 from BrianPugh/ctrl-c-on-close
Browse files Browse the repository at this point in the history
Ctrl c on close
  • Loading branch information
BrianPugh authored Feb 16, 2023
2 parents 5139497 + e33369d commit c558220
Show file tree
Hide file tree
Showing 6 changed files with 295 additions and 141 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ on:

jobs:
test:
timeout-minutes: 40
timeout-minutes: 45
defaults:
run:
shell: bash
Expand All @@ -31,7 +31,7 @@ jobs:
env:
OS: ${{ matrix.os }}
PYTHON: ${{ matrix.python-version }}
BELAY_SLEEP_MULTIPLIER: 3.0
BELAY_SLEEP_MULTIPLIER: 2.5

steps:
- name: Set OS Environment Variables (Windows)
Expand Down
9 changes: 8 additions & 1 deletion belay/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -650,12 +650,19 @@ def __exit__(self, exc_type, exc_value, exc_tb):
def close(self) -> None:
"""Close the connection to device."""
# Invoke all teardown executers prior to closing out connection.
if self._board is None:
# Has already been closed
return

atexit.unregister(self.close)

self._board.cancel_running_program()

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

return self._board.close()
self._board.close()
self._board = None

def reconnect(self, attempts: Optional[int] = None) -> None:
"""Reconnect to the device and replay the command history.
Expand Down
2 changes: 1 addition & 1 deletion belay/packagemanager/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _dependencies_name_validator(dependencies) -> dict:
return dependencies


def _dependencies_preprocessor(dependencies) -> dict[str, List[dict]]:
def _dependencies_preprocessor(dependencies) -> Dict[str, List[dict]]:
"""Preprocess various dependencies based on dtype.
* ``str`` -> single dependency that may get renamed to __init__.py, if appropriate.
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
Loading

0 comments on commit c558220

Please sign in to comment.