2 * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
5 * Permission is hereby granted, free of charge, to any person obtaining
6 * a copy of this software and associated documentation files (the
7 * "Software"), to deal in the Software without restriction, including
8 * without limitation the rights to use, copy, modify, merge, publish,
9 * distribute, sublicense, and/or sell copies of the Software, and to
10 * permit persons to whom the Software is furnished to do so, subject to
11 * the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 // These enums must match the enums in gpio.h
37 MODE_STRONG = 0, /**< No interrupt on Gpio */
38 MODE_PULLUP = 1, /**< Interupt on rising & falling */
39 MODE_PULLDOWN = 2, /**< Interupt on rising only */
40 MODE_HIZ = 3 /**< Interupt on falling only */
44 * Gpio Direction options
47 DIR_OUT = 0, /**< Output. A Mode can also be set */
48 DIR_IN = 1 /**< Input */
52 * Gpio Edge types for interupts
55 EDGE_NONE = 0, /**< No interrupt on Gpio */
56 EDGE_BOTH = 1, /**< Interupt on rising & falling */
57 EDGE_RISING = 2, /**< Interupt on rising only */
58 EDGE_FALLING = 3 /**< Interupt on falling only */
62 * @brief C++ API to General Purpose IO
64 * This file defines the gpio C++ interface for libmraa
66 * @snippet Blink-IO.cpp Interesting
71 * Instanciates a Gpio object
73 * @param pin pin number to use
74 * @param owner (optional) Set pin owner, default behaviour is to 'own'
75 * the pin if we exported it. This means we will close it on destruct.
76 * Otherwise it will get left open. This is only valid in sysfs use
78 * @param raw (optional) Raw pins will use gpiolibs pin numbering from
79 * the kernel module. Note that you will not get any muxers set up for
80 * you so this may not always work as expected.
82 Gpio(int pin, bool owner=true, bool raw=false) {
84 m_gpio = mraa_gpio_init_raw(pin);
86 m_gpio = mraa_gpio_init(pin);
88 mraa_gpio_owner(m_gpio, 0);
91 * Gpio object destructor, this will only unexport the gpio if we where
95 mraa_gpio_close(m_gpio);
98 * Set the edge mode for ISR
100 * @param mode The edge mode to set
101 * @return Result of operation
103 mraa_result_t edge(Edge mode) {
104 return mraa_gpio_edge_mode(m_gpio, (gpio_edge_t) mode);
106 #if defined(SWIGPYTHON)
107 mraa_result_t isr(Edge mode, PyObject *pyfunc, PyObject* args) {
108 return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, (void (*) (void *)) pyfunc, (void *) args);
112 * Sets a callback to be called when pin value changes
114 * @param mode The edge mode to set
115 * @param fptr Function pointer to function to be called when interupt is
117 * @param args Arguments passed to the interrupt handler (fptr)
118 * @return Result of operation
120 mraa_result_t isr(Edge mode, void (*fptr)(void *), void * args) {
121 return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, fptr, args);
125 * Exits callback - this call will not kill the isr thread imediatlu
126 * but only when it is out of it's critical section
128 * @return Result of operation
130 mraa_result_t isrExit() {
131 return mraa_gpio_isr_exit(m_gpio);
136 * @param mode The mode to change the gpio into
137 * @return Result of operation
139 mraa_result_t mode(Mode mode) {
140 return mraa_gpio_mode(m_gpio, (gpio_mode_t) mode);
143 * Change Gpio direction
145 * @param dir The direction to change the gpio into
146 * @return Result of operation
148 mraa_result_t dir(Dir dir) {
149 return mraa_gpio_dir(m_gpio, (gpio_dir_t) dir);
152 * Read value from Gpio
157 return mraa_gpio_read(m_gpio);
160 * Write value to Gpio
162 * @param value Value to write to Gpio
163 * @return Result of operation
165 mraa_result_t write(int value) {
166 return mraa_gpio_write(m_gpio, value);
169 mraa_gpio_context m_gpio;