I am writing a C++ code to handle my temperature controller. I have decided to learn more object oriented programming and learn more about classes therefore I have chose C++ this time ( I usually code in C ). I have created a simple Controller class where I have my public and private functions. In my controller.h file:
#include "stdint.h"
#include "stdio.h"
#include "params.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#ifdef __cplusplus
extern "C" {
#endif
class Controller
{
private://only accesible for class
int test1;
public:
//accesible outside class
Controller(); // INIT OBJECT
void begin(); // DECLARE DEFAULT
e_thermostat_state Get_state();
void normal_operation_task(void *argument); // FREERTOS TASK FUNCTION PROTOTYPE
};
#ifdef __cplusplus
}
#endif
#endif
And in my controller.c:
#include "Controller.h"
Controller controller_obj;
uint8_t app_temp = 10;
Controller::Controller(){
printf("Controller object created \n");
}
void Controller::begin(){
printf("Initialise the object with the default values \n");
xTaskCreate(this->normal_operation_task,"controller normal operation task",10000,NULL,1,NULL);
}
void Controller::normal_operation_task(void *argument){
for(;;)
{
printf("normal_operation_task\n");
vTaskDelay(1000/portTICK_RATE_MS);
}
}
As you can see when I call begin method, I want to simply start the task. The reason why I want the freertos task to be a part of my controller class is because inside that task I will need access to variables and functions of my controller class.
The error that I am getting:
../components/Controller/Controller.cpp: In member function 'void Controller::begin()':
../components/Controller/Controller.cpp:20:98: error: invalid use of non-static member function 'void Controller::normal_operation_task(void*)'
xTaskCreate(this->normal_operation_task,"thermostat normal operation task",10000,NULL,1,NULL); // receiving commands from main uart
Could someone give me some advice regarding this? I think I have 2 options:
In my class, declare all variables and methods as public so I can access them even if the freertos task is not part of the class.
Create task as part of Controller class so It can use private methods and variables.
UPDATE
I have managed to do it when declared the static function normal_operation_task as been suggested for me. in my controller.h I have declared:
static void Thermostat::normal_operation_task2(Thermostat* thermostat_obj){
for(;;)
{
printf("normal_operation_task\n");
vTaskDelay(1000/portTICK_RATE_MS);
}
}
I am now trying to understand the following:
- Why this function must be declared the static
- Why do I need to pass the pointer to my controller object? Since it is declared as class method, it automatically has access to class function and variables
ISSUE
What is the issue with the following and why is this wrong?
void Controller::begin(){
printf("Initialise the object with the default values \n");
xTaskCreate(this->normal_operation_task3,"thermostat normal operation task",10000,&controller_obj,1,NULL); //receiving commands from main uart
}
void Controller::normal_operation_task3(void* parameters)
{
Controller controller_obj = *((Controller*)parameters);
for(;;)
{
printf("normal_operation_task\n");
vTaskDelay(1000/portTICK_RATE_MS);
}
}