From 93196d62cbf7006d75dbc1846ea5fe8c642ea006 Mon Sep 17 00:00:00 2001 From: Adelin Dobre Date: Wed, 11 Jul 2018 17:36:25 +0300 Subject: [PATCH] APDS9930: Add string based constructor for Digital Proximity and Ambient Light Sensor Signed-off-by: Adelin Dobre Signed-off-by: Mihai Tudor Panu --- src/apds9930/apds9930.cxx | 41 ++++++++++++++++++++++++++++++++++++++- src/apds9930/apds9930.hpp | 8 ++++++++ 2 files changed, 48 insertions(+), 1 deletion(-) diff --git a/src/apds9930/apds9930.cxx b/src/apds9930/apds9930.cxx index 085ee923..a9be920e 100644 --- a/src/apds9930/apds9930.cxx +++ b/src/apds9930/apds9930.cxx @@ -27,6 +27,7 @@ #include #include "apds9930.hpp" +#include "upm_string_parser.hpp" using namespace upm; @@ -39,6 +40,44 @@ APDS9930::APDS9930(int device) } } +APDS9930::APDS9930(std::string initStr) : mraaIo(initStr) +{ + mraa_io_descriptor* descs = mraaIo.getMraaDescriptors(); + + if(!descs->iios) + { + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_iio_init() failed"); + } + else + { + if(!(m_iio = descs->iios[0])) + { + throw std::invalid_argument(std::string(__FUNCTION__) + + ": mraa_iio_init() failed"); + + } + } + + std::vector upmTokens; + + if (!mraaIo.getLeftoverStr().empty()) { + upmTokens = UpmStringParser::parse(mraaIo.getLeftoverStr()); + } + + for (std::string tok : upmTokens) { + if(tok.substr(0,16) == "enableProximity:") { + bool enable = std::stoi(tok.substr(16),nullptr,0); + enableProximity(enable); + } + if(tok.substr(0,18) == "enableIlluminance:") { + bool enable = std::stoi(tok.substr(18),nullptr,0); + enableIlluminance(enable); + } + } +} + + APDS9930::~APDS9930() { if (m_iio) @@ -81,4 +120,4 @@ APDS9930::enableIlluminance(bool enable) mraa_iio_write_int(m_iio, "in_illuminance_en", 0); return true; -} \ No newline at end of file +} diff --git a/src/apds9930/apds9930.hpp b/src/apds9930/apds9930.hpp index 38a46e0f..5d0203c7 100644 --- a/src/apds9930/apds9930.hpp +++ b/src/apds9930/apds9930.hpp @@ -25,6 +25,7 @@ #include #include +#include namespace upm { @@ -60,6 +61,12 @@ class APDS9930 * @param device iio device number */ APDS9930(int device); + /** + * Instantiates APDS9930 Digital Proximity and Ambient Light Sensor based on a given string. + * + * @param initStr string containing specific information for APDS9930 initialization. + */ + APDS9930(std::string initStr); /** * APDS9930 destructor */ @@ -89,5 +96,6 @@ class APDS9930 private: mraa_iio_context m_iio; + mraa::MraaIo mraaIo; }; }