summaryrefslogtreecommitdiff
path: root/lib/tinyfsm/examples/api/simple_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/simple_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/simple_switch.cpp')
-rw-r--r--lib/tinyfsm/examples/api/simple_switch.cpp85
1 files changed, 85 insertions, 0 deletions
diff --git a/lib/tinyfsm/examples/api/simple_switch.cpp b/lib/tinyfsm/examples/api/simple_switch.cpp
new file mode 100644
index 00000000..b20b2abd
--- /dev/null
+++ b/lib/tinyfsm/examples/api/simple_switch.cpp
@@ -0,0 +1,85 @@
+#include <tinyfsm.hpp>
+#include <iostream>
+
+struct Off; // forward declaration
+
+
+// ----------------------------------------------------------------------------
+// 1. Event Declarations
+//
+struct Toggle : tinyfsm::Event { };
+
+
+// ----------------------------------------------------------------------------
+// 2. State Machine Base Class Declaration
+//
+struct Switch : tinyfsm::Fsm<Switch>
+{
+ virtual void react(Toggle const &) { };
+
+ // alternative: enforce handling of Toggle in all states (pure virtual)
+ //virtual void react(Toggle const &) = 0;
+
+ virtual void entry(void) { }; /* entry actions in some states */
+ void exit(void) { }; /* no exit actions */
+
+ // alternative: enforce entry actions in all states (pure virtual)
+ //virtual void entry(void) = 0;
+};
+
+
+// ----------------------------------------------------------------------------
+// 3. State Declarations
+//
+struct On : Switch
+{
+ void entry() override { std::cout << "* Switch is ON" << std::endl; };
+ void react(Toggle const &) override { transit<Off>(); };
+};
+
+struct Off : Switch
+{
+ void entry() override { std::cout << "* Switch is OFF" << std::endl; };
+ void react(Toggle const &) override { transit<On>(); };
+};
+
+FSM_INITIAL_STATE(Switch, Off)
+
+
+// ----------------------------------------------------------------------------
+// 4. State Machine List Declaration (dispatches events to multiple FSM's)
+//
+// In this example, we only have a single state machine, no need to use FsmList<>:
+//using fsm_handle = tinyfsm::FsmList< Switch >;
+using fsm_handle = Switch;
+
+
+// ----------------------------------------------------------------------------
+// Main
+//
+int main()
+{
+ // instantiate events
+ Toggle toggle;
+
+ fsm_handle::start();
+
+ while(1)
+ {
+ char c;
+ std::cout << std::endl << "t=Toggle, q=Quit ? ";
+ std::cin >> c;
+ switch(c) {
+ case 't':
+ std::cout << "> Toggling switch..." << std::endl;
+ fsm_handle::dispatch(toggle);
+ // alternative: instantiating causes no overhead (empty declaration)
+ //fsm_handle::dispatch(Toggle());
+ break;
+ case 'q':
+ return 0;
+ default:
+ std::cout << "> Invalid input" << std::endl;
+ };
+ }
+}