FastArduino  v1.7
C++ library to build fast but small Arduino/AVR projects
events::Scheduler< CLOCK_, EVENT_ > Class Template Reference

Schedule jobs at predefined periods of time. More...

#include <fastarduino/scheduler.h>

Inheritance diagram for events::Scheduler< CLOCK_, EVENT_ >:
Collaboration diagram for events::Scheduler< CLOCK_, EVENT_ >:

Public Types

using CLOCK = CLOCK_
 The type of clock source used by this Scheduler.
 
using EVENT = EVENT_
 The events::Event<T> dispatched by the system and expected by this Scheduler.
 
- Public Types inherited from containers::LinkedList< Job >
using T = Job
 The type of items in this list.
 

Public Member Functions

 Scheduler (const Scheduler< CLOCK_, EVENT_ > &)=delete
 
Scheduler< CLOCK_, EVENT_ > & operator= (const Scheduler< CLOCK_, EVENT_ > &)=delete
 
 Scheduler (const CLOCK &clock, uint8_t type) INLINE
 Create a new Scheduler based on the given clock. More...
 
void schedule (Job &job) INLINE
 Add job to this scheduler. More...
 
void unschedule (Job &job) INLINE
 Remove job from this scheduler. More...
 
- Public Member Functions inherited from events::EventHandler< EVENT_ >
uint8_t type () const INLINE
 The type of event that this handler accepts and can act upon.
 
- Public Member Functions inherited from containers::LinkedList< Job >
 LinkedList (const LinkedList< Job > &)=default
 
LinkedList< Job > & operator= (const LinkedList< Job > &)=delete
 
void insert (T &item) INLINE
 Insert item at the beginning of this list. More...
 
bool remove (T &item) INLINE
 Remove item from this list. More...
 
void traverse (F func)
 Traverse all items of this list and execute f functor. More...
 

Additional Inherited Members

- Protected Member Functions inherited from events::EventHandler< EVENT_ >
 EventHandler (const EventHandler< EVENT_ > &)=default
 
 EventHandler (uint8_t type=Type::NO_EVENT) INLINE
 Create an Event Handler for given type of event. More...
 
EventHandler< EVENT_ > & operator= (const EventHandler< EVENT_ > &)=default
 
virtual void on_event (const EVENT_ &event)=0
 This pure virtual method is called by Dispatcher::dispatch() when event.type() matches the type supported by this EventHandler. More...
 

Detailed Description

template<typename CLOCK_, typename EVENT_>
class events::Scheduler< CLOCK_, EVENT_ >

Schedule jobs at predefined periods of time.

The timebase is provided by CLOCK clock instance. A scheduler is an EventHandler that must thus be attached to a Dispatcher as in this snippet:

using namespace events;
class MyJob: public Job
{
...
}
int main()
{
// Create event queue
const uint8_t EVENT_QUEUE_SIZE = 32;
EVENT buffer[EVENT_QUEUE_SIZE];
containers::Queue<EVENT> event_queue{buffer};
// Prepare event dispatcher, clock and scheduler
Dispatcher<EVENT> dispatcher;
dispatcher.insert(scheduler);
// Create and register a job
MyJob job;
scheduler.schedule(job);
// Start clock (watchdog)
watchdog.begin(watchdog::TimeOut::TO_64ms);
// Main event loop
while (true)
{
EVENT event = pull(event_queue);
dispatcher.dispatch(event);
}
}

In that snippet, we use Watchdog as the clock source, but other sources are available.

Template Parameters
CLOCK_the type of clock that will be used as time base
EVENT_the events::Event<T> dispatched by the system
See also
Job
Event
Dispatcher
watchdog::Watchdog
time::RTT

Definition at line 88 of file scheduler.h.

Constructor & Destructor Documentation

◆ Scheduler()

template<typename CLOCK_ , typename EVENT_ >
events::Scheduler< CLOCK_, EVENT_ >::Scheduler ( const CLOCK clock,
uint8_t  type 
)
inline

Create a new Scheduler based on the given clock.

Parameters
clockthe clock providing the timebase for this scheduler
typethe type of event generated by clock

Definition at line 104 of file scheduler.h.

Member Function Documentation

◆ schedule()

template<typename CLOCK_ , typename EVENT_ >
void events::Scheduler< CLOCK_, EVENT_ >::schedule ( Job job)
inline

Add job to this scheduler.

Parameters
jobthe job to be added to this scheduler
See also
Job

Definition at line 118 of file scheduler.h.

◆ unschedule()

template<typename CLOCK_ , typename EVENT_ >
void events::Scheduler< CLOCK_, EVENT_ >::unschedule ( Job job)
inline

Remove job from this scheduler.

Note that when a job is not periodic (i.e. it is a one-shot job) then it is automatically unscheduled after first execution.

Parameters
jobthe job to be removed from this scheduler; does nothing if job was not previously added to this scheduler.
See also
Job

Definition at line 131 of file scheduler.h.


The documentation for this class was generated from the following file:
watchdog
Defines the simple API for Watchdog timer management.
Definition: watchdog.h:101
containers::Queue< EVENT >
events::Dispatcher::dispatch
void dispatch(const EVENT &event)
Dispatch the given event to the right EventHandler, based on the event type.
Definition: events.h:256
events::Event
A standard Event as managed by FastArduino event API.
Definition: events.h:144
events::Scheduler
Schedule jobs at predefined periods of time.
Definition: scheduler.h:89
containers::LinkedList< EventHandler< EVENT > >::insert
void insert(T &item) INLINE
Insert item at the beginning of this list.
Definition: linked_list.h:106
events
Defines all API to handle events within FastArduino programs.
Definition: events.h:82
events::Scheduler::EVENT
EVENT_ EVENT
The events::Event<T> dispatched by the system and expected by this Scheduler.
Definition: scheduler.h:97
containers::pull
T pull(Queue< T, TREF > &queue)
Pull an item from the beginning of queue.
Definition: queue.h:556
watchdog::Watchdog
Simple API to use watchdog timer as a clock for events generation.
Definition: watchdog.h:305
events::Dispatcher
Utility to dispatch an event to a list of EventHandlers that are registered for its type.
Definition: events.h:240
events::Type::WDT_TIMER
const uint8_t WDT_TIMER
Type of events generated by watchdog::Watchdog for each watchdog timeout interrupt.
Definition: events.h:105
events::Job
Abstract class holding some action to be executed at given periods of time.
Definition: scheduler.h:161
REGISTER_WATCHDOG_CLOCK_ISR
#define REGISTER_WATCHDOG_CLOCK_ISR(EVENT)
Register the necessary ISR (Interrupt Service Routine) for a watchdog::Watchdog to work properly.
Definition: watchdog.h:36