96 :
Task(), mTotalElapsedMs(0), mRepeatTimes(0), mCanDoHardWork(false) {
97 mspCallback = std::make_shared<SchedulerTaskCallback>(callback);
106 void attach(uint32_t ms,
short times = 0) {
107 mRepeatTimes = times > 0 ? times : -1;
136 mCanDoHardWork =
true;
146 auto startMs = millis();
147 if (mCanDoHardWork) {
148 mCanDoHardWork =
false;
150 if (mRepeatTimes > 0 && !--mRepeatTimes) {
153 (*mspCallback)(*
this, mRepeatTimes);
155 mTotalElapsedMs += millis() - startMs;
164 return mTotalElapsedMs;
168 uint64_t mTotalElapsedMs;
170 volatile bool mCanDoHardWork;
213 return std::make_shared<SchedulerTask>(callback);
223 return std::make_shared<SchedulerTask>(executor);
255 auto startMs = millis();
257 while (!mTasks.isEnd()) {
258 const auto &task = mTasks.current();
262 if (!task.first && !task.second->isAttached()) {
264 mTasks.deleteCurrent();
273 mTotalElapsedMs += millis() - startMs;
286 uint64_t totalAnonymousElapsedMs = 0;
287 uint8_t anonymousTaskCount = 0;
291 callback(task.first, task.second->isAttached(), task.second->getTotalElapsedMs());
293 totalAnonymousElapsedMs += task.second->getTotalElapsedMs();
294 anonymousTaskCount++;
298 if (anonymousTaskCount > 0) {
299 constexpr size_t BUFFER_SIZE = 20;
300 static char anonymousTaskName[BUFFER_SIZE];
301 snprintf(anonymousTaskName, BUFFER_SIZE,
"__anonymous[%d]", anonymousTaskCount);
302 callback(anonymousTaskName,
true, totalAnonymousElapsedMs);
312 return mTotalElapsedMs;
316 uint64_t mTotalElapsedMs;
ESP32-specific task implementation for the Uniot Core.
Definition IterableQueue.h:36
Definition ESP32Task.h:41
Definition ESP8266Task.h:48
void attach(uint32_t ms, bool repeat, TaskCallback callback)
Attach a simple callback to run periodically.
Definition ESP8266Task.h:86
void detach()
Stop and detach the timer.
Definition ESP8266Task.h:107
Interface for executing tasks in the scheduler system.
Definition IExecutor.h:31
virtual void execute(short times)=0
Execute the implementation's functionality.
Interface for connecting components to the TaskScheduler.
Definition ISchedulerConnectionKit.h:35
virtual void pushTo(TaskScheduler &scheduler)=0
Register this component with the given scheduler.
uint64_t getTotalElapsedMs() const
Get the total execution time of this task in milliseconds.
Definition TaskScheduler.h:163
SchedulerTask(SchedulerTaskCallback callback)
Constructor with custom callback function.
Definition TaskScheduler.h:95
SchedulerTask(IExecutor &executor)
Constructor that wraps an IExecutor implementation.
Definition TaskScheduler.h:87
SharedPointer< SchedulerTaskCallback > spSchedulerTaskCallback
Shared pointer type for task callbacks.
Definition TaskScheduler.h:80
void loop()
Main execution loop for the task.
Definition TaskScheduler.h:145
std::function< void(SchedulerTask &, short)> SchedulerTaskCallback
Callback function signature for scheduled tasks.
Definition TaskScheduler.h:75
void once(uint32_t ms)
Schedule the task to run once after the specified delay.
Definition TaskScheduler.h:116
void attach(uint32_t ms, short times=0)
Attach the task to run on a specified interval.
Definition TaskScheduler.h:106
void trigger()
Run the task once on the scheduler's next pass.
Definition TaskScheduler.h:135
auto MakePair(Args &&...args) -> decltype(std::make_pair(std::forward< Args >(args)...))
Creates a pair instance, alias for std::make_pair.
Definition Common.h:337
std::shared_ptr< T > SharedPointer
Type alias for std::shared_ptr with cleaner syntax.
Definition Common.h:313
std::pair< T_First, T_Second > Pair
Type alias for std::pair with cleaner syntax.
Definition Common.h:322
SharedPointer< SchedulerTask > TaskPtr
Shared pointer type for scheduler tasks.
Definition TaskScheduler.h:190
std::function< void(const char *, bool, uint64_t)> TaskInfoCallback
Callback signature for task status reporting.
Definition TaskScheduler.h:199
void exportTasksInfo(TaskInfoCallback callback) const
Report information about all registered tasks.
Definition TaskScheduler.h:281
TaskScheduler()
Constructor.
Definition TaskScheduler.h:204
static TaskPtr make(SchedulerTask::SchedulerTaskCallback callback)
Static factory method to create a task with a callback.
Definition TaskScheduler.h:212
static TaskPtr make(IExecutor &executor)
Static factory method to create a task from an IExecutor.
Definition TaskScheduler.h:222
TaskScheduler & push(const char *name, TaskPtr task)
Add a named task to the scheduler.
Definition TaskScheduler.h:233
uint64_t getTotalElapsedMs() const
Get the total execution time of the scheduler in milliseconds.
Definition TaskScheduler.h:311
TaskScheduler & push(ISchedulerConnectionKit &connection)
Add connection kit components to the scheduler.
Definition TaskScheduler.h:244
void loop()
Main execution loop for the scheduler.
Definition TaskScheduler.h:254
Contains all classes and functions related to the Uniot Core.
ESP8266Task Task
Definition TaskScheduler.h:52