59 lines
1.4 KiB
C++
59 lines
1.4 KiB
C++
#pragma once
|
|
#include <Arduino.h>
|
|
#include <functional>
|
|
#include <ArduinoLog.h>
|
|
|
|
#define F_LIKELY(x) __builtin_expect(!!(x), true)
|
|
#define F_UNLIKELY(x) __builtin_expect(!!(x), false)
|
|
|
|
class Display;
|
|
class InputEvent;
|
|
class InputSource;
|
|
|
|
struct Loopable {
|
|
virtual void handleEvent(const InputEvent& event) {}
|
|
virtual void loop() = 0;
|
|
};
|
|
|
|
struct Task : public virtual Loopable {
|
|
virtual void onStart() {};
|
|
virtual void onStop() {};
|
|
|
|
enum State {
|
|
Running,
|
|
Stopped,
|
|
};
|
|
|
|
Task() {}
|
|
explicit Task(const char* name) : name(name) {}
|
|
|
|
void start() { state = Running; onStart(); }
|
|
void stop() { onStop(); state = Stopped; }
|
|
virtual bool isFigment() const { return false; }
|
|
|
|
const char* name = "";
|
|
State state = Stopped;
|
|
};
|
|
|
|
struct TaskFunc: public Task {
|
|
TaskFunc(std::function<void()> func) : Task("lambda"), func(func) {}
|
|
void loop() override {func();}
|
|
std::function<void()> func;
|
|
};
|
|
|
|
struct Figment: public Task {
|
|
Figment() : Task() {}
|
|
explicit Figment(const char* name) : Task(name) {}
|
|
virtual void render(Display* dpy) const = 0;
|
|
bool isFigment() const override { return true; }
|
|
};
|
|
|
|
struct FigmentFunc: public Figment {
|
|
FigmentFunc(std::function<void(Display*)> func) : Figment("lambda"), func(func) {}
|
|
void loop() override {}
|
|
void render(Display* dpy) const override {
|
|
func(dpy);
|
|
}
|
|
std::function<void(Display*)> func;
|
|
};
|