DS2413: Add string based constructor for Switch

Signed-off-by: Adelin Dobre <adelin.dobre@rinftech.com>
Signed-off-by: Mihai Tudor Panu <mihai.tudor.panu@intel.com>
This commit is contained in:
Adelin Dobre 2018-10-31 16:50:28 +02:00 committed by Mihai Tudor Panu
parent f608b0fa98
commit d3ce509334
2 changed files with 46 additions and 1 deletions

View File

@ -27,6 +27,8 @@
#include <stdexcept>
#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<std::string> 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()
{
}

View File

@ -26,6 +26,7 @@
#include <string>
#include <iostream>
#include <map>
#include <vector>
#include <stdlib.h>
#include <unistd.h>
@ -33,6 +34,7 @@
#include <mraa/common.hpp>
#include <mraa/uart_ow.hpp>
#include <mraa/initio.hpp>
#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,6 +173,7 @@ namespace upm {
protected:
mraa::UartOW m_uart;
mraa::MraaIo mraaIo;
// the total number of devices found
int m_devicesFound;