FastArduino  v1.7
C++ library to build fast but small Arduino/AVR projects
scheduler.h
Go to the documentation of this file.
1 // Copyright 2016-2021 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 
27 namespace 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<CLOCK_, EVENT_>&) = delete;
92  Scheduler<CLOCK_, EVENT_>& operator=(const Scheduler<CLOCK_, EVENT_>&) = 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) override INLINE
108  {
109  traverse(JobCaller{clock_});
110  }
112 
118  void schedule(Job& job) INLINE
119  {
120  insert(job);
121  }
122 
131  void unschedule(Job& job) INLINE
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:
203  Job(const Job&) = default;
204  Job& operator=(const Job&) = default;
205 
213  virtual void on_schedule(uint32_t millis) = 0;
214 
222  Job(uint32_t next = 0, uint32_t period = 0) INLINE : next_time_{next}, period_{period} {}
223 
224  private:
225  uint32_t next_time_;
226  uint32_t period_;
227 
228  template<typename CLOCK, typename T> friend class Scheduler;
229  };
230 
232  template<typename CLOCK, typename T> bool Scheduler<CLOCK, T>::JobCaller::operator()(Job& job)
233  {
234  uint32_t now = clock_.millis();
235  if (job.next_time() <= now)
236  {
237  job.on_schedule(now);
238  if (!job.is_periodic()) return true;
239  job.reschedule(now + job.period());
240  }
241  return false;
242  }
244 }
245 
246 #endif /* SCHEDULER_HH */
247 
events::Scheduler::schedule
void schedule(Job &job) INLINE
Add job to this scheduler.
Definition: scheduler.h:118
events::Scheduler::Scheduler
Scheduler(const CLOCK &clock, uint8_t type) INLINE
Create a new Scheduler based on the given clock.
Definition: scheduler.h:104
events::EventHandler< EVENT_ >::on_event
virtual void on_event(const EVENT_ &event)=0
This pure virtual method is called by Dispatcher::dispatch() when event.type() matches the type suppo...
events::EventHandler< EVENT_ >::type
uint8_t type() const INLINE
The type of event that this handler accepts and can act upon.
Definition: events.h:293
events.h
Support for events management.
events::Scheduler
Schedule jobs at predefined periods of time.
Definition: scheduler.h:89
events::Job::period
uint32_t period() const INLINE
Return the period of this job, or 0 if this is a one-shot job.
Definition: scheduler.h:183
containers::LinkedList< Job >::insert
void insert(T &item) INLINE
Insert item at the beginning of this list.
Definition: linked_list.h:106
time::millis
MILLIS_PTR millis
Count number of milliseconds elapsed since some time base reference (generally since MCU startup).
Definition: time.cpp:20
events::EventHandler
Abstract event handler, used by Dispatcher to get called back when an event of the expected type is d...
Definition: events.h:286
events
Defines all API to handle events within FastArduino programs.
Definition: events.h:82
events::Scheduler::CLOCK
CLOCK_ CLOCK
The type of clock source used by this Scheduler.
Definition: scheduler.h:95
events::Job::next_time
uint32_t next_time() const INLINE
Tell next time (in ms) when this job shall be executed.
Definition: scheduler.h:175
UNUSED
#define UNUSED
Specific GCC attribute to declare an argument or variable unused, so that the compiler does not emit ...
Definition: defines.h:45
events::Job::reschedule
void reschedule(uint32_t when) INLINE
Reschedule this job for a later time (in ms).
Definition: scheduler.h:197
events::Scheduler::EVENT
EVENT_ EVENT
The events::Event<T> dispatched by the system and expected by this Scheduler.
Definition: scheduler.h:97
containers::LinkedList< Job >::remove
bool remove(T &item) INLINE
Remove item from this list.
Definition: linked_list.h:121
events::Job::Job
Job(uint32_t next=0, uint32_t period=0) INLINE
Construct a new Job.
Definition: scheduler.h:222
events::Job::is_periodic
bool is_periodic() const INLINE
Tell if this job is periodic or not.
Definition: scheduler.h:166
events::Job::on_schedule
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().
linked_list.h
Utility API to handle linked list containers.
containers::LinkedList< Job >::traverse
void traverse(F func)
Traverse all items of this list and execute f functor.
Definition: linked_list.h:135
containers::LinkedList
Linked list of type T_ items.
Definition: linked_list.h:81
events::Job
Abstract class holding some action to be executed at given periods of time.
Definition: scheduler.h:161
events::Scheduler::unschedule
void unschedule(Job &job) INLINE
Remove job from this scheduler.
Definition: scheduler.h:131
INLINE
#define INLINE
Specific GCC attribute to force the compiler to always inline code of a given function.
Definition: defines.h:57