-
Notifications
You must be signed in to change notification settings - Fork 16
Process Callback Methods
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.
Destroyed ==> Disabled = virtual void setup()
Disabled ==> Destroyed = virtual void cleanup()
Disabled ==> Enabled = virtual void onEnable()
Enabled ==> Disabled = virtual void onDisable()
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() 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 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.
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.
Do whatever you need to do to put this Process into an enabled state.
Do whatever you need to do to undo what onEnable() did.
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)