FreRTOS task in a C++ class
Context: Use with Arduino and ESP32
Purpose: Background loop for sampling analog input.
Note: Arduino loop is set as the lowest priority task. The code works fine
Question: Is it appropriate as a FreeRTOS point of vue and could this FreeRTOS usage be improved?
====== Class code (part of) ==========
void AIFreeRTOS::begin(int ADCpin, float ADCmin, float ADCmax, float EUvalueMin, float EUvalueMax, int pace, int priority, int core)
{
_taskPriority = priority;
_taskCore = core ;
_ADCpace = pace ;
startTask();
}
void AIFreeRTOS::startTaskImpl(void* _this)
{
static_cast<AIFreeRTOS*>(_this)->task();
}
void AIFreeRTOS::startTask(void)
{
xTaskCreatePinnedToCore(this->startTaskImpl, "Task", 2048, this, _taskPriority, NULL, _taskCore);
}
void AIFreeRTOS::task(void)
{
unsigned long locCount = 0UL;
unsigned long locAccu = 0UL;
int locADCpace = this->getADCpace();
for (;;)
{
switch(this->getCmd())
{
case START_CONV:
locAccu += this->read();
++locCount;
break;
case STOP_CONV:
this->setCount(locCount);
this->setAccu(locAccu);
this->setState(CONV_STOPPED);
break;
case INIT_CONV:
locCount = 0UL;
locAccu = 0UL;
this->setState(CONV_INITIATED);
break;
}
vTaskDelay(pdMS_TO_TICKS(locADCpace));
}
}
int AIFreeRTOS::read(void)
{
return analogRead(_pin);
}
void AIFreeRTOS::setCount(unsigned long Count)
{
_count = Count;
}
void AIFreeRTOS::setAccu(unsigned long Accu)
{
_accu = Accu;
}
float AIFreeRTOS::EUValue(void)
{
setCmd(STOP_CONV);
while(getState() != CONV_STOPPED){};
_rawADC = computeMeanRaw((float)_accu, (float)_count);
_EUValue = computeEUvalue(_rawADC);
setCmd(INIT_CONV);
while(getState() != CONV_INITIATED){};
setCmd(START_CONV);
return _EUValue;
}
======== Arduino code (part of) ===========
#include "AIFreeRTOS.h"
AIFreeRTOS Anemometer;
AIFreeRTOS Radiation ;
void setup()
{
*/ Note tasks are at the same priority = 1 and running at 200 Hz (5 milllisecs) -> vTaskDelay(pdMS_TO_TICKS(locADCpace));
Anemometer.begin(34, 662.5, 1593.8224, 0.0, 16.2, 5, 1, APP_CPU);
Radiation.begin(39, 208.14, 2516.0, 0.0, 1200.0, 5, 1, APP_CPU);
Anemometer.setCmd(START_CONV);
Radiation.setCmd(START_CONV);
}
void loop()
{
Anemometer.readEUValue();
Radiation.readEUValue();
delay(1000);
}