From 8281c38db8c3949009e907725ca038962b604f84 Mon Sep 17 00:00:00 2001 From: Kienzle Date: Wed, 29 Mar 2017 11:10:15 +0200 Subject: [PATCH] rename some functions and add some implementations --- V1/switch.c | 70 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 53 insertions(+), 17 deletions(-) diff --git a/V1/switch.c b/V1/switch.c index 0ac0d12..b691f98 100644 --- a/V1/switch.c +++ b/V1/switch.c @@ -7,14 +7,12 @@ * * */ -#define PIN01 "1" -#define PIN12 "12" -#define PIN11 "11" -#define PIN06 "6" +#define BUTTON "17" #define IN "in\n" #define OUT "out\n" -#define V33 "1\n" -#define V0 "0\n" +#define HIGH "1\n" +#define LOW "0\n" +#define MAXBYTES 1 #include #include @@ -49,18 +47,18 @@ int reserve(char* gpioPort) return writeIntoFile(str,"/sys/class/gpio/export"); } -int inOrOut(char* gpioPort, char* inOut) +int setDirection(char* gpioPort, char* inOut) { char *direc = "/direction"; char *gpioPath = "/sys/class/gpio/gpio"; char path[32]; strcpy(path, gpioPath); - strcat(path, gpioPort); + strcat(path, gpioPort); strcat(path, direc); return writeIntoFile(inOut, path); } -int voltage(char* gpioPort, char* volt) +int setOutput(char* gpioPort, char* volt) { char *val = "/value"; char *gpioPath = "/sys/class/gpio/gpio"; @@ -71,14 +69,52 @@ int voltage(char* gpioPort, char* volt) return writeIntoFile(volt, path); } -int main() +int mainLoop(char* gpioPort) { - reserve(PIN06); - reserve(PIN01); - reserve(PIN11); - inOrOut(PIN01, OUT); - inOrOut(PIN11, IN); - voltage(PIN06, V0); - voltage(PIN01, V33); + int fid, portValue, count, countButton; + char *val = "/value"; + char *gpioPath = "/sys/class/gpio/gpio"; + char path[28]; + strcpy(path, gpioPath); + strcat(path, gpioPort); + strcat(path, val); + + while(true) + { + if( (fid=open(path, O_RDONLY)) == -1) + perror("Fehler "); + if(readInput(fid) == 1) + { + countButton++; + printf("%d", countButton); + } + + + close(fid); + } +} + +char[] readInput(int fid) +{ + char buffer[MAXBYTES]; + read(fid, &buffer, MAXBYTES); + return buffer; +} + +int unexport(char* gpioport) +{ + char str[3]; + strcpy(str, gpioPort); + strcat(str, "\n"); + return writeIntoFile(str,"/sys/class/gpio/unexport"); +} + + +int main() +{ + reserve(BUTTON); + setDirection(BUTTON, IN); + mainLoop(BUTTON); + unexport(BUTTON); return 0; }