2 * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
4 * Copyright © 2014 Intel Corporation
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:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
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.
38 gpio_get_valfp(gpio_t *gpio);
41 gpio_init(gpio_t *gpio, int pin) {
44 if((export_f = fopen("/sys/class/gpio/export", "w")) == NULL) {
45 fprintf(stderr, "Failed to open export for writing!\n");
47 fprintf(export_f, "%d", pin);
60 gpio_mode(gpio_t *gpio, gpio_mode_t mode) {
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;
71 snprintf(filepath, 64, "/sys/class/gpio/gpio%d/direction", gpio->pin);
74 if((direction = fopen(filepath, "w")) == NULL) {
75 fprintf(stderr, "Failed to open direction for writing!\n");
77 fprintf(direction, dir);
79 gpio->value_fp = NULL;
84 gpio_read(gpio_t *gpio) {
85 if(gpio->value_fp == NULL) {
88 fseek(gpio->value_fp, SEEK_SET, 0);
90 fread(buffer, 2, 1, gpio->value_fp);
91 fseek(gpio->value_fp, SEEK_SET, 0);
96 gpio_write(gpio_t *gpio, int value) {
97 if(gpio->value_fp == NULL) {
100 fseek(gpio->value_fp, SEEK_SET, 0);
101 fprintf(gpio->value_fp, "%d", value);
102 fseek(gpio->value_fp, SEEK_SET, 0);
107 gpio_close(gpio_t *gpio) {
112 gpio_get_valfp(gpio_t *gpio) {
114 sprintf(bu, "/sys/class/gpio/gpio%d/value", gpio->pin);
116 if((gpio->value_fp = fopen(bu, "r+b")) == NULL) {