FastLED 3.9.15
Loading...
Searching...
No Matches
sketch_macros.h
Go to the documentation of this file.
1#pragma once
2
3// Pull in platform detection BEFORE the tier classification fires. Without
4// this, FL_IS_ARM_LPC / FL_IS_TEENSY_LC / FL_IS_STM32_F1 etc. -- the gates
5// in the Low-tier branch below -- are undefined when sketch_macros.h is
6// transitively included via fl/system/engine_events.h before any other
7// platform-aware include. Result: LPC builds get FL_PLATFORM_HAS_LARGE_MEMORY=1
8// (wrong) and pull in FASTLED_HAS_ENGINE_EVENTS code that lacks symbol
9// definitions -- link fails. Pulling is_platform.h here makes the detection
10// canonical regardless of include order.
11#include "platforms/is_platform.h"
12
13// Four-tier platform-memory classification (FastLED #3000).
14//
15// Canonical names:
16// FL_PLATFORM_HAS_TINY_MEMORY — 1 on parts with <=1KB SRAM
17// (classic ATtiny <=512B, ATtiny1604 1KB,
18// select modern tinyAVR 0/1-series with
19// <=1KB SRAM — see FL_IS_AVR_ATTINY_TINY_MEMORY
20// in is_avr.h; 2-series parts with 2KB+ are
21// excluded)
22// FL_PLATFORM_HAS_LARGE_MEMORY — 1 on the "high" tier (Apollo3, nRF52,
23// SAMD21, generic ARM)
24// FL_PLATFORM_HAS_HUGE_MEMORY — 1 on the "huge" tier (ESP32, Teensy
25// 3.5/3.6/4.x, RP2040/RP2350, SAMD51,
26// STM32F4+/H7, native/WASM)
27//
28// Tier truth table:
29// Tiny: TINY=1, LARGE=0, HUGE=0 (<=1KB SRAM parts)
30// Low: TINY=0, LARGE=0, HUGE=0 (AVR Uno/Nano/Leonardo, ATtiny 2-3KB,
31// ESP8266, Teensy LC/3.0/3.1/3.2, STM32F1,
32// Renesas UNO, LPC8xx)
33// High: TINY=0, LARGE=1, HUGE=0
34// Huge: TINY=0, LARGE=1, HUGE=1
35//
36// Invariants:
37// HUGE=1 implies LARGE=1. LARGE=0 implies HUGE=0.
38// TINY=1 implies LARGE=0 and HUGE=0.
39//
40// Backward compatibility (#3000):
41// The names `SKETCH_HAS_TINY_MEMORY`, `SKETCH_HAS_LARGE_MEMORY`,
42// `SKETCH_HAS_HUGE_MEMORY` (and `_OVERRIDDEN` variants, plus the older
43// `SKETCH_HAS_LOTS_OF_MEMORY` / `SKETCH_HAS_VERY_LARGE_MEMORY` aliases)
44// are preserved as deprecated aliases so external user sketches keep
45// compiling. Internal FastLED code is migrating to the
46// `FL_PLATFORM_HAS_*` names over a series of follow-up PRs; both names
47// work identically until that migration completes. Either name can be
48// used as a build-flag override (e.g. `-DSKETCH_HAS_LARGE_MEMORY=1` or
49// `-DFL_PLATFORM_HAS_LARGE_MEMORY=1`).
50//
51// Override mechanism: define the macro to 0 or 1 in build flags or
52// before including FastLED. The header detects the override and sets
53// the corresponding `_OVERRIDDEN` flag, which `platforms/*/compile_test.hpp`
54// uses to skip the platform-default sanity assertion.
55
56// =============================================================================
57// Tier 1: tiny (<=1KB SRAM)
58// =============================================================================
59//
60// Detect BEFORE computing LARGE/HUGE so the invariant TINY=1 → LARGE=0,
61// HUGE=0 holds for the platform-default paths.
62//
63// `FL_IS_AVR_ATTINY_TINY_MEMORY` is defined in `platforms/avr/is_avr.h`
64// (included via `platforms/is_platform.h` upstream in FastLED.h).
65#if defined(FL_PLATFORM_HAS_TINY_MEMORY) || defined(SKETCH_HAS_TINY_MEMORY)
66 // User-supplied override. Accept either spelling.
67 #ifndef FL_PLATFORM_HAS_TINY_MEMORY
68 #define FL_PLATFORM_HAS_TINY_MEMORY SKETCH_HAS_TINY_MEMORY
69 #endif
70 #define FL_PLATFORM_HAS_TINY_MEMORY_OVERRIDDEN 1
71#else
72 #if defined(FL_IS_AVR_ATTINY_TINY_MEMORY)
73 #define FL_PLATFORM_HAS_TINY_MEMORY 1
74 #else
75 #define FL_PLATFORM_HAS_TINY_MEMORY 0
76 #endif
77#endif
78
79// =============================================================================
80// Tier 3: large / high (>= 256KB flash, "high" tier)
81// =============================================================================
82#if defined(FL_PLATFORM_HAS_LARGE_MEMORY) || defined(SKETCH_HAS_LARGE_MEMORY)
83 // User-supplied override. Accept either spelling.
84 #ifndef FL_PLATFORM_HAS_LARGE_MEMORY
85 #define FL_PLATFORM_HAS_LARGE_MEMORY SKETCH_HAS_LARGE_MEMORY
86 #endif
87 #define FL_PLATFORM_HAS_LARGE_MEMORY_OVERRIDDEN 1
88#else
89 #if defined(FL_IS_AVR) \
90 || defined(__AVR_ATtiny85__) \
91 || defined(__AVR_ATtiny88__) \
92 || defined(__AVR_ATmega32U4__) \
93 || defined(ARDUINO_attinyxy6) \
94 || defined(ARDUINO_attinyxy4) \
95 || defined(FL_IS_TEENSY_LC) \
96 || defined(FL_IS_TEENSY_30) \
97 || defined(FL_IS_TEENSY_31) \
98 || defined(FL_IS_TEENSY_32) \
99 || defined(FL_IS_STM32_F1) \
100 || defined(FL_IS_ESP8266) \
101 || defined(ARDUINO_ARCH_RENESAS_UNO) \
102 || defined(ARDUINO_BLUEPILL_F103C8) \
103 || defined(FL_IS_ARM_LPC)
104 #define FL_PLATFORM_HAS_LARGE_MEMORY 0
105 #else
106 #define FL_PLATFORM_HAS_LARGE_MEMORY 1
107 #endif
108#endif
109
110// =============================================================================
111// Tier 4: huge (>= 256KB RAM, "huge" tier)
112// =============================================================================
113//
114// ESP32 (all), Teensy 4.x/3.5/3.6, RP2040/RP2350, SAMD51, STM32F4+/H7,
115// native/WASM.
116#if defined(FL_PLATFORM_HAS_HUGE_MEMORY) || defined(SKETCH_HAS_HUGE_MEMORY)
117 // User-supplied override. Accept either spelling.
118 #ifndef FL_PLATFORM_HAS_HUGE_MEMORY
119 #define FL_PLATFORM_HAS_HUGE_MEMORY SKETCH_HAS_HUGE_MEMORY
120 #endif
121 #define FL_PLATFORM_HAS_HUGE_MEMORY_OVERRIDDEN 1
122#else
123 #if defined(FL_IS_ESP32) \
124 || defined(FL_IS_TEENSY_35) \
125 || defined(FL_IS_TEENSY_36) \
126 || defined(FL_IS_TEENSY_4X) \
127 || defined(ARDUINO_ARCH_RP2040) \
128 || defined(PICO_RP2040) \
129 || defined(PICO_RP2350) \
130 || defined(__SAMD51__) \
131 || defined(STM32F4xx) || defined(STM32H7xx) || defined(ARDUINO_GIGA) \
132 || defined(FASTLED_STUB_IMPL) \
133 || defined(__EMSCRIPTEN__)
134 #define FL_PLATFORM_HAS_HUGE_MEMORY 1
135 #else
136 #define FL_PLATFORM_HAS_HUGE_MEMORY 0
137 #endif
138#endif
139
140// =============================================================================
141// Invariant enforcement
142// =============================================================================
143#if FL_PLATFORM_HAS_HUGE_MEMORY && !FL_PLATFORM_HAS_LARGE_MEMORY
144 #error "FL_PLATFORM_HAS_HUGE_MEMORY=1 requires FL_PLATFORM_HAS_LARGE_MEMORY=1"
145#endif
146#if FL_PLATFORM_HAS_TINY_MEMORY && FL_PLATFORM_HAS_LARGE_MEMORY
147 #error "FL_PLATFORM_HAS_TINY_MEMORY=1 is incompatible with FL_PLATFORM_HAS_LARGE_MEMORY=1"
148#endif
149
150// =============================================================================
151// Backward-compat aliases (FastLED #3000)
152// =============================================================================
153//
154// The legacy `SKETCH_HAS_*_MEMORY` names mirror the canonical
155// `FL_PLATFORM_HAS_*_MEMORY` defines verbatim. Both names will continue
156// to evaluate identically; new code should prefer the `FL_PLATFORM_HAS_*`
157// names. The `_OVERRIDDEN` variants are also mirrored for parity with
158// the platform `compile_test.hpp` files that key off them.
159//
160// `#ifndef` guards protect the case where the user already supplied
161// `SKETCH_HAS_*` (which we picked up above as an override) — preventing
162// a redefinition warning.
163#ifndef SKETCH_HAS_TINY_MEMORY
164 #define SKETCH_HAS_TINY_MEMORY FL_PLATFORM_HAS_TINY_MEMORY
165#endif
166#ifndef SKETCH_HAS_LARGE_MEMORY
167 #define SKETCH_HAS_LARGE_MEMORY FL_PLATFORM_HAS_LARGE_MEMORY
168#endif
169#ifndef SKETCH_HAS_HUGE_MEMORY
170 #define SKETCH_HAS_HUGE_MEMORY FL_PLATFORM_HAS_HUGE_MEMORY
171#endif
172
173#ifdef FL_PLATFORM_HAS_TINY_MEMORY_OVERRIDDEN
174 #ifndef SKETCH_HAS_TINY_MEMORY_OVERRIDDEN
175 #define SKETCH_HAS_TINY_MEMORY_OVERRIDDEN 1
176 #endif
177#endif
178#ifdef FL_PLATFORM_HAS_LARGE_MEMORY_OVERRIDDEN
179 #ifndef SKETCH_HAS_LARGE_MEMORY_OVERRIDDEN
180 #define SKETCH_HAS_LARGE_MEMORY_OVERRIDDEN 1
181 #endif
182#endif
183#ifdef FL_PLATFORM_HAS_HUGE_MEMORY_OVERRIDDEN
184 #ifndef SKETCH_HAS_HUGE_MEMORY_OVERRIDDEN
185 #define SKETCH_HAS_HUGE_MEMORY_OVERRIDDEN 1
186 #endif
187#endif
188
189// Older spellings still seen in some user sketches.
190#ifndef SKETCH_HAS_LOTS_OF_MEMORY
191 #define SKETCH_HAS_LOTS_OF_MEMORY FL_PLATFORM_HAS_LARGE_MEMORY
192#endif
193#ifndef SKETCH_HAS_VERY_LARGE_MEMORY
194 #define SKETCH_HAS_VERY_LARGE_MEMORY FL_PLATFORM_HAS_HUGE_MEMORY
195#endif
196
197// =============================================================================
198// Misc utilities (kept as-is)
199// =============================================================================
200#ifndef SKETCH_STRINGIFY
201#define SKETCH_STRINGIFY_HELPER(x) #x
202#define SKETCH_STRINGIFY(x) SKETCH_STRINGIFY_HELPER(x)
203#endif
204
205// SKETCH_HALT and SKETCH_HALT_OK macros have been removed.
206// They caused watchdog timer resets on ESP32-C6 due to the infinite while(1) loop
207// preventing loop() from returning.
208//
209// Replacement: Use a static flag to run tests once:
210//
211// Pattern:
212// static bool tests_run = false;
213//
214// void loop() {
215// if (tests_run) {
216// delay(1000);
217// return;
218// }
219// tests_run = true;
220//
221// // ... your test code here ...
222// FL_PRINT("Tests complete");
223// }
224//
225// This allows loop() to return normally, preventing watchdog timer resets.