Initial commit

This commit is contained in:
2019-05-09 22:17:29 -07:00
parent dcf8ebd034
commit 354b72f160
95 changed files with 3174 additions and 6 deletions

View File

@ -0,0 +1,77 @@
#include "../Figments/Figments.h"
#include "../sprites/Chime.h"
#include "../sprites/Blob.h"
using namespace NSFastLED;
#define CHIME_LENGTH 23
#define CHIME_COUNT 4
#define BLOB_COUNT 10
class ChimesAnimation: public Figment {
public:
ChimesAnimation(Task::State initialState) : Figment("Chimes", initialState) {
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 handleEvent(const InputEvent& evt) override {
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);
});
}
}
void loop() override {
m_chimes.update();
m_blobs.update();
m_flashColor.update();
EVERY_N_MILLISECONDS(5) {
m_flashBrightness.update();
}
}
void render(Display* dpy) const override {
m_chimes.render(dpy);
m_blobs.render(dpy);
Surface fullSurface(dpy, {0, 0}, {255, 0});
NSFastLED::CRGB scaledColor = NSFastLED::CRGB(m_flashColor).nscale8_video(max(10, NSFastLED::ease8InOutCubic(m_flashBrightness)));
fullSurface.paintWith([&](NSFastLED::CRGB& pixel) {
pixel = NSFastLED::blend(scaledColor, pixel, 200);
//pixel = scaledColor;
});
}
SpriteList<Chime<CHIME_LENGTH>, CHIME_COUNT> m_chimes;
SpriteList<Blob, BLOB_COUNT> m_blobs;
AnimatedRGB m_flashColor;
AnimatedNumber m_flashBrightness;
};

View File

@ -0,0 +1,49 @@
#pragma once
#include "../Figments/Figments.h"
#include "../sprites/Blob.h"
using NSFastLED::CHSV;
class Flashlight: public Figment {
public:
Flashlight(Task::State initialState) : Figment("Flashlight", initialState) {
m_blobs.forEach([](Blob &blob) {
blob.setHue(random(255));
blob.setSaturation(10);
blob.setPos(random(255));
});
}
void handleEvent(const InputEvent& evt) override {
if (evt.intent == InputEvent::Acceleration) {
if (evt.asInt() > 10) {
m_blobs.forEach([](Blob& blob) {blob.update();});
}
}
/*if (evt.intent() == InputEvent::UserInput) {
if (evt.asInt() == 1) {
m_blobs.forEach([](Blob& blob) {blob.setPos(random(255));});
}
if (evt.asInt() == 2) {
m_blobs.forEach([](Blob& chime) {blob.setPos(0);});
}
}*/
}
void loop() override {
m_blobs.update();
}
void render(Display* dpy) const override {
m_blobs.render(dpy);
for(int i = 0; i < dpy->pixelCount();i++) {
dpy->pixelAt(i) |= 100;
}
}
private:
static constexpr int blobCount = 30;
SpriteList<Blob, blobCount> m_blobs;
};

View File

@ -0,0 +1,40 @@
#include "../Figments/Figments.h"
template<uint8_t MaxBrightness = 255, uint32_t MaxMilliAmps = 500, uint32_t Voltage = 5>
class Power: public Figment {
public:
Power() : Figment("Power") {}
void handleEvent(const InputEvent& evt) override {
switch (evt.intent) {
case InputEvent::PowerToggle:
m_powerState = m_powerState.value() <= 128 ? 255 : 0;
Log.info("POWER TOGGLE %d", m_powerState.value());
break;
case InputEvent::SetPower:
m_powerState = evt.asInt() == 0 ? 0 : 255;
break;
case InputEvent::SetBrightness:
m_brightness = evt.asInt();
}
}
void loop() override {
m_powerState.update();
m_brightness.update();
}
void render(Display* dpy) const override {
const uint8_t clippedBrightness = min((uint8_t)m_brightness, MaxBrightness);
const uint8_t scaledBrightness = NSFastLED::scale8(m_powerState, clippedBrightness);
const uint8_t videoBrightness = NSFastLED::brighten8_video(scaledBrightness);
const uint8_t powerBrightness = NSFastLED::calculate_max_brightness_for_power_mW(videoBrightness, Watts);
NSFastLED::FastLED.setBrightness(powerBrightness);
}
static constexpr uint32_t Watts = Voltage * MaxMilliAmps;
private:
AnimatedNumber m_powerState = 255;
AnimatedNumber m_brightness = MaxBrightness;
};

View File

@ -0,0 +1,54 @@
#include "../Figments/Figments.h"
#include "../sprites/Blob.h"
using NSFastLED::CRGB;
using NSFastLED::CHSV;
using namespace NSFastLED;
class SolidAnimation: public Figment {
private:
AnimatedNumber m_red, m_green, m_blue;
static constexpr int blobCount = 20;
SpriteList<Blob, blobCount> m_blobs;
public:
SolidAnimation(Task::State initialState) : Figment("Solid", initialState) {
m_blobs.forEach([](Blob& blob) {
blob.setPos(random(140));
blob.setBrightness(random(255));
if (random(255) % 2) {
blob.setVelocity(-1);
}
});
}
void handleEvent(const InputEvent& evt) override {
if (evt.intent == InputEvent::SetColor) {
CRGB nextColor = evt.asRGB();
m_red.set(nextColor.red);
m_green.set(nextColor.green);
m_blue.set(nextColor.blue);
}
}
void loop() override {
m_red.update();
m_green.update();
m_blue.update();
EVERY_N_MILLIS(6) {
CRGB rgb{m_red, m_green, m_blue};
CHSV hsv = NSFastLED::rgb2hsv_approximate(rgb);
m_blobs.forEach([=](Blob& blob) {
blob.setHue(hsv.hue);
blob.setSaturation(hsv.saturation);
});
m_blobs.update();
}
}
void render(Display* dpy) const override {
CRGB color(m_red.value(), m_green.value(), m_blue.value());
Surface(dpy, {0, 0}, {255, 0}) = color;
m_blobs.render(dpy);
}
};

View File

@ -0,0 +1,56 @@
#include "./TestAnimation.h"
#include "FastLED/FastLED.h"
using NSFastLED::CHSV;
using NSFastLED::scale8;
const char*
TestAnimation::name() const
{
return "Test";
}
void
TestAnimation::handleEvent(const InputEvent& evt)
{
if (evt.intent == InputEvent::Acceleration) {
if (evt.asInt() > 5) {
m_brightness += 15;
}
m_hue += scale8(evt.asInt(), 128);
}
if (evt.intent == InputEvent::UserInput) {
switch(evt.asInt()) {
case 1:
m_brightness.set(255, 0);break;
case 2:
m_saturation.set(255, 128);break;
default:
m_brightness.set(255, 0);
m_saturation.set(255, 128);
}
}
}
void
TestAnimation::loop()
{
m_x += 4;
if (m_x % 12 == 0) {
m_y += 28;
}
m_hue.update();
m_saturation.update();
m_brightness.update();
}
void
TestAnimation::render(Display* dpy) const
{
for(uint8_t col = 0; col < 3; col++) {
for (uint8_t i = 0; i < 254; i+=10) {
dpy->pixelAt(VirtualCoordinates{(uint8_t)(m_x + (col * (254 / 3))), (uint8_t)(i + m_y)}) = CHSV(m_hue, m_saturation + 100, scale8(i, m_brightness));
}
}
}

View File

@ -0,0 +1,16 @@
#include "../Figments/Figments.h"
class TestAnimation: public Figment {
public:
const char* name() const;
void handleEvent(const InputEvent& evt) override;
void loop() override;
void render(Display* dpy) const override;
private:
AnimatedNumber m_hue;
AnimatedNumber m_saturation;
AnimatedNumber m_brightness;
uint8_t m_x;
uint8_t m_y;
};

View File

@ -0,0 +1,36 @@
#include "./UpdateStatus.h"
#include "FastLED/FastLED.h"
#include "../Static.h"
using NSFastLED::CRGB;
void
UpdateStatus::handleEvent(const InputEvent& evt)
{
if (evt.intent == InputEvent::FirmwareUpdate) {
static int updateCount = 0;
updateCount++;
Log.info("Update count %d", updateCount);
m_updateReady = true;
}
}
void
UpdateStatus::loop()
{
m_pos++;
}
void
UpdateStatus::render(Display* dpy) const
{
if (m_updateReady) {
for(int i = 0; i < 12; i+=3) {
dpy->pixelAt(m_pos + i) = CRGB(255, 0, 0);
dpy->pixelAt(m_pos + i + 1) = CRGB(0, 255, 0);
dpy->pixelAt(m_pos + i + 2) = CRGB(0, 0, 255);
}
}
}
STATIC_ALLOC(UpdateStatus);

View File

@ -0,0 +1,13 @@
#include "../Figments/Figments.h"
class UpdateStatus: public Figment {
public:
UpdateStatus() : Figment("UpdateStatusAnimation") {}
void handleEvent(const InputEvent& evt) override;
void loop() override;
void render(Display* dpy) const override;
private:
bool m_updateReady = false;
uint8_t m_pos = 0;
};