* 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
*
* @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
*
* @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
*
*
* @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
*
* @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.
* @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,
*
*
* @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
*
*
* @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
*
* @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)
*
*
* @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.
*
*
* @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.
*
*
* @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
}
--- /dev/null
+/*
+ * 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;
+};
+
+}
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);
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);
int main ()
{
maa_init();
- maa_gpio_context* x;
+ maa_gpio_context x;
x = maa_gpio_init(6);
if (x == NULL) {
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
#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);
return MAA_SUCCESS;
}
-maa_gpio_context*
+maa_gpio_context
maa_gpio_init(int pin)
{
int pinm = maa_check_gpio(pin);
return maa_gpio_init_raw(pinm);
}
-maa_gpio_context*
+maa_gpio_context
maa_gpio_init_raw(int pin)
{
if (pin < 0)
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;
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)
}
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);
}
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) {
}
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;
}
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);
}
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;
}
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) {
}
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);
}
maa_result_t
-maa_gpio_unexport(maa_gpio_context *dev)
+maa_gpio_unexport(maa_gpio_context dev)
{
FILE *unexport_f;
}
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);
{
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;
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;
%{
#include "maa.h"
- #include "gpio.h"
+ #include "gpio.hpp"
#include "pwm.hpp"
#include "i2c.hpp"
#include "spi.h"
#### 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 ####