#ifndef BIWAK_I2C_SLAVE_H
#define BIWAK_I2C_SLAVE_H
//----------------------------------------------------------------------------
///
/// \brief  Generic i2c node
///
///         See implementation files for further information.
///
/// \date   20190919
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


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


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

//#include <biwak/output.hpp>
//#include <biwak/spi.hpp>
#include <biwak/i2c.hpp>
#include <lepto/log.h>
#include <lepto/lepto.h>

#if ! defined ( CONFIG_BIWAK_I2C_NODE_ABSENT )
   #define CONFIG_BIWAK_I2C_NODE_ABSENT         1
#endif


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


class CI2cSlave
{
   public:
      enum class EAddressSize
      {
         _8Bits = 0,
         _16Bits = 1,
      };

   private:
      const CI2c     &m_i2c;
      int m_slaveAddress;
      EAddressSize   m_addressSize;
      #if IS_ENABLED( CONFIG_BIWAK_I2C_NODE_ABSENT )
         bool m_present;
      #endif

   public:
      CI2cSlave( const CI2c &_i2c, int address );
      ~CI2cSlave();
      int getSlaveAddress() const
      {
         return(m_slaveAddress);
      }
      void setSlaveAddress( int address )
      {
         m_slaveAddress=address;
         return;
      }
      void setAddressWidth( EAddressSize size )
      {
         m_addressSize=size;
         return;
      }
      EAddressSize detectAddressWidth( char firstChar )
      {
         char buf;
         setAddressWidth( CI2cSlave::EAddressSize::_8Bits );
         i2cReadData(0x0, &buf, 1);
         if(buf==firstChar)
         {
            lDebug("I2C: DTCT 8Bit");
            return( CI2cSlave::EAddressSize::_8Bits );
         }
         setAddressWidth( CI2cSlave::EAddressSize::_16Bits );
         i2cReadData(0x0, &buf, 1);
         if(buf==firstChar)
         {
            lDebug("I2C: DTCT 16Bit");
            return( CI2cSlave::EAddressSize::_16Bits );
         }
         lCritical( LDS("CNDW (0x%x)", "Could not detect address width"), buf);
         setAddressWidth( CI2cSlave::EAddressSize::_8Bits );
         return( CI2cSlave::EAddressSize::_8Bits );
      }
      EAddressSize getAddressWidth( )
      {
         return ( m_addressSize );
      }
      int i2cReadData(uint16_t MemAddress, void* data, int size) const;
      int i2cWriteData(uint16_t MemAddress, const void* data, int size) const;
      int i2cReadData(void* data, int size ) const;
      int i2cWriteData(void* data, int size) const;
      
      #if IS_ENABLED( CONFIG_BIWAK_I2C_NODE_ABSENT )
         void setPresent( bool present )
         {
            m_present=present;
         };
         bool isPresent(  ) const
         {
            return( m_present );
         };
      #endif
      const CI2c& getBus()
      {
         return( m_i2c );
      }
      int waitBusy();
};


//---fin----------------------------------------------------------------------
#endif // ? ! BIWAK_I2C_SLAVE_H
