outsourcing of gpio-code

This commit is contained in:
Siegfried Kienzle
2017-04-25 07:33:04 +02:00
parent 0bcdfb38a3
commit 1616dd670b

View File

@@ -3,9 +3,10 @@
#include<string.h>
#include<unistd.h>
#include<string.h>
#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)
{