Keisuke Sehara
/
STM32_Whisking
fast-feedback virtual target task code on STM Nucleo
Diff: automaton.h
- Revision:
- 0:f736749c33d2
- Child:
- 4:fcf597f82632
diff -r 000000000000 -r f736749c33d2 automaton.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/automaton.h Mon May 14 14:12:32 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 transit() + { + 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