@@ -4,6 +4,56 @@ defmodule Nerves.Leds do
4
4
Handles LED blinking/handling in a configurable way, providing an
5
5
easy-to use interface to setting LEDs defined in `/sys/class/leds`.
6
6
7
+ While an application could write directly to /sys/class/leds, the main advantage
8
+ of using this module is to provide a layer of abstraction that allows easily
9
+ defining application-specific led names and states, like this:
10
+
11
+ ```elixir
12
+ alias Nerves.Leds
13
+
14
+ Leds.set power: true # turn on the led we called "power"
15
+ Leds.set power: :slowblink # make it blink slowly
16
+ Leds.set connected: false, alert: :fastblink # set multiple LED states at once
17
+ ```
18
+
19
+ ## Configuration
20
+
21
+ Use config.exs to create a friendly name that maps to an entry in
22
+ `/sys/class/leds` that make sense for your application. An example for the Alix 2D boards:
23
+
24
+ ```elixir
25
+ # in your app's config/config.exs:
26
+ config :nerves_leds, names: [
27
+ power: "alix:1",
28
+ connected: "alix:2",
29
+ alert: "alix:3"
30
+ ]
31
+ ```
32
+
33
+ ## Included states
34
+
35
+ In addition to `true` (on) and `false` (off) the following atoms provide predefined
36
+ behaviors:
37
+
38
+ - `:slowblink` - turns on and off slowly (about twice a second)
39
+ - `:fastblink` - turns on and off rapidly (about 7.5 times a second)
40
+ - `:slowwink` - mostly on, but "winks off" once every second or so
41
+ - `:hearbeat` - a heartbeat pattern
42
+
43
+ ## Customizing states
44
+
45
+ The standard LED states are defined as `@predefined_states` near the top of
46
+ `lib/nerves_leds.ex`. You can change or add to them using config.exs as
47
+ follows:
48
+
49
+ ```elixir
50
+ config :nerves_leds, states: [
51
+ fastblink: [ trigger: "timer", delay_off: 40, delay_on: 30 ],
52
+ blip: [ trigger: "timer", delay_off: 1000, delay_on: 100 ]]
53
+ ```
54
+
55
+ See the Linux documentation on `sys/class/leds` to understand the meaning of
56
+ trigger, delay, brightness, and other settings.
7
57
"""
8
58
9
59
@ app :nerves_leds
@@ -23,13 +73,15 @@ defmodule Nerves.Leds do
23
73
@ led_states Dict . merge ( Application . get_env ( @ app , :states , [ ] ) , @ predefined_states )
24
74
25
75
@ doc """
26
- Set status of one or more leds, like this:
76
+ Set one or more leds to one of the built-in or user-defined states
27
77
28
- ```
29
- Nerves.Leds.set power: true, error: fastblink
30
- ```
78
+ ~~~elixir
79
+ Nerves.Leds.set power: true, error: fastblink
80
+ ~~~
31
81
82
+ See the module overview for information about states and configuration.
32
83
"""
84
+
33
85
@ spec set ( Keyword.T ) :: true
34
86
def set ( settings ) do
35
87
Enum . each settings , & ( set_keyed_state ( & 1 ) )
0 commit comments