Example and exercise. A FSM with pointers

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Jamess
Date:
Thu May 28 19:14:02 2015 +0000
Commit message:
How to make a FSM (finite state machine) with pointer in C

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Thu May 28 19:14:02 2015 +0000
@@ -0,0 +1,46 @@
+/*****************************************************************/
+/*               How to use State Machines in C                  */
+/*                        Switch|Case                            */
+/*****************************************************************/
+
+//Librarys:
+#include "mbed.h"
+
+//Configuration:
+
+DigitalOut myled(LED1);
+Serial pc(USBTX,USBRX);
+
+//Functions:
+void SendThings(void);  // Sends Something to the computer
+void LedON(void);       // Turns RED LED ON
+void LedOF(void);       // Turns RED LED OFF
+void PrepThings(void);  // Prepares Something
+//Function Pointer:
+    void (*state)()=SendThings;;
+
+
+int main() {
+
+//State Machine    
+    while(1) {
+        (*state)();        
+        wait(1);
+    }
+}
+void SendThings(void){
+    pc.printf("Sedingthings");
+    state=LedON;
+    }
+void LedON(void){
+    pc.printf("ON");
+    state=LedOF;
+    }
+void LedOF(void){
+    pc.printf("OFF");
+    state=PrepThings;
+    }
+void PrepThings(void){
+    pc.printf("preparing");
+    state=SendThings;
+    }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Thu May 28 19:14:02 2015 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/cbbeb26dbd92
\ No newline at end of file