Hi, I would like to ask for some help with my project. I have a data set in json format where each variable holds a bundled string containing data for a single frame. It looks like this:
"totalFrames":"2",
"frame1":"0,0,255,0,0;0,1,255,0,0;0,2,255,255,0;0,3,255,255,0;",
"frame2":"0,0,255,255,0;0,2,255,255,0;",
...
The first two data is the x and y coordinate, the last three is the color in RGB. Each pixel of led is separated with a ";".
I wrote a script that opens up these bundles and processes them to fastled. My script works okay, but it's very slow. I tried to solve the issue but haven't been able make it faster.
StaticJsonDocument<1024> doc;
int currentFrame = 1;
#define DATA_PIN D7
#define LED_TYPE WS2811
#define COLOR_ORDER GRB
#define NUM_LEDS 36
CRGB leds[NUM_LEDS];
uint8_t Width = 4;
uint8_t Height = 9;
void setup() {
Serial.begin(115200);
FastLED.addLeds<LED_TYPE,DATA_PIN,COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalLEDStrip);
}
uint16_t XY( uint8_t x, uint8_t y) {
uint16_t i;
if ( x & 0x01) {
uint8_t reverseY = (Height - 1) - y;
i = (x * Height) + reverseY;
} else {
i = (x * Height) + y;
}
return i;
}
void loop() {
int totalFrames = doc["totalFrames"];
if(currentFrame<=totalFrames){
String frameData = doc["frame"+String(currentFrame)];
int a=0;
for (int i=0; i < frameData.length(); i++){
if(frameData.charAt(i) == ';') {
String s = frameData.substring(a, i);
s = s + ",";
int sa[5], b=0, t=0;
for (int j=0; j < s.length(); j++){
if(s.charAt(j) == ',') {
sa[t] = s.substring(b, j).toInt();
b=(j+1);
t++;
}
}
leds[XY(sa[0], sa[1])] = CRGB(sa[2], sa[3], sa[4]);
a=(i+1);
}
}
FastLED.show();
delay(80);
currentFrame = currentFrame + 1;
}
}