FSM Lecture 62: Exercise-007 Adding main application object and constructor

 

Exercise-007 Adding main application object and constructor

 

 

This article will learn about adding the main application object and constructor.

Go to ClockAlarm_SM.h, and let’s first define the signals. These are the signals, as shown in Figure 1. Let’s create an enum ClockAlarm_Signals. And don’t forget to initialize the first signal to  Q_USER_SIG.

Figure 1.ClockAlarm_SM.h file
Figure 1. ClockAlarm_SM.h file

 

And after that, let’s go to ClockAlarm_SM.cpp. And here, let’s give the declarations. So, type declare; you can drag and drop the class name Clock_Alarm: QHsm. 

After that, just add the required  header files here, 

#include<Arduino.h>  

#include “qpn.h” 

#include “Lcd.h”  

#include “ClockAlarm_SM.h” .(Figure 2)

Figure 2. ClockAlarm_SM.cpp file
Figure 2. ClockAlarm_SM.cpp file

 

And in the platformio.ini file, create the lib_deps variable if you don’t have it. You can add the LCD library from the previous project. After that, here, create a second line and give the path (Figure 3).

Figure 3. platformio.ini file
Figure 3. platformio.ini file

 

Let’s go to the next step. Let’s generate the definitions. $define, and you can drag and drop the class name Clock_Alarm: QHsm, as shown in Figure 4.  Save and generate the code.

Figure 4. Give the definition to ClockAlarm_SM.cpp file
Figure 4. Give the definition to ClockAlarm_SM.cpp file

 

Let’s create an object of ClockAlarm structure, our main application object. So, we will add that as a static variable. Right-click on the class name ClockAlarm, and click on Add Attribute. 

Name is ‘obj.’ Its type is Clock_Alarm, and it is a ‘static’ variable, and I’ll keep the visibility to ‘private’, as shown in Figure 5.

Figure 5. creating an object of ClockAlarm structure
Figure 5. creating an object of ClockAlarm structure

 

Now, let’s save and generate the code. Look at Figure 6, here we created an object of Clock_Alarm type.

Adding main application object and constructor
Figure 6. Object of ClockAlarm

 

Now, let’s provide a constructor for our ClockAlarm class or structure. Let’s add a constructor. The constructor can be added as a free operation. 

So, now let’s create a constructor for our class. In the constructor, you can initialize any attribute of the object if you want or use that constructor to call the constructor of the superclass, as we did in the previous exercise.

Remember that the constructor should be shared with the main.cpp. That’s why we will create the constructor as a free operation. 

Adding main application object and constructor
Figure 7. Create the constructor as a free operation

Let’s click on the package name HSMs and create the Add operation. 

We will give the name of our constructor. The name of the class is Clock_Alarm_ctor, and the return type is void because the return type of the ctor must be void. After that, you can keep visibility as a global function, and you can put any code here.

QHsm_ctor(&Clock_Alarm_obj.super, Q_STATE_CAST(&Clock_Alarm_initial));

In the code section, you can call the constructor of the superclass, that is, QHsm_ctor. And here, the first argument is a pointer to the superclass, that is, the address of our object, so Clock_Alarm_obj.super. The second argument here is the address of the initial state handler, Clock_Alarm_initial. What is an initial state handler? In our code, that is this macro Q_STATE_CAST. Use that macro and mention the initial state handler.

You can even create a couple of attributes or parameters. And by using that parameter, the main function can pass a couple of initial values to the constructor, so that the constructor can initialize the attributes of the main class object. You can also do that. Let’s save and generate the code. 

 

In the ClockAlarm_SM.h, let’s declare the constructor, as shown in Figure 8.

Adding main application object and constructor
Figure 8. Declare the constructor in the ClockAlarm_SM.h file

 

In ClockAlarm_SM.cpp, we can define that, as shown in Figure 9.

Adding main application object and constructor
Figure 9. Define the constructor in the ClockAlarm_SM.cpp file

 

Let’s generate the code. We got the constructor.

Adding main application object and constructor
Figure 10. Constructor

 

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.