FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
scheduler.h
Go to the documentation of this file.
1// Copyright 2016-2023 Jean-Francois Poilpret
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
16
21#ifndef SCHEDULER_HH
22#define SCHEDULER_HH
23
24#include "events.h"
25#include "linked_list.h"
26
27namespace events
28{
29 class Job;
30
87 template<typename CLOCK_, typename EVENT_>
88 class Scheduler : public EventHandler<EVENT_>, public containers::LinkedList<Job>
89 {
90 public:
91 Scheduler(const Scheduler&) = delete;
92 Scheduler& operator=(const Scheduler&) = delete;
93
95 using CLOCK = CLOCK_;
97 using EVENT = EVENT_;
98
104 Scheduler(const CLOCK& clock, uint8_t type) INLINE : EventHandler<EVENT>{type}, clock_{clock} {}
105
107 void on_event(UNUSED const EVENT& event) final INLINE
108 {
109 traverse(JobCaller{clock_});
110 }
112
118 void schedule(Job& job) INLINE
119 {
120 insert(job);
121 }
122
132 {
133 remove(job);
134 }
135
136 private:
137 class JobCaller
138 {
139 public:
140 explicit JobCaller(const CLOCK& clock) : clock_{clock} {}
141 bool operator()(Job& job);
142
143 private:
144 const CLOCK& clock_;
145 };
146
147 const CLOCK& clock_;
148 };
149
160 class Job : public containers::Link<Job>
161 {
162 public:
166 bool is_periodic() const INLINE
167 {
168 return period_ != 0;
169 }
170
175 uint32_t next_time() const INLINE
176 {
177 return next_time_;
178 }
179
183 uint32_t period() const INLINE
184 {
185 return period_;
186 }
187
197 void reschedule(uint32_t when) INLINE
198 {
199 next_time_ = when;
200 }
201
202 protected:
204 Job(const Job&) = default;
205 Job& operator=(const Job&) = default;
207
215 virtual void on_schedule(uint32_t millis) = 0;
216
224 Job(uint32_t next = 0, uint32_t period = 0) INLINE : next_time_{next}, period_{period} {}
225
226 private:
227 uint32_t next_time_;
228 uint32_t period_;
229
230 template<typename CLOCK, typename T> friend class Scheduler;
231 };
232
234 template<typename CLOCK, typename T> bool Scheduler<CLOCK, T>::JobCaller::operator()(Job& job)
235 {
236 uint32_t now = clock_.millis();
237 if (job.next_time() <= now)
238 {
239 job.on_schedule(now);
240 if (!job.is_periodic()) return true;
241 job.reschedule(now + job.period());
242 }
243 return false;
244 }
246}
247
248#endif /* SCHEDULER_HH */
Linked list of type T_ items.
Definition: linked_list.h:81
void traverse(F func)
Traverse all items of this list and execute f functor.
Definition: linked_list.h:132
void insert(T &item) INLINE
Insert item at the beginning of this list.
Definition: linked_list.h:103
bool remove(T &item) INLINE
Remove item from this list.
Definition: linked_list.h:118
Abstract event handler, used by Dispatcher to get called back when an event of the expected type is d...
Definition: events.h:288
uint8_t type() const INLINE
The type of event that this handler accepts and can act upon.
Definition: events.h:295
virtual void on_event(const EVENT_ &event)=0
This pure virtual method is called by Dispatcher::dispatch() when event.type() matches the type suppo...
Abstract class holding some action to be executed at given periods of time.
Definition: scheduler.h:161
virtual void on_schedule(uint32_t millis)=0
This method is called by Scheduler whenever current clock time is greater or equal to next_time().
Job(uint32_t next=0, uint32_t period=0) INLINE
Construct a new Job.
Definition: scheduler.h:224
uint32_t period() const INLINE
Return the period of this job, or 0 if this is a one-shot job.
Definition: scheduler.h:183
bool is_periodic() const INLINE
Tell if this job is periodic or not.
Definition: scheduler.h:166
void reschedule(uint32_t when) INLINE
Reschedule this job for a later time (in ms).
Definition: scheduler.h:197
uint32_t next_time() const INLINE
Tell next time (in ms) when this job shall be executed.
Definition: scheduler.h:175
Schedule jobs at predefined periods of time.
Definition: scheduler.h:89
Scheduler(const CLOCK &clock, uint8_t type) INLINE
Create a new Scheduler based on the given clock.
Definition: scheduler.h:104
void unschedule(Job &job) INLINE
Remove job from this scheduler.
Definition: scheduler.h:131
void schedule(Job &job) INLINE
Add job to this scheduler.
Definition: scheduler.h:118
EVENT_ EVENT
The events::Event<T> dispatched by the system and expected by this Scheduler.
Definition: scheduler.h:97
CLOCK_ CLOCK
The type of clock source used by this Scheduler.
Definition: scheduler.h:95
#define INLINE
Specific GCC attribute to force the compiler to always inline code of a given function.
Definition: defines.h:57
#define UNUSED
Specific GCC attribute to declare an argument or variable unused, so that the compiler does not emit ...
Definition: defines.h:45
Support for events management.
Utility API to handle linked list containers.
Defines all API to handle events within FastArduino programs.
Definition: events.h:82