pwm: made export functions static
[contrib/mraa.git] / api / 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 /** @file
28  *
29  * This file defines the gpio C++ interface for libmaa
30  *
31  */
32
33 #include "gpio.h"
34
35 namespace maa {
36
37 // These enums must match the enums in gpio.h
38 typedef enum {
39     MODE_STRONG = 0,
40     MODE_PULLUP = 1,
41     MODE_PULLDOWN = 2,
42     MODE_HIZ = 3
43 } Mode;
44
45 typedef enum {
46     DIR_OUT = 0,
47     DIR_IN = 1
48 } Dir;
49
50 typedef enum {
51     EDGE_NONE = 0,
52     EDGE_BOTH = 1,
53     EDGE_RISING = 2,
54     EDGE_FALLING = 3
55 } Edge;
56
57 class Gpio {
58     public:
59         Gpio(int pin, bool owner=true, bool raw=false) {
60             if (raw)
61                 m_gpio = maa_gpio_init_raw(pin);
62             else
63                 m_gpio = maa_gpio_init(pin);
64             if (!owner)
65                 maa_gpio_owner(m_gpio, 0);
66         }
67         ~Gpio() {
68             maa_result_t x = maa_gpio_close(m_gpio);
69         }
70         maa_result_t edge(Edge mode) {
71             return maa_gpio_edge_mode(m_gpio, (gpio_edge_t) mode);
72         }
73 #if defined(SWIGPYTHON)
74         maa_result_t isr(Edge mode, PyObject *pyfunc) {
75             return maa_gpio_isr(m_gpio, (gpio_edge_t) mode, (void (*) ()) pyfunc);
76         }
77 #else
78         maa_result_t isr(Edge mode, void (*fptr)(void)) {
79             return maa_gpio_isr(m_gpio, (gpio_edge_t) mode, fptr);
80         }
81 #endif
82         maa_result_t isr_exit() {
83             return maa_gpio_isr_exit(m_gpio);
84         }
85         maa_result_t mode(Mode mode) {
86             return maa_gpio_mode(m_gpio, (gpio_mode_t) mode);
87         }
88         maa_result_t dir(Dir dir) {
89             return maa_gpio_dir(m_gpio, (gpio_dir_t) dir);
90         }
91         int read() {
92             return maa_gpio_read(m_gpio);
93         }
94         int write(int value) {
95             return maa_gpio_write(m_gpio, value);
96         }
97     private:
98         maa_gpio_context m_gpio;
99 };
100
101 }