2019-05-10 05:17:29 +00:00
|
|
|
#pragma once
|
2021-03-29 08:10:55 +00:00
|
|
|
#include <FastLED.h>
|
2019-05-10 05:17:29 +00:00
|
|
|
#include "./Figment.h"
|
|
|
|
|
|
|
|
class Display;
|
|
|
|
|
|
|
|
struct AnimatedNumber {
|
|
|
|
void set(uint8_t v) {
|
|
|
|
set(value(), v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void set(uint8_t start, uint8_t end) {
|
|
|
|
m_start = start;
|
|
|
|
m_end = end;
|
|
|
|
m_idx = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t value() const;
|
|
|
|
|
|
|
|
void update() {
|
|
|
|
if (m_idx < 255) {
|
|
|
|
m_idx += 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-11 09:02:27 +00:00
|
|
|
void update(uint8_t speed) {
|
|
|
|
if (255 - speed >= m_idx) {
|
|
|
|
m_idx += speed;
|
|
|
|
} else {
|
|
|
|
m_idx = 255;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool isFinished() const {
|
|
|
|
return m_idx == 255;
|
|
|
|
}
|
|
|
|
|
2019-05-10 05:17:29 +00:00
|
|
|
AnimatedNumber() {}
|
|
|
|
AnimatedNumber(uint8_t v) : m_end(v) {}
|
|
|
|
|
|
|
|
AnimatedNumber& operator=(uint8_t v) {
|
|
|
|
set(v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
AnimatedNumber& operator+=(uint8_t v) {
|
|
|
|
set(value()+v);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
operator uint8_t() const {
|
|
|
|
return value();
|
|
|
|
}
|
|
|
|
|
|
|
|
operator int() const {
|
|
|
|
return value();
|
|
|
|
}
|
|
|
|
|
|
|
|
operator unsigned int() const {
|
|
|
|
return value();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool operator==(int other) const {
|
|
|
|
return value() == other;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t operator+(uint8_t other) const {
|
|
|
|
return value() + other;
|
|
|
|
}
|
|
|
|
|
|
|
|
int operator+(int other) const {
|
|
|
|
return value() + other;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
uint8_t m_idx = 255;
|
|
|
|
uint8_t m_start = 0;
|
|
|
|
uint8_t m_end = 0;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct AnimatedRGB {
|
2021-03-29 08:10:55 +00:00
|
|
|
CRGB start;
|
|
|
|
CRGB end;
|
2019-05-10 05:17:29 +00:00
|
|
|
AnimatedNumber pos;
|
|
|
|
|
2021-03-29 08:10:55 +00:00
|
|
|
AnimatedRGB(const CRGB& color)
|
2019-05-10 05:17:29 +00:00
|
|
|
: start(color), end(color) {}
|
|
|
|
|
|
|
|
AnimatedRGB() {}
|
|
|
|
|
2021-03-29 08:10:55 +00:00
|
|
|
AnimatedRGB& operator=(const CRGB& rgb) {
|
2019-05-10 05:17:29 +00:00
|
|
|
start = *this;
|
|
|
|
end = rgb;
|
|
|
|
pos.set(0, 255);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void update() {
|
|
|
|
pos.update();
|
|
|
|
}
|
|
|
|
|
2021-03-29 08:10:55 +00:00
|
|
|
operator CRGB() const {
|
|
|
|
uint8_t red = lerp8by8(start.red, end.red, pos);
|
|
|
|
uint8_t green = lerp8by8(start.green, end.green, pos);
|
|
|
|
uint8_t blue = lerp8by8(start.blue, end.blue, pos);
|
|
|
|
return CRGB(red, green, blue);
|
2019-05-10 05:17:29 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, int Size>
|
|
|
|
struct SpriteList {
|
|
|
|
void update() {
|
|
|
|
if (!m_enabled) return;
|
|
|
|
for(int i = 0; i < size; i++) {
|
|
|
|
animations[i].update();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void render(Display* dpy) const {
|
|
|
|
if (!m_enabled) return;
|
|
|
|
for(int i = 0; i < size; i++) {
|
|
|
|
animations[i].render(dpy);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void forEach(std::function<void(T&)> func) {
|
|
|
|
for(int i = 0; i < size; i++) {
|
|
|
|
func(animations[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void forEach(std::function<void(const T&)> func) const {
|
|
|
|
for(int i = 0; i < size; i++) {
|
|
|
|
func(animations[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void disable() {
|
|
|
|
m_enabled = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void enable() {
|
|
|
|
m_enabled = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void toggle() {
|
|
|
|
m_enabled = !m_enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
T& operator[](int idx) {
|
|
|
|
return animations[idx];
|
|
|
|
}
|
|
|
|
|
|
|
|
T animations[Size];
|
|
|
|
bool m_enabled = true;
|
|
|
|
static constexpr int size = Size;
|
|
|
|
using Type = T;
|
|
|
|
};
|