This is more a software design question.
so I have this SystemTask which contains a message queue to which other tasks write via Push method.
However Push is currently a non-static method which means there should be a SystemTask’s instance referenced in each class to invoke on.
// system.hpp
class SystemTask
{
QueueHandle_t mSystemQueue;
// ...
};
// system.cpp
void SystemTask::Start()
{
mSystemQueue = xQueueCreate(queueSize, itemSize);
if (xTaskCreate(SystemTask::Process, "Run", 300, this, 0, &taskHandle) != pdPASS)
{
// ...
}
}
void SystemTask::Process(void* instance)
{
auto* app = static_cast<SystemTask*>(instance);
app->Run();
}
void SystemTask::Run()
{
// start peripherals
while(true)
{
// block on the message
if (xQueueReceive(mSystemQueue, &msg, portMAX_DELAY) == pdPASS)
{ // ...
}
}
}
void SystemTask::Push(SystemTask::Message message) // write to queue
{
BaseType_t xHigherPriorityTaskWoken;
xHigherPriorityTaskWoken = pdFALSE;
xQueueSendFromISR(mSystemQueue, &message, &xHigherPriorityTaskWoken);
}
UART IRQ invokes a user-handling callback upon receiving an end-of-byte character however
the problem is that the callback (UartApp::Callback) is static which means it can’t use systemTask instance to Push.
I am debating whether:
- SystemTask instance is be static since there’s going to be a single instance rather than each class storing a reference to
SystemTask -
Pushmethod is only made static
It makes sense for UartApp to not have any instance and only contain static methods.
Pretty sure there must be a legit way to handle this for the sake of communication between different tasks (well, here it’s between ISR and a task!)
// uart.cpp
void UART_IRQ()
{
// ...
// end of byte is received, delegate the handling of the input to callback
if (data == '\r')
{
userCallback(fifo); // resides in a higher layer i.e UartApp::Callback
}
}
// uart_app.cpp
void UartApp::Callback(FIFO fifo) // static method
{
Message msg = Process(fifo);
systemTask.Push(msg); // systemTask & Push have to be static
}
// main.cpp
Uart uart{UART0, commParams, UartApp::Callback}; // 3rd argument
SystemTask systemTask{uart};
int main()
{
systemTask.Start();
vTaskStartScheduler();
}