GrovePi v0.6.1 GrovePi.Trigger behaviour View Source
The Trigger behaviour is used for implementing triggers for poller
behaviors such as GrovePi.Sound
and GrovePi.Button
.
The triggers must implement two callbacks, init/1
and update/2
.
Example
The following is the implementation of GrovePi.Button.DefaultTrigger
. Processes
can subscribe to two events :released
and :pressed
.
defmodule GrovePi.Button.DefaultTrigger do
@behaviour GrovePi.Trigger
defmodule State do
@moduledoc false
defstruct value: 0
end
def init(_) do
{:ok, %State{}}
end
def update(value, %{value: value} = state) do
{:ok, state}
end
def update(new_value, state) do
{event(new_value), %{state | value: new_value}}
end
defp event(0), do: :released
defp event(1), do: :pressed
end
Link to this section Summary
Callbacks
The init callback that must return {:ok, state}
or an error tuple
The update callback receives a new value and a trigger state and returns
a tuple of {:event_name, new_state}
Link to this section Types
Link to this section Callbacks
The init callback that must return {:ok, state}
or an error tuple.
The update callback receives a new value and a trigger state and returns
a tuple of {:event_name, new_state}
.
If no event is needed to fire return {:ok, new_state}
.