rename some functions and add some implementations
This commit is contained in:
66
V1/switch.c
66
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 <stdio.h>
|
||||
#include <stdlib.h>
|
||||
@@ -49,7 +47,7 @@ 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";
|
||||
@@ -60,7 +58,7 @@ int inOrOut(char* gpioPort, char* inOut)
|
||||
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 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()
|
||||
{
|
||||
reserve(PIN06);
|
||||
reserve(PIN01);
|
||||
reserve(PIN11);
|
||||
inOrOut(PIN01, OUT);
|
||||
inOrOut(PIN11, IN);
|
||||
voltage(PIN06, V0);
|
||||
voltage(PIN01, V33);
|
||||
reserve(BUTTON);
|
||||
setDirection(BUTTON, IN);
|
||||
mainLoop(BUTTON);
|
||||
unexport(BUTTON);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user