145 lines
3.6 KiB
C++
145 lines
3.6 KiB
C++
/**
|
|
* @file component.h
|
|
* @author Alexander Klein (alex@kleiax.de)
|
|
* @brief Contains an interface to make non blocking components with delay.
|
|
* @version 0.1
|
|
* @date 2023-08-16
|
|
*
|
|
* @copyright Copyright (c) 2023
|
|
*
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <Arduino.h>
|
|
|
|
#include <list>
|
|
|
|
/**
|
|
* @brief An Interface to make components
|
|
*
|
|
* A component is a task that be called in a loop which not
|
|
* runs every loop, so every component has a non blocking delay.
|
|
*
|
|
* A component can manage other components which called children components.
|
|
*/
|
|
class Component
|
|
{
|
|
public:
|
|
/**
|
|
* @brief Construct a new Component object
|
|
*
|
|
* With the default constructor the created component is
|
|
* by default inactive. This does not affect the execution of
|
|
* the children components.
|
|
*/
|
|
Component() {}
|
|
|
|
/**
|
|
* @brief Construct a new Component object
|
|
*
|
|
* If the given parameter is zero, there are no differences to the
|
|
* default constructor.
|
|
*
|
|
* @param loopDelay the minimum time in milliseconds before the task runs
|
|
*/
|
|
Component(uint16_t loopDelay);
|
|
|
|
/**
|
|
* @brief Runs the children components and the task
|
|
*
|
|
* The loop() function of the children is called every time this
|
|
* loop is called.
|
|
*
|
|
* The run() function which presents the task of this component is
|
|
* only called if the delay is reached.
|
|
*/
|
|
void loop();
|
|
|
|
/**
|
|
* @brief Deactivate this component
|
|
*
|
|
* If the component is deactivated the call of loop() ha no effect
|
|
*/
|
|
void deactivate() { this->active = false; }
|
|
void activate() { this->active = true; }
|
|
|
|
protected:
|
|
/**
|
|
* @brief Override this function to avoid the delay
|
|
*/
|
|
virtual void runAsChild() {}
|
|
|
|
/**
|
|
* @brief Runs befor the run() function
|
|
*
|
|
* This function is only called, if the delay
|
|
* is reached.
|
|
* The function do nothing, except the function is overwritten
|
|
* by the class which inherits this class.
|
|
*/
|
|
virtual void beforeRun() {}
|
|
|
|
/**
|
|
* @brief The actual task
|
|
*
|
|
* This function have to be overwritten by the inheriting class.
|
|
*/
|
|
virtual void run() = 0;
|
|
|
|
/**
|
|
* @brief Runs after the run() function
|
|
*
|
|
* This function is only called, if the delay
|
|
* is reached.
|
|
* The function do nothing, except the function is overwritten
|
|
* by the class which inherits this class.
|
|
*/
|
|
virtual void afterRun() {}
|
|
|
|
/**
|
|
* @brief Adds a child component
|
|
*
|
|
* The child component will be called every time the loop() function
|
|
* is called.
|
|
*
|
|
* @param child
|
|
*/
|
|
void addChildComponent(Component *child);
|
|
void removeChildComponent(Component *child);
|
|
|
|
void activateOnlyChilds() { this->onlyChilds = true; }
|
|
|
|
/**
|
|
* @brief Skip the actual task
|
|
*
|
|
* Same as set the loopDelay to zero.
|
|
*/
|
|
void deactivateOnlyChilds() { this->onlyChilds = false; }
|
|
|
|
/**
|
|
* @brief Set the timer after task
|
|
*
|
|
* If this function is called once the measurement of the delay
|
|
* starts after task has finished. The default is, that the
|
|
* measurement begins at the start of the task.
|
|
*/
|
|
void setTimerAfterTask() { this->timeUpdateAfter = true; }
|
|
|
|
/**
|
|
* @brief the minimum time in milliseconds before the task runs
|
|
*
|
|
* If this value is zero, the functions beforeRun(), run() and
|
|
* afterRun() would not be called
|
|
*/
|
|
uint16_t loopDelay = 0;
|
|
|
|
private:
|
|
std::list<Component *> childComponents;
|
|
|
|
bool active = true;
|
|
bool onlyChilds = false;
|
|
bool timeUpdateAfter = false;
|
|
uint32_t lastMillis = 0;
|
|
};
|