typedef struct {
/*@{*/
int pin; /**< the pin number, as known to the os. */
- FILE *value_fp; /**< the file pointer to the value of the gpio */
+ int value_fp; /**< the file pointer to the value of the gpio */
void (* isr)(); /**< the interupt service request */
pthread_t thread_id; /**< the isr handler thread id */
int isr_value_fp; /**< the isr file pointer on the value */
add_executable (i2c_HMC5883L i2c_HMC5883L.c)
add_executable (hellomaa hellomaa.c)
add_executable (cycle-pwm3 cycle-pwm3.c)
-add_executable (blink-io8 blink-io8.c)
+add_executable (blink-io blink-io.c)
add_executable (analogin_a0 analogin_a0.c)
add_executable (isr_pin6 isr_pin6.c)
+add_executable (gpio_read6 gpio_read6.c)
include_directories(${PROJECT_SOURCE_DIR}/api ${PROJECT_SOURCE_DIR}/include)
target_link_libraries (hellomaa maa ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (i2c_HMC5883L maa m ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (cycle-pwm3 maa ${CMAKE_THREAD_LIBS_INIT})
-target_link_libraries (blink-io8 maa ${CMAKE_THREAD_LIBS_INIT})
+target_link_libraries (blink-io maa ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (analogin_a0 maa ${CMAKE_THREAD_LIBS_INIT})
target_link_libraries (isr_pin6 maa ${CMAKE_THREAD_LIBS_INIT})
+target_link_libraries (gpio_read6 maa ${CMAKE_THREAD_LIBS_INIT})
--- /dev/null
+/*
+ * Author: Brendan Le Foll <brendan.le.foll@intel.com>
+ * Copyright (c) 2014 Intel Corporation.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files (the
+ * "Software"), to deal in the Software without restriction, including
+ * without limitation the rights to use, copy, modify, merge, publish,
+ * distribute, sublicense, and/or sell copies of the Software, and to
+ * permit persons to whom the Software is furnished to do so, subject to
+ * the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+ * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+ * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+ * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+#include <signal.h>
+
+#include "gpio.h"
+
+#define DEFAULT_IOPIN 8
+
+int running = 0;
+static int iopin;
+
+void
+sig_handler(int signo)
+{
+ if (signo == SIGINT) {
+ printf("closing IO%d nicely\n", iopin);
+ running = -1;
+ }
+}
+
+int
+main(int argc, char **argv)
+{
+ maa_result_t r = MAA_SUCCESS;
+ iopin = DEFAULT_IOPIN;
+
+ if (argc < 2) {
+ printf("Provide an int arg if you want to flash on something other than %d\n", DEFAULT_IOPIN);
+ } else {
+ iopin = strtol(argv[1], NULL, 10);
+ }
+
+ maa_init();
+ fprintf(stdout, "MAA Version: %s\nStarting Blinking on IO%d\n",
+ maa_get_version(), iopin);
+
+ maa_gpio_context* gpio;
+ gpio = maa_gpio_init(iopin);
+ printf("Initialised pin%d which is atually pin%d\n", iopin, gpio->pin);
+
+ // set direction to OUT
+ r = maa_gpio_dir(gpio, MAA_GPIO_OUT);
+ if (r != MAA_SUCCESS) {
+ maa_result_print(r);
+ }
+
+ signal(SIGINT, sig_handler);
+
+ while (running == 0) {
+ r = maa_gpio_write(gpio, 0);
+ if (r != MAA_SUCCESS) {
+ maa_result_print(r);
+ } else {
+ printf("off\n");
+ }
+
+ sleep(1);
+
+ r = maa_gpio_write(gpio, 1);
+ if (r != MAA_SUCCESS) {
+ maa_result_print(r);
+ } else {
+ printf("on\n");
+ }
+
+ sleep(1);
+ }
+
+ r = maa_gpio_close(gpio);
+ if (r != MAA_SUCCESS) {
+ maa_result_print(r);
+ }
+
+ return r;
+}
main(int argc, char **argv)
{
maa_init();
- fprintf(stdout, "MAA Version: %s\nStarting Blinking on IO8\n",
+ fprintf(stdout, "MAA Version: %s\nStarting Read on IO6\n",
maa_get_version());
+
maa_gpio_context* gpio;
- gpio = maa_gpio_init(8);
- maa_gpio_dir(gpio, MAA_GPIO_OUT);
- while (1) {
- maa_gpio_write(gpio, 0);
- sleep(1);
- maa_gpio_write(gpio, 1);
+ gpio = maa_gpio_init(6);
+
+ maa_gpio_dir(gpio, MAA_GPIO_IN);
+
+ for (;;) {
+ fprintf(stdout, "Gpio is %d\n", maa_gpio_read(gpio));
sleep(1);
}
+
+ maa_gpio_close(gpio);
+
return 0;
}
char bu[MAX_SIZE];
sprintf(bu, SYSFS_CLASS_GPIO "/gpio%d/value", dev->pin);
- if ((dev->value_fp = fopen(bu, "r+b")) == NULL) {
+ dev->value_fp = open(bu, O_RDWR);
+ if (dev->value_fp == -1) {
return MAA_ERROR_INVALID_RESOURCE;
}
maa_gpio_context* dev = (maa_gpio_context*) malloc(sizeof(maa_gpio_context));
memset(dev, 0, sizeof(maa_gpio_context));
+ dev->value_fp = -1;
dev->pin = pin;
if ((export_f = fopen(SYSFS_CLASS_GPIO "/export", "w")) == NULL) {
maa_result_t
maa_gpio_edge_mode(maa_gpio_context *dev, gpio_edge_t mode)
{
- if (dev->value_fp != NULL) {
- dev->value_fp = NULL;
+ if (dev->value_fp != -1) {
+ close(dev->value_fp);
+ dev->value_fp = -1;
}
char filepath[MAX_SIZE];
fwrite(bu, sizeof(char), length, edge);
fclose(edge);
- dev->value_fp = NULL;
return MAA_SUCCESS;
}
maa_result_t
maa_gpio_mode(maa_gpio_context *dev, gpio_mode_t mode)
{
- if (dev->value_fp != NULL) {
- dev->value_fp = NULL;
+ if (dev->value_fp != -1) {
+ close(dev->value_fp);
+ dev->value_fp = -1;
}
char filepath[MAX_SIZE];
fwrite(bu, sizeof(char), length, drive);
fclose(drive);
- dev->value_fp = NULL;
return MAA_SUCCESS;
}
maa_result_t
maa_gpio_dir(maa_gpio_context *dev, gpio_dir_t dir)
{
- if (dev == NULL)
+ if (dev == NULL) {
return MAA_ERROR_INVALID_HANDLE;
- if (dev->value_fp != NULL) {
- dev->value_fp = NULL;
+ }
+ if (dev->value_fp != -1) {
+ close(dev->value_fp);
+ dev->value_fp = -1;
}
char filepath[MAX_SIZE];
snprintf(filepath, MAX_SIZE, SYSFS_CLASS_GPIO "/gpio%d/direction", dev->pin);
fwrite(bu, sizeof(char), length, direction);
fclose(direction);
- dev->value_fp = NULL;
return MAA_SUCCESS;
}
int
maa_gpio_read(maa_gpio_context *dev)
{
- if (dev->value_fp == NULL) {
- maa_gpio_get_valfp(dev);
+ if (dev->value_fp == -1) {
+ if (maa_gpio_get_valfp(dev) != MAA_SUCCESS) {
+ fprintf(stderr, "Failed to get value file pointer\n");
+ }
+ }
+ else {
+ // if value_fp is new this is pointless
+ lseek(dev->value_fp, 0, SEEK_SET);
}
- fseek(dev->value_fp, SEEK_SET, 0);
char bu[2];
- fread(bu, 2, 1, dev->value_fp);
- fseek(dev->value_fp, SEEK_SET, 0);
+ if (read(dev->value_fp, bu, 2*sizeof(char)) != 2) {
+ fprintf(stderr, "Failed to read a sensible value from sysfs");
+ }
+ lseek(dev->value_fp, 0, SEEK_SET);
int ret = strtol(bu, NULL, 10);
return ret;
maa_result_t
maa_gpio_write(maa_gpio_context *dev, int value)
{
- if (dev->value_fp == NULL) {
+ if (dev->value_fp == -1) {
maa_gpio_get_valfp(dev);
}
- if (fseek(dev->value_fp, SEEK_SET, 0) != 0) {
+ if (lseek(dev->value_fp, 0, SEEK_SET) == -1) {
return MAA_ERROR_INVALID_RESOURCE;
}
char bu[MAX_SIZE];
int length = snprintf(bu, sizeof(bu), "%d", value);
- fwrite(bu, sizeof(char), length, dev->value_fp);
-
- if (fseek(dev->value_fp, SEEK_SET, 0) != 0) {
- return MAA_ERROR_INVALID_RESOURCE;
+ if (write(dev->value_fp, bu, length*sizeof(char)) == -1) {
+ return MAA_ERROR_INVALID_HANDLE;
}
- if (ferror(dev->value_fp) != 0)
- return MAA_ERROR_INVALID_RESOURCE;
+
return MAA_SUCCESS;
}
maa_result_t
maa_gpio_close(maa_gpio_context *dev)
{
- if (ferror(dev->value_fp) != 0) {
- return MAA_ERROR_INVALID_RESOURCE;
+ if (dev->value_fp != -1) {
+ close(dev->value_fp);
}
-
maa_gpio_unexport(dev);
free(dev);
return MAA_SUCCESS;