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.
30 #if defined(SWIGJAVASCRIPT)
31 #if NODE_MODULE_VERSION >= 0x000D
39 // These enums must match the enums in gpio.h
45 MODE_STRONG = 0, /**< Default. Strong High and Low */
46 MODE_PULLUP = 1, /**< Interupt on rising & falling */
47 MODE_PULLDOWN = 2, /**< Interupt on rising only */
48 MODE_HIZ = 3 /**< Interupt on falling only */
52 * Gpio Direction options
55 DIR_OUT = 0, /**< Output. A Mode can also be set */
56 DIR_IN = 1, /**< Input */
57 DIR_OUT_HIGH = 2, /**< Output. Init High */
58 DIR_OUT_LOW = 3 /**< Output. Init Low */
62 * Gpio Edge types for interupts
65 EDGE_NONE = 0, /**< No interrupt on Gpio */
66 EDGE_BOTH = 1, /**< Interupt on rising & falling */
67 EDGE_RISING = 2, /**< Interupt on rising only */
68 EDGE_FALLING = 3 /**< Interupt on falling only */
72 * @brief API to General Purpose IO
74 * This file defines the gpio interface for libmraa
76 * @snippet Blink-IO.cpp Interesting
82 * Instanciates a Gpio object
84 * @param pin pin number to use
85 * @param owner (optional) Set pin owner, default behaviour is to 'own'
86 * the pin if we exported it. This means we will close it on destruct.
87 * Otherwise it will get left open. This is only valid in sysfs use
89 * @param raw (optional) Raw pins will use gpiolibs pin numbering from
90 * the kernel module. Note that you will not get any muxers set up for
91 * you so this may not always work as expected.
93 Gpio(int pin, bool owner = true, bool raw = false)
96 m_gpio = mraa_gpio_init_raw(pin);
98 m_gpio = mraa_gpio_init(pin);
101 if (m_gpio == NULL) {
102 throw std::invalid_argument("Invalid GPIO pin specified");
106 mraa_gpio_owner(m_gpio, 0);
110 * Gpio object destructor, this will only unexport the gpio if we where
115 mraa_gpio_close(m_gpio);
118 * Set the edge mode for ISR
120 * @param mode The edge mode to set
121 * @return Result of operation
126 return mraa_gpio_edge_mode(m_gpio, (gpio_edge_t) mode);
128 #if defined(SWIGPYTHON)
130 isr(Edge mode, PyObject* pyfunc, PyObject* args)
132 return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, (void (*) (void*)) pyfunc, (void*) args);
134 #elif defined(SWIGJAVASCRIPT)
136 v8isr(uv_work_t* req, int status)
138 mraa::Gpio* This = (mraa::Gpio*) req->data;
140 v8::Local<v8::Value> argv[] = { SWIGV8_INTEGER_NEW(-1) };
141 #if NODE_MODULE_VERSION >= 0x000D
142 v8::Local<v8::Function> f = v8::Local<v8::Function>::New(v8::Isolate::GetCurrent(), This->m_v8isr);
143 f->Call(SWIGV8_CURRENT_CONTEXT()->Global(), argc, argv);
145 This->m_v8isr->Call(SWIGV8_CURRENT_CONTEXT()->Global(), argc, argv);
159 uv_work_t* req = new uv_work_t;
161 uv_queue_work(uv_default_loop(), req, nop, v8isr);
165 isr(Edge mode, v8::Handle<v8::Function> func)
167 #if NODE_MODULE_VERSION >= 0x000D
168 m_v8isr.Reset(v8::Isolate::GetCurrent(), func);
170 m_v8isr = v8::Persistent<v8::Function>::New(func);
172 return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, &uvwork, this);
176 * Sets a callback to be called when pin value changes
178 * @param mode The edge mode to set
179 * @param fptr Function pointer to function to be called when interupt is
181 * @param args Arguments passed to the interrupt handler (fptr)
182 * @return Result of operation
185 isr(Edge mode, void (*fptr)(void*), void* args)
187 return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, fptr, args);
191 * Exits callback - this call will not kill the isr thread immediatly
192 * but only when it is out of it's critical section
194 * @return Result of operation
199 #if defined(SWIGJAVASCRIPT)
200 #if NODE_MODULE_VERSION >= 0x000D
207 return mraa_gpio_isr_exit(m_gpio);
212 * @param mode The mode to change the gpio into
213 * @return Result of operation
218 return mraa_gpio_mode(m_gpio, (gpio_mode_t) mode);
221 * Change Gpio direction
223 * @param dir The direction to change the gpio into
224 * @return Result of operation
229 return mraa_gpio_dir(m_gpio, (gpio_dir_t) dir);
232 * Read value from Gpio
239 return mraa_gpio_read(m_gpio);
242 * Write value to Gpio
244 * @param value Value to write to Gpio
245 * @return Result of operation
250 return mraa_gpio_write(m_gpio, value);
253 * Enable use of mmap i/o if available.
255 * @param enable true to use mmap
256 * @return Result of operation
261 return mraa_gpio_use_mmaped(m_gpio, (mraa_boolean_t) enable);
264 * Get pin number of Gpio. If raw param is True will return the
265 * number as used within sysfs
267 * @param raw (optional) get the raw gpio number.
271 getPin(bool raw = false)
274 return mraa_gpio_get_pin_raw(m_gpio);
276 return mraa_gpio_get_pin(m_gpio);
280 mraa_gpio_context m_gpio;
281 #if defined(SWIGJAVASCRIPT)
282 v8::Persistent<v8::Function> m_v8isr;