Keisuke Sehara
/
STM32_Whisking
fast-feedback virtual target task code on STM Nucleo
Diff: core/automaton.h
- Revision:
- 26:b4421d1ee57a
- Parent:
- 4:fcf597f82632
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/core/automaton.h Thu Jul 05 20:15:37 2018 +0000 @@ -0,0 +1,67 @@ +#ifndef AUTOMATON_H_ +#define AUTOMATON_H_ + +#include "mbed.h" + +namespace automaton +{ + + /** + * event enum to notice changes to the instance + */ + enum Event + { + None = 0x00, + Transition = 0x01, + Done = 0x02, + }; + + /** + * a callback that does nothing + */ + void do_nothing_callback(); + + extern Callback<void()> entry; + extern Callback<void()> teardown_prev; + extern Callback<void()> setup_next; + extern volatile Event event; + + /** + * marks for performing a transition from FromState to ToState. + */ + template<typename FromState, typename ToState> + void jump() + { + if ( event == None ){ + teardown_prev = &FromState::teardown; + setup_next = &ToState::setup; + event = Transition; + } + } + + template<typename State> + void done() + { + if (event == None){ + teardown_prev = &State::teardown; + setup_next = &do_nothing_callback; + event = Done; + } + } + + template<typename State> + void init() + { + entry = &State::setup; + teardown_prev = &do_nothing_callback; + setup_next = &do_nothing_callback; + event = None; + } + + /** + * runs the automaton once + */ + void run(); + +} +#endif