Example and exercise. A FSM with pointers

Dependencies:   mbed

main.cpp

Committer:
Jamess
Date:
2015-05-28
Revision:
0:4205137775e5

File content as of revision 0:4205137775e5:

/*****************************************************************/
/*               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;
    }