1010import images as images
1111from serial import SerialException
1212from serial .tools import list_ports
13- from esptool import ESPROM
13+ from esptool import ESPLoader
14+ from esptool import NotImplementedInROMError
1415from argparse import Namespace
1516
16- __version__ = "1.0.1 "
17+ __version__ = "2.0-beta "
1718__supported_baud_rates__ = [9600 , 57600 , 74880 , 115200 , 230400 , 460800 , 921600 ]
1819
1920# ---------------------------------------------------------------------------
2324class RedirectText :
2425 def __init__ (self , text_ctrl ):
2526 self .__out = text_ctrl
26- self .__pending_backspaces = 0
2727
2828 def write (self , string ):
29- new_string = ""
30- number_of_backspaces = 0
31- for c in string :
32- if c == "\b " :
33- number_of_backspaces += 1
34- else :
35- new_string += c
36-
37- if self .__pending_backspaces > 0 :
38- # current value minus pending backspaces plus new string
39- new_value = self .__out .GetValue ()[:- 1 * self .__pending_backspaces ] + new_string
29+ if string .startswith ("\r " ):
30+ # carriage return -> remove last line i.e. reset position to start of last line
31+ current_value = self .__out .GetValue ()
32+ last_newline = current_value .rfind ("\n " )
33+ new_value = current_value [:last_newline + 1 ] # preserve \n
34+ new_value += string [1 :] # chop off leading \r
4035 wx .CallAfter (self .__out .SetValue , new_value )
4136 else :
42- wx .CallAfter (self .__out .AppendText , new_string )
43-
44- self .__pending_backspaces = number_of_backspaces
37+ wx .CallAfter (self .__out .AppendText , string )
4538
4639 def flush (self ):
4740 None
@@ -59,24 +52,34 @@ def __init__(self, parent, config):
5952
6053 def run (self ):
6154 try :
62- esp = ESPROM (port = self ._config .port )
55+ initial_baud = min (ESPLoader .ESP_ROM_BAUD , self ._config .baud )
56+ esp = ESPLoader .detect_chip (self ._config .port , initial_baud )
57+ esp = esp .run_stub ()
58+ if self ._config .baud > initial_baud :
59+ try :
60+ esp .change_baud (self ._config .baud )
61+ except NotImplementedInROMError :
62+ print ("WARNING: ROM doesn't support changing baud rate. Keeping initial baud rate %d" % initial_baud )
63+
64+ args = Namespace ()
65+ args .flash_size = "detect"
66+ args .flash_mode = self ._config .mode
67+ args .flash_freq = "40m"
68+ args .no_progress = False
69+ args .no_stub = False
70+ args .verify = False # TRUE is deprecated
71+ args .compress = True
72+ args .addr_filename = [[int ("0x00000" , 0 ), open (self ._config .firmware_path , 'rb' )]]
73+
74+ if self ._config .erase_before_flash :
75+ esptool .erase_flash (esp , args )
76+ esptool .write_flash (esp , args )
77+
78+ self ._parent .log_message ("Hard resetting..." ) # replicate behavior from esptool.py:2111
79+ esp .hard_reset ()
6380 except SerialException as e :
6481 self ._parent .report_error (e .strerror )
6582 raise e
66- args = Namespace ()
67- args .flash_size = "detect"
68- args .flash_mode = self ._config .mode
69- args .flash_freq = "40m"
70- args .no_progress = False
71- args .verify = True
72- args .baud = self ._config .baud
73- args .addr_filename = [[int ("0x00000" , 0 ), open (self ._config .firmware_path , 'rb' )]]
74- # needs connect() before each operation, see https://github.com/espressif/esptool/issues/157
75- if self ._config .erase_before_flash :
76- esp .connect ()
77- esptool .erase_flash (esp , args )
78- esp .connect ()
79- esptool .write_flash (esp , args )
8083
8184# ---------------------------------------------------------------------------
8285
@@ -326,6 +329,9 @@ def _on_help_about(self, event):
326329 def report_error (self , message ):
327330 self .console_ctrl .SetValue (message )
328331
332+ def log_message (self , message ):
333+ self .console_ctrl .AppendText (message )
334+
329335# ---------------------------------------------------------------------------
330336
331337
0 commit comments