FSM Lecture 53- Adding class operation

  • Post author:
  • Post category:Blog

 

Adding class operation

 

 

Adding class operation:

An operation added to a Class is called Class operation.

1. Static Class operation

  • In ‘C’, functions that don’t have access to the ‘me’ pointer( pointer of the instance of the main application structure)

2. Non-static Class operation

  • In C, a non-static class operation corresponds to a function that (by a coding convention) takes the “me” pointer to the struct with the class attribute. This is a function that can access instance of a main application structure(via ‘me’ pointer)

3. Class constructor

  • A function of the class/structure used to initialize the instance of a structure. The return type must be void. 

Just explore more about the class operation here: QM: Working with Class Operations

 

Let’s head over to the model. Now let’s try to create some operations.

  1. First of all, QHsmTst:QHsm is the main structure or main class. And let me add a class operation by choosing the option Add operation, as shown in Figure 1.
  2. Give one name. Let me create a static operation this_is_static_operation. To make this a static operation, you have to check static. That’s how you add a static operation.
Figure 1. Creation of static class Operation
Figure 1. Creation of static class Operation

 

You can add parameters here if you want. 

  1. For example, right-click on this_is_static_operation → Add parameter 
  2. Let’s name it ‘param1’. Give the type is ‘int,’ as shown in Figure 2. 
Figure 2. Adding a parameter to class operation
Figure 2. Adding a parameter to class operation

 

Generate the code. You can see that a ‘static’ operation is created static void QHsmTst_this_is_static_operation; the parameters are int param1, as shown in Figure 3. But the ‘me’ pointer is not available here. And you can also see that the QHsmTst structure name is prefixed here, which signifies this is a class operation.

Figure 3. Static class operation
Figure 3. Static class operation

 

Now let’s add another operation, which is a non-static class operation. The class operation Name is this_is_non_static_operation; you can keep this as private; the return type is void. (Figure 4)

Figure 4. Creation of non static class operation
Figure 4. Creation of non static class operation

 

In Figure 5, you can see that static void QHsmTst_this_is_non_static_operation is a non-static operation, and it receives the ‘me’ pointer. Pointer to our main application structure QHsmTst. And by using this ‘me’ pointer, you can access all the member elements of this QHsmTst structure.  

Figure 5. Non static class operation
Figure 5. Non static class operation

 

this_is_static_operation and  this_is_non_static_operation these 2 are class operation.

You can also add a free operation. For that, you have to click on the HSM package and select Add Operation.

Let’s name it  this_is_free_operation. The return type is void. You can control the visibility here; either make it a file scope or global. If you want to share that, if other files share this operation, you can keep it global—otherwise, file scope.

Figure 6. Adding a Free operation
Figure 6. Adding a Free operation

 

This free operation is outside QHsmTst:QHsm class. You have to use these directives to generate the code. Let me create that at the end. I will use $define, drag and drop this_is_free_operation, as shown in Figure 6. 

Generate code. You see that this_is_free_operation is created, as shown in Figure 7.  You can even assign any parameters here, so use the code field(shown in Figure 6) to put the code and add parameters. That’s about operations, adding operations to a class or structure.

Figure 7.Free operation
Figure 7. Free operation

 

Now here QHsmTst_ctor: void is a constructor of our class, which is created as a free operation. The return type must be void and make it global because QHsm_ctor we are going to share with the main.cpp. After that, let’s create the code for that. $define, drag and drop QHsmTst_ctor: void, as shown in Figure 8. 

Figure 8. Creation of constructor of our class FSM class operations
Figure 8. Creation of constructor of our class

 

The ctor is created, as shown in Figure 9.

Figure 9. QHsmTst_ctor() FSM class operations
Figure 9. QHsmTst_ctor()

 

Let’s call this QHsmTst_ctor() from main.cpp, as shown in Figure 10.

Figure 10. calling QHsmTst_ctor from main.cpp - FSM class operations
Figure 10. calling QHsmTst_ctor from main.cpp

 

Now, you have to give the prototype that you can implement in QHSM_Test.h. So, go to QHSM_Test.h, and here provide the declaration of the constructor, as shown in Figure 11.

Figure 11.prototype of QHsmTst_ctor - FSM class operations
Figure 11. Prototype of QHsmTst_ctor

 

In the QHSM_Test.h, you can see that the declaration of ctor is created, as shown in Figure 12. So, the main.c calls that.

Figure 12. Declaration of ctor,  FSM class operations
Figure 12. Declaration of ctor

 

Here you have to call the ctor of the superclass. So, let’s go to our class ctor. 

Figure 13.Constructor of the superclass FSM class operations
Figure 13. Constructor of the superclass

 

QHsm_ctor use that here. So, the first parameter is a pointer to the superclass. So, that we have already created super_QHsmTst, the second parameter is the initial state handler’s address. 

The address of the initial state handler is &QHsmTst_initial. You have to use one macro; the macro is a Q_STATE_CAST. This macro you have to use. This is just used for casting, and you need not do it manually; it is shown in Figure 13.

 

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.