-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #46 from axelclark/ultrasonic
Update Ultrasonic to use Poller
- Loading branch information
Showing
4 changed files
with
105 additions
and
29 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
defmodule GrovePi.Ultrasonic.DefaultTrigger do | ||
@behaviour GrovePi.Trigger | ||
|
||
@moduledoc """ | ||
This is the default triggering mechanism for changes to Ultrasonic reads. The | ||
event is `:changed` and includes the trigger state. The trigger state | ||
for the default trigger is a struct containing a `value` property. | ||
## Examples | ||
iex> GrovePi.Ultrasonic.DefaultTrigger.init([]) | ||
{:ok, %GrovePi.Ultrasonic.DefaultTrigger.State{value: 0}} | ||
iex> GrovePi.Ultrasonic.DefaultTrigger.update(50, %{value: 0}) | ||
{:changed, %{value: 50}} | ||
iex> GrovePi.Ultrasonic.DefaultTrigger.update(0, %{value: 50}) | ||
{:ok, %{value: 50}} | ||
iex> GrovePi.Ultrasonic.DefaultTrigger.update(125, %{value: 50}) | ||
{:changed, %{value: 125}} | ||
The value 0 is an error and therefore discarded. | ||
""" | ||
|
||
defmodule State do | ||
@moduledoc false | ||
defstruct value: 0 | ||
end | ||
|
||
def init(_) do | ||
{:ok, %State{}} | ||
end | ||
|
||
def update(value, %{value: value} = state), do: {:ok, state} | ||
|
||
def update(value, state) when value == 0, do: {:ok, state} | ||
|
||
def update(new_value, state), do: {:changed, %{state | value: new_value}} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
defmodule GrovePi.Ultrasonic.DefaultTriggerTest do | ||
use ExUnit.Case, async: true | ||
doctest GrovePi.Ultrasonic.DefaultTrigger | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters