Hi All,
I have this multitasking library that I hope will be useful. GitHub - glutio/Taskfun: Minimalist preemptive multitasking. I would like to ask the community to review and give feedback and/or help test. I submitted it to Arduino library repository and it should be available there shortly. I tested the basic functionality on two AVR and two SAMD21 boards but it needs more testing I think. I bought an Arduino kit for fun and ended up writing this library. I know how to program but I do not know what scenarios are typical in the world of hobby microcontrollers, so asking the community to test this library in typical scenarios reading sensors, displaying temperature or whatever. This is not an RTOS, and it makes no such claims, this is a task switcher which adds minimal but true preemptive multitasking to Arduino sketches on boards that target beginners. Thanks for your feedback!
Some highlights:
- strongly typed argument for tasks instead of void*,
- support for both functions and class methods as tasks,
- 3 task priority levels (maps to % of CPU time),
- SyncVar<> class for synchronized access for simple types (overloads all operators and adds noInterrupts/interrupts())
Here is an example of how you can blink in the main loop and run a chatbot as a task.
#include <Arduino.h>
#include "Taskfun.h"
void chatBot(const char* botName)
{
while(1) {
Serial.print(botName);
Serial.println("> What is your name?");
while(!Serial.available());
auto userName = Serial.readString();
Serial.print(botName);
Serial.print("> Hello, ");
Serial.println(userName);
}
}
void setup() {
Serial.begin(115200);
while(!Serial);
pinMode(LED_BUILTIN, OUTPUT);
noInterrupts();
setupTasks();
runTask(chatBot, "Arduino");
interrupts();
}
void loop() {
digitalWrite(LED_BUILTIN, HIGH);
delay(1000);
digitalWrite(LED_BUILTIN, LOW);
delay(1000);
}