FSM Lecture 65: Exercise-007 Adding class operations

  • Post author:
  • Post category:Blog

 

Exercise-007 Adding class operations

 

 

Adding class operations

We have defined our ISR, and from the ISR, we have to increment that current time variable.

Figure 1. Define ISR
Figure 1. Define ISR

 

But, there is one problem. Since the current time, we have selected it as a non-static variable. Whenever you create an object of the main structure, every time one copy of the current time variable will be there. Currently, we made it current time as a private attribute of the object, but that is not a good idea. 

We’ll make some changes here. We will make it a static variable. That means a single copy variable, which is generic to all the objects of this main structure because that makes sense. The current time variable can’t be a private variable of each object because a current time variable tracks the time. Time is the same for all the objects of this main structure. 

Let’s consider a scenario; you have created two objects: object1 and object2 of this main structure.

 

Figure 2. Main structure
Figure 2. Main structure

 

Object1 drives Display-1, and it displays Time Zone-X’s time in 24H format; and object2 drives another display, and it displays Time Zone-Y’s time in 12H format. No matter how many objects you create, the time is the same. The current time should be generic to these objects.

Object1 takes the current time, and it converts that into a Time Zone’s format, and it displays that. Likewise, object 2 does. That means we will keep this attribute outside these objects, making this current time attribute a static variable. There could be a good idea.

 

In ClockAlarm_SM.cpp, I will select the current time and make it static.

Figure 3. Class operation
Figure 3. Class operation

 

Let’s give one class operation to get that current time. Name it as get_curr_time;

I want to make it a static one. Because it accesses the static attribute that we just added, that is the current time.

It returns the current time; I will keep the return type as uint32_t. And you can make this public, no problem. The visibility doesn’t matter here, in the C; you can keep this public, this doesn’t affect this function; the code will be put later.

This returns the value of the current time. We will now generate the code. Code is generated. Let’s look at the code.

Adding class operations
Figure 4. Code

 

Here you see, the current time now went outside. Clock_Alarm_get_curr_time is a get current time; Get current time static function to return the value of this variable. We will implement this function later. 

 

Adding class operations
Figure 5. Another class operation

 

Now, let’s implement another function or another class operation. Here I’ll call this update_curr_time; the return type is void, which is also a static function. Why is it a static function? Because it cannot access any private variables or private attributes of clock alarm. It can only manipulate the static variables like the current time.

And also, note that, as I mentioned previously, the static class operations will not have access to the ‘me’ pointer. 

 

Adding class operations
Figure 6. code

 

Let’s go back to the code. And here you see(Figure 6), we got the Clock_Alarm_update_curr_time function. Here will put some code. If we have to increment this current time variable ++. if (++Clock_Alarm_curr_time == MAX_TIME), then we’ll make Clock_Alarm_curr_time as 0. And the MAX_TIME variable I just defined in the ClockAlarm_SM.h, and this is 24* 3600*10. That’s a MAX_TIME.

 

In the ClockAlarm_SM.h, define the MAX_TIME macro, as shown in Figure 7.

Adding class operations
Figure 7. Define the MAX_TIME macro

 

The Clock_Alarm_update_curr_time, this function will call from the interrupt service routine. We will call that from here. Let’s go to the interrupt service routine, and you call that. (Figure 8).

Adding class operations
Figure 8. Interrupt service routine

 

In the function update_curr_time, you have to add the code here. Because since this software manages the file. That’s why this code has to be there on the QM tool. In the update_curr_time function, add that code.

Adding class operations
Figure 9. Add the code

 

Now, let’s build the code. So, whenever an interrupt happens, the Clock_Alarm_Curr_time variable will be updated.

 

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