Skip to content

Process Callback Methods

Aaron Wisner edited this page Jul 8, 2016 · 3 revisions

First you should understand the 3 possible states a Process can be in and how it transitions between them. The state diagram would look something like this:


Destroyed <=======> Disabled <=========> Enabled

Destroyed
A process not part of the scheduler chain.
Disabled
A process part of the scheduler chain, but not being serviced.
Enabled
A process part of the scheduler chain, and being serviced.

For example, a Disabled process can become either Destroyed or Enabled in one state transition, but an Enabled Process can not. In-between each state transition, the following Process methods are automatically called. You can implement them as needed.

Callback Methods for State Transitions

Destroyed ==> Disabled = virtual void setup()

Disabled ==> Destroyed = virtual void cleanup()

Disabled ==> Enabled = virtual void onEnable()

Enabled ==> Disabled = virtual void onDisable()

setup()/cleanup()

Note: For both these methods you are guaranteed that they will only be called once during a state transition. ie. even if I try and add() an already added process many times in a row without destroying it, setup() will still only be called once.

setup()

Setup() does exactly as the name suggests. When a process is being added to the scheduler chain (with add()), the method virtual void setup() will be called. Inside this method you should setup all the necessary things so your Process can run when it is enabled. Here are some things you might need to do:

  • Initialize class attributes
  • Set pinMode()
  • Set Serial baud rates
  • Allocate dynamic memory if you must

cleanup()

Cleanup should undo whatever setup() did. When a process is being destroyed, virtual void cleanup() will be called automatically. If the process is currently enabled, disable() will automatically be called first.

onEnable()/onDisable()

Similar to setup() and cleanup(), onEnable() and onDisable() are callback methods that will be called appropriately when a process is being disabled (disable()) or enabled (enable()). However, unlike setup() and cleanup() these methods should be used to put the process into a disabled or enabled state. The Process will still be part of the Scheduler chain, it just will not be serviced.

onEnable()

Do whatever you need to do to put this Process into an enabled state.

onEnable()

Do whatever you need to do to undo what onEnable() did.

handleWarning(ProcessWarning warning)

This method is not related to any of the above. If the Scheduler encounters an error condition with this Process, this handler will be called with the appropriate warning/error. Currently, here are the possible errors/warnings:

  • WARNING_PROC_OVERSCHEDULED
  • ERROR_PROC_TIMED_OUT (only available with _PROCESS_TIMEOUT_INTERRUPTS compile option)