From bcad9de814660e56482774de73fda0d6ae2e2f6e Mon Sep 17 00:00:00 2001 From: Siegfried Kienzle Date: Fri, 24 Mar 2017 17:51:26 +0100 Subject: [PATCH] add function voltage and inOrOut --- V1/switch.c | 51 ++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 40 insertions(+), 11 deletions(-) diff --git a/V1/switch.c b/V1/switch.c index 3af17d7..d97f47a 100644 --- a/V1/switch.c +++ b/V1/switch.c @@ -7,12 +7,14 @@ * * */ -#define PIN01 "1\n" -#define PIN12 "12\n" -#define PIN11 "11\n" -#define PIN06 "6\n" +#define PIN01 "1" +#define PIN12 "12" +#define PIN11 "11" +#define PIN06 "6" #define IN "in\n" #define OUT "out\n" +#define V33 "1\n" +#define V0 "0\n" #include #include @@ -20,12 +22,12 @@ #include #include -int reserve(char* gpioPort) +int writeIntoFile(char* value, char* path) { int fd; - char buffer[2]; - strcpy(buffer, gpioPort ); - if((fd=open("/sys/class/gpio/export", O_RDWR | O_CREAT | O_TRUNC)) == -1) { + char buffer[3]; + strcpy(buffer, value); + if((fd=open(path, O_RDWR | O_CREAT | O_TRUNC)) == -1) { perror(NULL); return EXIT_FAILURE; } @@ -39,13 +41,40 @@ int reserve(char* gpioPort) return EXIT_SUCCESS; } -int inOut(char gpioPort, char * inOut) +int reserve(char* gpioPort) { + char str[3]; + strcpy(str, gpioPort); + strcat(str, "\n"); + return writeIntoFile(str,"/sys/class/gpio/export"); +} +int inOrOut(char* gpioPort, char* inOut) +{ + char *direc = "/direction"; + char *gpioPath = "/sys/class/gpio/gpio"; + char path[32]; + strcpy(path, gpioPath); + strcat(path, gpioPort); + strcat(path, direc); + return writeIntoFile(inOut, path); +} + +int voltage(char* gpioPort, char* volt) +{ + char *val = "/value"; + char *gpioPath = "/sys/class/gpio/gpio"; + char path[28]; + strcpy(path, gpioPath); + strcat(path, gpioPort); + strcat(path, val); + return writeIntoFile(volt, path); } int main() { - reserve(PIN06); - return 0; + reserve(PIN06); + inOrOut(PIN06, OUT); + voltage(PIN06, V0); + return 0; }