mraa  1.7.0
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  }
116  Gpio(void* gpio_context)
117  {
118  m_gpio = (mraa_gpio_context) gpio_context;
119  if (m_gpio == NULL) {
120  throw std::invalid_argument("Invalid GPIO context");
121  }
122  }
128  {
129  mraa_gpio_close(m_gpio);
130  }
137  Result
139  {
140  return (Result) mraa_gpio_edge_mode(m_gpio, (mraa_gpio_edge_t) mode);
141  }
142 #if defined(SWIGPYTHON)
143  Result
144  isr(Edge mode, PyObject* pyfunc, PyObject* args)
145  {
146  return (Result) mraa_gpio_isr(m_gpio, (mraa_gpio_edge_t) mode, (void (*) (void*)) pyfunc, (void*) args);
147  }
148 #elif defined(SWIGJAVASCRIPT)
149  static void
150  v8isr(uv_work_t* req, int status)
151  {
152 #if NODE_MODULE_VERSION >= 0x000D
153  v8::HandleScope scope(v8::Isolate::GetCurrent());
154 #endif
155  mraa::Gpio* This = (mraa::Gpio*) req->data;
156  int argc = 1;
157  v8::Local<v8::Value> argv[] = { SWIGV8_INTEGER_NEW(-1) };
158 #if NODE_MODULE_VERSION >= 0x000D
159  v8::Local<v8::Function> f = v8::Local<v8::Function>::New(v8::Isolate::GetCurrent(), This->m_v8isr);
160  f->Call(SWIGV8_CURRENT_CONTEXT()->Global(), argc, argv);
161 #else
162  This->m_v8isr->Call(SWIGV8_CURRENT_CONTEXT()->Global(), argc, argv);
163 #endif
164  delete req;
165  }
166 
167  static void
168  nop(uv_work_t* req)
169  {
170  // Do nothing.
171  }
172 
173  static void
174  uvwork(void* ctx)
175  {
176  uv_work_t* req = new uv_work_t;
177  req->data = ctx;
178  uv_queue_work(uv_default_loop(), req, nop, v8isr);
179  }
180 
181  Result
182  isr(Edge mode, v8::Handle<v8::Function> func)
183  {
184 #if NODE_MODULE_VERSION >= 0x000D
185  m_v8isr.Reset(v8::Isolate::GetCurrent(), func);
186 #else
187  m_v8isr = v8::Persistent<v8::Function>::New(func);
188 #endif
189  return (Result) mraa_gpio_isr(m_gpio, (mraa_gpio_edge_t) mode, &uvwork, this);
190  }
191 #elif defined(SWIGJAVA) || defined(JAVACALLBACK)
192  Result
193  isr(Edge mode, jobject runnable)
194  {
195  return (Result) mraa_gpio_isr(m_gpio, (mraa_gpio_edge_t) mode, mraa_java_isr_callback, runnable);
196  }
197 #endif
198 
207  Result
208  isr(Edge mode, void (*fptr)(void*), void* args)
209  {
210  return (Result) mraa_gpio_isr(m_gpio, (mraa_gpio_edge_t) mode, fptr, args);
211  }
212 
219  Result
221  {
222 #if defined(SWIGJAVASCRIPT)
223 #if NODE_MODULE_VERSION >= 0x000D
224  m_v8isr.Reset();
225 #else
226  m_v8isr.Dispose();
227  m_v8isr.Clear();
228 #endif
229 #endif
230  return (Result) mraa_gpio_isr_exit(m_gpio);
231  }
238  Result
239  mode(Mode mode)
240  {
241  return (Result )mraa_gpio_mode(m_gpio, (mraa_gpio_mode_t) mode);
242  }
249  Result
250  dir(Dir dir)
251  {
252  return (Result )mraa_gpio_dir(m_gpio, (mraa_gpio_dir_t) dir);
253  }
254 
261  Dir
263  {
264  mraa_gpio_dir_t dir;
265  if (mraa_gpio_read_dir(m_gpio, &dir) != MRAA_SUCCESS) {
266  throw std::runtime_error("Failed to read direction");
267  }
268  return (Dir) dir;
269  }
270 
276  int
278  {
279  return mraa_gpio_read(m_gpio);
280  }
287  Result
288  write(int value)
289  {
290  return (Result) mraa_gpio_write(m_gpio, value);
291  }
298  Result
299  useMmap(bool enable)
300  {
301  return (Result) mraa_gpio_use_mmaped(m_gpio, (mraa_boolean_t) enable);
302  }
310  int
311  getPin(bool raw = false)
312  {
313  if (raw) {
314  return mraa_gpio_get_pin_raw(m_gpio);
315  }
316  return mraa_gpio_get_pin(m_gpio);
317  }
318 
319  private:
320  mraa_gpio_context m_gpio;
321 #if defined(SWIGJAVASCRIPT)
322  v8::Persistent<v8::Function> m_v8isr;
323 #endif
324 };
325 }
int read()
Definition: gpio.hpp:277
Result
Definition: types.hpp:199
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:288
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:61
Definition: types.h:206
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:208
mraa_result_t mraa_gpio_close(mraa_gpio_context dev)
~Gpio()
Definition: gpio.hpp:127
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:70
Definition: gpio.hpp:48
mraa_gpio_mode_t
Definition: gpio.h:60
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:138
mraa_gpio_edge_t
Definition: gpio.h:80
mraa_gpio_context mraa_gpio_init_raw(int gpiopin)
Result isrExit()
Definition: gpio.hpp:220
struct _gpio * mraa_gpio_context
Definition: gpio.h:55
mraa_gpio_context mraa_gpio_init(int pin)
Result dir(Dir dir)
Definition: gpio.hpp:250
Definition: gpio.hpp:56
int mraa_gpio_get_pin_raw(mraa_gpio_context dev)
int getPin(bool raw=false)
Definition: gpio.hpp:311
mraa_result_t mraa_gpio_read_dir(mraa_gpio_context dev, mraa_gpio_dir_t *dir)
Gpio(void *gpio_context)
Definition: gpio.hpp:116
Result useMmap(bool enable)
Definition: gpio.hpp:299
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:262
Result mode(Mode mode)
Definition: gpio.hpp:239
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