feat: create examples and corresponding build config
This commit is contained in:
160
examples/tasks/task_management_example.cpp
Normal file
160
examples/tasks/task_management_example.cpp
Normal file
@@ -0,0 +1,160 @@
|
||||
/*
|
||||
* Task Management Example with std::bind and Class-Based Registration
|
||||
*
|
||||
* This example demonstrates how to use the TaskManager with std::bind
|
||||
* and how classes can register their own tasks.
|
||||
*/
|
||||
|
||||
#include <Arduino.h>
|
||||
#include <functional>
|
||||
#include "TaskManager.h"
|
||||
#include "NodeContext.h"
|
||||
|
||||
// Example service class that registers its own tasks
|
||||
class SensorService {
|
||||
public:
|
||||
SensorService(NodeContext& ctx, TaskManager& taskMgr) : ctx(ctx), taskManager(taskMgr) {
|
||||
// Register all sensor-related tasks in constructor
|
||||
registerTasks();
|
||||
}
|
||||
|
||||
void readTemperature() {
|
||||
Serial.println("[SensorService] Reading temperature");
|
||||
// Simulate temperature reading
|
||||
temperature = random(20, 30);
|
||||
Serial.printf("[SensorService] Temperature: %d°C\n", temperature);
|
||||
}
|
||||
|
||||
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 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");
|
||||
}
|
||||
|
||||
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 Class-Based Registration");
|
||||
|
||||
// Create context and task manager
|
||||
NodeContext ctx;
|
||||
TaskManager taskManager(ctx);
|
||||
|
||||
// Create service instances - they will register their own tasks
|
||||
SensorService sensors(ctx, taskManager);
|
||||
NetworkService network(ctx, taskManager);
|
||||
|
||||
// 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);
|
||||
|
||||
// Register a lambda function directly
|
||||
taskManager.registerTask("lambda_function", 6000, []() {
|
||||
Serial.println("[Lambda] Lambda function called");
|
||||
});
|
||||
|
||||
// Initialize and start all tasks
|
||||
taskManager.initialize();
|
||||
|
||||
// Print initial task status
|
||||
taskManager.printTaskStatus();
|
||||
|
||||
Serial.println("\n=== Task Registration Examples ===");
|
||||
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");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// The TaskManager handles all task execution
|
||||
// No need to call individual task functions
|
||||
yield();
|
||||
}
|
||||
Reference in New Issue
Block a user