From e491f99228bb635e9025cbea6c29070f2dff2037 Mon Sep 17 00:00:00 2001 From: Brendan Le Foll Date: Fri, 16 May 2014 10:28:26 +0100 Subject: [PATCH] i2c.hpp: Add C++ wrapper around I2c * maa_i2c_context bcomes an opaque pointer * C++ wrapper class I2c created * swig now uses C++ wrapper I2c to generate API Signed-off-by: Brendan Le Foll --- api/aio.hpp | 1 + api/i2c.h | 28 ++++++++------------ api/i2c.hpp | 70 +++++++++++++++++++++++++++++++++++++++++++++++++ examples/i2c_HMC5883L.c | 2 +- src/CMakeLists.txt | 1 + src/i2c/i2c.c | 28 +++++++++++++------- src/maa.i | 45 ++----------------------------- 7 files changed, 104 insertions(+), 71 deletions(-) create mode 100644 api/i2c.hpp diff --git a/api/aio.hpp b/api/aio.hpp index d78d765..bab5fc4 100644 --- a/api/aio.hpp +++ b/api/aio.hpp @@ -23,6 +23,7 @@ */ #pragma once + /** @file * * This file defines the aio C++ interface for libmaa diff --git a/api/i2c.h b/api/i2c.h index 2f0e650..dbf3e6e 100644 --- a/api/i2c.h +++ b/api/i2c.h @@ -42,29 +42,23 @@ extern "C" { #include "gpio.h" /** - * A structure representing an i2c device /dev/i2c-* + * Opaque pointer definition to the internal struct _i2c */ -typedef struct { - /*@{*/ - int hz; /**< frequency of communication */ - int fh; /**< the file handle to the /dev/i2c-* device */ - int addr; /**< the address of the i2c slave */ - /*@}*/ -} maa_i2c_context; +typedef struct _i2c* maa_i2c_context; /** Initialise i2c context, using board defintions * * @param bus i2c bus to use * @return maa_i2c_context i2c context ready for other calls. */ -maa_i2c_context* maa_i2c_init(int bus); +maa_i2c_context maa_i2c_init(int bus); /** Initialise i2c context, passing in spi bus to use. * * @param bus The i2c bus to use i.e. /dev/i2c-2 would be "2" * @return maa_i2c_context i2c context ready for other calls. */ -maa_i2c_context* maa_i2c_init_raw(unsigned int bus); +maa_i2c_context maa_i2c_init_raw(unsigned int bus); /** Sets the frequency of the i2c context * @@ -73,7 +67,7 @@ maa_i2c_context* maa_i2c_init_raw(unsigned int bus); * * @return maa_result_t the maa result. */ -maa_result_t maa_i2c_frequency(maa_i2c_context* dev, int hz); +maa_result_t maa_i2c_frequency(maa_i2c_context dev, int hz); /** Read from an i2c context * @@ -83,7 +77,7 @@ maa_result_t maa_i2c_frequency(maa_i2c_context* dev, int hz); * * @return maa_result_t the maa result. */ -maa_result_t maa_i2c_read(maa_i2c_context* dev, char *data, int length); +maa_result_t maa_i2c_read(maa_i2c_context dev, const char *data, int length); /** Read a single byte from the i2c context * @@ -91,7 +85,7 @@ maa_result_t maa_i2c_read(maa_i2c_context* dev, char *data, int length); * * @return byte the result of the read or -1 if failed. */ -int maa_i2c_read_byte(maa_i2c_context* dev); +int maa_i2c_read_byte(maa_i2c_context dev); /** Write to an i2c context * @@ -101,7 +95,7 @@ int maa_i2c_read_byte(maa_i2c_context* dev); * * @return maa_result_t the maa result. */ -maa_result_t maa_i2c_write(maa_i2c_context* dev, const char *data, int length); +maa_result_t maa_i2c_write(maa_i2c_context dev, char *data, int length); /** Write a single byte to an i2c context * @@ -110,7 +104,7 @@ maa_result_t maa_i2c_write(maa_i2c_context* dev, const char *data, int length); * * @return maa_result_t the maa result. */ -maa_result_t maa_i2c_write_byte(maa_i2c_context* dev, int data); +maa_result_t maa_i2c_write_byte(maa_i2c_context dev, int data); /** Sets the i2c context address. * @@ -121,7 +115,7 @@ maa_result_t maa_i2c_write_byte(maa_i2c_context* dev, int data); * * @return maa_result_t the maa result. */ -maa_result_t maa_i2c_address(maa_i2c_context* dev, int address); +maa_result_t maa_i2c_address(maa_i2c_context dev, int address); /** De-inits an maa_i2c_context device * @@ -129,7 +123,7 @@ maa_result_t maa_i2c_address(maa_i2c_context* dev, int address); * * @return maa_result_t the maa result. */ -maa_result_t maa_i2c_stop(maa_i2c_context* dev); +maa_result_t maa_i2c_stop(maa_i2c_context dev); #ifdef __cplusplus } diff --git a/api/i2c.hpp b/api/i2c.hpp new file mode 100644 index 0000000..f683446 --- /dev/null +++ b/api/i2c.hpp @@ -0,0 +1,70 @@ +/* + * 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 i2c C++ interface for libmaa + * + */ + +#include "i2c.h" + +namespace maa { + +class I2c { + public: + I2c(int bus, bool raw=false) { + if (raw) + m_i2c = maa_i2c_init_raw(bus); + else + m_i2c = maa_i2c_init(bus); + } + ~I2c() { + maa_i2c_stop(m_i2c); + } + maa_result_t frequency(int hz) { + return maa_i2c_frequency(m_i2c, hz); + } + maa_result_t address(int address) { + return maa_i2c_address(m_i2c, address); + } + int read_byte() { + return maa_i2c_read_byte(m_i2c); + } + maa_result_t read(const char *data, int length) { + return maa_i2c_read(m_i2c, data, length); + } + maa_result_t write(char *data, int length) { + return maa_i2c_write(m_i2c, data, length); + } + maa_result_t write_byte(int data) { + return maa_i2c_write_byte(m_i2c, data); + } + private: + maa_i2c_context m_i2c; +}; + +} diff --git a/examples/i2c_HMC5883L.c b/examples/i2c_HMC5883L.c index 7e65c51..4933e24 100644 --- a/examples/i2c_HMC5883L.c +++ b/examples/i2c_HMC5883L.c @@ -83,7 +83,7 @@ main(int argc, char **argv) int16_t x = 0, y = 0, z = 0; char rx_tx_buf[MAX_BUFFER_LENGTH]; - maa_i2c_context *i2c; + maa_i2c_context i2c; i2c = maa_i2c_init(0); maa_i2c_address(i2c, HMC5883L_I2C_ADDR); diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 89c20c4..a9e76de 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,6 +7,7 @@ set (maa_LIB_HEADERS ${PROJECT_SOURCE_DIR}/api/maa.h ${PROJECT_SOURCE_DIR}/api/gpio.h ${PROJECT_SOURCE_DIR}/api/i2c.h + ${PROJECT_SOURCE_DIR}/api/i2c.pp ${PROJECT_SOURCE_DIR}/api/pwm.h ${PROJECT_SOURCE_DIR}/api/spi.h ${PROJECT_SOURCE_DIR}/api/aio.h diff --git a/src/i2c/i2c.c b/src/i2c/i2c.c index df4dc57..39a8f5f 100644 --- a/src/i2c/i2c.c +++ b/src/i2c/i2c.c @@ -25,7 +25,15 @@ #include "i2c.h" #include "smbus.h" -maa_i2c_context* +struct _i2c { + /*@{*/ + int hz; /**< frequency of communication */ + int fh; /**< the file handle to the /dev/i2c-* device */ + int addr; /**< the address of the i2c slave */ + /*@}*/ +}; + +maa_i2c_context maa_i2c_init(int bus) { int checked_pin = maa_check_i2c(bus); @@ -46,10 +54,10 @@ maa_i2c_init(int bus) return maa_i2c_init_raw((unsigned int) checked_pin); } -maa_i2c_context* +maa_i2c_context maa_i2c_init_raw(unsigned int bus) { - maa_i2c_context* dev = (maa_i2c_context*) malloc(sizeof(maa_i2c_context)); + maa_i2c_context dev = (maa_i2c_context) malloc(sizeof(struct _i2c)); if (dev == NULL) return NULL; @@ -62,7 +70,7 @@ maa_i2c_init_raw(unsigned int bus) } maa_result_t -maa_i2c_frequency(maa_i2c_context* dev, int hz) +maa_i2c_frequency(maa_i2c_context dev, int hz) { dev->hz = hz; @@ -70,7 +78,7 @@ maa_i2c_frequency(maa_i2c_context* dev, int hz) } maa_result_t -maa_i2c_read(maa_i2c_context* dev, char *data, int length) +maa_i2c_read(maa_i2c_context dev, const char *data, int length) { // this is the read(3) syscall not maa_i2c_read() if (read(dev->fh, data, length) == length) { @@ -80,7 +88,7 @@ maa_i2c_read(maa_i2c_context* dev, char *data, int length) } int -maa_i2c_read_byte(maa_i2c_context* dev) +maa_i2c_read_byte(maa_i2c_context dev) { int byte; byte = i2c_smbus_read_byte(dev->fh); @@ -91,7 +99,7 @@ maa_i2c_read_byte(maa_i2c_context* dev) } maa_result_t -maa_i2c_write(maa_i2c_context* dev, const char* data, int length) +maa_i2c_write(maa_i2c_context dev, char* data, int length) { if (i2c_smbus_write_i2c_block_data(dev->fh, data[0], length-1, (uint8_t*) data+1) < 0) { fprintf(stderr, "Failed to write to i2c\n"); @@ -101,7 +109,7 @@ maa_i2c_write(maa_i2c_context* dev, const char* data, int length) } maa_result_t -maa_i2c_write_byte(maa_i2c_context* dev, int data) +maa_i2c_write_byte(maa_i2c_context dev, int data) { if (i2c_smbus_write_byte(dev->fh, data) < 0) { fprintf(stderr, "Failed to write to i2c\n"); @@ -111,7 +119,7 @@ maa_i2c_write_byte(maa_i2c_context* dev, int data) } maa_result_t -maa_i2c_address(maa_i2c_context* dev, int addr) +maa_i2c_address(maa_i2c_context dev, int addr) { dev->addr = addr; if (ioctl(dev->fh, I2C_SLAVE_FORCE, addr) < 0) { @@ -122,7 +130,7 @@ maa_i2c_address(maa_i2c_context* dev, int addr) } maa_result_t -maa_i2c_stop(maa_i2c_context* dev) +maa_i2c_stop(maa_i2c_context dev) { free(dev); return MAA_SUCCESS; diff --git a/src/maa.i b/src/maa.i index 42bb9fa..9d07f13 100644 --- a/src/maa.i +++ b/src/maa.i @@ -2,7 +2,7 @@ #include "maa.h" #include "gpio.h" #include "pwm.h" - #include "i2c.h" + #include "i2c.hpp" #include "spi.h" #include "aio.hpp" %} @@ -126,48 +126,7 @@ typedef struct { #### i2c #### -%nodefault maa_i2c_context; -%rename(I2c) maa_i2c_context; - -%ignore fh; -typedef struct { - /*@{*/ - int hz; /**< frequency of communication */ - int fh; /**< the file handle to the /dev/i2c-* device */ - int addr; /**< the address of the i2c slave */ - /*@}*/ -} maa_i2c_context; - -%nodefault maa_i2c_context; -%extend maa_i2c_context { - maa_i2c_context() - { - return maa_i2c_init(0); - } - ~maa_i2c_context() - { - } - int frequency(int hz) - { - return maa_i2c_frequency($self, hz); - } - int read(char *data, int length) - { - return maa_i2c_read($self, data, length); - } - int read() - { - return maa_i2c_read_byte($self); - } - int write(char *data, int length) - { - return maa_i2c_write($self, data, length); - } - int write(int data) - { - return maa_i2c_write_byte($self, data); - } -} +%include "i2c.hpp" #### PWM #### -- 2.7.4