FSM Lecture 5: Exercise-001 LED control Mealy machine implementation part 1

  • Post author:
  • Post category:Blog

 

Exercise-001 LED control Mealy machine implementation part 1

 

 

In this article, let’s understand LED control mealy machine implementation.

In our application, we also have a couple of states. We have got four states and a couple of events ‘ON’ and ‘OFF.’ Now all the information let’s define in the application.

enum event{
   ON,
   OFF
};

Let’s use C’s enumeration data type to define the details. We use the ‘enum event,’ and define the ON and OFF. 

 

Enumeration(enum) in C

  • enum or enumeration in C/C++ is a user-defined data type
  • By using enum, you can define your own data type.
  • Provides some clarity to your program 
  • To use enum, enumerator-list must be known in advance (a finite list set of named integer constant values a data type can take  on )

 

Example 1

I want to create a data type to represent the day of a week.

enum Day_of_week
{
   SUNDAY,
   MONDAY,
   TUESDAY
};
enum Day_of_week whatIsToday;
whatIsToday = MONDAY;

In this example, Sunday, Monday, Tuesday are labels. When you use such labels, it adds some clarity to your program. Instead of calling 0, 1, 2, 3, you call it Sunday, Monday, Tuesday, so it adds some clarity to your program. But these are not just labels; these are named integer constants. That’s why enum binds those named integer constants or labeled integer constants.

In the above example, you see a couple of labels for days of a week, like Sunday, Monday, Tuesday, and other days you can add, separated by a comma. That is called an ‘Enumerator-list,’ defined inside an enum tag. 

‘enum’ is an enum keyword, and ‘Day_of_week’ is an enum tag, or that is also called an identifier. The enumerator-list you have to place inside braces; That whole thing is called ‘enum declaration.’

enum Day_of_week whatIsToday;

whatIsToday = MONDAY;

After that, you create an ‘enum Day_of_week’ enumeration type variable. ‘enum Day_of_week’ is a data type, ‘whatIsToday’ is a variable. And with that variable, you use any of the values in the enumerator-list like SUNDAY, MONDAY, TUESDAY.

 

enum Day_of_week {
SUNDAY,
MONDAY,
TUESDAY,
};

This is a valid enum declaration. The last member can terminate with a comma, or usually, we don’t give a comma.

 

And after that, you can create a variable for enum Day_of_week. 

enum Day_of_week today = MONDAY. 

This is a valid variable creation and initialization.

In C programming, you must mention the ‘enum’ keyword, the tag or identifier, and then create a variable. But in C++, the ‘enum’ keyword is not required.

 

Example 2:

enum {
   RAINING,
   CLOUDY, 
   SUNNY
};

This is also another enum declaration. It doesn’t have any tag—a tagless declaration. You can even use such enums.

 

The c99 specification says,  an enumerator with = defines its enumeration constant as the value of the constant expression.

enum {
RAINING = 30,
CLOUDY, 
SUNNY
};

You can use the assignment operator here to mention the initial constant value. Like 30.

Each subsequent enumerator with no = defines its enumeration constant as the value of the constant expression obtained by adding 1 to the value of the previous enumeration constant. CLOUDY value becomes 31 implicitly, the SUNNY value becomes 32, like that. You can print them, and you would see automatically these labels get a value 1 added to the previous one.

If the first enumerator has no =, the value of its enumeration constant is 0. In example 1, the SUNDAY label has the integer constant 0. 0,1, 2, like that.

 

Example 3:

In this example(figure 2), you add the keyword typedef. It doesn’t have a tag. And after that, mention the typedef alias name activity_t and terminate the declaration with a semicolon. That is a typedef alias name.

Figure 2. Typedef enum (enumeration)
Figure 2. Typedef enum

 

That’s about the enum declarations.

 

Implementation of state machine

  • Nested switch approach
  • State table approach
  • State handler approach

There are many ways to implement the state machine or state machine handler. You can use ‘nested switch’ or ‘nested if-else’ or ‘if-else-if ladder,’ so there is something called ‘state table approach’ and ‘state handler approach.’

Out of these three states, the state handler approach is more efficient, and it helps you implement less redundant code.

But, as a starting point, let’s use a nested switch or nested if-else-if ladder. Below code snippet shows the switch case statement.

void light_state_machine(uint8_t event)
{
  switch(curr_state)
  {
     case LIGHT_OFF:{
      switch(event){
       case ON:{
         light_change_intensity(PIN_LED,LIGHT_BRIGHT_DIM);

       break;
     }
    }
   break;
 }

Switch case Example

 

Serial:

LED control Mealy machine implementation
Figure 3. Arduino serial object

 

How to receive the events?

We will receive the events from the host over the serial communication. For that, we have to refer to the Arduino serial object.

For that, go to documentation, reference, and the serial object you can access Serial.

You understand the serial object and its related APIs or functions in this serial. You have to configure the baudrate, number of bits you want to send as a payload, number of stop bits, etc. So, all those things can be done in one go by using a function called begin().

 

Serial.begin()

LED control Mealy machine, enumeration
Figure 4. Serial.begin()

 

The serial.begin has two flavors here. You have to mention the serial object and its associated method, like that you have to use because it’s a C++ framework. 

enumeration
Figure 5. Serial.begin()

 

All setup related activities you have to do in a setup function. Because the setup function executes only one time.

When you give power to the Arduino board, the microcontroller first jumps to the setup function, and it executes the setup related activities, then it goes and hangs in a loop function. The loop function is executed repeatedly.

Let’s use one baudrate 115200 or any values like 9600, 4800, or 1mbps, upto 2mbps you can use for Arduino Uno.

 

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.