mirror of
https://github.com/eclipse/upm.git
synced 2025-03-15 04:57:30 +03:00
CWLSXXA CO2: Add string based constructor
Signed-off-by: Adelin Dobre <adelin.dobre@rinftech.com>
This commit is contained in:
parent
dbd5fcf657
commit
5a42e44c86
@ -25,6 +25,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include "cwlsxxa.hpp"
|
#include "cwlsxxa.hpp"
|
||||||
|
#include "upm_string_parser.hpp"
|
||||||
|
|
||||||
using namespace upm;
|
using namespace upm;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
@ -40,10 +41,11 @@ static float c2f(float c)
|
|||||||
return (c * (9.0 / 5.0) + 32.0);
|
return (c * (9.0 / 5.0) + 32.0);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
CWLSXXA::CWLSXXA(int gPin, int hPin, int tPin, float rResistor, float aref) :
|
CWLSXXA::CWLSXXA(int gPin, int hPin, int tPin, float rResistor, float aref) :
|
||||||
m_aioCO2(gPin), m_aioHum(0), m_aioTemp(0)
|
m_aioCO2(0), m_aioHum(0), m_aioTemp(0)
|
||||||
{
|
{
|
||||||
|
m_aioCO2 = new mraa::Aio(gPin);
|
||||||
|
|
||||||
m_hasHum = (hPin >= 0) ? true : false;
|
m_hasHum = (hPin >= 0) ? true : false;
|
||||||
m_hasTemp = (tPin >= 0) ? true : false;
|
m_hasTemp = (tPin >= 0) ? true : false;
|
||||||
|
|
||||||
@ -63,7 +65,7 @@ CWLSXXA::CWLSXXA(int gPin, int hPin, int tPin, float rResistor, float aref) :
|
|||||||
else
|
else
|
||||||
m_aResHum = 0;
|
m_aResHum = 0;
|
||||||
|
|
||||||
m_aResCO2 = (1 << m_aioCO2.getBit());
|
m_aResCO2 = (1 << m_aioCO2->getBit());
|
||||||
|
|
||||||
m_temperature = 0.0;
|
m_temperature = 0.0;
|
||||||
m_humidity = 0.0;
|
m_humidity = 0.0;
|
||||||
@ -73,12 +75,83 @@ CWLSXXA::CWLSXXA(int gPin, int hPin, int tPin, float rResistor, float aref) :
|
|||||||
m_rResistor = rResistor;
|
m_rResistor = rResistor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CWLSXXA::CWLSXXA(std::string initStr)
|
||||||
|
{
|
||||||
|
mraaIo = new mraa::MraaIo(initStr);
|
||||||
|
if(mraaIo == NULL)
|
||||||
|
{
|
||||||
|
throw std::invalid_argument(std::string(__FUNCTION__) +
|
||||||
|
": Failed to allocate memory for internal member");
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!mraaIo->aios.empty())
|
||||||
|
{
|
||||||
|
m_aioCO2 = &mraaIo->aios[0];
|
||||||
|
|
||||||
|
if(mraaIo->aios.size() > 1)
|
||||||
|
{
|
||||||
|
m_hasTemp = 1;
|
||||||
|
m_aioTemp = &mraaIo->aios[1];
|
||||||
|
m_aResTemp = (1 << m_aioTemp->getBit());
|
||||||
|
|
||||||
|
m_hasHum = 1;
|
||||||
|
m_aioHum = &mraaIo->aios[2];
|
||||||
|
m_aResHum = (1 << m_aioHum->getBit());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
m_hasTemp = 0;
|
||||||
|
m_aResTemp = 0;
|
||||||
|
m_hasHum = 0;
|
||||||
|
m_aResHum = 0;
|
||||||
|
|
||||||
|
throw std::invalid_argument(std::string(__FUNCTION__) +
|
||||||
|
": mraa_aio_init() failed, invalid pin?");
|
||||||
|
}
|
||||||
|
|
||||||
|
m_aResCO2 = (1 << m_aioCO2->getBit());
|
||||||
|
|
||||||
|
m_temperature = 0.0;
|
||||||
|
m_humidity = 0.0;
|
||||||
|
m_co2 = 0.0;
|
||||||
|
|
||||||
|
std::vector<std::string> upmTokens;
|
||||||
|
|
||||||
|
if(!mraaIo->getLeftoverStr().empty())
|
||||||
|
{
|
||||||
|
upmTokens = UpmStringParser::parse(mraaIo->getLeftoverStr());
|
||||||
|
}
|
||||||
|
|
||||||
|
for(std::string tok:upmTokens)
|
||||||
|
{
|
||||||
|
if(tok.substr(0,5) == "aref:")
|
||||||
|
{
|
||||||
|
float aref = std::stof(tok.substr(5));
|
||||||
|
m_aref = aref;
|
||||||
|
}
|
||||||
|
if(tok.substr(0,10) == "rResistor:")
|
||||||
|
{
|
||||||
|
float rResistor = std::stof(tok.substr(10));
|
||||||
|
m_rResistor = rResistor;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
CWLSXXA::~CWLSXXA()
|
CWLSXXA::~CWLSXXA()
|
||||||
{
|
{
|
||||||
if (m_aioHum)
|
if(mraaIo)
|
||||||
delete m_aioHum;
|
delete mraaIo;
|
||||||
if (m_aioTemp)
|
else
|
||||||
delete m_aioTemp;
|
{
|
||||||
|
delete m_aioCO2;
|
||||||
|
|
||||||
|
if (m_aioHum)
|
||||||
|
delete m_aioHum;
|
||||||
|
if (m_aioTemp)
|
||||||
|
delete m_aioTemp;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void CWLSXXA::update()
|
void CWLSXXA::update()
|
||||||
@ -137,9 +210,9 @@ void CWLSXXA::update()
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CO2
|
// CO2
|
||||||
val = average(&m_aioCO2, samples);
|
val = average(m_aioCO2, samples);
|
||||||
volts = (float(val) * (m_aref / m_aResCO2));
|
volts = (float(val) * (m_aref / m_aResCO2));
|
||||||
|
|
||||||
// CO2 range is 0-2000ppm
|
// CO2 range is 0-2000ppm
|
||||||
if (!m_rResistor)
|
if (!m_rResistor)
|
||||||
m_co2 = ((volts / m_aref) * 2000.0);
|
m_co2 = ((volts / m_aref) * 2000.0);
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
||||||
#include <mraa/aio.hpp>
|
#include <mraa/aio.hpp>
|
||||||
|
#include <mraa/initio.hpp>
|
||||||
|
|
||||||
// Unlikey to be changable without external circuitry (voltage divider)
|
// Unlikey to be changable without external circuitry (voltage divider)
|
||||||
#define CWLSXXA_DEFAULT_AREF 5.0
|
#define CWLSXXA_DEFAULT_AREF 5.0
|
||||||
@ -111,6 +112,13 @@ namespace upm {
|
|||||||
CWLSXXA(int gPin, int hPin, int tPin, float rResistor=0.0,
|
CWLSXXA(int gPin, int hPin, int tPin, float rResistor=0.0,
|
||||||
float aref=CWLSXXA_DEFAULT_AREF);
|
float aref=CWLSXXA_DEFAULT_AREF);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Instantiates CWLSXXA CO2 Sensors based on a given string.
|
||||||
|
*
|
||||||
|
* @param initStr string containing specific information for CWLSXXA Sensor.
|
||||||
|
*/
|
||||||
|
CWLSXXA(std::string initStr);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* CWLSXXA object destructor
|
* CWLSXXA object destructor
|
||||||
*/
|
*/
|
||||||
@ -157,11 +165,13 @@ namespace upm {
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
// CO2 reporting is always supported
|
// CO2 reporting is always supported
|
||||||
mraa::Aio m_aioCO2;
|
mraa::Aio *m_aioCO2 = NULL;
|
||||||
|
|
||||||
// temperature and humidity are optional features of this transmitter
|
// temperature and humidity are optional features of this transmitter
|
||||||
mraa::Aio *m_aioHum;
|
mraa::Aio *m_aioHum = NULL;
|
||||||
mraa::Aio *m_aioTemp;
|
mraa::Aio *m_aioTemp = NULL;
|
||||||
|
|
||||||
|
mraa::MraaIo *mraaIo = NULL;
|
||||||
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user