b4dc7acacc31f77df4c138f7c98f7cc5b90225f3
[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 // These enums must match the enums in gpio.h
39
40 /**
41  * Gpio Output modes
42  */
43 typedef enum {
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 */
48 } Mode;
49
50 /**
51  * Gpio Direction options
52  */
53 typedef enum {
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 */
58 } Dir;
59
60 /**
61  * Gpio Edge types for interupts
62  */
63 typedef enum {
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 */
68 } Edge;
69
70 /**
71  * @brief API to General Purpose IO
72  *
73  * This file defines the gpio interface for libmraa
74  *
75  * @snippet Blink-IO.cpp Interesting
76  */
77 class Gpio {
78     public:
79         /**
80          * Instanciates a Gpio object
81          *
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
86          * cases
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.
90          */
91         Gpio(int pin, bool owner=true, bool raw=false) {
92             if (raw) {
93                 m_gpio = mraa_gpio_init_raw(pin);
94             }
95             else {
96                 m_gpio = mraa_gpio_init(pin);
97             }
98
99             if (m_gpio == NULL) {
100                 throw std::invalid_argument("Invalid GPIO pin specified");
101             }
102
103             if (!owner) {
104                 mraa_gpio_owner(m_gpio, 0);
105             }
106         }
107         /**
108          * Gpio object destructor, this will only unexport the gpio if we where
109          * the owner
110          */
111         ~Gpio() {
112             mraa_gpio_close(m_gpio);
113         }
114         /**
115          * Set the edge mode for ISR
116          *
117          * @param mode The edge mode to set
118          * @return Result of operation
119          */
120         mraa_result_t edge(Edge mode) {
121             return mraa_gpio_edge_mode(m_gpio, (gpio_edge_t) mode);
122         }
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);
126         }
127 #elif defined(SWIGJAVASCRIPT)
128         static void v8isr(uv_work_t* req, int status) {
129             mraa::Gpio *This = (mraa::Gpio *)req->data;
130             int argc = 1;
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);
135 #else
136             This->m_v8isr->Call(SWIGV8_CURRENT_CONTEXT()->Global(), argc, argv);
137 #endif
138             delete req;
139         }
140
141         static void nop(uv_work_t* req)
142         {
143             // Do nothing.
144         }
145
146         static void uvwork(void *ctx) {
147             uv_work_t* req = new uv_work_t;
148             req->data = ctx;
149             uv_queue_work(uv_default_loop(), req, nop, v8isr);
150         }
151
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);
155 #else
156             m_v8isr = v8::Persistent<v8::Function>::New(func);
157 #endif
158             return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, &uvwork, this);
159         }
160 #else
161         /**
162          * Sets a callback to be called when pin value changes
163          *
164          * @param mode The edge mode to set
165          * @param fptr Function pointer to function to be called when interupt is
166          * triggered
167          * @param args Arguments passed to the interrupt handler (fptr)
168          * @return Result of operation
169          */
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);
172         }
173 #endif
174         /**
175          * Exits callback - this call will not kill the isr thread imediatlu
176          * but only when it is out of it's critical section
177          *
178          * @return Result of operation
179          */
180         mraa_result_t isrExit() {
181 #if defined(SWIGJAVASCRIPT)
182   #if NODE_MODULE_VERSION >= 0x000D
183             m_v8isr.Reset();
184   #else
185             m_v8isr.Dispose();
186             m_v8isr.Clear();
187   #endif
188 #endif
189             return mraa_gpio_isr_exit(m_gpio);
190         }
191         /**
192          * Change Gpio mode
193          *
194          * @param mode The mode to change the gpio into
195          * @return Result of operation
196          */
197         mraa_result_t mode(Mode mode) {
198             return mraa_gpio_mode(m_gpio, (gpio_mode_t) mode);
199         }
200         /**
201          * Change Gpio direction
202          *
203          * @param dir The direction to change the gpio into
204          * @return Result of operation
205          */
206         mraa_result_t dir(Dir dir) {
207             return mraa_gpio_dir(m_gpio, (gpio_dir_t) dir);
208         }
209         /**
210          * Read value from Gpio
211          *
212          * @return Gpio value
213          */
214         int read() {
215             return mraa_gpio_read(m_gpio);
216         }
217         /**
218          * Write value to Gpio
219          *
220          * @param value Value to write to Gpio
221          * @return Result of operation
222          */
223         mraa_result_t write(int value) {
224             return mraa_gpio_write(m_gpio, value);
225         }
226         /**
227          * Enable use of mmap i/o if available.
228          *
229          * @param enable true to use mmap
230          * @return Result of operation
231          */
232         mraa_result_t useMmap(bool enable) {
233             return mraa_gpio_use_mmaped(m_gpio, (mraa_boolean_t) enable);
234         }
235         /**
236          * Get pin number of Gpio. If raw param is True will return the
237          * number as used within sysfs
238          *
239          * @param raw (optional) get the raw gpio number.
240          * @return Pin number
241          */
242         int getPin(bool raw = false) {
243             if (raw) {
244                 return mraa_gpio_get_pin_raw(m_gpio);
245             }
246             return mraa_gpio_get_pin(m_gpio);
247         }
248     private:
249         mraa_gpio_context m_gpio;
250 #if defined(SWIGJAVASCRIPT)
251         v8::Persistent<v8::Function> m_v8isr;
252 #endif
253 };
254
255 }