From 73e359873f119e7ca4e7565c79b34842b75c70c0 Mon Sep 17 00:00:00 2001 From: Siegfried Kienzle Date: Tue, 25 Apr 2017 07:33:40 +0200 Subject: [PATCH] outsourcing of gpio-code --- project/gpio.c | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++ project/gpio.h | 12 ++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 project/gpio.c create mode 100644 project/gpio.h diff --git a/project/gpio.c b/project/gpio.c new file mode 100644 index 0000000..2e2345b --- /dev/null +++ b/project/gpio.c @@ -0,0 +1,51 @@ +/*GPIO.c*/ +#include +#include +#include +#include +#include + +#define BASEPATH "/sys/class/gpio/" +#define GPIO_FOLDER "gpio%s/" + +void writeFile(char *filename, char *buffer, size_t count) +{ + int fd = open(filename, O_WRONLY); + if(fd == -1) { + perror("Fehler bei open "); + } + write(fd, buffer, count); + close(fd); +} + +void registerPin(char *pin) +{ + writeFile(BASEPATH "export", pin, strlen(pin)); +} + +void freePin(char *pin) +{ + writeFile(BASEPATH "unexport", pin, strlen(pin)); +} + +void setDirection(char *pin, char *direction, int dirlen) +{ + char path[50]; + sprintf(path, BASEPATH GPIO_FOLDER "direction", pin); + writeFile(path, direction, dirlen); +} + +void registerOutput(char *pin) +{ + registerPin(pin); + setDirection(pin, "out", 3); +} + +void writeOutput(char *pin, int state) +{ + char path[50]; + sprintf(path, BASEPATH GPIO_FOLDER "value", pin); + FILE *fd = fopen(path, "w"); + fprintf(fd, "%i", state); + fclose(fd); +} \ No newline at end of file diff --git a/project/gpio.h b/project/gpio.h new file mode 100644 index 0000000..8b4ac73 --- /dev/null +++ b/project/gpio.h @@ -0,0 +1,12 @@ +/*gpio.h*/ +#ifndef GPIO_H_ +#define GPIO_H_ + +void writeFile(char *filename, char *buffer, size_t count); +void registerPin(char *pin); +void freePin(char *pin); +void setDirection(char *pin, char *direction, int dirlen); +void registerOutput(char *pin); +void writeOutput(char *pin, int state); + +#endif \ No newline at end of file