Merge branch 'pwm' into for-pull
[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          gpio->value_fp = NULL;
68     }
69     char filepath[64];
70     snprintf(filepath, 64, "/sys/class/gpio/gpio%d/direction", gpio->pin);
71
72     FILE *direction;
73     if((direction = fopen(filepath, "w")) == NULL) {
74         fprintf(stderr, "Failed to open direction for writing!\n");
75     } else {
76         fprintf(direction, dir);
77         fclose(direction);
78         gpio->value_fp = NULL;
79     }
80 }
81
82 int
83 gpio_read(gpio_t *gpio) {
84     if(gpio->value_fp == NULL) {
85         gpio_get_valfp(gpio);
86     }
87     fseek(gpio->value_fp, SEEK_SET, 0);
88     char buffer[2];
89     fread(buffer, 2, 1, gpio->value_fp);
90     fseek(gpio->value_fp, SEEK_SET, 0);
91     return atoi(buffer);
92 }
93
94 void
95 gpio_write(gpio_t *gpio, int value) {
96     if(gpio->value_fp == NULL) {
97         gpio_get_valfp(gpio);
98     }
99     fseek(gpio->value_fp, SEEK_SET, 0);
100     fprintf(gpio->value_fp, "%d", value);
101     fseek(gpio->value_fp, SEEK_SET, 0);
102
103 }
104
105 void
106 gpio_close(gpio_t *gpio) {
107     FILE *unexport_f;
108
109     if((unexport_f = fopen("/sys/class/gpio/unexport", "w")) == NULL) {
110         fprintf(stderr, "Failed to open unexport for writing!\n");
111     } else {
112         fprintf(unexport_f, "%d", gpio->pin);
113         fclose(unexport_f);
114     }
115 }
116
117 int
118 gpio_get_valfp(gpio_t *gpio) {
119     char bu[64];
120     sprintf(bu, "/sys/class/gpio/gpio%d/value", gpio->pin);
121
122     if((gpio->value_fp = fopen(bu, "r+b")) == NULL) {
123         return 1;
124     } else {
125         return 0;
126     }
127     return 1;
128 }
129
130 #ifdef __cplusplus
131 }
132 #endif