Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion config/sys.config
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
[
{lights_off, "/tmp/gateway_lights_off"},
{button, [
{gpio, 7}
{gpio, 112}
]}
]}
].
1 change: 1 addition & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
]}.

{deps, [
{ws2312, ".*", {git, "https://github.com/iotpi-argon/ws2312.git", {branch, "master"}}},
{erlang_ale, ".*", {git, "https://github.com/helium/erlang_ale", {branch, "master"}}},
{h3, ".*", {git, "https://github.com/helium/erlang-h3.git", {branch, "master"}}},
{gatt, ".*", {git, "https://github.com/helium/ebus-gatt", {branch, "master"}}},
Expand Down
10 changes: 7 additions & 3 deletions rebar.lock
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
{<<"gpb">>,{pkg,<<"gpb">>,<<"4.19.5">>},2},
{<<"grpc_client">>,
{git,"https://github.com/Bluehouse-Technology/grpc_client.git",
{ref,"f29516eaf590c63c0c300f98c9b75feff24f8351"}},
{ref,"02689e7992ef70b42b6b890db450ae4a7c21a50c"}},
0},
{<<"grpc_lib">>,
{git,"https://github.com/Bluehouse-Technology/grpc_lib",
Expand All @@ -55,7 +55,7 @@
0},
{<<"helium_proto">>,
{git,"https://github.com/helium/proto.git",
{ref,"5b649b1b1e23002826c1d252a0646fc5a7328578"}},
{ref,"13a093fbd3b2109c1ce91fa17bbd083828e88507"}},
0},
{<<"http2_client">>,
{git,"https://github.com/Bluehouse-Technology/http2_client.git",
Expand All @@ -68,7 +68,11 @@
0},
{<<"multiaddr">>,{pkg,<<"multiaddr">>,<<"1.1.3">>},1},
{<<"multihash">>,{pkg,<<"multihash">>,<<"2.1.0">>},1},
{<<"small_ints">>,{pkg,<<"small_ints">>,<<"0.1.0">>},2}]}.
{<<"small_ints">>,{pkg,<<"small_ints">>,<<"0.1.0">>},2},
{<<"ws2312">>,
{git,"https://github.com/iotpi-argon/ws2312.git",
{ref,"1183c5a85115c3e51c678b7616b9b4a712f20fb6"}},
0}]}.
[
{pkg_hash,[
{<<"base32">>, <<"044F6DC95709727CA2176F3E97A41DDAA76B5BC690D3536908618C0CB32616A2">>},
Expand Down
1 change: 1 addition & 0 deletions src/gateway_config.app.src
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{registered, []},
{mod, {gateway_config_app, []}},
{applications, [
ws2312,
kernel,
stdlib,
erlang_ale,
Expand Down
28 changes: 25 additions & 3 deletions src/gateway_config_led.erl
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
-type led_state() :: undefined | panic | disable | {advert, term()} | online | offline.

-record(state, {
handle = undefined :: lp5562:state() | undefined,
handle = undefined :: lp5562:state() | ws2312_helium:ws2312_handle() |undefined,
state :: led_state(),
off_file :: string(),
pairable_signal :: ebus:filter_id(),
Expand Down Expand Up @@ -74,8 +74,12 @@ init([Bus]) ->
self() ! init_led,
LS;
_ ->
lager:warning("No i2c device found, running in stub mode"),
undefined
case ws2312_helium:start_link() of
{ok, Handle} -> Handle;
_ ->
lager:warning("No i2c device found, running in stub mode"),
undefined
end
end,

{ok, BluezProxy} = ebus_proxy:start_link(Bus, ?BLUEZ_SERVICE, []),
Expand Down Expand Up @@ -222,6 +226,23 @@ handle_led_event(Event, State = #state{}) ->
update_led(NewState)
end.

%% pattern match for ws2312
update_led(State = #state{state = panic}) when is_pid(State#state.handle) ->
io:format("panic: ws2312~n"),
ws2312_helium:panic();
update_led(State = #state{state = disable}) when is_pid(State#state.handle) ->
io:format("disable: ws2312~n"),
ws2312_helium:disable();
update_led(State = #state{state = online}) when is_pid(State#state.handle) ->
ws2312_helium:online();
update_led(State = #state{state = offline}) when is_pid(State#state.handle) ->
ws2312_helium:offline();
update_led(State = #state{state = undefined}) when is_pid(State#state.handle) ->
ws2312_helium:undef();
update_led(State = #state{state = {advert, _}}) when is_pid(State#state.handle) ->
io:format("advert: ws2312~n"),
ws2312_helium:advert();
%% pattern match for original lp5562
update_led(State = #state{state = panic}) ->
led_set_color(?COLOR_RED, State);
update_led(State = #state{state = disable}) ->
Expand All @@ -233,6 +254,7 @@ update_led(State = #state{state = offline}) ->
update_led(State = #state{state = undefined}) ->
led_set_color(?COLOR_ORANGE, State);
update_led(State = #state{state = {advert, _}}) ->
io:format("advert: lp5562, ~p~n", [State#state.handle]),
led_set_color(?COLOR_BLUE, State).

led_set_color(_Color, State = #state{handle = undefined}) ->
Expand Down
31 changes: 19 additions & 12 deletions src/gateway_config_worker.erl
Original file line number Diff line number Diff line change
Expand Up @@ -61,21 +61,28 @@ init(_) ->
{ok, Bus} = ebus:system(),
{ok, BluezProxy} = ebus_proxy:start_link(Bus, ?BLUEZ_SERVICE, []),

%% io:format("will enable advertise in 2 secs~n"),
%% erlang:send_after(2 * 1000, self(), {enable_advertising, true}),

{ok, #state{
bluetooth_proxy = BluezProxy,
button_handle = ButtonPid
}}.

init_button(Args) ->
case file:read_file_info("/dev/gpio") of
{ok, _} ->
Gpio = proplists:get_value(gpio, Args, 7),
{ok, Pid} = gpio_button:start_link(Gpio, self()),
Pid;
_ ->
lager:warning("No GPIO device tree found, running in stub mode"),
undefined
end.
Gpio = proplists:get_value(gpio, Args, 7),
{ok, Pid} = gpio_button:start_link(Gpio, self()),
Pid.
%% case file:read_file_info("/dev/gpio") of
%% {ok, _} ->
%% Gpio = proplists:get_value(gpio, Args, 7),
%% lager:info("GPIO: ~d", Gpio),
%% {ok, Pid} = gpio_button:start_link(Gpio, self()),
%% Pid;
%% _ ->
%% lager:warning("No GPIO device tree found, running in stub mode"),
%% undefined
%% end.

handle_message(Member, _Msg, State) ->
lager:warning("Unhandled config message ~p", [Member]),
Expand Down Expand Up @@ -120,7 +127,7 @@ handle_cast(Msg, State = #state{}) ->
{noreply, State}.

%% Button click
handle_info({button_clicked, _, 1}, State = #state{}) ->
handle_info({button_clicked, _, _}, State = #state{}) ->
lager:info("Button clicked"),
%% Start a scan for visible wifi services
connman:enable(wifi, true),
Expand All @@ -138,8 +145,8 @@ handle_info({enable_advertising, true}, State = #state{bluetooth_advertisement =
[]
),
erlang:cancel_timer(State#state.bluetooth_timer),
Timer = erlang:send_after(?ADVERTISING_TIMEOUT, self(), timeout_advertising),
{noreply, State#state{bluetooth_advertisement = AdvPid, bluetooth_timer = Timer}};
%% Timer = erlang:send_after(?ADVERTISING_TIMEOUT, self(), timeout_advertising),
{noreply, State#state{bluetooth_advertisement = AdvPid, bluetooth_timer = undefined}};
handle_info({enable_advertising, false}, State = #state{bluetooth_advertisement = Pid}) when
is_pid(Pid)
->
Expand Down
90 changes: 90 additions & 0 deletions src/ws2312_helium.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
-module(ws2312_helium).
-behavior(gen_server).

-export([start_link/0]).

-export([
init/1,
handle_call/3,
handle_cast/2,
handle_info/2
]).
-export([
panic/0,
disable/0,
online/0,
offline/0,
undef/0,
advert/0
]).
-define(COLOR_PANIC, <<16#00FF00:24>>).
-define(COLOR_ONLINE, <<16#FF0000:24>>).
-define(COLOR_OFFLINE, <<16#00AAAA:24>>).
-define(COLOR_UNDEFINED, <<16#00AAAA:24>>).
-define(COLOR_ADVERT, <<16#0000AA:24>>).


-type ws2312_handle() :: pid() | undefined.

-record(state,
{
ws2312_handle :: ws2312_handle()
}).

start_link() ->
gen_server:start_link({local, ?MODULE}, ?MODULE, [], []).

init([]) ->
%% Trap exits to allow for terminate led animation
erlang:process_flag(trap_exit, true),

{ok, Handle} = ws2312:start_link(),

{ok, #state {
ws2312_handle = Handle
}}.

panic() ->
gen_server:cast(?MODULE, panic).
disable() ->
gen_server:cast(?MODULE, disable).
online() ->
gen_server:cast(?MODULE, online).
offline() ->
gen_server:cast(?MODULE, offline).
undef() ->
gen_server:cast(?MODULE, undefined).
advert() ->
gen_server:cast(?MODULE, {advert, []}).


handle_call(Msg, _From, State = #state{}) ->
lager:warning("Unhandled call ~p: ~p", [Msg, State]),
{noreply, State}.

handle_cast(panic, State) ->
ws2312:blink(?COLOR_PANIC),
{noreply, State};
handle_cast(disable, State) ->
ws2312:off(),
{noreply, State};
handle_cast(online, State) ->
ws2312:always_on(?COLOR_ONLINE),
{noreply, State};
handle_cast(offline, State) ->
ws2312:blink(?COLOR_OFFLINE),
{noreply, State};
handle_cast(undefined, State) ->
ws2312:blink(?COLOR_UNDEFINED),
{noreply, State};
handle_cast({advert, _}, State) ->
ws2312:blink(?COLOR_ADVERT),
{noreply, State};
handle_cast(Msg, State) ->
lager:info("Unhandled cast ~p: ~p", [Msg, State]),
{noreply, State}.

handle_info(Msg, State) ->
lager:warning("Unhandled info ~p: ~p", [Msg, State]),
{noreply, State}.