From 1616dd670b0a793bf89a0505a7e4b5f749133705 Mon Sep 17 00:00:00 2001 From: Siegfried Kienzle Date: Tue, 25 Apr 2017 07:33:04 +0200 Subject: [PATCH] outsourcing of gpio-code --- project/motor.c | 45 +++------------------------------------------ 1 file changed, 3 insertions(+), 42 deletions(-) diff --git a/project/motor.c b/project/motor.c index 4b15e8f..43f2701 100755 --- a/project/motor.c +++ b/project/motor.c @@ -3,9 +3,10 @@ #include #include #include +#include "gpio.h" + + -#define BASEPATH "/sys/class/gpio/" -#define GPIO_FOLDER "gpio%s/" #define PIN_MOTOR1_FORWARD "20" #define PIN_MOTOR1_REVERSE "13" @@ -15,47 +16,7 @@ #define MOTOR_ON 1 #define MOTOR_OFF 0 -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); -} void forward(int motorNumber) {