a5dc24b3693dcf46e149c6ffed9c32f0ff22cd32
[contrib/mraa.git] / api / mraa / gpio.hpp
1 /*
2  * Author: Brendan Le Foll <brendan.le.foll@intel.com>
3  * Copyright (c) 2014 Intel Corporation.
4  *
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:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
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.
23  */
24
25 #pragma once
26
27 #include "gpio.h"
28 #include <stdexcept>
29
30 #if defined(SWIGJAVASCRIPT)
31 #if NODE_MODULE_VERSION >= 0x000D
32 #include <uv.h>
33 #endif
34 #endif
35
36 namespace mraa
37 {
38
39 // These enums must match the enums in gpio.h
40
41 /**
42  * Gpio Output modes
43  */
44 typedef enum {
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 */
49 } Mode;
50
51 /**
52  * Gpio Direction options
53  */
54 typedef enum {
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 */
59 } Dir;
60
61 /**
62  * Gpio Edge types for interupts
63  */
64 typedef enum {
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 */
69 } Edge;
70
71 /**
72  * @brief API to General Purpose IO
73  *
74  * This file defines the gpio interface for libmraa
75  *
76  * @snippet Blink-IO.cpp Interesting
77  */
78 class Gpio
79 {
80   public:
81     /**
82      * Instanciates a Gpio object
83      *
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
88      * cases
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.
92      */
93     Gpio(int pin, bool owner = true, bool raw = false)
94     {
95         if (raw) {
96             m_gpio = mraa_gpio_init_raw(pin);
97         } else {
98             m_gpio = mraa_gpio_init(pin);
99         }
100
101         if (m_gpio == NULL) {
102             throw std::invalid_argument("Invalid GPIO pin specified");
103         }
104
105         if (!owner) {
106             mraa_gpio_owner(m_gpio, 0);
107         }
108     }
109     /**
110      * Gpio object destructor, this will only unexport the gpio if we where
111      * the owner
112      */
113     ~Gpio()
114     {
115         mraa_gpio_close(m_gpio);
116     }
117     /**
118      * Set the edge mode for ISR
119      *
120      * @param mode The edge mode to set
121      * @return Result of operation
122      */
123     mraa_result_t
124     edge(Edge mode)
125     {
126         return mraa_gpio_edge_mode(m_gpio, (gpio_edge_t) mode);
127     }
128 #if defined(SWIGPYTHON)
129     mraa_result_t
130     isr(Edge mode, PyObject* pyfunc, PyObject* args)
131     {
132         return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, (void (*) (void*)) pyfunc, (void*) args);
133     }
134 #elif defined(SWIGJAVASCRIPT)
135     static void
136     v8isr(uv_work_t* req, int status)
137     {
138         mraa::Gpio* This = (mraa::Gpio*) req->data;
139         int argc = 1;
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);
144 #else
145         This->m_v8isr->Call(SWIGV8_CURRENT_CONTEXT()->Global(), argc, argv);
146 #endif
147         delete req;
148     }
149
150     static void
151     nop(uv_work_t* req)
152     {
153         // Do nothing.
154     }
155
156     static void
157     uvwork(void* ctx)
158     {
159         uv_work_t* req = new uv_work_t;
160         req->data = ctx;
161         uv_queue_work(uv_default_loop(), req, nop, v8isr);
162     }
163
164     mraa_result_t
165     isr(Edge mode, v8::Handle<v8::Function> func)
166     {
167 #if NODE_MODULE_VERSION >= 0x000D
168         m_v8isr.Reset(v8::Isolate::GetCurrent(), func);
169 #else
170         m_v8isr = v8::Persistent<v8::Function>::New(func);
171 #endif
172         return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, &uvwork, this);
173     }
174 #else
175     /**
176      * Sets a callback to be called when pin value changes
177      *
178      * @param mode The edge mode to set
179      * @param fptr Function pointer to function to be called when interupt is
180      * triggered
181      * @param args Arguments passed to the interrupt handler (fptr)
182      * @return Result of operation
183      */
184     mraa_result_t
185     isr(Edge mode, void (*fptr)(void*), void* args)
186     {
187         return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, fptr, args);
188     }
189 #endif
190     /**
191      * Exits callback - this call will not kill the isr thread immediatly
192      * but only when it is out of it's critical section
193      *
194      * @return Result of operation
195      */
196     mraa_result_t
197     isrExit()
198     {
199 #if defined(SWIGJAVASCRIPT)
200 #if NODE_MODULE_VERSION >= 0x000D
201         m_v8isr.Reset();
202 #else
203         m_v8isr.Dispose();
204         m_v8isr.Clear();
205 #endif
206 #endif
207         return mraa_gpio_isr_exit(m_gpio);
208     }
209     /**
210      * Change Gpio mode
211      *
212      * @param mode The mode to change the gpio into
213      * @return Result of operation
214      */
215     mraa_result_t
216     mode(Mode mode)
217     {
218         return mraa_gpio_mode(m_gpio, (gpio_mode_t) mode);
219     }
220     /**
221      * Change Gpio direction
222      *
223      * @param dir The direction to change the gpio into
224      * @return Result of operation
225      */
226     mraa_result_t
227     dir(Dir dir)
228     {
229         return mraa_gpio_dir(m_gpio, (gpio_dir_t) dir);
230     }
231     /**
232      * Read value from Gpio
233      *
234      * @return Gpio value
235      */
236     int
237     read()
238     {
239         return mraa_gpio_read(m_gpio);
240     }
241     /**
242      * Write value to Gpio
243      *
244      * @param value Value to write to Gpio
245      * @return Result of operation
246      */
247     mraa_result_t
248     write(int value)
249     {
250         return mraa_gpio_write(m_gpio, value);
251     }
252     /**
253      * Enable use of mmap i/o if available.
254      *
255      * @param enable true to use mmap
256      * @return Result of operation
257      */
258     mraa_result_t
259     useMmap(bool enable)
260     {
261         return mraa_gpio_use_mmaped(m_gpio, (mraa_boolean_t) enable);
262     }
263     /**
264      * Get pin number of Gpio. If raw param is True will return the
265      * number as used within sysfs
266      *
267      * @param raw (optional) get the raw gpio number.
268      * @return Pin number
269      */
270     int
271     getPin(bool raw = false)
272     {
273         if (raw) {
274             return mraa_gpio_get_pin_raw(m_gpio);
275         }
276         return mraa_gpio_get_pin(m_gpio);
277     }
278
279   private:
280     mraa_gpio_context m_gpio;
281 #if defined(SWIGJAVASCRIPT)
282     v8::Persistent<v8::Function> m_v8isr;
283 #endif
284 };
285 }