C Language, C++

How to implement finite state machine in c

state machine in c

Nowadays many applications either small or complex use the finite state machine (FSM). A finite state machine is one of the popular design patterns for the embedded system.A finite state machine makes the development easy and smooth.

There are a lot of devices which use event base states, like coffee machine, vending machine, POS devices, door lock system etc. Some POS devices are used the event table in which events are registered with an event handler.This event handler executes when the relevant events come.

A finite state machine can have multiple states, it can switch from one state to another state on the basis of internal or external input.This input could be timer expiry signal, hardware or software interrupt .. etc. In the finite state machine, the procedure to change one state to another state is called transition.

In this article, I will describe some approaches for implementing a state machine in c.

For example, I am considering ATM machine and creating its sample state machine in c. The state of ATM machine could be changed through the coming events.I have mentioned below the sample stats of ATM machine.



The Sample States of the ATM  machine.

  • Idle State
  • Card Inserted State
  • Pin entered State
  • Option Selected State
  • Amount Entered State

Initially, ATM machine would be in the Idle state, When a user inserts the card then it change their state and processes the card. After the card processing, ATM again changes their state and ask the user to enter the pin number.When the user entered the pin then it asks for choice ( Balance inquiry, withdrawal, Deposit) and after that change the state and ask to enter the amount and dispatch the entered amount.

state machine in c

Above figure describe the states of the ATM machine.

Recommended steps to create the state machine

  • Gather the information which user wants.
  • Analyze the all gather information and sketch the state transition diagram.
  • create a code skeleton of the state machine.
  • Make sure the transition (changing state) work properly
  • Implement the all the required information in code skeleton of the state machine.
  • Test the implemented state machine.

There are two most popular approaches for implementing an event based state machine in c. The selection of both approaches depends on requirement and situation.

  • Using the conditional statement (nested switch or nested if-else).
  • Using the lookup table




Using the conditional statement

This is the simplest way to implement the state machine in c. We have used if-else or the switch case to check the states and triggered event. If the combination of states and triggered event match, execute the event handler to serve the service and update the next state. It depends on a requirement which checks first states or the event. 

In below sample code, I am verifying the states first and after that checks the triggered event.If you want you can reverse the procedure that means you can check event first and after that checks the states.

Using the lookup table

A lookup table is also a very good technique to implement the state machine. Using the c language we can implement a lookup table in many ways. In below section, I am describing some ways to implement the state machine using the function pointer and lookup table.



State machine using 2D array

We create a 2D array containing the function pointers. In which rows and columns represented through the states and events of the finite state machine. This 2D array initializes using the designated initializer.

It is the simplest way to implement the state machine, using this technique we can reduce the length of the code. The most important feature of this technique in future if you want to add any new states or events, we can easily integrate with it without any huge hurdle.

Let’s see an example,

One thing needs to remember, here table is sparse, if the states and events are increasing, this technique increases the wastage of the memory. So before creating the state machine diagram we need to account all the things very precisely in the beginning of the design.

State machine using array of structure

This is an elegant way to create the finite state machine. The states and events of the state machine are encapsulated in a structure with function pointer (Event handler)  call at the proper state and event.

 




7 Comments

  1. John

    Nice article on the basics. Thank you

  2. Akhi

    eSystemEvent eNewEvent = ReadEvent();
    Can you explain this line?

    • Amlendra

      In this article ReadEvent() behaves like the third party or standard library which returns the current occurred event. For example, In POS device we have read function which when you pressed the key, swipe the card or insert the card then it returns relevant event value. On the basis of this event value, you call the relevant function, like when you swipe the card in POS device then an event value (MAG_CARD_EVENT) return by the read function.

Leave a Reply

Theme by Anders Norén