/*************************************************** 
  This is an example for the SHT4x Humidity & Temp Sensor

  Designed specifically to work with the SHT4x sensor from Adafruit
  ----> https://www.adafruit.com/products/4885

  These sensors use I2C to communicate, 2 pins are required to  
  interface
 ****************************************************/

#include "pins11.h"
#include "H:\application-specific\generic C compiler\DateFile.c"

#include "G:\source code\Teensy\home control\HC4\shared\enumerations.inc"

// ********************************************************** Infrastructure
#include "utilities\infrastructure.h"
tInfrastructure Infrastructure;

// ********************************************************** TeensyView
#include "utilities\RRFTeensyView.h"
tRRFTeensyView TV;

// ********************************************************** ILI9341 display
#include "SPI.h"
#include "utilities\TFTDisplay.h"
tTFTDisplay ILI9341Display;

// ********************************************************** generic display
#include "utilities\Display.h"
tDisplay Display;

// ********************************************************** DS1307 RTC
#include "utilities\DS1307.h"
tDS1307 DS1307;

// ********************************************************** file system
#include "utilities\FileSystem.h"
tFileSystem FileSystem;

// ********************************************************** SHT45
#include "Adafruit_SHT4x.h"
Adafruit_SHT4x sht4 = Adafruit_SHT4x();

void setup()
  { char Message[100];
    sensors_event_t humidity, temp;
    
    Serial.begin(115200);
    while (!Serial)
      delay(10);     // will pause Zero, Leonardo, etc until serial console opens

    Infrastructure.setup();
    delay(10 * msOneSecond);
    // Serial.printf("bsHeatIntent = %d, bsCoolIntent = %d\n", bsHeatIntent, bsCoolIntent);
    if (Infrastructure.UsingTFT320x240)
      { Infrastructure.SayInIDE("-- setup started using TFT320x240--"); }
    // else if (Infrastructure.UsingST7796)
    //   { Infrastructure.SayInIDE("---- setup started using ST7796----"); }
    Display.setup();
    Infrastructure.SayInIDE("back from Display.setup");
    DS1307.setup();
    DS1307.BigTimeScreen("rebooted");
    FileSystem.setup();

    Serial.println("Adafruit SHT4x test");
    if (sht4.begin(&Wire2))
      { // found
        Serial.println("Found SHT4x sensor");
        Serial.print("Serial number 0x");
        Serial.println(sht4.readSerial(), HEX);

        sht4.setPrecision(SHT4X_HIGH_PRECISION); // higher precision takes longer
        switch (sht4.getPrecision())
          { case SHT4X_HIGH_PRECISION: Serial.println("High precision"); break;
           case SHT4X_MED_PRECISION:   Serial.println("Med precision");  break;
           case SHT4X_LOW_PRECISION:   Serial.println("Low precision");  break;
          } // switch on precision

        sht4.setHeater(SHT4X_NO_HEATER); // higher heat and longer times uses more power
        switch (sht4.getHeater())
          { case SHT4X_NO_HEATER:         Serial.println("No heater");                  break;
            case SHT4X_HIGH_HEATER_1S:    Serial.println("High heat for 1 second");     break;
            case SHT4X_HIGH_HEATER_100MS: Serial.println("High heat for 0.1 second");   break;
            case SHT4X_MED_HEATER_1S:     Serial.println("Medium heat for 1 second");   break;
            case SHT4X_MED_HEATER_100MS:  Serial.println("Medium heat for 0.1 second"); break;
            case SHT4X_LOW_HEATER_1S:     Serial.println("Low heat for 1 second");      break;
            case SHT4X_LOW_HEATER_100MS:  Serial.println("Low heat for 0.1 second");    break;
          } // switch on heat

          sht4.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
          Serial.printf("T %.2f RH %.2f\n", temp.temperature, humidity.relative_humidity);
          sprintf(Message, "%02d%02d:%02d T %.2f RH %.2f",
                             DS1307.Hour(), DS1307.Minute(), DS1307.Second(),
                             temp.temperature, humidity.relative_humidity);
          Display.NLPrintString(Message);
          for (int I = 1; I < 5; I++)
           { sht4.setHeater(SHT4X_HIGH_HEATER_1S); // higher heat and longer times uses more power
             Display.NLPrintString("heat high 1 s");
             Serial.println("heat high 1 s");
             sht4.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
             DS1307.ReadDS1307();
             sprintf(Message, "%02d%02d:%02d T %.2f RH %.2f",
                             DS1307.Hour(), DS1307.Minute(), DS1307.Second(),
                             temp.temperature, humidity.relative_humidity);
             Display.NLPrintString(Message);
             Serial.printf("T %.2f RH %.2f\n", temp.temperature, humidity.relative_humidity);
             delay(msOneMinute);
           }
         sht4.setHeater(SHT4X_NO_HEATER); // higher heat and longer times uses more power
        
         while (true)
           { sht4.getEvent(&humidity, &temp);// populate temp and humidity objects with fresh data
             DS1307.ReadDS1307();
             sprintf(Message, "%02d%02d:%02d T %.2f RH %.2f",
                              DS1307.Hour(), DS1307.Minute(), DS1307.Second(),
                              temp.temperature, humidity.relative_humidity);
             Display.NLPrintString(Message);
             Serial.printf("T %.2f RH %.2f\n", temp.temperature, humidity.relative_humidity);

             delay(2 * msOneMinute);
           } // loop      
      } // found
    else
      { Serial.println("Couldn't find SHT4x"); }
  } // setup


void loop()
  {}
