From d3ce509334351976e33c9159cc78da1c302dd7e1 Mon Sep 17 00:00:00 2001 From: Adelin Dobre Date: Wed, 31 Oct 2018 16:50:28 +0200 Subject: [PATCH] DS2413: Add string based constructor for Switch Signed-off-by: Adelin Dobre Signed-off-by: Mihai Tudor Panu --- src/ds2413/ds2413.cxx | 35 +++++++++++++++++++++++++++++++++++ src/ds2413/ds2413.hpp | 12 +++++++++++- 2 files changed, 46 insertions(+), 1 deletion(-) diff --git a/src/ds2413/ds2413.cxx b/src/ds2413/ds2413.cxx index 24f27957..9aa11274 100644 --- a/src/ds2413/ds2413.cxx +++ b/src/ds2413/ds2413.cxx @@ -27,6 +27,8 @@ #include #include "ds2413.hpp" +#include "upm_string_parser.hpp" + using namespace upm; using namespace std; @@ -48,6 +50,39 @@ DS2413::DS2413(int uart) : } } +DS2413::DS2413(std::string initStr) : m_uart(nullptr), mraaIo(initStr) +{ + mraa_io_descriptor* descs = mraaIo.getMraaDescriptors(); + std::vector upmTokens; + + if(!mraaIo.getLeftoverStr().empty()) { + upmTokens = UpmStringParser::parse(mraaIo.getLeftoverStr()); + } + + m_uart = descs->uart_ows[0]; + m_devicesFound = 0; + + // check basic access to the 1-wire bus (presence detect) + mraa::Result rv; + + if ((rv = m_uart.reset()) != mraa::SUCCESS) + { + throw std::runtime_error(std::string(__FUNCTION__) + + ": reset() failed, no devices on bus?"); + return; + } + + for(std::string tok : upmTokens) { + if(tok.substr(0, 11) == "writeGpios:") { + std::string::size_type sz; + int index = std::stoi(tok.substr(11), &sz); + tok = tok.substr(11); + int value = std::stoi(tok.substr(sz+1), nullptr, 0); + writeGpios(index, value); + } + } +} + DS2413::~DS2413() { } diff --git a/src/ds2413/ds2413.hpp b/src/ds2413/ds2413.hpp index f2d5834e..19a639e1 100644 --- a/src/ds2413/ds2413.hpp +++ b/src/ds2413/ds2413.hpp @@ -26,6 +26,7 @@ #include #include #include +#include #include #include @@ -33,6 +34,7 @@ #include #include +#include #define DS2413_DEFAULT_UART 0 @@ -100,6 +102,13 @@ namespace upm { */ DS2413(int uart=DS2413_DEFAULT_UART); + /** + * Instantiates DS2413 Switch object based on a given string. + * + * @param initStr string containing specific information for DS2413 Switch initialization. + */ + DS2413(std::string initStr); + /** * DS2413 object destructor */ @@ -164,7 +173,8 @@ namespace upm { protected: mraa::UartOW m_uart; - + mraa::MraaIo mraaIo; + // the total number of devices found int m_devicesFound;