103 lines
3.5 KiB
C++
103 lines
3.5 KiB
C++
/*
|
|
* Task Management Example with std::bind
|
|
*
|
|
* This example demonstrates how to use the TaskManager with std::bind
|
|
* to register member functions, lambdas, and other callable objects.
|
|
*/
|
|
|
|
#include <Arduino.h>
|
|
#include <functional>
|
|
#include "TaskManager.h"
|
|
#include "NodeContext.h"
|
|
|
|
// Example class with methods that can be bound
|
|
class ExampleClass {
|
|
public:
|
|
void method1() {
|
|
Serial.println("[ExampleClass] Method 1 called");
|
|
}
|
|
|
|
void method2() {
|
|
Serial.println("[ExampleClass] Method 2 called");
|
|
}
|
|
|
|
void methodWithParam(int value) {
|
|
Serial.printf("[ExampleClass] Method with param called: %d\n", value);
|
|
}
|
|
};
|
|
|
|
// Example custom task functions (legacy style)
|
|
void customTask1() {
|
|
Serial.println("[CustomTask1] Executing custom task 1");
|
|
}
|
|
|
|
void customTask2() {
|
|
Serial.println("[CustomTask2] Executing custom task 2");
|
|
}
|
|
|
|
void periodicMaintenance() {
|
|
Serial.println("[Maintenance] Running periodic maintenance");
|
|
}
|
|
|
|
void setup() {
|
|
Serial.begin(115200);
|
|
Serial.println("Task Management Example with std::bind");
|
|
|
|
// Create context and task manager
|
|
NodeContext ctx;
|
|
TaskManager taskManager(ctx);
|
|
|
|
// Create an instance of our example class
|
|
ExampleClass example;
|
|
|
|
// Method 1: Register tasks using std::bind for member functions
|
|
taskManager.registerTask("member_method_1", 2000,
|
|
std::bind(&ExampleClass::method1, &example));
|
|
|
|
taskManager.registerTask("member_method_2", 3000,
|
|
std::bind(&ExampleClass::method2, &example));
|
|
|
|
// Method 2: Register a task with a lambda that calls a method with parameters
|
|
taskManager.registerTask("method_with_param", 4000,
|
|
std::bind([](ExampleClass* obj) {
|
|
obj->methodWithParam(42);
|
|
}, &example));
|
|
|
|
// Method 3: Register legacy global functions (still supported)
|
|
taskManager.registerTask("custom_task_1", 5000, customTask1);
|
|
taskManager.registerTask("custom_task_2", 10000, customTask2);
|
|
taskManager.registerTask("maintenance", 30000, periodicMaintenance);
|
|
|
|
// Method 4: Register a lambda function directly
|
|
taskManager.registerTask("lambda_function", 6000, []() {
|
|
Serial.println("[Lambda] Lambda function called");
|
|
});
|
|
|
|
// Method 5: Register a task that calls multiple methods
|
|
taskManager.registerTask("multi_method", 7000,
|
|
std::bind([](ExampleClass* obj) {
|
|
obj->method1();
|
|
obj->method2();
|
|
}, &example));
|
|
|
|
// Initialize and start all tasks
|
|
taskManager.initialize();
|
|
|
|
// Print initial task status
|
|
taskManager.printTaskStatus();
|
|
|
|
Serial.println("\n=== Task Registration Examples ===");
|
|
Serial.println("1. member_method_1: Uses std::bind(&ExampleClass::method1, &example)");
|
|
Serial.println("2. member_method_2: Uses std::bind(&ExampleClass::method2, &example)");
|
|
Serial.println("3. method_with_param: Uses lambda with std::bind for parameter passing");
|
|
Serial.println("4. custom_task_1: Legacy global function registration");
|
|
Serial.println("5. lambda_function: Direct lambda registration");
|
|
Serial.println("6. multi_method: Lambda that calls multiple methods");
|
|
Serial.println("=====================================\n");
|
|
}
|
|
|
|
void loop() {
|
|
// The TaskManager handles all task execution
|
|
// No need to call individual task functions
|
|
yield();
|
|
}
|