gpio: Add cpp example to use IO.
[contrib/mraa.git] / src / gpio / gpio.c
1 /*
2  * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3  *
4  * Copyright © 2014 Intel Corporation
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files (the
8  * "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 #include <stdlib.h>
27 #include <fcntl.h>
28 #include <stdio.h>
29 #include <unistd.h>
30
31 #include "gpio.h"
32
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36
37 int
38 gpio_get_valfp(gpio_t *gpio);
39
40 void
41 gpio_init(gpio_t *gpio, int pin) {
42     FILE *export_f;
43
44     if((export_f = fopen("/sys/class/gpio/export", "w")) == NULL) {
45         fprintf(stderr, "Failed to open export for writing!\n");
46     } else {
47         fprintf(export_f, "%d", pin);
48         fclose(export_f);
49     }
50     gpio->pin = pin;
51 }
52
53 int
54 gpio_set(int pin) {
55     //Stuff
56     return 0;
57 }
58
59 void
60 gpio_mode(gpio_t *gpio, gpio_mode_t mode) {
61     //gpio->pin
62 }
63
64 void
65 gpio_dir(gpio_t *gpio, gpio_dir_t dir) {
66 //if(gpio->value_fp != NULL) {
67 //      fclose(gpio->value_fp);
68 //       gpio->value_fp = NULL;
69 //   }
70     char filepath[64];
71     snprintf(filepath, 64, "/sys/class/gpio/gpio%d/direction", gpio->pin);
72
73     FILE *direction;
74     if((direction = fopen(filepath, "w")) == NULL) {
75         fprintf(stderr, "Failed to open direction for writing!\n");
76     } else {
77         fprintf(direction, dir);
78         fclose(direction);
79         gpio->value_fp = NULL;
80     }
81 }
82
83 int
84 gpio_read(gpio_t *gpio) {
85     if(gpio->value_fp == NULL) {
86         gpio_get_valfp(gpio);
87     }
88     fseek(gpio->value_fp, SEEK_SET, 0);
89     char buffer[2];
90     fread(buffer, 2, 1, gpio->value_fp);
91     fseek(gpio->value_fp, SEEK_SET, 0);
92     return atoi(buffer);
93 }
94
95 void
96 gpio_write(gpio_t *gpio, int value) {
97     if(gpio->value_fp == NULL) {
98         gpio_get_valfp(gpio);
99     }
100     fseek(gpio->value_fp, SEEK_SET, 0);
101     fprintf(gpio->value_fp, "%d", value);
102     fseek(gpio->value_fp, SEEK_SET, 0);
103
104 }
105
106 void
107 gpio_close(gpio_t *gpio) {
108
109 }
110
111 int
112 gpio_get_valfp(gpio_t *gpio) {
113     char bu[64];
114     sprintf(bu, "/sys/class/gpio/gpio%d/value", gpio->pin);
115
116     if((gpio->value_fp = fopen(bu, "r+b")) == NULL) {
117         return 1;
118     } else {
119         return 0;
120     }
121     return 1;
122 }
123
124 #ifdef __cplusplus
125 }
126 #endif