MistyGro API
C++ API for MistyGro's ESP32 controller based on the Arduino framework
scheduler.cpp
Go to the documentation of this file.
1 // Copyright 2023 Myron Rodrigues
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include "scheduler.h"
16 
17 #include <Arduino.h>
18 
19 Scheduler::Scheduler() : num_tasks_(0) {}
20 
21 void Scheduler::create_task(void (*task_func)(), unsigned long interval_sec)
22 {
23  if (num_tasks_ < MAX_TASKS) {
24  tasks_[num_tasks_].task_func = task_func;
25  tasks_[num_tasks_].interval = interval_sec;
26  tasks_[num_tasks_].last_execution_time = 0;
27  num_tasks_++;
28  }
29 }
30 
32 {
33  for (;;) {
34  time_t current_time = timer_.get_epoch_time();
35  for (int i = 0; i < num_tasks_; i++) {
36  if (current_time - tasks_[i].last_execution_time >= tasks_[i].interval) {
37  tasks_[i].last_execution_time = current_time;
38  tasks_[i].task_func();
39  }
40  }
41  // Additional code to be executed in the main loop can go here
42  }
43 }
44 
45 void Scheduler::begin(long int timer_sync_interval_ms)
46 {
47  timer_.begin(timer_sync_interval_ms);
48 }
void begin(long int timer_sync_interval_ms=600000)
initialise timer
Definition: scheduler.cpp:45
void run()
This needs to be run in the loop function continuously. Best practice is to have a loop function that...
Definition: scheduler.cpp:31
void create_task(void(*task_func)(), unsigned long interval_sec)
Create a task.
Definition: scheduler.cpp:21
void begin(long int sync_interval_ms=600000)
initialise the timer, set a sync interval to sync with NTP server
Definition: timer.cpp:52
time_t get_epoch_time()
Get the seconds since epoch now.
Definition: timer.cpp:38