From 554505b640cc2161723717e649e18cdb7c857ba1 Mon Sep 17 00:00:00 2001 From: Brendan Le Foll Date: Thu, 15 May 2014 22:47:38 +0100 Subject: [PATCH] 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 --- api/aio.h | 14 ++++++------- api/aio.hpp | 51 +++++++++++++++++++++++++++++++++++++++++++++ examples/CMakeLists.txt | 4 +++- examples/analogin_a0.c | 2 +- examples/c++/AioA0.cpp | 43 ++++++++++++++++++++++++++++++++++++++ examples/c++/CMakeLists.txt | 5 +++++ src/CMakeLists.txt | 1 + src/aio/aio.c | 17 ++++++++------- src/maa.c | 1 - src/maa.i | 27 +++--------------------- src/python/CMakeLists.txt | 1 + 11 files changed, 125 insertions(+), 41 deletions(-) create mode 100644 api/aio.hpp create mode 100644 examples/c++/AioA0.cpp create mode 100644 examples/c++/CMakeLists.txt diff --git a/api/aio.h b/api/aio.h index 7250c62..47d9ed3 100644 --- a/api/aio.h +++ b/api/aio.h @@ -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 } diff --git a/api/aio.hpp b/api/aio.hpp new file mode 100644 index 0000000..d78d765 --- /dev/null +++ b/api/aio.hpp @@ -0,0 +1,51 @@ +/* + * Author: Brendan Le Foll + * 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; +}; + +} diff --git a/examples/CMakeLists.txt b/examples/CMakeLists.txt index 1e651a7..370a187 100644 --- a/examples/CMakeLists.txt +++ b/examples/CMakeLists.txt @@ -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++) diff --git a/examples/analogin_a0.c b/examples/analogin_a0.c index fd8d45b..010842f 100644 --- a/examples/analogin_a0.c +++ b/examples/analogin_a0.c @@ -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); diff --git a/examples/c++/AioA0.cpp b/examples/c++/AioA0.cpp new file mode 100644 index 0000000..2a809d3 --- /dev/null +++ b/examples/c++/AioA0.cpp @@ -0,0 +1,43 @@ +/* + * Author: Brendan Le Foll + * 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; +} diff --git a/examples/c++/CMakeLists.txt b/examples/c++/CMakeLists.txt new file mode 100644 index 0000000..2be1a7d --- /dev/null +++ b/examples/c++/CMakeLists.txt @@ -0,0 +1,5 @@ +add_executable (AioA0 AioA0.cpp) + +include_directories(${PROJECT_SOURCE_DIR}/api) + +target_link_libraries (AioA0 maa) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 69c80a2..7374c72 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -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 diff --git a/src/aio/aio.c b/src/aio/aio.c index 9fc99a1..d573194 100644 --- a/src/aio/aio.c +++ b/src/aio/aio.c @@ -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); diff --git a/src/maa.c b/src/maa.c index 70b39f6..42e1ea8 100644 --- a/src/maa.c +++ b/src/maa.c @@ -24,7 +24,6 @@ */ #include -#include #include "maa.h" #include "intel_galileo_rev_d.h" diff --git a/src/maa.i b/src/maa.i index 8e4b383..42bb9fa 100644 --- a/src/maa.i +++ b/src/maa.i @@ -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" diff --git a/src/python/CMakeLists.txt b/src/python/CMakeLists.txt index 45f355f..fd2d9a6 100644 --- a/src/python/CMakeLists.txt +++ b/src/python/CMakeLists.txt @@ -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}) -- 2.7.4