Skip to content

Commit 5054723

Browse files
authored
Merge pull request #24 from mikevanis/master
Drag and drop feature to upload bin file locally
2 parents 01592b3 + 899a883 commit 5054723

File tree

1 file changed

+56
-4
lines changed

1 file changed

+56
-4
lines changed

esptool-python-gui.py

Lines changed: 56 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,23 @@ def upload_from_github(self, serial_port, url):
189189
print(e)
190190
self.callback(3)
191191

192+
def upload_bin(self, serial_port, filename):
193+
# Write to device with esptool
194+
esptool_options[3] = serial_port
195+
esptool_erase_options[3] = serial_port
196+
esptool_options[-1] = filename
197+
try:
198+
print(" ")
199+
print("👇 Please hold down BOOT button 👇")
200+
if self.erase:
201+
esptool_main(esptool_erase_options)
202+
esptool_main(esptool_options)
203+
self.callback(0)
204+
except Exception as e:
205+
print("Error uploading to device!")
206+
print(e)
207+
self.callback(3)
208+
192209
# Update serial list dropdown
193210
def get_serial_list(self):
194211
new_list = self.serial_ports()
@@ -212,6 +229,16 @@ def flush(self):
212229
None
213230

214231

232+
class DropTarget(wx.FileDropTarget):
233+
def __init__(self, frame):
234+
wx.FileDropTarget.__init__(self)
235+
self.frame = frame
236+
237+
def OnDropFiles(self, x, y, filenames):
238+
self.frame.on_file_drop(filenames)
239+
return True
240+
241+
215242
class MainFrame(wx.Frame):
216243

217244
def __init__(self, parent):
@@ -237,6 +264,10 @@ def __init__(self, parent):
237264
menu_bar = wx.MenuBar()
238265
self.SetMenuBar(menu_bar)
239266

267+
# Drag and drop
268+
file_drop_target = DropTarget(self)
269+
self.SetDropTarget(file_drop_target)
270+
240271
root_sizer = wx.BoxSizer(wx.VERTICAL)
241272

242273
self.title_label = wx.StaticText(self, wx.ID_ANY, u"Yo-Yo Machines Firmware Uploader", wx.DefaultPosition,
@@ -366,8 +397,6 @@ def populate_serial_list(self, new_list):
366397
self.serial_choice.AppendItems(self.serial_list)
367398
self.current_serial = self.serial_list[0]
368399
print('Refreshed serial list')
369-
self.esptool_thread.join()
370-
self.esptool_thread = None
371400
self.serial_refresh_button.Enable()
372401

373402
def populate_projects_list(self, new_list):
@@ -399,8 +428,11 @@ def update_status(self, result):
399428
self.debug_button.Enable()
400429
self.serial_refresh_button.Enable()
401430
self.serial_choice.Enable()
402-
self.esptool_thread.join()
403-
self.esptool_thread = None
431+
try:
432+
self.esptool_thread.join()
433+
self.esptool_thread = None
434+
except RuntimeError:
435+
self.esptool_thread = None
404436

405437
def upload_firmware(self):
406438
self.console_text.SetValue("")
@@ -412,6 +444,17 @@ def upload_firmware(self):
412444
url=self.current_project_url)
413445
self.esptool_thread.start()
414446

447+
def upload_bin_file(self, filename):
448+
self.console_text.SetValue("")
449+
self.status_bar.SetStatusText("Uploading firmware...")
450+
wx.CallAfter(self.console_text.AppendText, "Uploading firmware...")
451+
452+
self.esptool_thread = EspToolManager(EspToolManager.upload_bin,
453+
lambda res: self.update_status(res),
454+
port=self.current_serial, erase=self.erase_flash,
455+
url=filename)
456+
self.esptool_thread.start()
457+
415458
def update_serial_list(self):
416459
self.esptool_thread = EspToolManager(EspToolManager.get_serial_list,
417460
lambda new_list: self.populate_serial_list(new_list))
@@ -476,6 +519,15 @@ def on_debug_click(self, event):
476519
self.serial_choice.Disable()
477520
event.Skip()
478521

522+
def on_file_drop(self, filenames):
523+
for f in filenames:
524+
if f.endswith('combined.bin'):
525+
bin_file = f
526+
print("Uploading bin file...")
527+
self.upload_bin_file(bin_file)
528+
return
529+
print("Not a bin file! Will not upload.")
530+
479531

480532
if __name__ == '__main__':
481533
app = wx.App()

0 commit comments

Comments
 (0)