diff --git a/src/ds1808lc/ds1808lc.cxx b/src/ds1808lc/ds1808lc.cxx index 4e001102..2884e180 100644 --- a/src/ds1808lc/ds1808lc.cxx +++ b/src/ds1808lc/ds1808lc.cxx @@ -4,6 +4,7 @@ #include #include "ds1808lc.hpp" #include "mraa-utils.hpp" +#include "upm_string_parser.hpp" #define DS1808_I2C_ADDR 0x28 #define DS1808_POT2_OFFSET 0x40 @@ -11,6 +12,10 @@ #define DS1808_LOW_VALUE 32 // Lowest pot value that the eye can differentiate from 0 #define DS1808_HIGH_VALUE 51 // Highest pot value that the eye can differentiate from full +static bool operator!(mraa::MraaIo &mraaIo) +{ + return mraaIo.getMraaDescriptors() == NULL; +} namespace upm { DS1808LC::DS1808LC(int gpioPower, int i2cBus) @@ -22,8 +27,41 @@ DS1808LC::DS1808LC(int gpioPower, int i2cBus) getBrightness(); } +DS1808LC::DS1808LC(std::string initStr) : mraaIo(initStr) +{ + std::vector upmTokens; + + mraa_set_log_level(7); + if(!mraaIo.i2cs.empty()) { + i2c = &mraaIo.i2cs[0]; + } else { + throw std::invalid_argument(std::string(__FUNCTION__) + ": I2c.init() failed"); + } + + status = i2c->address(DS1808_I2C_ADDR); + + if(!mraaIo.getLeftoverStr().empty()) { + upmTokens = UpmStringParser::parse(mraaIo.getLeftoverStr()); + } + + for(std::string tok : upmTokens) { + if(tok.substr(0, 10) == "gpioPower:") { + int gpioPower = std::stoi(tok.substr(10), nullptr, 0); + pinPower = gpioPower; + } + if(tok.substr(0, 14) == "setBrightness:") { + int dutyPercent = std::stoi(tok.substr(14), nullptr, 0); + setBrightness(dutyPercent); + } + } + + getBrightness(); +} + DS1808LC::~DS1808LC() { + if(!mraaIo) + delete i2c; } bool DS1808LC::isPowered() diff --git a/src/ds1808lc/ds1808lc.hpp b/src/ds1808lc/ds1808lc.hpp index 6ffc3985..5d0d8f84 100644 --- a/src/ds1808lc/ds1808lc.hpp +++ b/src/ds1808lc/ds1808lc.hpp @@ -21,8 +21,10 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ +#include #include "mraa/i2c.hpp" +#include "mraa/initio.hpp" namespace upm { @@ -56,6 +58,7 @@ class DS1808LC { public: DS1808LC(int gpioPower, int i2cBus); + DS1808LC(std::string initStr); ~DS1808LC(); const char* getModuleName() { return "ds1808lc"; } @@ -73,6 +76,7 @@ private: mraa::Result status; mraa::I2c* i2c; + mraa::MraaIo mraaIo; int pinPower; };