diff --git a/emdbg.html b/emdbg.html index 615162c..027e38f 100644 --- a/emdbg.html +++ b/emdbg.html @@ -3,7 +3,7 @@ - + emdbg API documentation diff --git a/emdbg/analyze.html b/emdbg/analyze.html index c7a6f5b..fbdd7d0 100644 --- a/emdbg/analyze.html +++ b/emdbg/analyze.html @@ -3,7 +3,7 @@ - + emdbg.analyze API documentation diff --git a/emdbg/analyze/backtrace.html b/emdbg/analyze/backtrace.html index 9c8d02d..7b3bb22 100644 --- a/emdbg/analyze/backtrace.html +++ b/emdbg/analyze/backtrace.html @@ -3,7 +3,7 @@ - + emdbg.analyze.backtrace API documentation diff --git a/emdbg/analyze/callgraph.html b/emdbg/analyze/callgraph.html index a19de75..3c9ebef 100644 --- a/emdbg/analyze/callgraph.html +++ b/emdbg/analyze/callgraph.html @@ -3,7 +3,7 @@ - + emdbg.analyze.callgraph API documentation diff --git a/emdbg/analyze/calltrace.html b/emdbg/analyze/calltrace.html index 6945d11..78b5416 100644 --- a/emdbg/analyze/calltrace.html +++ b/emdbg/analyze/calltrace.html @@ -3,7 +3,7 @@ - + emdbg.analyze.calltrace API documentation diff --git a/emdbg/analyze/hardfault.html b/emdbg/analyze/hardfault.html index 2ca574a..35f8f78 100644 --- a/emdbg/analyze/hardfault.html +++ b/emdbg/analyze/hardfault.html @@ -3,7 +3,7 @@ - + emdbg.analyze.hardfault API documentation diff --git a/emdbg/analyze/priority.html b/emdbg/analyze/priority.html index 87204fa..491bdca 100644 --- a/emdbg/analyze/priority.html +++ b/emdbg/analyze/priority.html @@ -3,7 +3,7 @@ - + emdbg.analyze.priority API documentation diff --git a/emdbg/analyze/utils.html b/emdbg/analyze/utils.html index fec7c33..77ca8ef 100644 --- a/emdbg/analyze/utils.html +++ b/emdbg/analyze/utils.html @@ -3,7 +3,7 @@ - + emdbg.analyze.utils API documentation diff --git a/emdbg/bench.html b/emdbg/bench.html index 035604c..08bf703 100644 --- a/emdbg/bench.html +++ b/emdbg/bench.html @@ -3,7 +3,7 @@ - + emdbg.bench API documentation diff --git a/emdbg/bench/fmu.html b/emdbg/bench/fmu.html index 9c85b85..7988cb9 100644 --- a/emdbg/bench/fmu.html +++ b/emdbg/bench/fmu.html @@ -3,7 +3,7 @@ - + emdbg.bench.fmu API documentation diff --git a/emdbg/bench/skynode.html b/emdbg/bench/skynode.html index c96e3c6..fe3c6e8 100644 --- a/emdbg/bench/skynode.html +++ b/emdbg/bench/skynode.html @@ -3,7 +3,7 @@ - + emdbg.bench.skynode API documentation diff --git a/emdbg/debug.html b/emdbg/debug.html index 322aeab..600956b 100644 --- a/emdbg/debug.html +++ b/emdbg/debug.html @@ -3,7 +3,7 @@ - + emdbg.debug API documentation diff --git a/emdbg/debug/backend.html b/emdbg/debug/backend.html index c9985bc..722f313 100644 --- a/emdbg/debug/backend.html +++ b/emdbg/debug/backend.html @@ -3,7 +3,7 @@ - + emdbg.debug.backend API documentation diff --git a/emdbg/debug/crashdebug.html b/emdbg/debug/crashdebug.html index 38762af..1802eb8 100644 --- a/emdbg/debug/crashdebug.html +++ b/emdbg/debug/crashdebug.html @@ -3,7 +3,7 @@ - + emdbg.debug.crashdebug API documentation diff --git a/emdbg/debug/gdb.html b/emdbg/debug/gdb.html index 44c5792..4a909a1 100644 --- a/emdbg/debug/gdb.html +++ b/emdbg/debug/gdb.html @@ -3,7 +3,7 @@ - + emdbg.debug.gdb API documentation diff --git a/emdbg/debug/jlink.html b/emdbg/debug/jlink.html index cb1c40e..6745a43 100644 --- a/emdbg/debug/jlink.html +++ b/emdbg/debug/jlink.html @@ -3,7 +3,7 @@ - + emdbg.debug.jlink API documentation diff --git a/emdbg/debug/openocd.html b/emdbg/debug/openocd.html index 8e9b796..190fb12 100644 --- a/emdbg/debug/openocd.html +++ b/emdbg/debug/openocd.html @@ -3,7 +3,7 @@ - + emdbg.debug.openocd API documentation diff --git a/emdbg/debug/px4.html b/emdbg/debug/px4.html index 6770d35..b3abbb7 100644 --- a/emdbg/debug/px4.html +++ b/emdbg/debug/px4.html @@ -3,7 +3,7 @@ - + emdbg.debug.px4 API documentation diff --git a/emdbg/debug/px4/base.html b/emdbg/debug/px4/base.html index 5a8abb0..00e9d1d 100644 --- a/emdbg/debug/px4/base.html +++ b/emdbg/debug/px4/base.html @@ -3,7 +3,7 @@ - + emdbg.debug.px4.base API documentation diff --git a/emdbg/debug/px4/buffer.html b/emdbg/debug/px4/buffer.html index feb42b9..6a531ec 100644 --- a/emdbg/debug/px4/buffer.html +++ b/emdbg/debug/px4/buffer.html @@ -3,7 +3,7 @@ - + emdbg.debug.px4.buffer API documentation diff --git a/emdbg/debug/px4/data.html b/emdbg/debug/px4/data.html index a6b7ab2..5f171b5 100644 --- a/emdbg/debug/px4/data.html +++ b/emdbg/debug/px4/data.html @@ -3,7 +3,7 @@ - + emdbg.debug.px4.data API documentation diff --git a/emdbg/debug/px4/device.html b/emdbg/debug/px4/device.html index 74506d0..b62c937 100644 --- a/emdbg/debug/px4/device.html +++ b/emdbg/debug/px4/device.html @@ -3,7 +3,7 @@ - + emdbg.debug.px4.device API documentation diff --git a/emdbg/debug/px4/perf.html b/emdbg/debug/px4/perf.html index ae6fe71..38c8dcb 100644 --- a/emdbg/debug/px4/perf.html +++ b/emdbg/debug/px4/perf.html @@ -3,7 +3,7 @@ - + emdbg.debug.px4.perf API documentation diff --git a/emdbg/debug/px4/semaphore.html b/emdbg/debug/px4/semaphore.html index d66126d..cdde92d 100644 --- a/emdbg/debug/px4/semaphore.html +++ b/emdbg/debug/px4/semaphore.html @@ -3,7 +3,7 @@ - + emdbg.debug.px4.semaphore API documentation diff --git a/emdbg/debug/px4/svd.html b/emdbg/debug/px4/svd.html index 7696849..f8a9b1e 100644 --- a/emdbg/debug/px4/svd.html +++ b/emdbg/debug/px4/svd.html @@ -3,7 +3,7 @@ - + emdbg.debug.px4.svd API documentation diff --git a/emdbg/debug/px4/system_load.html b/emdbg/debug/px4/system_load.html index 5fd7d1d..c3d5af9 100644 --- a/emdbg/debug/px4/system_load.html +++ b/emdbg/debug/px4/system_load.html @@ -3,7 +3,7 @@ - + emdbg.debug.px4.system_load API documentation diff --git a/emdbg/debug/px4/task.html b/emdbg/debug/px4/task.html index 5bcfbce..79f0c18 100644 --- a/emdbg/debug/px4/task.html +++ b/emdbg/debug/px4/task.html @@ -3,7 +3,7 @@ - + emdbg.debug.px4.task API documentation diff --git a/emdbg/debug/px4/utils.html b/emdbg/debug/px4/utils.html index d0618d6..49c4695 100644 --- a/emdbg/debug/px4/utils.html +++ b/emdbg/debug/px4/utils.html @@ -3,7 +3,7 @@ - + emdbg.debug.px4.utils API documentation diff --git a/emdbg/debug/remote.html b/emdbg/debug/remote.html index e76734f..94bbf28 100644 --- a/emdbg/debug/remote.html +++ b/emdbg/debug/remote.html @@ -3,7 +3,7 @@ - + emdbg.debug.remote API documentation diff --git a/emdbg/debug/remote/gdb.html b/emdbg/debug/remote/gdb.html index fd0730b..7677970 100644 --- a/emdbg/debug/remote/gdb.html +++ b/emdbg/debug/remote/gdb.html @@ -3,7 +3,7 @@ - + emdbg.debug.remote.gdb API documentation diff --git a/emdbg/debug/remote/mi.html b/emdbg/debug/remote/mi.html index 6540f61..36e1935 100644 --- a/emdbg/debug/remote/mi.html +++ b/emdbg/debug/remote/mi.html @@ -3,7 +3,7 @@ - + emdbg.debug.remote.mi API documentation diff --git a/emdbg/debug/remote/rpyc.html b/emdbg/debug/remote/rpyc.html index 4440d1e..2301a98 100644 --- a/emdbg/debug/remote/rpyc.html +++ b/emdbg/debug/remote/rpyc.html @@ -3,7 +3,7 @@ - + emdbg.debug.remote.rpyc API documentation diff --git a/emdbg/debug/utils.html b/emdbg/debug/utils.html index 5a19578..804c667 100644 --- a/emdbg/debug/utils.html +++ b/emdbg/debug/utils.html @@ -3,7 +3,7 @@ - + emdbg.debug.utils API documentation diff --git a/emdbg/io.html b/emdbg/io.html index 40ab63a..c6d9c57 100644 --- a/emdbg/io.html +++ b/emdbg/io.html @@ -3,7 +3,7 @@ - + emdbg.io API documentation diff --git a/emdbg/io/digilent.html b/emdbg/io/digilent.html index cd0ef53..97d4d5a 100644 --- a/emdbg/io/digilent.html +++ b/emdbg/io/digilent.html @@ -3,7 +3,7 @@ - + emdbg.io.digilent API documentation diff --git a/emdbg/logger.html b/emdbg/logger.html index ce387f3..5b869d9 100644 --- a/emdbg/logger.html +++ b/emdbg/logger.html @@ -3,7 +3,7 @@ - + emdbg.logger API documentation diff --git a/emdbg/patch.html b/emdbg/patch.html index a45ca4d..e041e15 100644 --- a/emdbg/patch.html +++ b/emdbg/patch.html @@ -3,7 +3,7 @@ - + emdbg.patch API documentation diff --git a/emdbg/patch/operation.html b/emdbg/patch/operation.html index 73aefa0..b22323f 100644 --- a/emdbg/patch/operation.html +++ b/emdbg/patch/operation.html @@ -3,7 +3,7 @@ - + emdbg.patch.operation API documentation diff --git a/emdbg/patch/set.html b/emdbg/patch/set.html index 42edcbe..18d6130 100644 --- a/emdbg/patch/set.html +++ b/emdbg/patch/set.html @@ -3,7 +3,7 @@ - + emdbg.patch.set API documentation diff --git a/emdbg/power.html b/emdbg/power.html index 9db4172..ab6a854 100644 --- a/emdbg/power.html +++ b/emdbg/power.html @@ -3,7 +3,7 @@ - + emdbg.power API documentation diff --git a/emdbg/power/base.html b/emdbg/power/base.html index ba294b9..35e88a1 100644 --- a/emdbg/power/base.html +++ b/emdbg/power/base.html @@ -3,7 +3,7 @@ - + emdbg.power.base API documentation diff --git a/emdbg/power/yocto.html b/emdbg/power/yocto.html index 5aed67c..9a297fe 100644 --- a/emdbg/power/yocto.html +++ b/emdbg/power/yocto.html @@ -3,7 +3,7 @@ - + emdbg.power.yocto API documentation diff --git a/emdbg/serial.html b/emdbg/serial.html index e06a4ea..96387a4 100644 --- a/emdbg/serial.html +++ b/emdbg/serial.html @@ -3,7 +3,7 @@ - + emdbg.serial API documentation diff --git a/emdbg/serial/protocol.html b/emdbg/serial/protocol.html index 7b461ad..baa3291 100644 --- a/emdbg/serial/protocol.html +++ b/emdbg/serial/protocol.html @@ -3,7 +3,7 @@ - + emdbg.serial.protocol API documentation @@ -276,162 +276,161 @@

165 166 :return: received lines until matched pattern or None on timeout 167 """ -168 lines = [] +168 lines = "" 169 start = time.time() 170 while True: 171 if time.time() - start > timeout: 172 break -173 new_lines = self.read_lines(0) -174 lines.append(new_lines) -175 for line in new_lines: -176 if re.search(pattern, line): -177 return self._join(lines) -178 time.sleep(0.1) -179 _LOGGER.warning(f"Waiting for '{pattern}' timed out after {timeout:.1f}s!") -180 return None -181 -182 def wait_for_prompt(self, timeout: float = _TIMEOUT) -> list[str]: -183 """ -184 Waits to the `nsh> ` prompt to arrive in the stream. -185 Note that any ANSI escape codes (for color or cursor position) are -186 filtered out. -187 -188 :param timeout: seconds to wait until the prompt arrives. -189 -190 :return: all lines until the prompt arrives. -191 """ -192 if prompts := self._read_packets(Nsh._NEWLINE + Nsh._PROMPT, timeout): -193 prompt = Nsh._PROMPT + Nsh._PROMPT.join(prompts) -194 return self._join(self._filter(prompt.split(Nsh._NEWLINE))) -195 _LOGGER.warning(f"Waiting for 'nsh> ' prompt timed out after {timeout:.1f}s!") -196 return None +173 if (new_lines := self.read_lines(0)) is not None: +174 lines += new_lines +175 if re.search(pattern, new_lines): +176 return lines +177 time.sleep(0.1) +178 _LOGGER.warning(f"Waiting for '{pattern}' timed out after {timeout:.1f}s!") +179 return None +180 +181 def wait_for_prompt(self, timeout: float = _TIMEOUT) -> list[str]: +182 """ +183 Waits to the `nsh> ` prompt to arrive in the stream. +184 Note that any ANSI escape codes (for color or cursor position) are +185 filtered out. +186 +187 :param timeout: seconds to wait until the prompt arrives. +188 +189 :return: all lines until the prompt arrives. +190 """ +191 if prompts := self._read_packets(Nsh._NEWLINE + Nsh._PROMPT, timeout): +192 prompt = Nsh._PROMPT + Nsh._PROMPT.join(prompts) +193 return self._join(self._filter(prompt.split(Nsh._NEWLINE))) +194 _LOGGER.warning(f"Waiting for 'nsh> ' prompt timed out after {timeout:.1f}s!") +195 return None +196 197 -198 -199 def command(self, command: str, timeout: float = _TIMEOUT) -> str | None: -200 """ -201 Send a command and return all lines until the next prompt. -202 If the command is asynchronous, you need to poll for new lines separately. -203 Note that any ANSI escape codes (for color or cursor position) are -204 filtered out. -205 -206 :param command: command string to send to the NSH. -207 :param timeout: seconds to wait until the prompt arrives. -208 -209 :return: all lines from the command issue until the next prompt arrives. -210 """ -211 self._serial.clear() -212 self._write_line(command) -213 if timeout is not None: -214 return self.wait_for_prompt(timeout) -215 -216 def command_nowait(self, command: str): -217 """ -218 Send a command to the NSH without waiting for a response. -219 -220 :param command: command string to send to the NSH. -221 """ -222 self.command(command, None) +198 def command(self, command: str, timeout: float = _TIMEOUT) -> str | None: +199 """ +200 Send a command and return all lines until the next prompt. +201 If the command is asynchronous, you need to poll for new lines separately. +202 Note that any ANSI escape codes (for color or cursor position) are +203 filtered out. +204 +205 :param command: command string to send to the NSH. +206 :param timeout: seconds to wait until the prompt arrives. +207 +208 :return: all lines from the command issue until the next prompt arrives. +209 """ +210 self._serial.clear() +211 self._write_line(command) +212 if timeout is not None: +213 return self.wait_for_prompt(timeout) +214 +215 def command_nowait(self, command: str): +216 """ +217 Send a command to the NSH without waiting for a response. +218 +219 :param command: command string to send to the NSH. +220 """ +221 self.command(command, None) +222 223 -224 -225 def reboot(self, timeout: int = 15) -> str | None: -226 """ -227 Send the reboot command and wait for the reboot to be completed. -228 -229 :param timeout: seconds to wait until the prompt arrives. -230 -231 :return: all lines from reboot until the next prompt. -232 """ -233 return self.command("reboot", timeout) -234 -235 def is_alive(self, timeout: float = _TIMEOUT, attempts: int = 4) -> bool: -236 """ -237 Check if the NSH is responding to newline inputs with a `nsh> ` prompt. -238 The total timeout is `attempts * timeout`! -239 -240 :param timeout: seconds to wait until the prompt arrives. -241 :param attempts: number of times to send a newline and wait. -242 :return: `True` is NSH responds, `False` otherwise -243 """ -244 self._serial.clear() -245 attempt = 0 -246 timeout = timeout / attempts -247 while attempt < attempts: -248 self._write_line("") -249 if self.wait_for_prompt(timeout) is not None: -250 return True -251 attempt += 1 -252 return False +224 def reboot(self, timeout: int = 15) -> str | None: +225 """ +226 Send the reboot command and wait for the reboot to be completed. +227 +228 :param timeout: seconds to wait until the prompt arrives. +229 +230 :return: all lines from reboot until the next prompt. +231 """ +232 return self.command("reboot", timeout) +233 +234 def is_alive(self, timeout: float = _TIMEOUT, attempts: int = 4) -> bool: +235 """ +236 Check if the NSH is responding to newline inputs with a `nsh> ` prompt. +237 The total timeout is `attempts * timeout`! +238 +239 :param timeout: seconds to wait until the prompt arrives. +240 :param attempts: number of times to send a newline and wait. +241 :return: `True` is NSH responds, `False` otherwise +242 """ +243 self._serial.clear() +244 attempt = 0 +245 timeout = timeout / attempts +246 while attempt < attempts: +247 self._write_line("") +248 if self.wait_for_prompt(timeout) is not None: +249 return True +250 attempt += 1 +251 return False +252 253 -254 -255# ----------------------------------------------------------------------------- -256@contextmanager -257def nsh(serial_or_port: str, baudrate: int = 57600): -258 """ -259 Opens a serial port with the `serial` number or filepath and closes it again. -260 -261 :param serial_or_port: the serial number or the filepath of the port to -262 connect to. -263 :param baudrate: the baudrate to use. -264 -265 :raises `SerialException`: if serial port is not found. -266 -267 :return: yields an initialized `Nsh` object. -268 """ -269 nsh = None -270 if "/" in serial_or_port: -271 ttyDevice = serial_or_port -272 else: -273 ttyDevice = find_serial_port(serial_or_port).device -274 try: -275 _LOGGER.info(f"Starting on port '{serial_or_port}'..." -276 if serial_or_port else "Starting...") -277 device = Serial(ttyDevice, baudrate=baudrate) -278 reader_thread = ReaderThread(device, lambda: _NshReader(device)) -279 with reader_thread as reader: -280 nsh = Nsh(reader_thread, reader) -281 yield nsh -282 finally: -283 if nsh is not None: nsh.log_to_file(None) -284 _LOGGER.debug("Stopping.") +254# ----------------------------------------------------------------------------- +255@contextmanager +256def nsh(serial_or_port: str, baudrate: int = 57600): +257 """ +258 Opens a serial port with the `serial` number or filepath and closes it again. +259 +260 :param serial_or_port: the serial number or the filepath of the port to +261 connect to. +262 :param baudrate: the baudrate to use. +263 +264 :raises `SerialException`: if serial port is not found. +265 +266 :return: yields an initialized `Nsh` object. +267 """ +268 nsh = None +269 if "/" in serial_or_port: +270 ttyDevice = serial_or_port +271 else: +272 ttyDevice = find_serial_port(serial_or_port).device +273 try: +274 _LOGGER.info(f"Starting on port '{serial_or_port}'..." +275 if serial_or_port else "Starting...") +276 device = Serial(ttyDevice, baudrate=baudrate) +277 reader_thread = ReaderThread(device, lambda: _NshReader(device)) +278 with reader_thread as reader: +279 nsh = Nsh(reader_thread, reader) +280 yield nsh +281 finally: +282 if nsh is not None: nsh.log_to_file(None) +283 _LOGGER.debug("Stopping.") +284 285 -286 -287# ----------------------------------------------------------------------------- -288# We need to monkey patch the ReaderThread.run() function to prevent a -289# "device not ready" error to abort the reader thread. -290def _patched_run(self): -291 from serial import SerialException -292 self.serial.timeout = 0.1 -293 self.protocol = self.protocol_factory() -294 try: -295 self.protocol.connection_made(self) -296 except Exception as e: -297 self.alive = False -298 self.protocol.connection_lost(e) -299 self._connection_made.set() -300 return -301 error = None -302 self._connection_made.set() -303 while self.alive and self.serial.is_open: -304 try: -305 data = self.serial.read(self.serial.in_waiting or 1) -306 except SerialException as e: -307 if self.alive and "readiness" in str(e): -308 # _LOGGER.debug(e) -309 continue -310 error = e -311 break -312 else: -313 if data: -314 try: -315 self.protocol.data_received(data) -316 except Exception as e: -317 error = e -318 break -319 self.alive = False -320 self.protocol.connection_lost(error) -321 self.protocol = None -322 -323ReaderThread.run = _patched_run +286# ----------------------------------------------------------------------------- +287# We need to monkey patch the ReaderThread.run() function to prevent a +288# "device not ready" error to abort the reader thread. +289def _patched_run(self): +290 from serial import SerialException +291 self.serial.timeout = 0.1 +292 self.protocol = self.protocol_factory() +293 try: +294 self.protocol.connection_made(self) +295 except Exception as e: +296 self.alive = False +297 self.protocol.connection_lost(e) +298 self._connection_made.set() +299 return +300 error = None +301 self._connection_made.set() +302 while self.alive and self.serial.is_open: +303 try: +304 data = self.serial.read(self.serial.in_waiting or 1) +305 except SerialException as e: +306 if self.alive and "readiness" in str(e): +307 # _LOGGER.debug(e) +308 continue +309 error = e +310 break +311 else: +312 if data: +313 try: +314 self.protocol.data_received(data) +315 except Exception as e: +316 error = e +317 break +318 self.alive = False +319 self.protocol.connection_lost(error) +320 self.protocol = None +321 +322ReaderThread.run = _patched_run @@ -566,91 +565,90 @@

166 167 :return: received lines until matched pattern or None on timeout 168 """ -169 lines = [] +169 lines = "" 170 start = time.time() 171 while True: 172 if time.time() - start > timeout: 173 break -174 new_lines = self.read_lines(0) -175 lines.append(new_lines) -176 for line in new_lines: -177 if re.search(pattern, line): -178 return self._join(lines) -179 time.sleep(0.1) -180 _LOGGER.warning(f"Waiting for '{pattern}' timed out after {timeout:.1f}s!") -181 return None -182 -183 def wait_for_prompt(self, timeout: float = _TIMEOUT) -> list[str]: -184 """ -185 Waits to the `nsh> ` prompt to arrive in the stream. -186 Note that any ANSI escape codes (for color or cursor position) are -187 filtered out. -188 -189 :param timeout: seconds to wait until the prompt arrives. -190 -191 :return: all lines until the prompt arrives. -192 """ -193 if prompts := self._read_packets(Nsh._NEWLINE + Nsh._PROMPT, timeout): -194 prompt = Nsh._PROMPT + Nsh._PROMPT.join(prompts) -195 return self._join(self._filter(prompt.split(Nsh._NEWLINE))) -196 _LOGGER.warning(f"Waiting for 'nsh> ' prompt timed out after {timeout:.1f}s!") -197 return None +174 if (new_lines := self.read_lines(0)) is not None: +175 lines += new_lines +176 if re.search(pattern, new_lines): +177 return lines +178 time.sleep(0.1) +179 _LOGGER.warning(f"Waiting for '{pattern}' timed out after {timeout:.1f}s!") +180 return None +181 +182 def wait_for_prompt(self, timeout: float = _TIMEOUT) -> list[str]: +183 """ +184 Waits to the `nsh> ` prompt to arrive in the stream. +185 Note that any ANSI escape codes (for color or cursor position) are +186 filtered out. +187 +188 :param timeout: seconds to wait until the prompt arrives. +189 +190 :return: all lines until the prompt arrives. +191 """ +192 if prompts := self._read_packets(Nsh._NEWLINE + Nsh._PROMPT, timeout): +193 prompt = Nsh._PROMPT + Nsh._PROMPT.join(prompts) +194 return self._join(self._filter(prompt.split(Nsh._NEWLINE))) +195 _LOGGER.warning(f"Waiting for 'nsh> ' prompt timed out after {timeout:.1f}s!") +196 return None +197 198 -199 -200 def command(self, command: str, timeout: float = _TIMEOUT) -> str | None: -201 """ -202 Send a command and return all lines until the next prompt. -203 If the command is asynchronous, you need to poll for new lines separately. -204 Note that any ANSI escape codes (for color or cursor position) are -205 filtered out. -206 -207 :param command: command string to send to the NSH. -208 :param timeout: seconds to wait until the prompt arrives. -209 -210 :return: all lines from the command issue until the next prompt arrives. -211 """ -212 self._serial.clear() -213 self._write_line(command) -214 if timeout is not None: -215 return self.wait_for_prompt(timeout) -216 -217 def command_nowait(self, command: str): -218 """ -219 Send a command to the NSH without waiting for a response. -220 -221 :param command: command string to send to the NSH. -222 """ -223 self.command(command, None) +199 def command(self, command: str, timeout: float = _TIMEOUT) -> str | None: +200 """ +201 Send a command and return all lines until the next prompt. +202 If the command is asynchronous, you need to poll for new lines separately. +203 Note that any ANSI escape codes (for color or cursor position) are +204 filtered out. +205 +206 :param command: command string to send to the NSH. +207 :param timeout: seconds to wait until the prompt arrives. +208 +209 :return: all lines from the command issue until the next prompt arrives. +210 """ +211 self._serial.clear() +212 self._write_line(command) +213 if timeout is not None: +214 return self.wait_for_prompt(timeout) +215 +216 def command_nowait(self, command: str): +217 """ +218 Send a command to the NSH without waiting for a response. +219 +220 :param command: command string to send to the NSH. +221 """ +222 self.command(command, None) +223 224 -225 -226 def reboot(self, timeout: int = 15) -> str | None: -227 """ -228 Send the reboot command and wait for the reboot to be completed. -229 -230 :param timeout: seconds to wait until the prompt arrives. -231 -232 :return: all lines from reboot until the next prompt. -233 """ -234 return self.command("reboot", timeout) -235 -236 def is_alive(self, timeout: float = _TIMEOUT, attempts: int = 4) -> bool: -237 """ -238 Check if the NSH is responding to newline inputs with a `nsh> ` prompt. -239 The total timeout is `attempts * timeout`! -240 -241 :param timeout: seconds to wait until the prompt arrives. -242 :param attempts: number of times to send a newline and wait. -243 :return: `True` is NSH responds, `False` otherwise -244 """ -245 self._serial.clear() -246 attempt = 0 -247 timeout = timeout / attempts -248 while attempt < attempts: -249 self._write_line("") -250 if self.wait_for_prompt(timeout) is not None: -251 return True -252 attempt += 1 -253 return False +225 def reboot(self, timeout: int = 15) -> str | None: +226 """ +227 Send the reboot command and wait for the reboot to be completed. +228 +229 :param timeout: seconds to wait until the prompt arrives. +230 +231 :return: all lines from reboot until the next prompt. +232 """ +233 return self.command("reboot", timeout) +234 +235 def is_alive(self, timeout: float = _TIMEOUT, attempts: int = 4) -> bool: +236 """ +237 Check if the NSH is responding to newline inputs with a `nsh> ` prompt. +238 The total timeout is `attempts * timeout`! +239 +240 :param timeout: seconds to wait until the prompt arrives. +241 :param attempts: number of times to send a newline and wait. +242 :return: `True` is NSH responds, `False` otherwise +243 """ +244 self._serial.clear() +245 attempt = 0 +246 timeout = timeout / attempts +247 while attempt < attempts: +248 self._write_line("") +249 if self.wait_for_prompt(timeout) is not None: +250 return True +251 attempt += 1 +252 return False @@ -866,19 +864,18 @@

Returns
166 167 :return: received lines until matched pattern or None on timeout 168 """ -169 lines = [] +169 lines = "" 170 start = time.time() 171 while True: 172 if time.time() - start > timeout: 173 break -174 new_lines = self.read_lines(0) -175 lines.append(new_lines) -176 for line in new_lines: -177 if re.search(pattern, line): -178 return self._join(lines) -179 time.sleep(0.1) -180 _LOGGER.warning(f"Waiting for '{pattern}' timed out after {timeout:.1f}s!") -181 return None +174 if (new_lines := self.read_lines(0)) is not None: +175 lines += new_lines +176 if re.search(pattern, new_lines): +177 return lines +178 time.sleep(0.1) +179 _LOGGER.warning(f"Waiting for '{pattern}' timed out after {timeout:.1f}s!") +180 return None @@ -916,21 +913,21 @@
Returns
-
183    def wait_for_prompt(self, timeout: float = _TIMEOUT) -> list[str]:
-184        """
-185        Waits to the `nsh> ` prompt to arrive in the stream.
-186        Note that any ANSI escape codes (for color or cursor position) are
-187        filtered out.
-188
-189        :param timeout: seconds to wait until the prompt arrives.
-190
-191        :return: all lines until the prompt arrives.
-192        """
-193        if prompts := self._read_packets(Nsh._NEWLINE + Nsh._PROMPT, timeout):
-194            prompt = Nsh._PROMPT + Nsh._PROMPT.join(prompts)
-195            return self._join(self._filter(prompt.split(Nsh._NEWLINE)))
-196        _LOGGER.warning(f"Waiting for 'nsh> ' prompt timed out after {timeout:.1f}s!")
-197        return None
+            
182    def wait_for_prompt(self, timeout: float = _TIMEOUT) -> list[str]:
+183        """
+184        Waits to the `nsh> ` prompt to arrive in the stream.
+185        Note that any ANSI escape codes (for color or cursor position) are
+186        filtered out.
+187
+188        :param timeout: seconds to wait until the prompt arrives.
+189
+190        :return: all lines until the prompt arrives.
+191        """
+192        if prompts := self._read_packets(Nsh._NEWLINE + Nsh._PROMPT, timeout):
+193            prompt = Nsh._PROMPT + Nsh._PROMPT.join(prompts)
+194            return self._join(self._filter(prompt.split(Nsh._NEWLINE)))
+195        _LOGGER.warning(f"Waiting for 'nsh> ' prompt timed out after {timeout:.1f}s!")
+196        return None
 
@@ -964,22 +961,22 @@
Returns
-
200    def command(self, command: str, timeout: float = _TIMEOUT) -> str | None:
-201        """
-202        Send a command and return all lines until the next prompt.
-203        If the command is asynchronous, you need to poll for new lines separately.
-204        Note that any ANSI escape codes (for color or cursor position) are
-205        filtered out.
-206
-207        :param command: command string to send to the NSH.
-208        :param timeout: seconds to wait until the prompt arrives.
-209
-210        :return: all lines from the command issue until the next prompt arrives.
-211        """
-212        self._serial.clear()
-213        self._write_line(command)
-214        if timeout is not None:
-215            return self.wait_for_prompt(timeout)
+            
199    def command(self, command: str, timeout: float = _TIMEOUT) -> str | None:
+200        """
+201        Send a command and return all lines until the next prompt.
+202        If the command is asynchronous, you need to poll for new lines separately.
+203        Note that any ANSI escape codes (for color or cursor position) are
+204        filtered out.
+205
+206        :param command: command string to send to the NSH.
+207        :param timeout: seconds to wait until the prompt arrives.
+208
+209        :return: all lines from the command issue until the next prompt arrives.
+210        """
+211        self._serial.clear()
+212        self._write_line(command)
+213        if timeout is not None:
+214            return self.wait_for_prompt(timeout)
 
@@ -1015,13 +1012,13 @@
Returns
-
217    def command_nowait(self, command: str):
-218        """
-219        Send a command to the NSH without waiting for a response.
-220
-221        :param command: command string to send to the NSH.
-222        """
-223        self.command(command, None)
+            
216    def command_nowait(self, command: str):
+217        """
+218        Send a command to the NSH without waiting for a response.
+219
+220        :param command: command string to send to the NSH.
+221        """
+222        self.command(command, None)
 
@@ -1047,15 +1044,15 @@
Parameters
-
226    def reboot(self, timeout: int = 15) -> str | None:
-227        """
-228        Send the reboot command and wait for the reboot to be completed.
-229
-230        :param timeout: seconds to wait until the prompt arrives.
-231
-232        :return: all lines from reboot until the next prompt.
-233        """
-234        return self.command("reboot", timeout)
+            
225    def reboot(self, timeout: int = 15) -> str | None:
+226        """
+227        Send the reboot command and wait for the reboot to be completed.
+228
+229        :param timeout: seconds to wait until the prompt arrives.
+230
+231        :return: all lines from reboot until the next prompt.
+232        """
+233        return self.command("reboot", timeout)
 
@@ -1087,24 +1084,24 @@
Returns
-
236    def is_alive(self, timeout: float = _TIMEOUT, attempts: int = 4) -> bool:
-237        """
-238        Check if the NSH is responding to newline inputs with a `nsh> ` prompt.
-239        The total timeout is `attempts * timeout`!
-240
-241        :param timeout: seconds to wait until the prompt arrives.
-242        :param attempts: number of times to send a newline and wait.
-243        :return: `True` is NSH responds, `False` otherwise
-244        """
-245        self._serial.clear()
-246        attempt = 0
-247        timeout = timeout / attempts
-248        while attempt < attempts:
-249            self._write_line("")
-250            if self.wait_for_prompt(timeout) is not None:
-251                return True
-252            attempt += 1
-253        return False
+            
235    def is_alive(self, timeout: float = _TIMEOUT, attempts: int = 4) -> bool:
+236        """
+237        Check if the NSH is responding to newline inputs with a `nsh> ` prompt.
+238        The total timeout is `attempts * timeout`!
+239
+240        :param timeout: seconds to wait until the prompt arrives.
+241        :param attempts: number of times to send a newline and wait.
+242        :return: `True` is NSH responds, `False` otherwise
+243        """
+244        self._serial.clear()
+245        attempt = 0
+246        timeout = timeout / attempts
+247        while attempt < attempts:
+248            self._write_line("")
+249            if self.wait_for_prompt(timeout) is not None:
+250                return True
+251            attempt += 1
+252        return False
 
@@ -1140,35 +1137,35 @@
Returns
-
257@contextmanager
-258def nsh(serial_or_port: str, baudrate: int = 57600):
-259    """
-260    Opens a serial port with the `serial` number or filepath and closes it again.
-261
-262    :param serial_or_port: the serial number or the filepath of the port to
-263                           connect to.
-264    :param baudrate: the baudrate to use.
-265
-266    :raises `SerialException`: if serial port is not found.
-267
-268    :return: yields an initialized `Nsh` object.
-269    """
-270    nsh = None
-271    if "/" in serial_or_port:
-272        ttyDevice = serial_or_port
-273    else:
-274        ttyDevice = find_serial_port(serial_or_port).device
-275    try:
-276        _LOGGER.info(f"Starting on port '{serial_or_port}'..."
-277                    if serial_or_port else "Starting...")
-278        device = Serial(ttyDevice, baudrate=baudrate)
-279        reader_thread = ReaderThread(device, lambda: _NshReader(device))
-280        with reader_thread as reader:
-281            nsh = Nsh(reader_thread, reader)
-282            yield nsh
-283    finally:
-284        if nsh is not None: nsh.log_to_file(None)
-285        _LOGGER.debug("Stopping.")
+            
256@contextmanager
+257def nsh(serial_or_port: str, baudrate: int = 57600):
+258    """
+259    Opens a serial port with the `serial` number or filepath and closes it again.
+260
+261    :param serial_or_port: the serial number or the filepath of the port to
+262                           connect to.
+263    :param baudrate: the baudrate to use.
+264
+265    :raises `SerialException`: if serial port is not found.
+266
+267    :return: yields an initialized `Nsh` object.
+268    """
+269    nsh = None
+270    if "/" in serial_or_port:
+271        ttyDevice = serial_or_port
+272    else:
+273        ttyDevice = find_serial_port(serial_or_port).device
+274    try:
+275        _LOGGER.info(f"Starting on port '{serial_or_port}'..."
+276                    if serial_or_port else "Starting...")
+277        device = Serial(ttyDevice, baudrate=baudrate)
+278        reader_thread = ReaderThread(device, lambda: _NshReader(device))
+279        with reader_thread as reader:
+280            nsh = Nsh(reader_thread, reader)
+281            yield nsh
+282    finally:
+283        if nsh is not None: nsh.log_to_file(None)
+284        _LOGGER.debug("Stopping.")
 
diff --git a/emdbg/serial/utils.html b/emdbg/serial/utils.html index 8a81fc6..084be1d 100644 --- a/emdbg/serial/utils.html +++ b/emdbg/serial/utils.html @@ -3,7 +3,7 @@ - + emdbg.serial.utils API documentation diff --git a/emdbg/utils.html b/emdbg/utils.html index e111f5a..3f408bf 100644 --- a/emdbg/utils.html +++ b/emdbg/utils.html @@ -3,7 +3,7 @@ - + emdbg.utils API documentation