Skip to content

Custom Processes

Aaron Wisner edited this page Jul 13, 2016 · 4 revisions

The Abstract Process Base Class

A Process can be thought of as job or a task that needs to be serviced at certain times. Each Process had be scheduled to run Periodically, Constantly, or Periodically/Constantly with a set number of run iterations.

Each project will have multiple Processes, here are some examples of Processes:

  • A Process to handle user input
  • A Process to run a basic Webserver
  • A Process to update a display

Process is an abstract base class that should be extended to handle your particular Process (such as the examples mentioned above). Take a look at the constructor:

    /*
    * @param manager: The scheduler overseeing this Process
    * @param priority: The prority of this Process defined in config.h
    * @param period: The period this process should be serviced at (SERVICE_CONSTANTLY = As often as possible)
    * @param iterations: Number of iterations this process should be serviced before being disabled (RUNTIME_FOREVER = infinite)
    * @param overSchedThresh: The periods behind this process can get, before a WARNING_PROC_OVERSCHEDULED is triggered
    * (OVERSCHEDULED_NO_WARNING = a warning will never be triggered)
    */
    Process(Scheduler &manager, ProcPriority priority, uint32_t period,
            int iterations=RUNTIME_FOREVER,
            uint16_t overSchedThresh = OVERSCHEDULED_NO_WARNING);

The scheduler's will call the following virtual function as an entry point when it is time for you service to run:

    virtual void service();

Your First Process

It is your job to subclass the Process class to create your custom Process. You are required to implement the virtual void service() method. Let's continue with the example:

#include <ProcessScheduler.h>

// Create my custom process SayHelloProcess
class SayHelloProcess : public Process
{
public:
    // Call the Process constructor
    SayHelloProcess(Scheduler &manager, ProcPriority pr, unsigned int period, int iterations)
        :  Process(manager, pr, period, iterations) {}

protected:
    // Create our service routine
    virtual void service()
    {
        Serial.print("Hello from Process: ");
        Serial.println(getID());
    }
};

Scheduler sched; // Create a global Scheduler object

// Create our high priority process that will get serviced as often as possible and run forever
SayHelloProcess myProc(sched, HIGH_PRIORITY, SERVICE_CONSTANTLY, RUNTIME_FOREVER);

void setup() 
{
    Serial.begin(9600);
    // Add our process to the scheduler
    myProc.add();
    //enable it
    myProc.enable();
}

void loop() 
{
    sched.run();
}

That's it, you have created your first process! I recommend moving your custom Process class to a separate file as it gets bigger.

Multiple Processes

In the previous example there was only one process, not a very exciting demo... Thanks to the magic of OOP and polymorphism, I can create different custom Process subclasses (maybe a SayGoodByeProcess), or even add multiple instances of the same Process to the scheduler. Consider this:

SayHelloProcess myProc1(sched, HIGH_PRIORITY, 1000, RUNTIME_FOREVER);
SayHelloProcess myProc2(sched, HIGH_PRIORITY, 2000, RUNTIME_FOREVER);

After adding these processes to the scheduler, myProc1 will say hello every 1000ms, myProc2 will say hello every 2000ms. Take a look at the Serial Monitor output:

Hello from Process: 1
Hello from Process: 2
Hello from Process: 1
Hello from Process: 1
Hello from Process: 2
Hello from Process: 1
Hello from Process: 1
Hello from Process: 2
Hello from Process: 1
Hello from Process: 1
Hello from Process: 2
Clone this wiki locally