|
| 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 | +import uPy_IDE.esptool as 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_horiz=Pack(direction=ROW,padding=15) |
| 39 | + box_style_verti=Pack(direction=COLUMN,padding=15) |
| 40 | + |
| 41 | + #selections |
| 42 | + self.portselect=toga.Selection(items=serial_ports()) |
| 43 | + self.chipselect=toga.Selection(items=["ESP8266","ESP32"], on_select=self.update_selections) |
| 44 | + 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"]) |
| 45 | + |
| 46 | + #puerto |
| 47 | + self.port = None |
| 48 | + self.port_open=False |
| 49 | + |
| 50 | + #switchs |
| 51 | + self.switchdio=toga.Switch('DIO', is_on=False, style=Pack(padding_left=10,padding_top=5)) |
| 52 | + |
| 53 | + #textinputs |
| 54 | + self.textfile=toga.TextInput(style=Pack(flex=1,width=200)) |
| 55 | + self.commandesp=toga.TextInput(style=Pack(flex=1,width=450)) |
| 56 | + |
| 57 | + #intento de terminal |
| 58 | + self.textterminal=toga.MultilineTextInput(readonly=False,style=Pack(flex=1,width=600,height=600)) |
| 59 | + |
| 60 | + #textoutputs |
| 61 | + self.textoutputs=toga.MultilineTextInput(readonly=True,style=Pack(flex=1,width=200,height=200)) |
| 62 | + |
| 63 | + #botones |
| 64 | + self.btnport=toga.Button("Abrir puerto", on_press=self.open_port, style=Pack(padding=2)) |
| 65 | + |
| 66 | + |
| 67 | + self.filelabel=toga.Label("No ha seleccionado ningun archivo", style=Pack(padding=2)) |
| 68 | + self.fname=None |
| 69 | + self.main_window.content=toga.Box( |
| 70 | + children=[ |
| 71 | + toga.Box(style=box_style_verti, children=[ |
| 72 | + |
| 73 | + toga.Box(style=Pack(direction=ROW,padding_left=25), children=[ |
| 74 | + self.portselect, |
| 75 | + self.chipselect, |
| 76 | + self.verselect, |
| 77 | + self.switchdio |
| 78 | + ]), |
| 79 | + |
| 80 | + toga.Box(style=Pack(direction=COLUMN,padding_top=7), children=[ |
| 81 | + toga.Button("Ver archivos en ESP", on_press=self.read_files, style=Pack(padding_top=15,padding_left=2)), |
| 82 | + toga.Button("Seleccionar archivo", on_press=self.action_open_file_dialog, style=Pack(padding=2)), |
| 83 | + self.filelabel, |
| 84 | + toga.Button("Ejecutar archivo en ESP", on_press=self.run_in_esp, style=Pack(padding=2)), |
| 85 | + toga.Button("Grabar archivo en ESP", on_press=self.save_to_esp, style=Pack(padding=2)), |
| 86 | + self.textfile, |
| 87 | + toga.Button("Borrar archivo de ESP", on_press=self.erase_from_esp, style=Pack(padding=2)), |
| 88 | + toga.Button("Obtener archivo de ESP", on_press=self.get_file_esp, style=Pack(padding=2)), |
| 89 | + self.textoutputs |
| 90 | + ]) |
| 91 | + ]), |
| 92 | + toga.Box(style=box_style_verti, children=[ |
| 93 | + toga.Button("Flashear",on_press=self.flash), |
| 94 | + toga.Button("Borrar flash/firmware",on_press=self.eraseflash), |
| 95 | + toga.Button("Actualizar puertos",on_press=self.update_ports), |
| 96 | + self.btnport, |
| 97 | + self.textterminal, |
| 98 | + |
| 99 | + toga.Box(style=Pack(direction=ROW,padding_top=7), children=[ |
| 100 | + self.commandesp, |
| 101 | + toga.Button("Enviar comando", on_press=self.send_command, style=Pack(padding=2)) |
| 102 | + ]) |
| 103 | + |
| 104 | + ]) |
| 105 | + |
| 106 | + ]) |
| 107 | + |
| 108 | + self.main_window.show() |
| 109 | + |
| 110 | + #metodos para la parte de ampy |
| 111 | + def read_files(self,button): |
| 112 | + from uPy_IDE.pyboard import Pyboard |
| 113 | + from uPy_IDE import cli |
| 114 | + from uPy_IDE import files |
| 115 | + |
| 116 | + eboard=files.Files(Pyboard(self.portselect.value)) |
| 117 | + filesesp=eboard.ls() |
| 118 | + print(filesesp) |
| 119 | + lstext="" |
| 120 | + for f in filesesp: |
| 121 | + lstext=lstext+f+"\n" |
| 122 | + self.textoutputs.clear |
| 123 | + self.textoutputs.value=lstext |
| 124 | + |
| 125 | + def action_open_file_dialog(self, widget): |
| 126 | + try: |
| 127 | + self.fname = self.main_window.open_file_dialog( |
| 128 | + title="Open file with Toga", |
| 129 | + ) |
| 130 | + print(self.fname) |
| 131 | + except ValueError: |
| 132 | + print("ha ocurrido un error") |
| 133 | + self.filelabel.text="Archivo seleccionado: "+self.fname.split("/")[-1] |
| 134 | + |
| 135 | + def run_in_esp_thread(self, archiv, disp, terp): |
| 136 | + import uPy_IDE.pyboard as pyboard |
| 137 | + self.textterminal.clear() |
| 138 | + pyboard.execfile(archiv, device=disp,terminal=terp) |
| 139 | + |
| 140 | + def run_in_esp(self,button): |
| 141 | + import threading |
| 142 | + runespthread = threading.Thread(target=self.run_in_esp_thread, args=(self.fname, self.portselect.value, self.textterminal)) |
| 143 | + runespthread.start() |
| 144 | + |
| 145 | + def save_to_esp(self, button): |
| 146 | + from uPy_IDE.pyboard import Pyboard |
| 147 | + from uPy_IDE import cli |
| 148 | + eboard=Pyboard(self.portselect.value) |
| 149 | + cli.put(self.fname,board=eboard) |
| 150 | + |
| 151 | + def erase_from_esp(self,button): |
| 152 | + from uPy_IDE.pyboard import Pyboard |
| 153 | + import uPy_IDE.files as files |
| 154 | + eboard=files.Files(Pyboard(self.portselect.value)) |
| 155 | + eboard.rm(self.textfile.value) |
| 156 | + |
| 157 | + def get_file_esp(self,button): |
| 158 | + from uPy_IDE.pyboard import Pyboard |
| 159 | + import uPy_IDE.files as files |
| 160 | + eboard=files.Files(Pyboard(self.portselect.value)) |
| 161 | + fdata=eboard.get(self.textfile.value) |
| 162 | + self.textterminal.clear() |
| 163 | + self.textterminal.value=fdata |
| 164 | + |
| 165 | +#======================================SOLO MANEJO DE PUERTO======================================================== |
| 166 | + |
| 167 | + def open_port(self,button): |
| 168 | + from uPy_IDE.pyboard import Pyboard |
| 169 | + if not self.port_open: |
| 170 | + self.btnport.label="Cerrar puerto" |
| 171 | + self.port_open=True |
| 172 | + self.textterminal.clear() |
| 173 | + self.port=Pyboard(self.portselect.value) |
| 174 | + self.port.enter_raw_repl() |
| 175 | + read_port(self.port, self.port_open) |
| 176 | + else: |
| 177 | + self.btnport.label="Abrir puerto" |
| 178 | + self.port_open=False |
| 179 | + self.port.exit_raw_repl() |
| 180 | + |
| 181 | + def send_command(self,button): |
| 182 | + if self.port_open: |
| 183 | + print(self.commandesp.value) |
| 184 | + self.port.send(self.commandesp.value) |
| 185 | + |
| 186 | +#=================================================================================================================== |
| 187 | + #metodos para la parte de esptool |
| 188 | + def flash(self,button): |
| 189 | + port=self.portselect.value |
| 190 | + chip=self.chipselect.value |
| 191 | + ver=self.verselect.value |
| 192 | + |
| 193 | + if chip == "ESP32": |
| 194 | + esptool.main(["--chip","esp32","--port",self.portselect.value,"write_flash","-z","0x1000","uPy_IDE/esp32/"+ver+'.bin']) |
| 195 | + elif chip == "ESP8266": |
| 196 | + if self.switchdio.is_on: |
| 197 | + esptool.main(["--port",self.portselect.value,"--baud","460800","write_flash","--flash_size=detect","0","uPy_IDE/esp8266/"+ver+'.bin']) |
| 198 | + else: |
| 199 | + esptool.main(["--port",self.portselect.value,"--baud","460800","write_flash","--flash_size=detect","-fm","dio","0","uPy_IDE/esp8266/"+ver+'.bin']) |
| 200 | + |
| 201 | + def update_ports(self, button): |
| 202 | + portlist = serial_ports() |
| 203 | + if not portlist: |
| 204 | + pass |
| 205 | + else: |
| 206 | + self.portselect.items = portlist |
| 207 | + |
| 208 | + def update_selections(self,button): |
| 209 | + micro=self.chipselect.value |
| 210 | + if micro == "ESP32": |
| 211 | + versionlist=["v1.9.4","v1.10"] |
| 212 | + elif micro=="ESP8266": |
| 213 | + versionlist=["v1.8.7","v1.9.0","v1.9.1","v1.9.2","v1.9.3","v1.9.4","v1.10.0"] |
| 214 | + else: |
| 215 | + pass |
| 216 | + self.verselect.items = versionlist |
| 217 | + |
| 218 | + |
| 219 | + def eraseflash(self,button): |
| 220 | + |
| 221 | + port=self.portselect.value |
| 222 | + chip=self.chipselect.value |
| 223 | + if chip=='ESP32': |
| 224 | + esptool.main(["-p",self.portselect.value,"erase_flash"]) |
| 225 | + elif chip=='ESP8266': |
| 226 | + esptool.main(["-p",self.portselect.value,"erase_flash"]) |
| 227 | + |
| 228 | +def read_port_thread(port,port_status): |
| 229 | + while True: |
| 230 | + if port_status: |
| 231 | + ans=port.read_until(1, b'\x04', timeout=10, data_consumer=None) |
| 232 | + print(ans) |
| 233 | + |
| 234 | +def read_port(port, portstatus): |
| 235 | + import threading |
| 236 | + runportthread = threading.Thread(target=read_port_thread, args=(port, portstatus)) |
| 237 | + runportthread.start() |
| 238 | + |
| 239 | +def main(): |
| 240 | + return uPyIDE("uPyIDE","org.funpython.upyide") |
0 commit comments