gpio: Add a function to get a pin number from mraa_gpio_context
[contrib/mraa.git] / api / mraa / 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
34 /**
35  * Gpio Output modes
36  */
37 typedef enum {
38     MODE_STRONG   = 0, /**< Default. Strong High and Low */
39     MODE_PULLUP   = 1, /**< Interupt on rising & falling */
40     MODE_PULLDOWN = 2, /**< Interupt on rising only */
41     MODE_HIZ      = 3  /**< Interupt on falling only */
42 } Mode;
43
44 /**
45  * Gpio Direction options
46  */
47 typedef enum {
48     DIR_OUT = 0, /**< Output. A Mode can also be set */
49     DIR_IN  = 1  /**< Input */
50 } Dir;
51
52 /**
53  * Gpio Edge types for interupts
54  */
55 typedef enum {
56     EDGE_NONE    = 0, /**< No interrupt on Gpio */
57     EDGE_BOTH    = 1, /**< Interupt on rising & falling */
58     EDGE_RISING  = 2, /**< Interupt on rising only */
59     EDGE_FALLING = 3  /**< Interupt on falling only */
60 } Edge;
61
62 /**
63  * @brief API to General Purpose IO
64  *
65  * This file defines the gpio interface for libmraa
66  *
67  * @snippet Blink-IO.cpp Interesting
68  */
69 class Gpio {
70     public:
71         /**
72          * Instanciates a Gpio object
73          *
74          * @param pin pin number to use
75          * @param owner (optional) Set pin owner, default behaviour is to 'own'
76          * the pin if we exported it. This means we will close it on destruct.
77          * Otherwise it will get left open. This is only valid in sysfs use
78          * cases
79          * @param raw (optional) Raw pins will use gpiolibs pin numbering from
80          * the kernel module. Note that you will not get any muxers set up for
81          * you so this may not always work as expected.
82          */
83         Gpio(int pin, bool owner=true, bool raw=false) {
84             if (raw) {
85                 m_gpio = mraa_gpio_init_raw(pin);
86             }
87             else {
88                 m_gpio = mraa_gpio_init(pin);
89             }
90
91             if (m_gpio == NULL) {
92                 throw std::invalid_argument("Invalid GPIO pin specified");
93             }
94
95             if (!owner) {
96                 mraa_gpio_owner(m_gpio, 0);
97             }
98         }
99         /**
100          * Gpio object destructor, this will only unexport the gpio if we where
101          * the owner
102          */
103         ~Gpio() {
104             mraa_gpio_close(m_gpio);
105         }
106         /**
107          * Set the edge mode for ISR
108          *
109          * @param mode The edge mode to set
110          * @return Result of operation
111          */
112         mraa_result_t edge(Edge mode) {
113             return mraa_gpio_edge_mode(m_gpio, (gpio_edge_t) mode);
114         }
115 #if defined(SWIGPYTHON)
116         mraa_result_t isr(Edge mode, PyObject *pyfunc, PyObject* args) {
117             return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, (void (*) (void *)) pyfunc, (void *) args);
118         }
119 #else
120         /**
121          * Sets a callback to be called when pin value changes
122          *
123          * @param mode The edge mode to set
124          * @param fptr Function pointer to function to be called when interupt is
125          * triggered
126          * @param args Arguments passed to the interrupt handler (fptr)
127          * @return Result of operation
128          */
129         mraa_result_t isr(Edge mode, void (*fptr)(void *), void * args) {
130             return mraa_gpio_isr(m_gpio, (gpio_edge_t) mode, fptr, args);
131         }
132 #endif
133         /**
134          * Exits callback - this call will not kill the isr thread imediatlu
135          * but only when it is out of it's critical section
136          *
137          * @return Result of operation
138          */
139         mraa_result_t isrExit() {
140             return mraa_gpio_isr_exit(m_gpio);
141         }
142         /**
143          * Change Gpio mode
144          *
145          * @param mode The mode to change the gpio into
146          * @return Result of operation
147          */
148         mraa_result_t mode(Mode mode) {
149             return mraa_gpio_mode(m_gpio, (gpio_mode_t) mode);
150         }
151         /**
152          * Change Gpio direction
153          *
154          * @param dir The direction to change the gpio into
155          * @return Result of operation
156          */
157         mraa_result_t dir(Dir dir) {
158             return mraa_gpio_dir(m_gpio, (gpio_dir_t) dir);
159         }
160         /**
161          * Read value from Gpio
162          *
163          * @return Gpio value
164          */
165         int read() {
166             return mraa_gpio_read(m_gpio);
167         }
168         /**
169          * Write value to Gpio
170          *
171          * @param value Value to write to Gpio
172          * @return Result of operation
173          */
174         mraa_result_t write(int value) {
175             return mraa_gpio_write(m_gpio, value);
176         }
177         /**
178          * Enable use of mmap i/o if available.
179          *
180          * @param enable true to use mmap
181          * @return Result of operation
182          */
183         mraa_result_t useMmap(bool enable) {
184             return mraa_gpio_use_mmaped(m_gpio, (mraa_boolean_t) enable);
185         }
186         /**
187          * Get pin number of Gpio
188          *
189          * @return Pin number
190          */
191         int getPin() {
192             return mraa_gpio_get_pin(m_gpio);
193         }
194     private:
195         mraa_gpio_context m_gpio;
196 };
197
198 }