Permalink
Browse files

verify_event/init_event

  • Loading branch information...
1 parent c0e6257 commit 5adb0d61f4fb16a29e9b2cc484585402a4dad14e @tonyrog committed Feb 25, 2014
Showing with 58 additions and 14 deletions.
  1. +57 −13 src/hex_gpio.erl
  2. +1 −1 src/hex_gpio_server.erl
View
@@ -10,8 +10,11 @@
-behaviour(hex_plugin).
--export([add_event/2, del_event/1, output/2]).
--export([validate_flags/2]).
+-export([validate_event/2,
+ init_event/2,
+ add_event/2,
+ del_event/1,
+ output/2]).
%%
%% add_event(Flags::[{atom(),term()}, Signal::signal()) ->
@@ -46,28 +49,69 @@ output(Flags, _Env) ->
end.
%%
-%% validate_flags(in | out, Flags::[{atom(),term()}])
+%% init_event(in | out, Flags::[{atom(),term()}]) -> ok | {error,Reason}
+%% validate_event is assumed to have been run before init !
+init_event(in,Flags) ->
+ Pin = proplists:get_value(pin, Flags),
+ PinReg = proplists:get_value(pin_reg, Flags, 0),
+ case gpio:init(Pin, PinReg) of
+ ok -> gpio:set_direction(Pin, PinReg, in);
+ Error -> Error
+ end;
+init_event(out,Flags) ->
+ Pin = proplists:get_value(pin, Flags),
+ PinReg = proplists:get_value(pin_reg, Flags, 0),
+ Direct = proplists:get_bool(direct, Flags),
+ InitValue = case proplists:get_value(init_value, Flags) of
+ high -> high;
+ low -> low;
+ undefined -> out
+ end,
+ R = if Direct -> gpio:init_direct(Pin, PinReg);
+ true -> gpio:init(Pin, PinReg)
+ end,
+ case R of
+ ok -> gpio:set_direction(Pin, PinReg, InitValue);
+ _ -> R
+ end.
+
%%
-validate_flags(_Dir, []) ->
+%% validate_event(in | out, Flags::[{atom(),term()}])
+%%
+validate_event(Dir, Flags) ->
+ case lists:keytake(pin, 1, Flags) of
+ false ->
+ {error, {mandatory, [pin]}};
+ {value,{pin,Pin},Flags1} when is_integer(Pin), Pin >= 0 ->
+ validate_event1(Dir, Flags1);
+ _ ->
+ {error, {badarg, [pin]}}
+ end.
+
+validate_event1(_Dir, []) ->
ok;
-validate_flags(Dir, [{Key,Value}|Kvs]) ->
+validate_event1(Dir, [{Key,Value}|Kvs]) ->
case Key of
pin_reg when is_integer(Value), Value >= 0 ->
- validate_flags(Dir, Kvs);
- pin when is_integer(Value), Value >= 0 ->
- validate_flags(Dir, Kvs);
+ validate_event1(Dir, Kvs);
value when Dir =:= out, is_boolean(Value) ->
- validate_flags(Dir, Kvs);
+ validate_event1(Dir, Kvs);
value when Dir =:= out, is_integer(Value), Value >= 0, Value =< 1 ->
- validate_flags(Dir, Kvs);
+ validate_event1(Dir, Kvs);
interrupt when Value =:= rising;
Value =:= falling;
Value =:= both ->
- validate_flags(Dir, Kvs);
+ validate_event1(Dir, Kvs);
+ init_value when Value =:= high;
+ Value =:= low ->
+ validate_event1(Dir, Kvs);
polarity when is_boolean(Value) ->
- validate_flags(Dir, Kvs);
+ validate_event1(Dir, Kvs);
+ direct when is_boolean(Value) ->
+ %% direct access (require that gpio is initalized with chipset!)
+ validate_event1(Dir, Kvs);
_ ->
- lager:debug("validate_flags: unknown option/value ~p",
+ lager:debug("validate_event: unknown option/value ~p",
[{Key,Value}]),
{error, badarg}
end.
View
@@ -20,7 +20,7 @@
-define(SERVER, ?MODULE).
-define(EDGE_NONE, 0).
--define(EDGE_RISING, 1).
+-define(EDGE_RISING, 1).
-define(EDGE_FALLING, 2).
-define(EDGE_BOTH, 3).

0 comments on commit 5adb0d6

Please sign in to comment.