#include <FastLED.h>

#define LED_PIN         3
#define MATRIX_WIDTH    40
#define MATRIX_HEIGHT   12
#define NUM_LEDS        (MATRIX_WIDTH * MATRIX_HEIGHT)

CRGB leds[NUM_LEDS];

struct Line {
  int x1, y1, x2, y2;
  CRGB color;
};

// Define the number of elements in the lines array
#define NUM_LINES 3

Line lines[NUM_LINES]; // Array to hold multiple lines

void setup() {
  Serial.begin(9600);
  FastLED.addLeds<WS2812, LED_PIN, GRB>(leds, NUM_LEDS);

  // Load example lines into the array
  lines[0] = {5, 10, 35, 2, CRGB::Red};     // Line from (5,5) to (35,5) in Red
  lines[1] = {10, 0, 20, 12, CRGB::Green}; // Line from (10,10) to (30,10) in Green
  lines[2] = {0, 0, 40, 12, CRGB::Blue};  // Line from (15,15) to (25,15) in Blue
}

void loop() {
  // Iterate over all the elements in the lines array
  for (int i = 0; i < NUM_LINES; i++) {
    draw_line(lines[i]); // Draw each line in the lines array
  }
  FastLED.show();
  delay(1000); // Adjust delay time as needed
  FastLED.clear();
}

int XY(int x, int y) {
  return y * MATRIX_WIDTH + ((y % 2 == 0) ? x : MATRIX_WIDTH - 1 - x);
}

void wu_pixel(uint32_t x, uint32_t y, CRGB *col) {
  uint8_t xx = x & 0xff, yy = y & 0xff, ix = 255 - xx, iy = 255 - yy;
  uint8_t wu[4] = {((ix * iy) + ix + iy) >> 8, ((xx * iy) + xx) >> 8,
                   ((ix * yy) + ix + yy) >> 8, ((xx * yy) + xx) >> 8
                  };

  for (uint8_t i = 0; i < 4; i++) {
    uint16_t xy = XY((x >> 8) + (i & 1), (y >> 8) + ((i >> 1) & 1));
    CRGB *led = &leds[xy];
    led->r = qadd8(led->r, col->r * wu[i] >> 8);
    led->g = qadd8(led->g, col->g * wu[i] >> 8);
    led->b = qadd8(led->b, col->b * wu[i] >> 8);
  }
}

void draw_line(Line &line) {
  int x1 = line.x1 << 1;
  int y1 = line.y1 << 1;
  int x2 = line.x2 << 1;
  int y2 = line.y2 << 1;
  CRGB color = line.color;

  int dx = abs(x2 - x1);
  int dy = abs(y2 - y1);
  int sx = x1 < x2 ? 1 : -1;
  int sy = y1 < y2 ? 1 : -1;
  int err = dx - dy;
  int x = x1;
  int y = y1;

  while (true) {
    wu_pixel(x << 7, y << 7, &color);

    if (x == x2 && y == y2)
      break;
    int e2 = 2 * err;
    if (e2 > -dy) {
      err -= dy;
      x += sx;
    }
    if (e2 < dx) {
      err += dx;
      y += sy;
    }
  }
}