Add AtomVM support to gleam CLI #4222
Replies: 4 comments 3 replies
-
See this PR at AtomVM that introduces a basic example which can be flashed on ESP32: ![]() |
Beta Was this translation helpful? Give feedback.
-
What a lovely suggestion! This is fab. Are there any particular differences between the C and Erlang versions of the PackBEAM program? Is one preferred in some way?
Gleam's tooling has a convenience for running a main function, but you can use any entrypoint you wish (though it would be more irritating to run). Having proper release support is something I want to have in future.
I'd like to defer this one to start and focus on the more Gleam specific parts.
What a fun idea, and potentially quite useful. Would that not work on Windows? That's one of our core supported platforms. Are the APIs that one would need to use here stable? Or would there be any changes in future? |
Beta Was this translation helpful? Give feedback.
-
I would love this, just got a microcontroller started with AtomVM and immediately thought it would be amazing to write gleam code for it :) |
Beta Was this translation helpful? Give feedback.
-
Hi, |
Beta Was this translation helpful? Give feedback.
-
Using specific rebar3 or mix plugins, Erlang and Elixir users can easily generate .avm or .uf2 files for their Erlang or Elixir app that can be flashed to microcontrollers, and they can also easily flash ESP32 and Pico directly from their Erlang or Elixir project.
Similar support would be welcome to help Gleam users run Gleam on microcontrollers.
Four steps can be defined:
1. Generate .avm and .uf2 files
Add one or two new export targets to generate
.avm
or.uf2
or both..avm
files are binaries that are flashed on microcontrollers, including stm32 and esp32. They can also be executed on Generic Unix platforms by atomvm CLI..uf2
files are used for Raspberry Pi Pico and really are packed.avm
with flash address information. An UF2 can be generated from an.avm
using uf2tool..avm
files are generated by AtomVM's PackBeam which exists in two flavors:The new export targets could be invoked such as:
gleam export atomvm
or
gleam export atomvm-shipment
or
gleam export atomvm-avm
or
gleam export atomvm-uf2
depending on scenarios and naming taste.
2. Fix runtime
Gleam has a nice very strong requirement about
main/0
function. Unfortunately, AtomVM usesstart/0
which isn't configurable. This may change, but rather in favor of Erlang releases.AtomVM 0.6 and current main branch search for the first
start/0
exported function and calls this on startup.As a result, the following code seems to work if no other module define start.
With item 1 above, users will have to add this function. Noticeably, invoking
start/0
is quite frequent with Erlang, it is the shortcut with erl -s (erl -s module
will callmodule:start()
, whileerl -s module func
will callmodule:func()
).Gleam's
erlang-shipment
uses a template script for sh(1) or PowerShell that invokes runtime differently, by evaluating$PACKAGE@@main:run($PACKAGE)
. Such invocation could be put in a specific module (entrypoint.erl) or even the ``$PACKAGE@@main` module within a start/0 function doing exactly this. It may also help with regular erlang shipments.The trick here would be to put this module first when generating the .avm file or declare it as the start module if using the Erlang version of PackBEAM.
3. Allow users to directly flash using
gleam
CLI.More gleam CLI commands could be added, including commands to flash the avm or the .uf2 files directly to the devices. This could invoke esptool following rebar3 plugin.
The syntax could be something like:
gleam atomvm esp32_flash -p /dev/cu.usb-something -b 115200
4. Gleam runs atomvm
Additionally, a new runtime (target in gleam parlance) could be defined so that
gleam run --target atomvm
would invoke atomvm on Generic Unix, with atomvmlibs, to run the project (or tests). The example above doesn't run using Erlang because atomvmlibs is missing andatomvm:platform()
which is what is eventually invoked throughgleam_avm/atomvm.platform
isn't defined on BEAM.Beta Was this translation helpful? Give feedback.
All reactions