RGBMatrixDisplay/src/mode/ModeBase.h

92 lines
1.8 KiB
C++

#ifndef MODE_BASE_H
#define MODE_BASE_H
#define TIMER_COUNT 10
#include "BASICS.h"
#include "display/Display.h"
#include "Timer.h"
class ModeBase {
private:
Timer *timers[TIMER_COUNT]{};
uint8_t timerCount = 0;
protected:
Display *display;
public:
explicit ModeBase(Display *display) :
display(display) {
for (Timer **timer = timers; timer < timers + TIMER_COUNT; timer++) {
*timer = nullptr;
}
}
virtual ~ModeBase() {
destroyAllTimers();
};
virtual const char *getName() = 0;
void step(microseconds_t dt) {
for (Timer **timer = timers; timer < timers + TIMER_COUNT; timer++) {
if (*timer != nullptr) {
(*timer)->step(dt);
}
}
doStep(dt);
}
protected:
virtual void doStep(microseconds_t dt) {};
Timer *createTimer(long long millisecondsInterval, Timer::Callback callback) {
for (Timer **timer = timers; timer < timers + TIMER_COUNT; timer++) {
if (*timer == nullptr) {
*timer = new Timer(millisecondsInterval * 1000, callback);
timerCount++;
return *timer;
}
}
Serial.printf("ERROR: Cannot create more than %d timers!\n", TIMER_COUNT);
return nullptr;
}
bool destroyTimer(Timer *t) {
for (Timer **timer = timers; timer < timers + TIMER_COUNT; timer++) {
if (*timer == t) {
_destroyTimer(timer);
return true;
}
}
Serial.printf("ERROR: No such timer.\n");
return false;
}
void destroyAllTimers() {
for (Timer **timer = timers; timer < timers + TIMER_COUNT; timer++) {
if (*timer != nullptr) {
_destroyTimer(timer);
}
}
}
private:
void _destroyTimer(Timer **timer) {
timerCount--;
free(*timer);
*timer = nullptr;
}
};
#endif