Fastled with json document

Hi, I'm struggling to get this right. I have a json document looks like this:

{
"t":"2",
"f1":"255,0,0,0,1,4,5,8,9,12,13,16,17,20,21,24,25,28,29,32,33;0,110,255,2,3,6,7,10,11,14,15,18,19,22,23,30,31,34,35;",
"f2":"0,110,255,0,1,4,5,8,9,12,13,16,17,20,21,24,25,28,29,32,33;255,0,0,2,3,6,7,10,11,14,15,18,19,22,23,26,27,30,31,34,35;"
}

where 't' is the total frame number. The 'f1' and 'f2' are the frames data. The data is structured by color. So the first decimal is red, the second is green, the third is blue and the rest till the semicolon ';' is the number of the leds which has these rgb colors. After the semicolon ';' comes the next color with it's led numbers and so on.
The frame can also have an "x" value. This means this frame is the same as the previous one.

#include <stdio.h>
#include <stdlib.h> 
#include "LittleFS.h"
#include <ArduinoJson.h>

#define DATA_PIN    D7
#define LED_TYPE    WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS    36
CRGB leds[NUM_LEDS];

int currentFrame = 1;
String savedAnim = "";
int firstLoad = 0;
int totalFrames = 0;

String load_from_file(String file_name) {
  String result = "";
  
  File this_file = LittleFS.open(file_name, "r");
  if (!this_file) { // failed to open the file, retrn empty result
    return result;
  }

  while (this_file.available()) {
      result += (char)this_file.read();
  }
  
  this_file.close();
  return result;
}

void setup() {
  Serial.begin(115200);
  LittleFS.begin();
  FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
	
}

void loop() {
  if(firstLoad == 0){
    savedAnim = load_from_file("custom.json");
    if (savedAnim == "") {
      Serial.println("The file was empty or not present");
    } else {
      DeserializationError error = deserializeJson(doc, savedAnim);
      if (error) {
        Serial.print(F("Failed: "));
        Serial.println(error.f_str());
        return;
      }else{
         Serial.println("Custom anim file loaded.");
         firstLoad = 1;
         currentFrame = 1;
    	 totalFrames = doc["t"];
      }
    }
  }else{
    FastLED.clear();  


    if(currentFrame<=totalFrames){
    
      const char *frameData = doc["f"+String(currentFrame)];
      cFrame = frameData;
      
      char *ptr = NULL;
      char f[1280];
      int r, g, b; 
      if(frameData == "x"){
        frameData = pFrame;
      }else{
        //Serial.println(frameData);
        if(sscanf(frameData, "%d,%d,%d,%s;", &r, &g, &b, &f)==4){
          Serial.println(f);
          byte index = 0;
          ptr = strtok(f, ",");
          while (ptr != NULL){
            int num = atoi(ptr);
            leds[num] = CRGB(r, g, b);
            index++;
            ptr = strtok(NULL, ",");
          }
          frameData = strchr(frameData, ';') + 1;
        } 
      }
  
      FastLED.show();
      delay(80); 
      currentFrame = currentFrame + 1;
    }else{
      currentFrame = 1;
    }
    pFrame = cFrame;
  }
}

A post was merged into an existing topic: Json doc data to process fastled