#ifndef TG_SGP40_H
#define TG_SGP40_H

#include <Wire.h>
#include "../voc.h"


#define SGP40_ADDRESS (0x59)

//////////////////////////////////////////////////////////////////
/// SGP40 - Driver class for SGP40 sensor
///
/// Based on the data sheet provided
///
class SGP40 {
public:

    enum ChipModel {
        ChipModel_UNKNOWN = 0
    };


    SGP40(TwoWire* i2c): m_initialized(false),i2c(i2c) {}

    ChipModel chipModel();

    bool begin();
    bool reset();

    int32_t voc(float RH = 50, float T = 20);
    void read(int32_t& voc, float RH = 50, float T = 20);


protected:
    bool Initialize();


private:
    bool m_initialized;
    ChipModel m_chip_model;
    TwoWire* i2c;
    VocAlgorithmParams vocAlgorithmParameters;

    static const uint16_t REG_MEASURE_RAW = 0x260F;
    static const uint16_t REG_MEASURE_TEST = 0x280E;
    static const uint16_t REG_HEATER_OFF = 0x3615;
    static const uint16_t RESET_ADDR = 0x0006;

    static const uint16_t test_pass = 0xD400;
    static const uint16_t test_fail = 0x4B00;

    bool ReadChipID();

    bool ReadRegister(uint8_t addr,uint8_t data[],uint8_t length);
    bool ReadData(uint8_t* data, float RH, float T);

    bool WriteSettings();
    bool WriteRegister(uint16_t addr,uint8_t data);

    uint8_t CRC8(uint16_t twoBytes);

};

#endif