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)