|
| 1 | +if Code.ensure_loaded?(Igniter.Mix.Task) do |
| 2 | +defmodule Mix.Tasks.Exatomvm.Install do |
| 3 | + use Igniter.Mix.Task |
| 4 | + |
| 5 | + @example "mix igniter.new my_project --install exatomvm@github:atomvm/exatomvm && cd my_project" |
| 6 | + |
| 7 | + @shortdoc "Add and configure AtomVM for your project" |
| 8 | + @moduledoc """ |
| 9 | + #{@shortdoc} |
| 10 | +
|
| 11 | + This task sets up your Elixir project to work with AtomVM, adding necessary dependencies |
| 12 | + and configuration for targeting embedded devices like ESP32, Raspberry Pi Pico, and STM32. |
| 13 | +
|
| 14 | + ## Example |
| 15 | +
|
| 16 | + ```bash |
| 17 | + #{@example} |
| 18 | + ``` |
| 19 | +
|
| 20 | + """ |
| 21 | + |
| 22 | + @impl Igniter.Mix.Task |
| 23 | + def info(_argv, _composing_task) do |
| 24 | + %Igniter.Mix.Task.Info{ |
| 25 | + group: :exatomvm, |
| 26 | + adds_deps: [ |
| 27 | + {:pythonx, "~> 0.4.0", runtime: false}, |
| 28 | + {:req, "~> 0.5.0", runtime: false} |
| 29 | + ], |
| 30 | + example: @example |
| 31 | + } |
| 32 | + end |
| 33 | + |
| 34 | + @impl Igniter.Mix.Task |
| 35 | + def igniter(igniter) do |
| 36 | + selected_instructions = |
| 37 | + Igniter.Util.IO.select( |
| 38 | + """ |
| 39 | + About to configure project for AtomVM! |
| 40 | +
|
| 41 | + Which device would you like to see setup instructions for? |
| 42 | + (Your project will be configured for all devices - this only affects the instructions shown): |
| 43 | + """, |
| 44 | + ["ESP32", "Pico", "STM32", "All"], |
| 45 | + default: "All" |
| 46 | + ) |
| 47 | + |
| 48 | + module_name = Igniter.Project.Module.module_name_prefix(igniter) |
| 49 | + |
| 50 | + options = [ |
| 51 | + start: module_name, |
| 52 | + esp32_flash_offset: Sourceror.parse_string!("0x250000"), |
| 53 | + stm32_flash_offset: Sourceror.parse_string!("0x8080000"), |
| 54 | + chip: "auto", |
| 55 | + port: "auto" |
| 56 | + ] |
| 57 | + |
| 58 | + Igniter.Project.MixProject.update(igniter, :project, [:atomvm], fn _zipper -> |
| 59 | + {:ok, {:code, options}} |
| 60 | + end) |
| 61 | + |> Igniter.mkdir("avm_deps") |
| 62 | + |> Igniter.Project.Module.find_and_update_or_create_module( |
| 63 | + module_name, |
| 64 | + """ |
| 65 | + def start do |
| 66 | + IO.inspect("Hello AtomVM!") |
| 67 | + :ok |
| 68 | + end |
| 69 | + """, |
| 70 | + fn zipper -> |
| 71 | + case Igniter.Code.Function.move_to_def(zipper, :start, 0) do |
| 72 | + :error -> |
| 73 | + {:ok, |
| 74 | + Igniter.Code.Common.add_code( |
| 75 | + zipper, |
| 76 | + """ |
| 77 | + def start do |
| 78 | + IO.inspect("Hello AtomVM!") |
| 79 | + :ok |
| 80 | + end |
| 81 | + """, |
| 82 | + placement: :after |
| 83 | + )} |
| 84 | + |
| 85 | + _ -> |
| 86 | + {:ok, zipper} |
| 87 | + end |
| 88 | + end |
| 89 | + ) |
| 90 | + |> Igniter.create_new_file( |
| 91 | + "idf_component.yml", |
| 92 | + Application.app_dir(:exatomvm, "priv/idf_component.yml.example") |> File.read!(), |
| 93 | + on_exists: :skip |
| 94 | + ) |
| 95 | + |> Igniter.Project.Deps.set_dep_option(:exatomvm, :runtime, false) |
| 96 | + |> Igniter.Project.Deps.set_dep_option(:igniter, :runtime, false) |
| 97 | + |> output_instructions(selected_instructions) |
| 98 | + end |
| 99 | + |
| 100 | + defp common_intro do |
| 101 | + """ |
| 102 | + 🎉 Your AtomVM project is now ready! |
| 103 | +
|
| 104 | + Next, you need to install AtomVM itself on your device. |
| 105 | +
|
| 106 | + """ |
| 107 | + end |
| 108 | + |
| 109 | + defp output_instructions(igniter, selected_instructions) |
| 110 | + when selected_instructions == "ESP32" do |
| 111 | + igniter |
| 112 | + |> Igniter.add_notice("ESP32 Setup Instructions") |
| 113 | + |> Igniter.add_notice(""" |
| 114 | + #{common_intro()} |
| 115 | + ## Installing AtomVM on ESP32 |
| 116 | +
|
| 117 | + Choose one of these methods: |
| 118 | +
|
| 119 | + 1. **Using Mix task (recommended):** |
| 120 | + mix atomvm.esp32.install |
| 121 | +
|
| 122 | + 2. **Manual installation:** |
| 123 | + Follow the guide at: https://doc.atomvm.org/main/getting-started-guide.html#flashing-a-binary-image-to-esp32 |
| 124 | +
|
| 125 | + 3. **Web flasher (Chrome browser only):** |
| 126 | + (Choose the Elixir-enabled build of AtomVM.) |
| 127 | + Visit: https://petermm.github.io/atomvm_flasher |
| 128 | +
|
| 129 | + """) |
| 130 | + |> Igniter.add_notice(""" |
| 131 | + ## Flashing Your Project |
| 132 | +
|
| 133 | + Once AtomVM is installed on your device, flash your project with: |
| 134 | +
|
| 135 | + mix atomvm.esp32.flash |
| 136 | +
|
| 137 | + """) |
| 138 | + end |
| 139 | + |
| 140 | + defp output_instructions(igniter, selected_instructions) |
| 141 | + when selected_instructions == "Pico" do |
| 142 | + igniter |
| 143 | + |> Igniter.add_notice("Raspberry Pi Pico Setup Instructions") |
| 144 | + |> Igniter.add_notice(""" |
| 145 | + #{common_intro()} |
| 146 | + ## Installing AtomVM on Raspberry Pi Pico |
| 147 | +
|
| 148 | + Follow the installation guide at: |
| 149 | + https://doc.atomvm.org/main/getting-started-guide.html#flashing-a-binary-image-to-pico |
| 150 | +
|
| 151 | + """) |
| 152 | + |> Igniter.add_notice(""" |
| 153 | + ## Flashing Your Project |
| 154 | +
|
| 155 | + Once AtomVM is installed on your device, flash your project with: |
| 156 | +
|
| 157 | + mix atomvm.pico.flash |
| 158 | +
|
| 159 | + For more details, see: https://github.com/atomvm/exatomvm?tab=readme-ov-file#the-atomvmpicoflash-task |
| 160 | + """) |
| 161 | + end |
| 162 | + |
| 163 | + defp output_instructions(igniter, selected_instructions) |
| 164 | + when selected_instructions == "STM32" do |
| 165 | + igniter |
| 166 | + |> Igniter.add_notice("STM32 Setup Instructions") |
| 167 | + |> Igniter.add_notice(""" |
| 168 | + #{common_intro()} |
| 169 | + ## Building AtomVM for STM32 |
| 170 | +
|
| 171 | + STM32 requires building AtomVM for your specific board: |
| 172 | + https://doc.atomvm.org/main/build-instructions.html#building-for-stm32 |
| 173 | +
|
| 174 | + ## Installing st-link |
| 175 | +
|
| 176 | + You'll need st-link installed for flashing: |
| 177 | + - Installation guide: https://github.com/stlink-org/stlink?tab=readme-ov-file#installation |
| 178 | + - Flashing guide: https://doc.atomvm.org/main/getting-started-guide.html#flashing-a-binary-image-to-stm32 |
| 179 | + """) |
| 180 | + |> Igniter.add_notice(""" |
| 181 | + ## Flashing Your Project |
| 182 | +
|
| 183 | + Once AtomVM is built and installed on your device, flash your project with: |
| 184 | +
|
| 185 | + mix atomvm.stm32.flash |
| 186 | +
|
| 187 | + For more details, see: https://github.com/atomvm/exatomvm?tab=readme-ov-file#the-atomvmstm32flash-task |
| 188 | + """) |
| 189 | + end |
| 190 | + |
| 191 | + defp output_instructions(igniter, selected_instructions) |
| 192 | + when selected_instructions == "All" do |
| 193 | + igniter |
| 194 | + |> output_instructions("ESP32") |
| 195 | + |> output_instructions("Pico") |
| 196 | + |> output_instructions("STM32") |
| 197 | + end |
| 198 | +end |
| 199 | +end |
0 commit comments