Skip to content

Instantly share code, notes, and snippets.

@brian-lc
Last active April 7, 2016 07:21
Show Gist options
  • Select an option

  • Save brian-lc/5799847 to your computer and use it in GitHub Desktop.

Select an option

Save brian-lc/5799847 to your computer and use it in GitHub Desktop.
Remote LED with erlang
-module(net_led).
-export([start_light/0, switch/2, light/1, decouple/1]).
decouple(Light_Node) ->
{light, Light_Node} ! decouple,
io:format("Switch is decoupled~n", []).
switch(Light_Node, Action) ->
{light, Light_Node} ! {switch, self(), Action},
receive
Resp ->
io:format("switch received response ~p ~n", [Resp])
end.
light(LedPin) ->
receive
{switch, Switch_PID, decouple} ->
io:format("light decouple~n", []),
LedPin ! stop,
Switch_PID ! decoupled;
{switch, Switch_PID, on} ->
io:format("Light received on~n", []),
LedPin ! on,
Switch_PID ! on,
light(LedPin);
{switch, Switch_PID, off} ->
io:format("Light received off~n", []),
LedPin ! off,
Switch_PID ! off,
light(LedPin);
{switch, Switch_PID, X} ->
io:format("Unknown command ~p~n", [X]),
Switch_PID ! error,
light(LedPin)
end.
start_light() ->
LedPin = led:start(27),
register(light, spawn(net_led, light, [LedPin])).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment