mraa  0.6.0
Low Level Skeleton Library for Communication on GNU/Linux platforms
 All Data Structures Namespaces Files Functions Typedefs Enumerations Enumerator 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 <stdexcept>
29 
30 namespace mraa {
31 
32 // These enums must match the enums in gpio.h
33 
37 typedef enum {
41  MODE_HIZ = 3
42 } Mode;
43 
47 typedef enum {
48  DIR_OUT = 0,
49  DIR_IN = 1,
52 } Dir;
53 
57 typedef enum {
58  EDGE_NONE = 0,
59  EDGE_BOTH = 1,
62 } Edge;
63 
71 class Gpio {
72  public:
85  Gpio(int pin, bool owner=true, bool raw=false) {
86  if (raw) {
87  m_gpio = mraa_gpio_init_raw(pin);
88  }
89  else {
90  m_gpio = mraa_gpio_init(pin);
91  }
92 
93  if (m_gpio == NULL) {
94  throw std::invalid_argument("Invalid GPIO pin specified");
95  }
96 
97  if (!owner) {
98  mraa_gpio_owner(m_gpio, 0);
99  }
100  }
105  ~Gpio() {
106  mraa_gpio_close(m_gpio);
107  }
115  return mraa_gpio_edge_mode(m_gpio, (gpio_edge_t) mode);
116  }
117 #if defined(SWIGPYTHON)
118  mraa_result_t isr(Edge mode, PyObject *pyfunc, PyObject* args) {
119  return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, (void (*) (void *)) pyfunc, (void *) args);
120  }
121 #else
122 
131  mraa_result_t isr(Edge mode, void (*fptr)(void *), void * args) {
132  return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, fptr, args);
133  }
134 #endif
135 
142  return mraa_gpio_isr_exit(m_gpio);
143  }
151  return mraa_gpio_mode(m_gpio, (gpio_mode_t) mode);
152  }
160  return mraa_gpio_dir(m_gpio, (gpio_dir_t) dir);
161  }
167  int read() {
168  return mraa_gpio_read(m_gpio);
169  }
176  mraa_result_t write(int value) {
177  return mraa_gpio_write(m_gpio, value);
178  }
185  mraa_result_t useMmap(bool enable) {
186  return mraa_gpio_use_mmaped(m_gpio, (mraa_boolean_t) enable);
187  }
195  int getPin(bool raw = false) {
196  if (raw) {
197  return mraa_gpio_get_pin_raw(m_gpio);
198  }
199  return mraa_gpio_get_pin(m_gpio);
200  }
201  private:
202  mraa_gpio_context m_gpio;
203 };
204 
205 }
int read()
Definition: gpio.hpp:167
mraa_result_t dir(Dir dir)
Definition: gpio.hpp:159
mraa_result_t mraa_gpio_owner(mraa_gpio_context dev, mraa_boolean_t owner)
API to General Purpose IO.
Definition: gpio.hpp:71
mraa_result_t mraa_gpio_edge_mode(mraa_gpio_context dev, gpio_edge_t mode)
Definition: gpio.hpp:61
mraa_result_t write(int value)
Definition: gpio.hpp:176
Definition: gpio.hpp:41
General Purpose IO.
unsigned int mraa_boolean_t
Definition: common.h:44
mraa_result_t isrExit()
Definition: gpio.hpp:141
Gpio(int pin, bool owner=true, bool raw=false)
Definition: gpio.hpp:85
mraa_result_t mraa_gpio_isr_exit(mraa_gpio_context dev)
gpio_mode_t
Definition: gpio.h:61
mraa_result_t mraa_gpio_dir(mraa_gpio_context dev, gpio_dir_t dir)
Definition: gpio.hpp:60
Definition: gpio.hpp:39
mraa_result_t mraa_gpio_close(mraa_gpio_context dev)
~Gpio()
Definition: gpio.hpp:105
mraa_result_t isr(Edge mode, void(*fptr)(void *), void *args)
Definition: gpio.hpp:131
Dir
Definition: gpio.hpp:47
Definition: gpio.hpp:40
mraa_result_t mraa_gpio_mode(mraa_gpio_context dev, gpio_mode_t mode)
gpio_dir_t
Definition: gpio.h:71
Definition: gpio.hpp:51
Edge
Definition: gpio.hpp:57
mraa_result_t mraa_gpio_isr(mraa_gpio_context dev, gpio_edge_t edge, void(*fptr)(void *), void *args)
int mraa_gpio_get_pin(mraa_gpio_context dev)
Definition: gpio.hpp:58
mraa_gpio_context mraa_gpio_init_raw(int gpiopin)
struct _gpio * mraa_gpio_context
Definition: gpio.h:56
mraa_gpio_context mraa_gpio_init(int pin)
mraa_result_t edge(Edge mode)
Definition: gpio.hpp:114
Definition: gpio.hpp:48
int mraa_gpio_get_pin_raw(mraa_gpio_context dev)
mraa_result_t
Definition: types.h:145
mraa_result_t useMmap(bool enable)
Definition: gpio.hpp:185
int getPin(bool raw=false)
Definition: gpio.hpp:195
gpio_edge_t
Definition: gpio.h:81
mraa_result_t mode(Mode mode)
Definition: gpio.hpp:150
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)
int mraa_gpio_read(mraa_gpio_context dev)
Mode
Definition: gpio.hpp:37
Definition: gpio.hpp:59
Definition: gpio.hpp:38
Definition: gpio.hpp:49
Definition: gpio.hpp:50