diff options
Diffstat (limited to 'lib/tinyfsm/examples/elevator/motor.cpp')
| -rw-r--r-- | lib/tinyfsm/examples/elevator/motor.cpp | 60 |
1 files changed, 60 insertions, 0 deletions
diff --git a/lib/tinyfsm/examples/elevator/motor.cpp b/lib/tinyfsm/examples/elevator/motor.cpp new file mode 100644 index 00000000..b4668070 --- /dev/null +++ b/lib/tinyfsm/examples/elevator/motor.cpp @@ -0,0 +1,60 @@ +#include <tinyfsm.hpp> +#include "motor.hpp" +#include <iostream> + + +// ---------------------------------------------------------------------------- +// Motor states +// + +class Stopped +: public Motor +{ + void entry() override { + std::cout << "Motor: stopped" << std::endl; + direction = 0; + }; +}; + +class Up +: public Motor +{ + void entry() override { + std::cout << "Motor: moving up" << std::endl; + direction = 1; + }; +}; + +class Down +: public Motor +{ + void entry() override { + std::cout << "Motor: moving down" << std::endl; + direction = -1; + }; +}; + + +// ---------------------------------------------------------------------------- +// Base State: default implementations +// + +void Motor::react(MotorStop const &) { + transit<Stopped>(); +} + +void Motor::react(MotorUp const &) { + transit<Up>(); +} + +void Motor::react(MotorDown const &) { + transit<Down>(); +} + +int Motor::direction{0}; + + +// ---------------------------------------------------------------------------- +// Initial state definition +// +FSM_INITIAL_STATE(Motor, Stopped) |
