/*
  HTW Dresden, Fakultät Informatik 
*/

// include the library code:
#include <LiquidCrystal.h>

#include "Wire.h"
#include "SHT2x.h"

#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>


uint32_t start;
uint32_t stop;
SHT2x sht;

Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
        
// initialize the library by associating any needed LCD interface pin
// with the arduino pin number it is connected to
const int rs = 8, en = 9, d4 = 4, d5 = 5, d6 = 6, d7 = 7;

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

int hours, min, sec;



/**************************************************************************/
/*
    Configures the gain and integration time for the TSL2561
*/
/**************************************************************************/
void configureSensor(void)
{
  /* You can also manually set the gain or enable auto-gain support */
  // tsl.setGain(TSL2561_GAIN_1X);      /* No gain ... use in bright light to avoid sensor saturation */
  // tsl.setGain(TSL2561_GAIN_16X);     /* 16x gain ... use in low light to boost sensitivity */
  tsl.enableAutoRange(true);            /* Auto-gain ... switches automatically between 1x and 16x */
  
  /* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
  tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);      /* fast but low resolution */
  // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS);  /* medium resolution and speed   */
  // tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS);  /* 16-bit data but slowest conversions */

  /* Update these values depending on what you've set above! */  
  Serial.println("------------------------------------");
  Serial.print  ("Gain:         "); Serial.println("Auto");
  Serial.print  ("Timing:       "); Serial.println("13 ms");
  Serial.println("------------------------------------");
}



void setup() {
  lcd.begin(16, 2);  // set up the LCD's number of columns and rows:
  lcd.print("T/H: "); // Print a message to the LCD.

  Serial.begin(115200);
  Serial.println(__FILE__);
  Serial.print("SHT2x_LIB_VERSION: \t");
  Serial.println(SHT2x_LIB_VERSION);

  sht.begin();

  uint8_t stat = sht.getStatus();
  lcd.print(stat, HEX);

/* Setup the sensor gain and integration time */
  configureSensor();
  delay(1000);
}

void loop() {
  lcd.setCursor(5, 0);
  // print the number of seconds since reset:
  //lcd.print(millis() / 1000);

  start = micros();
  sht.read();
  stop = micros();

  Serial.print("\t");
  Serial.print(stop - start);
  Serial.print("\t");
  Serial.print(sht.getTemperature() );
  Serial.print("\t");
  Serial.println(sht.getHumidity(), 1);

  lcd.print(sht.getTemperature(), 1);
  lcd.print(" ");
  lcd.print(sht.getHumidity(), 1);
  
  sensors_event_t event;
  tsl.getEvent(&event);
 
  /* Display the results (light is measured in lux) */
  if (event.light)
  {
    Serial.print(event.light); Serial.println(" lux");
    lcd.setCursor(1, 1);
    lcd.print(event.light); lcd.print(" lux   ");
  }

  delay(1000);
}

