Private
Public Access
2
0

String based IO initialization for MRAA

Signed-off-by: Mihai Stefanescu <mihai.stefanescu@rinftech.com>
Signed-off-by: Noel Eck <noel.eck@intel.com>
This commit is contained in:
Mihai Stefanescu
2018-03-21 15:44:36 +02:00
committed by Noel Eck
parent 4b2279704b
commit ae391fe9fe
13 changed files with 1540 additions and 31 deletions

View File

@@ -24,17 +24,16 @@
#pragma once
#include <stdexcept>
#include <sstream>
#include "iio.h"
#include "types.hpp"
#include <sstream>
#include <stdexcept>
namespace mraa
{
/** Iio Event Data */
struct IioEventData
{
struct IioEventData {
/** Channel Type */
int channelType;
/** Modifier */
@@ -54,11 +53,11 @@ struct IioEventData
/** Iio Handler */
class IioHandler
{
public:
/** onIioEvent Handler */
virtual void onIioEvent(const IioEventData& eventData) = 0;
/** Destructor */
virtual ~IioHandler() {}; // add an empty destructor to get rid of warning
public:
/** onIioEvent Handler */
virtual void onIioEvent(const IioEventData& eventData) = 0;
/** Destructor */
virtual ~IioHandler(){}; // add an empty destructor to get rid of warning
};
@@ -112,6 +111,19 @@ class Iio
}
}
/**
* IIO constructor, takes a pointer to a IIO context and initialises the IIO class
*
* @param iio_context void * to an IIO context
*/
Iio(void* iio_context)
{
m_iio = (mraa_iio_context) iio_context;
if (m_iio == NULL) {
throw std::invalid_argument("Invalid IIO context");
}
}
/**
* Iio destructor
*/
@@ -193,7 +205,6 @@ class Iio
oss << "IIO writeInt for attibute " << attributeName << " failed";
throw std::runtime_error(oss.str());
}
}
/**
@@ -213,7 +224,6 @@ class Iio
oss << "IIO writeFloat for attibute " << attributeName << " failed";
throw std::runtime_error(oss.str());
}
}
/**
@@ -233,13 +243,15 @@ class Iio
}
private:
static void private_event_handler(iio_event_data* data, void *args)
static void
private_event_handler(iio_event_data* data, void* args)
{
if (args != NULL) {
IioHandler* handler = (IioHandler*)args;
IioHandler* handler = (IioHandler*) args;
IioEventData eventData;
int chan_type, modifier, type, direction, channel, channel2, different;
mraa_iio_event_extract_event(data, &chan_type, &modifier, &type, &direction, &channel, &channel2, &different);
mraa_iio_event_extract_event(data, &chan_type, &modifier, &type, &direction, &channel,
&channel2, &different);
eventData.channelType = chan_type;
eventData.modifier = modifier;
eventData.type = type;
@@ -253,5 +265,4 @@ class Iio
mraa_iio_context m_iio;
};
}

149
api/mraa/initio.h Normal file
View File

@@ -0,0 +1,149 @@
/*
* Author: Noel Eck <noel.eck@intel.com>
* Copyright (c) 2014-2016 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
/**
* @file
* @brief I/O initializer
*
* initio allows for string initialization of mraa resources.
*
*/
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
#include "aio.h"
#include "gpio.h"
#include "i2c.h"
#include "iio.h"
#include "pwm.h"
#include "spi.h"
#include "uart.h"
#include "uart_ow.h"
typedef struct _mraa_io_descriptor {
int n_aio;
mraa_aio_context* aios;
int n_gpio;
mraa_gpio_context* gpios;
int n_i2c;
mraa_i2c_context* i2cs;
int n_iio;
mraa_iio_context* iios;
int n_pwm;
mraa_pwm_context* pwms;
int n_spi;
mraa_spi_context* spis;
int n_uart;
mraa_uart_context* uarts;
int n_uart_ow;
mraa_uart_ow_context* uart_ows;
char* leftover_str;
} mraa_io_descriptor;
/**
* Initialize a structure of MRAA context elements given a description string.
*
* @param strdesc of one or more MRAA IO
* io:io_ndx[:option_0:option_1:option_2:option_n][,io:io_ndx]
*
* AIO
* AIO_KEY:aio pin[:num_bits]
*
* examples:
* a:13 # aio 13
* a:13:10 # aio 13, 10 bits
*
* GPIO
* GPIO_KEY:gpio pin[:dir:value:mode:edge:input:driver]
*
* examples:
* g:13:input # gpio 13, input
* g:13:0:output # gpio 13, value 0, output
*
* I2C
* I2C_KEY:i2c bus[:address:mode]
*
* examples:
* i:1:std # i2c bus 1, STD speed (100 KHz)
* i:1:16 # i2c bus 1, address 16
* i:0x1:0x10 # i2c bus 1, address 16
*
* IIO
* IIO_KEY:iio device
*
* examples:
* ii:1 # iio device 1
* ii:0x1 # iio device 1
*
* PWM
* PWM_KEY:pwm pin
*
* examples:
* p:1 # pwm pin 1
* p:0x1 # pwm pin 1
*
* SPI
* SPI_KEY:spi bus[:mode:frequency]
*
* examples:
* s:1 # spi bus 1
* s:0x1:mode2:400000 # spi bus 1, mode2 (CPOL = 1, CPHA = 0), 400 KHz
*
* UART
* UART_KEY:uart ndx[:baud:mode]
*
* examples:
* u:1 # uart bus 1
* u:0x1:9600:8N1 # uart bus 1, 9600 baud, 8 bit byte, no parity, 1 stop bit
*
* UART_OW
* UART_OW_KEY:uart_ow ndx
*
* examples:
* ow:1 # uart_ow bus 1
* ow:0x1 # uart_ow bus 1
*
*
* @param desc Pointer to structure containing number/pointer collections for initialized IO.
* @return Result of operation
*/
mraa_result_t mraa_io_init(const char* strdesc, mraa_io_descriptor** desc);
/**
* Free and close resources used by mraa_io_descriptor structure.
*
* @param desc mraa_io_descriptor structure
* @return Result of operation
*/
mraa_result_t mraa_io_close(mraa_io_descriptor* desc);
#ifdef __cplusplus
}
#endif

167
api/mraa/initio.hpp Normal file
View File

@@ -0,0 +1,167 @@
/*
* Author: Mihai Stefanescu <mihai.stefanescu@rinftech.com>
* Copyright (c) 2018 Intel Corporation.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#pragma once
#include "initio.h"
#include <stdexcept>
#include <string>
#include <vector>
#include "aio.hpp"
#include "gpio.hpp"
#include "i2c.hpp"
#include "iio.hpp"
#include "pwm.hpp"
#include "spi.hpp"
#include "uart.hpp"
#include "uart_ow.hpp"
namespace mraa
{
class MraaIo
{
private:
mraa_io_descriptor* descs;
public:
MraaIo(const std::string& initStr) : descs()
{
if (mraa_io_init(initStr.c_str(), &descs) != MRAA_SUCCESS) {
throw std::runtime_error("mraa_io_init error");
}
aios.reserve(descs->n_aio);
for (int i = 0; i < descs->n_aio; ++i) {
aios.emplace_back(descs->aios[i]);
}
gpios.reserve(descs->n_gpio);
for (int i = 0; i < descs->n_gpio; ++i) {
gpios.emplace_back(descs->gpios[i]);
}
i2cs.reserve(descs->n_i2c);
for (int i = 0; i < descs->n_i2c; ++i) {
i2cs.emplace_back(descs->i2cs[i]);
}
iios.reserve(descs->n_iio);
for (int i = 0; i < descs->n_iio; ++i) {
iios.emplace_back(descs->iios[i]);
}
pwms.reserve(descs->n_pwm);
for (int i = 0; i < descs->n_pwm; ++i) {
pwms.emplace_back(descs->pwms[i]);
}
spis.reserve(descs->n_spi);
for (int i = 0; i < descs->n_spi; ++i) {
spis.emplace_back(descs->spis[i]);
}
uarts.reserve(descs->n_uart);
for (int i = 0; i < descs->n_uart; ++i) {
uarts.emplace_back(descs->uarts[i]);
}
uart_ows.reserve(descs->n_uart_ow);
for (int i = 0; i < descs->n_uart_ow; ++i) {
uart_ows.emplace_back(descs->uart_ows[i]);
}
if (descs->leftover_str) {
leftoverStr = std::string(descs->leftover_str);
} else {
leftoverStr = std::string("");
}
}
MraaIo() : descs() {}
~MraaIo()
{
if (descs->leftover_str) {
free(descs->leftover_str);
}
if (descs->n_aio) {
free(descs->aios);
}
if (descs->n_gpio) {
free(descs->gpios);
}
if (descs->n_i2c) {
free(descs->i2cs);
}
if (descs->n_iio) {
free(descs->iios);
}
if (descs->n_pwm) {
free(descs->pwms);
}
if (descs->n_spi) {
free(descs->spis);
}
if (descs->n_uart) {
free(descs->uarts);
}
if (descs->n_uart_ow) {
free(descs->uart_ows);
}
/* Finally free the mraa_io_descriptor structure. */
free(descs);
}
public:
std::vector<Aio> aios;
std::vector<Gpio> gpios;
std::vector<I2c> i2cs;
std::vector<Iio> iios;
std::vector<Pwm> pwms;
std::vector<Spi> spis;
std::vector<Uart> uarts;
std::vector<UartOW> uart_ows;
private:
/* Used exclusively by the UPM library. */
std::string leftoverStr;
public:
/* This is used mainly by sensors that use C structs/functions in C++ code. */
mraa_io_descriptor*
getMraaDescriptors()
{
return descs;
}
std::string
getLeftoverStr()
{
return leftoverStr;
}
};
}

View File

@@ -24,10 +24,10 @@
#pragma once
#include "uart_ow.h"
#include "types.hpp"
#include <stdexcept>
#include "uart_ow.h"
#include <cstring>
#include <stdexcept>
namespace mraa
{
@@ -74,6 +74,21 @@ class UartOW
}
}
/**
* UartOW Constructor, takes a pointer to the UartOW context and initialises
* the UartOW class
*
* @param uart_ow_context void * to a UartOW context
*/
UartOW(void* uart_ow_context)
{
m_uart = (mraa_uart_ow_context) uart_ow_context;
if (m_uart == NULL) {
throw std::invalid_argument("Invalid UART_OW context");
}
}
/**
* Uart destructor
*/