feat: register tasks outside of main
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
/*
|
||||
* Task Management Example with std::bind
|
||||
* Task Management Example with std::bind and Class-Based Registration
|
||||
*
|
||||
* This example demonstrates how to use the TaskManager with std::bind
|
||||
* to register member functions, lambdas, and other callable objects.
|
||||
* and how classes can register their own tasks.
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
@@ -10,23 +10,101 @@
|
||||
#include "TaskManager.h"
|
||||
#include "NodeContext.h"
|
||||
|
||||
// Example class with methods that can be bound
|
||||
class ExampleClass {
|
||||
// Example service class that registers its own tasks
|
||||
class SensorService {
|
||||
public:
|
||||
void method1() {
|
||||
Serial.println("[ExampleClass] Method 1 called");
|
||||
SensorService(NodeContext& ctx, TaskManager& taskMgr) : ctx(ctx), taskManager(taskMgr) {
|
||||
// Register all sensor-related tasks in constructor
|
||||
registerTasks();
|
||||
}
|
||||
|
||||
void method2() {
|
||||
Serial.println("[ExampleClass] Method 2 called");
|
||||
void readTemperature() {
|
||||
Serial.println("[SensorService] Reading temperature");
|
||||
// Simulate temperature reading
|
||||
temperature = random(20, 30);
|
||||
Serial.printf("[SensorService] Temperature: %d°C\n", temperature);
|
||||
}
|
||||
|
||||
void methodWithParam(int value) {
|
||||
Serial.printf("[ExampleClass] Method with param called: %d\n", value);
|
||||
void readHumidity() {
|
||||
Serial.println("[SensorService] Reading humidity");
|
||||
// Simulate humidity reading
|
||||
humidity = random(40, 80);
|
||||
Serial.printf("[SensorService] Humidity: %d%%\n", humidity);
|
||||
}
|
||||
|
||||
void calibrateSensors() {
|
||||
Serial.println("[SensorService] Calibrating sensors");
|
||||
// Simulate calibration
|
||||
calibrationOffset = random(-2, 3);
|
||||
Serial.printf("[SensorService] Calibration offset: %d\n", calibrationOffset);
|
||||
}
|
||||
|
||||
void printStatus() {
|
||||
Serial.printf("[SensorService] Status - Temp: %d°C, Humidity: %d%%, Offset: %d\n",
|
||||
temperature, humidity, calibrationOffset);
|
||||
}
|
||||
|
||||
private:
|
||||
void registerTasks() {
|
||||
// Register all sensor tasks using std::bind
|
||||
taskManager.registerTask("temp_read", 2000,
|
||||
std::bind(&SensorService::readTemperature, this));
|
||||
taskManager.registerTask("humidity_read", 3000,
|
||||
std::bind(&SensorService::readHumidity, this));
|
||||
taskManager.registerTask("calibrate", 10000,
|
||||
std::bind(&SensorService::calibrateSensors, this));
|
||||
taskManager.registerTask("status_print", 5000,
|
||||
std::bind(&SensorService::printStatus, this));
|
||||
|
||||
Serial.println("[SensorService] Registered all sensor tasks");
|
||||
}
|
||||
|
||||
NodeContext& ctx;
|
||||
TaskManager& taskManager;
|
||||
int temperature = 25;
|
||||
int humidity = 60;
|
||||
int calibrationOffset = 0;
|
||||
};
|
||||
|
||||
// Example custom task functions (legacy style)
|
||||
// Example network service class
|
||||
class NetworkService {
|
||||
public:
|
||||
NetworkService(NodeContext& ctx, TaskManager& taskMgr) : ctx(ctx), taskManager(taskMgr) {
|
||||
registerTasks();
|
||||
}
|
||||
|
||||
void checkConnection() {
|
||||
Serial.println("[NetworkService] Checking WiFi connection");
|
||||
if (WiFi.status() == WL_CONNECTED) {
|
||||
Serial.printf("[NetworkService] Connected to %s\n", WiFi.SSID().c_str());
|
||||
} else {
|
||||
Serial.println("[NetworkService] WiFi disconnected");
|
||||
}
|
||||
}
|
||||
|
||||
void sendHeartbeat() {
|
||||
Serial.println("[NetworkService] Sending heartbeat");
|
||||
// Simulate heartbeat
|
||||
heartbeatCount++;
|
||||
Serial.printf("[NetworkService] Heartbeat #%d sent\n", heartbeatCount);
|
||||
}
|
||||
|
||||
private:
|
||||
void registerTasks() {
|
||||
taskManager.registerTask("connection_check", 5000,
|
||||
std::bind(&NetworkService::checkConnection, this));
|
||||
taskManager.registerTask("heartbeat", 8000,
|
||||
std::bind(&NetworkService::sendHeartbeat, this));
|
||||
|
||||
Serial.println("[NetworkService] Registered all network tasks");
|
||||
}
|
||||
|
||||
NodeContext& ctx;
|
||||
TaskManager& taskManager;
|
||||
int heartbeatCount = 0;
|
||||
};
|
||||
|
||||
// Example custom task functions (legacy style - still supported)
|
||||
void customTask1() {
|
||||
Serial.println("[CustomTask1] Executing custom task 1");
|
||||
}
|
||||
@@ -41,45 +119,26 @@ void periodicMaintenance() {
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
Serial.println("Task Management Example with std::bind");
|
||||
Serial.println("Task Management Example with Class-Based Registration");
|
||||
|
||||
// Create context and task manager
|
||||
NodeContext ctx;
|
||||
TaskManager taskManager(ctx);
|
||||
|
||||
// Create an instance of our example class
|
||||
ExampleClass example;
|
||||
// Create service instances - they will register their own tasks
|
||||
SensorService sensors(ctx, taskManager);
|
||||
NetworkService network(ctx, taskManager);
|
||||
|
||||
// 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);
|
||||
// Register additional custom tasks (legacy style)
|
||||
taskManager.registerTask("custom_task_1", 12000, customTask1);
|
||||
taskManager.registerTask("custom_task_2", 15000, customTask2);
|
||||
taskManager.registerTask("maintenance", 30000, periodicMaintenance);
|
||||
|
||||
// Method 4: Register a lambda function directly
|
||||
// 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();
|
||||
|
||||
@@ -87,12 +146,10 @@ void setup() {
|
||||
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("1. SensorService: Registers its own tasks in constructor");
|
||||
Serial.println("2. NetworkService: Registers its own tasks in constructor");
|
||||
Serial.println("3. Custom tasks: Legacy function registration");
|
||||
Serial.println("4. Lambda function: Direct lambda registration");
|
||||
Serial.println("=====================================\n");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user