feat: bind tasks instead of passing fn ptrs when registering a task
This commit is contained in:
@@ -1,43 +1,84 @@
|
||||
/*
|
||||
* Task Management Example
|
||||
* Task Management Example with std::bind
|
||||
*
|
||||
* This example demonstrates how to use the TaskManager to add custom tasks
|
||||
* to the SPORE system. The TaskManager provides a clean interface for
|
||||
* registering, controlling, and monitoring system tasks.
|
||||
* 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 custom task functions
|
||||
// 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");
|
||||
// Add your custom logic here
|
||||
}
|
||||
|
||||
void customTask2() {
|
||||
Serial.println("[CustomTask2] Executing custom task 2");
|
||||
// Add your custom logic here
|
||||
}
|
||||
|
||||
void periodicMaintenance() {
|
||||
Serial.println("[Maintenance] Running periodic maintenance");
|
||||
// Add maintenance logic here
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println("Task Management Example");
|
||||
Serial.println("Task Management Example with std::bind");
|
||||
|
||||
// Create context and task manager
|
||||
NodeContext ctx;
|
||||
TaskManager taskManager(ctx);
|
||||
|
||||
// Register custom tasks with different intervals
|
||||
taskManager.registerTask("custom_task_1", 5000, customTask1); // Every 5 seconds
|
||||
taskManager.registerTask("custom_task_2", 10000, customTask2); // Every 10 seconds
|
||||
taskManager.registerTask("maintenance", 30000, periodicMaintenance); // Every 30 seconds
|
||||
// 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();
|
||||
@@ -45,20 +86,14 @@ void setup() {
|
||||
// Print initial task status
|
||||
taskManager.printTaskStatus();
|
||||
|
||||
// Example: Disable a task temporarily
|
||||
taskManager.disableTask("custom_task_2");
|
||||
Serial.println("Disabled custom_task_2");
|
||||
|
||||
// Example: Change task interval
|
||||
taskManager.setTaskInterval("custom_task_1", 2000); // Change to 2 seconds
|
||||
Serial.println("Changed custom_task_1 interval to 2 seconds");
|
||||
|
||||
// Re-enable the disabled task
|
||||
taskManager.enableTask("custom_task_2");
|
||||
Serial.println("Re-enabled custom_task_2");
|
||||
|
||||
// Print updated 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() {
|
||||
|
||||
Reference in New Issue
Block a user