#include <Wire.h>
#include <MS5611.h>

#include "baro_channel.h"

namespace {

class BaroI2cChannel : public BaroChannel
{
public:
  BaroI2cChannel()
    : BaroChannel("i2c", LED_BARO_I2C_PIN), _driver(BARO_I2C_ADDRESS, &Wire) {}

protected:
  bool driverBegin() override
  {
    if (!_busStarted)
    {
      Wire.begin();
      _busStarted = true;
    }

    _driver.setOversampling(BARO_OVERSAMPLING);

    return _driver.begin();
  }

  void driverReadProm(uint16_t *prom) override
  {
    for (uint8_t i = 0; i < 8; i++) prom[i] = _driver.getProm(i);
  }

  bool  driverRead() override        { return _driver.read() == MS5611_READ_OK; }
  float driverPressure() override    { return _driver.getPressure(); }
  float driverTemperature() override { return _driver.getTemperature(); }

private:
  MS5611 _driver;
  bool   _busStarted = false;
};

BaroI2cChannel instance;

}  // namespace

BaroChannel &baroI2cChannel() { return instance; }
