#include <FS.h>
#include <ArduinoJson.h>
#include <FastLED.h>

#define LED_PIN     4
#define LED_WIDTH   64
#define LED_HEIGHT  32
#define LED_TYPE    WS2812B

CRGB leds[LED_HEIGHT][LED_WIDTH];

void setup() {
  Serial.begin(115200);
  delay(2000);

  FastLED.addLeds<LED_TYPE, LED_PIN>(leds[0], LED_WIDTH * LED_HEIGHT);
  FastLED.setBrightness(100);

  SPIFFS.begin();
  File file = SPIFFS.open("/image.json", "r");
  size_t size = file.size();
  std::unique_ptr<char[]> buf(new char[size]);
  file.readBytes(buf.get(), size);

  DynamicJsonDocument doc(1024);
  DeserializationError error = deserializeJson(doc, buf.get());
  if (error) {
    Serial.print(F("deserializeJson() failed: "));
    Serial.println(error.c_str());
    return;
  }

  const char* base64 = doc["image"];
  size_t len = strlen(base64);
  size_t decodedLen = base64_decoded_size(base64, len);
  std::unique_ptr<uint8_t[]> decoded(new uint8_t[decodedLen]);
  base64_decode(decoded.get(), base64, len);

  uint8_t* p = decoded.get();
  for (int y = 0; y < LED_HEIGHT; y++) {
    for (int x = 0; x < LED_WIDTH; x++) {
      int index = y * LED_WIDTH + x;
      leds[y][x].r = *p++;
      leds[y][x].g = *p++;
      leds[y][x].b = *p++;
    }
  }

  FastLED.show();
}

void loop() {
}