From dd73c4aa45d8279cf753cf2b817e53b0dc4de70e Mon Sep 17 00:00:00 2001 From: g-vidal Date: Sun, 15 Apr 2018 16:06:13 +0200 Subject: [PATCH] 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 Signed-off-by: Noel Eck --- src/mhz16/mhz16.cxx | 32 ++++++++++++++++++++++++++++++++ src/mhz16/mhz16.hpp | 17 +++++++++-------- 2 files changed, 41 insertions(+), 8 deletions(-) diff --git a/src/mhz16/mhz16.cxx b/src/mhz16/mhz16.cxx index ca3162e7..5b29e305 100644 --- a/src/mhz16/mhz16.cxx +++ b/src/mhz16/mhz16.cxx @@ -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) diff --git a/src/mhz16/mhz16.hpp b/src/mhz16/mhz16.hpp index 999996c0..b46699e4 100644 --- a/src/mhz16/mhz16.hpp +++ b/src/mhz16/mhz16.hpp @@ -40,12 +40,6 @@ #include -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