diff --git a/src/ds1307/ds1307.cxx b/src/ds1307/ds1307.cxx index b7095dbe..08143d9b 100644 --- a/src/ds1307/ds1307.cxx +++ b/src/ds1307/ds1307.cxx @@ -46,6 +46,27 @@ DS1307::DS1307(int bus) : m_i2c(bus) } } +DS1307::DS1307(std::string initStr) : m_i2c(nullptr), mraaIo(initStr) +{ + mraa_io_descriptor* descs = mraaIo.getMraaDescriptors(); + + if(!descs->i2cs[0]) { + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_i2c_init() failed"); + return; + } else { + m_i2c = descs->i2cs[0]; + } + + // setup our i2c link + 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) { if (!len || !buffer) diff --git a/src/ds1307/ds1307.hpp b/src/ds1307/ds1307.hpp index d5d205cb..bdf85c62 100644 --- a/src/ds1307/ds1307.hpp +++ b/src/ds1307/ds1307.hpp @@ -28,6 +28,7 @@ #include #include +#include #define DS1307_I2C_BUS 0 #define DS1307_I2C_ADDR 0x68 @@ -77,6 +78,13 @@ namespace upm { * @param bus I2C bus to use */ DS1307(int bus); + + /** + * Instantiates DS1307 Clock based on a given string. + * + * @param initStr string containing specific information for DS1307 initialization. + */ + DS1307(std::string initStr); /** * Loads all the time values @@ -188,6 +196,7 @@ namespace upm { private: mraa::I2c m_i2c; + mraa::MraaIo mraaIo; }; }