17 {
18 Serial.begin(115200);
19
20
23
24 Serial.println("FastLED Ideal JSON API Demo Starting...");
25
26
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
46
48 Serial.println("JSON parsed successfully with ideal API!");
49
50
51 int numLeds = json["strip"]["num_leds"] | 100;
52 int pin = json["strip"]["pin"] | 3;
54 int brightness = json[
"strip"][
"brightness"] | 64;
55
56
57 int missing = json["non_existent"]["missing"] | 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
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
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
85 const char* mixedJson = R"({
86 "config": {
87 "brightness": "128",
88 "timeout": "5.5",
89 "enabled": true,
90 "name": "LED Strip"
91 }
92 })";
93
95
96 Serial.println("\nThree New Conversion Methods:");
97
98
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
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
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}
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
UISlider brightness("Brightness", 128, 0, 255, 1)
fl::optional< T > try_as() const
T as_or(const T &fallback) const
static Json parse(const fl::string &txt)
const char * c_str() const
@ GRB
Green, Red, Blue (0102)