FastLED 3.9.15
Loading...
Searching...
No Matches
animartrix.hpp
Go to the documentation of this file.
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#include "crgb.h"
11#include "fl/dbg.h"
12#include "fl/namespace.h"
13#include "fl/memory.h"
14#include "fl/unique_ptr.h"
15#include "fl/xymap.h"
16#include "fx/fx2d.h"
17#include "eorder.h"
18#include "pixel_controller.h" // For RGB_BYTE_0, RGB_BYTE_1, RGB_BYTE_2
19
20#define ANIMARTRIX_INTERNAL
21#include "animartrix_detail.hpp"
22
23namespace fl {
24
26
81
82fl::string getAnimartrixName(int animation);
83
84class FastLEDANIMartRIX;
85class Animartrix : public Fx2d {
86 public:
87 Animartrix(const XYMap& xyMap, AnimartrixAnim which_animation) : Fx2d(xyMap) {
88 // Note: Swapping out height and width.
89 this->current_animation = which_animation;
90 mXyMap.convertToLookUpTable();
91 }
92
93 Animartrix(const Animartrix &) = delete;
94 void draw(DrawContext context) override;
95 int fxNum() const { return NUM_ANIMATIONS; }
96 void fxSet(int fx);
97 int fxGet() const { return static_cast<int>(current_animation); }
98 Str fxName() const override { return "Animartrix:"; }
99 void fxNext(int fx = 1) { fxSet(fxGet() + fx); }
100 void setColorOrder(EOrder order) { color_order = order; }
101 EOrder getColorOrder() const { return color_order; }
102
103 private:
104 friend void AnimartrixLoop(Animartrix &self, fl::u32 now);
105 friend class FastLEDANIMartRIX;
106 static const char *getAnimartrixName(AnimartrixAnim animation);
109 CRGB *leds = nullptr; // Only set during draw, then unset back to nullptr.
112};
113
114void AnimartrixLoop(Animartrix &self, fl::u32 now);
115
118
124
126 Animartrix *data = nullptr;
127
128 public:
130 this->data = _data;
131 this->init(data->getWidth(), data->getHeight());
132 }
133
134 void setPixelColor(int x, int y, CRGB pixel) {
135 data->leds[xyMap(x, y)] = pixel;
136 }
137 void setPixelColorInternal(int x, int y,
138 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 " << getAnimartrixName(current_animation));
160}
161
162void AnimartrixLoop(Animartrix &self, fl::u32 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 }
169 }
170 if (!self.impl) {
171 self.impl.reset(new FastLEDANIMartRIX(&self));
172 }
173 self.impl->setTime(now);
174 self.impl->loop();
175}
176
185 {ZOOM2, "ZOOM2", &FastLEDANIMartRIX::Zoom2},
186 {ZOOM, "ZOOM", &FastLEDANIMartRIX::Zoom},
190 {YVES, "YVES", &FastLEDANIMartRIX::Yves},
192 {LAVA1, "LAVA1", &FastLEDANIMartRIX::Lava1},
196 {DISTANCE_EXPERIMENT, "DISTANCE_EXPERIMENT",
199 {WAVES, "WAVES", &FastLEDANIMartRIX::Waves},
202 {RINGS, "RINGS", &FastLEDANIMartRIX::Rings},
204 {COMPLEX_KALEIDO_2, "COMPLEX_KALEIDO_2",
206 {COMPLEX_KALEIDO_3, "COMPLEX_KALEIDO_3",
208 {COMPLEX_KALEIDO_4, "COMPLEX_KALEIDO_4",
210 {COMPLEX_KALEIDO_5, "COMPLEX_KALEIDO_5",
212 {COMPLEX_KALEIDO_6, "COMPLEX_KALEIDO_6",
214 {WATER, "WATER", &FastLEDANIMartRIX::Water},
215 {PARAMETRIC_WATER, "PARAMETRIC_WATER",
217 {MODULE_EXPERIMENT1, "MODULE_EXPERIMENT1",
219 {MODULE_EXPERIMENT2, "MODULE_EXPERIMENT2",
221 {MODULE_EXPERIMENT3, "MODULE_EXPERIMENT3",
223 {MODULE_EXPERIMENT4, "MODULE_EXPERIMENT4",
225 {MODULE_EXPERIMENT5, "MODULE_EXPERIMENT5",
227 {MODULE_EXPERIMENT6, "MODULE_EXPERIMENT6",
229 {MODULE_EXPERIMENT7, "MODULE_EXPERIMENT7",
231 {MODULE_EXPERIMENT8, "MODULE_EXPERIMENT8",
233 {MODULE_EXPERIMENT9, "MODULE_EXPERIMENT9",
235 {MODULE_EXPERIMENT10, "MODULE_EXPERIMENT10",
237 {MODULE_EXPERIMENT_SM1, "MODULE_EXPERIMENT_SM1", &FastLEDANIMartRIX::SM1},
238 {MODULE_EXPERIMENT_SM2, "MODULE_EXPERIMENT_SM2", &FastLEDANIMartRIX::SM2},
239 {MODULE_EXPERIMENT_SM3, "MODULE_EXPERIMENT_SM3", &FastLEDANIMartRIX::SM3},
240 {MODULE_EXPERIMENT_SM4, "MODULE_EXPERIMENT_SM4", &FastLEDANIMartRIX::SM4},
241 {MODULE_EXPERIMENT_SM5, "MODULE_EXPERIMENT_SM5", &FastLEDANIMartRIX::SM5},
242 {MODULE_EXPERIMENT_SM6, "MODULE_EXPERIMENT_SM6", &FastLEDANIMartRIX::SM6},
243 {MODULE_EXPERIMENT_SM8, "MODULE_EXPERIMENT_SM8", &FastLEDANIMartRIX::SM8},
244 {MODULE_EXPERIMENT_SM9, "MODULE_EXPERIMENT_SM9", &FastLEDANIMartRIX::SM9},
245 {MODULE_EXPERIMENT_SM10, "MODULE_EXPERIMENT_SM10",
247};
248
250 if (animation < 0 || animation >= NUM_ANIMATIONS) {
251 return "UNKNOWN";
252 }
253 return ANIMATION_TABLE[animation].name;
254}
255
257 for (const auto &entry : ANIMATION_TABLE) {
258 if (entry.anim == data->current_animation) {
259 (this->*entry.func)();
260 return;
261 }
262 }
263 // (this->*ANIMATION_TABLE[index].func)();
264 FASTLED_DBG("Animation not found for " << int(data->current_animation));
265}
266
268 for (const auto &entry : ANIMATION_TABLE) {
269 if (entry.anim == animation) {
270 return entry.name;
271 }
272 }
273 FASTLED_DBG("Animation not found for " << int(animation));
274 return "UNKNOWN";
275}
276
278 this->leds = ctx.leds;
279 AnimartrixLoop(*this, ctx.now);
280 if (color_order != RGB) {
281 for (int i = 0; i < mXyMap.getTotal(); ++i) {
282 CRGB &pixel = ctx.leds[i];
283 const uint8_t b0_index = RGB_BYTE0(color_order);
284 const uint8_t b1_index = RGB_BYTE1(color_order);
285 const uint8_t b2_index = RGB_BYTE2(color_order);
286 pixel = CRGB(pixel.raw[b0_index], pixel.raw[b1_index],
287 pixel.raw[b2_index]);
288 }
289
290 }
291 this->leds = nullptr;
292}
293
294} // namespace fl
int y
Definition simple.h:93
int x
Definition simple.h:92
fl::XYMap xyMap
Definition ColorBoost.h:61
Animartrix(const Animartrix &)=delete
void fxNext(int fx=1)
static const char * getAnimartrixName(AnimartrixAnim animation)
void draw(DrawContext context) override
friend void AnimartrixLoop(Animartrix &self, fl::u32 now)
void setColorOrder(EOrder order)
Animartrix(const XYMap &xyMap, AnimartrixAnim which_animation)
Str fxName() const override
AnimartrixAnim current_animation
friend class FastLEDANIMartRIX
AnimartrixAnim prev_animation
int fxGet() const
fl::unique_ptr< FastLEDANIMartRIX > impl
void fxSet(int fx)
int fxNum() const
EOrder getColorOrder() const
void setPixelColor(int x, int y, CRGB pixel)
uint16_t xyMap(uint16_t x, uint16_t y) override
FastLEDANIMartRIX(Animartrix *_data)
void setPixelColorInternal(int x, int y, animartrix_detail::rgb pixel) override
XYMap mXyMap
Definition fx2d.h:31
uint16_t xyMap(uint16_t x, uint16_t y) const
Definition fx2d.h:21
uint16_t getHeight() const
Definition fx2d.h:24
Fx2d(const XYMap &xyMap)
Definition fx2d.h:20
uint16_t getWidth() const
Definition fx2d.h:25
_DrawContext DrawContext
Definition fx.h:21
Defines the red, green, and blue (RGB) pixel struct.
#define FASTLED_DBG(X)
Definition dbg.h:61
Implements the FastLED namespace macros.
fl::string getAnimartrixName(int animation)
static const AnimartrixEntry ANIMATION_TABLE[]
AnimartrixAnim
@ COMPLEX_KALEIDO
@ MODULE_EXPERIMENT_SM6
@ ZOOM2
@ CALEIDO2
@ ZOOM
@ CHASING_SPIRALS
@ MODULE_EXPERIMENT10
@ WATER
@ MODULE_EXPERIMENT_SM3
@ MODULE_EXPERIMENT6
@ MODULE_EXPERIMENT3
@ CENTER_FIELD
@ RGB_BLOBS4
@ DISTANCE_EXPERIMENT
@ POLAR_WAVES
@ PARAMETRIC_WATER
@ MODULE_EXPERIMENT_SM8
@ COMPLEX_KALEIDO_4
@ MODULE_EXPERIMENT7
@ SPIRALUS
@ MODULE_EXPERIMENT_SM9
@ WAVES
@ COMPLEX_KALEIDO_5
@ COMPLEX_KALEIDO_2
@ MODULE_EXPERIMENT8
@ NUM_ANIMATIONS
@ ROTATING_BLOB
@ MODULE_EXPERIMENT_SM1
@ MODULE_EXPERIMENT_SM4
@ SLOW_FADE
@ RGB_BLOBS
@ MODULE_EXPERIMENT_SM5
@ LAVA1
@ MODULE_EXPERIMENT9
@ COMPLEX_KALEIDO_6
@ MODULE_EXPERIMENT1
@ YVES
@ SCALEDEMO1
@ RGB_BLOBS2
@ MODULE_EXPERIMENT_SM10
@ MODULE_EXPERIMENT4
@ MODULE_EXPERIMENT5
@ HOT_BLOB
@ MODULE_EXPERIMENT2
@ RINGS
@ RGB_BLOBS5
@ COMPLEX_KALEIDO_3
@ CALEIDO3
@ CALEIDO1
@ RGB_BLOBS3
@ MODULE_EXPERIMENT_SM2
@ SPIRALUS2
void AnimartrixLoop(Animartrix &self, fl::u32 now)
EOrder
RGB color channel orderings, used when instantiating controllers to determine what order the controll...
Definition eorder.h:13
@ RGB
Red, Green, Blue (0012)
Definition eorder.h:14
fl::string Str
Definition str.h:36
IMPORTANT!
Definition crgb.h:20
AnimartrixAnim anim
void(FastLEDANIMartRIX::* func)()
################################################## Details with the implementation of Animartrix
#define RGB_BYTE2(RO)
Gets the color channel for byte 2.
#define RGB_BYTE1(RO)
Gets the color channel for byte 1.
#define RGB_BYTE0(RO)
Gets the color channel for byte 0.
Low level pixel data writing class.
#define FASTLED_SMART_PTR(type)
Definition ptr.h:33
Representation of an RGB pixel (Red, Green, Blue)
Definition crgb.h:86