#ifndef _TEMP_MAX31855_H_
#define _TEMP_MAX31855_H_
//----------------------------------------------------------------------------
///
/// \brief  An Maxim MAX31855 Thermocouple-to-Digital Converter
///
///         "MAX31855 Cold-Junction Compensated Thermocouple-to-Digital Converter"
///         See implementation for further information.
///
//----------------------------------------------------------------------------


//---Includes-----------------------------------------------------------------


//---Own------------------------------

#include <biwak/input.h>
#include <biwak/spi.h>
#include <biwak/output.h>
#include <biwak/temperature.h>
#include <lepto/time.h>          // USEC_PER_MSEC
#include <lepto/ring.h>

#error "Obsolete! use max31855.h"

//---Declaration--------------------------------------------------------------


/// \brief  an TSIC digital temperature sensor
///
///         CInput already is an interrupt (Exti), so we just have to overload
///         the interrupt handler.
class CTempMax31855
{
   private:
      //ETsicError eTsicError;
      CSpi &m_spi;
      CGpio m_outputCs;
      hrtimer_t readTimer;
      int   m_temperature;
      int   m_refTemperature;
      int m_status;
      int m_calibration;

   public:

      CTempMax31855(CSpi &spi, EGpioPeripheral eGpioPeripheralCS, EGpioPin eGpioPinCS)
      :m_spi(spi)
      ,m_outputCs(eGpioPeripheralCS, eGpioPinCS)
      ,readTimer(0)
      ,m_status(2)
      ,m_calibration(0)
      {
         // Test with size 8; Delta 1.094°C after 2 minute; thats ok
         // Test with size 16; Delta 0.625°C after 2 minute
         //equalRing.setVolatile(true);
      }
      ~CTempMax31855()
      {

      }

      int32_t acquireTemperature()
      {
         int rawValue, mTemp;
         bool fault;

         m_outputCs.switchOff();

         //usleep(150);
         m_spi.setDataSize(8);
         rawValue=m_spi.readData32();
         // reverse byte order
         rawValue=__REV(rawValue);
         m_outputCs.switchOn();
         //usleep(150);
         //printf("RAW: 0x%X\n", rawValue);

         fault=(rawValue >> 16) & 1;
         mTemp=(rawValue >> 18);
         mTemp*=(100/4);
         m_temperature=mTemp;

         //Reference Junction Temperature
         mTemp=(rawValue >> 4) & 0xFFF;
         mTemp*=(100/16);
         m_refTemperature=mTemp;

         m_status=fault ? 1 : 0;

         m_temperature+=m_calibration;

         if(m_status)
            m_temperature=0;

         return(m_temperature);
      }

      int32_t getTemperature()
      {
         //if( elapsed(&readTimer, 1 * 250 * USEC_PER_MSEC) )
         //if( elapsed(&readTimer, 1 * 500 * USEC_PER_MSEC) )
         {
            acquireTemperature();
         }

         return( m_temperature );
      }

      int32_t getReferenceTemperature()
      {
         return( m_refTemperature );
      }

      void setCalibration(int calibration)
      {
          m_calibration=calibration;
      }

      int getStatus()
      {
         return(m_status);
      }
};


//---fin----------------------------------------------------------------------
#endif // ? ! _TEMP_MAX31855_H_
