75 lines
2.0 KiB
C++
75 lines
2.0 KiB
C++
#include "Chimes.h"
|
|
#include "../Static.h"
|
|
|
|
ChimesAnimation::ChimesAnimation() : Figment("Chimes", Task::Stopped) {
|
|
}
|
|
|
|
void ChimesAnimation::randomize() {
|
|
m_isRandom = true;
|
|
m_chimes.forEach([](Chime<CHIME_LENGTH> &chime) {
|
|
chime.setPos(random(Chime<CHIME_LENGTH>::Length * 5));
|
|
chime.setHue(random(255));
|
|
chime.setSpeed(random(90) + 138);
|
|
chime.setBrightness(200);
|
|
chime.setOffset(random(1024));
|
|
});
|
|
m_blobs.forEach([](Blob &blob) {
|
|
blob.setPos(random(255));
|
|
blob.setHue(random(255));
|
|
blob.setBrightness(random(255));
|
|
if (random(255) % 2) {
|
|
blob.setVelocity(-1);
|
|
} else {
|
|
blob.setVelocity(1);
|
|
}
|
|
});
|
|
}
|
|
|
|
void ChimesAnimation::handleEvent(const InputEvent& evt) {
|
|
if (evt.intent == InputEvent::UserInput) {
|
|
if (strcmp(evt.asString(), "blobs") == 0) {
|
|
m_blobs.toggle();
|
|
} else if (strcmp(evt.asString(), "chimes") == 0) {
|
|
m_chimes.toggle();
|
|
}
|
|
} else if (evt.intent == InputEvent::SetColor) {
|
|
m_flashBrightness.set(255, 0);
|
|
m_flashColor = evt.asRGB();
|
|
uint8_t flashHue = rgb2hsv_approximate(m_flashColor).hue;
|
|
m_blobs.forEach([&](Blob& blob) {
|
|
blob.setHue(flashHue);
|
|
});
|
|
m_chimes.forEach([&](Chime<CHIME_LENGTH>& chime) {
|
|
chime.setHue(flashHue);
|
|
});
|
|
} else if (evt.intent == InputEvent::Beat) {
|
|
m_isRandom = false;
|
|
}
|
|
}
|
|
|
|
void ChimesAnimation::loop() {
|
|
if (!m_isRandom) {
|
|
randomize();
|
|
}
|
|
m_chimes.update();
|
|
m_blobs.update();
|
|
m_flashColor.update();
|
|
EVERY_N_MILLISECONDS(5) {
|
|
m_flashBrightness.update();
|
|
}
|
|
}
|
|
|
|
void ChimesAnimation::render(Display* dpy) const {
|
|
m_chimes.render(dpy);
|
|
m_blobs.render(dpy);
|
|
Surface fullSurface(dpy, {0, 0}, {255, 0});
|
|
CRGB scaledColor = CRGB(m_flashColor).nscale8_video(std::max((uint8_t)10, ease8InOutCubic(m_flashBrightness)));
|
|
fullSurface.paintWith([&](CRGB& pixel) {
|
|
pixel = blend(scaledColor, pixel, 200);
|
|
//pixel = scaledColor;
|
|
});
|
|
}
|
|
|
|
STATIC_ALLOC(ChimesAnimation);
|
|
STATIC_TASK(ChimesAnimation);
|