Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: automaton.cpp
- Revision:
- 0:f736749c33d2
- Child:
- 4:fcf597f82632
diff -r 000000000000 -r f736749c33d2 automaton.cpp
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/automaton.cpp Mon May 14 14:12:32 2018 +0000
@@ -0,0 +1,63 @@
+#include "automaton.h"
+
+//#define DEBUG_AUTOMATON
+
+#ifdef DEBUG_AUTOMATON
+#include "IO.h"
+#define LOG_DEBUG(msg) IO::debug(msg)
+#else
+#define LOG_DEBUG(msg)
+#endif
+
+namespace automaton {
+
+ Callback<void()> entry;
+ Callback<void()> teardown_prev;
+ Callback<void()> setup_next;
+ volatile Event event;
+
+ void makeTransition();
+
+ void empty_entry_callback()
+ {
+ event = Done;
+ }
+
+ void do_nothing_callback(){ } // do nothing
+
+ void run(){
+ event = None;
+
+ // go into the entry point
+ LOG_DEBUG("starting");
+ entry();
+
+ while(event != Done){
+ // wait until event status changes
+ while(event == None);
+
+ switch(event){
+ case Transition:
+ makeTransition();
+ break;
+ case Done:
+ break;
+ default:
+ // TODO: handle error more nicely
+ LOG_DEBUG("unexpected event");
+ event = Done;
+ break;
+ }
+ }
+
+ // pc.printf("done.\r\n");
+ }
+
+ void makeTransition()
+ {
+ LOG_DEBUG("transition");
+ teardown_prev();
+ setup_next();
+ event = None;
+ }
+}