summaryrefslogtreecommitdiff
path: root/lib/tinyfsm/examples/api/debugging_switch.cpp
diff options
context:
space:
mode:
authorjacqueline <me@jacqueline.id.au>2023-05-19 21:21:27 +1000
committerjacqueline <me@jacqueline.id.au>2023-05-19 21:21:27 +1000
commita6ab1504058304012791281f9eb42c262745888f (patch)
treef82379cd1e66a8ae2f1afbae5cf083a8ab7acc53 /lib/tinyfsm/examples/api/debugging_switch.cpp
parentb320a6a863cf1c10dc79254af41f573730935564 (diff)
downloadtangara-fw-a6ab1504058304012791281f9eb42c262745888f.tar.gz
Add tinyfsm, start converting core functions to an FSM-based event loop
Diffstat (limited to 'lib/tinyfsm/examples/api/debugging_switch.cpp')
-rw-r--r--lib/tinyfsm/examples/api/debugging_switch.cpp122
1 files changed, 122 insertions, 0 deletions
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 <tinyfsm.hpp>
+#include <iostream>
+#include <cassert>
+
+struct Off; // forward declaration
+
+
+// ----------------------------------------------------------------------------
+// Event Declarations
+//
+struct Toggle : tinyfsm::Event { }; // Event Declarations
+
+
+// ----------------------------------------------------------------------------
+// State Machine Declaration
+//
+struct Switch
+: tinyfsm::Fsm<Switch>
+{
+ static void reset(void);
+
+ // NOTE: on reset: "tinyfsm::StateList<Off, On>::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<Off>(); };
+};
+
+struct Off : Switch {
+ void react(Toggle const &) override { transit<On>(); };
+};
+
+FSM_INITIAL_STATE(Switch, Off)
+
+
+// ----------------------------------------------------------------------------
+// State Machine Definitions
+//
+void Switch::reset() {
+ tinyfsm::StateList<Off, On>::reset();
+}
+
+void Switch::entry() {
+ counter++;
+
+ // debugging only. properly designed state machines don't need this:
+ if(is_in_state<On>()) { std::cout << "* On::entry()" << std::endl; }
+ else if(is_in_state<Off>()) { std::cout << "* Off::entry()" << std::endl; }
+ else assert(true);
+
+ assert(current_state_ptr == this);
+ std::cout << " this (cur) = " << this << std::endl
+ << " state<On> = " << &state<On>() << std::endl
+ << " state<Off> = " << &state<Off>() << std::endl;
+}
+
+void Switch::exit() {
+ assert(current_state_ptr == this);
+ std::cout << "* exit()" << std::endl
+ << " this (cur) = " << this << std::endl
+ << " state<On> = " << &state<On>() << std::endl
+ << " state<Off> = " << &state<Off>() << 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<On>().counter << std::endl
+ << " off_counter = " << Switch::state<Off>().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;
+ };
+ }
+}