-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp_controller.py
More file actions
561 lines (464 loc) · 22.2 KB
/
app_controller.py
File metadata and controls
561 lines (464 loc) · 22.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
from glob import glob
from serial.tools import list_ports
from logging import getLogger, debug, info, error
from os.path import expanduser, basename
from customtkinter import CTkButton, CTkFrame
from tkinter import filedialog, Event
from webbrowser import open_new
from queue import Queue, Empty
from typing import Optional, Callable, Tuple
from ui.base_ui import BaseUI
from ui.frame_device_information import FrameDeviceInformation
from ui.frame_erase_device import FrameEraseDevice
from ui.frame_firmware_flash import FrameFirmwareFlash
from ui.frame_search_device import FrameSearchDevice
from ui.frame_console import FrameConsole
from ui.frame_plugins import FramePlugIns
from esptool_plugin.esptool_command_runner import CommandRunner
from serial_plugin.serial_command_runner import SerialCommandRunner
from config.device_configuration import BAUDRATE_OPTIONS, DEFAULT_URL, CONFIGURED_DEVICES
logger = getLogger(__name__)
class MicroPythonFirmwareStudio(BaseUI):
"""
A GUI class to manage ESP device configuration and firmware flashing.
Provides an interface to connect, configure, and flash firmware for ESP devices.
Includes options to toggle expert mode and interact with device-specific details.
"""
def __init__(self):
"""
A GUI class to manage ESP device configuration and firmware flashing.
"""
super().__init__()
# Variables and objects
self._console_queue: Queue = Queue()
self.__device_path: Optional[str] = None
self.__selected_chip: Optional[str] = None
self.__selected_baudrate: Optional[int] = 460800
self.__selected_firmware: Optional[str] = None
self.__url: str = DEFAULT_URL
self.__expert_mode: bool = False
self.esptool_runner = CommandRunner(
on_output=self._handle_esptool_output,
on_error=self._handle_esptool_error,
on_complete=self._handle_esptool_complete
)
debug('Adding frames to UI and configuring elements')
# Search Device
self.search_device = FrameSearchDevice(self)
self.search_device.reload_btn.configure(command=self._search_devices)
self.search_device.device_option.configure(command=self._set_device)
# Device Information
self.information = FrameDeviceInformation(self)
self.information.chip_info_btn.configure(command=lambda: self._esptool_command("chip_id"))
self.information.memory_info_btn.configure(command=lambda: self._esptool_command("flash_id"))
self.information.mac_info_btn.configure(command=lambda: self._esptool_command("read_mac"))
self.information.flash_status_btn.configure(command=lambda: self._esptool_command("read_flash_status"))
self.information.mac_info_btn.pack_forget()
self.information.flash_status_btn.pack_forget()
# Erase Device
self.erase_device = FrameEraseDevice(self)
self.erase_device.erase_btn.configure(command=lambda: self._esptool_command("erase_flash"))
# PlugIns
self.plugins = FramePlugIns(self)
self.plugins.mp_debug_btn.configure(command=self._handler_toplevel_serial_debug)
self.plugins.mp_version_btn.configure(command=self._get_version)
self.plugins.mp_structure_btn.configure(command=self._get_structure)
self.plugins.grid_remove()
# Flash Firmware
self.flash_firmware = FrameFirmwareFlash(self)
self.flash_firmware.expert_mode.configure(command=self.toggle_expert_mode)
self.flash_firmware.chip_option.configure(command=self._set_chip)
self.flash_firmware.firmware_btn.configure(command=self._handle_firmware_selection)
self.flash_firmware.link_label.bind("<Button-1>", self.open_url)
self.flash_firmware.baudrate_option.set(str(self.__selected_baudrate))
self.flash_firmware.baudrate_option.configure(command=self._set_baudrate)
self.flash_firmware.baudrate_checkbox.select()
self.flash_firmware.sector_input.bind("<KeyRelease>", self._handle_sector_input)
self.flash_firmware.flash_btn.configure(command=self._flash_firmware_command)
self.flash_firmware.flash_mode_label.grid_remove()
self.flash_firmware.flash_mode_option.grid_remove()
self.flash_firmware.flash_mode_info.grid_remove()
self.flash_firmware.flash_frequency_label.grid_remove()
self.flash_firmware.flash_frequency_option.grid_remove()
self.flash_firmware.flash_frequency_info.grid_remove()
self.flash_firmware.flash_size_label.grid_remove()
self.flash_firmware.flash_size_option.grid_remove()
self.flash_firmware.flash_size_info.grid_remove()
self.flash_firmware.erase_before_label.grid_remove()
self.flash_firmware.erase_before_switch.grid_remove()
self.flash_firmware.erase_before_info.grid_remove()
# Console
self.console = FrameConsole(self)
self.console.console_text.bind("<Key>", BaseUI._block_text_input)
# search for devices on the start
debug('Searching for USB devices')
self._search_devices()
# poll the console queue for new lines
debug('Starting console queue poll')
self._poll_console_queue()
def open_url(self, event: Event) -> None:
"""
Opens a new web browser tab or window using the URL specified in the class attribute.
:param event: The event instance is triggering this method
:type event: Event
:return: None
"""
_ = event
debug(f'Opening URL: {self.__url}')
open_new(url=self.__url)
def toggle_expert_mode(self) -> None:
"""
Toggles between expert mode and standard mode in the user interface.
:return: None
"""
if self.flash_firmware.expert_mode.get():
debug('Expert mode enabled')
self.__expert_mode = True
self.information.mac_info_btn.pack(padx=10, pady=5)
self.information.flash_status_btn.pack(padx=10, pady=5)
self.plugins.grid(row=3, column=0, padx=10, pady=5, sticky="nsew")
self.flash_firmware.flash_mode_label.grid(row=5, column=0, padx=10, pady=5, sticky="w")
self.flash_firmware.flash_mode_option.grid(row=5, column=1, padx=10, pady=5, sticky="w")
self.flash_firmware.flash_mode_info.grid(row=5, column=3, columnspan=3, padx=10, pady=5, sticky="w")
self.flash_firmware.flash_frequency_label.grid(row=6, column=0, padx=10, pady=5, sticky="w")
self.flash_firmware.flash_frequency_option.grid(row=6, column=1, padx=10, pady=5, sticky="w")
self.flash_firmware.flash_frequency_info.grid(row=6, column=3, columnspan=3, padx=10, pady=5, sticky="w")
self.flash_firmware.flash_size_label.grid(row=7, column=0, padx=10, pady=5, sticky="w")
self.flash_firmware.flash_size_option.grid(row=7, column=1, padx=10, pady=5, sticky="w")
self.flash_firmware.flash_size_info.grid(row=7, column=3, columnspan=3, padx=10, pady=5, sticky="w")
self.flash_firmware.erase_before_label.grid(row=8, column=0, padx=10, pady=5, sticky="w")
self.flash_firmware.erase_before_switch.grid(row=8, column=1, padx=10, pady=5, sticky="w")
self.flash_firmware.erase_before_info.grid(row=8, column=3, columnspan=3, padx=10, pady=5, sticky="w")
else:
debug('Expert mode disabled')
self.__expert_mode = False
self.information.mac_info_btn.pack_forget()
self.information.flash_status_btn.pack_forget()
self.plugins.grid_remove()
self.flash_firmware.flash_mode_label.grid_remove()
self.flash_firmware.flash_mode_option.grid_remove()
self.flash_firmware.flash_mode_info.grid_remove()
self.flash_firmware.flash_frequency_label.grid_remove()
self.flash_firmware.flash_frequency_option.grid_remove()
self.flash_firmware.flash_frequency_info.grid_remove()
self.flash_firmware.flash_size_label.grid_remove()
self.flash_firmware.flash_size_option.grid_remove()
self.flash_firmware.flash_size_info.grid_remove()
self.flash_firmware.erase_before_label.grid_remove()
self.flash_firmware.erase_before_switch.grid_remove()
self.flash_firmware.erase_before_info.grid_remove()
def _poll_console_queue(self) -> None:
"""
Polls the console queue for new messages and updates the text widget
with messages retrieved from the queue. Continuously checks the
console queue at regular intervals, inserts retrieved lines into
the text widget, and ensures the view stays scrolled to the most
recent message.
:raises Empty: This is silently handled within the method logic when the queue is empty.
:return: None
"""
try:
while True:
line = self._console_queue.get_nowait()
self.console.console_text.insert("end", f'{line}\n', "normal")
self.console.console_text.see("end")
except Empty:
pass
self.after(100, self._poll_console_queue)
def _delete_console(self) -> None:
"""
Deletes all the content from the console text box.
:return: None
"""
self.console.console_text.delete("1.0", "end")
def _disable_buttons(self) -> None:
"""
Disables specific buttons in the user interface by changing their state to 'disabled'.
:return: None
"""
frames: Tuple[CTkFrame, ...] = (self.information, self.plugins, self.erase_device)
buttons = [
widget
for frame in frames if isinstance(frame, CTkFrame)
for widget in frame.winfo_children()
if isinstance(widget, CTkButton)
]
for button in buttons:
button.configure(state='disabled')
self.flash_firmware.flash_btn.configure(state='disabled')
def _enable_buttons(self) -> None:
"""
Enables specific UI buttons by changing their state to 'normal'.
:return: None
"""
frames: Tuple[CTkFrame, ...] = (self.information, self.plugins, self.erase_device)
buttons = [
widget
for frame in frames if isinstance(frame, CTkFrame)
for widget in frame.winfo_children()
if isinstance(widget, CTkButton)
]
for button in buttons:
button.configure(state='normal')
self.flash_firmware.flash_btn.configure(state='normal')
def _search_devices(self) -> None:
"""
Updates the device dropdown menu with a list of available devices. If there are no
connected devices, sets "No devices found" as the only dropdown value.
:return: None
"""
current_selection = self.search_device.device_option.get()
if self._current_platform in ["Linux", "Darwin"]:
devices = glob(self._device_search_path)
else:
devices = [port.device for port in list_ports.comports()]
if not devices:
devices = ['No devices found']
else:
devices.insert(0, 'Select Device')
debug(f'Devices: {devices}')
self.search_device.device_option.configure(values=devices)
if current_selection in devices:
self.search_device.device_option.set(current_selection)
else:
self.search_device.device_option.set(devices[0])
self._set_device(None)
def _set_device(self, selected_device: Optional[str]) -> None:
"""
Handles the selection of a device using a device selection dialog, updates the
device path, and modifies the configuration label to display the new device path.
:param selected_device: The selected device path as a string.
:type selected_device: Optional[str]
:return: None
"""
if selected_device and selected_device not in ("Select Device", "No devices found"):
info(f'Selected device: {selected_device}')
self.__device_path = selected_device
self.search_device.label.configure(text=f'Device Path: {self.__device_path}')
else:
self.__device_path = None
self.search_device.label.configure(text='Device Path:')
def _set_chip(self, selection: str) -> None:
"""
Sets the chip configuration based on the provided selection.
:param selection: The selected chip name as a string.
:type selection: str
:return: None
"""
debug(f'Selected chip: {selection}')
if selection != "Select Chip":
self.__selected_chip = CONFIGURED_DEVICES[selection]["name"]
self.flash_firmware.sector_input.delete(0, "end")
self.flash_firmware.sector_input.insert(0, hex(CONFIGURED_DEVICES[selection]["write_flash"]))
self.flash_firmware.chip_checkbox.select()
self.flash_firmware.baudrate_checkbox.select()
self.flash_firmware.sector_checkbox.select()
self.__url = CONFIGURED_DEVICES[selection]["url"]
else:
self.__selected_chip = None
self.flash_firmware.sector_input.delete(0, "end")
self.flash_firmware.chip_checkbox.deselect()
self.__url = DEFAULT_URL
def _set_baudrate(self, selection: str) -> None:
"""
Sets the baud rate based on the user selection if it is a valid option.
:param selection: The baud rate option as a string that the user has selected.
:type selection: str
:return: None
"""
debug(f'Selected baudrate: {selection}')
if selection and selection in BAUDRATE_OPTIONS:
self.__selected_baudrate = int(selection)
self.flash_firmware.baudrate_checkbox.select()
else:
self.__selected_baudrate = None
self.flash_firmware.baudrate_checkbox.deselect()
def _handle_sector_input(self, event: Optional[Event] = None) -> None:
"""
Handles changes to the sector input field and updates the sector checkbox
state accordingly.
:param event: An optional event object representing a UI change.
:return: None
"""
_ = event
try:
int(self.flash_firmware.sector_input.get().strip(), 0)
self.flash_firmware.sector_checkbox.select()
except ValueError:
self.flash_firmware.sector_checkbox.deselect()
def _handle_firmware_selection(self) -> None:
"""
Handles selection and association of a firmware file in the user interface.
:return: None
"""
default_dir = expanduser(self._firmware_search_path)
file_path = filedialog.askopenfilename(
initialdir=default_dir,
title='Select Firmware File',
filetypes=(("Binary files", "*.bin"), ("All files", "*.*"))
)
debug(f'Selected firmware: {file_path}')
if file_path:
self.__selected_firmware = file_path
self.flash_firmware.firmware_checkbox.select()
self.flash_firmware.firmware_btn.configure(text=str(basename(file_path)[:15]))
else:
self.__selected_firmware = None
self.flash_firmware.firmware_checkbox.deselect()
def _handle_serial_output(self, output: str, context: Optional[str] = None) -> None:
"""
Handles the processing and queuing of serial output in the application.
:param output: The output to be queued for processing.
:type output: str
:param context: An optional context string indicating the type of output.
:type context: Optional[str]
:return: None
"""
if context == "structure":
output = f"root\n{output}"
self._console_queue.put(output)
self.after(0, self._enable_buttons)
def _run_serial_task(self, info_text: str, command: Callable[[SerialCommandRunner], None]) -> None:
"""
Executes a serial task by running a provided command callable for a serial command runner.
:param info_text: The text to be displayed in the console indicating the task being executed.
:type info_text: str
:param command: The command callable to be executed for the serial command runner.
:type command: Callable[[SerialCommandRunner], None]
:return: None
"""
info(info_text)
self._delete_console()
if not self.__device_path:
error('No device selected!')
self.console.console_text.insert("end", '[ERROR] No device selected!\n', "error")
return
self._disable_buttons()
self.console.console_text.insert("end", f'[INFO] {info_text}...\n', "info")
runner = SerialCommandRunner()
command(runner)
def _handler_toplevel_serial_debug(self) -> None:
"""
Handles the creation or focus of a top-level debug window.
:return: None
"""
self._run_serial_task(
info_text="Start Serial debugging",
command=lambda runner: runner.get_debug(
port=self.__device_path,
callback=lambda output: self._handle_serial_output(output)
)
)
def _get_version(self) -> None:
"""
Triggers a task to get the MicroPython version and process its output.
:return: None
"""
self._run_serial_task(
info_text="Getting MicroPython version",
command=lambda runner: runner.get_version(
port=self.__device_path,
callback=lambda output: self._handle_serial_output(output)
)
)
def _get_structure(self) -> None:
"""
Triggers a task to get the file structure and process its output.
:return: None
"""
self._run_serial_task(
info_text="Getting device structure",
command=lambda runner: runner.get_structure(
port=self.__device_path,
callback=lambda output: self._handle_serial_output(output, "structure")
)
)
def _handle_esptool_output(self, text: str) -> None:
"""
Handles the output by scheduling a task for posting the text to the console queue.
:param text: The text to be posted to the console queue.
:type text: str
:return: None
"""
self.after(0, lambda: self._console_queue.put(text))
def _handle_esptool_error(self, text: str) -> None:
"""
Handles errors by updating the console text widget with the provided error message.
:param text: The error message to be displayed in the console.
:type text: str
:return: None
"""
self.after(0, lambda: self.console.console_text.insert("end", f'[ERROR] {text}\n', "error"))
def _handle_esptool_complete(self) -> None:
"""
Handles the completion of a specific task by enabling buttons.
:return: None
"""
self.after(0, lambda: self._enable_buttons())
def _esptool_command(self, command_name: str) -> None:
"""
Validate and prepares a simple esptool command based on user input.
:param command_name: The name of the command to be executed.
:type command_name: str
:return: None
"""
info(f'Prepare esptool command for: {command_name}')
self._delete_console()
allowed_commands = {"chip_id", "flash_id", "read_mac", "read_flash_status", "erase_flash"}
if command_name not in allowed_commands:
error(f'Invalid command: {command_name}')
self.console.console_text.insert("end", f'[ERROR] Invalid command: {command_name}\n', "error")
return
if not self.__device_path:
error('No device selected!')
self.console.console_text.insert("end", '[ERROR] No device selected!\n', "error")
return
chip = self.__selected_chip if self.__selected_chip else "auto"
cmd = ["python", "-m", "esptool",
"-c", chip,
"-p",
self.__device_path,
command_name]
self._disable_buttons()
self.console.console_text.insert("end", f'[INFO] {" ".join(cmd)}\n\n', "info")
self.esptool_runner.run_threaded_command(command=cmd)
def _flash_firmware_command(self) -> None:
"""
Validate and prepares the esptool flash firmware command based on user input.
:return: None
"""
info('Prepare esptool command for: firmware flash')
self._delete_console()
errors = []
if not self.__device_path:
errors.append('No device path selected')
if not self.__selected_chip:
errors.append('No chip selected')
if not self.__selected_firmware:
errors.append('No firmware selected')
if not self.__selected_baudrate:
errors.append('No baudrate selected')
if not self.flash_firmware.sector_input.get().strip():
errors.append('No sector value provided')
if errors:
error(f'Found errors: {errors}')
self.console.console_text.insert("end", f'[ERROR] {", ".join(errors)}\n', "error")
return
cmd = ["python", "-m", "esptool",
'-p', self.__device_path,
'-c', self.__selected_chip,
'-b', str(self.__selected_baudrate),
'write_flash', self.flash_firmware.sector_input.get().strip(),
self.__selected_firmware]
if self.__expert_mode:
flash_mode = self.flash_firmware.flash_mode_option.get().strip()
flash_freq = self.flash_firmware.flash_frequency_option.get().strip()
flash_size = self.flash_firmware.flash_size_option.get().strip()
expert_args = ['-fm', flash_mode, '-ff', flash_freq, '-fs', flash_size]
if self.flash_firmware.erase_before_switch.get():
expert_args.append('-e')
index = cmd.index('write_flash') + 1
cmd = cmd[:index] + expert_args + cmd[index:]
self._disable_buttons()
self.console.console_text.insert("end", f'[INFO] {" ".join(cmd)}\n\n', "info")
self.esptool_runner.run_threaded_command(command=cmd)