/* I bought 4 of the 8x8 NeoPixel panels with the intention to made a 16x16 display
   but I'm having trouble wrapping my head around addressing each of the LEDs.
   I wired up the panels like this
   1,3
   2,4
*/

#include <FastLED.h>

#define kMatrixWidth 16
#define kMatrixHeight 16
#define NUM_LEDS ((kMatrixWidth) * (kMatrixHeight))

CRGB leds[NUM_LEDS];

uint16_t XY(int8_t x, int8_t y) {
  if (x >= kMatrixWidth || y >= kMatrixHeight || x < 0 || y < 0)
    return -1;

  const uint8_t panel_size = 8;
  const bool horizontal_tiling = 0; // set to 1 if panels are chained in X first
  const bool serpentine_tiling = 0; // set to 1 if odd rows/columns of panels are reversed
  const bool serpentine_panels = 1; // set to 1 if panels have odd rows of LEDs reversed

  const uint8_t x_panels = kMatrixWidth / panel_size;
  const uint8_t y_panels = kMatrixHeight / panel_size;
  
  // determine which panel this pixel falls on
  uint8_t x_panel = x / panel_size;
  uint8_t y_panel = y / panel_size;

  // for serpentine layouts of panels
  if (serpentine_tiling) {
    if (horizontal_tiling) {
      if (y_panel & 1) // odd rows of panels are reversed
        x_panel = x_panels - 1 - x_panel;
    } else {
      if (x_panel & 1) // odd columns of panels are reversed
        y_panel = y_panels - 1 - y_panel;
    }
  }

  // where in the leds[] array does this panel start
  uint16_t panel_offset = panel_size * panel_size;
  if (horizontal_tiling)
    panel_offset *= x_panels * y_panel + x_panel;
  else 
    panel_offset *= y_panels * x_panel + y_panel;

  // constrain coordinates to the panel size
  x %= panel_size;
  y %= panel_size;

  // serpentine layout panels have odd rows reversed
  if (serpentine_panels && (y & 1))
    x = panel_size - 1 - x;

  return panel_offset + x + y * panel_size;
}

void setup() {
  FastLED.addLeds<WS2812B, 3, GRB>(leds, NUM_LEDS);
}

void loop() {
  static uint16_t startHue = 0;
  static uint16_t xPhase = 0;
  static int16_t xPhaseMul = 2 * 256;
  static int16_t yPhaseMul = 256;
  static int8_t yPhaseMulStep = 3;
  static int8_t xPhaseMulStep = 2;

  startHue += 512;
  xPhase += 512;

  yPhaseMul += yPhaseMulStep;
  if (yPhaseMul <= 96)
    yPhaseMulStep = random8(4) + 1;
  if (yPhaseMul >= 8 * 128)
    yPhaseMulStep = -random8(4) - 1;

  xPhaseMul += xPhaseMulStep;
  if (xPhaseMul <= 96)
    xPhaseMulStep = random8(4) + 1;
  if (xPhaseMul >= 8 * 128)
    xPhaseMulStep = -random8(4) - 1;

  uint16_t pixelHue = startHue;
  for (uint16_t i = 0; i < 384; i++) {
    uint16_t x = 32767 + cos16(xPhase + i * xPhaseMul);
    uint16_t y = 32767 + sin16(i * yPhaseMul);
    x /= 256 / (kMatrixWidth - 1);
    y /= 256 / (kMatrixHeight - 1);
    CRGB col = ColorFromPalette(RainbowStripeColors_p, pixelHue >> 8, 255, LINEARBLEND);
    wu_pixel(x, y, col);
    pixelHue += 128;
  }
  FastLED.show();
  FastLED.clear();
}

void wu_pixel(uint16_t x, uint16_t y, CRGB &col) {
  // extract the fractional parts and derive their inverses
  uint8_t xx = x & 0xff, yy = y & 0xff, ix = 255 - xx, iy = 255 - yy;
  // calculate the intensities for each affected pixel
  #define WU_WEIGHT(a, b) ((uint8_t)(((a) * (b) + (a) + (b)) >> 8))
  uint8_t wu[4] = {WU_WEIGHT(ix, iy), WU_WEIGHT(xx, iy),
                   WU_WEIGHT(ix, yy), WU_WEIGHT(xx, yy)
                  };
  #undef WU_WEIGHT
  // multiply the intensities by the colour, and saturating-add them to the pixels
  for (uint8_t i = 0; i < 4; i++) {
    uint8_t local_x = (x >> 8) + (i & 1);
    uint8_t local_y = (y >> 8) + ((i >> 1) & 1);
    if (uint16_t xy = XY(local_x, local_y); xy < NUM_LEDS)
      leds[xy] += col % wu[i];
  }
}