//
// source https://github.com/dvarrel/DHT22.git
//
#ifndef TEMPERATUREDHT22_H
#define TEMPERATUREDHT22_H
//
#include <Arduino.h>
//
class CTemperatureDHT22
{
  public:
  enum EErrorDHT22
  {
    NO_ERROR,
    ERROR_TIMING_80,
    ERROR_TIMING_50,
    ERROR_TIMING_BITS,
    ERROR_CRC
  };
  //
  private:
  uint8_t FPinData;
  uint32_t _timer;
  uint64_t _rawData;
  uint16_t _h16bits;
  uint16_t _t16bits;
  uint8_t _crc8bits;
  uint8_t _timing80L = 80;
  uint8_t _timing80H = 80;
  uint8_t _timing50 = 50;
  uint8_t _timingBit0 = 27; //specs for AM2303
  uint8_t _timingBit1 = 70; //specs for AM2303
  bool _firstStart=true;
  EErrorDHT22 FLastError;
  const uint8_t T = 30;
  const uint16_t cSamplingTime = 2100; // ms : dht22 sampling rate ~0.5Hz
  //
  private:
  // @brief measure timings of sensor
  void measureTimings();
  // @brief read the 40bits sensor data
  // @return 0 if no error, num of error instead
  uint8_t readSensor();
  // @brief compute checksum of h & t data
  // @return true if checksum==crc
  bool computeCRC();
  // 
  // @return String with timings, 40bits, and calculate values
  String Debug();

  // @return 40bits of data sensor : h16 + t16 + crc8
  uint64_t GetRawData();
  // @return 40bits in a String ("0" left include)
  String GetRawStringData();
  //
  public:
  CTemperatureDHT22(uint8_t pindata);
  uint8_t GetLastError(void);
  // Open
  void Open(void);  
  // Humidity[%]
  float GetHumidityP();
  // Temperature[]
  float GetTemperatureC(void);
  // Temperature[]
  float GetTemperatureF(void);
  // Temperature[]
  float GetTemperatureK(void);
  // HeatIndex[F] | https://www.weather.gov/media/ffc/ta_htindx.PDF
  float GetHeatIndexC(float tc, float h);
  // HeatIndex[F]
  float GetHeatIndexF(float tf, float h);
  // C -> F / K
  // F -> C / K
  // K -> C / F
  float ConvertCtoF(float);
  float ConvertCtoK(float);
  float ConvertFtoC(float);
  float ConvertFtoK(float);
  float ConvertKtoC(float);
  float ConvertKtoF(float);
};

//
#endif // TEMPERATUREDHT22_H
//
