Skip to content

Commit 4e83526

Browse files
Refactor __autogenerate_config method to return success status; enhance user input handling for baud rate and address
1 parent c7e839e commit 4e83526

File tree

2 files changed

+44
-16
lines changed

2 files changed

+44
-16
lines changed

.idea/workspace.xml

Lines changed: 24 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

flasher.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,23 +91,27 @@ def display_menu(self) -> None:
9191
except Exception as e:
9292
handler.exception(msg=e)
9393

94-
def __autogenerate_config(self, folder_path: str) -> None:
94+
def __autogenerate_config(self, folder_path: str) -> bool:
9595
try:
9696
bin_files = [f for f in os.listdir(folder_path) if f.endswith('.bin')]
9797
if not bin_files:
9898
tprint.error("No .bin files found in the folder to generate config.ini.")
99-
return
99+
return False
100100

101101
print()
102102
tprint.info(f"Found BIN files: {', '.join(bin_files)}")
103103

104104
baud = self.__get_valid_baud_rate()
105+
if baud == 'exit':
106+
return False
105107

106108
config = configparser.ConfigParser()
107109
config['Settings'] = {'Baud_Rate': baud}
108110

109111
for bin_file in bin_files:
110112
address = self.__get_valid_address(bin_file)
113+
if address == 'exit':
114+
return False
111115
config['Settings'][bin_file] = address
112116

113117
config_path = os.path.join(folder_path, 'config.ini')
@@ -116,14 +120,18 @@ def __autogenerate_config(self, folder_path: str) -> None:
116120

117121
print()
118122
tprint.success("'config.ini' generated successfully!")
123+
return True
119124
except Exception as e:
120125
handler.exception(msg=e)
126+
return False
121127

122128
@staticmethod
123129
def __get_valid_baud_rate() -> str:
124130
try:
125131
while True:
126132
baud = tprint.input("Enter Baud Rate: > ").strip()
133+
if baud.lower() == 'exit':
134+
return 'exit'
127135
if baud.isdigit():
128136
return baud
129137
tprint.warning("Invalid baud rate. Please enter numbers only.")
@@ -135,6 +143,8 @@ def __get_valid_address(bin_file: str) -> str:
135143
try:
136144
while True:
137145
address = tprint.input(f"Enter memory address for '{bin_file}': > ").strip()
146+
if address.lower() == 'exit':
147+
return 'exit'
138148
if address.startswith('0x') and all(c in '0123456789abcdefABCDEF' for c in address[2:]):
139149
return address
140150
tprint.warning("Invalid address format. Use hex (e.g., 0x10000).")
@@ -250,12 +260,15 @@ def __handle_selection(self, item: tuple[str, str, list[str], list[str]]) -> Non
250260
tprint.input("Press enter to recheck the project: > ")
251261
self.__check_project(folder_name, folder_path)
252262
elif choice == '2':
253-
self.__autogenerate_config(folder_path)
254-
self.__check_project(folder_name, folder_path)
263+
if self.__autogenerate_config(folder_path):
264+
self.__check_project(folder_name, folder_path)
265+
else:
266+
tprint.error("Failed to autogenerate config.ini.")
255267
elif choice == '3':
256268
self.__check_project(folder_name, folder_path)
257269
else:
258-
tprint.warning("Invalid choice, returning to menu.")
270+
if choice.lower() != 'exit':
271+
tprint.warning("Invalid choice, returning to menu.")
259272
self.display_menu()
260273
else:
261274
self.__flash_esp32(folder_name)
@@ -312,6 +325,8 @@ def __flash_esp32(self, folder_name: str) -> None:
312325
tprint.info(f"<{idx + 1}> {port.device} - {port.description}{marker}")
313326

314327
choice = tprint.input("Select a COM port (or press enter to use suggested): > ").strip()
328+
if choice.lower() == 'exit':
329+
return
315330

316331
selected_port = self.__get_selected_com_port(choice, likely_port, ports)
317332
if not selected_port:

0 commit comments

Comments
 (0)