#include "FastLED.h"

#define NUM_STRIPS 8
#define NUM_LEDS_PER_STRIP 90
#define NUM_LEDS ((NUM_LEDS_PER_STRIP) * (NUM_STRIPS))

CRGB leds[NUM_LEDS];
CRGBPalette16 palette = RainbowStripeColors_p;

void setup() {
  Serial.begin(115200);
  FastLED.addLeds<NEOPIXEL, 8> (leds, 0 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<NEOPIXEL, 27>(leds, 1 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<NEOPIXEL, 16>(leds, 2 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<NEOPIXEL, 17>(leds, 3 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<NEOPIXEL, 25>(leds, 4 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<NEOPIXEL, 26>(leds, 5 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<NEOPIXEL, 12>(leds, 6 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
  FastLED.addLeds<NEOPIXEL, 13>(leds, 7 * NUM_LEDS_PER_STRIP, NUM_LEDS_PER_STRIP);
}


void loop() {
  uint32_t startHue = millis() * 128;
  uint32_t yHueDelta = ((uint32_t)sin16(millis() * 5) * 2);
  uint32_t xHueDelta = ((uint32_t)cos16(millis() * 6) * 2);
  uint32_t lineStartHue = startHue - (NUM_STRIPS / 2) * yHueDelta;
  uint32_t yd2 = 11234;
  uint32_t xd2 = 678;
  CRGB *led = leds;
  for (byte y = 0; y < NUM_STRIPS; y++) {
    lineStartHue += yHueDelta;
    yHueDelta += yd2;
    uint32_t pixelHue = lineStartHue - (NUM_LEDS_PER_STRIP / 2) * xHueDelta;
    uint32_t xhd = xHueDelta;
    for (byte x = 0; x < NUM_LEDS_PER_STRIP; x++) {
      pixelHue += xhd;
      xhd += xd2;
      // *led++ = ColorFromPalette(palette, pixelHue >> 15, 255, LINEARBLEND);
      *led++ = ColorFromPaletteExtended(palette, pixelHue >> 7, 255, LINEARBLEND);
    }
  }
  FastLED.show();
}


// https://github.com/FastLED/FastLED/pull/202
CRGB ColorFromPaletteExtended(const CRGBPalette16& pal, uint16_t index, uint8_t brightness, TBlendType blendType) {
  // Extract the four most significant bits of the index as a palette index.
  uint8_t index_4bit = (index >> 12);
  // Calculate the 8-bit offset from the palette index.
  uint8_t offset = (uint8_t)(index >> 4);
  // Get the palette entry from the 4-bit index
  const CRGB* entry = &(pal[0]) + index_4bit;
  uint8_t red1   = entry->red;
  uint8_t green1 = entry->green;
  uint8_t blue1  = entry->blue;

  uint8_t blend = offset && (blendType != NOBLEND);
  if (blend) {
    if (index_4bit == 15) {
      entry = &(pal[0]);
    } else {
      entry++;
    }

    // Calculate the scaling factor and scaled values for the lower palette value.
    uint8_t f1 = 255 - offset;
    red1   = scale8_LEAVING_R1_DIRTY(red1,   f1);
    green1 = scale8_LEAVING_R1_DIRTY(green1, f1);
    blue1  = scale8_LEAVING_R1_DIRTY(blue1,  f1);

    // Calculate the scaled values for the neighbouring palette value.
    uint8_t red2   = entry->red;
    uint8_t green2 = entry->green;
    uint8_t blue2  = entry->blue;
    red2   = scale8_LEAVING_R1_DIRTY(red2,   offset);
    green2 = scale8_LEAVING_R1_DIRTY(green2, offset);
    blue2  = scale8_LEAVING_R1_DIRTY(blue2,  offset);
    cleanup_R1();

    // These sums can't overflow, so no qadd8 needed.
    red1   += red2;
    green1 += green2;
    blue1  += blue2;
  }
  if (brightness != 255) {
    // nscale8x3_video(red1, green1, blue1, brightness);
    nscale8x3(red1, green1, blue1, brightness);
  }
  return CRGB(red1, green1, blue1);
}