FSM Lecture 40: Exercise-004 State table approach for implementation of an FSM part-1

  • Post author:
  • Post category:Blog

 

Exercise-004 State table approach for implementation of an FSM part-1

 

 

We will study the state table approach in this article. And in the previous article, we discussed the state handler approach and the nested switch approach. In the state table approach, we will use a state table.

Let me explain this with an example. We used lots of comparisons in the previous two methods, in the state handler method or the nested switch method. The comparison is implemented by using the Switch statement. There were switch statements, a couple of cases, so lot’s of comparisons.

In this method, there will be no comparison. Whenever an event occurs, the function related to that event is executed, and we can achieve this using a function pointer. The state table is nothing but it’s a table containing various handlers. It’s a table of handlers, or you can call it a table of event handlers. 

Figure 1. State table
Figure 1. State table

 

For example, if a START_PAUSE event happens in your application when the application is in COUNTDOWN state, then COUNTDOWN_Start_pause() handler is called, which takes care of handling the START_PAUSE event when the state is  COUNTDOWN. 

Like that, you have to make one table, and for each event, you have to write its own event handler considering the state. That’s why, for our application, there will be a table, which consists of 5 rows and seven columns. Rows indicate the number of states, and columns indicate Events. We have five states, so five rows, and in our case, the table will have seven columns because we have seven events as shown in Figure 1. 

After that, you convert this table into some data structure in your program. You can use a two-dimensional array to save this information. And after that, you can use that two-dimensional array to execute the different event handlers. 

 

Let’s create a new project, and we will remove all the switch case statements and convert each case into different event handling functions. 

Let’s go back to the project. Now, let’s implement the state table method. Create a new project. I have just created a new project, 005Protimer_ST. And after that, go to the project folder, we will reuse the files of the 003Protimer project. Just copy all the src code, go to 005Protimer_ST → src → paste it. 

In the IDE, let’s go to the protimer_state_machine.cpp.

event_status_t IDLE_Entry(protimer_t *const mobj, event_t const *const e){
    mobj->curr_time = 0;
    mobj->elapsed_time = 0;
    display_time(0);
    display_message("Set",0,0);
    display_message("time",0,1);
    return EVENT_HANDLED;
}

event_status_t IDLE_Exit(protimer_t *const mobj, event_t const *const e){
     display_clear();
     return EVENT_HANDLED;
}

Protimer_state_machine.cpp file

 

Let me create one function to handle the ENTRY case of the IDLE state.

event_status_t  IDLE_Entry(protimer_t *const mobj, event_t const *const e) 

I will write like IDLE_Entry; this returns the same as this function event_status_t, and after that, the two input parameters mobj, event_t. This is a non-static function; you need not put the static function because we must share this with main.cpp to include it in the state table. That’s why this can’t be the static function. And write the instructions from this case as shown above. 

And let’s create one more function. The following case is for EXIT, so create one function EXIT. And copy whatever there is in the EXIT case and paste it here. Similarly, you have to do it for all the states, covering all the events. After this, you go to the TIME_SET handler and create a similar function.

 

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.