mirror of
https://github.com/esphome/esphome-flasher.git
synced 2025-01-22 21:31:25 +01:00
207 lines
8.1 KiB
Python
207 lines
8.1 KiB
Python
# This GUI is a fork of the brilliant https://github.com/marcelstoer/nodemcu-pyflasher
|
|
|
|
import sys
|
|
import threading
|
|
|
|
import wx
|
|
import wx.adv
|
|
from wx.lib.embeddedimage import PyEmbeddedImage
|
|
import wx.lib.inspection
|
|
import wx.lib.mixins.inspection
|
|
|
|
from esphomeflasher.helpers import list_serial_ports
|
|
|
|
|
|
# See discussion at http://stackoverflow.com/q/41101897/131929
|
|
class RedirectText:
|
|
def __init__(self, text_ctrl):
|
|
self._out = text_ctrl
|
|
|
|
def write(self, string):
|
|
if string.startswith("\r"):
|
|
# carriage return -> remove last line i.e. reset position to start of last line
|
|
current_value = self._out.GetValue()
|
|
last_newline = current_value.rfind("\n")
|
|
new_value = current_value[:last_newline + 1] # preserve \n
|
|
new_value += string[1:] # chop off leading \r
|
|
wx.CallAfter(self._out.SetValue, new_value)
|
|
else:
|
|
wx.CallAfter(self._out.AppendText, string)
|
|
|
|
def flush(self):
|
|
pass
|
|
|
|
|
|
class FlashingThread(threading.Thread):
|
|
def __init__(self, parent, firmware, port):
|
|
threading.Thread.__init__(self)
|
|
self.daemon = True
|
|
self._parent = parent
|
|
self._firmware = firmware
|
|
self._port = port
|
|
|
|
def run(self):
|
|
try:
|
|
from esphomeflasher.__main__ import run_esphomeflasher
|
|
|
|
argv = ['esphomeflasher', '--port', self._port, self._firmware]
|
|
run_esphomeflasher(argv)
|
|
except Exception as e:
|
|
print("Unexpected error: {}".format(e))
|
|
raise
|
|
|
|
|
|
class MainFrame(wx.Frame):
|
|
def __init__(self, parent, title):
|
|
wx.Frame.__init__(self, parent, -1, title, size=(700, 650),
|
|
style=wx.DEFAULT_FRAME_STYLE | wx.NO_FULL_REPAINT_ON_RESIZE)
|
|
|
|
self._firmware = None
|
|
self._port = None
|
|
|
|
self._init_ui()
|
|
|
|
sys.stdout = RedirectText(self.console_ctrl)
|
|
|
|
self.SetMinSize((640, 480))
|
|
self.Centre(wx.BOTH)
|
|
self.Show(True)
|
|
|
|
def _init_ui(self):
|
|
def on_reload(event):
|
|
self.choice.SetItems(self._get_serial_ports())
|
|
|
|
def on_clicked(event):
|
|
self.console_ctrl.SetValue("")
|
|
worker = FlashingThread(self, self._firmware, self._port)
|
|
worker.start()
|
|
|
|
def on_select_port(event):
|
|
choice = event.GetEventObject()
|
|
self._port = choice.GetString(choice.GetSelection())
|
|
|
|
def on_pick_file(event):
|
|
self._firmware = event.GetPath().replace("'", "")
|
|
|
|
panel = wx.Panel(self)
|
|
|
|
hbox = wx.BoxSizer(wx.HORIZONTAL)
|
|
|
|
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)
|
|
bmp = 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)
|
|
|
|
button = wx.Button(panel, -1, "Flash ESP")
|
|
button.Bind(wx.EVT_BUTTON, on_clicked)
|
|
|
|
self.console_ctrl = wx.TextCtrl(panel, style=wx.TE_MULTILINE | wx.TE_READONLY | wx.HSCROLL)
|
|
self.console_ctrl.SetFont(
|
|
wx.Font(13, wx.FONTFAMILY_TELETYPE, wx.FONTSTYLE_NORMAL, wx.FONTWEIGHT_NORMAL))
|
|
self.console_ctrl.SetBackgroundColour(wx.BLACK)
|
|
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="Firmware")
|
|
|
|
console_label = wx.StaticText(panel, label="Console")
|
|
|
|
fgs.AddMany([
|
|
port_label, (serial_boxsizer, 1, wx.EXPAND),
|
|
file_label, (file_picker, 1, wx.EXPAND),
|
|
(wx.StaticText(panel, label="")), (button, 1, wx.EXPAND),
|
|
(console_label, 1, wx.EXPAND), (self.console_ctrl, 1, wx.EXPAND)])
|
|
fgs.AddGrowableRow(3, 1)
|
|
fgs.AddGrowableCol(1, 1)
|
|
hbox.Add(fgs, proportion=2, flag=wx.ALL | wx.EXPAND, border=15)
|
|
panel.SetSizer(hbox)
|
|
|
|
def _get_serial_ports(self):
|
|
ports = []
|
|
for port, desc in list_serial_ports():
|
|
ports.append(port)
|
|
if not self._port and ports:
|
|
self._port = ports[0]
|
|
if not ports:
|
|
ports.append("")
|
|
return ports
|
|
|
|
# Menu methods
|
|
def _on_exit_app(self, event):
|
|
self.Close(True)
|
|
|
|
def log_message(self, message):
|
|
self.console_ctrl.AppendText(message)
|
|
|
|
|
|
class App(wx.App, wx.lib.mixins.inspection.InspectionMixin):
|
|
def OnInit(self):
|
|
wx.SystemOptions.SetOption("mac.window-plain-transition", 1)
|
|
self.SetAppName("esphomeflasher")
|
|
|
|
frame = MainFrame(None, "esphomeflasher")
|
|
frame.Show()
|
|
|
|
return True
|
|
|
|
|
|
Exit = PyEmbeddedImage(
|
|
"iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAABGdBTUEAAK/INwWK6QAAABl0"
|
|
"RVh0U29mdHdhcmUAQWRvYmUgSW1hZ2VSZWFkeXHJZTwAAAN1SURBVHjaYvz//z8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