FastLED 3.9.15
Loading...
Searching...
No Matches
fl Directory Reference
+ Directory dependency graph for fl:

Directories

 asset
 
 audio
 
 build
 
 channels
 
 chipsets
 
 codec
 
 control
 
 font
 
 fx
 
 gfx
 
 log
 
 math
 
 net
 
 remote
 
 sensors
 
 stl
 
 system
 
 task
 
 test
 
 ui
 
 video
 
 wdt
 

Files

 _build.cpp.hpp
 

Detailed Description

This document introduces the FastLED core library housed under src/fl/, first by listing its headers, then by progressively expanding into an educational guide for two audiences:

FastLED avoids direct dependencies on the C++ standard library in embedded contexts and offers its own STL‑like building blocks in the fl:: namespace.

Table of Contents


Overview and Quick Start

What is fl::?

fl:: is FastLED’s cross‑platform foundation layer. It provides containers, algorithms, memory utilities, math, graphics primitives, async/concurrency, I/O, and platform shims designed to work consistently across embedded targets and host builds. It replaces common std:: facilities with equivalents tailored for embedded constraints and portability.

Key properties:

Goals and design constraints

Naming and idioms

Getting started: common building blocks

Include the top‑level header you need, or just include FastLED.h in sketches. When writing platform‑agnostic C++ within this repo, include the specific fl/ headers you use.

Common types to reach for:

Example: using containers, views, and ownership

#include "fl/stl/vector.h"
#include "fl/stl/span.h"
#include "fl/stl/memory.h" // for fl::make_unique
void process(fl::span<const int> values) {
// Non-owning view over contiguous data
}
void example() {
data.push_back(1);
data.push_back(2);
data.push_back(3);
process(fl::span<const int>(data));
auto ptr = fl::make_unique<int>(42);
// Exclusive ownership; automatically freed when leaving scope
}
void push_back(const T &value) FL_NOEXCEPT
Definition vector.h:624
fl::enable_if<!fl::is_array< T >::value, unique_ptr< T > >::type make_unique(Args &&... args) FL_NOEXCEPT
Definition unique_ptr.h:261

Example: JSON with safe default access

#include "fl/stl/json.h"
void json_example(const fl::string& jsonStr) {
fl::json json = fl::json::parse(jsonStr);
int brightness = json["config"]["brightness"] | 128; // default if missing
bool enabled = json["enabled"] | false;
}
fl::UISlider brightness("Brightness", BRIGHTNESS, 0, 255)
static json parse(const fl::string &txt) FL_NOEXCEPT
Definition json.h:677
FastLED's Elegant JSON Library: fl::json

Header Groups (5 major areas)

1) STL‑like data structures and core utilities

Containers, views, algorithms, compile‑time utilities, memory/ownership, portability helpers.

Per‑header quick descriptions:

2) Graphics, geometry, and rendering

Rasterization, coordinate mappings, paths, grids, resampling, draw buffers, and related glue.

Per‑header quick descriptions:

3) Color, math, and signal processing

Color models, gradients, gamma, math helpers, random, noise, mapping, and basic DSP.

Per‑header quick descriptions:

4) Concurrency, async, and functional

Threads, synchronization, async primitives, eventing, and callable utilities.

Per‑header quick descriptions:

5) I/O, JSON, and text/formatting

Streams, strings, formatted output, bytestreams, filesystem, JSON.

Per‑header quick descriptions:

Comprehensive Module Breakdown

This section groups headers by domain, explains their role, and shows minimal usage snippets. Names shown are representative; see the header list above for the full inventory.

Containers and Views

Why: Embedded‑aware containers with predictable behavior across platforms. Prefer passing fl::span<T> to functions.

#include "fl/stl/vector.h"
#include "fl/stl/span.h"
size_t count_nonzero(fl::span<const uint8_t> bytes) {
size_t count = 0;
for (uint8_t b : bytes) { if (b != 0) { ++count; } }
return count;
}

Strings and Streams

Why: Consistent string/stream facilities without pulling in the standard streams.

#include "fl/string.h"
#include "fl/sstream.h"
fl::string greet(const fl::string& name) {
ss << "Hello, " << name << "!";
return ss.str();
}
string str() const FL_NOEXCEPT
Definition strstream.h:43

Memory and Ownership

Why: RAII ownership with explicit semantics. Prefer fl::make_shared<T>()/fl::make_unique<T>() patterns where available, or direct constructors provided by these headers.

#include "../fl/stl/shared_ptr.h"
struct Widget { int value; };
void ownership_example() {
fl::shared_ptr<Widget> w(new Widget{123});
auto w2 = w; // shared ownership
}

Functional Utilities

Why: Store callbacks and multicast them safely.

void on_event(int code) { /* ... */ }
void register_handlers() {
fl::function_list<void(int)> handlers;
handlers.add(on_event);
handlers(200); // invoke all
}

Concurrency and Async

Why: Lightweight foundations for parallel work or async orchestration where supported.

fl::task::promise<int> compute_async(); // returns a moveable wrapper around a future-like result
Promise-based fluent API for FastLED - standalone async primitives.

Interrupt Service Routines (ISR)

Why: Unified interrupt handling across ESP32, Teensy, AVR, STM32, and other platforms. Provides timer-based and GPIO-based interrupt attachment with consistent configuration and lifecycle management.

Key features:

Platform support:

Basic timer interrupt example:

#include "fl/stl/isr.h"
// Counter updated from ISR (use atomic or volatile for shared state)
static volatile uint32_t tick_count = 0;
// ISR handler - must be fast and IRAM-safe on ESP32
static void my_timer_isr(void* user_data) {
tick_count++;
}
void setup() {
// Configure a 1 kHz (1000 Hz) timer interrupt
config.handler = my_timer_isr;
config.user_data = nullptr;
config.frequency_hz = 1000; // 1 kHz
config.flags = fl::isr::ISR_FLAG_IRAM_SAFE; // Required for ESP32
int result = fl::isr::attach_timer_handler(config, &handle);
if (result == 0) {
// ISR is now active
FL_DBG("Timer ISR attached successfully");
} else {
FL_WARN("Failed to attach ISR: " << fl::isr::get_error_string(result));
}
}
void setup()
Umbrella header for ISR subsystem.
#define FL_WARN(X)
Definition log.h:276
#define FL_DBG
Definition log.h:388
constexpr u32 ISR_FLAG_IRAM_SAFE
Definition constants.h:21
int attach_timer_handler(const config &cfg, handle *out_handle)
Attach a timer-based ISR handler.
const char * get_error_string(int error_code)
Get platform-specific error description.
constexpr u8 ISR_PRIORITY_MEDIUM
Definition constants.h:15
handler_fn handler
Definition handler.h:20
void * user_data
Definition handler.h:21
Configuration for ISR attachment.
Definition handler.h:19
Opaque handle to an attached ISR.
Definition handler.h:36

Advanced example with user data and enable/disable:

#include "fl/stl/isr.h"
struct ISRContext {
volatile uint32_t count;
volatile uint32_t max_count;
};
static void counting_isr(void* user_data) {
ISRContext* ctx = static_cast<ISRContext*>(user_data);
if (ctx->count < ctx->max_count) {
ctx->count++;
}
}
void example_with_control() {
ISRContext ctx = { 0, 1000 };
// Configure 10 kHz timer with user context
config.handler = counting_isr;
config.user_data = &ctx;
config.frequency_hz = 10000; // 10 kHz
if (fl::isr::attach_timer_handler(config, &handle) == 0) {
// Run for a while
delay(100);
// Temporarily disable ISR
FL_DBG("ISR disabled, count: " << ctx.count);
delay(100);
// Re-enable ISR
FL_DBG("ISR re-enabled");
delay(100);
// Clean up when done
FL_DBG("Final count: " << ctx.count);
}
}
constexpr u8 ISR_PRIORITY_HIGH
Definition constants.h:16
int detach_handler(handle &h)
Detach an ISR handler.
int disable_handler(handle &h)
Disable an ISR temporarily (without detaching).
int enable_handler(handle &h)
Enable an ISR (after temporary disable).
void delay(u32 ms, bool run_async=true) FL_NOEXCEPT
Public delay wrapper that keeps bare Arduino delay() preferred after using fl::delay; while still all...
Definition delay.h:98

GPIO interrupt example:

#include "fl/stl/isr.h"
static void button_isr(void* user_data) {
// Handle button press (keep this fast!)
FL_DBG("Button pressed!");
}
void setup_gpio_interrupt() {
config.handler = button_isr;
config.user_data = nullptr;
config.flags = fl::isr::ISR_FLAG_EDGE_RISING; // Trigger on rising edge
uint8_t button_pin = 2; // GPIO pin number
if (fl::isr::attach_external_handler(button_pin, config, &handle) == 0) {
FL_DBG("Button ISR attached to pin " << button_pin);
}
}
constexpr u32 ISR_FLAG_EDGE_RISING
Definition constants.h:22
int attach_external_handler(u8 pin, const config &cfg, handle *out_handle)
Attach an external interrupt handler (GPIO-based).

Platform capability queries:

#include "fl/stl/isr.h"
void print_platform_info() {
FL_DBG("Platform: " << fl::isr::get_platform_name());
FL_DBG("Max timer frequency: " << fl::isr::get_max_timer_frequency() << " Hz");
FL_DBG("Min timer frequency: " << fl::isr::get_min_timer_frequency() << " Hz");
FL_DBG("Max priority: " << fl::isr::get_max_priority());
FL_DBG("Maximum priority requires assembly handler");
}
}
u32 get_max_timer_frequency()
Get the maximum timer frequency supported by this platform.
constexpr u8 ISR_PRIORITY_MAX
Definition constants.h:18
u32 get_min_timer_frequency()
Get the minimum timer frequency supported by this platform.
u8 get_max_priority()
Get the maximum priority level supported by this platform.
const char * get_platform_name()
Get the platform name.
bool requires_assembly_handler(u8 priority)
Check if assembly is required for a given priority level.

Important ISR handler requirements:

Priority guidelines:

See src/fl/isr.h for complete API documentation and platform-specific configuration flags.

Category-Based Logging

What it is: A compile-time selective logging system that enables detailed diagnostics for specific subsystems (SPI, RMT, I2S, GPIO, PIO, DMA, TIMER, and many more) with zero overhead when disabled. Unlike FL_DBG (general-purpose), category logging provides structured, category-specific diagnostics by enabling only the categories you need.

Key features:

Available categories:

Hardware interfaces:

Comparison with alternatives:

Feature FL_DBG FL_WARN FL_LOG_*
Purpose General debug Critical warnings Category diagnostics
Selective enable No (all-or-nothing) No Yes (per-category)
Default state Memory-dependent Always enabled Always disabled
Memory ~5KB Variable 0 bytes
Control mechanism Compile-time N/A Compile-time only

How to enable logging:

Add to platformio.ini or Arduino IDE build flags:

# Enable SPI logging code
build_flags = -DFASTLED_LOG_SPI_ENABLED
# Enable multiple categories
build_flags =
-DFASTLED_LOG_SPI_ENABLED
-DFASTLED_LOG_I2S_ENABLED
-DFASTLED_LOG_TIMER_ENABLED
# Enable multiple categories (alternative syntax)
build_flags =
-DFASTLED_LOG_SPI_ENABLED
-DFASTLED_LOG_RMT_ENABLED
-DFASTLED_LOG_VIDEO_ENABLED

Logging in your code:

#include "fl/log/log.h"
void myCustomDriver::initialize(uint32_t freq_hz) {
FL_LOG_I2S("I2S initialized");
FL_LOG_I2S(" Frequency: " << freq_hz << " Hz");
FL_LOG_I2S(" Buffer size: " << mBufferSize << " bytes");
}
void myCustomDriver::transmit(fl::span<const uint8_t> data) {
FL_LOG_I2S("Transmitting " << data.size() << " bytes");
}
constexpr fl::size size() const FL_NOEXCEPT
Definition span.h:458
Centralized logging categories for FastLED hardware interfaces and subsystems.

Implementation details:

Math, Random, and DSP

Why: Efficient numeric operations for LED effects, audio reactivity, and transforms.

#include "fl/gamma.h"
uint8_t apply_gamma(uint8_t v) {
return fl::gamma::correct8(v);
}

Geometry and Grids

Why: Building blocks for 2D/3D layouts and path operations.

Graphics, Rasterization, and Resampling

Why: Efficient frame manipulation for LED matrices and coordinate spaces.

#include "fl/downscale.h"
// Downscale a high‑res buffer to a target raster (API varies by adapter)

Graphics Deep Dive

This section explains how the major graphics utilities fit together and how to use them effectively for high‑quality, high‑performance rendering on LED strips, matrices, and complex shapes.

JSON Utilities

Why: Ergonomic, crash‑resistant access with defaulting operator (|). Prefer the fl::json API for new code.

fl::json cfg = fl::json::parse("{\"enabled\":true}");
bool enabled = cfg["enabled"] | false;

I/O and Filesystem

Why: Cross‑platform text formatting, buffered I/O, and optional file access on host builds.

Algorithms and Utilities

Why: Familiar patterns with embedded‑appropriate implementations and compiler‑portable controls.

Audio and Reactive Systems

UI System (JSON UI)

FastLED includes a JSON‑driven UI layer that can expose controls (sliders, buttons, checkboxes, number fields, dropdowns, titles, descriptions, help, audio visualizers) for interactive demos and remote control.

Why: Build effects that respond to input signals.

Miscellaneous and Specialized


Guidance for New Users

Guidance for C++ Developers


This README will evolve alongside the codebase. If you are exploring a specific subsystem, start from the relevant headers listed above and follow includes to supporting modules.