mraa  0.4.5
Low Level Skeleton Library for Communication on GNU/Linux platforms
 All Data Structures Namespaces Files Functions Variables 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 
29 namespace mraa {
30 
31 // These enums must match the enums in gpio.h
32 
36 typedef enum {
40  MODE_HIZ = 3
41 } Mode;
42 
46 typedef enum {
47  DIR_OUT = 0,
48  DIR_IN = 1
49 } Dir;
50 
54 typedef enum {
55  EDGE_NONE = 0,
56  EDGE_BOTH = 1,
59 } Edge;
60 
68 class Gpio {
69  public:
82  Gpio(int pin, bool owner=true, bool raw=false) {
83  if (raw)
84  m_gpio = mraa_gpio_init_raw(pin);
85  else
86  m_gpio = mraa_gpio_init(pin);
87  if (!owner)
88  mraa_gpio_owner(m_gpio, 0);
89  }
94  ~Gpio() {
95  mraa_gpio_close(m_gpio);
96  }
104  return mraa_gpio_edge_mode(m_gpio, (gpio_edge_t) mode);
105  }
106 #if defined(SWIGPYTHON)
107  mraa_result_t isr(Edge mode, PyObject *pyfunc, PyObject* args) {
108  return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, (void (*) (void *)) pyfunc, (void *) args);
109  }
110 #else
111 
120  mraa_result_t isr(Edge mode, void (*fptr)(void *), void * args) {
121  return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, fptr, args);
122  }
123 #endif
124 
131  return mraa_gpio_isr_exit(m_gpio);
132  }
140  return mraa_gpio_mode(m_gpio, (gpio_mode_t) mode);
141  }
149  return mraa_gpio_dir(m_gpio, (gpio_dir_t) dir);
150  }
156  int read() {
157  return mraa_gpio_read(m_gpio);
158  }
165  mraa_result_t write(int value) {
166  return mraa_gpio_write(m_gpio, value);
167  }
174  mraa_result_t useMmap(bool enable) {
175  return mraa_gpio_use_mmaped(m_gpio, (mraa_boolean_t) enable);
176  }
177  private:
178  mraa_gpio_context m_gpio;
179 };
180 
181 }
int read()
Definition: gpio.hpp:156
mraa_result_t dir(Dir dir)
Definition: gpio.hpp:148
mraa_result_t mraa_gpio_owner(mraa_gpio_context dev, mraa_boolean_t owner)
API to General Purpose IO.
Definition: gpio.hpp:68
mraa_result_t mraa_gpio_edge_mode(mraa_gpio_context dev, gpio_edge_t mode)
Definition: gpio.hpp:58
mraa_result_t write(int value)
Definition: gpio.hpp:165
Definition: gpio.hpp:40
General Purpose IO.
unsigned int mraa_boolean_t
Definition: common.h:42
mraa_result_t isrExit()
Definition: gpio.hpp:130
Gpio(int pin, bool owner=true, bool raw=false)
Definition: gpio.hpp:82
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:57
Definition: gpio.hpp:38
mraa_result_t mraa_gpio_close(mraa_gpio_context dev)
~Gpio()
Definition: gpio.hpp:94
mraa_result_t isr(Edge mode, void(*fptr)(void *), void *args)
Definition: gpio.hpp:120
Dir
Definition: gpio.hpp:46
Definition: gpio.hpp:39
mraa_result_t mraa_gpio_mode(mraa_gpio_context dev, gpio_mode_t mode)
gpio_dir_t
Definition: gpio.h:71
Edge
Definition: gpio.hpp:54
mraa_result_t mraa_gpio_isr(mraa_gpio_context dev, gpio_edge_t edge, void(*fptr)(void *), void *args)
Definition: gpio.hpp:55
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:103
Definition: gpio.hpp:47
mraa_result_t
Definition: types.h:49
mraa_result_t useMmap(bool enable)
Definition: gpio.hpp:174
gpio_edge_t
Definition: gpio.h:79
mraa_result_t mode(Mode mode)
Definition: gpio.hpp:139
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:36
Definition: gpio.hpp:56
Definition: gpio.hpp:37
Definition: gpio.hpp:48