MCPcopy Create free account
hub / github.com/FastLED/FastLED / setup

Function setup

examples/Json/JsonSketch.h:17–139  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

15CRGB leds[NUM_LEDS];
16
17void setup() {
18 Serial.begin(115200);
19
20 // Initialize FastLED
21 FastLED.addLeds<WS2812, DATA_PIN, GRB>(leds, NUM_LEDS);
22 FastLED.setBrightness(64);
23
24 Serial.println("FastLED Ideal JSON API Demo Starting...");
25
26 // Example JSON string with LED configuration
27 const char* configJson = R"({
28 "strip": {
29 "num_leds": 150,
30 "pin": 5,
31 "type": "WS2812B",
32 "brightness": 200
33 },
34 "effects": {
35 "current": "rainbow",
36 "speed": 75
37 },
38 "animation_settings": {
39 "duration_ms": 5000,
40 "loop": true
41 }
42 })";
43
44 // NEW: Parse using ideal API
45 fl::json json = fl::json::parse(configJson);
46
47 if (json.has_value()) {
48 Serial.println("JSON parsed successfully with ideal API!");
49
50 // NEW: Clean syntax with default values - no more verbose error checking!
51 int numLeds = json["strip"]["num_leds"] | 100; // Gets 150, or 100 if missing
52 int pin = json["strip"]["pin"] | 3; // Gets 5, or 3 if missing
53 fl::string type = json["strip"]["type"] | fl::string("WS2812"); // Gets "WS2812B"
54 int brightness = json["strip"]["brightness"] | 64; // Gets 200, or 64 if missing
55
56 // Safe access to missing values - no crashes!
57 int missing = json["non_existent"]["missing"] | 999; // Gets 999
58
59 Serial.println("LED Strip Configuration:");
60 Serial.print(" LEDs: "); Serial.println(numLeds);
61 Serial.print(" Pin: "); Serial.println(pin);
62 Serial.print(" Type: "); Serial.println(type.c_str());
63 Serial.print(" Brightness: "); Serial.println(brightness);
64 Serial.print(" Missing field default: "); Serial.println(missing);
65
66 // Effect configuration with safe defaults
67 fl::string effect = json["effects"]["current"] | fl::string("solid");
68 int speed = json["effects"]["speed"] | 50;
69
70 Serial.println("Effect Configuration:");
71 Serial.print(" Current: "); Serial.println(effect.c_str());
72 Serial.print(" Speed: "); Serial.println(speed);
73
74 // Accessing nested objects with defaults

Callers

nothing calls this directly

Calls 7

stringClass · 0.50
beginMethod · 0.45
setBrightnessMethod · 0.45
printlnMethod · 0.45
has_valueMethod · 0.45
printMethod · 0.45
c_strMethod · 0.45

Tested by

no test coverage detected