From fc0c0fdbaa094de2e63619193bc611f75b5e5df5 Mon Sep 17 00:00:00 2001 From: Adelin Dobre Date: Mon, 23 Jul 2018 14:44:32 +0300 Subject: [PATCH] BMP: Add string based constructor Signed-off-by: Adelin Dobre Signed-off-by: Mihai Tudor Panu --- src/bmpx8x/bmpx8x.cxx | 73 +++++++++++++++++++++++++++++++++++++++++++ src/bmpx8x/bmpx8x.hpp | 9 ++++++ 2 files changed, 82 insertions(+) diff --git a/src/bmpx8x/bmpx8x.cxx b/src/bmpx8x/bmpx8x.cxx index ac1eb0bc..a715ae5c 100644 --- a/src/bmpx8x/bmpx8x.cxx +++ b/src/bmpx8x/bmpx8x.cxx @@ -34,6 +34,7 @@ #include #include "bmpx8x.hpp" +#include "upm_string_parser.hpp" using namespace upm; using namespace std; @@ -46,6 +47,78 @@ BMPX8X::BMPX8X (int bus, int addr) : + ": bmpx8x_init() failed"); } +BMPX8X::BMPX8X(std::string initStr) : mraaIo(initStr) +{ + mraa_io_descriptor* descs = mraaIo.getMraaDescriptors(); + std::vector upmTokens; + + if(!mraaIo.getLeftoverStr().empty()) { + upmTokens = UpmStringParser::parse(mraaIo.getLeftoverStr()); + } + + m_bmpx8x = (bmpx8x_context)malloc(sizeof(struct _bmpx8x_context)); + if(!m_bmpx8x) { + throw std::runtime_error(std::string(__FUNCTION__) + + ": bmpx8x_init() failed"); + } + + // zero out context + memset((void *)m_bmpx8x, 0, sizeof(struct _bmpx8x_context)); + + // make sure MRAA is initialized + int mraa_rv; + if ((mraa_rv = mraa_init()) != MRAA_SUCCESS) { + bmpx8x_close(m_bmpx8x); + throw std::runtime_error(std::string(__FUNCTION__) + + ": mraa_init() failed"); + } + + if(!descs->i2cs) { + throw std::runtime_error(std::string(__FUNCTION__) + + ": mraa_i2c_init() failed"); + } else { + if( !(m_bmpx8x->i2c = descs->i2cs[0]) ) { + bmpx8x_close(m_bmpx8x); + throw std::runtime_error(std::string(__FUNCTION__) + + ": mraa_i2c_init() failed"); + } + } + + // check the chip id + uint8_t chipID = bmpx8x_get_chip_id(m_bmpx8x); + + if (chipID != BMPX8X_DEFAULT_CHIPID) { + printf("%s: invalid chip id: %02x. Expected %02x\n", + __FUNCTION__, chipID, BMPX8X_DEFAULT_CHIPID); + bmpx8x_close(m_bmpx8x); + throw std::runtime_error(string(__FUNCTION__) + + ": bmpx8x_init() failed"); + } + + // call devinit with a default ultrahigh resolution mode + if (bmpx8x_devinit(m_bmpx8x, BMPX8X_OSS_ULTRAHIGHRES)) { + printf("%s: bmpx8x_devinit() failed.\n", __FUNCTION__); + bmpx8x_close(m_bmpx8x); + throw std::runtime_error(string(__FUNCTION__) + + ": bmpx8x_init() failed"); + } + + std::string::size_type sz; + for(std::string tok:upmTokens) { + if(tok.substr(0,16) == "setOversampling:") { + BMPX8X_OSS_T oss = (BMPX8X_OSS_T)std::stoi(tok.substr(16),nullptr,0); + setOversampling(oss); + } + if(tok.substr(0,9) == "writeReg:") { + uint8_t reg = std::stoi(tok.substr(9),&sz,0); + tok = tok.substr(9); + uint8_t val = std::stoi(tok.substr(sz+1),nullptr,0); + writeReg(reg, val); + } + } +} + + BMPX8X::~BMPX8X() { bmpx8x_close(m_bmpx8x); diff --git a/src/bmpx8x/bmpx8x.hpp b/src/bmpx8x/bmpx8x.hpp index 26c387dd..4657bb07 100644 --- a/src/bmpx8x/bmpx8x.hpp +++ b/src/bmpx8x/bmpx8x.hpp @@ -32,6 +32,7 @@ #include "bmpx8x.h" +#include "mraa/initio.hpp" #include namespace upm { @@ -79,6 +80,13 @@ namespace upm { BMPX8X(int bus=BMPX8X_DEFAULT_I2C_BUS, int addr=BMPX8X_DEFAULT_I2C_ADDR); + /** + * Instantiates BMP Atmospheric Pressure Sensor based on a given string. + * + * @param initStr string containing specific information for BMP Atmospheric Pressure Sensor. + */ + BMPX8X(std::string initStr); + /** * BMPX8X object destructor. */ @@ -212,6 +220,7 @@ namespace upm { protected: // our underlying C context. bmpx8x_context m_bmpx8x; + mraa::MraaIo mraaIo; /** * Read a register.