Skip to content

Commit ed42d8f

Browse files
Add mpremote session validation
1 parent 7cf54f4 commit ed42d8f

File tree

2 files changed

+29
-4
lines changed

2 files changed

+29
-4
lines changed

MicroPython_Firmware_Uploader/MicroPython_Firmware_Uploader.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -634,6 +634,7 @@ def on_finished(self, status, action_type, job_id) -> None:
634634
# If the reset is finished, re-enable the UX
635635
if action_type == AUxEsptoolResetESP32.ACTION_ID:
636636
self.writeMessage("Reset complete...")
637+
self.writeMessage("DONE: Firmware file copied to ESP32 device.\n")
637638
self.disable_interface(False)
638639

639640
if action_type == AUxRp2UploadRp2.ACTION_ID:
@@ -851,7 +852,7 @@ def on_discover_btn_pressed(self) -> None:
851852
return
852853

853854
mpr = self.get_mpremote_session()
854-
if mpr.is_connected():
855+
if mpr is not None:
855856
try:
856857
boardName = mpr.get_short_board_name()
857858
if boardName is None:
@@ -1059,7 +1060,14 @@ def get_mpremote_session(self) -> MPRemoteSession:
10591060
else:
10601061
portName = self.port_button.text()
10611062

1062-
return MPRemoteSession(portName)
1063+
mpr = MPRemoteSession(portName)
1064+
1065+
if mpr.validate_session():
1066+
self.writeMessage("MPRemote session validated.\n")
1067+
return mpr
1068+
else:
1069+
self.writeMessage("MPRemote session failed to validate.\n")
1070+
return None
10631071

10641072
def do_upload_rp2(self, fwFile = None) -> None:
10651073
"""Upload the firmware to the RP2."""
@@ -1074,7 +1082,7 @@ def do_upload_rp2(self, fwFile = None) -> None:
10741082

10751083
# If we are able to use mpremote to enter bootloader mode, do so
10761084
# Otherwise, prompt the user to press the correct button sequence to enter bootloader mode
1077-
if mpr.is_connected():
1085+
if mpr is not None:
10781086
self.writeMessage("Able to automatically enter bootloader mode...\n")
10791087
mpr.enter_bootloader()
10801088
self.writeMessage("Entering bootloader mode...\n")

MicroPython_Firmware_Uploader/mpremote_utils.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def exec_command(self, command: str) -> None:
117117
self.args.expr = [command]
118118
commands.do_exec(state=self.state, args=self.args)
119119

120-
def exec_command_with_output(self, command: str, timeout: Optional[float] = 3) -> str:
120+
def exec_command_with_output(self, command: str, timeout: Optional[float] = 2) -> str:
121121

122122
self.args.expr = [command]
123123
capture = StdoutCapture()
@@ -146,6 +146,23 @@ def execute():
146146
capture.stop()
147147
return capture.get_output()
148148

149+
def validate_session(self) -> None:
150+
# Ensure that we can ping the device by checking sys.implementation.name
151+
# This is a good way to check if the device is connected and responsive
152+
try:
153+
if self.is_connected():
154+
res = self.exec_command_with_output("import sys; print(sys.implementation.name)")
155+
if res is None:
156+
return False
157+
if res.strip() == "micropython":
158+
return True
159+
except Exception as e:
160+
#print("Error validating session:", e)
161+
pass
162+
163+
return False
164+
165+
149166
def eval_command(self, command: str) -> None:
150167
self.args.expr = self.args.eval = [command]
151168
commands.do_eval(state=self.state, args=self.args)

0 commit comments

Comments
 (0)