mhz16: Adding raw path constructor for UART

This commit adds an additional constructor to the MHZ16 which takes a
path (string reference) to a UART device on the filesystem.

    Example: MHZ16("/dev/ttyS0");

    * Added constructor for device path w/default
    * Removed unused global consts from header

Signed-off-by: g-vidal <gerard.vidal@ens-lyon.fr>
Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
g-vidal 2018-04-15 16:06:13 +02:00 committed by Noel Eck
parent 8f63914d76
commit dd73c4aa45
2 changed files with 41 additions and 8 deletions

View File

@ -65,6 +65,38 @@ MHZ16::MHZ16(int uart)
}
}
MHZ16::MHZ16(const std::string& uart_raw)
{
m_ttyFd = -1;
if ( !(m_uart = mraa_uart_init_raw(uart_raw.c_str())) )
{
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_uart_init_raw() failed");
return;
}
// This requires a recent MRAA (1/2015)
const char *devPath = mraa_uart_get_dev_path(m_uart);
if (!devPath)
{
throw std::runtime_error(std::string(__FUNCTION__) +
": mraa_uart_get_dev_path() failed");
return;
}
// now open the tty
if ( (m_ttyFd = open(devPath, O_RDWR)) == -1)
{
throw std::runtime_error(std::string(__FUNCTION__) +
": open of " +
string(devPath) + " failed: " +
string(strerror(errno)));
return;
}
}
MHZ16::~MHZ16()
{
if (m_ttyFd != -1)

View File

@ -40,12 +40,6 @@
#include <mraa/uart.h>
const int MHZ16_DEFAULT_UART = 0;
// protocol start and end codes
const uint8_t MHZ16_START = 0x7e;
const uint8_t MHZ16_END = 0x7e;
namespace upm {
/**
* @brief MHZ16 Serial CO2 Sensor
@ -80,14 +74,21 @@ namespace upm {
/**
* MHZ16 constructor
*
* @param uart Default UART to use (0 or 1)
* @param uart Default mraa UART index to use
*/
MHZ16(int uart);
/**
* MHZ16 constructor
*
* @param uart_raw Full path to UART device.
*/
MHZ16(const std::string& uart_raw = "/dev/ttyS0");
/**
* MHZ16 destructor
*/
~MHZ16();
virtual ~MHZ16();
/**
* Checks to see if there is data available for reading