I have inherited some code using Task scheduling that I am not very familiar with and here's a section of code I can't quite wrap my head around.
The code is running on an ESP32-WROOM, DevKitC V4, and I am using the Arduino IDE.
There's a lot of code in several files, so I can't include it all here. But here is an excerpt that I hope is sufficient to help me understand how the scheduling works.
In essence I am trying to understand how hwLoop works.
It seems it is only started once, enters an endless loop, and then somehow is allowed to continue running every now and then by itself, or what is going on? When is it called, how often is it called, does it start more than once, is it interrupted in its running and comes back to where it left off? What is the point of using task scheduling in the case of hwloop?
Please help me understand about this.
#include <TaskScheduler.h>
TaskHandle_t hwLoopTh;
Scheduler hwScheduler;
volatile int16_t rpm=0;
void IRAM_ATTR interruptRpm() {
rpm++;
if(rpm>2000) rpm = 0;
}
setup() {
xTaskCreateUniversal(hwLoop, "HW", 16384, NULL, 1, &hwLoopTh, 0);
}
loop()
{
delay(1000);
}
void hwLoop(void *param) {
bool on = true;
hw_timer_t *timer = NULL;
timer = timerBegin(0, 80, true);
timerAttachInterrupt(timer, &interruptRpm, false);
timerAlarmWrite(timer, 1000000, true);
timerAlarmEnable(timer);
for (;;) {
// some code for calculations etc
if (on) {
hwScheduler.execute();
}
delay(1000);
}
}