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;
127 auto startMs = millis();
128 if (mCanDoHardWork) {
129 mCanDoHardWork =
false;
131 if (mRepeatTimes > 0 && !--mRepeatTimes) {
134 (*mspCallback)(*
this, mRepeatTimes);
136 mTotalElapsedMs += millis() - startMs;
145 return mTotalElapsedMs;
149 uint64_t mTotalElapsedMs;
151 volatile bool mCanDoHardWork;
194 return std::make_shared<SchedulerTask>(callback);
204 return std::make_shared<SchedulerTask>(executor);
236 auto startMs = millis();
238 while (!mTasks.isEnd()) {
239 const auto &task = mTasks.current();
243 if (!task.first && !task.second->isAttached()) {
245 mTasks.deleteCurrent();
254 mTotalElapsedMs += millis() - startMs;
267 uint64_t totalAnonymousElapsedMs = 0;
268 uint8_t anonymousTaskCount = 0;
272 callback(task.first, task.second->isAttached(), task.second->getTotalElapsedMs());
274 totalAnonymousElapsedMs += task.second->getTotalElapsedMs();
275 anonymousTaskCount++;
279 if (anonymousTaskCount > 0) {
280 constexpr size_t BUFFER_SIZE = 20;
281 static char anonymousTaskName[BUFFER_SIZE];
282 snprintf(anonymousTaskName, BUFFER_SIZE,
"__anonymous[%d]", anonymousTaskCount);
283 callback(anonymousTaskName,
true, totalAnonymousElapsedMs);
293 return mTotalElapsedMs;
297 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:144
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:126
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
auto MakePair(Args &&...args) -> decltype(std::make_pair(std::forward< Args >(args)...))
Creates a pair instance, alias for std::make_pair.
Definition Common.h:184
std::shared_ptr< T > SharedPointer
Type alias for std::shared_ptr with cleaner syntax.
Definition Common.h:160
std::pair< T_First, T_Second > Pair
Type alias for std::pair with cleaner syntax.
Definition Common.h:169
SharedPointer< SchedulerTask > TaskPtr
Shared pointer type for scheduler tasks.
Definition TaskScheduler.h:171
std::function< void(const char *, bool, uint64_t)> TaskInfoCallback
Callback signature for task status reporting.
Definition TaskScheduler.h:180
void exportTasksInfo(TaskInfoCallback callback) const
Report information about all registered tasks.
Definition TaskScheduler.h:262
TaskScheduler()
Constructor.
Definition TaskScheduler.h:185
static TaskPtr make(SchedulerTask::SchedulerTaskCallback callback)
Static factory method to create a task with a callback.
Definition TaskScheduler.h:193
static TaskPtr make(IExecutor &executor)
Static factory method to create a task from an IExecutor.
Definition TaskScheduler.h:203
TaskScheduler & push(const char *name, TaskPtr task)
Add a named task to the scheduler.
Definition TaskScheduler.h:214
uint64_t getTotalElapsedMs() const
Get the total execution time of the scheduler in milliseconds.
Definition TaskScheduler.h:292
TaskScheduler & push(ISchedulerConnectionKit &connection)
Add connection kit components to the scheduler.
Definition TaskScheduler.h:225
void loop()
Main execution loop for the scheduler.
Definition TaskScheduler.h:235
Contains all classes and functions related to the Uniot Core.
ESP8266Task Task
Definition TaskScheduler.h:52