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 mainLoop(char* gpioPort)
{
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() int main()
{ {
reserve(PIN06); reserve(BUTTON);
reserve(PIN01); setDirection(BUTTON, IN);
reserve(PIN11); mainLoop(BUTTON);
inOrOut(PIN01, OUT); unexport(BUTTON);
inOrOut(PIN11, IN);
voltage(PIN06, V0);
voltage(PIN01, V33);
return 0; return 0;
} }