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
38 // These enums must match the enums in gpio.h
44 MODE_STRONG = 0, /**< Default. Strong High and Low */
45 MODE_PULLUP = 1, /**< Interupt on rising & falling */
46 MODE_PULLDOWN = 2, /**< Interupt on rising only */
47 MODE_HIZ = 3 /**< Interupt on falling only */
51 * Gpio Direction options
54 DIR_OUT = 0, /**< Output. A Mode can also be set */
55 DIR_IN = 1, /**< Input */
56 DIR_OUT_HIGH = 2, /**< Output. Init High */
57 DIR_OUT_LOW = 3 /**< Output. Init Low */
61 * Gpio Edge types for interupts
64 EDGE_NONE = 0, /**< No interrupt on Gpio */
65 EDGE_BOTH = 1, /**< Interupt on rising & falling */
66 EDGE_RISING = 2, /**< Interupt on rising only */
67 EDGE_FALLING = 3 /**< Interupt on falling only */
71 * @brief API to General Purpose IO
73 * This file defines the gpio interface for libmraa
75 * @snippet Blink-IO.cpp Interesting
80 * Instanciates a Gpio object
82 * @param pin pin number to use
83 * @param owner (optional) Set pin owner, default behaviour is to 'own'
84 * the pin if we exported it. This means we will close it on destruct.
85 * Otherwise it will get left open. This is only valid in sysfs use
87 * @param raw (optional) Raw pins will use gpiolibs pin numbering from
88 * the kernel module. Note that you will not get any muxers set up for
89 * you so this may not always work as expected.
91 Gpio(int pin, bool owner=true, bool raw=false) {
93 m_gpio = mraa_gpio_init_raw(pin);
96 m_gpio = mraa_gpio_init(pin);
100 throw std::invalid_argument("Invalid GPIO pin specified");
104 mraa_gpio_owner(m_gpio, 0);
108 * Gpio object destructor, this will only unexport the gpio if we where
112 mraa_gpio_close(m_gpio);
115 * Set the edge mode for ISR
117 * @param mode The edge mode to set
118 * @return Result of operation
120 mraa_result_t edge(Edge mode) {
121 return mraa_gpio_edge_mode(m_gpio, (gpio_edge_t) mode);
123 #if defined(SWIGPYTHON)
124 mraa_result_t isr(Edge mode, PyObject *pyfunc, PyObject* args) {
125 return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, (void (*) (void *)) pyfunc, (void *) args);
127 #elif defined(SWIGJAVASCRIPT)
128 static void v8isr(uv_work_t* req, int status) {
129 mraa::Gpio *This = (mraa::Gpio *)req->data;
131 v8::Local<v8::Value> argv[] = { SWIGV8_INTEGER_NEW(-1) };
132 #if NODE_MODULE_VERSION >= 0x000D
133 v8::Local<v8::Function> f = v8::Local<v8::Function>::New(v8::Isolate::GetCurrent(), This->m_v8isr);
134 f->Call(SWIGV8_CURRENT_CONTEXT()->Global(), argc, argv);
136 This->m_v8isr->Call(SWIGV8_CURRENT_CONTEXT()->Global(), argc, argv);
141 static void nop(uv_work_t* req)
146 static void uvwork(void *ctx) {
147 uv_work_t* req = new uv_work_t;
149 uv_queue_work(uv_default_loop(), req, nop, v8isr);
152 mraa_result_t isr(Edge mode, v8::Handle<v8::Function> func) {
153 #if NODE_MODULE_VERSION >= 0x000D
154 m_v8isr.Reset(v8::Isolate::GetCurrent(), func);
156 m_v8isr = v8::Persistent<v8::Function>::New(func);
158 return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, &uvwork, this);
162 * Sets a callback to be called when pin value changes
164 * @param mode The edge mode to set
165 * @param fptr Function pointer to function to be called when interupt is
167 * @param args Arguments passed to the interrupt handler (fptr)
168 * @return Result of operation
170 mraa_result_t isr(Edge mode, void (*fptr)(void *), void * args) {
171 return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, fptr, args);
175 * Exits callback - this call will not kill the isr thread imediatlu
176 * but only when it is out of it's critical section
178 * @return Result of operation
180 mraa_result_t isrExit() {
181 #if defined(SWIGJAVASCRIPT)
182 #if NODE_MODULE_VERSION >= 0x000D
189 return mraa_gpio_isr_exit(m_gpio);
194 * @param mode The mode to change the gpio into
195 * @return Result of operation
197 mraa_result_t mode(Mode mode) {
198 return mraa_gpio_mode(m_gpio, (gpio_mode_t) mode);
201 * Change Gpio direction
203 * @param dir The direction to change the gpio into
204 * @return Result of operation
206 mraa_result_t dir(Dir dir) {
207 return mraa_gpio_dir(m_gpio, (gpio_dir_t) dir);
210 * Read value from Gpio
215 return mraa_gpio_read(m_gpio);
218 * Write value to Gpio
220 * @param value Value to write to Gpio
221 * @return Result of operation
223 mraa_result_t write(int value) {
224 return mraa_gpio_write(m_gpio, value);
227 * Enable use of mmap i/o if available.
229 * @param enable true to use mmap
230 * @return Result of operation
232 mraa_result_t useMmap(bool enable) {
233 return mraa_gpio_use_mmaped(m_gpio, (mraa_boolean_t) enable);
236 * Get pin number of Gpio. If raw param is True will return the
237 * number as used within sysfs
239 * @param raw (optional) get the raw gpio number.
242 int getPin(bool raw = false) {
244 return mraa_gpio_get_pin_raw(m_gpio);
246 return mraa_gpio_get_pin(m_gpio);
249 mraa_gpio_context m_gpio;
250 #if defined(SWIGJAVASCRIPT)
251 v8::Persistent<v8::Function> m_v8isr;