docs: update task manager
This commit is contained in:
110
README.md
110
README.md
@@ -256,6 +256,73 @@ taskManager.registerTask("maintenance", 30000, maintenanceFunction);
|
||||
taskManager.initialize();
|
||||
```
|
||||
|
||||
#### Using std::bind with Member Functions
|
||||
|
||||
```cpp
|
||||
#include <functional>
|
||||
#include "TaskManager.h"
|
||||
|
||||
class MyService {
|
||||
public:
|
||||
void sendHeartbeat() {
|
||||
Serial.println("Service heartbeat");
|
||||
}
|
||||
|
||||
void performMaintenance() {
|
||||
Serial.println("Running maintenance");
|
||||
}
|
||||
};
|
||||
|
||||
MyService service;
|
||||
TaskManager taskManager(ctx);
|
||||
|
||||
// Register member functions using std::bind
|
||||
taskManager.registerTask("heartbeat", 2000,
|
||||
std::bind(&MyService::sendHeartbeat, &service));
|
||||
taskManager.registerTask("maintenance", 30000,
|
||||
std::bind(&MyService::performMaintenance, &service));
|
||||
|
||||
// Initialize and start all tasks
|
||||
taskManager.initialize();
|
||||
```
|
||||
|
||||
#### Using Lambda Functions
|
||||
|
||||
```cpp
|
||||
// Register lambda functions directly
|
||||
taskManager.registerTask("counter", 1000, []() {
|
||||
static int count = 0;
|
||||
Serial.printf("Count: %d\n", ++count);
|
||||
});
|
||||
|
||||
// Lambda with capture
|
||||
int threshold = 100;
|
||||
taskManager.registerTask("monitor", 5000, [&threshold]() {
|
||||
if (ESP.getFreeHeap() < threshold) {
|
||||
Serial.println("Low memory warning!");
|
||||
}
|
||||
});
|
||||
```
|
||||
|
||||
#### Complex Task Registration
|
||||
|
||||
```cpp
|
||||
class NetworkManager {
|
||||
public:
|
||||
void checkConnection() { /* ... */ }
|
||||
void sendData(String data) { /* ... */ }
|
||||
};
|
||||
|
||||
NetworkManager network;
|
||||
|
||||
// Multiple operations in one task
|
||||
taskManager.registerTask("network_ops", 3000,
|
||||
std::bind([](NetworkManager* net) {
|
||||
net->checkConnection();
|
||||
net->sendData("status_update");
|
||||
}, &network));
|
||||
```
|
||||
|
||||
### Task Control API
|
||||
|
||||
```cpp
|
||||
@@ -291,7 +358,35 @@ curl -X POST http://192.168.1.100/api/tasks/control \
|
||||
|
||||
### Adding Custom Tasks
|
||||
|
||||
To add custom tasks to your SPORE system:
|
||||
#### Method 1: Using std::bind (Recommended)
|
||||
|
||||
1. **Create your service class**:
|
||||
```cpp
|
||||
class SensorService {
|
||||
public:
|
||||
void readTemperature() {
|
||||
// Read sensor logic
|
||||
Serial.println("Reading temperature");
|
||||
}
|
||||
|
||||
void calibrateSensors() {
|
||||
// Calibration logic
|
||||
Serial.println("Calibrating sensors");
|
||||
}
|
||||
};
|
||||
```
|
||||
|
||||
2. **Register with TaskManager**:
|
||||
```cpp
|
||||
SensorService sensors;
|
||||
|
||||
taskManager.registerTask("temp_read", 1000,
|
||||
std::bind(&SensorService::readTemperature, &sensors));
|
||||
taskManager.registerTask("calibrate", 60000,
|
||||
std::bind(&SensorService::calibrateSensors, &sensors));
|
||||
```
|
||||
|
||||
#### Method 2: Traditional Functions
|
||||
|
||||
1. **Define your task function**:
|
||||
```cpp
|
||||
@@ -306,20 +401,27 @@ To add custom tasks to your SPORE system:
|
||||
taskManager.registerTask("my_task", 10000, myCustomTask);
|
||||
```
|
||||
|
||||
3. **The TaskManager handles the rest** - automatic execution, lifecycle management, and monitoring.
|
||||
|
||||
### Task Configuration Options
|
||||
|
||||
When registering tasks, you can specify:
|
||||
|
||||
- **Name**: Unique identifier for the task
|
||||
- **Interval**: Execution frequency in milliseconds
|
||||
- **Callback**: Function, bound method, or lambda to execute
|
||||
- **Enabled**: Whether the task starts enabled (default: true)
|
||||
- **AutoStart**: Whether to start automatically (default: true)
|
||||
|
||||
```cpp
|
||||
// Traditional function
|
||||
taskManager.registerTask("delayed_task", 5000, taskFunction, true, false);
|
||||
// Creates a task that's enabled but won't auto-start
|
||||
|
||||
// Member function with std::bind
|
||||
taskManager.registerTask("service_task", 3000,
|
||||
std::bind(&Service::method, &instance), true, false);
|
||||
|
||||
// Lambda function
|
||||
taskManager.registerTask("lambda_task", 2000,
|
||||
[]() { Serial.println("Lambda!"); }, true, false);
|
||||
```
|
||||
|
||||
## Current Limitations
|
||||
|
||||
Reference in New Issue
Block a user