Private
Public Access
2
0

gpio.hpp: Add C++ wrapper around Gpio

* maa_gpio_context bcomes an opaque pointer
* C++ wrapper class Gpio created
* swig now uses C++ wrapper Gpio to generate API

Signed-off-by: Brendan Le Foll <brendan.le.foll@intel.com>
This commit is contained in:
Brendan Le Foll
2014-05-16 11:56:06 +01:00
parent f0763844fa
commit 058084cd88
9 changed files with 136 additions and 152 deletions

View File

@@ -46,19 +46,10 @@ extern "C" {
* A strucutre representing a gpio pin.
*/
typedef struct {
/*@{*/
int pin; /**< the pin number, as known to the os. */
int value_fp; /**< the file pointer to the value of the gpio */
#ifdef SWIGPYTHON
PyObject *isr; /**< the interupt service request */
#else
void (* isr)(); /**< the interupt service request */
#endif
pthread_t thread_id; /**< the isr handler thread id */
int isr_value_fp; /**< the isr file pointer on the value */
/*@}*/
} maa_gpio_context;
/**
* Opaque pointer definition to the internal struct _gpio
*/
typedef struct _gpio* maa_gpio_context;
/**
* GPIO Output modes
@@ -91,7 +82,7 @@ typedef enum {
*
* @returns maa_gpio_context based on the IO pin
*/
maa_gpio_context* maa_gpio_init(int pin);
maa_gpio_context maa_gpio_init(int pin);
/** Initialise gpio context without any mapping to a pin.
* - For more expert users
@@ -100,7 +91,7 @@ maa_gpio_context* maa_gpio_init(int pin);
*
* @return gpio context
*/
maa_gpio_context* maa_gpio_init_raw(int gpiopin);
maa_gpio_context maa_gpio_init_raw(int gpiopin);
/** Set the edge mode on the gpio
*
@@ -109,7 +100,7 @@ maa_gpio_context* maa_gpio_init_raw(int gpiopin);
*
* @return maa result type.
*/
maa_result_t maa_gpio_edge_mode(maa_gpio_context *dev, gpio_edge_t mode);
maa_result_t maa_gpio_edge_mode(maa_gpio_context dev, gpio_edge_t mode);
/** Set an interupt on pin
*
@@ -121,7 +112,7 @@ maa_result_t maa_gpio_edge_mode(maa_gpio_context *dev, gpio_edge_t mode);
* @return maa result type.
*/
maa_result_t
maa_gpio_isr(maa_gpio_context *dev, gpio_edge_t edge, void (*fptr)(void));
maa_gpio_isr(maa_gpio_context dev, gpio_edge_t edge, void (*fptr)(void));
/** Stop the current interupt watcher on this GPIO, and set the GPIO edge mode
* to MAA_GPIO_EDGE_NONE.
@@ -131,7 +122,7 @@ maa_gpio_isr(maa_gpio_context *dev, gpio_edge_t edge, void (*fptr)(void));
* @return maa result type.
*/
maa_result_t
maa_gpio_isr_exit(maa_gpio_context *dev);
maa_gpio_isr_exit(maa_gpio_context dev);
/** Set GPIO Output Mode,
*
@@ -140,7 +131,7 @@ maa_gpio_isr_exit(maa_gpio_context *dev);
*
* @return maa result type.
*/
maa_result_t maa_gpio_mode(maa_gpio_context *dev, gpio_mode_t mode);
maa_result_t maa_gpio_mode(maa_gpio_context dev, gpio_mode_t mode);
/** Set GPIO direction
*
@@ -149,7 +140,7 @@ maa_result_t maa_gpio_mode(maa_gpio_context *dev, gpio_mode_t mode);
*
* @return maa result type.
*/
maa_result_t maa_gpio_dir(maa_gpio_context *dev, gpio_dir_t dir);
maa_result_t maa_gpio_dir(maa_gpio_context dev, gpio_dir_t dir);
/** Close the GPIO context
* - Will free the memory for the context and unexport the GPIO
@@ -158,7 +149,7 @@ maa_result_t maa_gpio_dir(maa_gpio_context *dev, gpio_dir_t dir);
*
* @return maa result type.
*/
maa_result_t maa_gpio_close(maa_gpio_context *dev);
maa_result_t maa_gpio_close(maa_gpio_context dev);
/** Unexport the GPIO context (maa_gpio_close() will call this function)
*
@@ -166,7 +157,7 @@ maa_result_t maa_gpio_close(maa_gpio_context *dev);
*
* @return maa result type.
*/
maa_result_t maa_gpio_unexport(maa_gpio_context *dev);
maa_result_t maa_gpio_unexport(maa_gpio_context dev);
/** Read the GPIO value.
*
@@ -174,7 +165,7 @@ maa_result_t maa_gpio_unexport(maa_gpio_context *dev);
*
* @return the integer value of the GPIO
*/
int maa_gpio_read(maa_gpio_context *dev);
int maa_gpio_read(maa_gpio_context dev);
/** Write to the GPIO Value.
*
@@ -183,7 +174,7 @@ int maa_gpio_read(maa_gpio_context *dev);
*
* @return maa result type
*/
maa_result_t maa_gpio_write(maa_gpio_context *dev, int value);
maa_result_t maa_gpio_write(maa_gpio_context dev, int value);
#ifdef __cplusplus
}

79
api/gpio.hpp Normal file
View File

@@ -0,0 +1,79 @@
/*
* 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 gpio C++ interface for libmaa
*
*/
#include "gpio.h"
namespace maa {
class Gpio {
public:
Gpio(int pin, bool raw=false) {
if (raw)
m_gpio = maa_gpio_init_raw(pin);
else
m_gpio = maa_gpio_init(pin);
}
~Gpio() {
maa_gpio_close(m_gpio);
}
maa_result_t edge(gpio_edge_t mode) {
return maa_gpio_edge_mode(m_gpio, mode);
}
#if defined(SWIGPYTHON)
maa_result_t isr(gpio_edge_t mode, PyObject *pyfunc) {
return maa_gpio_isr(m_gpio, mode, (void (*) ()) pyfunc);
}
#else
maa_result_t isr(gpio_edge_t mode, void (*fptr)(void)) {
return maa_gpio_isr(m_gpio, mode, fptr);
}
#endif
maa_result_t isr_exit() {
return maa_gpio_isr_exit(m_gpio);
}
maa_result_t mode(gpio_mode_t mode) {
return maa_gpio_mode(m_gpio, mode);
}
maa_result_t dir(gpio_dir_t dir) {
return maa_gpio_dir(m_gpio, dir);
}
int read() {
return maa_gpio_read(m_gpio);
}
int write(int value) {
return maa_gpio_write(m_gpio, value);
}
private:
maa_gpio_context m_gpio;
};
}

View File

@@ -60,13 +60,13 @@ main(int argc, char **argv)
fprintf(stdout, "MAA Version: %s\nStarting Blinking on IO%d\n",
maa_get_version(), iopin);
maa_gpio_context* gpio;
maa_gpio_context gpio;
gpio = maa_gpio_init(iopin);
if (gpio == NULL) {
fprintf(stderr, "Are you sure that pin%d you requested is valid on your platform?", iopin);
exit(1);
}
printf("Initialised pin%d which is atually pin%d\n", iopin, gpio->pin);
printf("Initialised pin%d\n", iopin);
// set direction to OUT
r = maa_gpio_dir(gpio, MAA_GPIO_OUT);

View File

@@ -34,7 +34,7 @@ main(int argc, char **argv)
fprintf(stdout, "MAA Version: %s\nStarting Read on IO6\n",
maa_get_version());
maa_gpio_context* gpio;
maa_gpio_context gpio;
gpio = maa_gpio_init(6);

View File

@@ -36,7 +36,7 @@ void interrupt (void) {
int main ()
{
maa_init();
maa_gpio_context* x;
maa_gpio_context x;
x = maa_gpio_init(6);
if (x == NULL) {

View File

@@ -6,6 +6,7 @@ include_directories(
set (maa_LIB_HEADERS
${PROJECT_SOURCE_DIR}/api/maa.h
${PROJECT_SOURCE_DIR}/api/gpio.h
${PROJECT_SOURCE_DIR}/api/gpio.hpp
${PROJECT_SOURCE_DIR}/api/i2c.h
${PROJECT_SOURCE_DIR}/api/i2c.hpp
${PROJECT_SOURCE_DIR}/api/pwm.h

View File

@@ -36,8 +36,26 @@
#define MAX_SIZE 64
#define POLL_TIMEOUT
/**
* A strucutre representing a gpio pin.
*/
struct _gpio {
/*@{*/
int pin; /**< the pin number, as known to the os. */
int value_fp; /**< the file pointer to the value of the gpio */
#ifdef SWIGPYTHON
PyObject *isr; /**< the interupt service request */
#else
void (* isr)(); /**< the interupt service request */
#endif
pthread_t thread_id; /**< the isr handler thread id */
int isr_value_fp; /**< the isr file pointer on the value */
/*@}*/
};
static maa_result_t
maa_gpio_get_valfp(maa_gpio_context *dev)
maa_gpio_get_valfp(maa_gpio_context dev)
{
char bu[MAX_SIZE];
sprintf(bu, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
@@ -50,7 +68,7 @@ maa_gpio_get_valfp(maa_gpio_context *dev)
return MAA_SUCCESS;
}
maa_gpio_context*
maa_gpio_context
maa_gpio_init(int pin)
{
int pinm = maa_check_gpio(pin);
@@ -60,7 +78,7 @@ maa_gpio_init(int pin)
return maa_gpio_init_raw(pinm);
}
maa_gpio_context*
maa_gpio_context
maa_gpio_init_raw(int pin)
{
if (pin < 0)
@@ -70,7 +88,7 @@ maa_gpio_init_raw(int pin)
char bu[MAX_SIZE];
int length;
maa_gpio_context* dev = (maa_gpio_context*) malloc(sizeof(maa_gpio_context));
maa_gpio_context dev = (maa_gpio_context) malloc(sizeof(struct _gpio));
memset(dev, 0, sizeof(maa_gpio_context));
dev->value_fp = -1;
dev->isr_value_fp = -1;
@@ -117,7 +135,7 @@ maa_gpio_wait_interrupt(int fd)
static void*
maa_gpio_interrupt_handler(void* arg)
{
maa_gpio_context* dev = (maa_gpio_context*) arg;
maa_gpio_context dev = (maa_gpio_context) arg;
maa_result_t ret;
// open gpio value with open(3)
@@ -154,7 +172,7 @@ maa_gpio_interrupt_handler(void* arg)
}
maa_result_t
maa_gpio_edge_mode(maa_gpio_context *dev, gpio_edge_t mode)
maa_gpio_edge_mode(maa_gpio_context dev, gpio_edge_t mode)
{
if (dev->value_fp != -1) {
close(dev->value_fp);
@@ -196,7 +214,7 @@ maa_gpio_edge_mode(maa_gpio_context *dev, gpio_edge_t mode)
}
maa_result_t
maa_gpio_isr(maa_gpio_context *dev, gpio_edge_t mode, void (*fptr)(void))
maa_gpio_isr(maa_gpio_context dev, gpio_edge_t mode, void (*fptr)(void))
{
// we only allow one isr per maa_gpio_context
if (dev->thread_id != 0) {
@@ -211,7 +229,7 @@ maa_gpio_isr(maa_gpio_context *dev, gpio_edge_t mode, void (*fptr)(void))
}
maa_result_t
maa_gpio_isr_exit(maa_gpio_context *dev)
maa_gpio_isr_exit(maa_gpio_context dev)
{
maa_result_t ret = MAA_SUCCESS;
@@ -248,7 +266,7 @@ maa_gpio_isr_exit(maa_gpio_context *dev)
}
maa_result_t
maa_gpio_mode(maa_gpio_context *dev, gpio_mode_t mode)
maa_gpio_mode(maa_gpio_context dev, gpio_mode_t mode)
{
if (dev->value_fp != -1) {
close(dev->value_fp);
@@ -290,7 +308,7 @@ maa_gpio_mode(maa_gpio_context *dev, gpio_mode_t mode)
}
maa_result_t
maa_gpio_dir(maa_gpio_context *dev, gpio_dir_t dir)
maa_gpio_dir(maa_gpio_context dev, gpio_dir_t dir)
{
if (dev == NULL) {
return MAA_ERROR_INVALID_HANDLE;
@@ -328,7 +346,7 @@ maa_gpio_dir(maa_gpio_context *dev, gpio_dir_t dir)
}
int
maa_gpio_read(maa_gpio_context *dev)
maa_gpio_read(maa_gpio_context dev)
{
if (dev->value_fp == -1) {
if (maa_gpio_get_valfp(dev) != MAA_SUCCESS) {
@@ -350,7 +368,7 @@ maa_gpio_read(maa_gpio_context *dev)
}
maa_result_t
maa_gpio_write(maa_gpio_context *dev, int value)
maa_gpio_write(maa_gpio_context dev, int value)
{
if (dev->value_fp == -1) {
maa_gpio_get_valfp(dev);
@@ -369,7 +387,7 @@ maa_gpio_write(maa_gpio_context *dev, int value)
}
maa_result_t
maa_gpio_unexport(maa_gpio_context *dev)
maa_gpio_unexport(maa_gpio_context dev)
{
FILE *unexport_f;
@@ -389,7 +407,7 @@ maa_gpio_unexport(maa_gpio_context *dev)
}
maa_result_t
maa_gpio_close(maa_gpio_context *dev)
maa_gpio_close(maa_gpio_context dev)
{
if (dev->value_fp != -1) {
close(dev->value_fp);

View File

@@ -68,7 +68,7 @@ maa_setup_mux_mapped(maa_pin_t meta)
{
int mi;
for (mi = 0; mi < meta.mux_total; mi++) {
maa_gpio_context* mux_i;
maa_gpio_context mux_i;
mux_i = maa_gpio_init_raw(meta.mux[mi].pin);
if (mux_i == NULL)
return MAA_ERROR_INVALID_HANDLE;
@@ -150,10 +150,10 @@ maa_check_pwm(int pin)
return NULL;
if (plat->pins[pin].capabilites.pwm != 1)
return NULL;
return NULL;
if (plat->pins[pin].capabilites.gpio == 1) {
maa_gpio_context* mux_i;
maa_gpio_context mux_i;
mux_i = maa_gpio_init_raw(plat->pins[pin].gpio.pinmap);
if (mux_i == NULL)
return NULL;

109
src/maa.i
View File

@@ -1,6 +1,6 @@
%{
#include "maa.h"
#include "gpio.h"
#include "gpio.hpp"
#include "pwm.hpp"
#include "i2c.hpp"
#include "spi.h"
@@ -17,112 +17,7 @@ const char * maa_get_version();
#### GPIO ####
typedef enum {
MAA_GPIO_STRONG = 0, /**< Default. Strong high and low */
MAA_GPIO_PULLUP = 1, /**< Resistive High */
MAA_GPIO_PULLDOWN = 2, /**< Resistive Low */
MAA_GPIO_HIZ = 3 /**< High Z State */
} gpio_mode_t;
typedef enum {
MAA_GPIO_OUT = 0, /**< Output. A Mode can also be set */
MAA_GPIO_IN = 1 /**< Input. */
} gpio_dir_t;
%nodefault maa_gpio_context;
%rename(Gpio) maa_gpio_context;
%ignore value_fp;
%feature("autodoc") maa_gpio_context "
Create a Gpio object and export it. Depending on your board the correct GPIO
value will be used. If raw is true then the pin that will be initialised will
be the hardcoded pin value in the kernel. Please see your board IO
documentation to understand exactly what will happen.
Parameters:
* pin: pin number read from the board, i.e IO3 is 3
* raw: set to True to use real pin value from the kernel";
typedef struct {
/*@{*/
int pin; /**< the pin number, as known to the os. */
FILE *value_fp; /**< the file pointer to the value of the gpio */
#if defined(SWIGPYTHON)
PyObject *isr; /**< the interupt service request */
#endif
pthread_t thread_id; /**< the isr handler thread id */
int isr_value_fp; /**< the isr file pointer on the value */
/*@}*/
} maa_gpio_context;
%typemap(check) PyObject *pyfunc {
if (!PyCallable_Check($1))
SWIG_exception(SWIG_TypeError,"Expected function.");
}
%extend maa_gpio_context {
maa_gpio_context(int pin, int raw=0)
{
if (raw)
return maa_gpio_init_raw(pin);
return maa_gpio_init(pin);
}
~maa_gpio_context()
{
maa_gpio_unexport($self);
}
%feature("autodoc") write "
Write a value to a GPIO pin
Parameters:
* value: value to write to GPIO";
int write(int value)
{
return maa_gpio_write($self, value);
}
%feature("autodoc") dir "
Set the gpio direction
Parameters:
* dir: GPIO direction";
int dir(gpio_dir_t dir)
{
return maa_gpio_dir($self, dir);
}
%feature("autodoc") read "
Read the value of a GPIO
Returns:
* value: GPIO value";
int read()
{
return maa_gpio_read($self);
}
%feature("autodoc") mode "
Set the GPIO mode
Parameters:
* mode: GPIO mode to set";
int mode(gpio_mode_t mode)
{
return maa_gpio_mode($self, mode);
}
#if defined(SWIGPYTHON)
//set python method as the isr function
int set_isr(PyObject *pyfunc)
{
Py_INCREF(pyfunc);
// do a nasty cast to get away from the warnings
maa_gpio_isr(self, MAA_GPIO_EDGE_BOTH, (void (*) ()) pyfunc);
return 0;
}
#else
%ignore maa_gpio_isr;
#endif
int isr_exit()
{
maa_gpio_isr_exit(self);
}
}
%include "gpio.hpp"
#### i2c ####