Skip to content

Commit 5d59b9e

Browse files
committed
move methods into serial_base
1 parent cba1846 commit 5d59b9e

File tree

3 files changed

+67
-65
lines changed

3 files changed

+67
-65
lines changed

serial_plugin/serial_base.py

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1+
from logging import getLogger, error, debug
12
from serial import Serial
23
from time import sleep
3-
from logging import getLogger, error
44
from types import TracebackType
55
from typing import Optional, Type
66
from config.application_configuration import SERIAL_RATE
@@ -11,7 +11,7 @@
1111

1212
class SerialBase:
1313
"""
14-
Manages a MicroPython serial connection.
14+
Manages a MicroPython serial connection and REPL modes.
1515
"""
1616

1717
def __init__(self, port: str, baudrate: int = SERIAL_RATE, timeout: int = 2):
@@ -53,6 +53,65 @@ def _disconnect(self) -> None:
5353
if self._ser and self._ser.is_open:
5454
self._ser.close()
5555

56+
def wake_repl(self) -> None:
57+
"""
58+
Invokes a wake-up sequence for the REPL (Read-Eval-Print Loop) interface by
59+
interacting with the serial connection.
60+
61+
:return: None
62+
"""
63+
self._ser.reset_input_buffer()
64+
self._ser.write(b'\r\n')
65+
sleep(0.2)
66+
67+
self._ser.read_all()
68+
69+
def send_command(self, command: str, wait: float = 0.3) -> str:
70+
"""
71+
Sends a command to the REPL interface and retrieves its output.
72+
73+
:param command: The command to be sent to the REPL interface.
74+
:type command: str
75+
:param wait: The amount of time, in seconds.
76+
:type wait: float, optional
77+
:return: The output received from the REPL
78+
:rtype: str
79+
:raises RuntimeError: If the REPL interface is not connected or available.
80+
"""
81+
if not self._ser or not self._ser.is_open:
82+
raise RuntimeError("REPL not connected")
83+
84+
self._ser.write(command.encode() + b'\r\n')
85+
sleep(wait)
86+
87+
output = self._ser.read_all().decode(errors='ignore')
88+
debug(f"[DEBUG] command output: {output}")
89+
90+
return output.strip()
91+
92+
def enter_raw_repl(self) -> None:
93+
"""
94+
Enter raw REPL mode on the connected device.
95+
96+
:return: None
97+
"""
98+
self._ser.write(b'\r\x03\x03')
99+
sleep(0.1)
100+
101+
self._ser.write(b'\r\x01')
102+
sleep(0.1)
103+
104+
self._ser.reset_input_buffer()
105+
106+
def exit_raw_repl(self) -> None:
107+
"""
108+
Exits raw REPL mode on the connected device.
109+
110+
:return: None
111+
"""
112+
self._ser.write(b'\r\x02')
113+
sleep(0.1)
114+
56115
def __enter__(self) -> "SerialBase":
57116
"""
58117
Provides context management for the MicroPythonTree, ensuring resources are

serial_plugin/serial_get_file_structure.py

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from logging import getLogger, debug
2-
from time import time, sleep
2+
from time import time
33
from serial_plugin.serial_base import SerialBase
44

55

@@ -33,29 +33,6 @@ class FileStructure(SerialBase):
3333
"tree('')\n"
3434
)
3535

36-
def _enter_raw_repl(self) -> None:
37-
"""
38-
Enter raw REPL mode on the connected device.
39-
40-
:return: None
41-
"""
42-
self._ser.write(b'\r\x03\x03')
43-
sleep(0.1)
44-
45-
self._ser.write(b'\r\x01')
46-
sleep(0.1)
47-
48-
self._ser.reset_input_buffer()
49-
50-
def _exit_raw_repl(self) -> None:
51-
"""
52-
Exits raw REPL mode on the connected device.
53-
54-
:return: None
55-
"""
56-
self._ser.write(b'\r\x02')
57-
sleep(0.1)
58-
5936
def get_tree(self) -> str:
6037
"""
6138
Retrieves the current state of the tree structure by communicating with
@@ -66,7 +43,7 @@ def get_tree(self) -> str:
6643
:return: The tree structure information.
6744
:rtype: str
6845
"""
69-
self._enter_raw_repl()
46+
self.enter_raw_repl()
7047
self._ser.write(self._TREE_CODE.encode('utf-8') + b'\x04')
7148
output = b''
7249
start = time()
@@ -83,7 +60,7 @@ def get_tree(self) -> str:
8360
if output.endswith(b'\x04>'):
8461
break
8562

86-
self._exit_raw_repl()
63+
self.exit_raw_repl()
8764
out = output.decode(errors="ignore")
8865
debug(f"[DEBUG] tree output: {out}")
8966

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from logging import getLogger, debug
2-
from time import sleep
32
from serial_plugin.serial_base import SerialBase
43

54

@@ -30,41 +29,6 @@ def _extract_version(raw_output: str) -> str:
3029

3130
return "No MicroPython version found"
3231

33-
def _wake_repl(self) -> None:
34-
"""
35-
Invokes a wake-up sequence for the REPL (Read-Eval-Print Loop) interface by
36-
interacting with the serial connection.
37-
38-
:return: None
39-
"""
40-
self._ser.reset_input_buffer()
41-
self._ser.write(b'\r\n')
42-
sleep(0.2)
43-
44-
self._ser.read_all()
45-
46-
def _send_command(self, command: str, wait: float = 0.3) -> str:
47-
"""
48-
Sends a command to the REPL interface and retrieves its output.
49-
50-
:param command: The command to be sent to the REPL interface.
51-
:type command: str
52-
:param wait: The amount of time, in seconds.
53-
:type wait: float, optional
54-
:return: The output received from the REPL
55-
:rtype: str
56-
:raises RuntimeError: If the REPL interface is not connected or available.
57-
"""
58-
if not self._ser or not self._ser.is_open:
59-
raise RuntimeError("REPL not connected")
60-
61-
self._ser.write(command.encode() + b'\r\n')
62-
sleep(wait)
63-
64-
output = self._ser.read_all().decode(errors='ignore')
65-
debug(f"[DEBUG] command output: {output}")
66-
return output.strip()
67-
6832
def get_version(self) -> str:
6933
"""
7034
Extracts and returns the Python version from the command output.
@@ -73,5 +37,7 @@ def get_version(self) -> str:
7337
:return: The extracted Python version as a string.
7438
:rtype: str
7539
"""
76-
output = self._send_command("import sys; print(sys.version)")
40+
output = self.send_command("import sys; print(sys.version)")
41+
debug(f"[DEBUG] version output: {output}")
42+
7743
return self._extract_version(output)

0 commit comments

Comments
 (0)