-
Notifications
You must be signed in to change notification settings - Fork 10
Add Elixir ESP NVS example #30
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mnishiguchi
wants to merge
1
commit into
atomvm:master
Choose a base branch
from
mnishiguchi:feat/add-elixir-esp-nvs-example
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+303
−2
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <!--- | ||
| Copyright 2026 Masatoshi Nishiguchi | ||
| SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later | ||
| --> | ||
|
|
||
| # `EspNvs` Application | ||
|
|
||
| Welcome to the `EspNvs` AtomVM application. | ||
|
|
||
| The `EspNvs` AtomVM application uses ESP32 non-volatile storage (NVS) to store a small value. | ||
| If the NVS storage has not been set for this application, it will store a default value once. The application then logs the result and restarts every 10 seconds. | ||
|
|
||
| For more information about programming on the AtomVM platform, see the [AtomVM Programmers Guide](https://doc.atomvm.org/latest/programmers-guide.html). | ||
|
|
||
| For general information about building and executing Elixir AtomVM example programs, see the Elixir example program [README](../README.md). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # | ||
| # This file is part of AtomVM. | ||
| # | ||
| # Copyright 2026 Masatoshi Nishiguchi | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later | ||
| # | ||
|
|
||
| defmodule EspNvs do | ||
| @moduledoc """ | ||
| Demonstrates ESP32 NVS usage by storing a small value once. | ||
| """ | ||
|
|
||
| @compile {:no_warn_undefined, :atomvm} | ||
| @compile {:no_warn_undefined, :esp} | ||
|
|
||
| @nvs_namespace :esp_nvs | ||
| @nvs_key :word | ||
| @default_word "Hello" | ||
| @restart_interval_ms 10_000 | ||
|
|
||
| def start do | ||
| case verify_platform(:atomvm.platform()) do | ||
| :ok -> | ||
| case :esp.nvs_fetch_binary(@nvs_namespace, @nvs_key) do | ||
| {:ok, word} when is_binary(word) -> | ||
| IO.puts("Fetched word from NVS: #{word}") | ||
|
|
||
| {:error, :not_found} -> | ||
| IO.puts("No word found. Storing default word to NVS once: #{@default_word}") | ||
| :ok = :esp.nvs_put_binary(@nvs_namespace, @nvs_key, @default_word) | ||
|
|
||
| _ -> | ||
| IO.puts("Error reading NVS. Skipping write.") | ||
| end | ||
|
|
||
| IO.puts("Restarting in 10 seconds ...") | ||
| Process.sleep(@restart_interval_ms) | ||
| :esp.restart() | ||
|
|
||
| error -> | ||
| error | ||
| end | ||
| end | ||
|
|
||
| defp verify_platform(:esp32), do: :ok | ||
| defp verify_platform(platform), do: {:error, {:unsupported_platform, platform}} | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # | ||
| # This file is part of AtomVM. | ||
| # | ||
| # Copyright 2026 Masatoshi Nishiguchi | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later | ||
| # | ||
|
|
||
| defmodule EspNvs.MixProject do | ||
| use Mix.Project | ||
|
|
||
| def project do | ||
| [ | ||
| app: :esp_nvs, | ||
| version: "0.1.0", | ||
| elixir: "~> 1.17", | ||
| start_permanent: Mix.env() == :prod, | ||
| deps: deps(), | ||
| atomvm: [ | ||
| start: EspNvs, | ||
| flash_offset: 0x250000 | ||
| ] | ||
| ] | ||
| end | ||
|
|
||
| # Run "mix help compile.app" to learn about applications. | ||
| def application do | ||
| [ | ||
| extra_applications: [:logger] | ||
| ] | ||
| end | ||
|
|
||
| # Run "mix help deps" to learn about dependencies. | ||
| defp deps do | ||
| [ | ||
| {:exatomvm, git: "https://github.com/atomvm/ExAtomVM/", branch: "main"} | ||
| ] | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <!--- | ||
| Copyright 2026 Masatoshi Nishiguchi | ||
| SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later | ||
| --> | ||
|
|
||
| # `EspRtc` Application | ||
|
|
||
| Welcome to the `EspRtc` AtomVM application. | ||
|
|
||
| The `EspRtc` AtomVM application uses ESP32 RTC slow memory to record the number of times the device has restarted. | ||
| If no value has been stored in RTC slow memory yet, the counter is initialized to 0. The device will then sleep for 10 seconds and restart. After each restart, the counter is incremented and stored back to RTC slow memory. | ||
|
|
||
| For more information about programming on the AtomVM platform, see the [AtomVM Programmers Guide](https://doc.atomvm.org/latest/programmers-guide.html). | ||
|
|
||
| For general information about building and executing Elixir AtomVM example programs, see the Elixir example program [README](../README.md). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| # | ||
| # This file is part of AtomVM. | ||
| # | ||
| # Copyright 2026 Masatoshi Nishiguchi | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later | ||
| # | ||
|
|
||
| defmodule EspRtc do | ||
| @moduledoc """ | ||
| Demonstrates ESP32 RTC slow memory usage by persisting a restart counter. | ||
| """ | ||
|
|
||
| @compile {:no_warn_undefined, :atomvm} | ||
| @compile {:no_warn_undefined, :esp} | ||
|
|
||
| @restart_delay_ms 10_000 | ||
|
|
||
| defmodule State do | ||
| @moduledoc false | ||
| defstruct count: 0 | ||
| end | ||
|
|
||
| def start do | ||
| case verify_platform(:atomvm.platform()) do | ||
| :ok -> | ||
| load_state() | ||
| |> increment_state() | ||
| |> save_state() | ||
|
|
||
| IO.puts("Restarting in 10 seconds ...") | ||
| Process.sleep(@restart_delay_ms) | ||
| :esp.restart() | ||
|
|
||
| :ok | ||
|
|
||
| error -> | ||
| error | ||
| end | ||
| end | ||
|
|
||
| defp load_state do | ||
| IO.puts("Loading count from RTC slow memory ...") | ||
|
|
||
| encoded_state = | ||
| try do | ||
| :esp.rtc_slow_get_binary() | ||
| catch | ||
| :error, :badarg -> | ||
| IO.puts("This device has not recorded the number of starts. Initializing to 0.") | ||
| :erlang.term_to_binary(%State{}) | ||
| end | ||
|
|
||
| case safe_binary_to_term(encoded_state) do | ||
| {:ok, %State{count: count} = state} when is_integer(count) and count >= 0 -> | ||
| state | ||
|
|
||
| _ -> | ||
| IO.puts("Error: bad value, resetting to 0.") | ||
| %State{} | ||
| end | ||
| end | ||
|
|
||
| defp safe_binary_to_term(encoded_term) when is_binary(encoded_term) do | ||
| try do | ||
| {:ok, :erlang.binary_to_term(encoded_term)} | ||
| catch | ||
| :error, _ -> :error | ||
| end | ||
| end | ||
|
|
||
| defp save_state(current_state) do | ||
| IO.puts("Saving count to RTC slow memory ...") | ||
|
|
||
| current_state | ||
| |> :erlang.term_to_binary() | ||
| |> :esp.rtc_slow_set_binary() | ||
| end | ||
|
|
||
| defp increment_state(%State{count: count} = state) when is_integer(count) and count >= 0 do | ||
| new_count = count + 1 | ||
| IO.puts("This device has restarted #{new_count} time(s).") | ||
| %State{state | count: new_count} | ||
| end | ||
|
|
||
| defp increment_state(_) do | ||
| IO.puts("Error: bad value, resetting to 0.") | ||
| %State{} | ||
| end | ||
|
|
||
| defp verify_platform(:esp32), do: :ok | ||
| defp verify_platform(platform), do: {:error, {:unsupported_platform, platform}} | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # | ||
| # This file is part of AtomVM. | ||
| # | ||
| # Copyright 2026 Masatoshi Nishiguchi | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| # | ||
| # SPDX-License-Identifier: Apache-2.0 OR LGPL-2.1-or-later | ||
| # | ||
|
|
||
| defmodule EspRtc.MixProject do | ||
| use Mix.Project | ||
|
|
||
| def project do | ||
| [ | ||
| app: :esp_rtc, | ||
| version: "0.1.0", | ||
| elixir: "~> 1.17", | ||
| start_permanent: Mix.env() == :prod, | ||
| deps: deps(), | ||
| atomvm: [ | ||
| start: EspRtc, | ||
| flash_offset: 0x250000 | ||
| ] | ||
| ] | ||
| end | ||
|
|
||
| # Run "mix help compile.app" to learn about applications. | ||
| def application do | ||
| [ | ||
| extra_applications: [:logger] | ||
| ] | ||
| end | ||
|
|
||
| # Run "mix help deps" to learn about dependencies. | ||
| defp deps do | ||
| [ | ||
| {:exatomvm, git: "https://github.com/atomvm/ExAtomVM/", branch: "main"} | ||
| ] | ||
| end | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick:
I'm not a fan of a nested module - I would just
defstruct count: 0here and then use%__MODULE__{count: count}or%EspRtcMemory{count: count}?(but certainly a matter of taste, so I'm good with a nested module if that is preferred )