17 {
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:");
64 Serial.print(
" Missing field default: ");
Serial.println(missing);
65
66
68 int speed = json[
"effects"][
"speed"] | 50;
69
70 Serial.println(
"Effect Configuration:");
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:");
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): ");
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::UISlider brightness("Brightness", BRIGHTNESS, 0, 255)
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
const char * c_str() const FL_NOEXCEPT
bool has_value() const FL_NOEXCEPT
fl::optional< T > try_as() const FL_NOEXCEPT
T as_or(const T &fallback) const FL_NOEXCEPT
T value() const FL_NOEXCEPT
static json parse(const fl::string &txt) FL_NOEXCEPT