#include <FastLED.h>

// Define LED matrix parameters
#define LED_PIN         3
#define MATRIX_WIDTH    40
#define MATRIX_HEIGHT   12
#define NUM_LEDS        (MATRIX_WIDTH * MATRIX_HEIGHT)

CRGB leds[NUM_LEDS];

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

void loop() {
  for (uint8_t i = 0; i <= MATRIX_WIDTH ; i++) {
    FastLED.clear();
    draw_smooth_line(i << 1, 2 << 1, MATRIX_WIDTH - i << 1, 12 << 1, CRGB::White);
    FastLED.show();
  }
}

// Function to map 2D (x, y) coordinates to 1D LED array index
inline int XY(int x, int y) {
  return y * MATRIX_WIDTH + ((y % 2 == 0) ? x : MATRIX_WIDTH - 1 - x);
}

// Xiaolin Wu's line algorithm for drawing a smoothed line
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);
  }
}

// Function to draw a smoothed line using Xiaolin Wu's algorithm
void draw_smooth_line(int x1, int y1, int x2, int y2, CRGB 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;
    }
  }
}