PQRST  1.1
Priority Queue for Running Simple Tasks
Public Member Functions | List of all members
FunctionTask Class Reference

A task which runs a supplied function at a given time. More...

#include <pqrst.h>

Inheritance diagram for FunctionTask:
Inheritance graph
[legend]
Collaboration diagram for FunctionTask:
Collaboration graph
[legend]

Public Member Functions

 FunctionTask (run_task_t, ms_t cadence=0, int repeats=-1)
 Construct a task which will run the supplied function at a later time. More...
 
void run (ms_t)
 Run a single iteration of the loop. More...
 
- Public Member Functions inherited from LoopTask
 LoopTask (ms_t cadence, int repeats=-1)
 Construct a looping task. More...
 
ms_t set_cadence (ms_t)
 Set the scheduling cadence. More...
 
int set_repeats (int n)
 Set the repeat-count of the loop. More...
 
ms_t get_cadence (void) const
 Return the interval between loop runs. More...
 
int get_repeats (void) const
 Returns the number of repeats still to do. More...
 
- Public Member Functions inherited from Task
 Task ()
 Construct a task. More...
 
virtual ~Task ()
 
virtual void start (ms_t delay)
 Start the task. More...
 
virtual void start (void)
 Start the task immediately. More...
 
ms_t cancel (void)
 Cancel this task. More...
 
ms_t uncancel (void)
 Uncancel a task. More...
 
void run_at (ms_t t, Task *T=nullptr)
 Push a new task onto the queue, due at a given time. More...
 
void run_after (ms_t t, Task *T=nullptr)
 Push a new Task onto the queue, due some (non-negative) interval after the queue's current reference time. More...
 
void run_immediately (Task *T=nullptr)
 Push a task onto the queue, due immediately. More...
 
bool before (const Task *) const
 Impose an ordering on tasks. More...
 
bool set_task_slippy (bool)
 Set a task to be ‘slippy’. More...
 
bool get_task_slippy (void) const
 Determine whether a task is ‘slippy’. More...
 
bool is_enqueued_p (void) const
 Return true if the task is currently scheduled on a queue. More...
 
ms_t ready_after (void) const
 Return the interval until the Task is next ready, relative to the queue reference time. More...
 
void set_priority (unsigned char)
 Set the priority of the task. More...
 
unsigned char get_priority (void) const
 Return the priority of the task. More...
 
void set_duration (ms_t)
 Set the duration of the task. More...
 
ms_t get_duration (void) const
 Return the task's expected duration. More...
 
virtual bool user_task_p (void) const
 True if this is a ‘user task’, as opposed to a maintenance task. More...
 
void set_task_ident (const char *ident_string)
 Set the identification string for a task. More...
 
const char * get_task_ident (void) const
 Retrieve the identification string. More...
 

Additional Inherited Members

- Static Public Attributes inherited from Task
static const PROGMEM char *const pqrst_version
 Reports the PQRST version and compilation options. More...
 
- Protected Member Functions inherited from LoopTask
void run_task (ms_t)
 Run the task, additionally rescheduling it. More...
 
- Protected Member Functions inherited from Task
virtual void signal (int signal, ms_t t)
 Receive a signal. More...
 

Detailed Description

A task which runs a supplied function at a given time.

The supplied function must match the type run_task_t, and can be a predefined function or a lambda. Note, however, that in this version of the code the lambda cannot declare any objects in its closure.

Constructor & Destructor Documentation

◆ FunctionTask()

FunctionTask::FunctionTask ( run_task_t  f,
ms_t  cadence = 0,
int  repeats = -1 
)

Construct a task which will run the supplied function at a later time.

The function in question has a rather restricted form. It must match the type typedef void(*run_task_t)(ms_t t), which means that it cannot be an instance method; and it cannot refer to any local automatic variables. It can, however, be a simple function or a static method, and it can refer freely to global variables.

Note that the function can also usefully be a C++ lambda, as in:

FunctionTask([](ms_t) { ...; }).run_after(10);

This will run the contents of the function body 10ms after the current time.

This class extends the LoopTask, but unlike the constructor for that task, the cadence is optional and defaults to zero, meaning that by default the task will run only once.

If PQRST_SELF_QUEUE is non-zero (the default is zero), this constructor includes an initial TaskQueue* argument (see Task).

Parameters
fthe function to be run
cadencethe interval between runs, or zero to suppress any repetition (default: 0)
repeatsthe number of repeats before being cancelled (default: -1)

Member Function Documentation

◆ run()

void FunctionTask::run ( ms_t  )
virtual

Run a single iteration of the loop.

The run may schedule other tasks, but will typically not reschedule itself, since this is handled automatically. If, however, the task has a cadence or repeats of zero, so that it is not automatically rescheduled, then this method may need or want to do so as appropriate.

Implements LoopTask.


The documentation for this class was generated from the following files: