ABP: Add string based constructor for Pressure Sensor

Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Adelin Dobre 2018-06-28 16:44:57 +03:00 committed by Mihai Tudor Panu
parent 9ba309bc48
commit d23183897d
2 changed files with 70 additions and 2 deletions

View File

@ -26,6 +26,7 @@
#include <string>
#include <stdexcept>
#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<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_close(m_abp);

View File

@ -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 <string>
#include <mraa/initio.hpp>
#include "abp.h"
#include <interfaces/iTemperature.hpp>
@ -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;}
};
}
}