rename some functions and add some implementations

This commit is contained in:
Kienzle
2017-03-29 11:10:15 +02:00
parent fc25e2f506
commit 8281c38db8

View File

@@ -7,14 +7,12 @@
* *
* */ * */
#define PIN01 "1" #define BUTTON "17"
#define PIN12 "12"
#define PIN11 "11"
#define PIN06 "6"
#define IN "in\n" #define IN "in\n"
#define OUT "out\n" #define OUT "out\n"
#define V33 "1\n" #define HIGH "1\n"
#define V0 "0\n" #define LOW "0\n"
#define MAXBYTES 1
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -49,18 +47,18 @@ int reserve(char* gpioPort)
return writeIntoFile(str,"/sys/class/gpio/export"); return writeIntoFile(str,"/sys/class/gpio/export");
} }
int inOrOut(char* gpioPort, char* inOut) int setDirection(char* gpioPort, char* inOut)
{ {
char *direc = "/direction"; char *direc = "/direction";
char *gpioPath = "/sys/class/gpio/gpio"; char *gpioPath = "/sys/class/gpio/gpio";
char path[32]; char path[32];
strcpy(path, gpioPath); strcpy(path, gpioPath);
strcat(path, gpioPort); strcat(path, gpioPort);
strcat(path, direc); strcat(path, direc);
return writeIntoFile(inOut, path); return writeIntoFile(inOut, path);
} }
int voltage(char* gpioPort, char* volt) int setOutput(char* gpioPort, char* volt)
{ {
char *val = "/value"; char *val = "/value";
char *gpioPath = "/sys/class/gpio/gpio"; char *gpioPath = "/sys/class/gpio/gpio";
@@ -71,14 +69,52 @@ int voltage(char* gpioPort, char* volt)
return writeIntoFile(volt, path); return writeIntoFile(volt, path);
} }
int main() int mainLoop(char* gpioPort)
{ {
reserve(PIN06); int fid, portValue, count, countButton;
reserve(PIN01); char *val = "/value";
reserve(PIN11); char *gpioPath = "/sys/class/gpio/gpio";
inOrOut(PIN01, OUT); char path[28];
inOrOut(PIN11, IN); strcpy(path, gpioPath);
voltage(PIN06, V0); strcat(path, gpioPort);
voltage(PIN01, V33); 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; return 0;
} }