FastLED 3.9.7
Loading...
Searching...
No Matches
animartrix.hpp
1#pragma once
2
3// FastLED Adapter for the animartrix fx library.
4// Copyright Stefen Petrick 2023.
5// Adapted to C++ by Netmindz 2023.
6// Adapted to FastLED by Zach Vorhies 2024.
7// For details on the animartrix library and licensing information, see
8// fx/aninamtrix_detail.hpp
9
10
11#include "crgb.h"
12#include "fx/fx2d.h"
13#include "fl/namespace.h"
14#include "fl/ptr.h"
15#include "fl/scoped_ptr.h"
16#include "fl/dbg.h"
17#include "fl/xymap.h"
18
19#define ANIMARTRIX_INTERNAL
20#include "animartrix_detail.hpp"
21
22namespace fl {
23
24FASTLED_SMART_PTR(Animartrix);
25
26enum AnimartrixAnim {
27 RGB_BLOBS5 = 0,
28 RGB_BLOBS4,
29 RGB_BLOBS3,
30 RGB_BLOBS2,
31 RGB_BLOBS,
32 POLAR_WAVES,
33 SLOW_FADE,
34 ZOOM2,
35 ZOOM,
36 HOT_BLOB,
37 SPIRALUS2,
38 SPIRALUS,
39 YVES,
40 SCALEDEMO1,
41 LAVA1,
42 CALEIDO3,
43 CALEIDO2,
44 CALEIDO1,
45 DISTANCE_EXPERIMENT,
46 CENTER_FIELD,
47 WAVES,
48 CHASING_SPIRALS,
49 ROTATING_BLOB,
50 RINGS,
51 COMPLEX_KALEIDO,
52 COMPLEX_KALEIDO_2,
53 COMPLEX_KALEIDO_3,
54 COMPLEX_KALEIDO_4,
55 COMPLEX_KALEIDO_5,
56 COMPLEX_KALEIDO_6,
57 WATER,
58 PARAMETRIC_WATER,
59 MODULE_EXPERIMENT1,
60 MODULE_EXPERIMENT2,
61 MODULE_EXPERIMENT3,
62 MODULE_EXPERIMENT4,
63 MODULE_EXPERIMENT5,
64 MODULE_EXPERIMENT6,
65 MODULE_EXPERIMENT7,
66 MODULE_EXPERIMENT8,
67 MODULE_EXPERIMENT9,
68 MODULE_EXPERIMENT10,
69 MODULE_EXPERIMENT_SM1,
70 MODULE_EXPERIMENT_SM2,
71 MODULE_EXPERIMENT_SM3,
72 MODULE_EXPERIMENT_SM4,
73 MODULE_EXPERIMENT_SM5,
74 MODULE_EXPERIMENT_SM6,
75 MODULE_EXPERIMENT_SM8,
76 MODULE_EXPERIMENT_SM9,
77 MODULE_EXPERIMENT_SM10,
78 NUM_ANIMATIONS
79};
80
81
82
83class FastLEDANIMartRIX;
84class Animartrix : public Fx2d {
85 public:
86 Animartrix(XYMap xyMap, AnimartrixAnim which_animation) : Fx2d(xyMap) {
87 // Note: Swapping out height and width.
88 this->current_animation = which_animation;
89 mXyMap.convertToLookUpTable();
90 }
91
92 Animartrix(const Animartrix &) = delete;
93 void draw(DrawContext context) override;
94 int fxNum() const { return NUM_ANIMATIONS; }
95 void fxSet(int fx);
96 int fxGet() const { return static_cast<int>(current_animation); }
97 Str fxName() const override {
98 return "Animartrix:";
99 }
100 void fxNext(int fx = 1) { fxSet(fxGet() + fx); }
101
102 private:
103 friend void AnimartrixLoop(Animartrix &self, uint32_t now);
104 friend class FastLEDANIMartRIX;
105 static const char *getAnimationName(AnimartrixAnim animation);
106 AnimartrixAnim prev_animation = NUM_ANIMATIONS;
108 CRGB *leds = nullptr; // Only set during draw, then unset back to nullptr.
109 AnimartrixAnim current_animation = RGB_BLOBS5;
110};
111
112void AnimartrixLoop(Animartrix &self, uint32_t now);
113
116
117
119 AnimartrixAnim anim;
120 const char* name;
121 void (FastLEDANIMartRIX::*func)();
122};
123
124
125
127 Animartrix *data = nullptr;
128
129 public:
131 this->data = _data;
132 this->init(data->getWidth(), data->getWidth());
133 }
134
135 void setPixelColor(int x, int y, CRGB pixel) {
136 data->leds[xyMap(x, y)] = pixel;
137 }
138 void setPixelColorInternal(int x, int y, animartrix_detail::rgb pixel) override {
139 setPixelColor(x, y, CRGB(pixel.red, pixel.green, pixel.blue));
140 }
141
142 uint16_t xyMap(uint16_t x, uint16_t y) override {
143 return data->xyMap(x, y);
144 }
145
146 void loop();
147};
148
149void Animartrix::fxSet(int fx) {
150 int curr = fxGet();
151 if (fx < 0) {
152 fx = curr + fx;
153 if (fx < 0) {
154 fx = NUM_ANIMATIONS - 1;
155 }
156 }
157 fx = fx % NUM_ANIMATIONS;
158 current_animation = static_cast<AnimartrixAnim>(fx);
159 FASTLED_DBG("Setting animation to " << getAnimationName(current_animation));
160}
161
162void AnimartrixLoop(Animartrix &self, uint32_t now) {
163 if (self.prev_animation != self.current_animation) {
164 if (self.impl) {
165 // Re-initialize object.
166 self.impl->init(self.getWidth(), self.getHeight());
167 }
168 self.prev_animation = self.current_animation;
169 }
170 if (!self.impl) {
171 self.impl.reset(new FastLEDANIMartRIX(&self));
172 }
173 self.impl->setTime(now);
174 self.impl->loop();
175}
176
177static const AnimartrixEntry ANIMATION_TABLE[] = {
178 {RGB_BLOBS5, "RGB_BLOBS5", &FastLEDANIMartRIX::RGB_Blobs5},
179 {RGB_BLOBS4, "RGB_BLOBS4", &FastLEDANIMartRIX::RGB_Blobs4},
180 {RGB_BLOBS3, "RGB_BLOBS3", &FastLEDANIMartRIX::RGB_Blobs3},
181 {RGB_BLOBS2, "RGB_BLOBS2", &FastLEDANIMartRIX::RGB_Blobs2},
182 {RGB_BLOBS, "RGB_BLOBS", &FastLEDANIMartRIX::RGB_Blobs},
183 {POLAR_WAVES, "POLAR_WAVES", &FastLEDANIMartRIX::Polar_Waves},
184 {SLOW_FADE, "SLOW_FADE", &FastLEDANIMartRIX::Slow_Fade},
185 {ZOOM2, "ZOOM2", &FastLEDANIMartRIX::Zoom2},
186 {ZOOM, "ZOOM", &FastLEDANIMartRIX::Zoom},
187 {HOT_BLOB, "HOT_BLOB", &FastLEDANIMartRIX::Hot_Blob},
188 {SPIRALUS2, "SPIRALUS2", &FastLEDANIMartRIX::Spiralus2},
189 {SPIRALUS, "SPIRALUS", &FastLEDANIMartRIX::Spiralus},
190 {YVES, "YVES", &FastLEDANIMartRIX::Yves},
191 {SCALEDEMO1, "SCALEDEMO1", &FastLEDANIMartRIX::Scaledemo1},
192 {LAVA1, "LAVA1", &FastLEDANIMartRIX::Lava1},
193 {CALEIDO3, "CALEIDO3", &FastLEDANIMartRIX::Caleido3},
194 {CALEIDO2, "CALEIDO2", &FastLEDANIMartRIX::Caleido2},
195 {CALEIDO1, "CALEIDO1", &FastLEDANIMartRIX::Caleido1},
196 {DISTANCE_EXPERIMENT, "DISTANCE_EXPERIMENT", &FastLEDANIMartRIX::Distance_Experiment},
197 {CENTER_FIELD, "CENTER_FIELD", &FastLEDANIMartRIX::Center_Field},
198 {WAVES, "WAVES", &FastLEDANIMartRIX::Waves},
199 {CHASING_SPIRALS, "CHASING_SPIRALS", &FastLEDANIMartRIX::Chasing_Spirals},
200 {ROTATING_BLOB, "ROTATING_BLOB", &FastLEDANIMartRIX::Rotating_Blob},
201 {RINGS, "RINGS", &FastLEDANIMartRIX::Rings},
202 {COMPLEX_KALEIDO, "COMPLEX_KALEIDO", &FastLEDANIMartRIX::Complex_Kaleido},
203 {COMPLEX_KALEIDO_2, "COMPLEX_KALEIDO_2", &FastLEDANIMartRIX::Complex_Kaleido_2},
204 {COMPLEX_KALEIDO_3, "COMPLEX_KALEIDO_3", &FastLEDANIMartRIX::Complex_Kaleido_3},
205 {COMPLEX_KALEIDO_4, "COMPLEX_KALEIDO_4", &FastLEDANIMartRIX::Complex_Kaleido_4},
206 {COMPLEX_KALEIDO_5, "COMPLEX_KALEIDO_5", &FastLEDANIMartRIX::Complex_Kaleido_5},
207 {COMPLEX_KALEIDO_6, "COMPLEX_KALEIDO_6", &FastLEDANIMartRIX::Complex_Kaleido_6},
208 {WATER, "WATER", &FastLEDANIMartRIX::Water},
209 {PARAMETRIC_WATER, "PARAMETRIC_WATER", &FastLEDANIMartRIX::Parametric_Water},
210 {MODULE_EXPERIMENT1, "MODULE_EXPERIMENT1", &FastLEDANIMartRIX::Module_Experiment1},
211 {MODULE_EXPERIMENT2, "MODULE_EXPERIMENT2", &FastLEDANIMartRIX::Module_Experiment2},
212 {MODULE_EXPERIMENT3, "MODULE_EXPERIMENT3", &FastLEDANIMartRIX::Module_Experiment3},
213 {MODULE_EXPERIMENT4, "MODULE_EXPERIMENT4", &FastLEDANIMartRIX::Module_Experiment4},
214 {MODULE_EXPERIMENT5, "MODULE_EXPERIMENT5", &FastLEDANIMartRIX::Module_Experiment5},
215 {MODULE_EXPERIMENT6, "MODULE_EXPERIMENT6", &FastLEDANIMartRIX::Module_Experiment6},
216 {MODULE_EXPERIMENT7, "MODULE_EXPERIMENT7", &FastLEDANIMartRIX::Module_Experiment7},
217 {MODULE_EXPERIMENT8, "MODULE_EXPERIMENT8", &FastLEDANIMartRIX::Module_Experiment8},
218 {MODULE_EXPERIMENT9, "MODULE_EXPERIMENT9", &FastLEDANIMartRIX::Module_Experiment9},
219 {MODULE_EXPERIMENT10, "MODULE_EXPERIMENT10", &FastLEDANIMartRIX::Module_Experiment10},
220 {MODULE_EXPERIMENT_SM1, "MODULE_EXPERIMENT_SM1", &FastLEDANIMartRIX::SM1},
221 {MODULE_EXPERIMENT_SM2, "MODULE_EXPERIMENT_SM2", &FastLEDANIMartRIX::SM2},
222 {MODULE_EXPERIMENT_SM3, "MODULE_EXPERIMENT_SM3", &FastLEDANIMartRIX::SM3},
223 {MODULE_EXPERIMENT_SM4, "MODULE_EXPERIMENT_SM4", &FastLEDANIMartRIX::SM4},
224 {MODULE_EXPERIMENT_SM5, "MODULE_EXPERIMENT_SM5", &FastLEDANIMartRIX::SM5},
225 {MODULE_EXPERIMENT_SM6, "MODULE_EXPERIMENT_SM6", &FastLEDANIMartRIX::SM6},
226 {MODULE_EXPERIMENT_SM8, "MODULE_EXPERIMENT_SM8", &FastLEDANIMartRIX::SM8},
227 {MODULE_EXPERIMENT_SM9, "MODULE_EXPERIMENT_SM9", &FastLEDANIMartRIX::SM9},
228 {MODULE_EXPERIMENT_SM10, "MODULE_EXPERIMENT_SM10", &FastLEDANIMartRIX::SM10},
229};
230
231void FastLEDANIMartRIX::loop() {
232 for (const auto& entry : ANIMATION_TABLE) {
233 if (entry.anim == data->current_animation) {
234 (this->*entry.func)();
235 return;
236 }
237 }
238 // (this->*ANIMATION_TABLE[index].func)();
239 FASTLED_DBG("Animation not found for " << int(data->current_animation));
240}
241
242const char* Animartrix::getAnimationName(AnimartrixAnim animation) {
243 for (const auto& entry : ANIMATION_TABLE) {
244 if (entry.anim == animation) {
245 return entry.name;
246 }
247 }
248 FASTLED_DBG("Animation not found for " << int(animation));
249 return "UNKNOWN";
250}
251
253 this->leds = ctx.leds;
254 AnimartrixLoop(*this, ctx.now);
255 this->leds = nullptr;
256}
257
258} // namespace fl
void draw(DrawContext context) override
Definition str.h:336
Defines the red, green, and blue (RGB) pixel struct.
Implements the FastLED namespace macros.
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:54
################################################## Details with the implementation of Animartrix