FSM Lecture 85| Exercise-008:Implementation part 4

  • Post author:
  • Post category:Blog

 

Exercise-008:Implementation part 4

 

 

In this article, now let’s generate some code. In the source(../src),  it has got a couple of files like ClockAlarm_SM.cpp, ClockAlarm_SM.h, alarm.cpp, alarm.h, button.cpp, button.h. (Figure 1)

First of all, ClockAlarm_SM.cpp. So, this is just like the previous code. We are going to modify this. And this implements the state machine of the ClockAlarm active object and its associated ClockAlarm_SM.h file.

ClockAlarm_SM.cpp file the source
Figure 1. ClockAlarm_SM.cpp file

 

ClockAlarm_SM.h file the source
Figure 2. ClockAlarm_SM.h file

 

In the ClockAlarm_SM.h file, I added a few more signals; SET_SIG, OK_SIG, and ABRT_SIG were over there. I just added ALARM_SIG, and another one I added ALARM_CHECK_SIG. Please note this change.

 

After that, alarm.cpp. So this is a source file for implementing the component alarm and its associated state machine. And its associated header file alarm.h, which is now empty. We will fill that in later.

alarm.cpp file the source
Figure 3. alarm.cpp file

 

And Button.cpp or I would rather call this as button_SM.cpp. This implements the state machine of the button active object and its associated header file Button_SM.h. 

Button_SM.cpp file the source
Figure 4. Button_SM.cpp file

 

ClockALarm_SM.h file the source
Figure 5. ClockAlarm_SM.h file

 

In the ClockAlarm_SM.h file, you also see one more enum definition here for setting. There are three options, NO_SETTING, CLOCK_SETTING, ALARM_SETTING.

 

Now, let’s go to alarm.cpp. First, let’s modify this file.

alarm.cpp file
Figure 6. alarm.cpp file

 

This is a .cpp file. Just include these header files, like ‘Arduino.h’, ‘qpn.h’, its associated header file, and the ‘ClockAlarm_SM.h.’ Because of this alarm class containers header file. 

After that, generate the code. Here, let’s use the code generation directive $define and drag and drop this Alarm: QHsm. And also, you should define the ctor. (Figure 6)

 

Let’s go to the alarm.h and here you have to declare. Declare this alarm class and also declare the ctor, as shown in Figure 7.

 alarm.h file
Figure 7. alarm.h file

 

button_SM.cpp ClockAlarm
Figure 8. button_SM.cpp

 

Now, let’s go to the button_SM.cpp, and here also include all the header files that you included in the alarm.cpp. You don’t need #include “alarm.h”. Remove that. You may need this “button_SM.h.” 

Here, define button class and also define the button_ctor, as shown in Figure 8. 

 

And go to button_SM.h, declare button class and declare Button_ctor, as shown in Figure 9.

button_SM.h file, ClockAlarm
Figure 9. button_SM.h file

 

Now, let’s generate the code. There are a couple of issues. Let’s see what that is.

Issues in code, ClockAlarm
Figure 10. Issues in code

 

Error is saying something is broken. In the ClockAlarm_SM.cpp file, the package name is not HSMs. Now the package name has changed. It must be AOs. Let’s replace that. Save it. (as shown in Figure 11)

Implementation part 4  ClockAlarm
Figure 11. ClockAlarm_SM.cpp file

 

Go to ClockAlarm_SM.h, and here the package name is AOs. (Figure 12)

Implementation part 4 ClockAlarm
Figure 12. ClockAlarm_SM.h file

 

Let’s generate. Now, it is fine, with 0 errors. 

And we also have this global free attribute: AO_ClockAlarm:QActive *const and AO_Button:QActive *const. For that also, you have to generate the code. 

That’s why, in the ClockAlarm_SM.cpp, I have this code, as shown in Figure 13. I have defined that here in the .cpp  $define${AOs::AO_ClockAlarm}

Same thing you have to do in button_SM.cpp.

 

Implementation part 4 ClockAlarm
Figure 13.  Global free attribute

 

In the button_SM.cpp, define that pointer variable, that is AO_Button:QActive *const. 

Implementation part 4 ClockAlarm
Figure 14. button_SM.cpp file

 

And it should be shared with other files. That’s why we have to declare that in the button_SM.h file.

 

We will do that in the button_SM.h file. $declare${AOs::AO_Button} shown in Figure 15.

Implementation part 4 the source -ClockAlarm
Figure 15. button_SM.h file

 

Let’s go to the IDE, and our project is 008ClockAlarm_AO. In the source, as you can see here, we got a couple of files here, as shown in Figure 16.

Implementation part 4 the source ClockAlarm
Figure 16. 008ClockAlarm_AO project IDE

 

We should also update the library path. Go to 007ClockAlarm, go to the platformio.ini, copy the library path code. And go to your current project 008ClockAlarm_AO, go to platform.ini and paste that code, as shown in Figure 17.

Implementation part 4 the source.  ClockAlarm
Figure 17. 008ClockAlarm_AO, Library path code

 

Also, go to your old project, and you need these files lcd.cpp, lcd.h. Copy both files, go to your current project, and goto source and paste it here. You can see the lcd. cpp and lcd.h file shown in Figure 16.

 

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.

Leave a Reply