maa: change complex C++ write calls to CamelCase for spi & i2c
[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
29 namespace mraa {
30
31 // These enums must match the enums in gpio.h
32
33 /**
34  * Gpio Output modes
35  */
36 typedef enum {
37     MODE_STRONG   = 0, /**< No interrupt on Gpio */
38     MODE_PULLUP   = 1, /**< Interupt on rising & falling */
39     MODE_PULLDOWN = 2, /**< Interupt on rising only */
40     MODE_HIZ      = 3  /**< Interupt on falling only */
41 } Mode;
42
43 /**
44  * Gpio Direction options
45  */
46 typedef enum {
47     DIR_OUT = 0, /**< Output. A Mode can also be set */
48     DIR_IN  = 1  /**< Input */
49 } Dir;
50
51 /**
52  * Gpio Edge types for interupts
53  */
54 typedef enum {
55     EDGE_NONE    = 0, /**< No interrupt on Gpio */
56     EDGE_BOTH    = 1, /**< Interupt on rising & falling */
57     EDGE_RISING  = 2, /**< Interupt on rising only */
58     EDGE_FALLING = 3  /**< Interupt on falling only */
59 } Edge;
60
61 /**
62  * @brief C++ API to General Purpose IO
63  *
64  * This file defines the gpio C++ interface for libmraa
65  *
66  * @snippet Blink-IO.cpp Interesting
67  */
68 class Gpio {
69     public:
70         /**
71          * Instanciates a Gpio object
72          *
73          * @param pin pin number to use
74          * @param owner (optional) Set pin owner, default behaviour is to 'own'
75          * the pin if we exported it. This means we will close it on destruct.
76          * Otherwise it will get left open. This is only valid in sysfs use
77          * cases
78          * @param raw (optional) Raw pins will use gpiolibs pin numbering from
79          * the kernel module. Note that you will not get any muxers set up for
80          * you so this may not always work as expected.
81          */
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         }
90         /**
91          * Gpio object destructor, this will only unexport the gpio if we where
92          * the owner
93          */
94         ~Gpio() {
95             mraa_gpio_close(m_gpio);
96         }
97         /**
98          * Set the edge mode for ISR
99          *
100          * @param mode The edge mode to set
101          * @return Result of operation
102          */
103         mraa_result_t edge(Edge mode) {
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         /**
112          * Sets a callback to be called when pin value changes
113          *
114          * @param mode The edge mode to set
115          * @param fptr Function pointer to function to be called when interupt is
116          * triggered
117          * @param args Arguments passed to the interrupt handler (fptr)
118          * @return Result of operation
119          */
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         /**
125          * Exits callback - this call will not kill the isr thread imediatlu
126          * but only when it is out of it's critical section
127          *
128          * @return Result of operation
129          */
130         mraa_result_t isrExit() {
131             return mraa_gpio_isr_exit(m_gpio);
132         }
133         /**
134          * Change Gpio mode
135          *
136          * @param mode The mode to change the gpio into
137          * @return Result of operation
138          */
139         mraa_result_t mode(Mode mode) {
140             return mraa_gpio_mode(m_gpio, (gpio_mode_t) mode);
141         }
142         /**
143          * Change Gpio direction
144          *
145          * @param dir The direction to change the gpio into
146          * @return Result of operation
147          */
148         mraa_result_t dir(Dir dir) {
149             return mraa_gpio_dir(m_gpio, (gpio_dir_t) dir);
150         }
151         /**
152          * Read value from Gpio
153          *
154          * @return Gpio value
155          */
156         int read() {
157             return mraa_gpio_read(m_gpio);
158         }
159         /**
160          * Write value to Gpio
161          *
162          * @param value Value to write to Gpio
163          * @return Result of operation
164          */
165         mraa_result_t write(int value) {
166             return mraa_gpio_write(m_gpio, value);
167         }
168     private:
169         mraa_gpio_context m_gpio;
170 };
171
172 }