FastLED 3.10.8
Loading...
Searching...
No Matches
center_field.cpp.hpp
Go to the documentation of this file.
5#include "fl/stl/noexcept.h"
6
9
10namespace fl {
11
13 auto *e = ctx.mEngine.get();
14 e->get_ready();
15
16 // Perlin noise is periodic with period 256 at integer coordinates.
17 // With scale_y = 0.07, the effective period for offset_y is 256/0.07 ≈ 3657.14.
18 // Reducing modulo this period keeps float32 precise at extreme uptimes.
19 static constexpr float perlin_period = 3657.14285714f; // 256.0f / 0.07f
20
21 e->timings.master_speed = 0.01;
22 e->timings.ratio[0] = 1;
23 e->timings.ratio[1] = 1.1;
24 e->timings.ratio[2] = 1.2;
25
26 e->timings.offset[1] = 100;
27 e->timings.offset[2] = 200;
28 e->timings.offset[3] = 300;
29
30 e->calculate_oscillators(e->timings);
31
32 for (int x = 0; x < e->num_x; x++) {
33 for (int y = 0; y < e->num_y; y++) {
34 e->animation.angle = e->polar_theta[x][y];
35 e->animation.scale_x = 0.07;
36 e->animation.scale_y = 0.07;
37 e->animation.scale_z = 0.1;
38 e->animation.dist = 5 * fl::sqrtf(e->distance[x][y]);
39 e->animation.offset_y = fl::fmodf(e->move.linear[0], perlin_period);
40 e->animation.offset_x = 0;
41 e->animation.z = 0;
42 float show1 = e->render_value(e->animation);
43
44 e->animation.angle = e->polar_theta[x][y];
45 e->animation.scale_x = 0.07;
46 e->animation.scale_y = 0.07;
47 e->animation.scale_z = 0.1;
48 e->animation.dist = 4 * fl::sqrtf(e->distance[x][y]);
49 e->animation.offset_y = fl::fmodf(e->move.linear[0], perlin_period);
50 e->animation.offset_x = 0;
51 e->animation.z = 0;
52 float show2 = e->render_value(e->animation);
53
54 e->pixel.red = show1;
55 e->pixel.green = show2;
56 e->pixel.blue = 0;
57
58 e->pixel = e->rgb_sanity_check(e->pixel);
59 e->setPixelColorInternal(x, y, e->pixel);
60 }
61 }
62}
63
64// ============================================================================
65// Fixed-Point Q31 Implementation of Center_Field
66// ============================================================================
67
69 using FP = fl::s16x16;
70
71 auto *e = ctx.mEngine.get();
72 e->get_ready();
73
74 // Same timing setup as float
75 e->timings.master_speed = 0.01;
76 e->timings.ratio[0] = 1;
77 e->timings.ratio[1] = 1.1;
78 e->timings.ratio[2] = 1.2;
79 e->timings.offset[1] = 100;
80 e->timings.offset[2] = 200;
81 e->timings.offset[3] = 300;
82 e->calculate_oscillators(e->timings);
83
84 // Build/update per-pixel geometry cache
85 mState.ensureCache(e);
86
87 const int total_pixels = mState.count;
88 const fl::i32 *fade_lut = fl::assume_aligned<16>(mState.fade_lut);
89 const fl::u8 *perm = PERLIN_NOISE;
90
91 // Per-frame constants converted to s16x16
92 constexpr FP scale_xy(0.07f);
93 const FP center_x_fp(e->animation.center_x);
94 const FP center_y_fp(e->animation.center_y);
95
96 // Period reduction: Perlin noise is periodic with period 256 at integer
97 // coordinates. With scale_y = 0.07, the effective period for offset_y is
98 // 256 / 0.07 ≈ 3657.14. Reducing modulo this period prevents s16x16
99 // overflow and maintains precision at high time values.
100 constexpr float perlin_period = 3657.14285714f; // 256.0f / 0.07f
101 const FP offset_y_fp(fl::fmodf(e->move.linear[0], perlin_period));
102 constexpr FP five(5.0f);
103 constexpr FP four(4.0f);
104 const fl::i32 scale_xy_raw = scale_xy.raw();
105 const fl::i32 cx_raw = center_x_fp.raw();
106 const fl::i32 cy_raw = center_y_fp.raw();
107 const fl::i32 oy_raw = offset_y_fp.raw();
108 const fl::i32 five_raw = five.raw();
109 const fl::i32 four_raw = four.raw();
110
111 // low_limit=0, high_limit=1 (default render_parameters)
112 constexpr fl::i32 FP_ONE = static_cast<fl::i32>(1) << FP::FRAC_BITS;
113
115 p.scale_x_raw = scale_xy_raw;
116 p.scale_y_raw = scale_xy_raw;
117 p.scale_z_raw = FP(0.1f).raw();
118 p.offset_x_raw = 0;
119 p.offset_y_raw = oy_raw;
120 p.offset_z_raw = 0;
121 p.z_raw = 0;
122 p.center_x_raw = cx_raw;
123 p.center_y_raw = cy_raw;
124 p.low_limit_raw = 0;
126
127 fl::span<CRGB> leds = e->mCtx->leds;
128
129 for (int i = 0; i < total_pixels; i++) {
130 const fl::i32 theta_raw = mState.polar_theta_raw[i];
131 const fl::i32 sqrt_dist_raw = mState.sqrt_distance_raw[i];
132
133 // Pass 1: dist = 5 * sqrt(distance)
134 fl::i32 dist1_raw = static_cast<fl::i32>(
135 (static_cast<fl::i64>(five_raw) * sqrt_dist_raw) >> FP::FRAC_BITS);
136 p.angle_raw = theta_raw;
137 p.dist_raw = dist1_raw;
138 fl::i32 show1 = render_value_fp(p, fade_lut, perm);
139
140 // Pass 2: dist = 4 * sqrt(distance)
141 fl::i32 dist2_raw = static_cast<fl::i32>(
142 (static_cast<fl::i64>(four_raw) * sqrt_dist_raw) >> FP::FRAC_BITS);
143 p.dist_raw = dist2_raw;
144 fl::i32 show2 = render_value_fp(p, fade_lut, perm);
145
146 // pixel.red = show1, pixel.green = show2, pixel.blue = 0
147 fl::i32 r = show1;
148 fl::i32 g = show2;
149 fl::i32 b = 0;
150 rgb_sanity_check_fp(r, g, b);
151
152 leds[mState.pixel_idx[i]] = CRGB(
153 static_cast<fl::u8>(r),
154 static_cast<fl::u8>(g),
155 static_cast<fl::u8>(b));
156 }
157}
158
159} // namespace fl
160
CRGB leds[1]
void draw(Context &ctx) FL_NO_EXCEPT override
void draw(Context &ctx) FL_NO_EXCEPT override
constexpr i32 raw() const FL_NO_EXCEPT
Definition s16x16.h:62
#define FL_OPTIMIZATION_LEVEL_O3_BEGIN
#define FL_FAST_MATH_BEGIN
#define FL_FAST_MATH_END
#define FL_OPTIMIZATION_LEVEL_O3_END
T * assume_aligned(T *ptr) FL_NO_EXCEPT
Definition s16x16x4.h:126
unsigned char u8
Definition stdint.h:131
FASTLED_FORCE_INLINE fl::i32 render_value_fp(const render_parameters_fp &p, const fl::i32 *fade_lut, const fl::u8 *perm) FL_NO_EXCEPT
fl::CRGB CRGB
Definition video.h:18
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
FASTLED_FORCE_INLINE void rgb_sanity_check_fp(fl::i32 &r, fl::i32 &g, fl::i32 &b) FL_NO_EXCEPT
static constexpr i32 FP_ONE
float sqrtf(float value) FL_NO_EXCEPT
Definition math.h:461
InputGamut g
Definition rgbw.h:125
float fmodf(float x, float y) FL_NO_EXCEPT
Definition math.h:344
Base definition for an LED controller.
Definition crgb.hpp:179