From 0e5e250ade9a9b5a861befb0f4b7336f1a0235d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20St=C3=B6r?= Date: Thu, 22 Nov 2018 21:17:51 +0100 Subject: [PATCH] Reintroduce the serial port dropdown, add auto-select option --- Main.py | 54 +++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/Main.py b/Main.py index dd43c33..b4dd2b2 100644 --- a/Main.py +++ b/Main.py @@ -33,6 +33,8 @@ __flash_help__ = '''

''' +__auto_select__ = "Auto-select" +__auto_select_explanation__ = "(first port with Espressif device)" __supported_baud_rates__ = [9600, 57600, 74880, 115200, 230400, 460800, 921600] # --------------------------------------------------------------------------- @@ -75,7 +77,10 @@ class FlashingThread(threading.Thread): print("esptool.py v%s" % esptool.__version__) initial_baud = min(ESPLoader.ESP_ROM_BAUD, self._config.baud) - esp = self.connect_to_esp(initial_baud) + if self._config.port.startswith(__auto_select__): + esp = self.connect_to_esp(initial_baud) + else: + esp = ESPLoader.detect_chip(self._config.port, initial_baud) print("Chip is %s" % (esp.get_chip_description())) print("Features: %s" % ", ".join(esp.get_chip_features())) @@ -145,6 +150,7 @@ class FlashConfig: self.erase_before_flash = False self.mode = "dio" self.firmware_path = None + self.port = None @classmethod def load(cls, file_path): @@ -152,6 +158,7 @@ class FlashConfig: if os.path.exists(file_path): with open(file_path, 'r') as f: data = json.load(f) + conf.port = data['port'] conf.baud = data['baud'] conf.mode = data['mode'] conf.erase_before_flash = data['erase'] @@ -159,6 +166,7 @@ class FlashConfig: def safe(self, file_path): data = { + 'port': self.port, 'baud': self.baud, 'mode': self.mode, 'erase': self.erase_before_flash, @@ -167,7 +175,7 @@ class FlashConfig: json.dump(data, f) def is_complete(self): - return self.firmware_path is not None + return self.firmware_path is not None and self.port is not None # --------------------------------------------------------------------------- @@ -190,8 +198,13 @@ class NodeMcuFlasher(wx.Frame): self.SetMinSize((640, 480)) self.Centre(wx.BOTH) self.Show(True) + print("Connect your device") + print("\nIf you chose the serial port auto-select feature you might want to ") + print("turn off Bluetooth") def _init_ui(self): + def on_reload(event): + self.choice.SetItems(self._get_serial_ports()) def on_baud_changed(event): radio_button = event.GetEventObject() @@ -216,6 +229,10 @@ class NodeMcuFlasher(wx.Frame): worker = FlashingThread(self, self._config) worker.start() + def on_select_port(event): + choice = event.GetEventObject() + self._config.port = choice.GetString(choice.GetSelection()) + def on_pick_file(event): self._config.firmware_path = event.GetPath().replace("'", "") @@ -225,9 +242,23 @@ class NodeMcuFlasher(wx.Frame): fgs = wx.FlexGridSizer(7, 2, 10, 10) + self.choice = wx.Choice(panel, choices=self._get_serial_ports()) + self.choice.Bind(wx.EVT_CHOICE, on_select_port) + self._select_configured_port() + bmp = images.Reload.GetBitmap() + reload_button = wx.BitmapButton(panel, id=wx.ID_ANY, bitmap=bmp, + size=(bmp.GetWidth() + 7, bmp.GetHeight() + 7)) + reload_button.Bind(wx.EVT_BUTTON, on_reload) + reload_button.SetToolTip("Reload serial device list") + file_picker = wx.FilePickerCtrl(panel, style=wx.FLP_USE_TEXTCTRL) file_picker.Bind(wx.EVT_FILEPICKER_CHANGED, on_pick_file) + serial_boxsizer = wx.BoxSizer(wx.HORIZONTAL) + serial_boxsizer.Add(self.choice, 1, wx.EXPAND) + serial_boxsizer.AddStretchSpacer(0) + serial_boxsizer.Add(reload_button, 0, wx.ALIGN_RIGHT, 20) + baud_boxsizer = wx.BoxSizer(wx.HORIZONTAL) def add_baud_radio_button(sizer, index, baud_rate): @@ -282,6 +313,7 @@ class NodeMcuFlasher(wx.Frame): self.console_ctrl.SetForegroundColour(wx.RED) self.console_ctrl.SetDefaultStyle(wx.TextAttr(wx.RED)) + port_label = wx.StaticText(panel, label="Serial port") file_label = wx.StaticText(panel, label="NodeMCU firmware") baud_label = wx.StaticText(panel, label="Baud rate") flashmode_label = wx.StaticText(panel, label="Flash mode") @@ -309,17 +341,33 @@ class NodeMcuFlasher(wx.Frame): console_label = wx.StaticText(panel, label="Console") fgs.AddMany([ + port_label, (serial_boxsizer, 1, wx.EXPAND), file_label, (file_picker, 1, wx.EXPAND), baud_label, baud_boxsizer, flashmode_label_boxsizer, flashmode_boxsizer, erase_label, erase_boxsizer, (wx.StaticText(panel, label="")), (button, 1, wx.EXPAND), (console_label, 1, wx.EXPAND), (self.console_ctrl, 1, wx.EXPAND)]) - fgs.AddGrowableRow(5, 1) + fgs.AddGrowableRow(6, 1) fgs.AddGrowableCol(1, 1) hbox.Add(fgs, proportion=2, flag=wx.ALL | wx.EXPAND, border=15) panel.SetSizer(hbox) + def _select_configured_port(self): + count = 0 + for item in self.choice.GetItems(): + if item == self._config.port: + self.choice.Select(count) + break + count += 1 + + @staticmethod + def _get_serial_ports(): + ports = [__auto_select__ + " " + __auto_select_explanation__] + for port, desc, hwid in sorted(list_ports.comports()): + ports.append(port) + return ports + def _set_icons(self): self.SetIcon(images.Icon.GetIcon())