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 
75 typedef enum {
78 } InputMode;
79 
83 typedef enum {
86 } OutputMode;
87 
95 class Gpio
96 {
97  public:
110  Gpio(int pin, bool owner = true, bool raw = false)
111  {
112  if (raw) {
113  m_gpio = mraa_gpio_init_raw(pin);
114  } else {
115  m_gpio = mraa_gpio_init(pin);
116  }
117 
118  if (m_gpio == NULL) {
119  throw std::invalid_argument("Invalid GPIO pin specified");
120  }
121 
122  if (!owner) {
123  mraa_gpio_owner(m_gpio, 0);
124  }
125  }
132  Gpio(void* gpio_context)
133  {
134  m_gpio = (mraa_gpio_context) gpio_context;
135  if (m_gpio == NULL) {
136  throw std::invalid_argument("Invalid GPIO context");
137  }
138  }
144  {
145  mraa_gpio_close(m_gpio);
146  }
153  Result
155  {
156  return (Result) mraa_gpio_edge_mode(m_gpio, (mraa_gpio_edge_t) mode);
157  }
158 #if defined(SWIGPYTHON)
159  Result
160  isr(Edge mode, PyObject* pyfunc, PyObject* args)
161  {
162  return (Result) mraa_gpio_isr(m_gpio, (mraa_gpio_edge_t) mode, (void (*) (void*)) pyfunc, (void*) args);
163  }
164 #elif defined(SWIGJAVASCRIPT)
165  static void
166  v8isr(uv_work_t* req, int status)
167  {
168 #if NODE_MODULE_VERSION >= 0x000D
169  v8::HandleScope scope(v8::Isolate::GetCurrent());
170 #endif
171  mraa::Gpio* This = (mraa::Gpio*) req->data;
172  int argc = 1;
173  v8::Local<v8::Value> argv[] = { SWIGV8_INTEGER_NEW(-1) };
174 #if NODE_MODULE_VERSION >= 0x000D
175  v8::Local<v8::Function> f = v8::Local<v8::Function>::New(v8::Isolate::GetCurrent(), This->m_v8isr);
176  f->Call(SWIGV8_CURRENT_CONTEXT()->Global(), argc, argv);
177 #else
178  This->m_v8isr->Call(SWIGV8_CURRENT_CONTEXT()->Global(), argc, argv);
179 #endif
180  delete req;
181  }
182 
183  static void
184  nop(uv_work_t* req)
185  {
186  // Do nothing.
187  }
188 
189  static void
190  uvwork(void* ctx)
191  {
192  uv_work_t* req = new uv_work_t;
193  req->data = ctx;
194  uv_queue_work(uv_default_loop(), req, nop, v8isr);
195  }
196 
197  Result
198  isr(Edge mode, v8::Handle<v8::Function> func)
199  {
200 #if NODE_MODULE_VERSION >= 0x000D
201  m_v8isr.Reset(v8::Isolate::GetCurrent(), func);
202 #else
203  m_v8isr = v8::Persistent<v8::Function>::New(func);
204 #endif
205  return (Result) mraa_gpio_isr(m_gpio, (mraa_gpio_edge_t) mode, &uvwork, this);
206  }
207 #elif defined(SWIGJAVA) || defined(JAVACALLBACK)
208  Result
209  isr(Edge mode, jobject runnable)
210  {
211  return (Result) mraa_gpio_isr(m_gpio, (mraa_gpio_edge_t) mode, mraa_java_isr_callback, runnable);
212  }
213 #endif
214 
223  Result
224  isr(Edge mode, void (*fptr)(void*), void* args)
225  {
226  return (Result) mraa_gpio_isr(m_gpio, (mraa_gpio_edge_t) mode, fptr, args);
227  }
228 
235  Result
237  {
238 #if defined(SWIGJAVASCRIPT)
239 #if NODE_MODULE_VERSION >= 0x000D
240  m_v8isr.Reset();
241 #else
242  m_v8isr.Dispose();
243  m_v8isr.Clear();
244 #endif
245 #endif
246  return (Result) mraa_gpio_isr_exit(m_gpio);
247  }
254  Result
255  mode(Mode mode)
256  {
257  return (Result )mraa_gpio_mode(m_gpio, (mraa_gpio_mode_t) mode);
258  }
265  Result
266  dir(Dir dir)
267  {
268  return (Result )mraa_gpio_dir(m_gpio, (mraa_gpio_dir_t) dir);
269  }
270 
277  Dir
279  {
280  mraa_gpio_dir_t dir;
281  if (mraa_gpio_read_dir(m_gpio, &dir) != MRAA_SUCCESS) {
282  throw std::runtime_error("Failed to read direction");
283  }
284  return (Dir) dir;
285  }
286 
292  int
294  {
295  return mraa_gpio_read(m_gpio);
296  }
303  Result
304  write(int value)
305  {
306  return (Result) mraa_gpio_write(m_gpio, value);
307  }
314  Result
315  useMmap(bool enable)
316  {
317  return (Result) mraa_gpio_use_mmaped(m_gpio, (mraa_boolean_t) enable);
318  }
326  int
327  getPin(bool raw = false)
328  {
329  if (raw) {
330  return mraa_gpio_get_pin_raw(m_gpio);
331  }
332  return mraa_gpio_get_pin(m_gpio);
333  }
334 
341  Result
343  {
344  return (Result )mraa_gpio_input_mode(m_gpio, (mraa_gpio_input_mode_t) mode);
345  }
346 
353  Result
355  {
357  }
358 
359  private:
360  mraa_gpio_context m_gpio;
361 #if defined(SWIGJAVASCRIPT)
362  v8::Persistent<v8::Function> m_v8isr;
363 #endif
364 };
365 }
int read()
Definition: gpio.hpp:293
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:95
Result write(int value)
Definition: gpio.hpp:304
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:110
mraa_result_t mraa_gpio_isr_exit(mraa_gpio_context dev)
InputMode
Definition: gpio.hpp:75
Definition: gpio.hpp:84
Definition: gpio.hpp:68
Definition: gpio.hpp:77
Definition: gpio.hpp:47
Result isr(Edge mode, void(*fptr)(void *), void *args)
Definition: gpio.hpp:224
mraa_result_t mraa_gpio_close(mraa_gpio_context dev)
~Gpio()
Definition: gpio.hpp:143
OutputMode
Definition: gpio.hpp:83
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:154
mraa_gpio_edge_t
Definition: gpio.h:80
mraa_gpio_context mraa_gpio_init_raw(int gpiopin)
Definition: gpio.hpp:76
Result isrExit()
Definition: gpio.hpp:236
struct _gpio * mraa_gpio_context
Definition: gpio.h:55
mraa_gpio_context mraa_gpio_init(int pin)
Result dir(Dir dir)
Definition: gpio.hpp:266
Definition: gpio.hpp:56
int mraa_gpio_get_pin_raw(mraa_gpio_context dev)
Result outputMode(OutputMode mode)
Definition: gpio.hpp:354
int getPin(bool raw=false)
Definition: gpio.hpp:327
Definition: gpio.hpp:85
mraa_result_t mraa_gpio_read_dir(mraa_gpio_context dev, mraa_gpio_dir_t *dir)
mraa_result_t mraa_gpio_input_mode(mraa_gpio_context dev, mraa_gpio_input_mode_t)
Gpio(void *gpio_context)
Definition: gpio.hpp:132
Result useMmap(bool enable)
Definition: gpio.hpp:315
mraa_gpio_input_mode_t
Definition: gpio.h:90
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:278
Result mode(Mode mode)
Definition: gpio.hpp:255
mraa_gpio_out_driver_mode_t
Definition: gpio.h:99
Result inputMode(InputMode mode)
Definition: gpio.hpp:342
int mraa_gpio_read(mraa_gpio_context dev)
Mode
Definition: gpio.hpp:45
Definition: gpio.hpp:67
Definition: gpio.hpp:46
mraa_result_t mraa_gpio_out_driver_mode(mraa_gpio_context dev, mraa_gpio_out_driver_mode_t mode)
Definition: gpio.hpp:57
Definition: gpio.hpp:58