#pragma once
#include <Arduino.h>

void get_lux(float *lux)            
{
    Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
    int nbytes;
    sensors_event_t event;

    #ifdef DEBUG
        unsigned long function_start = millis(); 
    #endif

    tsl.enableAutoRange(true); 
    // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);
    tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);

    bool tsl_ok = tsl.getEvent(&event);

    if (tsl_ok)
    {
        if (event.light)
        {
            *lux = event.light;
            if (*lux > 65535) *lux = -99.9f; // fault
        } else
        {
            /* If event.light = 0 lux the sensor is probably saturated
            and no reliable data could be generated! */
            Serial.printf("[%s]: LUX overload=%d\n",__func__,event.light);
            *lux = -99.9f; // HomeKit does not accept ZERO as valid value, <0 indicates in my code as ERROR
        }        
    } else 
    {
        Serial.printf("[%s]: TSL2561 not working ... Check your wiring or I2C ADDR!\n",__func__);
        *lux = -99.9f;
    }
}