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

@@ -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 ####