ADIS16448: Add string based constructor for Accelerometer

Signed-off-by: Adelin Dobre <adelin.dobre1994@gmail.com>
This commit is contained in:
Adelin Dobre 2018-07-04 16:46:56 +03:00 committed by Stefan Andritoiu
parent b2c46f602e
commit 4aa25f2281
2 changed files with 88 additions and 20 deletions

View File

@ -44,6 +44,7 @@
#include <functional>
#include <string.h>
#include "upm_string_parser.hpp"
#include "adis16448.hpp"
using namespace upm;
@ -75,6 +76,63 @@ ADIS16448::ADIS16448(int bus, int rst)
configSPI();
}
ADIS16448::ADIS16448(std::string initStr) : mraaIo(initStr)
{
mraa_io_descriptor* descs = mraaIo.getMraaDescriptors();
std::vector<std::string> upmTokens;
if (!mraaIo.getLeftoverStr().empty()) {
upmTokens = UpmStringParser::parse(mraaIo.getLeftoverStr());
}
// Configure I/O
//Initialize RST pin
if(!descs->gpios)
{
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_gpio_init() failed, invalid pin?");
}
else
{
if( !(_rst = descs->gpios[0]) )
{
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_gpio_init() failed, invalid pin?");
}
}
mraa_gpio_dir(_rst, MRAA_GPIO_IN); //Set direction as INPUT
// Configure SPI
if(!descs->spis)
{
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_spi_init() failed");
}
else
{
if( !(_spi = descs->spis[0]) )
{
throw std::invalid_argument(std::string(__FUNCTION__) +
": mraa_spi_init() failed");
}
}
configSPI();
std::string::size_type sz;
for (std::string tok : upmTokens) {
if(tok.substr(0,9) == "regWrite:") {
uint8_t regAddr = std::stoi(tok.substr(9),&sz,0);
tok = tok.substr(9);
uint16_t regData = std::stoi(tok.substr(sz+1),nullptr,0);
regWrite(regAddr, regData);
}
}
}
////////////////////////////////////////////////////////////////////////////
// Destructor - Stops SPI and Closes all GPIO used. Reports an error if
// unable to close either properly.
@ -162,7 +220,8 @@ int16_t ADIS16448::regRead(uint8_t regAddr)
// regData - data to be written to the register
////////////////////////////////////////////////////////////////////////////
void ADIS16448::regWrite(uint8_t regAddr,uint16_t regData)
{
{ std::vector<std::string> upmTokens;
configSPI();
// Separate the 16 bit command word into two bytes
uint16_t addr = (((regAddr & 0x7F) | 0x80) << 8); //Check that the address is 7 bits, flip the sign bit

View File

@ -39,6 +39,7 @@
#include <string>
#include <mraa/spi.h>
#include <mraa/gpio.h>
#include <mraa/initio.hpp>
// User Register Memory Map from Table 6 of the Datasheet
#define FLASH_CNT 0x00 //Flash memory write count
@ -113,6 +114,13 @@ namespace upm {
*/
ADIS16448(int bus, int rst);
/**
* Instantiates ADIS16448 Accelerometer object based on a given string.
*
* @param initStr string containing specific information for ADIS16448 initialization.
*/
ADIS16448(std::string initStr);
/**
* Destructor
*/
@ -165,6 +173,7 @@ namespace upm {
private:
mraa::MraaIo mraaIo;
mraa_spi_context _spi;
mraa_gpio_context _rst;