From d23183897dc2611c1dd5c3676053265a50ec7650 Mon Sep 17 00:00:00 2001 From: Adelin Dobre Date: Thu, 28 Jun 2018 16:44:57 +0300 Subject: [PATCH] ABP: Add string based constructor for Pressure Sensor Signed-off-by: Mihai Tudor Panu --- src/abp/abp.cxx | 59 +++++++++++++++++++++++++++++++++++++++++++++++++ src/abp/abp.hpp | 13 +++++++++-- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/src/abp/abp.cxx b/src/abp/abp.cxx index b8d7b13e..2aae9bcd 100644 --- a/src/abp/abp.cxx +++ b/src/abp/abp.cxx @@ -26,6 +26,7 @@ #include #include +#include "upm_string_parser.hpp" #include "abp.hpp" using namespace upm; @@ -38,6 +39,64 @@ ABP::ABP(int bus, int devAddress) : ": abp_init failed"); } +ABP::ABP(std::string initStr) : mraaIo(initStr) +{ + mraa_io_descriptor* descs = mraaIo.getMraaDescriptors(); + std::vector upmTokens; + + if(mraaIo.getLeftoverStr() != "") + { + upmTokens = UpmStringParser::parse(mraaIo.getLeftoverStr()); + } + + m_abp = (abp_context)malloc(sizeof(struct _abp_context)); + + if(!m_abp) + { + throw std::runtime_error(std::string(__FUNCTION__) + + ": abp_init failed"); + } + + // make sure MRAA is initialized + int mraa_rv; + if ((mraa_rv = mraa_init()) != MRAA_SUCCESS) + { + throw std::runtime_error(std::string(__FUNCTION__) + + ": mraa_init() failed"); + } + + if(!descs->i2cs) + { + abp_close(m_abp); + throw std::runtime_error(std::string(__FUNCTION__) + + ": mraa_i2c_init() failed"); + } + else + { + if( !(m_abp->i2c = descs->i2cs[0]) ) + { + abp_close(m_abp); + throw std::runtime_error(std::string(__FUNCTION__) + + ": mraa_i2c_init() failed"); + } + } + + // setting up defaults + m_abp->abp_pressure_max = 5; + m_abp->abp_pressure_min = 0; + + for (std::string tok : upmTokens) { + if(tok.substr(0,12) == "maxPressure:") { + int pmax = std::stoi(tok.substr(12),nullptr,0); + setMaxPressure(pmax); + } + if(tok.substr(0,12) == "minPressure:") { + int pmin = std::stoi(tok.substr(12),nullptr,0); + setMinPressure(pmin); + } + } +} + ABP::~ABP() { abp_close(m_abp); diff --git a/src/abp/abp.hpp b/src/abp/abp.hpp index 3ae1ff00..6ff9c07a 100644 --- a/src/abp/abp.hpp +++ b/src/abp/abp.hpp @@ -21,9 +21,11 @@ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ - + #pragma once +#include +#include #include "abp.h" #include @@ -70,6 +72,12 @@ namespace upm { * @param devAddress i2c address of the sensor */ ABP(int bus, int devAddress); + /** + * Instantiates an ABP sensor object based on a given string. + * + * @param initStr string containing specific information for ABP sensor initialization. + */ + ABP(std::string initStr); /** * ABP destructor */ @@ -121,8 +129,9 @@ namespace upm { */ void setMinPressure(int min); private: + mraa::MraaIo mraaIo; abp_context m_abp; ABP(const ABP& src) { /* do not create copied constructor */} ABP& operator=(const ABP&) {return *this;} }; -} \ No newline at end of file +}