Exercise-008:Implementation part 3
Let’s create the ctor for Clock_Alarm, which is already there, but the code is not present. We have to update the code for the ctor.
We also have one more active object now, ‘Button.’ We have to create the ctor for this Button class, and we can do that by creating one free operation.
Select this package AOs and click on Add Operation. And the name of the operation is a Button_ctor; the return type is void; the visibility is global, as shown in Figure 1. This is a global function. Because the ctor must be called from the external file, such as main. We will write the code later.
For the Alarm class, we should also give one ctor. Let’s create a ctor for the Alarm class. We’ll name it Alarm_ctor; the return type is void; visibility is global. And we will add the code later, as shown in Figure 2.
Let’s first go to the ctor of Clock_Alarm. That is Clock_Alarm_ctor: void (as shown in Figure 3). What’s the code for that? From here you have to call the ctor of the superclass.
In the previous exercise 007ClockAlarm, in the Clock_Alarm_ctor code (as shown in Figure 4), you called the ctor of the superclass, and you provided the address of the initial state handler of the state machine.
Instead of QHsm ctor, now you should use a slightly different function called QActive_ctor because the superclass is now QActive.
Here in the QP nano API reference( Figure 5), for active objects, you have to use QActive_ctor.
The syntax is the same; you have to mention the address of the initial state handler.
Now, let’s go to the current project, 008ClockAlarm_AO. Select the ctor of Clock_Alarm, the same code we have to use here.
QActive_ctor(AO_ClockAlarm, Q_STATE_CAST(&Clock_Alarm_initial));
Instead of QHsm_ctor, we have to use QActive_ctor. You have to give a pointer to the superclass, or you can use a here AO_ClockAlarm, which we created here. And then mention the address of the initial state handler &Clock_Alarm_initial.
Now the same thing has to be copied for the Button_ctor. Go to the Button_ctor and the same code.
QActive_ctor(AO_Button, Q_STATE_CAST(&Button_initial));
Here, instead of AO_ClockAlarm, it is AO_Button. And here, instead of Clock_Alarm, this is Button, as shown in Figure 8.
Now, let’s quickly finish the Alarm_ctor.
QHsm_ctor(me, Q_STATE_CAST(&Alarm_initial));
The alarm class is based on QHsm. That’s why it is not QActive here, QHsm.
And a pointer to the superclass, you use me here. Because the ctor of this will be called from its container, the container must send the address.
And instead of Button_initial, this is Alarm_initial. Because the Alarm class also will have its own state machine, so will draw that later. If it has its own state machine, then it will have its initial transition. So, Alarm_initial signifies that.
We will add a parameter to Alarm_ctor for the ‘me’ pointer. Add parameter, name is ‘me’, type is Alarm *const. Because this ‘me’ should be sent by the container.
FastBit Embedded Brain Academy Courses
Click here: https://fastbitlab.com/course1