init: allow init to be called multiple times
[contrib/mraa.git] / api / gpio.h
1 /*
2  * Author: Thomas Ingleby <thomas.c.ingleby@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 interface for libmaa
30  *
31  */
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 #include <stdio.h>
38
39 #include "maa.h"
40 /**
41  * A strucutre representing a gpio pin.
42  */
43 typedef struct {
44     /*@{*/
45     int pin; /**< the pin number, as known to the os. */
46     FILE *value_fp; /**< the file pointer to the value of the gpio */
47     /*@}*/
48 } maa_gpio_context;
49
50 /**
51  * GPIO Output modes
52  */
53 typedef enum {
54     MAA_GPIO_STRONG     = 0, /**< Default. Strong high and low */
55     MAA_GPIO_PULLUP     = 1, /**< Resistive High */
56     MAA_GPIO_PULLDOWN   = 2, /**< Resistive Low */
57     MAA_GPIO_HIZ        = 3  /**< High Z State */
58 } gpio_mode_t;
59
60 /**
61  * GPIO Direction options.
62  */
63 typedef enum {
64     MAA_GPIO_OUT    = 0, /**< Output. A Mode can also be set */
65     MAA_GPIO_IN     = 1  /**< Input. */
66 } gpio_dir_t;
67
68 /** Initialise gpio_context, based on board number
69  *
70  *  @param pin pin number read from the board, i.e IO3 is 3.
71  *
72  *  @returns maa_gpio_context based on the IO pin
73  */
74 maa_gpio_context* maa_gpio_init(int pin);
75
76 /** Initialise gpio context without any mapping to a pin.
77  * - For more expert users
78  *
79  * @param gpiopin gpio pin as listed in SYSFS
80  *
81  * @return gpio context
82  */
83 maa_gpio_context* maa_gpio_init_raw(int gpiopin);
84
85 /** Set GPIO Output Mode,
86  *
87  * @param dev The GPIO context
88  * @param mode The GPIO Output Mode.
89  *
90  * @return maa result type.
91  */
92 maa_result_t maa_gpio_mode(maa_gpio_context *dev, gpio_mode_t mode);
93
94 /** Set GPIO direction
95  *
96  * @param dev The GPIO context.
97  * @param dir The direction of the GPIO.
98  *
99  * @return maa result type.
100  */
101 maa_result_t maa_gpio_dir(maa_gpio_context *dev, gpio_dir_t dir);
102
103 /** Close the GPIO context
104  * - Will free the memory for the context and unexport the GPIO
105  *
106  * @param dev the GPIO context
107  *
108  * @return maa result type.
109  */
110 maa_result_t maa_gpio_close(maa_gpio_context *dev);
111
112 /** Unexport the GPIO context (maa_gpio_close() will call this function)
113  *
114  * @param dev The GPIO context.
115  *
116  * @return maa result type.
117  */
118 maa_result_t maa_gpio_unexport(maa_gpio_context *dev);
119
120 /** Read the GPIO value.
121  *
122  * @param dev The GPIO context.
123  *
124  * @return the integer value of the GPIO
125  */
126 int maa_gpio_read(maa_gpio_context *dev);
127
128 /** Write to the GPIO Value.
129  *
130  * @param dev The GPIO context.
131  * @param value Integer value to write.
132  *
133  * @return maa result type
134  */
135 maa_result_t maa_gpio_write(maa_gpio_context *dev, int value);
136
137 #ifdef __cplusplus
138 }
139 #endif