Initial commit
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
#include "component.h"
|
||||
|
||||
Component::Component(uint16_t loopDelay) {
|
||||
this->loopDelay = loopDelay;
|
||||
}
|
||||
|
||||
void Component::loop() {
|
||||
if (!this->active)
|
||||
return;
|
||||
|
||||
if (this->childComponents.size()){
|
||||
std::list<Component*>::iterator it;
|
||||
for (it = this->childComponents.begin(); it != this->childComponents.end(); it++)
|
||||
(*it)->loop();
|
||||
}
|
||||
this->runAsChild();
|
||||
|
||||
if (this->onlyChilds)
|
||||
return;
|
||||
|
||||
if (this->loopDelay && millis() - this->lastMillis < this->loopDelay)
|
||||
return;
|
||||
|
||||
this->lastMillis = millis();
|
||||
|
||||
this->beforeRun();
|
||||
this->run();
|
||||
this->afterRun();
|
||||
|
||||
if (this->timeUpdateAfter)
|
||||
this->lastMillis = millis();
|
||||
}
|
||||
|
||||
void Component::addChildComponent(Component* child) {
|
||||
this->childComponents.push_back(child);
|
||||
}
|
||||
|
||||
void Component::removeChildComponent(Component* child) {
|
||||
this->childComponents.remove(child);
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
/**
|
||||
* @file component.h
|
||||
* @author Alexander Klein (alex@kleiax.de)
|
||||
* @brief
|
||||
* @version 0.1
|
||||
* @date 2023-08-16
|
||||
*
|
||||
* @copyright Copyright (c) 2023
|
||||
*
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <Arduino.h>
|
||||
|
||||
#include <list>
|
||||
|
||||
class Component {
|
||||
public:
|
||||
Component() {}
|
||||
Component(uint16_t loopDelay);
|
||||
|
||||
void loop();
|
||||
|
||||
void deactivate() { this->active = false; }
|
||||
void activate() { this->active = false; }
|
||||
|
||||
protected:
|
||||
virtual void runAsChild() {}
|
||||
virtual void beforeRun() {}
|
||||
virtual void run() = 0;
|
||||
virtual void afterRun() {}
|
||||
|
||||
void addChildComponent(Component* child);
|
||||
void removeChildComponent(Component* child);
|
||||
|
||||
void activateOnlyChilds() { this->onlyChilds = true; }
|
||||
void deactivateOnlyChilds() { this->onlyChilds = false; }
|
||||
|
||||
void setTimerAfterTask() { this->timeUpdateAfter = true; }
|
||||
|
||||
uint16_t loopDelay = 0;
|
||||
|
||||
private:
|
||||
std::list<Component*> childComponents;
|
||||
|
||||
bool active = true;
|
||||
bool onlyChilds = false;
|
||||
bool timeUpdateAfter = false;
|
||||
uint32_t lastMillis = 0;
|
||||
};
|
||||
Reference in New Issue
Block a user