2 * Author: Thomas Ingleby <thomas.c.ingleby@intel.com>
3 * Copyright (c) 2014 Intel Corporation.
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:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
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.
33 maa_gpio_get_valfp(maa_gpio_context *dev)
36 sprintf(bu, "/sys/class/gpio/gpio%d/value", dev->pin);
38 if ((dev->value_fp = fopen(bu, "r+b")) == NULL) {
45 maa_gpio_init(int pin)
48 maa_gpio_context* dev = (maa_gpio_context*) malloc(sizeof(maa_gpio_context));
50 if ((export_f = fopen("/sys/class/gpio/export", "w")) == NULL) {
51 fprintf(stderr, "Failed to open export for writing!\n");
53 fprintf(export_f, "%d", pin);
61 maa_gpio_mode(maa_gpio_context *dev, gpio_mode_t mode)
67 maa_gpio_dir(maa_gpio_context *dev, gpio_dir_t dir)
69 if (dev->value_fp != NULL) {
73 snprintf(filepath, 64, "/sys/class/gpio/gpio%d/direction", dev->pin);
76 if ((direction = fopen(filepath, "w")) == NULL) {
77 fprintf(stderr, "Failed to open direction for writing!\n");
79 fprintf(direction, dir);
86 maa_gpio_read(maa_gpio_context *dev)
88 if (dev->value_fp == NULL) {
89 maa_gpio_get_valfp(dev);
91 fseek(dev->value_fp, SEEK_SET, 0);
93 fread(buffer, 2, 1, dev->value_fp);
94 fseek(dev->value_fp, SEEK_SET, 0);
99 maa_gpio_write(maa_gpio_context *dev, int value)
101 if (dev->value_fp == NULL) {
102 maa_gpio_get_valfp(dev);
104 fseek(dev->value_fp, SEEK_SET, 0);
105 fprintf(dev->value_fp, "%d", value);
106 fseek(dev->value_fp, SEEK_SET, 0);
111 maa_gpio_close(maa_gpio_context *dev)
115 if ((unexport_f = fopen("/sys/class/gpio/unexport", "w")) == NULL) {
116 fprintf(stderr, "Failed to open unexport for writing!\n");
118 fprintf(unexport_f, "%d", dev->pin);