diff --git a/src/bmpx8x/bmpx8x.cxx b/src/bmpx8x/bmpx8x.cxx index 36144907..e2a0ef91 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 680c2b57..ebbb00bb 100644 --- a/src/bmpx8x/bmpx8x.hpp +++ b/src/bmpx8x/bmpx8x.hpp @@ -32,6 +32,7 @@ #include "bmpx8x.h" +#include "mraa/initio.hpp" #include "interfaces/iPressureSensor.hpp" #include "interfaces/iTemperatureSensor.hpp" @@ -80,6 +81,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. */ @@ -213,6 +221,7 @@ namespace upm { protected: // our underlying C context. bmpx8x_context m_bmpx8x; + mraa::MraaIo mraaIo; /** * Read a register.