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 gpio->value_fp = NULL;
70 snprintf(filepath, 64, "/sys/class/gpio/gpio%d/direction", gpio->pin);
73 if((direction = fopen(filepath, "w")) == NULL) {
74 fprintf(stderr, "Failed to open direction for writing!\n");
76 fprintf(direction, dir);
78 gpio->value_fp = NULL;
83 gpio_read(gpio_t *gpio) {
84 if(gpio->value_fp == NULL) {
87 fseek(gpio->value_fp, SEEK_SET, 0);
89 fread(buffer, 2, 1, gpio->value_fp);
90 fseek(gpio->value_fp, SEEK_SET, 0);
95 gpio_write(gpio_t *gpio, int value) {
96 if(gpio->value_fp == NULL) {
99 fseek(gpio->value_fp, SEEK_SET, 0);
100 fprintf(gpio->value_fp, "%d", value);
101 fseek(gpio->value_fp, SEEK_SET, 0);
106 gpio_close(gpio_t *gpio) {
109 if((unexport_f = fopen("/sys/class/gpio/unexport", "w")) == NULL) {
110 fprintf(stderr, "Failed to open unexport for writing!\n");
112 fprintf(unexport_f, "%d", gpio->pin);
118 gpio_get_valfp(gpio_t *gpio) {
120 sprintf(bu, "/sys/class/gpio/gpio%d/value", gpio->pin);
122 if((gpio->value_fp = fopen(bu, "r+b")) == NULL) {