Skip to content

Instantly share code, notes, and snippets.

@msantos
Created April 13, 2010 00:52
Show Gist options
  • Save msantos/364195 to your computer and use it in GitHub Desktop.
Save msantos/364195 to your computer and use it in GitHub Desktop.
-module(light).
-export([start/0,start/1]).
-define(TABLE, sensor).
-define(STORE, "db/sensor.db").
-define(TTY, "/dev/ttyUSB0").
-define(LDR, $1).
-define(INTRVL, 1000).
start() ->
start([]).
start(Options) ->
dets:open_file(?TABLE, [{type, set}, {file, ?STORE}]),
TTY = proplists:get_value(tty, Options, ?TTY),
Interval = proplists:get_value(interval, Options, ?INTRVL),
Speed = proplists:get_value(speed, Options, 9600),
LDR = proplists:get_value(ldr, Options, ?LDR),
Pid = serial:start([{open, TTY}, {speed, Speed}]),
timer:send_interval(Interval, Pid, {send, LDR}),
loop(Pid).
loop(Pid) ->
receive
{data, N} when is_binary(N) ->
%io:format("~w~n", [binary_to_integer(N)]),
dets:insert(?TABLE, {erlang:now(), binary_to_integer(N)}),
loop(Pid);
Error ->
io:format("error:~p~n", [Error])
end.
binary_to_integer(B) when is_binary(B) ->
list_to_integer(binary_to_list(B)).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment