FreeRTOS Emulator with SDL2 Based Graphics and Input Library  1.0
A POSIX wrapper to run FreeRTOS on an x86 machine with some basic input and output libraries aimed at making teaching FreeRTOS simpler.
State Machine Implementation

This basic state machine implementation shows the basic concept of a state machine where each state is represented by a data object that contains function points to an init, enter, run and exit function. More...

Functions

unsigned char uStatesInit (void)
 Initialized the states stored in the state machine by calling their probe functions, if set. More...
 
unsigned char uStatesRun (void)
 Ticks the state machine over. More...
 
int xStatesAdd (void(*probe)(void), void(*enter)(void), void(*run)(void), void(*exit)(void), int ID, char *name)
 Adds a state to the state machine. More...
 
void vStatesSetCallback (void(*callback)(void))
 Sets the callback function for the state machine. More...
 
void vStatesSetData (void *data)
 Sets the data of the current state. More...
 
void vStatesSetInput (unsigned char input)
 Sets the input variable stored in the state machine. More...
 
unsigned char uStatesSetState (unsigned int state_id)
 Sets the next state of the state machine using the state's ID. More...
 
void * pStatesGetData (void)
 Returns a pointer to the data stored in the current state. More...
 
char * pStatesGetStateName (void)
 Returns the string of the current state's name. More...
 
unsigned char uStatesGetInput (void)
 Retrieves the input vector stored within the state machine. More...
 
int xStatesGetStateID (void)
 Returns the ID of the current state. More...
 
int xStatesGetStateCount (void)
 Returns the number of states currently stored within the state machine. More...
 
int xStatesIncrementState (void)
 Increments the state to the next in the linked list of states. More...
 
int xStatesDecrementState (void)
 Decrements the state to the previous in the linked list of states. More...
 
void vStatesClearInput (void)
 Clears the 8 bit input vector stored in the state machine. More...
 

Detailed Description

This basic state machine implementation shows the basic concept of a state machine where each state is represented by a data object that contains function points to an init, enter, run and exit function.

Basic use of this framework requires the user to firstly add the states using

xStatesAdd(init_function, enter_function, run_function, exit_function, ID, name_string)
int xStatesAdd(void(*probe)(void), void(*enter)(void), void(*run)(void), void(*exit)(void), int ID, char *name)
Adds a state to the state machine.
Definition: states.c:286

The handler functions take the form seen in xStatesAdd().

Once the states have been added an optional callback can be set for the state machine. It runs after the execution of each state machine cycle. This is set using

vStatesSetCallback(callback_function)
void vStatesSetCallback(void(*callback)(void))
Sets the callback function for the state machine.
Definition: states.c:83

The states can then be initialized such that their probe functions are called. This is done using

unsigned char uStatesInit(void)
Initialized the states stored in the state machine by calling their probe functions,...
Definition: states.c:347

The initial state can be set using

uStatesSetState(state_ID)
unsigned char uStatesSetState(unsigned int state_id)
Sets the next state of the state machine using the state's ID.
Definition: states.c:106

Otherwise the first state added is run by default. This call is the same that is used to change state during the machines execution.

Function Documentation

◆ pStatesGetData()

void* pStatesGetData ( void  )

Returns a pointer to the data stored in the current state.

The data is stored using a void pointer and must be type cast once returned.

Returns
void * to the data

◆ pStatesGetStateName()

char* pStatesGetStateName ( void  )

Returns the string of the current state's name.

Returns
char * to the string

◆ uStatesGetInput()

unsigned char uStatesGetInput ( void  )

Retrieves the input vector stored within the state machine.

Returns
unsigned char 8 bit input vector

◆ uStatesInit()

unsigned char uStatesInit ( void  )

Initialized the states stored in the state machine by calling their probe functions, if set.

Returns
0 on success

First added state is the initial state if possible

◆ uStatesRun()

unsigned char uStatesRun ( void  )

Ticks the state machine over.

A call to states_run will cause the state machine to check if a state change is pending, if so then the appropriate exit and enter functions for the current and next states will be called. The run function of the next state will be run after the state change. If there is no state change to be done then the run function of the current state is simply called.

Returns
0 on success

SM callback if set

◆ uStatesSetState()

unsigned char uStatesSetState ( unsigned int  state_id)

Sets the next state of the state machine using the state's ID.

To change state in the state machine you must set the next state's ID which will then be handled during the next call to states_run. This will ensure the the current state's exit function is called before the next state's enter function is called.

Parameters
state_idID of the state that is to be run next
Returns
0 on success

◆ vStatesClearInput()

void vStatesClearInput ( void  )

Clears the 8 bit input vector stored in the state machine.

◆ vStatesSetCallback()

void vStatesSetCallback ( void(*)(void)  callback)

Sets the callback function for the state machine.

The callback function is run at the end of each cycle of th state machine, the cycle being called using states_run.

Parameters
callbackA function pointer to the function that is to be called

◆ vStatesSetData()

void vStatesSetData ( void *  data)

Sets the data of the current state.

Parameters
dataA void pointer to the data structure that is to be stored in the current state

◆ vStatesSetInput()

void vStatesSetInput ( unsigned char  input)

Sets the input variable stored in the state machine.

The input variable of the state machine is a buffer to hold input that can be set from within the input function (interupts, polling, etc.)

Parameters
input8 bit input vector

◆ xStatesAdd()

int xStatesAdd ( void(*)(void)  probe,
void(*)(void)  enter,
void(*)(void)  run,
void(*)(void)  exit,
int  ID,
char *  name 
)

Adds a state to the state machine.

Parameters
probeA function pointer to the state's init function
enterA function pointer to the state's enter function
runA function pointer to the state's run function
exitA function pointer to the state's exit function
IDThe state's unique ID number
nameA string representation of the state's name
Returns
0 on success

◆ xStatesDecrementState()

int xStatesDecrementState ( void  )

Decrements the state to the previous in the linked list of states.

Returns
0 on success

◆ xStatesGetStateCount()

int xStatesGetStateCount ( void  )

Returns the number of states currently stored within the state machine.

Returns
unsigned int count of the number of states in the state machine

◆ xStatesGetStateID()

int xStatesGetStateID ( void  )

Returns the ID of the current state.

Returns
unsigned int ID

◆ xStatesIncrementState()

int xStatesIncrementState ( void  )

Increments the state to the next in the linked list of states.

Returns
0 on success