/* 
 Adapted from sutaburosu's AA_lines.ino code. Many thanks!
*/

#include <FastLED.h>

#define WIDTH 64
#define HEIGHT 13
#define NUM_LEDS (WIDTH * HEIGHT)

CRGB leds[NUM_LEDS + 1];
CRGB buffer1[NUM_LEDS + 1];
CRGB buffer2[NUM_LEDS + 1];

CRGBPalette16 rainbowPalette = {
  0xFF0000, 0x7F0000, 0xAB5500, 0x552A00, 0xABAB00, 0x555500, 0x00FF00, 0x007F00,
  0x00AB55, 0x00552A, 0x0000FF, 0x00007F, 0x5500AB, 0x2A0055, 0xAB0055, 0x55002A
};

CRGBPalette16 greenStripe = {
  0x00FF00, 0x00FF00, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
  0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000
};

CRGBPalette16 redStripe = {
  0xFFFFFF, 0xFFFFFF, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000,
  0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000, 0x000000
};


void setup() {
  FastLED.addLeds<NEOPIXEL, 3>(leds, NUM_LEDS);
  Serial.begin(115200);
}

void loop()
{
    sup_heat();
    sup_aspect();
    for (int i = 0; i < NUM_LEDS ; i++ ) {
      if( buffer2[i] == CRGB(0x000000) ) {
        leds[i] = buffer1[i];
      } else {
        leds[i] = nblend(buffer1[i], buffer2[i], 128);
      }
    }
    // blend(buffer1,buffer2,leds,NUM_LEDS,0);
    // memcpy8( &leds, &buffer2, sizeof(CRGB)*NUM_LEDS ); 
    FastLED.show();
}

uint16_t XY(uint8_t x, uint8_t y) {
  if (x >= WIDTH) return NUM_LEDS;
  if (y >= HEIGHT) return NUM_LEDS;
  return y * WIDTH + x;
}

void sup_heat() {
  static uint32_t yHueDelta ;
  static uint32_t xHueDelta ;
  static float rotationSpeed = 1;
  static uint32_t lastMillis = 16383 ;
  static uint8_t width = 6;
  static int16_t translationSpeed = -1284;

  uint32_t ms = millis();

  if( rotationSpeed != 0 ) {
    int16_t sinPar = (int16_t) round(ms * rotationSpeed);
    int16_t cosPar = (int16_t) round(ms * rotationSpeed);
  // Serial.print(sinPar);
  // Serial.print(" ");
  // Serial.println(cosPar);
    yHueDelta = (int32_t)sin16(sinPar) * width;
    xHueDelta = (int32_t)cos16(cosPar) * width;
    lastMillis = ms;
  } else {
    yHueDelta = (int32_t)sin16(lastMillis) * width;
    xHueDelta = (int32_t)cos16(lastMillis) * width;
    // yHueDelta = 1280040;
    // xHueDelta = 0;
  }
  // Serial.print(ms);
  // Serial.print(" ");
  // Serial.print(yHueDelta);
  // Serial.print(" ");
  // Serial.println(xHueDelta);
  int32_t startHue = ms * translationSpeed;
  int32_t lineStartHue = startHue - (HEIGHT + 2) / 2 * yHueDelta;
  int16_t yd2 = sin16(ms * 3) / 4;
  int16_t xd2 = sin16(ms * 7) / 4;
  for (byte y = 0; y < HEIGHT; y++) {
    uint32_t pixelHue = lineStartHue - (WIDTH + 2) / 2 * xHueDelta;
    uint32_t xhd = xHueDelta;
    lineStartHue += yHueDelta;
    // yHueDelta += yd2;
    for (byte x = 0; x < WIDTH; x++) {
      buffer1[XY(x, y)] = ColorFromPaletteExtended(CloudColors_p, pixelHue >> 7, 255, LINEARBLEND);
      pixelHue += xHueDelta;
      // xhd += xd2;
    }
  }

}

void sup_aspect() {
  static uint32_t yHueDelta ;
  static uint32_t xHueDelta ;
  static float rotationSpeed = -2;
  static uint32_t lastMillis ;
  static uint8_t width = 8;
  static int16_t translationSpeed = 1256;

  uint32_t ms = millis();

  if( rotationSpeed != 0 ) {
    yHueDelta = (int32_t)sin16((int16_t)ms * rotationSpeed) * width;
    xHueDelta = (int32_t)cos16((int16_t)ms * rotationSpeed) * width;
    lastMillis = ms;
  } else {
    // yHueDelta = (int32_t)sin16(lastMillis) * width;
    // xHueDelta = (int32_t)cos16(lastMillis) * width;
    yHueDelta =281650;
    xHueDelta =163670;
  }
  // Serial.print(yHueDelta);
  // Serial.print("\t");
  // Serial.println(xHueDelta);
  int32_t startHue = ms * translationSpeed;
  int32_t lineStartHue = startHue - (HEIGHT + 2) / 2 * yHueDelta;
  int16_t yd2 = sin16(ms * 3) / 4;
  int16_t xd2 = sin16(ms * 7) / 4;
  for (byte y = 0; y < HEIGHT; y++) {
    uint32_t pixelHue = lineStartHue - (WIDTH + 2) / 2 * xHueDelta;
    uint32_t xhd = xHueDelta;
    lineStartHue += yHueDelta;
    // yHueDelta += yd2;
    for (byte x = 0; x < WIDTH; x++) {
      buffer2[XY(x, y)] = ColorFromPaletteExtended(redStripe, pixelHue >> 7, 255, NOBLEND);
      pixelHue += xHueDelta;
      // xhd += xd2;
    }
  }

}


// from: 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);
}