ds1307: throw exception(s) on fatal errors
Signed-off-by: Jon Trulson <jtrulson@ics.com> Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
committed by
Mihai Tudor Panu
parent
3d837284fd
commit
78d940aaf5
@@ -27,6 +27,7 @@
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <stdexcept>
|
||||
|
||||
#include "ds1307.h"
|
||||
|
||||
@@ -34,20 +35,15 @@ using namespace upm;
|
||||
using namespace std;
|
||||
|
||||
|
||||
DS1307::DS1307(int bus)
|
||||
DS1307::DS1307(int bus) : m_i2c(bus)
|
||||
{
|
||||
// setup our i2c link
|
||||
m_i2c = mraa_i2c_init(bus);
|
||||
|
||||
mraa_result_t ret = mraa_i2c_address(m_i2c, DS1307_I2C_ADDR);
|
||||
|
||||
if (ret != MRAA_SUCCESS)
|
||||
cerr << "DS1307: Could not initialize i2c bus. " << endl;
|
||||
}
|
||||
|
||||
DS1307::~DS1307()
|
||||
{
|
||||
mraa_i2c_stop(m_i2c);
|
||||
mraa::Result ret = m_i2c.address(DS1307_I2C_ADDR);
|
||||
if (ret != mraa::SUCCESS){
|
||||
throw std::invalid_argument(std::string(__FUNCTION__) +
|
||||
": i2c.address() failed");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
mraa::Result DS1307::writeBytes(uint8_t reg, uint8_t *buffer, int len)
|
||||
@@ -65,9 +61,14 @@ mraa::Result DS1307::writeBytes(uint8_t reg, uint8_t *buffer, int len)
|
||||
for (int i=1; i<(len + 1); i++)
|
||||
buf2[i] = buffer[i-1];
|
||||
|
||||
mraa_i2c_address(m_i2c, DS1307_I2C_ADDR);
|
||||
mraa::Result ret = m_i2c.address(DS1307_I2C_ADDR);
|
||||
if (ret != mraa::SUCCESS){
|
||||
throw std::invalid_argument(std::string(__FUNCTION__) +
|
||||
": i2c.address() failed");
|
||||
return ret;
|
||||
}
|
||||
|
||||
return (mraa::Result) mraa_i2c_write(m_i2c, buf2, len + 1);
|
||||
return m_i2c.write(buf2, len + 1);
|
||||
}
|
||||
|
||||
int DS1307::readBytes(uint8_t reg, uint8_t *buffer, int len)
|
||||
@@ -75,10 +76,15 @@ int DS1307::readBytes(uint8_t reg, uint8_t *buffer, int len)
|
||||
if (!len || !buffer)
|
||||
return 0;
|
||||
|
||||
mraa_i2c_address(m_i2c, DS1307_I2C_ADDR);
|
||||
mraa_i2c_write_byte(m_i2c, reg);
|
||||
mraa::Result ret = m_i2c.address(DS1307_I2C_ADDR);
|
||||
if (ret != mraa::SUCCESS){
|
||||
throw std::invalid_argument(std::string(__FUNCTION__) +
|
||||
": i2c.address() failed");
|
||||
return 0;
|
||||
}
|
||||
m_i2c.writeByte(reg);
|
||||
|
||||
return mraa_i2c_read(m_i2c, buffer, len);
|
||||
return m_i2c.read(buffer, len);
|
||||
}
|
||||
|
||||
bool DS1307::loadTime()
|
||||
@@ -90,8 +96,8 @@ bool DS1307::loadTime()
|
||||
if (bytesRead != 7)
|
||||
{
|
||||
// problem
|
||||
cerr << __FUNCTION__ << ": read " << bytesRead <<
|
||||
" bytes, expected 7." << endl;
|
||||
throw std::runtime_error(std::string(__FUNCTION__) +
|
||||
": failed to read expected 7 bytes from device");
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user