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.
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)
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).
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.
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.
Now, let’s save and generate the code. Look at Figure 6, here we created an object of Clock_Alarm type.
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.
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.
In ClockAlarm_SM.cpp, we can define that, as shown in Figure 9.
Let’s generate the code. We got the constructor.
FastBit Embedded Brain Academy Courses,
Click here: https://fastbitlab.com/course1