mraa  0.9.6
Low Level Skeleton Library for Communication on GNU/Linux platforms
 All Data Structures Namespaces Files Functions Typedefs Enumerations Enumerator Macros Pages
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 "types.hpp"
29 #include <stdexcept>
30 
31 #if defined(SWIGJAVASCRIPT)
32 #if NODE_MODULE_VERSION >= 0x000D
33 #include <uv.h>
34 #endif
35 #endif
36 
37 namespace mraa
38 {
39 
40 // These enums must match the enums in gpio.h
41 
45 typedef enum {
49  MODE_HIZ = 3
50 } Mode;
51 
55 typedef enum {
56  DIR_OUT = 0,
57  DIR_IN = 1,
60 } Dir;
61 
65 typedef enum {
66  EDGE_NONE = 0,
67  EDGE_BOTH = 1,
70 } Edge;
71 
79 class Gpio
80 {
81  public:
94  Gpio(int pin, bool owner = true, bool raw = false)
95  {
96  if (raw) {
97  m_gpio = mraa_gpio_init_raw(pin);
98  } else {
99  m_gpio = mraa_gpio_init(pin);
100  }
101 
102  if (m_gpio == NULL) {
103  throw std::invalid_argument("Invalid GPIO pin specified");
104  }
105 
106  if (!owner) {
107  mraa_gpio_owner(m_gpio, 0);
108  }
109  }
115  {
116  mraa_gpio_close(m_gpio);
117  }
124  Result
126  {
127  return (Result) mraa_gpio_edge_mode(m_gpio, (mraa_gpio_edge_t) mode);
128  }
129 #if defined(SWIGPYTHON)
130  Result
131  isr(Edge mode, PyObject* pyfunc, PyObject* args)
132  {
133  return (Result) mraa_gpio_isr(m_gpio, (mraa_gpio_edge_t) mode, (void (*) (void*)) pyfunc, (void*) args);
134  }
135 #elif defined(SWIGJAVASCRIPT)
136  static void
137  v8isr(uv_work_t* req, int status)
138  {
139 #if NODE_MODULE_VERSION >= 0x000D
140  v8::HandleScope scope(v8::Isolate::GetCurrent());
141 #endif
142  mraa::Gpio* This = (mraa::Gpio*) req->data;
143  int argc = 1;
144  v8::Local<v8::Value> argv[] = { SWIGV8_INTEGER_NEW(-1) };
145 #if NODE_MODULE_VERSION >= 0x000D
146  v8::Local<v8::Function> f = v8::Local<v8::Function>::New(v8::Isolate::GetCurrent(), This->m_v8isr);
147  f->Call(SWIGV8_CURRENT_CONTEXT()->Global(), argc, argv);
148 #else
149  This->m_v8isr->Call(SWIGV8_CURRENT_CONTEXT()->Global(), argc, argv);
150 #endif
151  delete req;
152  }
153 
154  static void
155  nop(uv_work_t* req)
156  {
157  // Do nothing.
158  }
159 
160  static void
161  uvwork(void* ctx)
162  {
163  uv_work_t* req = new uv_work_t;
164  req->data = ctx;
165  uv_queue_work(uv_default_loop(), req, nop, v8isr);
166  }
167 
168  Result
169  isr(Edge mode, v8::Handle<v8::Function> func)
170  {
171 #if NODE_MODULE_VERSION >= 0x000D
172  m_v8isr.Reset(v8::Isolate::GetCurrent(), func);
173 #else
174  m_v8isr = v8::Persistent<v8::Function>::New(func);
175 #endif
176  return (Result) mraa_gpio_isr(m_gpio, (mraa_gpio_edge_t) mode, &uvwork, this);
177  }
178 #elif defined(SWIGJAVA) || defined(JAVACALLBACK)
179  Result
180  isr(Edge mode, jobject runnable)
181  {
182  return (Result) mraa_gpio_isr(m_gpio, (mraa_gpio_edge_t) mode, mraa_java_isr_callback, runnable);
183  }
184 #endif
185 
194  Result
195  isr(Edge mode, void (*fptr)(void*), void* args)
196  {
197  return (Result) mraa_gpio_isr(m_gpio, (mraa_gpio_edge_t) mode, fptr, args);
198  }
199 
206  Result
208  {
209 #if defined(SWIGJAVASCRIPT)
210 #if NODE_MODULE_VERSION >= 0x000D
211  m_v8isr.Reset();
212 #else
213  m_v8isr.Dispose();
214  m_v8isr.Clear();
215 #endif
216 #endif
217  return (Result) mraa_gpio_isr_exit(m_gpio);
218  }
225  Result
226  mode(Mode mode)
227  {
228  return (Result )mraa_gpio_mode(m_gpio, (mraa_gpio_mode_t) mode);
229  }
236  Result
237  dir(Dir dir)
238  {
239  return (Result )mraa_gpio_dir(m_gpio, (mraa_gpio_dir_t) dir);
240  }
241 
248  Dir
250  {
251  mraa_gpio_dir_t dir;
252  if (mraa_gpio_read_dir(m_gpio, &dir) != MRAA_SUCCESS) {
253  throw std::runtime_error("Failed to read direction");
254  }
255  return (Dir) dir;
256  }
257 
263  int
265  {
266  return mraa_gpio_read(m_gpio);
267  }
274  Result
275  write(int value)
276  {
277  return (Result) mraa_gpio_write(m_gpio, value);
278  }
285  Result
286  useMmap(bool enable)
287  {
288  return (Result) mraa_gpio_use_mmaped(m_gpio, (mraa_boolean_t) enable);
289  }
297  int
298  getPin(bool raw = false)
299  {
300  if (raw) {
301  return mraa_gpio_get_pin_raw(m_gpio);
302  }
303  return mraa_gpio_get_pin(m_gpio);
304  }
305 
306  private:
307  mraa_gpio_context m_gpio;
308 #if defined(SWIGJAVASCRIPT)
309  v8::Persistent<v8::Function> m_v8isr;
310 #endif
311 };
312 }
int read()
Definition: gpio.hpp:264
Result
Definition: types.hpp:185
mraa_result_t mraa_gpio_owner(mraa_gpio_context dev, mraa_boolean_t owner)
API to General Purpose IO.
Definition: gpio.hpp:79
Result write(int value)
Definition: gpio.hpp:275
Definition: gpio.hpp:69
mraa_result_t mraa_gpio_isr(mraa_gpio_context dev, mraa_gpio_edge_t edge, void(*fptr)(void *), void *args)
Definition: gpio.hpp:49
General Purpose IO.
unsigned int mraa_boolean_t
Definition: common.h:60
Definition: types.h:194
Gpio(int pin, bool owner=true, bool raw=false)
Definition: gpio.hpp:94
mraa_result_t mraa_gpio_isr_exit(mraa_gpio_context dev)
Definition: gpio.hpp:68
Definition: gpio.hpp:47
Result isr(Edge mode, void(*fptr)(void *), void *args)
Definition: gpio.hpp:195
mraa_result_t mraa_gpio_close(mraa_gpio_context dev)
~Gpio()
Definition: gpio.hpp:114
mraa_result_t mraa_gpio_dir(mraa_gpio_context dev, mraa_gpio_dir_t dir)
Dir
Definition: gpio.hpp:55
mraa_gpio_dir_t
Definition: gpio.h:76
Definition: gpio.hpp:48
mraa_gpio_mode_t
Definition: gpio.h:66
Definition: gpio.hpp:59
Edge
Definition: gpio.hpp:65
mraa_result_t mraa_gpio_mode(mraa_gpio_context dev, mraa_gpio_mode_t mode)
int mraa_gpio_get_pin(mraa_gpio_context dev)
Definition: gpio.hpp:66
mraa_result_t mraa_gpio_edge_mode(mraa_gpio_context dev, mraa_gpio_edge_t mode)
Result edge(Edge mode)
Definition: gpio.hpp:125
mraa_gpio_edge_t
Definition: gpio.h:86
mraa_gpio_context mraa_gpio_init_raw(int gpiopin)
Result isrExit()
Definition: gpio.hpp:207
struct _gpio * mraa_gpio_context
Definition: gpio.h:61
mraa_gpio_context mraa_gpio_init(int pin)
Result dir(Dir dir)
Definition: gpio.hpp:237
Definition: gpio.hpp:56
int mraa_gpio_get_pin_raw(mraa_gpio_context dev)
int getPin(bool raw=false)
Definition: gpio.hpp:298
mraa_result_t mraa_gpio_read_dir(mraa_gpio_context dev, mraa_gpio_dir_t *dir)
Result useMmap(bool enable)
Definition: gpio.hpp:286
mraa_result_t mraa_gpio_write(mraa_gpio_context dev, int value)
mraa_result_t mraa_gpio_use_mmaped(mraa_gpio_context dev, mraa_boolean_t mmap)
Dir readDir()
Definition: gpio.hpp:249
Result mode(Mode mode)
Definition: gpio.hpp:226
int mraa_gpio_read(mraa_gpio_context dev)
Mode
Definition: gpio.hpp:45
Definition: gpio.hpp:67
Definition: gpio.hpp:46
Definition: gpio.hpp:57
Definition: gpio.hpp:58