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.
32 // These enums must match the enums in gpio.h
38 MODE_STRONG = 0, /**< Default. Strong High and Low */
39 MODE_PULLUP = 1, /**< Interupt on rising & falling */
40 MODE_PULLDOWN = 2, /**< Interupt on rising only */
41 MODE_HIZ = 3 /**< Interupt on falling only */
45 * Gpio Direction options
48 DIR_OUT = 0, /**< Output. A Mode can also be set */
49 DIR_IN = 1 /**< Input */
53 * Gpio Edge types for interupts
56 EDGE_NONE = 0, /**< No interrupt on Gpio */
57 EDGE_BOTH = 1, /**< Interupt on rising & falling */
58 EDGE_RISING = 2, /**< Interupt on rising only */
59 EDGE_FALLING = 3 /**< Interupt on falling only */
63 * @brief API to General Purpose IO
65 * This file defines the gpio interface for libmraa
67 * @snippet Blink-IO.cpp Interesting
72 * Instanciates a Gpio object
74 * @param pin pin number to use
75 * @param owner (optional) Set pin owner, default behaviour is to 'own'
76 * the pin if we exported it. This means we will close it on destruct.
77 * Otherwise it will get left open. This is only valid in sysfs use
79 * @param raw (optional) Raw pins will use gpiolibs pin numbering from
80 * the kernel module. Note that you will not get any muxers set up for
81 * you so this may not always work as expected.
83 Gpio(int pin, bool owner=true, bool raw=false) {
85 m_gpio = mraa_gpio_init_raw(pin);
88 m_gpio = mraa_gpio_init(pin);
92 throw std::invalid_argument("Invalid GPIO pin specified");
96 mraa_gpio_owner(m_gpio, 0);
100 * Gpio object destructor, this will only unexport the gpio if we where
104 mraa_gpio_close(m_gpio);
107 * Set the edge mode for ISR
109 * @param mode The edge mode to set
110 * @return Result of operation
112 mraa_result_t edge(Edge mode) {
113 return mraa_gpio_edge_mode(m_gpio, (gpio_edge_t) mode);
115 #if defined(SWIGPYTHON)
116 mraa_result_t isr(Edge mode, PyObject *pyfunc, PyObject* args) {
117 return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, (void (*) (void *)) pyfunc, (void *) args);
121 * Sets a callback to be called when pin value changes
123 * @param mode The edge mode to set
124 * @param fptr Function pointer to function to be called when interupt is
126 * @param args Arguments passed to the interrupt handler (fptr)
127 * @return Result of operation
129 mraa_result_t isr(Edge mode, void (*fptr)(void *), void * args) {
130 return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, fptr, args);
134 * Exits callback - this call will not kill the isr thread imediatlu
135 * but only when it is out of it's critical section
137 * @return Result of operation
139 mraa_result_t isrExit() {
140 return mraa_gpio_isr_exit(m_gpio);
145 * @param mode The mode to change the gpio into
146 * @return Result of operation
148 mraa_result_t mode(Mode mode) {
149 return mraa_gpio_mode(m_gpio, (gpio_mode_t) mode);
152 * Change Gpio direction
154 * @param dir The direction to change the gpio into
155 * @return Result of operation
157 mraa_result_t dir(Dir dir) {
158 return mraa_gpio_dir(m_gpio, (gpio_dir_t) dir);
161 * Read value from Gpio
166 return mraa_gpio_read(m_gpio);
169 * Write value to Gpio
171 * @param value Value to write to Gpio
172 * @return Result of operation
174 mraa_result_t write(int value) {
175 return mraa_gpio_write(m_gpio, value);
178 * Enable use of mmap i/o if available.
180 * @param enable true to use mmap
181 * @return Result of operation
183 mraa_result_t useMmap(bool enable) {
184 return mraa_gpio_use_mmaped(m_gpio, (mraa_boolean_t) enable);
187 * Get pin number of Gpio
192 return mraa_gpio_get_pin(m_gpio);
195 mraa_gpio_context m_gpio;