Last active
March 31, 2017 03:00
-
-
Save adkron/b43eb80af24037459d5698459f46e738 to your computer and use it in GitHub Desktop.
API idea
This file contains hidden or 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
defmodule Alarm.Worker do | |
@moduledoc false | |
use GenServer | |
# Pick ports that work on both the GrovePi+ and GrovePi Zero | |
@button_pin 14 # Port A0 | |
@buzzer_pin 3 # Port D3 | |
def start_link() do | |
GenServer.start_link(__MODULE__, []) | |
end | |
def init([]) do | |
with | |
#GrovePi is the application so no need to start | |
{:ok, _} -> GrovePi.Button.attach(@button_pin), | |
{:ok, buzzer_pi} -> GrovePi.Buzzer.attach(@buzzer_pin), | |
:ok -> GrovePi.listen_for({:pressed, @button_pin}), | |
do | |
{:ok, :ok} | |
end | |
end | |
def handle_info({:pressed, @button_pin}, state) do | |
IO.puts("Alert!!!!") | |
# Sound the alarm, but only for a second | |
GrovePi.Buzzer.buzz(@buzzer_pin, 1000) | |
{:noreply, state} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was thinking that each attach call would start its own little process tree, but that that tree would be under a GrovePi supervisor. The reason I was thinking that is that I'm looking at the GrovePi as an independent thing that has a bunch of attachments. The outside application code is a configuration, and then it registers for events that come from the GrovePi itself. We could also add a
detach
to the API that will end that little process tree.I actually just noticed that this module no longer needs state at this point.