- 论坛徽章:
- 27
|
- -module(fsm).
- -behaviour(gen_fsm).
- -define(SERVER, ?MODULE).
- %% ------------------------------------------------------------------
- %% API Function Exports
- %% ------------------------------------------------------------------
- %% ------------------------------------------------------------------
- %% gen_fsm Function Exports
- %% ------------------------------------------------------------------
- -export([init/1, state_name/2, state_name/3, handle_event/3,
- handle_sync_event/4, handle_info/3, terminate/3,
- code_change/4]).
- -compile(export_all).
- %% ------------------------------------------------------------------
- %% API Function Definitions
- %% ------------------------------------------------------------------
- start_link(_Args) ->
- gen_fsm:start_link({local, ?SERVER}, ?MODULE, _Args, []).
- %% ------------------------------------------------------------------
- %% gen_fsm Function Definitions
- %% ------------------------------------------------------------------
- init(_Args) ->
- {ok, initial_state_name, {[],_Args}}.
- state_name(_Event, State) ->
- io:format("state_event /2"),
- {next_state, state_name, State}.
- state_name(_Event, _From, State) ->
- io:format("state_event /3"),
- {reply, ok, state_name, State}.
- handle_event(_Event, StateName, State) ->
- io:format("i handle it"),
- {next_state, StateName, State}.
- handle_sync_event(_Event, _From, StateName, State) ->
- {reply, ok, StateName, State}.
- handle_info(_Info, StateName, State) ->
- {next_state, StateName, State}.
- terminate(_Reason, _StateName, _State) ->
- ok.
- code_change(_OldVsn, StateName, State, _Extra) ->
- {ok, StateName, State}.
- %% ------------------------------------------------------------------
- %% Internal Function Definitions
- %% ------------------------------------------------------------------
- initial_state_name({button,Digit},{SoFar,Code})->
- io:format("buttion: ~p, So far: ~p, Code: ~p~n", [Digit, SoFar, Code]),
- InputDigits = lists:append(SoFar, Digit),
- case InputDigits of
- Code ->
- do_unlock(),
- {next_state, open, {[], Code}, 10000};
- Incomplete when length(Incomplete)<length(Code) ->
- {next_state, initial_state_name, {Incomplete, Code}, 5000};
- Wrong ->
- io:format("wrong passwd: ~p~n", [Wrong]),
- {next_state, initial_state_name, {[], Code}}
- end;
- initial_state_name(timeout, {_SoFar, Code}) ->
- io:format("timout when waiting button inputting, clean the input, button again plz~n"),
- {next_state, initial_state_name, {[], Code}}.
- open(timeout, State) ->
- do_lock(),
- {next_state, initial_state_name, State}.
- do_unlock() ->
- io:format("passwd is right, open the DOOR.~n").
-
- do_lock() ->
- io:format("over, close the DOOR.~n").
复制代码 |
|