Private
Public Access
2
0

aio.hpp: Add C++ wrapper around Aio

* maa_aio_context becomes an opaque pointer
* C++ wrapper class Aio created
* examples/c++ with a sample for Aio created
* swig now uses C++ wrapper Aio to generate an API
* python generated code is now C++

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Brendan Le Foll
2014-05-15 22:47:38 +01:00
parent e84406d697
commit 554505b640
11 changed files with 125 additions and 41 deletions

View File

@@ -43,10 +43,10 @@ extern "C" {
#define ADC_RAW_RESOLUTION_BITS (12)
#define ADC_SUPPORTED_RESOLUTION_BITS (10)
typedef struct {
unsigned int channel;
int adc_in_fp;
} maa_aio_context;
/**
* Opaque pointer definition to the internal struct _aio
*/
typedef struct _aio* maa_aio_context;
/** Initialise an Analog input device, connected to the specified pin
*
@@ -55,7 +55,7 @@ typedef struct {
* @returns pointer to maa_aio_context structure after initialisation of Analog
* input pin successfully, else returns null.
*/
maa_aio_context* maa_aio_init(unsigned int aio_channel);
maa_aio_context maa_aio_init(unsigned int aio_channel);
/** Read the input voltage, represented as an unsigned short in the range [0x0,
* 0xFFFF]
@@ -66,7 +66,7 @@ maa_aio_context* maa_aio_init(unsigned int aio_channel);
* @returns 16-bit unsigned integer representing the current input voltage,
* normalised to a 16-bit value
*/
uint16_t maa_aio_read(maa_aio_context* dev);
uint16_t maa_aio_read(maa_aio_context dev);
/** Close the analog input context
* - Will free the memory for the context.
@@ -76,7 +76,7 @@ uint16_t maa_aio_read(maa_aio_context* dev);
*
* @return maa_result_t - result type.
*/
maa_result_t maa_aio_close(maa_aio_context* dev);
maa_result_t maa_aio_close(maa_aio_context dev);
#ifdef __cplusplus
}

51
api/aio.hpp Normal file
View File

@@ -0,0 +1,51 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Copyright (c) 2014 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
*
* This file defines the aio C++ interface for libmaa
*
*/
#include "aio.h"
namespace maa {
class Aio {
public:
Aio(unsigned int pin) {
m_aio = maa_aio_init(pin);
}
~Aio() {
maa_aio_close(m_aio);
}
uint16_t read() {
return maa_aio_read(m_aio);
}
private:
maa_aio_context m_aio;
};
}

View File

@@ -6,7 +6,7 @@ add_executable (analogin_a0 analogin_a0.c)
add_executable (isr_pin6 isr_pin6.c)
add_executable (gpio_read6 gpio_read6.c)
include_directories(${PROJECT_SOURCE_DIR}/api ${PROJECT_SOURCE_DIR}/include)
include_directories(${PROJECT_SOURCE_DIR}/api)
target_link_libraries (hellomaa maa)
target_link_libraries (i2c_HMC5883L maa m)
@@ -15,3 +15,5 @@ target_link_libraries (blink-io maa)
target_link_libraries (analogin_a0 maa)
target_link_libraries (isr_pin6 maa)
target_link_libraries (gpio_read6 maa)
add_subdirectory (c++)

View File

@@ -29,7 +29,7 @@
int main ()
{
maa_init();
maa_aio_context* adc_a0;
maa_aio_context adc_a0;
uint16_t adc_value = 0;
adc_a0 = maa_aio_init(0);

43
examples/c++/AioA0.cpp Normal file
View File

@@ -0,0 +1,43 @@
/*
* Author: Brendan Le Foll <brendan.le.foll@intel.com>
* Copyright (c) 2014 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.
*/
#include "aio.hpp"
int main ()
{
uint16_t adc_value;
maa::Aio* a0;
a0 = new maa::Aio(0);
if (a0 == NULL) {
return MAA_ERROR_UNSPECIFIED;
}
for(;;) {
adc_value = a0->read();
fprintf(stdout, "ADC A0 read %X - %d\n", adc_value, adc_value);
}
return MAA_SUCCESS;
}

View File

@@ -0,0 +1,5 @@
add_executable (AioA0 AioA0.cpp)
include_directories(${PROJECT_SOURCE_DIR}/api)
target_link_libraries (AioA0 maa)

View File

@@ -10,6 +10,7 @@ set (maa_LIB_HEADERS
${PROJECT_SOURCE_DIR}/api/pwm.h
${PROJECT_SOURCE_DIR}/api/spi.h
${PROJECT_SOURCE_DIR}/api/aio.h
${PROJECT_SOURCE_DIR}/api/aio.hpp
${PROJECT_SOURCE_DIR}/include/smbus.h
${PROJECT_SOURCE_DIR}/include/version.h
${PROJECT_SOURCE_DIR}/include/intel_galileo_rev_d.h

View File

@@ -28,7 +28,12 @@
#include "aio.h"
static maa_result_t aio_get_valid_fp(maa_aio_context* dev)
struct _aio {
unsigned int channel;
int adc_in_fp;
};
static maa_result_t aio_get_valid_fp(maa_aio_context dev)
{
char file_path[64]= "";
@@ -52,10 +57,8 @@ static maa_result_t aio_get_valid_fp(maa_aio_context* dev)
* @returns pointer to maa_aio_context structure after initialisation of
* Analog input pin connected to the device successfully, else returns NULL.
*/
maa_aio_context* maa_aio_init(unsigned int aio_channel)
maa_aio_context maa_aio_init(unsigned int aio_channel)
{
maa_aio_context* dev;
int checked_pin = maa_check_aio(aio_channel);
if (checked_pin < 0) {
switch(checked_pin) {
@@ -75,7 +78,7 @@ maa_aio_context* maa_aio_init(unsigned int aio_channel)
}
//Create ADC device connected to specified channel
dev = (maa_aio_context*) malloc(sizeof(maa_aio_context));
maa_aio_context dev = malloc(sizeof(struct _aio));
if (dev == NULL) {
fprintf(stderr, "Insufficient memory for specified Analog input channel "
"%d\n", aio_channel);
@@ -102,7 +105,7 @@ maa_aio_context* maa_aio_init(unsigned int aio_channel)
* unsigned 16 bit int representing the current input voltage, normalised to
* a 16-bit value
*/
uint16_t maa_aio_read(maa_aio_context* dev)
uint16_t maa_aio_read(maa_aio_context dev)
{
char buffer[16];
unsigned int analog_value = 0;
@@ -152,7 +155,7 @@ uint16_t maa_aio_read(maa_aio_context* dev)
*
* @return maa result type.
*/
maa_result_t maa_aio_close(maa_aio_context* dev)
maa_result_t maa_aio_close(maa_aio_context dev)
{
if (NULL != dev)
free(dev);

View File

@@ -24,7 +24,6 @@
*/
#include <stddef.h>
#include <stdlib.h>
#include "maa.h"
#include "intel_galileo_rev_d.h"

View File

@@ -4,7 +4,7 @@
#include "pwm.h"
#include "i2c.h"
#include "spi.h"
#include "aio.h"
#include "aio.hpp"
%}
%init %{
@@ -112,7 +112,7 @@ typedef struct {
{
Py_INCREF(pyfunc);
// do a nasty cast to get away from the warnings
maa_gpio_isr(self, MAA_GPIO_EDGE_BOTH, pyfunc);
maa_gpio_isr(self, MAA_GPIO_EDGE_BOTH, (void (*) ()) pyfunc);
return 0;
}
#else
@@ -270,25 +270,4 @@ typedef struct {
#### AIO ####
%rename(Aio) maa_aio_context;
%ignore adc_in_fp;
typedef struct {
unsigned int channel;
FILE *adc_in_fp;
} maa_aio_context;
%nodefault maa_aio_context;
%extend maa_aio_context {
maa_aio_context(unsigned int aio_channel)
{
return maa_aio_init(aio_channel);
}
~maa_aio_context()
{
}
unsigned int read()
{
return maa_aio_read($self);
}
}
%include "aio.hpp"

View File

@@ -6,6 +6,7 @@ include_directories(
${PYTHON_INCLUDE_DIRS}
)
set_source_files_properties (pymaa.i PROPERTIES CPLUSPLUS ON)
swig_add_module (pymaa python pymaa.i ${maa_LIB_SRCS})
swig_link_libraries (pymaa ${PYTHON_LIBRARIES})