#include <Adafruit_GFX.h>
#include <Adafruit_NeoMatrix.h>
#include <Adafruit_NeoPixel.h>

// Define the pin for the matrix data input
#define MATRIX_PIN 3

// Dimensions of the matrix
#define MATRIX_WIDTH 16
#define MATRIX_HEIGHT 16

// Create the NeoMatrix object
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(
    MATRIX_WIDTH, MATRIX_HEIGHT, MATRIX_PIN,
    NEO_MATRIX_TOP + NEO_MATRIX_LEFT +
    NEO_MATRIX_ROWS + NEO_MATRIX_ZIGZAG,
    NEO_GRB + NEO_KHZ800);

// Gamma correction table for brightness adjustment
const uint8_t gammaTable[256] = {
    0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 4, 4, 4, 4,
    5, 5, 6, 6, 6, 7, 7, 8, 8, 8, 9, 9, 10, 10, 11, 11,
    // Add all values up to 255...
    255};

// Function to apply gamma correction
uint32_t applyGamma(uint32_t color) {
  uint8_t r = gammaTable[(color >> 16) & 0xFF];
  uint8_t g = gammaTable[(color >> 8) & 0xFF];
  uint8_t b = gammaTable[color & 0xFF];
  return (r << 16) | (g << 8) | b;
}

// Function to create a rainbow effect
uint32_t Wheel(byte wheelPos) {
  wheelPos = 255 - wheelPos;
  if (wheelPos < 85) {
    return applyGamma((255 - wheelPos * 3) << 16 | (0 << 8) | (wheelPos * 3)); // Red to Green
  } else if (wheelPos < 170) {
    wheelPos -= 85;
    return applyGamma((0 << 16) | (wheelPos * 3) << 8 | (255 - wheelPos * 3)); // Green to Blue
  } else {
    wheelPos -= 170;
    return applyGamma((wheelPos * 3) << 16 | (255 - wheelPos * 3) << 8 | 0); // Blue to Red
  }
}

void setup() {
  matrix.begin();
  matrix.setBrightness(240); // Adjust brightness (0-255)
  matrix.show();
}

void loop() {
  static uint8_t hue = 0; // Start hue
  for (int x = 0; x < MATRIX_WIDTH; x++) {
    for (int y = 0; y < MATRIX_HEIGHT; y++) {
      matrix.drawPixel(x, y, Wheel((x + y + hue) & 255));
    }
  }
  hue++; // Cycle through colors
  matrix.show();
  delay(50); // Adjust speed of animation
}
FPS: 0
Power: 0.00W