Private
Public Access
2
0

gpio: Add cpp example to use IO.

* Patch direction setting

Signed-off-by: Thomas Ingleby <thomas.c.ingleby@intel.com>
This commit is contained in:
Thomas Ingleby
2014-04-11 17:57:53 +01:00
parent 6bf02cf25e
commit 40438d709a
2 changed files with 29 additions and 8 deletions

20
examples/blink-io8.cpp Normal file
View File

@@ -0,0 +1,20 @@
#include "stdio.h"
#include "maa.h"
int
main(int argc, char **argv)
{
fprintf(stdout, "MAA Version: %d\n Starting Blinking on IO8", get_version());
gpio_t gpio;
gpio_init(&gpio, 26);
gpio_dir(&gpio, "out");
while(1){
gpio_write(&gpio, 0);
sleep(1);
gpio_write(&gpio, 1);
sleep(1);
}
return 0;
}

View File

@@ -63,19 +63,20 @@ gpio_mode(gpio_t *gpio, gpio_mode_t mode) {
void
gpio_dir(gpio_t *gpio, gpio_dir_t dir) {
fclose(gpio->value_fp);
gpio->value_fp = NULL;
//if(gpio->value_fp != NULL) {
// fclose(gpio->value_fp);
// gpio->value_fp = NULL;
// }
char filepath[64];
snprintf(filepath, 64, "/sys/class/gpio/gpio%d/direction", gpio->pin);
int fd;
fd = open(filepath, O_WRONLY);
if(fd == -1) {
FILE *direction;
if((direction = fopen(filepath, "w")) == NULL) {
fprintf(stderr, "Failed to open direction for writing!\n");
} else if(write(fd, dir, 5) == -1) {
fprintf(stderr, "Failed to set direction\n");
} else {
close(fd);
fprintf(direction, dir);
fclose(direction);
gpio->value_fp = NULL;
}
}