Skip to content

Commit

Permalink
Merge pull request #120 from BrianPugh/read-until-always-one
Browse files Browse the repository at this point in the history
read_until now always assumes the minimum number of return bytes is 1
  • Loading branch information
BrianPugh authored Mar 17, 2023
2 parents 890fe42 + 1e037cd commit 122df55
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion belay/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -894,7 +894,7 @@ def soft_reset(self):
# When in Raw REPL, ctrl-d will perform a reset, but won't execute ``main.py``
# https://github.com/micropython/micropython/issues/2249
self._board.exit_raw_repl()
self._board.read_until(1, b">>>")
self._board.read_until(b">>>")
self._board.ctrl_d()

def _traceback_execute(
Expand Down
20 changes: 10 additions & 10 deletions belay/pyboard.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ def close(self):
self.serial = None
atexit.unregister(self.close)

def read_until(self, min_num_bytes, ending, timeout=10, data_consumer=None):
def read_until(self, ending, timeout=10, data_consumer=None):
"""Read bytes until a specified ending pattern is reached.
Parameters
Expand All @@ -372,7 +372,7 @@ def read_until(self, min_num_bytes, ending, timeout=10, data_consumer=None):
Timeout in seconds.
If None, no timeout.
"""
data = self.serial.read(min_num_bytes)
data = self.serial.read(1)
if data_consumer:
data_consumer(data)
timeout_count = 0
Expand Down Expand Up @@ -410,13 +410,13 @@ def enter_raw_repl(self, soft_reset=True):
n = self.serial.in_waiting
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.read_until(b">>>")
self.serial.write(b"\r\x01") # ctrl-A: enter raw REPL
if soft_reset:
self.read_until(1, b"raw REPL; CTRL-B to exit\r\n>")
self.read_until(b"raw REPL; CTRL-B to exit\r\n>")
self.ctrl_d()

self.read_until(1, b"raw REPL; CTRL-B to exit\r\n")
self.read_until(b"raw REPL; CTRL-B to exit\r\n")
self.in_raw_repl = True

def exit_raw_repl(self):
Expand All @@ -425,11 +425,11 @@ def exit_raw_repl(self):

def follow(self, timeout, data_consumer=None):
# wait for normal output (first EOF reception)
data = self.read_until(1, b"\x04", timeout=timeout, data_consumer=data_consumer)
data = self.read_until(b"\x04", timeout=timeout, data_consumer=data_consumer)
data = data[:-1]

# wait for error output
data_err = self.read_until(1, b"\x04", timeout=timeout)
data_err = self.read_until(b"\x04", timeout=timeout)
data_err = data_err[:-1]

# return normal and error output
Expand Down Expand Up @@ -466,7 +466,7 @@ def raw_paste_write(self, command_bytes):
self.serial.write(b"\x04")

# Wait for device to acknowledge end of data.
self.read_until(1, b"\x04")
self.read_until(b"\x04")

def exec_raw_no_follow(self, command):
if isinstance(command, bytes):
Expand All @@ -475,7 +475,7 @@ def exec_raw_no_follow(self, command):
command_bytes = bytes(command, encoding="utf8")

# check we have a prompt
self.read_until(1, b">")
self.read_until(b">")

if self.use_raw_paste:
# Try to enter raw-paste mode.
Expand All @@ -489,7 +489,7 @@ def exec_raw_no_follow(self, command):
return self.raw_paste_write(command_bytes)
else:
# Device doesn't support raw-paste, fall back to normal raw REPL.
self.read_until(1, b"w REPL; CTRL-B to exit\r\n>")
self.read_until(b"w REPL; CTRL-B to exit\r\n>")
# Don't try to use raw-paste mode again for this connection.
self.use_raw_paste = False

Expand Down

0 comments on commit 122df55

Please sign in to comment.