Skip to content

Commit

Permalink
wire up the buttons with property table
Browse files Browse the repository at this point in the history
  • Loading branch information
mmmries committed Sep 9, 2023
1 parent 36cd9b7 commit 7b7be22
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 8 deletions.
4 changes: 3 additions & 1 deletion lib/trilobot/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ defmodule Trilobot.Application do

def children(_target) do
[
{Trilobot.Buttons, nil}
{PropertyTable, name: Trilobot.ButtonTable},
{Trilobot.Buttons, nil},
{Trilobot.RGB, nil}
]
end

Expand Down
20 changes: 14 additions & 6 deletions lib/trilobot/buttons.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ defmodule Trilobot.Buttons do
use GenServer
alias Circuits.GPIO

@table Trilobot.ButtonTable

@a_button_pin 5
@b_button_pin 6
@x_button_pin 16
Expand All @@ -19,10 +21,10 @@ defmodule Trilobot.Buttons do
}

@button_pins_to_button_names %{
@a_button_pin => :a,
@b_button_pin => :b,
@x_button_pin => :x,
@y_button_pin => :y
@a_button_pin => "a",
@b_button_pin => "b",
@x_button_pin => "x",
@y_button_pin => "y"
}

def start_link(nil) do
Expand All @@ -44,6 +46,11 @@ defmodule Trilobot.Buttons do
{:ok, x_led} = GPIO.open(@x_led_pin, :output, initial_value: 0)
{:ok, y_led} = GPIO.open(@y_led_pin, :output, initial_value: 0)

PropertyTable.put(@table, ["button", "a"], :released)
PropertyTable.put(@table, ["button", "b"], :released)
PropertyTable.put(@table, ["button", "c"], :released)
PropertyTable.put(@table, ["button", "d"], :released)

state = %{
a_button: a_button,
a_led: a_led,
Expand Down Expand Up @@ -77,8 +84,9 @@ defmodule Trilobot.Buttons do

defp log_pin_change(pin, value) do
require Logger
pin = Map.get(@button_pins_to_button_names, pin)
name = Map.get(@button_pins_to_button_names, pin)
event = if value == 0, do: :pressed, else: :released
Logger.info("pin #{pin} #{event}")
Logger.info("pin #{pin} => #{name} #{event}")
PropertyTable.put(@table, ["button", name], event)
end
end
32 changes: 32 additions & 0 deletions lib/trilobot/rgb.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
defmodule Trilobot.RGB do
use GenServer

def start_link(nil) do
GenServer.start_link(__MODULE__, nil, name: __MODULE__)
end

def init(nil) do
PropertyTable.subscribe(Trilobot.ButtonTable, ["button", "a"])
{:ok, i2c} = Circuits.I2C.open("i2c-1")
:ok = SN3218.RgbLeds.enable(i2c)
{:ok, %{i2c: i2c}}
end

def handle_info(%PropertyTable.Event{value: :pressed}, state) do
:ok = SN3218.RgbLeds.set_leds(state.i2c, List.duplicate({127, 255, 127}, 6))

{:noreply, state}
end

def handle_info(%PropertyTable.Event{value: :released}, state) do
:ok = SN3218.RgbLeds.set_leds(state.i2c, List.duplicate({0, 0, 0}, 6))

{:noreply, state}
end

def handle_info(message, state) do
require Logger
Logger.error("#{__MODULE__} received an unexpected message: #{inspect(message)}")
{:noreply, state}
end
end
3 changes: 2 additions & 1 deletion mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ defmodule Trilobot.MixProject do
{:nerves_system_mangopi_mq_pro, "~> 0.4", runtime: false, targets: :mangopi_mq_pro},
{:circuits_gpio, "~> 1.1"},
{:circuits_i2c, "~> 2.0"},
{:pigpiox, "~> 0.1.2"}
{:pigpiox, "~> 0.1.2"},
{:property_table, "~> 0.2"}
]
end

Expand Down

0 comments on commit 7b7be22

Please sign in to comment.