FastLED 3.9.15
Loading...
Searching...
No Matches

◆ setup()

void setup ( )

Definition at line 17 of file JsonSketch.h.

17 {
18 Serial.begin(115200);
19
20 // Initialize FastLED
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
75 long duration = json["animation_settings"]["duration_ms"] | 1000;
76 bool loop = json["animation_settings"]["loop"] | false;
77
78 Serial.println("Animation Settings:");
79 Serial.print(" Duration (ms): "); Serial.println(static_cast<int32_t>(duration));
80 Serial.print(" Loop: "); Serial.println(loop ? "true" : "false");
81
82 Serial.println("\n=== NEW ERGONOMIC API DEMONSTRATION ===");
83
84 // Example JSON with mixed data types including strings that can be converted
85 const char* mixedJson = R"({
86 "config": {
87 "brightness": "128",
88 "timeout": "5.5",
89 "enabled": true,
90 "name": "LED Strip"
91 }
92 })";
93
94 fl::Json config = fl::Json::parse(mixedJson);
95
96 Serial.println("\nThree New Conversion Methods:");
97
98 // Method 1: try_as<T>() - Explicit optional handling
99 Serial.println("\n1. try_as<T>() - When you need explicit error handling:");
100 auto maybeBrightness = config["config"]["brightness"].try_as<int>();
101 if (maybeBrightness.has_value()) {
102 Serial.print(" Brightness converted from string: ");
103 Serial.println(*maybeBrightness);
104 } else {
105 Serial.println(" Brightness conversion failed");
106 }
107
108 // Method 2: value<T>() - Direct conversion with sensible defaults
109 Serial.println("\n2. value<T>() - When you want defaults and don't care about failure:");
110 int brightnessDirect = config["config"]["brightness"].value<int>();
111 int missingDirect = config["missing_field"].value<int>();
112 Serial.print(" Brightness (from string): ");
113 Serial.println(brightnessDirect);
114 Serial.print(" Missing field (default 0): ");
115 Serial.println(missingDirect);
116
117 // Method 3: as_or<T>(default) - Custom defaults
118 Serial.println("\n3. as_or<T>(default) - When you want custom defaults:");
119 int customBrightness = config["config"]["brightness"].as_or<int>(255);
120 int customMissing = config["missing_field"].as_or<int>(100);
121 double timeout = config["config"]["timeout"].as_or<double>(10.0);
122 Serial.print(" Brightness with custom default: ");
123 Serial.println(customBrightness);
124 Serial.print(" Missing with custom default: ");
125 Serial.println(customMissing);
126 Serial.print(" Timeout (string to double): ");
127 Serial.println(timeout);
128
129 Serial.println("\nNew API provides:");
130 Serial.println(" ✓ Type safety with automatic string-to-number conversion");
131 Serial.println(" ✓ Three distinct patterns for different use cases");
132 Serial.println(" ✓ Backward compatibility with existing as<T>() API");
133 Serial.println(" ✓ Clean, readable syntax");
134 Serial.println(" ✓ Significantly less code for common operations");
135
136 } else {
137 Serial.println("JSON parsing failed with ideal API");
138 }
139}
CRGB leds[NUM_LEDS]
#define NUM_LEDS
#define DATA_PIN
void loop()
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
Definition FastLED.cpp:74
uint16_t speed
Definition Noise.ino:63
UISlider brightness("Brightness", 128, 0, 255, 1)
WS2812 controller class.
Definition FastLED.h:218
T value() const
Definition json.h:1802
bool has_value() const
Definition json.h:2107
fl::optional< T > try_as() const
Definition json.h:1785
T as_or(const T &fallback) const
Definition json.h:2101
static Json parse(const fl::string &txt)
Definition json.h:2126
const char * c_str() const
Definition str.h:326
@ GRB
Green, Red, Blue (0102)
Definition eorder.h:16

References fl::Json::as_or(), brightness(), fl::StrN< SIZE >::c_str(), DATA_PIN, FastLED, GRB, fl::Json::has_value(), leds, loop(), NUM_LEDS, fl::Json::parse(), speed, fl::Json::try_as(), and fl::Json::value().

+ Here is the call graph for this function: