sm000 A state machine in CThe most raw one - simple all include in the C source below is just for printing and sleep
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
// EFFICIENT
// but no variable box as in sm001 and sm002
// you have to use global variables..
// Jens
// pointer til funktion af type: void * func(void)
// vi vil returnere adresse på next state via retur vaerdi
//typedef void *(*pFct)(void);
#define NEXT(x) return(x);
void * init();
void * ledOn();
void* ledOff();
void * init(void)
{
printf("\ninit\n");
sleep(1);
NEXT(ledOn);
}
void * ledOn(void)
{
printf("\nledOn\n");
sleep(1);
NEXT(ledOff);
}
void * ledOff(void)
{
printf("\nledOff\n");
sleep(1);
NEXT(ledOn);
}
int main()
{
//pFct statefunc = init;
//variable that holds address of function: void * fct(void)
void *(*statefunc)(void) = init;
printf("\nstarting state machine\n");
while (1) {
printf("\nback in scheduler");
statefunc = (*statefunc)();
}
}
|