Skip to content
This repository has been archived by the owner on Dec 6, 2021. It is now read-only.
/ SimpleFSM Public archive

πŸ” Simple finite state machine framework

License

Notifications You must be signed in to change notification settings

frenzox/SimpleFSM

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

12 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

SimpleFSM

SimpleFSM

A really, really simple finite state machine framework

Usage

Import SimpleFSM.h and define your states in your FSM implementation .h

#ifndef TRAFFIC_LIGHT_H
#define TRAFFIC_LIGHT_H

#include "SimpleFSM.h"

SimpleFSM traffic_light_sm;

STATE(GREEN);
STATE(YELLOW);
STATE(RED);

#endif // TRAFFIC_LIGHT_H

In your .c file, implement the states logic

#include "traffic_light.h"

STATE(GREEN) {
  if(FIRST) {
    tick_count = 0;
    set_red_light(false);
    set_green_light(true);
  }
  
  //LOGIC HERE
  
  tick_count++;
  
  if(tick_count > MAX_TICKS_IN_GREEN)
    NEXT_STATE(YELLOW);
}

STATE(YELLOW) {
  if(FIRST) {
    tick_count = 0;
    set_green_light(false);
    set_yellow_light(true);
  }
  
  tick_count++;
  
  if(tick_count > MAX_TICKS_IN_YELLOW)
    NEXT_STATE(RED);
}

and so on...

Init and exec your state machine somewhere else

INIT(traffic_light_sm, GREEN);

while(1) {
  EXEC(traffic_light_sm);
  
  //YOU CAN INSERT SOME TIME DELAY HERE
  
}

You can also compare the state machine current state

if(COMPARE(traffic_light_sm, RED)) {
  EXEC(another_sm, ANY_ANOTHER_SM_STATE);
}

Acknowledgements

Many thanks to Professor Afonso Miguel

About

πŸ” Simple finite state machine framework

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages