diff options
| author | jacqueline <me@jacqueline.id.au> | 2023-05-19 21:21:27 +1000 |
|---|---|---|
| committer | jacqueline <me@jacqueline.id.au> | 2023-05-19 21:21:27 +1000 |
| commit | a6ab1504058304012791281f9eb42c262745888f (patch) | |
| tree | f82379cd1e66a8ae2f1afbae5cf083a8ab7acc53 /lib/tinyfsm/examples/elevator/motor.cpp | |
| parent | b320a6a863cf1c10dc79254af41f573730935564 (diff) | |
| download | tangara-fw-a6ab1504058304012791281f9eb42c262745888f.tar.gz | |
Add tinyfsm, start converting core functions to an FSM-based event loop
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) |
