/***************************************************
  This is a library for the TMP006 Temp Sensor

  Designed specifically to work with the Adafruit TMP006 Breakout
  ----> https://www.adafruit.com/products/1296

  These displays use I2C to communicate, 2 pins are required to
  interface
  Adafruit invests time and resources providing this open source code,
  please support Adafruit and open-source hardware by purchasing
  products from Adafruit!

  Written by Limor Fried/Ladyada for Adafruit Industries.
  BSD license, all text above must be included in any redistribution
 ****************************************************/

#define TMP006_B0 -0.0000294
#define TMP006_B1 -0.00000057
#define TMP006_B2 0.00000000463
#define TMP006_C2 13.4
#define TMP006_TREF 298.15
#define TMP006_A2 -0.00001678
#define TMP006_A1 0.00175
#define TMP006_S0 6.4 // * 10^-14

#if (ARDUINO >= 100)
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#include <Adafruit_Sensor.h>

#ifdef __AVR_ATtiny85__
#include "TinyWireM.h"
#define Wire TinyWireM
#else
#include "Wire.h"
#endif

// uncomment for debugging!
//#define TMP006_DEBUG 1

#define TMP006_CONFIG 0x02

#define TMP006_CFG_RESET 0x8000
#define TMP006_CFG_MODEON 0x7000
#define TMP006_CFG_1SAMPLE 0x0000
#define TMP006_CFG_2SAMPLE 0x0200
#define TMP006_CFG_4SAMPLE 0x0400
#define TMP006_CFG_8SAMPLE 0x0600
#define TMP006_CFG_16SAMPLE 0x0800
#define TMP006_CFG_DRDYEN 0x0100
#define TMP006_CFG_DRDY 0x0080

#define TMP006_I2CADDR 0x40
#define TMP006_MANID 0xFE
#define TMP006_DEVID 0xFF

#define TMP006_VOBJ 0x0
#define TMP006_TAMB 0x01

/**************************************************************************/
/*!
    @brief The class for storing and communicating with TMP006 sensors
*/
/**************************************************************************/
class Adafruit_TMP006 {
public:
  Adafruit_TMP006(uint8_t addr = TMP006_I2CADDR);
  boolean
  begin(uint16_t samplerate = TMP006_CFG_16SAMPLE); // by default go highres

  void
  sleep(); // Put chip into low power mode (disables temperature measurements).
  void wake(); // Wake from low power mode.

  int16_t readRawDieTemperature(void);
  int16_t readRawVoltage(void);
  double readObjTempC(void);
  double readDieTempC(void);

private:
  uint8_t _addr;
  uint16_t read16(uint8_t addr);
  void write16(uint8_t addr, uint16_t data);
};
