/*
{{copyright}} 
*/

/*
{{version}} 
*/

/*
{{license}} 
*/

#pragma once

#include "I2CBus.h"
#include "ICommDevice.h"

namespace sdi_uToolBox::comm
{
//--------------------------------------------------------------
class I2CDevice : public ICommDevice
{
  public:
  I2CDevice() = delete;                                  // Default constructor
  explicit I2CDevice(I2CBus &bus, uint8_t address);      // Constructor
  virtual ~I2CDevice() override           = default;     // Default destructor
  I2CDevice(const I2CDevice &)            = delete;      // Copy constructor
  I2CDevice &operator=(const I2CDevice &) = delete;      // Copy assignment operator
  I2CDevice(I2CDevice &&)                 = delete;      // Move constructor
  I2CDevice &operator=(I2CDevice &&)      = delete;      // Move assignment operator

  ReturnType write(WriteBuf buf, Timeout timeout) override;     // Write data to device
  ReturnType read(ReadBuf buf, Timeout timeout) override;       // Read data from device

  protected:
  I2CBus &m_bus;         // Reference to I2C bus
  uint8_t m_address;     // I2C device address
};
//--------------------------------------------------------------

//--------------------------------------------------------------
/* Constructor */
inline I2CDevice::I2CDevice(I2CBus &bus, const uint8_t address)
  : m_bus(bus)
  , m_address(address)
{
  // Nothing to do
}
//--------------------------------------------------------------
/* Write data to device */
inline ICommDevice::ReturnType I2CDevice::write(const WriteBuf buf, const Timeout timeout)
{
  return m_bus.write(m_address, buf, timeout);
}
//--------------------------------------------------------------
/* Read data from device */
inline ICommDevice::ReturnType I2CDevice::read(const ReadBuf buf, const Timeout timeout)
{
  return m_bus.read(m_address, buf, timeout);
}
//--------------------------------------------------------------
}     // namespace sdi_uToolBox::comm