@@ -59,8 +59,68 @@ 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
+ Application . ensure_all_started ( :pythonx )
77
+
78
+ Pythonx . uv_init ( """
79
+ [project]
80
+ name = "project"
81
+ version = "0.0.0"
82
+ requires-python = "==3.13.*"
83
+ dependencies = [
84
+ "esptool==4.9.dev6"
85
+ ]
86
+ """ )
87
+
88
+ { _result , globals } =
89
+ try do
90
+ Pythonx . eval (
91
+ """
92
+ import esptool
93
+ import sys
94
+
95
+ command = [x.decode('utf-8') for x in tool_args]
96
+
97
+ def flash_esp():
98
+ esptool.main(command)
99
+
100
+ if __name__ == "__main__":
101
+ try:
102
+ result = flash_esp()
103
+ result = True
104
+ except SystemExit as e:
105
+ print(f"SystemExit: {e}")
106
+ result = False
107
+ except Exception as e:
108
+ print(f"Warning: {e}")
109
+ result = False
110
+
111
+ """ ,
112
+ % { "tool_args" => tool_args }
113
+ )
114
+ rescue
115
+ e in Pythonx.Error ->
116
+ IO . inspect ( "Pythonx error occurred: #{ inspect ( e ) } " )
117
+ exit ( { :shutdown , 1 } )
118
+ end
119
+
120
+ case Pythonx . decode ( globals [ "result" ] ) do
121
+ true -> exit ( { :shutdown , 0 } )
122
+ false -> exit ( { :shutdown , 1 } )
123
+ end
64
124
end
65
125
66
126
defp get_esptool_path ( << "" >> ) do
0 commit comments