#include <FastLED.h>
#include <LiquidCrystal_I2C.h>
//LED Matrix Characteristics
#define LED_Matrix_PIN  3
#define COLOR_ORDER GRB
#define CHIPSET     WS2811
const uint8_t kMatrixWidth = 22;
const uint8_t kMatrixHeight = 22;
#define NUM_LEDS (kMatrixWidth * kMatrixHeight)
CRGB leds_plus_safety_pixel[ NUM_LEDS + 1];
CRGB* const leds( leds_plus_safety_pixel + 1);
//LED Characteristics
#define LED_PIN 11
//LCD Charachteristics
#define LCD_PIN_1 A4
#define LCD_PIN_2 A5
LiquidCrystal_I2C lcd(0x27, LCD_PIN_1, LCD_PIN_2);
//LDR Characteristics
#define LDR_PIN A0
const float GAMMA = 0.7;
const float RL10 = 50;
//Function to transfom LED coordinates in carthesian X-Y
uint16_t XY( uint8_t x, uint8_t y)
{
  uint16_t i;
  if( y & 0x01) {
    //Odd rows run backwards
    uint8_t reverseX = (kMatrixWidth - 1) - x;
    i = (y * kMatrixWidth) + reverseX;
  } else {
    //Even rows run forwards
    i = (y * kMatrixWidth) + x;
  }
  return i;
}
void setup() {
  //Necessary definition (digital pins)
  pinMode(LED_Matrix_PIN, OUTPUT);
  pinMode(LED_PIN, OUTPUT);
  //Optional definition (analog pins) - Not required for functional code
  pinMode(LCD_PIN_1, OUTPUT);
  pinMode(LCD_PIN_2, OUTPUT);
  pinMode(LDR_PIN, INPUT);
  FastLED.addLeds<CHIPSET, LED_Matrix_PIN, COLOR_ORDER>(leds, NUM_LEDS).setCorrection(TypicalSMD5050);
  FastLED.setBrightness(255);
  lcd.init();
  lcd.backlight();
}
void loop() {
  //Reset LEDs at beginning
  FastLED.show();
 
  //Check light intensity
  int analogValue = analogRead(A0);
  float analogValue_mapped = analogValue / 1024. * 255;
  //Compute lux from LDR input
  float voltage = analogValue / 1024. * 5;
  float resistance = 2000 * voltage / (1 - voltage / 5);
  float lux = pow(RL10 * 1e3 * pow(10, GAMMA) / resistance, (1 / GAMMA));
 
  //Command LCD screen 
  lcd.setCursor(0, 0);
  lcd.print("Lux: ");
  lcd.print(lux);
  lcd.print("         ");
  //Indicate lux with LED
  float LED_brightness = map(analogValue_mapped, 0, 255, 255, 0);
  analogWrite(LED_PIN, LED_brightness);
  //Command LED matrix
  for (uint8_t j = 0; j <= (kMatrixWidth - 1); j += 1) {
    for (uint8_t k = 0; k <= (kMatrixHeight - 1); k += 1) {
      leds[XY(j, k)]  = CHSV(analogValue_mapped, 255, 255);
  
    }
  }
}