FastArduino v1.10
C++ library to build fast but small Arduino/AVR projects
Loading...
Searching...
No Matches
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. More...
 
using EVENT = EVENT_
 The events::Event<T> dispatched by the system and expected by this Scheduler. More...
 
- Public Types inherited from containers::LinkedList< Job >
using T = Job
 The type of items in this list. More...
 

Public Member Functions

 Scheduler (const Scheduler &)=delete
 
Scheduleroperator= (const Scheduler &)=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. More...
 
- Public Member Functions inherited from containers::LinkedList< Job >
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_ >
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...
 
 EventHandler (uint8_t type=Type::NO_EVENT) INLINE
 Create an Event Handler for given type of event. 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)
// Main event loop
while (true)
{
EVENT event = pull(event_queue);
dispatcher.dispatch(event);
}
}
void insert(T &item) INLINE
Insert item at the beginning of this list.
Definition: linked_list.h:103
Queue of type T_ items.
Definition: queue.h:59
Utility to dispatch an event to a list of EventHandlers that are registered for its type.
Definition: events.h:242
void dispatch(const EVENT &event)
Dispatch the given event to the right EventHandler, based on the event type.
Definition: events.h:258
A standard Event as managed by FastArduino event API.
Definition: events.h:144
Abstract class holding some action to be executed at given periods of time.
Definition: scheduler.h:161
Schedule jobs at predefined periods of time.
Definition: scheduler.h:89
EVENT_ EVENT
The events::Event<T> dispatched by the system and expected by this Scheduler.
Definition: scheduler.h:97
Simple API to use watchdog timer as a clock for events generation.
Definition: watchdog.h:302
T pull(Queue< T, TREF > &queue)
Pull an item from the beginning of queue.
Definition: queue.h:556
const uint8_t WDT_TIMER
Type of events generated by watchdog::Watchdog for each watchdog timeout interrupt.
Definition: events.h:105
Defines all API to handle events within FastArduino programs.
Definition: events.h:82
Defines the simple API for Watchdog timer management.
Definition: watchdog.h:101
@ TO_64ms
Watchdog timeout 64 ms.
#define REGISTER_WATCHDOG_CLOCK_ISR(EVENT)
Register the necessary ISR (Interrupt Service Routine) for a watchdog::Watchdog to work properly.
Definition: watchdog.h:36

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.

Member Typedef Documentation

◆ CLOCK

template<typename CLOCK_ , typename EVENT_ >
using events::Scheduler< CLOCK_, EVENT_ >::CLOCK = CLOCK_

The type of clock source used by this Scheduler.

Definition at line 95 of file scheduler.h.

◆ EVENT

template<typename CLOCK_ , typename EVENT_ >
using events::Scheduler< CLOCK_, EVENT_ >::EVENT = EVENT_

The events::Event<T> dispatched by the system and expected by this Scheduler.

Definition at line 97 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: