#include "tcn75a.h"

void
i2c_tcn75a::Initialize(unsigned port_addr, unsigned s_addr)
{
  i2c_port::Initialize(port_addr, TCN75ADDR + (s_addr << 1));
}

bool
i2c_tcn75a::ModeSet(unsigned char new_mode)
{
  bool ok = false;
  Start();
  if (ByteSent(Geti2cAddr()) + I2C_WRITE) {
    if (ByteSent(TCN75CONFIG)) {
      if (ByteSent(new_mode)) {
        ok = true;
      }
    }
  }
  Stop();
  return ok;
}

bool
i2c_tcn75a::LimitSet(unsigned new_limit)
{
  bool ok = false;
  Start();
  if (ByteSent(Geti2cAddr() + I2C_WRITE))
    if (ByteSent(TCN75LIMIT)) 
      if (ByteSent(new_limit >> 8)) 
        if (ByteSent(new_limit & 0xff)) 
          ok = true;
  Stop();
  return ok;
}

bool
i2c_tcn75a::HysteresisSet(unsigned new_hyst)
{
  bool ok = false;
  Start();
  if (ByteSent(Geti2cAddr() + I2C_WRITE))
    if (ByteSent(TCN75HYST))
      if (ByteSent(new_hyst >> 8))
        if (ByteSent(new_hyst & 0xff))
          ok = true;
  Stop();
}

bool i2c_tcn75a::TemperatureRead(unsigned *temp)
{
  unsigned char msb, lsb;
  bool ok = false;
  Start();
  if (ByteSent(Geti2cAddr() + I2C_WRITE)) {
    if (ByteSent(TCN75TEMP)) {	// Select temperature register
      Stop();
      Start();
      if (ByteSent(Geti2cAddr() + I2C_READ))
        if (ByteReceived(&msb, true))
          if (ByteReceived(&lsb, false)) {
            *temp = msb * 256 + lsb;
            ok = true;
          }
    }
  }
  Stop();
  return ok;
}
