|
| 1 | +#pragma once |
| 2 | +using namespace std; |
| 3 | + |
| 4 | +#include "../interfaces.h" |
| 5 | +#include "../ledeffectbase.h" |
| 6 | +#include "../pixeltypes.h" |
| 7 | +#include <cmath> |
| 8 | + |
| 9 | +class AuroraEffect : public LEDEffectBase |
| 10 | +{ |
| 11 | +private: |
| 12 | + double _time; |
| 13 | + double _speed; |
| 14 | + double _brightness; |
| 15 | + |
| 16 | +public: |
| 17 | + AuroraEffect(const string& name, double speed = 0.2, double brightness = 1.0) |
| 18 | + : LEDEffectBase(name, "AuroraEffect"), _time(0.0), _speed(speed), _brightness(brightness) |
| 19 | + { |
| 20 | + } |
| 21 | + |
| 22 | + void Start(ICanvas& canvas) override |
| 23 | + { |
| 24 | + _time = 0.0; |
| 25 | + } |
| 26 | + |
| 27 | + void Update(ICanvas& canvas, microseconds deltaTime) override |
| 28 | + { |
| 29 | + _time += _speed * deltaTime.count() / 1000000.0; |
| 30 | + |
| 31 | + auto& graphics = canvas.Graphics(); |
| 32 | + int width = graphics.Width(); |
| 33 | + int height = graphics.Height(); |
| 34 | + |
| 35 | + for (int y = 0; y < height; ++y) |
| 36 | + { |
| 37 | + for (int x = 0; x < width; ++x) |
| 38 | + { |
| 39 | + // Create some "dancing" waves using multiple sine waves |
| 40 | + double xf = x / static_cast<double>(width); |
| 41 | + double yf = y / static_cast<double>(height); |
| 42 | + |
| 43 | + double v1 = sin(xf * 2.0 + _time * 0.7); |
| 44 | + double v2 = sin(yf * 3.0 + _time * 0.5); |
| 45 | + double v3 = sin((xf + yf) * 1.5 + _time * 0.3); |
| 46 | + double v4 = sin(sqrt(xf*xf + yf*yf) * 4.0 + _time * 0.2); |
| 47 | + |
| 48 | + double combined = (v1 + v2 + v3 + v4) / 4.0; // range [-1, 1] |
| 49 | + combined = (combined + 1.0) / 2.0; // range [0, 1] |
| 50 | + |
| 51 | + // Aurora colors: green, blue, purple |
| 52 | + // We want a nice transition between these colors |
| 53 | + double hue; |
| 54 | + if (combined < 0.3) { |
| 55 | + // Deep Blue to Cyan (240 to 180) |
| 56 | + hue = 240.0 - (combined / 0.3) * 60.0; |
| 57 | + } else if (combined < 0.6) { |
| 58 | + // Cyan to Green (180 to 120) |
| 59 | + hue = 180.0 - ((combined - 0.3) / 0.3) * 60.0; |
| 60 | + } else { |
| 61 | + // Green to Purple (120 to 280) |
| 62 | + double t = (combined - 0.6) / 0.4; |
| 63 | + if (t < 0.5) { |
| 64 | + hue = 120.0 - (t * 2.0) * 40.0; // Green to Lime (120 to 80) |
| 65 | + } else { |
| 66 | + hue = 260.0 + (t - 0.5) * 2.0 * 60.0; // Purple to Violet (260 to 320) |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + // Value (brightness) should also pulse |
| 71 | + // Lift the floor significantly and use a smoother curve |
| 72 | + double brightness = (0.4 + 0.6 * combined) * _brightness; |
| 73 | + |
| 74 | + // Add some vertical bands to make it look more like aurora curtains |
| 75 | + double curtain = sin(xf * 15.0 + sin(_time * 0.2) * 5.0) * 0.15 + 0.85; |
| 76 | + brightness *= curtain; |
| 77 | + |
| 78 | + // Max saturation for visibility |
| 79 | + CRGB color = CRGB::HSV2RGB(hue, 1.0, brightness); |
| 80 | + graphics.SetPixel(x, y, color); |
| 81 | + } |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + friend inline void to_json(nlohmann::json& j, const AuroraEffect & effect); |
| 86 | + friend inline void from_json(const nlohmann::json& j, shared_ptr<AuroraEffect>& effect); |
| 87 | +}; |
| 88 | + |
| 89 | +inline void to_json(nlohmann::json& j, const AuroraEffect & effect) |
| 90 | +{ |
| 91 | + j = { |
| 92 | + {"name", effect.Name()}, |
| 93 | + {"speed", effect._speed}, |
| 94 | + {"brightness", effect._brightness} |
| 95 | + }; |
| 96 | +} |
| 97 | + |
| 98 | +inline void from_json(const nlohmann::json& j, shared_ptr<AuroraEffect>& effect) |
| 99 | +{ |
| 100 | + effect = make_shared<AuroraEffect>( |
| 101 | + j.at("name").get<string>(), |
| 102 | + j.value("speed", 0.2), |
| 103 | + j.value("brightness", 1.0) |
| 104 | + ); |
| 105 | +} |
0 commit comments