I'm trying to understand FreeRTOS building a C++ class which contains a LED blinking task. But in the task body (which is also a class member), other class members i.e. LED1_delay are empty/not-initialized. It seems like the task body was linked to another instance.
Class function which sets the blinking frequency and starts the task: (gpio.cpp)
void c_gpio::LED_blink_on(float frequency){
LED1_delay=(uint32_t)(1000/frequency);
if(LEDTaskcreated!=true){
//Create task
LEDTaskHandle = osThreadNew(startTask_LED1_blinker, LEDTask_args, &LEDTask_attributes);
LEDTaskcreated=true;
}
}
Wrapper function avoiding static declaration: (gpio.cpp)
void c_gpio::startTask_LED1_blinker(void* _this){
static_cast<c_gpio*>(_this)->taskbody_LED1_blinker((void*)0);
}
Task body: (gpio.cpp)
void c_gpio::taskbody_LED1_blinker(void* arguments){
//All class members are uninitialized here..
while(1)
{
HAL_GPIO_TogglePin(GP_LED_1_GPIO_Port,GP_LED_1_Pin);
osDelay(this->LED1_delay); //LED1_delay is not set.
}
}
Class declaration (gpio.hpp)
class c_gpio{
public:
void LED_blink_on(uint8_t LED_id, float frequency);
private:
static void startTask_LED1_blinker(void* _this);
void taskbody_LED1_blinker(void *arguments);
uint32_t LED1_delay;
//Task handles & attributes
osThreadId_t LEDTaskHandle;
osThreadAttr_t LEDTask_attributes;
uint16_t LEDTask_args[2];
};
Instantiation (main.cpp)
#include "gpio.hpp"
c_gpio gpio;
int main(void)
{
gpio.LED_blink_on(1,10);
/* Init scheduler */
osKernelInitialize();
/* Start scheduler */
osKernelStart();
}
I thought, the members taskbody_LED1_blinker() and LED1_delay are belonging to the same instance. But this doesn't seem to be so. Why? How to properly construct such a task?