Ledx is a simple library for interfacing with LEDs on embedded platforms.
-
Add ledx to your list of dependencies in
mix.exs
:def deps do [{:ledx, "~> 0.0.1"}] end
-
Ensure ledx is started before your application:
def application do [applications: [:ledx]] end
Ledx depends on elixir-ale for interfacing with GPIO. Follow the instructions on getting-started if you are cross-compiling.
You can configure your LEDs easily in your config.exs file.
config :ledx, leds:
[{:led1, :gpio, %{pin: 18}},
{:led2, :gpio, %{pin: 17}}]
This will make :led1
and :led2
be available when your app is started.
You can also start a LED from your code. Just pass the led name, the led type, and the led configuration.
{:ok, _pid} = Ledx.start_led(:my_led, :gpio, %{pin: 18})
Currently only :gpio
is supported, see advanced if you want
to implement your own types.
Given our previously started led, we could turn it on doing:
Ledx.turn_on(:my_led)
# the led is on!
Then turn it off calling:
Ledx.turn_off(:my_led)
# the led is off
We could also toggle it:
Ledx.toggle(:my_led)
# the led is on
Ledx.toggle(:my_led)
# the led is off
Sometimes you want to turn on a LED just for a certain time:
Ledx.turn_on(:my_led, 100)
# the led is on for 100 ms
# then off
Or turn it off for a certain time:
Ledx.turn_off(:my_led, 100)
# the led is off for 100 ms
# then on
Or toggle it for a certain time:
Ledx.toggle(:my_led, 100)
# the led toggles for 100 ms
# then toggles again
It's also really easy to repeatedly blink a led:
Ledx.blink(:my_led, 100, 50)
# the led is on for 100 ms
# then off for 50 ms
# then on for 100 ms
# ...
You can find examples in the examples
directory.
You just need to implement the Ledx.Driver
behaviour:
defmodule MyDriver do
@behaviour Ledx.Driver
def init(config) do
# your init code here
config
end
def on(config) do
# your code that turns the led on
config
end
def off(config) do
# your code that turns the led off
config
end
end
You can check the implementation of Ledx.Drivers.Gpio
to see an example.
To start a led, you need to call start_led/3
passing a name for the led, a driver
module, and the configuration needed for the driver.
{:ok, _pid} = Ledx.start_led(:my_led, MyDriver, %{pin: 18})
You can also start LEDs with your own drivers, using the config.exs
file:
config :ledx, leds:
[{:led1, MyDriver, %{path: "/dev/led/led1"}},
{:led2, MyDriver, %{path: "/dev/led/led2"}}]