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