From a6ab1504058304012791281f9eb42c262745888f Mon Sep 17 00:00:00 2001 From: jacqueline Date: Fri, 19 May 2023 21:21:27 +1000 Subject: Add tinyfsm, start converting core functions to an FSM-based event loop --- lib/tinyfsm/examples/api/debugging_switch.cpp | 122 ++++++++++++++++++++++++++ 1 file changed, 122 insertions(+) create mode 100644 lib/tinyfsm/examples/api/debugging_switch.cpp (limited to 'lib/tinyfsm/examples/api/debugging_switch.cpp') diff --git a/lib/tinyfsm/examples/api/debugging_switch.cpp b/lib/tinyfsm/examples/api/debugging_switch.cpp new file mode 100644 index 00000000..065d0e13 --- /dev/null +++ b/lib/tinyfsm/examples/api/debugging_switch.cpp @@ -0,0 +1,122 @@ +#include +#include +#include + +struct Off; // forward declaration + + +// ---------------------------------------------------------------------------- +// Event Declarations +// +struct Toggle : tinyfsm::Event { }; // Event Declarations + + +// ---------------------------------------------------------------------------- +// State Machine Declaration +// +struct Switch +: tinyfsm::Fsm +{ + static void reset(void); + + // NOTE: on reset: "tinyfsm::StateList::reset()", copy + // constructor is used by default, so "this" points to neither + // "Off" nor "On" (see operator=() below). + Switch() : counter(0) { + std::cout << "* Switch()" << std::endl + << " this = " << this << std::endl; + } + + ~Switch() { + std::cout << "* ~Switch()" << std::endl + << " this = " << this << std::endl; + } + + Switch & operator=(const Switch & other) { + std::cout << "* operator=()" << std::endl + << " this = " << this << std::endl + << " other = " << &other << std::endl; + counter = other.counter; + return *this; + } + + virtual void react(Toggle const &) { }; + void entry(void); + void exit(void); + + int counter; +}; + +struct On : Switch { + void react(Toggle const &) override { transit(); }; +}; + +struct Off : Switch { + void react(Toggle const &) override { transit(); }; +}; + +FSM_INITIAL_STATE(Switch, Off) + + +// ---------------------------------------------------------------------------- +// State Machine Definitions +// +void Switch::reset() { + tinyfsm::StateList::reset(); +} + +void Switch::entry() { + counter++; + + // debugging only. properly designed state machines don't need this: + if(is_in_state()) { std::cout << "* On::entry()" << std::endl; } + else if(is_in_state()) { std::cout << "* Off::entry()" << std::endl; } + else assert(true); + + assert(current_state_ptr == this); + std::cout << " this (cur) = " << this << std::endl + << " state = " << &state() << std::endl + << " state = " << &state() << std::endl; +} + +void Switch::exit() { + assert(current_state_ptr == this); + std::cout << "* exit()" << std::endl + << " this (cur) = " << this << std::endl + << " state = " << &state() << std::endl + << " state = " << &state() << std::endl; +} + + +// ---------------------------------------------------------------------------- +// Main +// +int main() +{ + Switch::start(); + + while(1) + { + char c; + std::cout << "* main()" << std::endl + << " cur_counter = " << Switch::current_state_ptr->counter << std::endl + << " on_counter = " << Switch::state().counter << std::endl + << " off_counter = " << Switch::state().counter << std::endl; + + std::cout << std::endl << "t=Toggle, r=Restart, q=Quit ? "; + std::cin >> c; + switch(c) { + case 't': + Switch::dispatch(Toggle()); + break; + case 'r': + Switch::reset(); + Switch::start(); + break; + case 'q': + return 0; + default: + std::cout << "> Invalid input" << std::endl; + }; + } +} -- cgit v1.2.3