# FastLED Project Rules for Cursor ## Cursor Configuration ### Post-Change Hooks Run linting after every code change: ```yaml post_change_hooks: - command: "bash lint" description: "Run code formatting and linting" working_directory: "." ``` ## MCP Server Configuration This project includes a custom MCP server (`mcp_server.py`) that provides tools for: - Running tests with various options - Compiling examples for different platforms - Code fingerprinting and change detection - Linting and formatting - Project information and status - **Build info analysis for platform-specific defines, compiler flags, and toolchain information** - **Symbol analysis for binary optimization (all platforms)** - Stack trace setup for enhanced debugging - **๐ŸŒ FastLED Web Compiler with Playwright (FOREGROUND AGENTS ONLY)** - **๐Ÿšจ CRITICAL: `validate_completion` tool for background agents** To use the MCP server, run: `uv run mcp_server.py` **BACKGROUND AGENTS:** The MCP server includes a mandatory `validate_completion` tool that MUST be used before indicating task completion. This tool runs `bash test` and ensures all tests pass. ### FastLED Web Compiler (FOREGROUND AGENTS ONLY) **๐ŸŒ FOR INTERACTIVE DEVELOPMENT:** The MCP server includes a `run_fastled_web_compiler` tool that: **Note:** For direct command-line WASM compilation, see the **WASM Sketch Compilation** section below. - **Compiles Arduino sketches to WASM** for browser execution - **Captures console.log output** with playwright automation - **Takes screenshots** of running visualizations - **Monitors FastLED_onFrame calls** to verify proper initialization - **Provides detailed analysis** of errors and performance **PREREQUISITES:** - `pip install fastled` - FastLED web compiler - `pip install playwright` - Browser automation (included in pyproject.toml) - Docker (optional, for faster compilation) **USAGE EXAMPLES:** ```python # Via MCP Server - Basic usage use run_fastled_web_compiler tool with: - example_path: "examples/Audio" - capture_duration: 30 - headless: false - save_screenshot: true # Via MCP Server - Different examples use run_fastled_web_compiler tool with: - example_path: "examples/Blink" - capture_duration: 15 - headless: true # Via MCP Server - Quick test use run_fastled_web_compiler tool with: - example_path: "examples/wasm" - capture_duration: 10 ``` **KEY FEATURES:** - **Automatic browser installation:** Installs Chromium via playwright - **Console.log capture:** Records all browser console output with timestamps - **Error detection:** Identifies compilation failures and runtime errors - **FastLED monitoring:** Tracks `FastLED_onFrame` calls to verify functionality - **Screenshot capture:** Saves visualization images with timestamps - **Docker detection:** Checks for Docker availability for faster builds - **Background agent protection:** Automatically disabled for CI/background environments **๐Ÿšซ BACKGROUND AGENT RESTRICTION:** This tool is **COMPLETELY DISABLED** for background agents and CI environments. Background agents attempting to use this tool will receive an error message. This is intentional to prevent: - Hanging processes in automated environments - Resource conflicts in CI/CD pipelines - Interactive browser windows in headless environments **CONSOLE.LOG CAPTURE PATTERN:** The tool follows the pattern established in `ci/wasm_test.py` and `ci/ci/scrapers/`: ```javascript // Example captured console.log patterns: [14:25:30] log: FastLED_onFrame called: {"frame":1,"leds":100} [14:25:30] log: Audio worklet initialized [14:25:31] error: Missing audio_worklet_processor.js [14:25:31] warning: WebGL context lost ``` **INTEGRATION WITH EXISTING CI:** - Complements existing `ci/wasm_test.py` functionality - Uses same playwright patterns as `ci/ci/scrapers/` - Leverages existing pyproject.toml dependencies - Compatible with existing Docker-based compilation workflow ## Project Structure - `src/` - Main FastLED library source code - `examples/` - Arduino examples demonstrating FastLED usage - `tests/` - Test files and infrastructure - `ci/` - Continuous integration scripts - `docs/` - Documentation ## Key Commands - `uv run test.py` - Run all tests - `uv run test.py --cpp` - Run C++ tests only - `uv run test.py TestName` - Run specific C++ test - For example: running test_xypath.cpp would be uv run test.py xypath - `./lint` - Run code formatting/linting - `uv run ci/ci-compile.py uno --examples Blink` - Compile examples for specific platform - For example (uno): `uv run ci/ci-compile.py uno --examples Blink` - For example (esp32dev): `uv run ci/ci-compile.py esp32dev --examples Blink` - For example (esp8266): `uv run ci/ci-compile.py esp01 --examples Blink` - For example (teensy31): `uv run ci/ci-compile.py teensy31 --examples Blink` - **WASM Compilation** - Compile Arduino sketches to run in web browsers: - `uv run ci/wasm_compile.py examples/Blink -b --open` - Compile Blink to WASM and open browser - `uv run ci/wasm_compile.py path/to/your/sketch -b --open` - Compile any sketch to WASM - **Symbol Analysis** - Analyze binary size and optimization opportunities: - `uv run ci/ci/symbol_analysis.py --board uno` - Analyze UNO platform - `uv run ci/ci/symbol_analysis.py --board esp32dev` - Analyze ESP32 platform - `uv run ci/demo_symbol_analysis.py` - Analyze all available platforms ## ๐Ÿค– AI AGENT LINTING GUIDELINES ### FOREGROUND AGENTS (Interactive Development) **๐Ÿšจ ALWAYS USE `bash lint` - DO NOT RUN INDIVIDUAL LINTING SCRIPTS** - **โœ… CORRECT:** `bash lint` - **โŒ FORBIDDEN:** `./lint-js`, `./check-js`, `python3 scripts/enhance-js-typing.py` - **โŒ FORBIDDEN:** `uv run ruff check`, `uv run black`, individual tools **WHY:** `bash lint` provides: - **Comprehensive coverage** - Python, C++, JavaScript, and enhancement analysis - **Consistent workflow** - Single command for all linting needs - **Proper sequencing** - Runs tools in the correct order with dependencies - **Clear output** - Organized sections showing what's being checked - **Agent guidance** - Shows proper usage for AI agents ### BACKGROUND AGENTS (Automated/CI Environments) **CAN USE FINE-GRAINED LINTING FOR SPECIFIC NEEDS** Background agents may use individual linting scripts when needed: - `./lint-js` - JavaScript-only linting - `./check-js` - JavaScript type checking - `python3 scripts/enhance-js-typing.py` - JavaScript enhancement analysis - `uv run ruff check` - Python linting only - MCP server `lint_code` tool - Programmatic access **BUT STILL PREFER `bash lint` FOR COMPREHENSIVE CHECKING** ### Linting Script Integration The `bash lint` command now includes: 1. **๐Ÿ“ Python Linting** - ruff, black, isort, pyright 2. **๐Ÿ”ง C++ Linting** - clang-format (when enabled) 3. **๐ŸŒ JavaScript Linting** - Deno lint, format check, type checking 4. **๐Ÿ” JavaScript Enhancement** - Analysis and recommendations 5. **๐Ÿ’ก AI Agent Guidance** - Clear instructions for proper usage ### When to Use Individual Scripts **FOREGROUND AGENTS:** Never. Always use `bash lint`. **BACKGROUND AGENTS:** Only when: - **Debugging specific issues** with one language/tool - **Testing incremental changes** to linting configuration - **Running targeted analysis** for specific files - **Integrating with automated workflows** via MCP server ## Development Guidelines - Follow existing code style and patterns - Run tests before committing changes - Use the MCP server tools for common tasks - Check examples when making API changes ## ๐Ÿšจ CRITICAL: .INO FILE CREATION RULES ๐Ÿšจ ### โš ๏ธ THINK BEFORE CREATING .INO FILES โš ๏ธ **.ino files should be created SPARINGLY and ONLY when truly justified.** ### ๐Ÿšซ WHEN NOT TO CREATE .INO FILES: - **Testing minor code changes** - Use existing test files or unit tests - **Quick API validation** - Use unit tests or modify existing examples - **Debugging specific functions** - Use test files, not new sketches - **One-off experiments** - Create temporary test files instead - **Small feature tests** - Extend existing relevant examples ### โœ… WHEN TO CREATE .INO FILES: #### 1. **Temporary Testing (.ino)** **Use Pattern:** `temp_.ino` or `test_.ino` ```cpp // temp_json_api.ino - Testing new JSON fetch functionality // test_networking.ino - Validating network stack changes ``` - โœ… **FOR:** Testing new APIs during development - โœ… **FOR:** Quick prototyping and validation - โœ… **DELETE AFTER USE** - These are temporary by design #### 2. **Significant New Feature Examples** **Use Pattern:** `examples//.ino` ```cpp // examples/JsonFetchApi/JsonFetchApi.ino - Comprehensive JSON API example // examples/NetworkStack/NetworkStack.ino - Major networking features ``` - โœ… **FOR:** Large, comprehensive new features - โœ… **FOR:** APIs that warrant dedicated examples - โœ… **FOR:** Features that users will commonly implement - โœ… **PERMANENT** - These become part of the example library ### ๐Ÿ“‹ CREATION CHECKLIST: **Before creating ANY .ino file, ask:** 1. **๐Ÿค” Is this testing a new API?** - YES โ†’ Create `temp_.ino`, delete after testing - NO โ†’ Consider alternatives 2. **๐Ÿค” Is this a significant new feature that users will commonly use?** - YES โ†’ Create `examples//.ino` - NO โ†’ Use existing examples or test files 3. **๐Ÿค” Can I modify an existing example instead?** - YES โ†’ Extend existing example rather than creating new - NO โ†’ Proceed with creation 4. **๐Ÿค” Is this just for debugging/validation?** - YES โ†’ Use unit tests or temporary test files - NO โ†’ Consider if it meets the "significant feature" criteria ### ๐Ÿ” REVIEW CRITERIA: **For Feature Examples (.ino files that stay):** - โœ… **Demonstrates complete, real-world usage patterns** - โœ… **Covers multiple aspects of the feature comprehensively** - โœ… **Provides educational value for users** - โœ… **Shows best practices and common use cases** - โœ… **Is likely to be referenced by multiple users** **For Temporary Testing (.ino files that get deleted):** - โœ… **Clearly named as temporary (temp_*, test_*)** - โœ… **Focused on specific API validation** - โœ… **Will be deleted after development cycle** - โœ… **Too complex for unit test framework** ### โŒ EXAMPLES OF WHAT NOT TO CREATE: - `test_basic_led.ino` - Use existing Blink example - `debug_colors.ino` - Use existing ColorPalette example - `quick_brightness.ino` - Use unit tests or modify existing example - `validate_pins.ino` - Use PinTest example or unit tests ### โœ… EXAMPLES OF JUSTIFIED CREATIONS: - `temp_new_wifi_api.ino` - Testing major new WiFi functionality (temporary) - `examples/MachineLearning/MachineLearning.ino` - New ML integration feature (permanent) - `temp_performance_test.ino` - Validating optimization changes (temporary) ### ๐Ÿงน CLEANUP RESPONSIBILITY: - **Temporary files:** Creator must delete when testing is complete - **Feature examples:** Must be maintained and updated as API evolves - **Abandoned files:** Regular cleanup reviews to remove unused examples **Remember: The examples directory is user-facing documentation. Every .ino file should provide clear value to FastLED users.** ### Memory Management with Smart Pointers and Moveable Objects **๐Ÿšจ CRITICAL: Always use proper RAII patterns - smart pointers, moveable objects, or wrapper classes instead of raw pointers for resource management.** **Resource Management Options:** - โœ… **PREFERRED**: `fl::shared_ptr` for shared ownership (multiple references to same object) - โœ… **PREFERRED**: `fl::unique_ptr` for exclusive ownership (single owner, automatic cleanup) - โœ… **PREFERRED**: Moveable wrapper objects (like `fl::promise`) for safe copying and transferring of unique resources - โœ… **ACCEPTABLE**: `fl::vector` storing objects by value when objects support move/copy semantics - โŒ **AVOID**: `fl::vector` storing raw pointers - use `fl::vector>` or `fl::vector>` - โŒ **AVOID**: Manual `new`/`delete` - use `fl::make_shared()` or `fl::make_unique()` **Moveable Wrapper Pattern:** When you have a unique resource (like a future, file handle, or network connection) that needs to be passed around easily, create a moveable wrapper class that: - Internally manages the unique resource (often with `fl::unique_ptr` or similar) - Provides copy semantics through shared implementation details - Maintains clear ownership semantics - Allows safe transfer between contexts **Examples:** ```cpp // โœ… GOOD - Using smart pointers fl::vector> mActiveClients; auto client = fl::make_shared(); mActiveClients.push_back(client); // โœ… GOOD - Using unique_ptr for exclusive ownership fl::unique_ptr client = fl::make_unique(); // โœ… GOOD - Moveable wrapper objects (fl::promise example) fl::vector> mActivePromises; // Copyable wrapper around unique future fl::promise promise = fetch.get(url).execute(); mActivePromises.push_back(promise); // Safe copy, shared internal state // โœ… GOOD - Objects stored by value (if copyable/moveable) fl::vector mRequests; // When Request supports copy/move // โŒ BAD - Raw pointers require manual memory management fl::vector mActivePromises; // Memory leaks possible Promise* promise = new Promise(); // Who calls delete? ``` **fl::promise as Moveable Wrapper Example:** ```cpp // fl::promise wraps a unique fl::future but provides copyable semantics class promise { fl::shared_ptr> mImpl; // Shared wrapper around unique resource public: promise(const promise& other) : mImpl(other.mImpl) {} // Safe copying promise(promise&& other) : mImpl(fl::move(other.mImpl)) {} // Move support // ... wrapper delegates to internal future }; // Usage - can be copied and passed around safely fl::promise promise = http_get_promise("https://api.example.com"); someContainer.push_back(promise); // Copy is safe processAsync(promise); // Can pass to multiple places ``` **Why This Pattern:** - **Automatic cleanup** - No memory leaks from forgotten `delete` calls - **Exception safety** - Resources automatically freed even if exceptions occur - **Clear ownership** - Code clearly shows who owns what objects - **Thread safety** - Smart pointers provide atomic reference counting - **Easy sharing** - Moveable wrappers allow safe copying of unique resources - **API flexibility** - Can pass resources between different contexts safely ## ๐Ÿ”ง CMAKE BUILD SYSTEM ARCHITECTURE (DEPRECATED - NO LONGER USED) **โš ๏ธ IMPORTANT: CMake build system is no longer used. FastLED now uses a Python-based build system.** ### Build System Overview (Historical Reference) FastLED previously used a sophisticated CMake build system located in `tests/cmake/` with modular configuration: **Core Build Files:** - `tests/CMakeLists.txt` - Main CMake entry point - `tests/cmake/` - Modular CMake configuration directory **Key CMake Modules:** - `LinkerCompatibility.cmake` - **๐Ÿšจ CRITICAL for linker issues** - GNUโ†”MSVC flag translation, lld-link compatibility, warning suppression - `CompilerDetection.cmake` - Compiler identification and toolchain setup - `CompilerFlags.cmake` - Compiler-specific flag configuration - `DebugSettings.cmake` - Debug symbol and optimization configuration - `OptimizationSettings.cmake` - LTO and performance optimization settings - `ParallelBuild.cmake` - Parallel compilation and linker selection (mold, lld) - `TestConfiguration.cmake` - Test target setup and configuration - `BuildOptions.cmake` - Build option definitions and defaults ### Linker Configuration (Most Important for Build Issues) **๐ŸŽฏ PRIMARY LOCATION for linker problems: `tests/cmake/LinkerCompatibility.cmake`** **Key Functions:** - `apply_linker_compatibility()` - **Main entry point** - auto-detects lld-link and applies compatibility - `translate_gnu_to_msvc_linker_flags()` - Converts GNU-style flags to MSVC-style for lld-link - `get_dead_code_elimination_flags()` - Platform-specific dead code elimination - `get_debug_flags()` - Debug information configuration - `get_optimization_flags()` - Performance optimization flags **Linker Detection Logic:** ```cmake if(WIN32 AND CMAKE_CXX_COMPILER_ID MATCHES "Clang") find_program(LLDLINK_EXECUTABLE lld-link) if(LLDLINK_EXECUTABLE) # Apply lld-link compatibility automatically endif() endif() ``` **Common Linker Issues & Solutions:** - **lld-link warnings**: Suppressed via `/ignore:4099` in `apply_linker_compatibility()` - **GNUโ†’MSVC flag translation**: Automatic in `translate_gnu_to_msvc_linker_flags()` - **Dead code elimination**: Platform-specific via `get_dead_code_elimination_flags()` - **Debug symbol conflicts**: Handled in `get_debug_flags()` ### Compiler Configuration **Compiler Detection**: `tests/cmake/CompilerDetection.cmake` - Auto-detects Clang, GCC, MSVC - Sets up toolchain-specific configurations - Handles cross-compilation scenarios **Compiler Flags**: `tests/cmake/CompilerFlags.cmake` - Warning configurations per compiler - Optimization level management - Platform-specific adjustments ### Build Options & Configuration **Available Build Options** (defined in `BuildOptions.cmake`): - `FASTLED_DEBUG_LEVEL` - Debug information level (NONE, MINIMAL, STANDARD, FULL) - `FASTLED_OPTIMIZATION_LEVEL` - Optimization (O0, O1, O2, O3, Os, Ofast) - `FASTLED_ENABLE_LTO` - Link-time optimization - `FASTLED_ENABLE_PARALLEL_BUILD` - Parallel compilation - `FASTLED_STATIC_RUNTIME` - Static runtime linking ### Testing Infrastructure **Test Configuration**: `tests/cmake/TestConfiguration.cmake` - Defines test targets and dependencies - Configures test execution parameters - Sets up coverage and profiling **Test Execution**: - `bash test` - Comprehensive test runner (preferred) - `uv run test.py` - Python test interface - Individual test executables in `.build/bin/` ### Build Troubleshooting Guide **For Linker Issues:** 1. **Check `tests/cmake/LinkerCompatibility.cmake` first** 2. Look for lld-link detection and compatibility functions 3. Check GNUโ†’MSVC flag translation logic 4. Verify warning suppression settings **For Compiler Issues:** 1. Check `tests/cmake/CompilerDetection.cmake` for detection logic 2. Review `tests/cmake/CompilerFlags.cmake` for flag conflicts 3. Verify optimization settings in `OptimizationSettings.cmake` **For Build Performance:** 1. Check `tests/cmake/ParallelBuild.cmake` for parallel settings 2. Review LTO configuration in `OptimizationSettings.cmake` 3. Verify linker selection (mold, lld, default) ## ๐Ÿšจ CRITICAL REQUIREMENTS FOR ALL AGENTS (FOREGROUND & BACKGROUND) ๐Ÿšจ ## ๐Ÿšจ MANDATORY COMMAND EXECUTION RULES ๐Ÿšจ ### FOREGROUND AGENTS (Interactive Development) **FOREGROUND AGENTS MUST FOLLOW THESE COMMAND EXECUTION PATTERNS:** #### Python Code Execution: - โŒ **NEVER run Python code directly** - โœ… **ALWAYS create/modify tmp.py** with your code - โœ… **ALWAYS run: `uv run tmp.py`** #### Shell Command Execution: - โŒ **NEVER run shell commands directly** - โœ… **ALWAYS create/modify tmp.sh** with your commands - โœ… **ALWAYS run: `bash tmp.sh`** #### Command Execution Examples: **Python Code Pattern:** ```python # tmp.py import subprocess result = subprocess.run(['git', 'status'], capture_output=True, text=True) print(result.stdout) ``` Then run: `uv run tmp.py` **Shell Commands Pattern:** ```bash # tmp.sh #!/bin/bash git status ls -la uv run ci/ci-compile.py uno --examples Blink ``` Then run: `bash tmp.sh` ### BACKGROUND AGENTS (Automated/CI Environments) **BACKGROUND AGENTS MUST FOLLOW THESE RESTRICTED COMMAND EXECUTION PATTERNS:** #### Python Code Execution: - โŒ **NEVER run Python code directly** - โŒ **NEVER create/use tmp.py files** (forbidden for background agents) - โœ… **ALWAYS use `uv run` with existing scripts** (e.g., `uv run test.py`, `uv run ci/ci-compile.py`) - โœ… **ALWAYS use MCP server tools** for programmatic operations when available #### Shell Command Execution: - โŒ **NEVER run shell commands directly** - โŒ **NEVER create/use tmp.sh files** (forbidden for background agents) - โœ… **ALWAYS use existing bash scripts** (e.g., `bash test`, `bash lint`) - โœ… **ALWAYS use `uv run` for Python scripts** with proper arguments - โœ… **ALWAYS use MCP server tools** for complex operations when available #### Background Agent Command Examples: **โœ… ALLOWED - Using existing scripts:** ```bash bash test bash lint uv run test.py audio_json_parsing uv run ci/ci-compile.py uno --examples Blink ``` **โŒ FORBIDDEN - Creating temporary files:** ```bash # DON'T DO THIS - tmp.sh is forbidden for background agents echo "git status" > tmp.sh bash tmp.sh ``` **โœ… PREFERRED - Using MCP server tools:** ```bash uv run mcp_server.py # Then use appropriate MCP tools like: validate_completion, symbol_analysis, etc. ``` ### DELETE Operations - DANGER ZONE (ALL AGENTS): - ๐Ÿšจ **STOP and ask for permission** before ANY delete operations - โœ… **EXCEPTION:** Single files that you just created are OK to delete - โŒ **NEVER delete multiple files** without explicit permission - โŒ **NEVER delete directories** without explicit permission - โŒ **NEVER delete system files or project files** without permission ### Git-Bash Terminal Truncation Issue **๐Ÿšจ IMPORTANT: Git-Bash terminal may truncate the first character of commands** **Problem:** The git-bash terminal on Windows sometimes truncates the first character of commands, causing them to fail or execute incorrectly. **Solution:** Pre-pend commands with an additional space when using git-bash: - โœ… **CORRECT**: ` bash test` (note the leading space) - โŒ **INCORRECT**: `bash test` (may get truncated to `ash test`) **Examples:** ```bash # Good - with leading space for git-bash compatibility bash test uv run test.py bash lint # Bad - may get truncated in git-bash bash test uv run test.py bash lint ``` **Why:** This ensures commands work reliably across all terminal environments, especially git-bash on Windows systems. ### Why These Rules Exist: - **Ensures all operations are reviewable and traceable** - **Prevents accidental destructive operations in automated environments** - **Allows for better debugging and error handling** - **Maintains consistency across different agent types** - **Provides audit trail for all system modifications** - **Prevents background agents from creating unnecessary temporary files** **These command execution rules apply to ALL operations including but not limited to:** - File system operations (creating, modifying, deleting files) - Git operations (commits, pushes, branch changes) - Package installations and updates - Build and compilation commands - Test execution and validation - System configuration changes ### MANDATORY MEMORY REFRESH BEFORE COMPLETION **๐Ÿšจ ALL AGENTS MUST REFRESH THEIR MEMORY BEFORE CONCLUDING WORK:** - **The AI shall read the cursor rules again to find out what to do before task is considered complete** - This is MANDATORY for both foreground and background agents - Reading the cursor rules refreshes your memory about the latest project rules, coding standards, and requirements - This ensures you have current information about testing procedures, validation tools, and completion requirements - Do NOT indicate your work is "done" until you have refreshed your memory by reading the cursor rules ## ๐Ÿšจ CRITICAL REQUIREMENTS FOR BACKGROUND AGENTS ๐Ÿšจ ### MANDATORY PRE-COMPLETION VALIDATION **ALL BACKGROUND AGENTS MUST FOLLOW THESE REQUIREMENTS BEFORE INDICATING COMPLETION:** 1. **๐Ÿšจ ALWAYS RUN `bash test` BEFORE INDICATING COMPLETION** - This is MANDATORY and NON-NEGOTIABLE for all background agents - The command `bash test` runs the full test suite including unit tests and compilation checks - Background agents MUST NOT indicate they are "done" until ALL tests pass 2. **๐Ÿšจ USE MCP SERVER VALIDATION TOOL** - Use the `validate_completion` tool from the MCP server: `uv run mcp_server.py` - This tool runs `bash test` and validates that all tests pass - Only indicate completion if this validation tool returns success 3. **๐Ÿšจ ZERO TOLERANCE FOR TEST FAILURES** - If ANY test fails, the background agent MUST fix the issues before completion - Do NOT indicate completion with failing tests - Do NOT ignore test errors or warnings ### Background Agent Completion Checklist: - [ ] All code changes have been made - [ ] `bash test` has been run and ALL tests pass - [ ] No compilation errors or warnings - [ ] MCP server `validate_completion` tool shows success - [ ] Only then indicate task completion **FAILURE TO FOLLOW THESE REQUIREMENTS WILL RESULT IN BROKEN CODE SUBMISSIONS.** ### C++ Design Patterns **Shared Implementation Pattern:** When writing a lot of code that applies the same operation on a bunch of C++ objects, try and determine if those objects share a common base class or internal object. If so consider whether it's appropriate to move the implementation into a shared space. ## Code Standards ### Avoid std:: Prefixed Functions **DO NOT use `std::` prefixed functions or headers in the codebase.** This project provides its own STL-equivalent implementations under the `fl::` namespace. **Examples of what to avoid and use instead:** **Headers:** **Core Language Support:** - โŒ `#include ` โ†’ โœ… `#include "fl/type_traits.h"` - โŒ `#include ` โ†’ โœ… `#include "fl/algorithm.h"` - โŒ `#include ` โ†’ โœ… `#include "fl/functional.h"` - โŒ `#include ` โ†’ โœ… `#include "fl/initializer_list.h"` **Containers:** - โŒ `#include ` โ†’ โœ… `#include "fl/vector.h"` - โŒ `#include ` โ†’ โœ… `#include "fl/map.h"` - โŒ `#include ` โ†’ โœ… `#include "fl/hash_map.h"` - โŒ `#include ` โ†’ โœ… `#include "fl/hash_set.h"` - โŒ `#include ` โ†’ โœ… `#include "fl/set.h"` - โŒ `#include ` โ†’ โœ… `#include "fl/slice.h"` **Utilities & Smart Types:** - โŒ `#include ` โ†’ โœ… `#include "fl/optional.h"` - โŒ `#include ` โ†’ โœ… `#include "fl/variant.h"` - โŒ `#include ` โ†’ โœ… `#include "fl/pair.h"` (for std::pair) - โŒ `#include ` โ†’ โœ… `#include "fl/string.h"` - โŒ `#include ` โ†’ โœ… `#include "fl/scoped_ptr.h"` or `#include "fl/ptr.h"` **Stream/IO:** - โŒ `#include ` โ†’ โœ… `#include "fl/sstream.h"` **Threading:** - โŒ `#include ` โ†’ โœ… `#include "fl/thread.h"` **Math & System:** - โŒ `#include ` โ†’ โœ… `#include "fl/math.h"` - โŒ `#include ` โ†’ โœ… `#include "fl/stdint.h"` **Functions and classes:** - โŒ `std::move()` โ†’ โœ… `fl::move()` - โŒ `std::forward()` โ†’ โœ… `fl::forward()` - โŒ `std::vector` โ†’ โœ… `fl::vector` - โŒ `std::enable_if` โ†’ โœ… `fl::enable_if` **Why:** The project maintains its own implementations to ensure compatibility across all supported platforms and to avoid bloating the library with unnecessary STL dependencies. **Before using any standard library functionality, check if there's a `fl::` equivalent in the `src/fl/` directory first.** ### Compiler Warning Suppression **ALWAYS use the FastLED compiler control macros from `fl/compiler_control.h` for warning suppression.** This ensures consistent cross-compiler support and proper handling of platform differences. **Correct Warning Suppression Pattern:** ```cpp #include "fl/compiler_control.h" // Suppress specific warning around problematic code FL_DISABLE_WARNING_PUSH FL_DISABLE_FORMAT_TRUNCATION // Use specific warning macros // ... code that triggers warnings ... FL_DISABLE_WARNING_POP ``` **Available Warning Suppression Macros:** - โœ… `FL_DISABLE_WARNING_PUSH` / `FL_DISABLE_WARNING_POP` - Standard push/pop pattern - โœ… `FL_DISABLE_WARNING(warning_name)` - Generic warning suppression (use sparingly) - โœ… `FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS` - Clang global constructor warnings - โœ… `FL_DISABLE_WARNING_SELF_ASSIGN_OVERLOADED` - Clang self-assignment warnings - โœ… `FL_DISABLE_FORMAT_TRUNCATION` - GCC format truncation warnings **What NOT to do:** - โŒ **NEVER use raw `#pragma` directives** - they don't handle compiler differences - โŒ **NEVER write manual `#ifdef __clang__` / `#ifdef __GNUC__` blocks** - use the macros - โŒ **NEVER ignore warnings without suppression** - fix the issue or suppress appropriately **Examples:** ```cpp // โœ… CORRECT - Using FastLED macros #include "fl/compiler_control.h" FL_DISABLE_WARNING_PUSH FL_DISABLE_FORMAT_TRUNCATION // Code that triggers format truncation warnings sprintf(small_buffer, "Very long format string %d", value); FL_DISABLE_WARNING_POP // โŒ INCORRECT - Raw pragma directives #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wformat-truncation" sprintf(small_buffer, "Very long format string %d", value); #pragma GCC diagnostic pop ``` **Why:** The FastLED compiler control system automatically handles: - **Compiler detection** (GCC vs Clang vs MSVC) - **Version compatibility** (warnings that don't exist on older compilers) - **Platform specifics** (AVR, ESP32, ARM, etc.) - **Consistent naming** across different compiler warning systems ### Debug Printing **Use `FL_WARN` for debug printing throughout the codebase.** This ensures consistent debug output that works in both unit tests and live application testing. **Usage:** - โœ… `FL_WARN("Debug message: " << message);` - โŒ `FL_WARN("Value: %d", value);` **Why:** `FL_WARN` provides a unified logging interface that works across all platforms and testing environments, including unit tests and Arduino sketches. ### Emoticon and Emoji Usage Policy **NO emoticons or emoji characters are allowed in C++ source files.** This ensures professional, maintainable code that works correctly across all platforms and development environments. **Prohibited in C++ Files:** - โŒ **C++ source files** (*.cpp, *.c) - โŒ **C++ header files** (*.h, *.hpp) - โŒ **Comments in C++ files** - use clear text instead - โŒ **String literals in C++ code** - use descriptive text - โŒ **Log messages in C++ code** - use text prefixes like "SUCCESS:", "ERROR:", "WARNING:" **Examples of what NOT to do in C++ files:** ```cpp // โŒ BAD - Emoticons in comments // ๐ŸŽฏ This function handles user input // โŒ BAD - Emoticons in log messages FL_WARN("โœ… Operation successful!"); FL_WARN("โŒ Error occurred: " << error_msg); // โŒ BAD - Emoticons in string literals const char* status = "๐Ÿ”„ Processing..."; ``` **Examples of correct alternatives in C++ files:** ```cpp // โœ… GOOD - Clear text in comments // TUTORIAL: This function handles user input // โœ… GOOD - Text prefixes in log messages FL_WARN("SUCCESS: Operation completed successfully!"); FL_WARN("ERROR: Failed to process request: " << error_msg); // โœ… GOOD - Descriptive text in string literals const char* status = "PROCESSING: Request in progress..."; ``` **Allowed in Python Files:** - โœ… **Python source files** (*.py) - emoticons are acceptable for build scripts, tools, and utilities - โœ… **Python comments and docstrings** - can use emoticons for clarity in development tools - โœ… **Python log messages** - emoticons OK in build/test output for visual distinction **Why:** - **Cross-platform compatibility** - Some compilers/platforms have issues with Unicode characters - **Professional codebase** - Maintains serious, enterprise-grade appearance - **Accessibility** - Screen readers and text-based tools work better with plain text - **Consistency** - Ensures uniform code style across all C++ files - **Debugging** - Text-based prefixes are easier to search and filter in logs **Before adding any visual indicators to C++ code, use text-based alternatives like "TODO:", "NOTE:", "WARNING:", "SUCCESS:", "ERROR:" prefixes.** ### Naming Conventions **Follow these naming conventions for consistency across the codebase:** **Simple Objects:** - โœ… Use lowercase class names for simple objects (e.g., `fl::vec2f`, `fl::point`, `fl::rect`) - โŒ Avoid: `fl::Vec2f`, `fl::Point`, `fl::Rect` **Complex Objects:** - โœ… Use CamelCase with uppercase first character for complex objects (e.g., `Raster`, `Controller`, `Canvas`) - โŒ Avoid: `raster`, `controller`, `canvas` **Pixel Types:** - โœ… Use ALL CAPS for pixel types (e.g., `CRGB`, `CHSV`, `HSV16`, `RGB24`) - โŒ Avoid: `crgb`, `Crgb`, `chsv`, `Chsv` **Member Variables and Functions:** **Complex Classes/Objects:** - โœ… **Member variables:** Use `mVariableName` format (e.g., `mPixelCount`, `mBufferSize`, `mCurrentIndex`) - โœ… **Member functions:** Use camelCase (e.g., `getValue()`, `setPixelColor()`, `updateBuffer()`) - โŒ Avoid: `m_variable_name`, `variableName`, `GetValue()`, `set_pixel_color()` **Simple Classes/Structs:** - โœ… **Member variables:** Use lowercase snake_case (e.g., `x`, `y`, `width`, `height`, `pixel_count`) - โœ… **Member functions:** Use camelCase (e.g., `getValue()`, `setPosition()`, `normalize()`) - โŒ Avoid: `mX`, `mY`, `get_value()`, `set_position()` **Examples:** ```cpp // Complex class - use mVariableName for members class Controller { private: int mPixelCount; // โœ… Complex class member variable uint8_t* mBuffer; // โœ… Complex class member variable bool mIsInitialized; // โœ… Complex class member variable public: void setPixelCount(int count); // โœ… Complex class member function int getPixelCount() const; // โœ… Complex class member function void updateBuffer(); // โœ… Complex class member function }; // Simple struct - use snake_case for members struct vec2 { int x; // โœ… Simple struct member variable int y; // โœ… Simple struct member variable float magnitude() const; // โœ… Simple struct member function void normalize(); // โœ… Simple struct member function }; struct point { float x; // โœ… Simple struct member variable float y; // โœ… Simple struct member variable float z; // โœ… Simple struct member variable void setPosition(float x, float y, float z); // โœ… Simple struct member function float distanceTo(const point& other) const; // โœ… Simple struct member function }; ``` **Why:** These conventions help distinguish between different categories of objects and maintain consistency with existing FastLED patterns. Complex classes use Hungarian notation for member variables to clearly distinguish them from local variables, while simple structs use concise snake_case for lightweight data containers. ### Container Parameter Types **Prefer `fl::span` over `fl::vector` or arrays for function parameters.** `fl::span` provides a non-owning view that automatically converts from various container types, making APIs more flexible and efficient. **Examples:** - โœ… `void processData(fl::span data)` - accepts arrays, vectors, and other containers - โŒ `void processData(fl::vector& data)` - only accepts fl::Vector - โŒ `void processData(uint8_t* data, size_t length)` - requires manual length tracking **Benefits:** - **Automatic conversion:** `fl::span` can automatically convert from `fl::vector`, C-style arrays, and other container types - **Type safety:** Maintains compile-time type checking while being more flexible than raw pointers - **Performance:** Zero-cost abstraction that avoids unnecessary copying or allocation - **Consistency:** Provides a uniform interface for working with contiguous data **When to use `fl::vector` instead:** - When you need ownership and dynamic resizing capabilities - When storing data as a class member that needs to persist **Why:** Using `fl::span` for parameters makes functions more reusable and avoids forcing callers to convert their data to specific container types. ### Exception Handling **DO NOT use try-catch blocks or C++ exception handling in the codebase.** FastLED is designed to work on embedded systems like Arduino where exception handling may not be available or desired due to memory and performance constraints. **Examples of what to avoid and use instead:** **Avoid Exception Handling:** - โŒ `try { ... } catch (const std::exception& e) { ... }` - Exception handling not available on many embedded platforms - โŒ `throw std::runtime_error("error message")` - Throwing exceptions not supported - โŒ `#include ` or `#include ` - Exception headers not needed **Use Error Handling Alternatives:** - โœ… **Return error codes:** `bool function() { return false; }` or custom error enums - โœ… **Optional types:** `fl::optional` for functions that may not return a value - โœ… **Assertions:** `FL_ASSERT(condition)` for debug-time validation - โœ… **Early returns:** `if (!valid) return false;` for error conditions - โœ… **Status objects:** Custom result types that combine success/failure with data **Examples of proper error handling:** ```cpp // Good: Using return codes bool initializeHardware() { if (!setupPins()) { FL_WARN("Failed to setup pins"); return false; } return true; } // Good: Using fl::optional fl::optional calculateValue(int input) { if (input < 0) { return fl::nullopt; // No value, indicates error } return fl::make_optional(sqrt(input)); } // Good: Using early returns void processData(const uint8_t* data, size_t len) { if (!data || len == 0) { FL_WARN("Invalid input data"); return; // Early return on error } // Process data... } ``` **Why:** Many embedded platforms (especially Arduino-compatible boards) don't support C++ exceptions or have them disabled to save memory and improve performance. FastLED must work reliably across all supported platforms. ### JSON Usage - Ideal API Patterns **๐ŸŽฏ PREFERRED: Use the modern `fl::Json` class for all JSON operations.** FastLED provides an ideal JSON API that prioritizes type safety, ergonomics, and crash-proof operation. **โœ… IDIOMATIC JSON USAGE:** ```cpp // NEW: Clean, safe, idiomatic API fl::Json json = fl::Json::parse(jsonStr); int brightness = json["config"]["brightness"] | 128; // Gets value or 128 default string name = json["device"]["name"] | string("default"); // Type-safe with default bool enabled = json["features"]["networking"] | false; // Never crashes // Array operations if (json["effects"].contains("rainbow")) { // Safe array checking } ``` **โŒ DISCOURAGED: Verbose legacy API:** ```cpp // OLD: Verbose, error-prone API (still works, but not recommended) fl::JsonDocument doc; fl::string error; fl::parseJson(jsonStr, &doc, &error); int brightness = doc["config"]["brightness"].as(); // Can crash if missing ``` **Key Benefits of Ideal API:** - **๐Ÿ›ก๏ธ Type Safety** - No crashes on missing fields or type mismatches - **๐ŸŽฏ Default Values** - Clean `json["key"] | default` syntax - **๐Ÿ“– Readable Code** - 50% less boilerplate for common operations - **๐Ÿš€ Testing** - Easy test data construction with `JsonBuilder` **๐Ÿ“š Reference Example:** See `examples/Json/Json.ino` for comprehensive usage patterns and API comparison. **Testing with JsonBuilder:** ```cpp // Easy test data construction auto json = JsonBuilder() .set("brightness", 128) .set("enabled", true) .set("name", "test_device") .build(); // Type-safe testing CHECK_EQ(json["brightness"] | 0, 128); CHECK(json["enabled"] | false); ``` **๐ŸŽฏ GUIDELINE:** Always prefer the ideal `fl::Json` API for new code. The legacy `fl::parseJson` API remains available for backward compatibility but should be avoided in new implementations. ## โš ๏ธ CRITICAL WARNING: C++ โ†” JavaScript Bindings **๐Ÿšจ EXTREMELY IMPORTANT: DO NOT MODIFY FUNCTION SIGNATURES IN WEBASSEMBLY BINDINGS WITHOUT EXTREME CAUTION! ๐Ÿšจ** The FastLED project includes WebAssembly (WASM) bindings that bridge C++ and JavaScript code. **Changing function signatures in these bindings is a major source of runtime errors and build failures.** ### Key Binding Files (โš ๏ธ HIGH RISK ZONE โš ๏ธ): - `src/platforms/wasm/js_bindings.cpp` - Main JavaScript interface via EM_ASM - `src/platforms/wasm/ui.cpp` - UI update bindings with extern "C" wrappers - `src/platforms/wasm/active_strip_data.cpp` - Strip data bindings via EMSCRIPTEN_BINDINGS - `src/platforms/wasm/fs_wasm.cpp` - File system bindings via EMSCRIPTEN_BINDINGS ### Before Making ANY Changes to These Files: 1. **๐Ÿ›‘ STOP and consider if the change is absolutely necessary** 2. **๐Ÿ“– Read the warning comments at the top of each binding file** 3. **๐Ÿงช Test extensively on WASM target after any changes** 4. **๐Ÿ”— Verify both C++ and JavaScript sides remain synchronized** 5. **๐Ÿ“ Update corresponding JavaScript code if function signatures change** ### Common Binding Errors: - **Parameter type mismatches** (e.g., `const char*` vs `std::string`) - **Return type changes** that break JavaScript expectations - **Function name changes** without updating JavaScript calls - **Missing `extern "C"` wrappers** for EMSCRIPTEN_KEEPALIVE functions - **EMSCRIPTEN_BINDINGS macro changes** without updating JS Module calls ### If You Must Modify Bindings: 1. **Update BOTH sides simultaneously** (C++ and JavaScript) 2. **Maintain backward compatibility** when possible 3. **Add detailed comments** explaining the interface contract 4. **Test thoroughly** with real WASM builds, not just compilation 5. **Update documentation** and interface specs **Remember: The bindings are a CONTRACT between C++ and JavaScript. Breaking this contract causes silent failures and mysterious bugs that are extremely difficult to debug.** ## ๐Ÿšจ WASM PLATFORM SPECIFIC RULES ๐Ÿšจ ### WASM Unified Build Awareness **๐Ÿšจ CRITICAL: WASM builds use unified compilation when `FASTLED_ALL_SRC=1` is enabled (automatic for Clang builds)** **Root Cause**: Multiple .cpp files are compiled together in a single compilation unit, causing: - Duplicate function definitions - Type signature conflicts - Symbol redefinition errors **MANDATORY RULES:** - **ALWAYS check for unified builds** when modifying WASM platform files - **NEVER create duplicate function definitions** across WASM .cpp files - **USE `EMSCRIPTEN_KEEPALIVE` functions as canonical implementations** - **MATCH Emscripten header signatures exactly** for external C functions - **REMOVE conflicting implementations** and add explanatory comments **Fix Pattern Example:** ```cpp // In timer.cpp (CANONICAL) extern "C" { EMSCRIPTEN_KEEPALIVE uint32_t millis() { // Implementation } } // In js.cpp (FIXED) extern "C" { // NOTE: millis() and micros() functions are defined in timer.cpp with EMSCRIPTEN_KEEPALIVE // to avoid duplicate definitions in unified builds } ``` ### WASM Async Platform Pump Pattern **๐Ÿšจ CRITICAL: Avoid long blocking sleeps that prevent responsive async processing** **MANDATORY RULES:** - **AVOID long blocking sleeps** (e.g., `emscripten_sleep(100)`) in main loops - **USE short sleep intervals** (1ms) for responsive yielding in main loops - **ALLOW JavaScript to control timing** via extern functions rather than blocking C++ loops **Correct Implementation:** ```cpp // โœ… GOOD - responsive yielding without blocking while (true) { // Keep pthread alive for extern function calls // Use short sleep for responsiveness emscripten_sleep(1); // 1ms yield for responsiveness } ``` **Why This Matters:** - Long blocking sleeps prevent responsive browser interaction - JavaScript should control FastLED timing via requestAnimationFrame - Short sleep intervals maintain responsiveness while allowing other threads to work ### WASM Function Signature Matching **๐Ÿšจ CRITICAL: External C function declarations must match Emscripten headers exactly** **Common Error Pattern:** ```cpp // โŒ WRONG - causes compilation error extern "C" void emscripten_sleep(int ms); // โœ… CORRECT - matches official Emscripten header extern "C" void emscripten_sleep(unsigned int ms); ``` **MANDATORY RULES:** - **ALWAYS verify against official Emscripten header signatures** - **NEVER assume parameter types** - check the actual headers - **UPDATE signatures immediately** when compilation errors occur ### WASM Sketch Compilation **๐ŸŽฏ HOW TO COMPILE ANY ARDUINO SKETCH TO WASM:** **Basic Compilation Commands:** ```bash # Compile any sketch directory to WASM uv run ci/wasm_compile.py path/to/your/sketch # Compile with build and open browser automatically uv run ci/wasm_compile.py path/to/your/sketch -b --open # Compile examples/Blink to WASM uv run ci/wasm_compile.py examples/Blink -b --open # Compile examples/DemoReel100 to WASM uv run ci/wasm_compile.py examples/DemoReel100 -b --open ``` **Output:** Creates `fastled_js/` folder with: - `fastled.js` - JavaScript loader - `fastled.wasm` - WebAssembly binary - `index.html` - HTML page to run the sketch **Run in Browser:** ```bash # Simple HTTP server to test cd fastled_js python -m http.server 8000 # Open http://localhost:8000 ``` **๐Ÿšจ REQUIREMENTS:** - **Docker must be installed and running** - **Internet connection** (for Docker image download on first run) - **~1GB RAM** for Docker container during compilation ### WASM Testing Requirements **๐Ÿšจ MANDATORY: Always test WASM compilation after platform file changes** **Platform Testing Commands:** ```bash # Test WASM platform changes (for platform developers) uv run ci/wasm_compile.py -b examples/wasm # Quick test without full build uv run ci/wasm_compile.py examples/wasm --quick ``` **Watch For These Error Patterns:** - `error: conflicting types for 'function_name'` - `error: redefinition of 'function_name'` - `warning: attribute declaration must precede definition` - `RuntimeError: unreachable` (often async-related) **MANDATORY RULES:** - **ALWAYS test WASM compilation** after modifying any WASM platform files - **USE `uv run ci/wasm_compile.py` for validation** - **WATCH for unified build conflicts** in compilation output - **VERIFY async operations work properly** in browser environment ### WASM Platform File Organization **Best Practices for WASM platform files:** - โœ… **Use `timer.cpp` for canonical timing functions** with `EMSCRIPTEN_KEEPALIVE` - โœ… **Use `entry_point.cpp` for main() and setup/loop coordination** with async pumping - โœ… **Use `js.cpp` for JavaScript utility functions** without duplicating timer functions - โœ… **Include proper async infrastructure** (`fl/async.h`) in entry points - โœ… **Comment when removing duplicate implementations** to explain unified build conflicts ## Testing The project uses a comprehensive test suite including: - C++ unit tests - Platform compilation tests - Code quality checks (ruff, clang-format) - Example compilation verification ### Test Assertion Macros **๐Ÿšจ CRITICAL: Always use the proper assertion macros for better error messages and debugging:** **Equality Assertions:** - โœ… **CORRECT**: `CHECK_EQ(A, B)` - for equality comparisons - โŒ **INCORRECT**: `CHECK(A == B)` - provides poor error messages **Inequality Assertions:** - โœ… **CORRECT**: `CHECK_LT(A, B)` - for less than comparisons - โœ… **CORRECT**: `CHECK_LE(A, B)` - for less than or equal comparisons - โœ… **CORRECT**: `CHECK_GT(A, B)` - for greater than comparisons - โœ… **CORRECT**: `CHECK_GE(A, B)` - for greater than or equal comparisons - โŒ **INCORRECT**: `CHECK(A < B)`, `CHECK(A <= B)`, `CHECK(A > B)`, `CHECK(A >= B)` **Boolean Assertions:** - โœ… **CORRECT**: `CHECK_TRUE(condition)` - for true conditions - โœ… **CORRECT**: `CHECK_FALSE(condition)` - for false conditions - โŒ **INCORRECT**: `CHECK(condition)` - for boolean checks **String Assertions:** - โœ… **CORRECT**: `CHECK_STREQ(str1, str2)` - for string equality - โœ… **CORRECT**: `CHECK_STRNE(str1, str2)` - for string inequality - โŒ **INCORRECT**: `CHECK(str1 == str2)` - for string comparisons **Floating Point Assertions:** - โœ… **CORRECT**: `CHECK_DOUBLE_EQ(a, b)` - for floating point equality - โœ… **CORRECT**: `CHECK_DOUBLE_NE(a, b)` - for floating point inequality - โŒ **INCORRECT**: `CHECK(a == b)` - for floating point comparisons **Examples:** ```cpp // Good assertion usage CHECK_EQ(expected_value, actual_value); CHECK_LT(current_index, max_index); CHECK_GT(temperature, 0.0); CHECK_TRUE(is_initialized); CHECK_FALSE(has_error); CHECK_STREQ("expected", actual_string); CHECK_DOUBLE_EQ(3.14159, pi_value, 0.001); // Bad assertion usage CHECK(expected_value == actual_value); // Poor error messages CHECK(current_index < max_index); // Poor error messages CHECK(is_initialized); // Unclear intent CHECK("expected" == actual_string); // Wrong comparison type ``` **Why:** Using the proper assertion macros provides: - **Better error messages** with actual vs expected values - **Clearer intent** about what is being tested - **Consistent debugging** across all test failures - **Type safety** for different comparison types ### Test File Creation Guidelines **๐Ÿšจ CRITICAL: Minimize test file proliferation - Consolidate tests whenever possible** **PREFERRED APPROACH:** - โœ… **CONSOLIDATE:** Add new test cases to existing related test files - โœ… **EXTEND:** Expand existing `TEST_CASE()` blocks with additional scenarios - โœ… **REUSE:** Leverage existing test infrastructure and helper functions - โœ… **COMPREHENSIVE:** Create single comprehensive test files that cover entire feature areas **CREATE NEW TEST FILES ONLY WHEN:** - โœ… **Testing completely new subsystems** with no existing related tests - โœ… **Isolated feature areas** that don't fit logically into existing test structure - โœ… **Complex integration tests** that require dedicated setup/teardown **AVOID:** - โŒ **Creating new test files for minor bug fixes** - add to existing tests - โŒ **One test case per file** - consolidate related functionality - โŒ **Duplicate test patterns** across multiple files - โŒ **Scattered feature testing** - keep related tests together **EXAMPLES:** **โœ… GOOD - Consolidation:** ```cpp // Add to existing tests/test_json_comprehensive.cpp TEST_CASE("JSON - New Feature Addition") { // Add new functionality tests to existing comprehensive file } ``` **โŒ BAD - Proliferation:** ```cpp // Don't create tests/test_json_new_feature.cpp for minor additions // Instead add to existing comprehensive test file ``` **DEVELOPMENT WORKFLOW:** 1. **During Development/Bug Fixing:** Temporary test files are acceptable for rapid iteration 2. **Near Completion:** **MANDATORY** - Consolidate temporary tests into existing files 3. **Final Review:** Remove temporary test files and ensure comprehensive coverage in main test files **CONSOLIDATION CHECKLIST:** - [ ] Can this test be added to an existing `TEST_CASE` in the same file? - [ ] Does an existing test file cover the same functional area? - [ ] Would this test fit better as a sub-section of a comprehensive test? - [ ] Are there duplicate test patterns that can be eliminated? **Why:** Maintaining a clean, consolidated test suite: - **Easier maintenance** - fewer files to manage and update - **Better organization** - related functionality tested together - **Faster builds** - fewer compilation units - **Cleaner repository** - less file clutter - **Improved discoverability** - easier to find existing test coverage ### Test Execution Format **๐Ÿšจ CRITICAL: Always use the correct test execution format:** - โœ… **CORRECT**: `bash test ` (e.g., `bash test audio_json_parsing`) - โŒ **INCORRECT**: `./.build/bin/test_.exe` - โŒ **INCORRECT**: Running executables directly **Examples:** - `bash test` - Run all tests (includes debug symbols) - `bash test audio_json_parsing` - Run specific test - `bash test xypath` - Run test_xypath.cpp - `bash compile uno --examples Blink` - Compile examples **Quick Build Options:** - `bash test --quick --cpp` - Quick C++ tests only (when no *.py changes) - `bash test --quick` - Quick tests including Python (when *.py changes) **Why:** The `bash test` wrapper handles platform differences, environment setup, and proper test execution across all supported systems. Use `bash test` as specified in user rules for running unit tests. For compilation tests, use `bash compile --examples ` (e.g., `bash compile uno --examples Blink`). **๐Ÿšจ FOR BACKGROUND AGENTS:** Running `bash test` is MANDATORY before indicating completion. Use the MCP server `validate_completion` tool to ensure all tests pass before completing any task. ## Debugging and Stack Traces ### Stack Trace Setup The FastLED project supports enhanced debugging through stack trace functionality for crash analysis and debugging. **For Background Agents**: Use the MCP server tool `setup_stack_traces` to automatically install and configure stack trace support: ```bash # Via MCP server (recommended for background agents) uv run mcp_server.py # Then use setup_stack_traces tool with method: "auto" ``` **Manual Installation**: **Ubuntu/Debian**: ```bash sudo apt-get update sudo apt-get install -y libunwind-dev build-essential ``` **CentOS/RHEL/Fedora**: ```bash sudo yum install -y libunwind-devel gcc-c++ # CentOS/RHEL sudo dnf install -y libunwind-devel gcc-c++ # Fedora ``` **macOS**: ```bash brew install libunwind ``` ### Available Stack Trace Methods 1. **LibUnwind** (Recommended) - Enhanced stack traces with symbol resolution 2. **Execinfo** (Fallback) - Basic stack traces using standard glibc 3. **Windows** (On Windows) - Windows-specific debugging APIs 4. **No-op** (Last resort) - Minimal crash handling The build system automatically detects and configures the best available option. ### Testing Stack Traces ```bash # Note: Stack trace testing now uses Python build system # CMake commands are deprecated cd tests cmake . && make crash_test_standalone crash_test_execinfo # DEPRECATED # Test libunwind version ./.build/bin/crash_test_standalone manual # Manual stack trace ./.build/bin/crash_test_standalone nullptr # Crash test # Test execinfo version ./.build/bin/crash_test_execinfo manual # Manual stack trace ./.build/bin/crash_test_execinfo nullptr # Crash test ``` ### Using in Code ```cpp #include "tests/crash_handler.h" int main() { setup_crash_handler(); // Enable crash handling // Your code here... return 0; } ``` **For Background Agents**: Always run the `setup_stack_traces` MCP tool when setting up a new environment to ensure proper debugging capabilities are available. ## Platform Build Information Analysis The FastLED project includes comprehensive tools for analyzing platform-specific build information, including preprocessor defines, compiler flags, and toolchain paths from `build_info.json` files generated during compilation. ### Overview Platform build information is stored in `.build/{platform}/build_info.json` files that are automatically generated when compiling for any platform. These files contain: - **Platform Defines** - Preprocessor definitions (`#define` values) specific to each platform - **Compiler Information** - Paths, flags, and types for C/C++ compilers - **Toolchain Aliases** - Tool paths for gcc, g++, ar, objcopy, nm, etc. - **Build Configuration** - Framework, build type, and other settings ### Quick Start #### 1. Generate Build Information Before analyzing, ensure you have compiled the platform: ```bash # Compile a platform to generate build_info.json uv run ci/ci-compile.py uno --examples Blink uv run ci/ci-compile.py esp32dev --examples Blink ``` This creates `.build/{platform}/build_info.json` with all platform information. #### 2. Analyze Platform Defines **Get platform-specific preprocessor defines:** ```bash # Command line tool python3 ci/ci/build_info_analyzer.py --board uno --show-defines # Via MCP server uv run mcp_server.py # Use build_info_analysis tool with: board="uno", show_defines=true ``` **Example output for UNO:** ``` ๐Ÿ“‹ Platform Defines for UNO: PLATFORMIO=60118 ARDUINO_AVR_UNO F_CPU=16000000L ARDUINO_ARCH_AVR ARDUINO=10808 __AVR_ATmega328P__ ``` **Example output for ESP32:** ``` ๐Ÿ“‹ Platform Defines for ESP32DEV: ESP32=ESP32 ESP_PLATFORM F_CPU=240000000L ARDUINO_ARCH_ESP32 IDF_VER="v5.3.2-174-g083aad99cf-dirty" ARDUINO_ESP32_DEV # ... and 23 more defines ``` #### 3. Compare Platforms **Compare defines between platforms:** ```bash # Command line python3 ci/ci/build_info_analyzer.py --compare uno esp32dev # Via MCP # Use build_info_analysis tool with: board="uno", compare_with="esp32dev" ``` Shows differences and commonalities between platform defines. ### Available Tools #### Command Line Tool **Basic Usage:** ```bash # List available platforms python3 ci/ci/build_info_analyzer.py --list-boards # Show platform defines python3 ci/ci/build_info_analyzer.py --board uno --show-defines # Show compiler information python3 ci/ci/build_info_analyzer.py --board esp32dev --show-compiler # Show toolchain aliases python3 ci/ci/build_info_analyzer.py --board teensy31 --show-toolchain # Show everything python3 ci/ci/build_info_analyzer.py --board digix --show-all # Compare platforms python3 ci/ci/build_info_analyzer.py --compare uno esp32dev # JSON output for automation python3 ci/ci/build_info_analyzer.py --board uno --show-defines --json ``` #### MCP Server Tool **For Background Agents**, use the MCP server `build_info_analysis` tool: ```bash # Start MCP server uv run mcp_server.py # Use build_info_analysis tool with parameters: # - board: "uno", "esp32dev", "teensy31", etc. or "list" to see available # - show_defines: true/false (default: true) # - show_compiler: true/false # - show_toolchain: true/false # - show_all: true/false # - compare_with: "other_board_name" for comparison # - output_json: true/false for programmatic use ``` ### Supported Platforms The build info analysis works with **ANY platform** that generates a `build_info.json` file: **Embedded Platforms:** - โœ… **UNO (AVR)** - 8-bit microcontroller (6 defines) - โœ… **ESP32DEV** - WiFi-enabled 32-bit platform (29 defines) - โœ… **ESP32S3, ESP32C3, ESP32C6** - All ESP32 variants - โœ… **TEENSY31, TEENSYLC** - ARM Cortex-M platforms - โœ… **DIGIX, BLUEPILL** - ARM Cortex-M3 platforms - โœ… **STM32, NRF52** - Various ARM platforms - โœ… **RPIPICO, RPIPICO2** - Raspberry Pi Pico platforms - โœ… **ATTINY85, ATTINY1616** - Small AVR microcontrollers ### Use Cases #### For Code Development **Understanding Platform Differences:** ```bash # See what defines are available for conditional compilation python3 ci/ci/build_info_analyzer.py --board esp32dev --show-defines # Compare two platforms to understand differences python3 ci/ci/build_info_analyzer.py --compare uno esp32dev ``` **Compiler and Toolchain Information:** ```bash # Get compiler paths and flags for debugging builds python3 ci/ci/build_info_analyzer.py --board teensy31 --show-compiler # Get toolchain paths for symbol analysis python3 ci/ci/build_info_analyzer.py --board digix --show-toolchain ``` #### For Automation **JSON output for scripts:** ```bash # Get defines as JSON for processing python3 ci/ci/build_info_analyzer.py --board uno --show-defines --json # Get all build info as JSON python3 ci/ci/build_info_analyzer.py --board esp32dev --show-all --json ``` ### Integration with Other Tools Build info analysis integrates with other FastLED tools: 1. **Symbol Analysis** - Uses build_info.json to find toolchain paths 2. **Compilation** - Generated automatically during example compilation 3. **Testing** - Provides platform context for test results ### Troubleshooting **Common Issues:** 1. **"No boards with build_info.json found"** - **Solution:** Compile a platform first: `uv run ci/ci-compile.py {board} --examples Blink` 2. **"Board key not found in build_info.json"** - **Solution:** Check available boards: `python3 ci/ci/build_info_analyzer.py --list-boards` 3. **"Build info not found for platform"** - **Solution:** Ensure the platform compiled successfully and check `.build/{board}/build_info.json` exists ### Best Practices 1. **Always compile first** before analyzing build information 2. **Use comparison feature** to understand platform differences 3. **Check defines** when adding platform-specific code 4. **Use JSON output** for automated processing and CI/CD 5. **Combine with symbol analysis** for complete platform understanding **For Background Agents:** Use the MCP server `build_info_analysis` tool for consistent access to platform build information and proper error handling. ## Symbol Analysis for Binary Optimization The FastLED project includes comprehensive symbol analysis tools to identify optimization opportunities and understand binary size allocation across all supported platforms. ### Overview Symbol analysis examines compiled ELF files to: - **Identify large symbols** that may be optimization targets - **Understand memory allocation** across different code sections - **Find unused features** that can be eliminated to reduce binary size - **Compare symbol sizes** between different builds or platforms - **Provide optimization recommendations** based on actual usage patterns ### Supported Platforms The symbol analysis tools work with **ANY platform** that generates a `build_info.json` file, including: **Embedded Platforms:** - โœ… **UNO (AVR)** - Small 8-bit microcontroller (typically ~3-4KB symbols) - โœ… **ESP32DEV (Xtensa)** - WiFi-enabled 32-bit platform (typically ~200-300KB symbols) - โœ… **ESP32S3, ESP32C3, ESP32C6, etc.** - All ESP32 variants supported **ARM Platforms:** - โœ… **TEENSY31 (ARM Cortex-M4)** - High-performance 32-bit (typically ~10-15KB symbols) - โœ… **TEENSYLC (ARM Cortex-M0+)** - Low-power ARM platform (typically ~8-10KB symbols) - โœ… **DIGIX (ARM Cortex-M3)** - Arduino Due compatible (typically ~15-20KB symbols) - โœ… **STM32 (ARM Cortex-M3)** - STMicroelectronics platform (typically ~12-18KB symbols) **And many more!** Any platform with toolchain support and `build_info.json` generation. ### Quick Start #### 1. Prerequisites Before running symbol analysis, ensure you have compiled the platform: ```bash # Compile platform first (required step) uv run ci/ci-compile.py uno --examples Blink uv run ci/ci-compile.py esp32dev --examples Blink uv run ci/ci-compile.py teensy31 --examples Blink ``` This generates the required `.build/{platform}/build_info.json` file and ELF binary. #### 2. Run Symbol Analysis **Analyze specific platform:** ```bash uv run ci/ci/symbol_analysis.py --board uno uv run ci/ci/symbol_analysis.py --board esp32dev uv run ci/ci/symbol_analysis.py --board teensy31 ``` **Analyze all available platforms at once:** ```bash uv run ci/demo_symbol_analysis.py ``` **Using MCP Server (Recommended for Background Agents):** ```bash # Use MCP server tools uv run mcp_server.py # Then use symbol_analysis tool with board: "uno" or "auto" # Or use symbol_analysis tool with run_all_platforms: true ``` ### Analysis Output Symbol analysis provides detailed reports including: **Summary Information:** - **Total symbols count** and **total size** - **Symbol type breakdown** (text, data, bss, etc.) - **Platform-specific details** (toolchain, ELF file location) **Largest Symbols List:** - **Top symbols by size** for optimization targeting - **Demangled C++ names** for easy identification - **Size in bytes** and **percentage of total** **Optimization Recommendations:** - **Feature analysis** - unused features that can be disabled - **Size impact estimates** - potential savings from removing features - **Platform-specific suggestions** based on symbol patterns ### Example Analysis Results **UNO Platform (Small embedded):** ``` ================================================================================ UNO SYMBOL ANALYSIS REPORT ================================================================================ SUMMARY: Total symbols: 51 Total symbol size: 3767 bytes (3.7 KB) LARGEST SYMBOLS (sorted by size): 1. 1204 bytes - ClocklessController<...>::showPixels(...) 2. 572 bytes - CFastLED::show(unsigned char) 3. 460 bytes - main 4. 204 bytes - CPixelLEDController<...>::show(...) ``` **ESP32 Platform (Feature-rich):** ``` ================================================================================ ESP32DEV SYMBOL ANALYSIS REPORT ================================================================================ SUMMARY: Total symbols: 2503 Total symbol size: 237092 bytes (231.5 KB) LARGEST SYMBOLS (sorted by size): 1. 12009 bytes - _vfprintf_r 2. 11813 bytes - _svfprintf_r 3. 8010 bytes - _vfiprintf_r 4. 4192 bytes - port_IntStack ``` ### Advanced Features #### JSON Output Save detailed analysis results for programmatic processing: ```bash uv run symbol_analysis.py --board esp32dev --output-json # Results saved to: .build/esp32dev_symbol_analysis.json ``` #### Batch Analysis Analyze multiple platforms in sequence: ```bash for board in uno esp32dev teensy31; do uv run symbol_analysis.py --board $board done ``` #### Integration with Build Systems The symbol analysis can be integrated into automated build processes: ```python # In your Python build script import subprocess result = subprocess.run(['uv', 'run', 'symbol_analysis.py', '--board', 'uno'], capture_output=True, text=True) ``` ### MCP Server Tools **For Background Agents**, use the MCP server tools for symbol analysis: 1. **Generic Symbol Analysis** (`symbol_analysis` tool): - Works with **any platform** (UNO, ESP32, Teensy, STM32, etc.) - Auto-detects available platforms from `.build/` directory - Can analyze single platform or all platforms simultaneously - Provides comprehensive usage instructions 2. **ESP32-Specific Analysis** (`esp32_symbol_analysis` tool): - **ESP32 platforms only** with FastLED-focused filtering - Includes feature analysis and optimization recommendations - FastLED-specific symbol identification and grouping **Usage via MCP:** ```bash # Start MCP server uv run mcp_server.py # Use symbol_analysis tool with parameters: # - board: "uno", "esp32dev", or "auto" # - run_all_platforms: true/false # - output_json: true/false ``` ### Troubleshooting **Common Issues:** 1. **"build_info.json not found"** - **Solution:** Compile the platform first: `uv run ci/ci-compile.py {board} --examples Blink` 2. **"ELF file not found"** - **Solution:** Ensure compilation completed successfully and check `.build/{board}/` directory 3. **"Tool not found"** (nm, c++filt, etc.) - **Solution:** The platform toolchain isn't installed or configured properly - **Check:** Verify PlatformIO platform installation: `uv run pio platform list` 4. **"No symbols found"** - **Solution:** The ELF file may be stripped or compilation failed - **Check:** Verify ELF file exists and has debug symbols **Debug Mode:** ```bash # Run with verbose Python output for debugging uv run python -v ci/ci/symbol_analysis.py --board uno ``` ### Best Practices 1. **Always compile first** before running symbol analysis 2. **Use consistent examples** (like Blink) for size comparisons 3. **Run analysis on clean builds** to avoid cached/incremental build artifacts 4. **Compare results across platforms** to understand feature scaling 5. **Focus on the largest symbols first** for maximum optimization impact 6. **Use JSON output for automated processing** and trend analysis **For Background Agents:** Always use the MCP server `symbol_analysis` tool for consistent results and proper error handling.