nerves_examples
nerves_examples copied to clipboard
Use delux in blinky
Replace nerves_leds with delux in the blinky
example.
Do we still need the hello_leds
example as well?
As I quickly compare blinky
and hello_leds
, only remarkable difference is how blinking loop is implemented in the top-level module:
-
blinky
- loop and intervals are managed by our code -
hello_leds
- loop and intervals are managed byNerves.Leds
Other than that, they look nearly identical to me.
❯ diff -u blinky/lib/blinky.ex hello_leds/lib/hello_leds.ex
--- blinky/lib/blinky.ex 2022-08-16 19:56:22.000000000 -0400
+++ hello_leds/lib/hello_leds.ex 2022-08-16 19:58:29.000000000 -0400
@@ -1,9 +1,9 @@
-defmodule Blinky do
+defmodule HelloLeds do
@moduledoc """
Simple example to blink a list of LEDs forever.
- The list of LEDs is platform-dependent, and defined in the config directory
- (see config.exs). See README.md for build instructions.
+ The list of LEDs is platform-dependent, and defined in the config
+ directory (see config.exs). See README.md for build instructions.
"""
# Durations are in milliseconds
@@ -14,24 +14,30 @@
require Logger
def start(_type, _args) do
- led_list = Application.get_env(:blinky, :led_list)
+ led_list = Application.get_env(:hello_leds, :led_list)
Logger.debug("list of leds to blink is #{inspect(led_list)}")
- spawn(fn -> blink_list_forever(led_list) end)
+ Enum.each(led_list, &start_blink(&1))
{:ok, self()}
end
- # call blink_led on each led in the list sequence, repeating forever
- defp blink_list_forever(led_list) do
- Enum.each(led_list, &blink(&1))
- blink_list_forever(led_list)
- end
-
- # given an led key, turn it on for @on_duration then back off
- defp blink(led_key) do
- # Logger.debug "blinking led #{inspect led_key}"
- Leds.set([{led_key, true}])
- :timer.sleep(@on_duration)
- Leds.set([{led_key, false}])
- :timer.sleep(@off_duration)
+ # Set led `led_key` to the state defined below. It is also possible
+ # to globally define states in `config/config.exs` by passing a list
+ # of states with the `:states` keyword.
+ #
+ # The first parameter must be an atom.
+ @spec start_blink(Keyword.T) :: true
+ defp start_blink(led_key) do
+ Logger.debug("blinking led #{inspect(led_key)}")
+ # led_key is a variable that contains an atom
+ Leds.set([
+ {
+ led_key,
+ [
+ trigger: "timer",
+ delay_off: @off_duration,
+ delay_on: @on_duration
+ ]
+ }
+ ])
end
end
Probably we no longer need hello_leds
.
Once we know we keep only one of them, the name of this example can be either blinky
or hello_leds
.
Looks great! I squashed some commits together and merged!