GCC Code Coverage Report


Directory: ./
File: overlays/wled/wled.h
Date: 2026-10-29 08:11:58
Exec Total Coverage
Lines: 19 24 79.2%
Functions: 10 11 90.9%
Branches: 4 6 66.7%

Line Branch Exec Source
1 #pragma once
2 /*
3 * This file is part of the Falcon Player (FPP) and is Copyright (C)
4 * 2013-2022 by the Falcon Player Developers.
5 *
6 * The Falcon Player (FPP) is free software, and is covered under
7 * multiple Open Source licenses. Please see the included 'LICENSES'
8 * file for descriptions of what files are covered by each license.
9 *
10 * This source file is covered under the LGPL v2.1 as described in the
11 * included LICENSE.LGPL file.
12 */
13
14 #include <algorithm>
15 #include <array>
16 #include <mutex>
17 #include <math.h>
18 #include <stdint.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <string>
22
23 #define MAX_LEDS 1024 * 64
24 #define MAX_LEDS_PER_BUS MAX_LEDS
25 #define MAX_LED_MEMORY (MAX_LEDS * 3)
26
27 #define ABL_MILLIAMPS_DEFAULT 0
28
29 #define WLED_DISABLE_ALEXA
30 #define WLED_DISABLE_MQTT
31 #define WLED_DISABLE_ESPNOW
32 #define WLED_DISABLE_HTTP
33 #define WLED_DISABLE_WEBSOCKETS
34 #define WLED_DISABLE_FTP
35 #define WLED_DISABLE_HTTP_AUTH
36 #define WLED_DISABLE_OTA
37 #define WLED_DISABLE_NTP
38 #define WLED_DISABLE_MDNS
39 #define WLED_DISABLE_DMX
40 #define WLED_DISABLE_DMX512
41 #define WLED_DISABLE_DMX512_SERIAL
42 #define WLED_DISABLE_DMX512_UNIVERSE
43 #define WLED_DISABLE_DMX512_UNIVERSE_SERIAL
44 #define WLED_DISABLE_MODE_BLEND
45 #define WLED_PS_DONT_REPLACE_FX
46
47 constexpr int NUM_SAMPLES = 1024;
48
49 long long GetTimeMS(void);
50 long long GetMonotonicTimeMS(void);
51 long long GetMonotonicTimeMicros(void);
52 long long GetTimeMicros(void);
53
54 // Audio sample buffer + computed FFT frame, in the form used by both
55 // the WLED reactive effects and the WLEDAudioSync UDP broadcast.
56 // `volumeSmth`/`volumeRaw` are 0-255 amplitudes (NOT frequencies);
57 // `samplePeak` is a beat-detection flag with a short hold window.
58 struct AudioFrame {
59 uint8_t fftResult[16];
60 float volumeSmth;
61 uint16_t volumeRaw;
62 uint8_t samplePeak;
63 float FFT_Magnitude;
64 float FFT_MajorPeak;
65 };
66
67 // Pull samples from the configured audio source (playing media or a
68 // configured input device). Returns false if no source is producing.
69 bool fetchAudioSamples(std::array<float, NUM_SAMPLES>& samples, int& sampleRate);
70
71 // FFT + log-binning + RMS + beat detection. Pure function over
72 // (samples, sampleRate) → AudioFrame, with EMA + peak-timing kept
73 // in static locals (single-threaded callers only).
74 void computeAudioFrame(const std::array<float, NUM_SAMPLES>& samples,
75 int sampleRate,
76 AudioFrame& out);
77 // Effects time themselves with these (as WLED does), so they are on the
78 // monotonic clock: on the wall clock a step back froze every running WLED
79 // effect for the size of the step.
80 54189 inline uint32_t millis() {
81 54189 return GetMonotonicTimeMS();
82 }
83 inline uint32_t GET_MILLIS() {
84 return GetMonotonicTimeMS();
85 }
86
87 276 inline uint64_t micros() {
88 276 return GetMonotonicTimeMicros();
89 }
90
91 #define PSTR(a) a
92
93 #include "FastLED.h"
94 #include "fcn_declare.h"
95
96 #include "bus_manager.h"
97
98 #include "FX.h"
99
100 class PixelOverlayModel;
101 #include "../PolarBufferMap.h"
102
103 typedef uint8_t fract8;
104 typedef uint16_t fract16;
105 typedef uint16_t accum88;
106 typedef uint8_t byte;
107 typedef bool boolean;
108
109 #define PROGMEM
110 #define IRAM_ATTR
111 #define USER_PRINTLN(a)
112 #define USER_PRINT(a)
113 #define pgm_read_byte(x) (*((const uint8_t*)(x)))
114 #define pgm_read_word(x) (*((const uint16_t*)(x)))
115 #define pgm_read_dword(x) (*((const uint32_t*)(x)))
116
117 6761 inline uint8_t pgm_read_byte_near(uint8_t* p) {
118 6761 return *p;
119 }
120 123568 inline uint8_t pgm_read_byte_near(const unsigned char* p) {
121 123568 return *p;
122 }
123
124 constexpr float HALF_PI = 3.14159f / 2.0f;
125
126 #define RGBW32(r, g, b, w) (uint32_t((byte(w) << 24) | (byte(r) << 16) | (byte(g) << 8) | (byte(b))))
127 #define R(c) (byte((c) >> 16))
128 #define G(c) (byte((c) >> 8))
129 #define B(c) (byte(c))
130 #define W(c) (byte((c) >> 24))
131
132 #define yield()
133
134 extern uint16_t rand16seed;
135 extern time_t localTime;
136 extern uint8_t randomPaletteChangeTime;
137 extern bool stateChanged;
138 extern byte lastRandomIndex;
139 extern char* ledmapNames[];
140
141 constexpr bool fadeTransition = false;
142 constexpr bool gammaCorrectCol = false;
143 constexpr bool useAMPM = false;
144 constexpr bool correctWB = false;
145 constexpr bool autoSegments = false;
146 constexpr bool gammaCorrectBri = false;
147 constexpr bool cctFromRgb = false;
148 extern uint16_t ledMaps;
149
150 extern int hour(time_t t); // the second for the given time
151 extern int minute(time_t t); // the minute for the given time
152 extern int second(time_t t); // the second for the given time
153 extern int day(time_t t); // the day for the given time
154 extern int month(time_t t); // the month for the given time
155 extern int year(time_t t); // the year for the given time
156 extern int weekday(time_t t); // the day of the week, 1-7 with Sunday = 1
157 extern const char* monthStr(uint8_t month);
158 extern const char* monthShortStr(uint8_t month);
159 extern const char* dayStr(uint8_t day);
160 extern const char* dayShortStr(uint8_t day);
161
162 #define FASTLED_RAND16_2053 ((uint16_t)(2053))
163 #define FASTLED_RAND16_13849 ((uint16_t)(13849))
164 #define APPLY_FASTLED_RAND16_2053(x) (x * FASTLED_RAND16_2053)
165
166 inline uint8_t map8(uint8_t x, uint8_t in_min, uint8_t in_max, uint8_t out_min, uint8_t out_max) {
167 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
168 }
169 inline uint16_t map16(uint16_t x, uint16_t in_min, uint16_t in_max, uint16_t out_min, uint16_t out_max) {
170 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
171 }
172 143554 inline long map(long x, long in_min, long in_max, long out_min, long out_max) {
173 143554 return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
174 }
175
176 12316 inline uint16_t max(uint16_t a, uint16_t b) {
177
2/2
✓ Branch 0 taken 11017 times.
✓ Branch 1 taken 1299 times.
12316 return a >= b ? a : b;
178 }
179 9561045 inline uint16_t min(uint16_t a, uint16_t b) {
180
2/2
✓ Branch 0 taken 600062 times.
✓ Branch 1 taken 8960983 times.
9561045 return a >= b ? b : a;
181 }
182
183 12673 inline bool bitRead(uint8_t b, int position) {
184 12673 return (b >> position) & 0x1;
185 }
186 ✗ inline void bitWrite(uint8_t& b, int position, bool v) {
187 ✗ if (v) {
188 ✗ b |= 1 << position;
189 } else {
190 ✗ b &= ~(1 << position);
191 }
192 ✗ }
193
194 #define constrain(a, b, c) ((a < b) ? b : ((a > c) ? c : a))
195
196 #define M_TWOPI 6.283185307179586476925286766559
197 #define radians(a) ((a * 3.14159) / 180.0)
198 #define DEG_TO_RAD radians(1)
199
200 #define PSTR(a) a
201 #define strncmp_P strncmp
202 #define sprintf_P sprintf
203 #define memcpy_P memcpy
204 #define snprintf_P snprintf
205 #define strcpy_P strcpy
206 #define strlen_P strlen
207 #define strncpy_P strncpy
208
209 #define bitClear(var, bit) ((var) &= (~(1ULL << (bit))))
210 #define bitSet(var, bit) ((var) |= (1ULL << (bit)))
211
212 typedef std::string String;
213
214 void colorHStoRGB(uint16_t hue, byte sat, byte* rgb);
215
216 class WLEDFileSystem {
217 public:
218 961 bool exists(...) { return false; }
219 };
220 extern WLEDFileSystem WLED_FS;
221
222 // various globals
223 extern uint8_t blendingStyle; // effect blending/transitionig style
224 extern std::vector<BusConfig> busConfigs;
225 extern uint8_t errorFlag;
226
227 // brightness
228 extern uint8_t briS; // default brightness
229 extern uint8_t bri; // global brightness (set)
230 extern uint8_t briOld; // global brightness while in transition loop (previous iteration)
231 extern uint8_t briT; // global brightness during transition
232 extern uint8_t briLast; // brightness before turned off. Used for toggle function
233 extern uint8_t whiteLast; // white channel before turned off. Used for toggle function in ir.cpp
234
235 extern bool useHarmonicRandomPalette;
236 extern bool useGlobalLedBuffer;
237 extern uint8_t realtimeMode;
238 extern bool realtimeRespectLedMaps;
239
240 extern char settingsPIN[5]; // PIN for settings pages
241 extern bool correctPIN;
242 extern unsigned long lastEditTime;
243
244 class WS2812FXExt : public WS2812FX {
245 public:
246 WS2812FXExt();
247 WS2812FXExt(PixelOverlayModel* m, int mapping, int brightness,
248 uint8_t mode, uint8_t speed, uint8_t itensity, uint8_t palette,
249 uint32_t c1, uint32_t c2, uint32_t c3,
250 uint8_t custom1, uint8_t custom2, uint8_t custom3,
251 int check1, int check2, int check3,
252 const std::string& text,
253 const PolarMode polarMode = PolarMode::None);
254 ~WS2812FXExt();
255
256 bool setEffectConfig(uint8_t m, uint8_t s, uint8_t i, uint8_t p);
257 PixelOverlayModel* model = nullptr;
258 int mapping = 0;
259 // Non-null when a Radial/Angular mapping resolved to a real layout. Owned
260 // by the model, which outlives the effect. Null means "use `mapping`".
261 const PolarBufferMap* polar = nullptr;
262 int brightness = 127;
263
264 static void pushCurrent(WS2812FXExt* e);
265 static void popCurrent();
266 static void clearInstance();
267 static WS2812FXExt* getInstance();
268
269 // The WLED code was written for one strip per device: matrix geometry
270 // (Segment::maxWidth/maxHeight) and the segment-data budget are statics
271 // shared by every strip. Each model's effect is a strip, so construction,
272 // service() and destruction all hold this, and a strip restores its own
273 // geometry when it becomes current. Recursive; a leaf below effectLock.
274 static std::recursive_mutex& lock();
275
276 um_data_t* getAudioSamples(int simulationId);
277
278 private:
279 WS2812FXExt* parent = nullptr;
280 // This strip's matrix geometry, saved after finalizeInit() and restored
281 // into the shared statics by pushCurrent(). 0 until saved.
282 uint16_t ownMaxWidth = 0;
283 uint16_t ownMaxHeight = 0;
284
285 // Sound Reactive Stuff
286 void processSamples(std::array<float, NUM_SAMPLES>& samples, int sampleRate);
287 static constexpr int UDATA_ELEMENTS = 8;
288 um_data_t um_data;
289
290 std::array<uint8_t, 16> fftResult;
291 std::array<um_types_t, UDATA_ELEMENTS> udata_types;
292 std::array<void*, UDATA_ELEMENTS> udata_ptrs;
293 uint8_t samplePeak;
294 float FFT_MajorPeak;
295 uint8_t maxVol;
296 uint8_t binNum;
297 float volumeSmth;
298 uint16_t volumeRaw;
299 float my_magnitude;
300 };
301 15763791 inline WS2812FXExt& strip() {
302 15763791 return *WS2812FXExt::getInstance();
303 }
304