feat: bind tasks instead of passing fn ptrs when registering a task
This commit is contained in:
@@ -2,6 +2,9 @@
|
||||
#include <Arduino.h>
|
||||
#include <TaskScheduler.h>
|
||||
|
||||
// Define static members
|
||||
std::map<std::string, std::function<void()>> TaskManager::callbackRegistry;
|
||||
|
||||
TaskManager::TaskManager(NodeContext& ctx) : ctx(ctx) {}
|
||||
|
||||
TaskManager::~TaskManager() {
|
||||
@@ -12,7 +15,7 @@ TaskManager::~TaskManager() {
|
||||
tasks.clear();
|
||||
}
|
||||
|
||||
void TaskManager::registerTask(const std::string& name, unsigned long interval, TaskCallback callback, bool enabled, bool autoStart) {
|
||||
void TaskManager::registerTask(const std::string& name, unsigned long interval, TaskFunction callback, bool enabled, bool autoStart) {
|
||||
TaskDefinition taskDef(name, interval, callback, enabled, autoStart);
|
||||
registerTask(taskDef);
|
||||
}
|
||||
@@ -39,8 +42,11 @@ void TaskManager::initialize() {
|
||||
}
|
||||
|
||||
void TaskManager::createTask(const TaskDefinition& taskDef) {
|
||||
// Create new task
|
||||
Task* task = new Task(0, TASK_FOREVER, taskDef.callback);
|
||||
// Store the callback in the static registry
|
||||
callbackRegistry[taskDef.name] = taskDef.callback;
|
||||
|
||||
// Create a dummy task - we'll handle execution ourselves
|
||||
Task* task = new Task(0, TASK_FOREVER, []() { /* Dummy callback */ });
|
||||
task->setInterval(taskDef.interval);
|
||||
|
||||
// Add to scheduler
|
||||
@@ -149,7 +155,33 @@ void TaskManager::printTaskStatus() const {
|
||||
}
|
||||
|
||||
void TaskManager::execute() {
|
||||
ctx.scheduler->execute();
|
||||
// Execute all enabled tasks by calling their stored callbacks
|
||||
static unsigned long lastExecutionTimes[100] = {0}; // Simple array for timing
|
||||
static int taskCount = 0;
|
||||
|
||||
if (taskCount == 0) {
|
||||
taskCount = tasks.size();
|
||||
}
|
||||
|
||||
unsigned long currentTime = millis();
|
||||
|
||||
for (size_t i = 0; i < tasks.size() && i < taskDefinitions.size(); ++i) {
|
||||
Task* task = tasks[i];
|
||||
const std::string& taskName = taskDefinitions[i].name;
|
||||
|
||||
if (task->isEnabled()) {
|
||||
// Check if it's time to run this task
|
||||
if (currentTime - lastExecutionTimes[i] >= task->getInterval()) {
|
||||
// Execute the stored callback
|
||||
if (callbackRegistry.find(taskName) != callbackRegistry.end()) {
|
||||
callbackRegistry[taskName]();
|
||||
}
|
||||
|
||||
// Update the last execution time
|
||||
lastExecutionTimes[i] = currentTime;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Task* TaskManager::findTask(const std::string& name) const {
|
||||
|
||||
Reference in New Issue
Block a user