mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
BMP: Add string based constructor
Signed-off-by: Adelin Dobre <adelin.dobre@rinftech.com>
This commit is contained in:
parent
4cb4cf9020
commit
74f87ce082
@ -34,6 +34,7 @@
|
||||
#include <stdlib.h>
|
||||
|
||||
#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<std::string> 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);
|
||||
|
@ -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.
|
||||
|
Loading…
x
Reference in New Issue
Block a user