|
| 1 | +import toga |
| 2 | +from toga.style import Pack |
| 3 | +from toga.constants import COLUMN, ROW |
| 4 | +import os |
| 5 | +import sys |
| 6 | +import glob |
| 7 | +import serial |
| 8 | +import time |
| 9 | +from uPyIDE import esptool |
| 10 | + |
| 11 | +def serial_ports(): |
| 12 | + if sys.platform.startswith('win'): |
| 13 | + ports = ['COM%s' % (i + 1) for i in range(256)] |
| 14 | + elif sys.platform.startswith('linux') or sys.platform.startswith('cygwin'): |
| 15 | + ports = glob.glob('/dev/tty[A-Za-z]*') |
| 16 | + elif sys.platform.startswith('darwin'): |
| 17 | + ports = glob.glob('/dev/tty.*') |
| 18 | + else: |
| 19 | + raise EnvironmentError('Unsupported platform') |
| 20 | + |
| 21 | + result = [] |
| 22 | + for port in ports: |
| 23 | + try: |
| 24 | + s = serial.Serial(port) |
| 25 | + s.close() |
| 26 | + result.append(port) |
| 27 | + except (OSError, serial.SerialException): |
| 28 | + pass |
| 29 | + return result |
| 30 | + |
| 31 | +class uPyIDE(toga.App): |
| 32 | + |
| 33 | + def startup(self): |
| 34 | + |
| 35 | + self.main_window=toga.MainWindow(title=self.name,size=(640,400)) |
| 36 | + |
| 37 | + label_style=Pack(flex=1,padding_right=24) |
| 38 | + box_style=Pack(direction=ROW,padding=10) |
| 39 | + |
| 40 | + self.portselect=toga.Selection(items=serial_ports()) |
| 41 | + self.chipselect=toga.Selection(items=["ESP8266","ESP32"], on_select=self.update_selections) |
| 42 | + self.verselect=toga.Selection(items=["v1.8.7","v1.9.0","v1.9.1","v1.9.2","v1.9.3","v1.9.4","v1.10.0"]) |
| 43 | + |
| 44 | + self.filelabel=toga.Label("No ha seleccionado ningun archivo") |
| 45 | + self.fname=None |
| 46 | + self.main_window.content=toga.Box( |
| 47 | + children=[ |
| 48 | + |
| 49 | + toga.Box(style=box_style, children=[ |
| 50 | + self.portselect, |
| 51 | + self.chipselect, |
| 52 | + self.verselect |
| 53 | + ]), |
| 54 | + |
| 55 | + toga.Box(style=box_style, children=[ |
| 56 | + toga.Button("Flashear",on_press=self.flash), |
| 57 | + toga.Button("Borrar flash/firmware",on_press=self.eraseflash), |
| 58 | + toga.Button("Actualizar puertos",on_press=self.update_ports) |
| 59 | + ]), |
| 60 | + |
| 61 | + toga.Box(style=box_style, children=[ |
| 62 | + toga.Button("Seleccionar archivo",on_press=self.action_open_file_dialog), |
| 63 | + self.filelabel, |
| 64 | + toga.Button("Grabar archivo en ESP", on_press=self.save_esp) |
| 65 | + ]) |
| 66 | + ]) |
| 67 | + self.main_window.show() |
| 68 | + |
| 69 | + |
| 70 | + def save_esp(self, button): |
| 71 | + command="python3.6 cli.py -p "+self.portselect.value+" put "+fname |
| 72 | + os.system(command) |
| 73 | + |
| 74 | + |
| 75 | + def flash(self,button): |
| 76 | + import os |
| 77 | + port=self.portselect.value |
| 78 | + chip=self.chipselect.value |
| 79 | + ver=self.verselect.value |
| 80 | + |
| 81 | + if chip == "ESP32": |
| 82 | + command = 'python3.6 esptool.py --chip esp32 --port '+port+' write_flash -z 0x1000 esp32/'+ver+'.bin' |
| 83 | + os.system(command) |
| 84 | + elif chip == "ESP8266": |
| 85 | + command = 'python3.6 esptool.py --port '+port+' --baud 460800 write_flash --flash_size=detect 0 esp8266/'+ver+'.bin' |
| 86 | + os.system(command) |
| 87 | + |
| 88 | + def update_ports(self, button): |
| 89 | + portlist = serial_ports() |
| 90 | + if not portlist: |
| 91 | + pass |
| 92 | + else: |
| 93 | + self.portselect.items = portlist |
| 94 | + |
| 95 | + def update_selections(self,button): |
| 96 | + micro=self.chipselect.value |
| 97 | + if micro == "ESP32": |
| 98 | + versionlist=["v1.9.4","v1.10"] |
| 99 | + elif micro=="ESP8266": |
| 100 | + versionlist=["v1.8.7","v1.9.0","v1.9.1","v1.9.2","v1.9.3","v1.9.4","v1.10.0"] |
| 101 | + else: |
| 102 | + pass |
| 103 | + self.verselect.items = versionlist |
| 104 | + |
| 105 | + |
| 106 | + def eraseflash(self,button): |
| 107 | + import os |
| 108 | + port=self.portselect.value |
| 109 | + chip=self.chipselect.value |
| 110 | + if chip=='ESP32': |
| 111 | + command='python3.6 esptool.py --chip esp32 erase_flash' |
| 112 | + os.system(command) |
| 113 | + elif chip=='ESP8266': |
| 114 | + command='python3.6 esptool.py --port '+port+' erase_flash' |
| 115 | + os.system(command) |
| 116 | + |
| 117 | + def action_open_file_dialog(self, widget): |
| 118 | + try: |
| 119 | + self.fname = self.main_window.open_file_dialog( |
| 120 | + title="Open file with Toga", |
| 121 | + ) |
| 122 | + print(self.fname) |
| 123 | + except ValueError: |
| 124 | + print("ha ocurrido un error") |
| 125 | + self.filelabel.text="Archivo seleccionado: "+self.fname.split("/")[-1] |
| 126 | + |
| 127 | +def main(): |
| 128 | + return uPyIDE("uPyIDE","org.funpython.upyide") |
0 commit comments