@@ -59,8 +59,58 @@ defmodule Mix.Tasks.Atomvm.Esp32.Flash do
59
59
60
60
tool_args = if port == "auto" , do: tool_args , else: [ "--port" , port ] ++ tool_args
61
61
62
- tool_full_path = get_esptool_path ( idf_path )
63
- System . cmd ( tool_full_path , tool_args , stderr_to_stdout: true , into: IO . stream ( :stdio , 1 ) )
62
+ case Code . ensure_loaded ( Pythonx ) do
63
+ { :module , Pythonx } ->
64
+ IO . inspect ( "Flashing using Pythonx installed esptool.." )
65
+ flash_pythonx ( tool_args )
66
+
67
+ _ ->
68
+ IO . inspect ( "Flashing using esptool.." )
69
+ tool_full_path = get_esptool_path ( idf_path )
70
+ System . cmd ( tool_full_path , tool_args , stderr_to_stdout: true , into: IO . stream ( :stdio , 1 ) )
71
+ end
72
+ end
73
+
74
+ defp flash_pythonx ( tool_args ) do
75
+ # https://github.com/espressif/esptool/blob/master/docs/en/esptool/scripting.rst
76
+ ExAtomVM.EsptoolHelper . setup ( )
77
+
78
+ { _result , globals } =
79
+ try do
80
+ Pythonx . eval (
81
+ """
82
+ import esptool
83
+ import sys
84
+
85
+ command = [arg.decode('utf-8') for arg in tool_args]
86
+
87
+ def flash_esp():
88
+ esptool.main(command)
89
+
90
+ if __name__ == "__main__":
91
+ try:
92
+ result = flash_esp()
93
+ result = True
94
+ except SystemExit as e:
95
+ print(f"SystemExit: {e}")
96
+ result = False
97
+ except Exception as e:
98
+ print(f"Warning: {e}")
99
+ result = False
100
+
101
+ """ ,
102
+ % { "tool_args" => tool_args }
103
+ )
104
+ rescue
105
+ e in Pythonx.Error ->
106
+ IO . inspect ( "Pythonx error occurred: #{ inspect ( e ) } " )
107
+ exit ( { :shutdown , 1 } )
108
+ end
109
+
110
+ case Pythonx . decode ( globals [ "result" ] ) do
111
+ true -> exit ( { :shutdown , 0 } )
112
+ false -> exit ( { :shutdown , 1 } )
113
+ end
64
114
end
65
115
66
116
defp get_esptool_path ( << "" >> ) do
0 commit comments