Skip to content

Commit 364dd6d

Browse files
committed
Built-in esptool flash (via Pythonx)
Optional use built-in esptool install via Pythonx. Signed-off-by: Peter M <[email protected]>
1 parent cbd2fb6 commit 364dd6d

File tree

2 files changed

+65
-3
lines changed

2 files changed

+65
-3
lines changed

Diff for: lib/mix/tasks/esp32_flash.ex

+62-2
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,68 @@ defmodule Mix.Tasks.Atomvm.Esp32.Flash do
5959

6060
tool_args = if port == "auto", do: tool_args, else: ["--port", port] ++ tool_args
6161

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
64124
end
65125

66126
defp get_esptool_path(<<"">>) do

Diff for: mix.exs

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ defmodule ExAtomVM.MixProject do
2121
# Run "mix help deps" to learn about dependencies.
2222
defp deps do
2323
[
24-
{:uf2tool, "1.1.0"}
24+
{:uf2tool, "1.1.0"},
25+
{:pythonx, "~> 0.4.0", runtime: false, optional: true}
26+
2527
# {:dep_from_hexpm, "~> 0.3.0"},
2628
# {:dep_from_git, git: "https://github.com/elixir-lang/my_dep.git", tag: "0.1.0"}
2729
]

0 commit comments

Comments
 (0)