Nerves.Leds v0.8.0 Nerves.Leds

A convenient interface to setting LEDs defined in /sys/class/leds.

While an application could write directly to /sys/class/leds, the main advantage of using this module is to provide a layer of abstraction that allows easily defining application-specific led names and states, like this:

alias Nerves.Leds

Leds.set power: true                            # turn on the led we called "power"
Leds.set power: :slowblink                      # make it blink slowly
Leds.set connected: false, alert: :fastblink    # set multiple LED states at once

## alernate syntax via set/2

Leds.set :power, :slowblink

Configuration

Use config.exs in your application to create a friendly name that maps to an entry in /sys/class/leds that make sense for your application.

A trivial example for Raspberry Pi:

# in your app's config/config.exs:
config :nerves_leds, names: [ red: "led0", green: "led1" ]

A more useful example for the Alix 2D boards implementing a router:

# in your app's config/config.exs:
config :nerves_leds, names: [
  power:     "alix:1",
  connected: "alix:2",
  alert:     "alix:3"
]

Included states

In addition to true (on) and false (off) the following atoms provide predefined behaviors:

  • :slowblink - turns on and off slowly (about twice a second)
  • :fastblink - turns on and off rapidly (about 7.5 times a second)
  • :slowwink - mostly on, but “winks off” once every second or so
  • :hearbeat - a heartbeat pattern

Customizing states

The standard LED states are defined as @predefined_states near the top of lib/nerves_leds.ex. You can change or add to them using config.exs as follows:

config :nerves_leds, states: [
    fastblink: [ trigger: "timer", delay_off: 40, delay_on: 30 ],
    blip: [ trigger: "timer", delay_off: 1000, delay_on: 100 ]]

See the Linux documentation on sys/class/leds to understand the meaning of trigger, delay, brightness, and other settings.

Summary

Functions

Set states of one or more LEDs by using their mapped name

Set the state of a single LED

Functions

set(settings)
set(Keyword.t) :: true

Set states of one or more LEDs by using their mapped name

  Nerves.Leds.set power: true, error: :fastblink
set(led, state)
set(atom | binary, atom | Keyword.t) :: true

Set the state of a single LED

Nerves.Leds.set :power, true
Nerves.Leds.set :backlight, brightness: 200

Note that unlike set/1, this allows optionally directly naming the file of the led in /sys/class/leds by using a string name rather than an atom.

Nerves.Leds.set "led2", [ trigger: "timer", delay_on: 1000, delay_off: 1000 ]