24 Serial.println(
"FastLED Ideal JSON API Demo Starting...");
27 const char* configJson = R
"({
38 "animation_settings": {
48 Serial.println(
"JSON parsed successfully with ideal API!");
51 int numLeds = json[
"strip"][
"num_leds"] | 100;
52 int pin = json[
"strip"][
"pin"] | 3;
54 int brightness = json[
"strip"][
"brightness"] | 64;
57 int missing = json[
"non_existent"][
"missing"] | 999;
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);
68 int speed = json[
"effects"][
"speed"] | 50;
70 Serial.println(
"Effect Configuration:");
71 Serial.print(
" Current: "); Serial.println(effect.
c_str());
72 Serial.print(
" Speed: "); Serial.println(
speed);
75 long duration = json[
"animation_settings"][
"duration_ms"] | 1000;
76 bool loop = json[
"animation_settings"][
"loop"] |
false;
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");
82 Serial.println(
"\n=== NEW ERGONOMIC API DEMONSTRATION ===");
85 const char* mixedJson = R
"({
96 Serial.println("\nThree New Conversion Methods:");
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);
105 Serial.println(
" Brightness conversion failed");
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);
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);
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");
137 Serial.println(
"JSON parsing failed with ideal API");
143 static uint8_t
hue = 0;
FL_DISABLE_WARNING_PUSH FL_DISABLE_WARNING_GLOBAL_CONSTRUCTORS CFastLED FastLED
Global LED strip management instance.
central include file for FastLED, defines the CFastLED class/object
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
void fill_rainbow(struct CRGB *targetArray, int numToFill, fl::u8 initialhue, fl::u8 deltahue=5)
Fill a range of LEDs with a rainbow of colors.
@ GRB
Green, Red, Blue (0102)
FastLED's Elegant JSON Library: fl::Json
Representation of an RGB pixel (Red, Green, Blue)