Last active
April 7, 2016 07:21
-
-
Save brian-lc/5799847 to your computer and use it in GitHub Desktop.
Remote LED with erlang
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
| -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