ABP: Add string based constructor for Pressure Sensor

This commit is contained in:
Adelin Dobre 2018-06-28 16:44:57 +03:00 committed by Stefan Andritoiu
parent 5c66bfc08d
commit 317a780161
2 changed files with 70 additions and 2 deletions

View File

@ -26,6 +26,7 @@
#include <string> #include <string>
#include <stdexcept> #include <stdexcept>
#include "upm_string_parser.hpp"
#include "abp.hpp" #include "abp.hpp"
using namespace upm; using namespace upm;
@ -38,6 +39,64 @@ ABP::ABP(int bus, int devAddress) :
": abp_init failed"); ": abp_init failed");
} }
ABP::ABP(std::string initStr) : mraaIo(initStr)
{
mraa_io_descriptor* descs = mraaIo.getMraaDescriptors();
std::vector<std::string> 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::~ABP()
{ {
abp_close(m_abp); abp_close(m_abp);

View File

@ -21,9 +21,11 @@
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/ */
#pragma once #pragma once
#include <string>
#include <mraa/initio.hpp>
#include "abp.h" #include "abp.h"
namespace upm { namespace upm {
@ -69,6 +71,12 @@ namespace upm {
* @param devAddress i2c address of the sensor * @param devAddress i2c address of the sensor
*/ */
ABP(int bus, int devAddress); 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 * ABP destructor
*/ */
@ -120,8 +128,9 @@ namespace upm {
*/ */
void setMinPressure(int min); void setMinPressure(int min);
private: private:
mraa::MraaIo mraaIo;
abp_context m_abp; abp_context m_abp;
ABP(const ABP& src) { /* do not create copied constructor */} ABP(const ABP& src) { /* do not create copied constructor */}
ABP& operator=(const ABP&) {return *this;} ABP& operator=(const ABP&) {return *this;}
}; };
} }