GCC Code Coverage Report


Directory: ./
File: overlays/wled/fcn_declare.h
Date: 2026-10-29 08:11:58
Exec Total Coverage
Lines: 24 25 96.0%
Functions: 13 13 100.0%
Branches: 3 10 30.0%

Line Branch Exec Source
1 #pragma once
2 #ifndef WLED_FCN_DECLARE_H
3 #define WLED_FCN_DECLARE_H
4
5 /*
6 * All globally accessible functions are declared here
7 */
8
9 //alexa.cpp
10 #ifndef WLED_DISABLE_ALEXA
11 void onAlexaChange(EspalexaDevice* dev);
12 void alexaInit();
13 void handleAlexa();
14 void onAlexaChange(EspalexaDevice* dev);
15 #endif
16
17
18 #ifndef WLED_DISABLE_FPP
19 //button.cpp
20 void shortPressAction(uint8_t b=0);
21 void longPressAction(uint8_t b=0);
22 void doublePressAction(uint8_t b=0);
23 bool isButtonPressed(uint8_t b=0);
24 void handleButton();
25 void handleIO();
26 void IRAM_ATTR touchButtonISR();
27
28 //cfg.cpp
29 bool deserializeConfig(JsonObject doc, bool fromFS = false);
30 void deserializeConfigFromFS();
31 bool deserializeConfigSec();
32 void serializeConfig(JsonObject doc);
33 void serializeConfigToFS();
34 void serializeConfigSec();
35
36 template<typename DestType>
37 bool getJsonValue(const JsonVariant& element, DestType& destination) {
38 if (element.isNull()) {
39 return false;
40 }
41
42 destination = element.as<DestType>();
43 return true;
44 }
45
46 template<typename DestType, typename DefaultType>
47 bool getJsonValue(const JsonVariant& element, DestType& destination, const DefaultType defaultValue) {
48 if(!getJsonValue(element, destination)) {
49 destination = defaultValue;
50 return false;
51 }
52
53 return true;
54 }
55
56 typedef struct WiFiConfig {
57 char clientSSID[33];
58 char clientPass[65];
59 uint8_t bssid[6];
60 IPAddress staticIP;
61 IPAddress staticGW;
62 IPAddress staticSN;
63 WiFiConfig(const char *ssid="", const char *pass="", uint32_t ip=0, uint32_t gw=0, uint32_t subnet=0x00FFFFFF) // little endian
64 : staticIP(ip)
65 , staticGW(gw)
66 , staticSN(subnet)
67 {
68 strncpy(clientSSID, ssid, 32); clientSSID[32] = 0;
69 strncpy(clientPass, pass, 64); clientPass[64] = 0;
70 memset(bssid, 0, sizeof(bssid));
71 }
72 } wifi_config;
73 #endif
74
75
76 //colors.cpp
77 #define ColorFromPalette ColorFromPaletteWLED // override fastled version
78
79 // CRGBW can be used to manipulate 32bit colors faster. However: if it is passed to functions, it adds overhead compared to a uint32_t color
80 // use with caution and pay attention to flash size. Usually converting a uint32_t to CRGBW to extract r, g, b, w values is slower than using bitshifts
81 // it can be useful to avoid back and forth conversions between uint32_t and fastled CRGB
82 struct CRGBW {
83 union {
84 uint32_t color32; // Access as a 32-bit value (0xWWRRGGBB)
85 struct {
86 uint8_t b;
87 uint8_t g;
88 uint8_t r;
89 uint8_t w;
90 };
91 uint8_t raw[4]; // Access as an array in the order B, G, R, W
92 };
93
94 // Default constructor
95 inline CRGBW() __attribute__((always_inline)) = default;
96
97 // Constructor from a 32-bit color (0xWWRRGGBB)
98 332693 constexpr CRGBW(uint32_t color) __attribute__((always_inline)) : color32(color) {}
99
100 // Constructor with r, g, b, w values
101 constexpr CRGBW(uint8_t red, uint8_t green, uint8_t blue, uint8_t white = 0) __attribute__((always_inline)) : b(blue), g(green), r(red), w(white) {}
102
103 // Constructor from CRGB
104 13143 constexpr CRGBW(CRGB rgb) __attribute__((always_inline)) : b(rgb.b), g(rgb.g), r(rgb.r), w(0) {}
105
106 // Access as an array
107 inline const uint8_t& operator[] (uint8_t x) const __attribute__((always_inline)) { return raw[x]; }
108
109 // Assignment from 32-bit color
110 7511 inline CRGBW& operator=(uint32_t color) __attribute__((always_inline)) { color32 = color; return *this; }
111
112 // Assignment from r, g, b, w
113 inline CRGBW& operator=(const CRGB& rgb) __attribute__((always_inline)) { b = rgb.b; g = rgb.g; r = rgb.r; w = 0; return *this; }
114
115 // Conversion operator to uint32_t
116 inline operator uint32_t() const __attribute__((always_inline)) {
117 28165 return color32;
118 }
119 /*
120 // Conversion operator to CRGB
121 inline operator CRGB() const __attribute__((always_inline)) {
122 return CRGB(r, g, b);
123 }
124
125 CRGBW& scale32 (uint8_t scaledown) // 32bit math
126 {
127 if (color32 == 0) return *this; // 2 extra instructions, worth it if called a lot on black (which probably is true) adding check if scaledown is zero adds much more overhead as its 8bit
128 uint32_t scale = scaledown + 1;
129 uint32_t rb = (((color32 & 0x00FF00FF) * scale) >> 8) & 0x00FF00FF; // scale red and blue
130 uint32_t wg = (((color32 & 0xFF00FF00) >> 8) * scale) & 0xFF00FF00; // scale white and green
131 color32 = rb | wg;
132 return *this;
133 }*/
134
135 };
136
137 struct CHSV32 { // 32bit HSV color with 16bit hue for more accurate conversions
138 union {
139 struct {
140 uint16_t h; // hue
141 uint8_t s; // saturation
142 uint8_t v; // value
143 };
144 uint32_t raw; // 32bit access
145 };
146 inline CHSV32() __attribute__((always_inline)) = default; // default constructor
147
148 /// Allow construction from hue, saturation, and value
149 /// @param ih input hue
150 /// @param is input saturation
151 /// @param iv input value
152 inline CHSV32(uint16_t ih, uint8_t is, uint8_t iv) __attribute__((always_inline)) // constructor from 16bit h, s, v
153 ✗ : h(ih), s(is), v(iv) {}
154 inline CHSV32(uint8_t ih, uint8_t is, uint8_t iv) __attribute__((always_inline)) // constructor from 8bit h, s, v
155 : h((uint16_t)ih << 8), s(is), v(iv) {}
156 inline CHSV32(const CHSV& chsv) __attribute__((always_inline)) // constructor from CHSV
157 : h((uint16_t)chsv.h << 8), s(chsv.s), v(chsv.v) {}
158 457 inline operator CHSV() const { return CHSV((uint8_t)(h >> 8), s, v); } // typecast to CHSV
159 };
160 // similar to NeoPixelBus NeoGammaTableMethod but allows dynamic changes (superseded by NPB::NeoGammaDynamicTableMethod)
161 class NeoGammaWLEDMethod {
162 public:
163 [[gnu::hot]] static uint8_t Correct(uint8_t value); // apply Gamma to single channel
164 [[gnu::hot]] static uint32_t Correct32(uint32_t color); // apply Gamma to RGBW32 color (WLED specific, not used by NPB)
165 static void calcGammaTable(float gamma); // re-calculates & fills gamma table
166 27863 static inline uint8_t rawGamma8(uint8_t val) { return gammaT[val]; } // get value from Gamma table (WLED specific, not used by NPB)
167 private:
168 static uint8_t gammaT[];
169 };
170 #define gamma32(c) NeoGammaWLEDMethod::Correct32(c)
171 #define gamma8(c) NeoGammaWLEDMethod::rawGamma8(c)
172 [[gnu::hot, gnu::pure]] uint32_t color_blend(uint32_t c1, uint32_t c2 , uint8_t blend);
173 17706 inline uint32_t color_blend16(uint32_t c1, uint32_t c2, uint16_t b) { return color_blend(c1, c2, b >> 8); };
174 [[gnu::hot, gnu::pure]] uint32_t color_add(uint32_t, uint32_t, bool preserveCR = false);
175 [[gnu::hot, gnu::pure]] uint32_t color_fade(uint32_t c1, uint8_t amount, bool video=false);
176 [[gnu::hot, gnu::pure]] uint32_t ColorFromPaletteWLED(const CRGBPalette16 &pal, unsigned index, uint8_t brightness = (uint8_t)255U, TBlendType blendType = LINEARBLEND);
177 CRGBPalette16 generateHarmonicRandomPalette(const CRGBPalette16 &basepalette);
178 CRGBPalette16 generateRandomPalette();
179 inline uint32_t colorFromRgbw(byte* rgbw) { return uint32_t((byte(rgbw[3]) << 24) | (byte(rgbw[0]) << 16) | (byte(rgbw[1]) << 8) | (byte(rgbw[2]))); }
180 void hsv2rgb(const CHSV32& hsv, uint32_t& rgb);
181 void colorHStoRGB(uint16_t hue, byte sat, byte* rgb);
182 void rgb2hsv(const uint32_t rgb, CHSV32& hsv);
183 void adjust_color(uint32_t& rgb, int32_t hueShift, int32_t satChange, int32_t valueChange);
184
1/2
✓ Branch 0 taken 457 times.
✗ Branch 1 not taken.
914 inline CHSV rgb2hsv(const CRGB c) { CHSV32 hsv; rgb2hsv((uint32_t((byte(c.r) << 16) | (byte(c.g) << 8) | (byte(c.b)))), hsv); return CHSV(hsv); } // CRGB to hsv
185 void colorKtoRGB(uint16_t kelvin, byte* rgb);
186 void colorCTtoRGB(uint16_t mired, byte* rgb); //white spectrum to rgb
187 void colorXYtoRGB(float x, float y, byte* rgb); // only defined if huesync disabled TODO
188 void colorRGBtoXY(const byte* rgb, float* xy); // only defined if huesync disabled TODO
189 void colorFromDecOrHexString(byte* rgb, const char* in);
190 bool colorFromHexString(byte* rgb, const char* in);
191 uint32_t colorBalanceFromKelvin(uint16_t kelvin, uint32_t rgb);
192 uint16_t approximateKelvinFromRGB(uint32_t rgb);
193 void setRandomColor(byte* rgb);
194
195 #ifndef WLED_DISABLE_FPP
196
197 //dmx_output.cpp
198 void initDMXOutput();
199 void handleDMXOutput();
200
201 //dmx_input.cpp
202 void initDMXInput();
203 void handleDMXInput();
204
205 //e131.cpp
206 void handleE131Packet(e131_packet_t* p, IPAddress clientIP, byte protocol);
207 void handleDMXData(uint16_t uni, uint16_t dmxChannels, uint8_t* e131_data, uint8_t mde, uint8_t previousUniverses);
208 void handleArtnetPollReply(IPAddress ipAddress);
209 void prepareArtnetPollReply(ArtPollReply* reply);
210 void sendArtnetPollReply(ArtPollReply* reply, IPAddress ipAddress, uint16_t portAddress);
211
212 //file.cpp
213 bool handleFileRead(AsyncWebServerRequest*, String path);
214 bool writeObjectToFileUsingId(const char* file, uint16_t id, const JsonDocument* content);
215 bool writeObjectToFile(const char* file, const char* key, const JsonDocument* content);
216 bool readObjectFromFileUsingId(const char* file, uint16_t id, JsonDocument* dest, const JsonDocument* filter = nullptr);
217 bool readObjectFromFile(const char* file, const char* key, JsonDocument* dest, const JsonDocument* filter = nullptr);
218 void updateFSInfo();
219 void closeFile();
220 inline bool writeObjectToFileUsingId(const String &file, uint16_t id, const JsonDocument* content) { return writeObjectToFileUsingId(file.c_str(), id, content); };
221 inline bool writeObjectToFile(const String &file, const char* key, const JsonDocument* content) { return writeObjectToFile(file.c_str(), key, content); };
222 inline bool readObjectFromFileUsingId(const String &file, uint16_t id, JsonDocument* dest, const JsonDocument* filter = nullptr) { return readObjectFromFileUsingId(file.c_str(), id, dest); };
223 inline bool readObjectFromFile(const String &file, const char* key, JsonDocument* dest, const JsonDocument* filter = nullptr) { return readObjectFromFile(file.c_str(), key, dest); };
224
225 //hue.cpp
226 void handleHue();
227 void reconnectHue();
228 void onHueError(void* arg, AsyncClient* client, int8_t error);
229 void onHueConnect(void* arg, AsyncClient* client);
230 void sendHuePoll();
231 void onHueData(void* arg, AsyncClient* client, void *data, size_t len);
232
233 #endif
234
235 #include "FX.h" // must be below colors.cpp declarations (potentially due to duplicate declarations of e.g. color_blend)
236
237 //image_loader.cpp
238 #ifdef WLED_ENABLE_GIF
239 bool fileSeekCallback(unsigned long position);
240 unsigned long filePositionCallback(void);
241 int fileReadCallback(void);
242 int fileReadBlockCallback(void * buffer, int numberOfBytes);
243 int fileSizeCallback(void);
244 byte renderImageToSegment(Segment &seg);
245 void endImagePlayback(Segment* seg);
246 #endif
247
248 #ifndef WLED_DISABLE_FPP
249 //improv.cpp
250 enum ImprovRPCType {
251 Command_Wifi = 0x01,
252 Request_State = 0x02,
253 Request_Info = 0x03,
254 Request_Scan = 0x04
255 };
256
257 void handleImprovPacket();
258 void sendImprovRPCResult(ImprovRPCType type, uint8_t n_strings = 0, const char **strings = nullptr);
259 void sendImprovStateResponse(uint8_t state, bool error = false);
260 void sendImprovInfoResponse();
261 void startImprovWifiScan();
262 void handleImprovWifiScan();
263 void sendImprovIPRPCResult(ImprovRPCType type);
264
265 //ir.cpp
266 void initIR();
267 void deInitIR();
268 void handleIR();
269
270 //json.cpp
271 #include "ESPAsyncWebServer.h"
272 #include "src/dependencies/json/ArduinoJson-v6.h"
273 #include "src/dependencies/json/AsyncJson-v6.h"
274 #include "FX.h"
275
276 bool deserializeSegment(JsonObject elem, byte it, byte presetId = 0);
277 bool deserializeState(JsonObject root, byte callMode = CALL_MODE_DIRECT_CHANGE, byte presetId = 0);
278 void serializeSegment(const JsonObject& root, const Segment& seg, byte id, bool forPreset = false, bool segmentBounds = true);
279 void serializeState(JsonObject root, bool forPreset = false, bool includeBri = true, bool segmentBounds = true, bool selectedSegmentsOnly = false);
280 void serializeInfo(JsonObject root);
281 void serializeModeNames(JsonArray arr);
282 void serializeModeData(JsonArray fxdata);
283 void serveJson(AsyncWebServerRequest* request);
284 #ifdef WLED_ENABLE_JSONLIVE
285 bool serveLiveLeds(AsyncWebServerRequest* request, uint32_t wsClient = 0);
286 #endif
287
288 //led.cpp
289 void setValuesFromSegment(uint8_t s);
290 void setValuesFromMainSeg();
291 void setValuesFromFirstSelectedSeg();
292 void toggleOnOff();
293 void applyBri();
294 void applyFinalBri();
295 void applyValuesToSelectedSegs();
296 void colorUpdated(byte callMode);
297 void stateUpdated(byte callMode);
298 void updateInterfaces(uint8_t callMode);
299 void handleTransitions();
300 void handleNightlight();
301 byte scaledBri(byte in);
302
303 #ifdef WLED_ENABLE_LOXONE
304 //lx_parser.cpp
305 bool parseLx(int lxValue, byte* rgbw);
306 void parseLxJson(int lxValue, byte segId, bool secondary);
307 #endif
308
309 //mqtt.cpp
310 bool initMqtt();
311 void publishMqtt();
312
313 //ntp.cpp
314 void handleTime();
315 void handleNetworkTime();
316 void sendNTPPacket();
317 bool checkNTPResponse();
318 void updateLocalTime();
319 void getTimeString(char* out);
320 bool checkCountdown();
321 void setCountdown();
322 byte weekdayMondayFirst();
323 void checkTimers();
324 void calculateSunriseAndSunset();
325 void setTimeFromAPI(uint32_t timein);
326 #endif
327
328
329 //overlay.cpp
330 void handleOverlayDraw();
331 void _overlayAnalogCountdown();
332 void _overlayAnalogClock();
333
334 #ifndef WLED_DISABLE_FPP
335
336 //playlist.cpp
337 void shufflePlaylist();
338 void unloadPlaylist();
339 int16_t loadPlaylist(JsonObject playlistObject, byte presetId = 0);
340 void handlePlaylist();
341 void serializePlaylist(JsonObject obj);
342
343 //presets.cpp
344 const char *getPresetsFileName(bool persistent = true);
345 bool presetNeedsSaving();
346 void initPresetsFile();
347 void handlePresets();
348 bool applyPreset(byte index, byte callMode = CALL_MODE_DIRECT_CHANGE);
349 bool applyPresetFromPlaylist(byte index);
350 void applyPresetWithFallback(uint8_t presetID, uint8_t callMode, uint8_t effectID = 0, uint8_t paletteID = 0);
351 inline bool applyTemporaryPreset() {return applyPreset(255);};
352 void savePreset(byte index, const char* pname = nullptr, JsonObject saveobj = JsonObject());
353 inline void saveTemporaryPreset() {savePreset(255);};
354 void deletePreset(byte index);
355 bool getPresetName(byte index, String& name);
356
357 //remote.cpp
358 void handleWiZdata(uint8_t *incomingData, size_t len);
359 void handleRemote();
360
361 //set.cpp
362 bool isAsterisksOnly(const char* str, byte maxLen);
363 void handleSettingsSet(AsyncWebServerRequest *request, byte subPage);
364 bool handleSet(AsyncWebServerRequest *request, const String& req, bool apply=true);
365
366 //udp.cpp
367 void notify(byte callMode, bool followUp=false);
368 uint8_t realtimeBroadcast(uint8_t type, IPAddress client, uint16_t length, const uint8_t* buffer, uint8_t bri=255, bool isRGBW=false);
369 void realtimeLock(uint32_t timeoutMs, byte md = REALTIME_MODE_GENERIC);
370 void exitRealtime();
371 void handleNotifications();
372 void setRealtimePixel(uint16_t i, byte r, byte g, byte b, byte w);
373 void refreshNodeList();
374 void sendSysInfoUDP();
375 #ifndef WLED_DISABLE_ESPNOW
376 void espNowSentCB(uint8_t* address, uint8_t status);
377 void espNowReceiveCB(uint8_t* address, uint8_t* data, uint8_t len, signed int rssi, bool broadcast);
378 #endif
379
380 //network.cpp
381 bool initEthernet(); // result is informational
382 int getSignalQuality(int rssi);
383 void fillMAC2Str(char *str, const uint8_t *mac);
384 void fillStr2MAC(uint8_t *mac, const char *str);
385 int findWiFi(bool doScan = false);
386 bool isWiFiConfigured();
387 void WiFiEvent(WiFiEvent_t event);
388 #endif
389
390 //um_manager.cpp
391 typedef enum UM_Data_Types {
392 UMT_BYTE = 0,
393 UMT_UINT16,
394 UMT_INT16,
395 UMT_UINT32,
396 UMT_INT32,
397 UMT_FLOAT,
398 UMT_DOUBLE,
399 UMT_BYTE_ARR,
400 UMT_UINT16_ARR,
401 UMT_INT16_ARR,
402 UMT_UINT32_ARR,
403 UMT_INT32_ARR,
404 UMT_FLOAT_ARR,
405 UMT_DOUBLE_ARR
406 } um_types_t;
407 typedef struct UM_Exchange_Data {
408 // should just use: size_t arr_size, void **arr_ptr, byte *ptr_type
409 size_t u_size; // size of u_data array
410 um_types_t *u_type; // array of data types
411 void **u_data; // array of pointers to data
412 3542 UM_Exchange_Data() {
413 3542 u_size = 0;
414 3542 u_type = nullptr;
415 3542 u_data = nullptr;
416 3542 }
417 1628 ~UM_Exchange_Data() {
418
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1628 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1628 if (u_type) delete[] u_type;
419
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 1628 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
1628 if (u_data) delete[] u_data;
420 1628 }
421 } um_data_t;
422 const unsigned int um_data_size = sizeof(um_data_t); // 12 bytes
423
424 class Usermod {
425 protected:
426 um_data_t *um_data; // um_data should be allocated using new in (derived) Usermod's setup() or constructor
427 public:
428 Usermod() : um_data(nullptr) {};
429 virtual ~Usermod() { if (um_data) delete um_data; }
430 virtual void setup() = 0; // pure virtual, has to be overriden
431 virtual void loop() = 0; // pure virtual, has to be overriden
432 virtual void handleOverlayDraw() {} // called after all effects have been processed, just before strip.show()
433 virtual bool handleButton(uint8_t b) { return false; } // button overrides are possible here
434 virtual bool getUMData(um_data_t **data) { if (data) *data = nullptr; return false; }; // usermod data exchange [see examples for audio effects]
435 virtual void connected() {} // called when WiFi is (re)connected
436 #ifndef WLED_DISABLE_FPP
437 virtual void appendConfigData(Print& settingsScript); // helper function called from usermod settings page to add metadata for entry fields
438 virtual void addToJsonState(JsonObject& obj) {} // add JSON objects for WLED state
439 virtual void addToJsonInfo(JsonObject& obj) {} // add JSON objects for UI Info page
440 virtual void readFromJsonState(JsonObject& obj) {} // process JSON messages received from web server
441 virtual void addToConfig(JsonObject& obj) {} // add JSON entries that go to cfg.json
442 virtual bool readFromConfig(JsonObject& obj) { return true; } // Note as of 2021-06 readFromConfig() now needs to return a bool, see usermod_v2_example.h
443 #endif
444 virtual void onMqttConnect(bool sessionPresent) {} // fired when MQTT connection is established (so usermod can subscribe)
445 virtual bool onMqttMessage(char* topic, char* payload) { return false; } // fired upon MQTT message received (wled topic)
446 virtual bool onEspNowMessage(uint8_t* sender, uint8_t* payload, uint8_t len) { return false; } // fired upon ESP-NOW message received
447 virtual void onUpdateBegin(bool) {} // fired prior to and after unsuccessful firmware update
448 virtual void onStateChange(uint8_t mode) {} // fired upon WLED state change
449 virtual uint16_t getId() {return USERMOD_ID_UNSPECIFIED;}
450
451 // API shims
452 private:
453 static Print* oappend_shim;
454 // old form of appendConfigData; called by default appendConfigData(Print&) with oappend_shim set up
455 // private so it is not accidentally invoked except via Usermod::appendConfigData(Print&)
456 virtual void appendConfigData() {}
457 protected:
458 // Shim for oappend(), which used to exist in utils.cpp
459 template<typename T> static inline void oappend(const T& t) { oappend_shim->print(t); };
460 #ifdef ESP8266
461 // Handle print(PSTR()) without crashing by detecting PROGMEM strings
462 static void oappend(const char* c) { if ((intptr_t) c >= 0x40000000) oappend_shim->print(FPSTR(c)); else oappend_shim->print(c); };
463 #endif
464 };
465
466 namespace UsermodManager {
467 void loop();
468 void handleOverlayDraw();
469 bool handleButton(uint8_t b);
470 bool getUMData(um_data_t **um_data, uint8_t mod_id = USERMOD_ID_RESERVED); // USERMOD_ID_RESERVED will poll all usermods
471 void setup();
472 void connected();
473 #ifndef WLED_DISABLE_FPP
474 void appendConfigData(Print&);
475 void addToJsonState(JsonObject& obj);
476 void addToJsonInfo(JsonObject& obj);
477 void readFromJsonState(JsonObject& obj);
478 void addToConfig(JsonObject& obj);
479 bool readFromConfig(JsonObject& obj);
480 #ifndef WLED_DISABLE_MQTT
481 void onMqttConnect(bool sessionPresent);
482 bool onMqttMessage(char* topic, char* payload);
483 #endif
484 #ifndef WLED_DISABLE_ESPNOW
485 bool onEspNowMessage(uint8_t* sender, uint8_t* payload, uint8_t len);
486 #endif
487 #endif
488 void onUpdateBegin(bool);
489 void onStateChange(uint8_t);
490 Usermod* lookup(uint16_t mod_id);
491 size_t getModCount();
492 };
493
494 // Register usermods by building a static list via a linker section
495 #define REGISTER_USERMOD(x) Usermod* const um_##x __attribute__((__section__(".dtors.tbl.usermods.1"), used)) = &x
496
497 //usermod.cpp
498 void userSetup();
499 void userConnected();
500 void userLoop();
501
502 //util.cpp
503 #ifndef WLED_DISABLE_FPP
504 #ifdef ESP8266
505 #define HW_RND_REGISTER RANDOM_REG32
506 #else // ESP32 family
507 #include "soc/wdev_reg.h"
508 #define HW_RND_REGISTER REG_READ(WDEV_RND_REG)
509 #endif
510 #endif
511 #define inoise8 perlin8 // fastled legacy alias
512 #define inoise16 perlin16 // fastled legacy alias
513 #define hex2int(a) (((a)>='0' && (a)<='9') ? (a)-'0' : ((a)>='A' && (a)<='F') ? (a)-'A'+10 : ((a)>='a' && (a)<='f') ? (a)-'a'+10 : 0)
514 [[gnu::pure]] int getNumVal(const String* req, uint16_t pos);
515 void parseNumber(const char* str, byte* val, byte minv=0, byte maxv=255);
516 #ifndef WLED_DISABLE_FPP
517 bool getVal(JsonVariant elem, byte* val, byte vmin=0, byte vmax=255); // getVal supports inc/decrementing and random ("X~Y(r|[w]~[-][Z])" form)
518 [[gnu::pure]] bool getBoolVal(const JsonVariant &elem, bool dflt);
519 #endif
520 bool updateVal(const char* req, const char* key, byte* val, byte minv=0, byte maxv=255);
521 size_t printSetFormCheckbox(Print& settingsScript, const char* key, int val);
522 size_t printSetFormValue(Print& settingsScript, const char* key, int val);
523 size_t printSetFormValue(Print& settingsScript, const char* key, const char* val);
524 size_t printSetFormIndex(Print& settingsScript, const char* key, int index);
525 size_t printSetClassElementHTML(Print& settingsScript, const char* key, const int index, const char* val);
526 void prepareHostname(char* hostname);
527 [[gnu::pure]] bool isAsterisksOnly(const char* str, byte maxLen);
528 bool requestJSONBufferLock(uint8_t moduleID=255);
529 void releaseJSONBufferLock();
530 uint8_t extractModeName(uint8_t mode, const char *src, char *dest, uint8_t maxLen);
531 uint8_t extractModeSlider(uint8_t mode, uint8_t slider, char *dest, uint8_t maxLen, uint8_t *var = nullptr);
532 int16_t extractModeDefaults(uint8_t mode, const char *segVar);
533 void checkSettingsPIN(const char *pin);
534 uint16_t crc16(const unsigned char* data_p, size_t length);
535 uint16_t beatsin88_t(accum88 beats_per_minute_88, uint16_t lowest = 0, uint16_t highest = 65535, uint32_t timebase = 0, uint16_t phase_offset = 0);
536 uint16_t beatsin16_t(accum88 beats_per_minute, uint16_t lowest = 0, uint16_t highest = 65535, uint32_t timebase = 0, uint16_t phase_offset = 0);
537 uint8_t beatsin8_t(accum88 beats_per_minute, uint8_t lowest = 0, uint8_t highest = 255, uint32_t timebase = 0, uint8_t phase_offset = 0);
538 um_data_t* simulateSound(uint8_t simulationId);
539 void enumerateLedmaps();
540 [[gnu::hot]] uint8_t get_random_wheel_index(uint8_t pos);
541 [[gnu::hot, gnu::pure]] float mapf(float x, float in_min, float in_max, float out_min, float out_max);
542 uint32_t hashInt(uint32_t s);
543 int32_t perlin1D_raw(uint32_t x, bool is16bit = false);
544 int32_t perlin2D_raw(uint32_t x, uint32_t y, bool is16bit = false);
545 int32_t perlin3D_raw(uint32_t x, uint32_t y, uint32_t z, bool is16bit = false);
546 uint16_t perlin16(uint32_t x);
547 uint16_t perlin16(uint32_t x, uint32_t y);
548 uint16_t perlin16(uint32_t x, uint32_t y, uint32_t z);
549 uint8_t perlin8(uint16_t x);
550 uint8_t perlin8(uint16_t x, uint16_t y);
551 uint8_t perlin8(uint16_t x, uint16_t y, uint16_t z);
552
553 // fast (true) random numbers using hardware RNG, all functions return values in the range lowerlimit to upperlimit-1
554 // note: for true random numbers with high entropy, do not call faster than every 200ns (5MHz)
555 // tests show it is still highly random reading it quickly in a loop (better than fastled PRNG)
556 // for 8bit and 16bit random functions: no limit check is done for best speed
557 // 32bit inputs are used for speed and code size, limits don't work if inverted or out of range
558 // inlining does save code size except for random(a,b) and 32bit random with limits
559 #ifndef WLED_DISABLE_FPP
560 #define random hw_random // replace arduino random()
561 inline uint32_t hw_random() { return HW_RND_REGISTER; };
562 uint32_t hw_random(uint32_t upperlimit); // not inlined for code size
563 int32_t hw_random(int32_t lowerlimit, int32_t upperlimit);
564 inline uint16_t hw_random16() { return HW_RND_REGISTER; };
565 inline uint16_t hw_random16(uint32_t upperlimit) { return (hw_random16() * upperlimit) >> 16; }; // input range 0-65535 (uint16_t)
566 inline int16_t hw_random16(int32_t lowerlimit, int32_t upperlimit) { int32_t range = upperlimit - lowerlimit; return lowerlimit + hw_random16(range); }; // signed limits, use int16_t ranges
567 inline uint8_t hw_random8() { return HW_RND_REGISTER; };
568 inline uint8_t hw_random8(uint32_t upperlimit) { return (hw_random8() * upperlimit) >> 8; }; // input range 0-255
569 inline uint8_t hw_random8(uint32_t lowerlimit, uint32_t upperlimit) { uint32_t range = upperlimit - lowerlimit; return lowerlimit + hw_random8(range); }; // input range 0-255
570 #else
571 1618 inline uint32_t hw_random() { return random(); }
572 uint32_t hw_random(uint32_t upperlimit); // not inlined for code size
573 int32_t hw_random(int32_t lowerlimit, int32_t upperlimit);
574 17687 inline uint16_t hw_random16() { return random16(); };
575 16133 inline uint16_t hw_random16(uint32_t upperlimit) { return (hw_random16() * upperlimit) >> 16; }; // input range 0-65535 (uint16_t)
576 2124 inline int16_t hw_random16(int32_t lowerlimit, int32_t upperlimit) { int32_t range = upperlimit - lowerlimit; return lowerlimit + hw_random16(range); }; // signed limits, use int16_t ranges
577 125402 inline uint8_t hw_random8() { return random8(); };
578 76295 inline uint8_t hw_random8(uint32_t upperlimit) { return (hw_random8() * upperlimit) >> 8; }; // input range 0-255
579 44047 inline uint8_t hw_random8(uint32_t lowerlimit, uint32_t upperlimit) { uint32_t range = upperlimit - lowerlimit; return lowerlimit + hw_random8(range); }; // input range 0-255
580 #endif
581
582 // RAII guard class for the JSON Buffer lock
583 // Modeled after std::lock_guard
584 #ifndef WLED_DISABLE_FPP
585 class JSONBufferGuard {
586 bool holding_lock;
587 public:
588 inline JSONBufferGuard(uint8_t module=255) : holding_lock(requestJSONBufferLock(module)) {};
589 inline ~JSONBufferGuard() { if (holding_lock) releaseJSONBufferLock(); };
590 inline JSONBufferGuard(const JSONBufferGuard&) = delete; // Noncopyable
591 inline JSONBufferGuard& operator=(const JSONBufferGuard&) = delete;
592 inline JSONBufferGuard(JSONBufferGuard&& r) : holding_lock(r.holding_lock) { r.holding_lock = false; }; // but movable
593 inline JSONBufferGuard& operator=(JSONBufferGuard&& r) { holding_lock |= r.holding_lock; r.holding_lock = false; return *this; };
594 inline bool owns_lock() const { return holding_lock; }
595 explicit inline operator bool() const { return owns_lock(); };
596 inline void release() { if (holding_lock) releaseJSONBufferLock(); holding_lock = false; }
597 };
598
599 #ifdef WLED_ADD_EEPROM_SUPPORT
600 //wled_eeprom.cpp
601 void applyMacro(byte index);
602 void deEEP();
603 void deEEPSettings();
604 void clearEEPROM();
605 #endif
606 #endif
607
608 //wled_math.cpp
609 //float cos_t(float phi); // use float math
610 //float sin_t(float phi);
611 //float tan_t(float x);
612 int16_t sin16_t(uint16_t theta);
613 int16_t cos16_t(uint16_t theta);
614 uint8_t sin8_t(uint8_t theta);
615 uint8_t cos8_t(uint8_t theta);
616 float sin_approx(float theta); // uses integer math (converted to float), accuracy +/-0.0015 (compared to sinf())
617 float cos_approx(float theta);
618 float tan_approx(float x);
619 float atan2_t(float y, float x);
620 float acos_t(float x);
621 float asin_t(float x);
622 template <typename T> T atan_t(T x);
623 float floor_t(float x);
624 float fmod_t(float num, float denom);
625 uint32_t sqrt32_bw(uint32_t x);
626 #define sin_t sin_approx
627 #define cos_t cos_approx
628 #define tan_t tan_approx
629
630 /*
631 #include <math.h> // standard math functions. use a lot of flash
632 #define sin_t sinf
633 #define cos_t cosf
634 #define tan_t tanf
635 #define asin_t asinf
636 #define acos_t acosf
637 #define atan_t atanf
638 #define fmod_t fmodf
639 #define floor_t floorf
640 */
641 #ifndef WLED_DISABLE_FPP
642
643 //wled_serial.cpp
644 void handleSerial();
645 void updateBaudRate(uint32_t rate);
646
647 //wled_server.cpp
648 void createEditHandler(bool enable);
649 void initServer();
650 void serveMessage(AsyncWebServerRequest* request, uint16_t code, const String& headl, const String& subl="", byte optionT=255);
651 void serveJsonError(AsyncWebServerRequest* request, uint16_t code, uint16_t error);
652 void serveSettings(AsyncWebServerRequest* request, bool post = false);
653 void serveSettingsJS(AsyncWebServerRequest* request);
654
655 //ws.cpp
656 void handleWs();
657 void wsEvent(AsyncWebSocket * server, AsyncWebSocketClient * client, AwsEventType type, void * arg, uint8_t *data, size_t len);
658 void sendDataWs(AsyncWebSocketClient * client = nullptr);
659
660 //xml.cpp
661 void XML_response(Print& dest);
662 void getSettingsJS(byte subPage, Print& dest);
663 #endif
664 #endif
665