add function voltage and inOrOut

This commit is contained in:
Siegfried Kienzle
2017-03-24 17:51:26 +01:00
parent a2baf654b6
commit bcad9de814

View File

@@ -7,12 +7,14 @@
* *
* */ * */
#define PIN01 "1\n" #define PIN01 "1"
#define PIN12 "12\n" #define PIN12 "12"
#define PIN11 "11\n" #define PIN11 "11"
#define PIN06 "6\n" #define PIN06 "6"
#define IN "in\n" #define IN "in\n"
#define OUT "out\n" #define OUT "out\n"
#define V33 "1\n"
#define V0 "0\n"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
@@ -20,12 +22,12 @@
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
int reserve(char* gpioPort) int writeIntoFile(char* value, char* path)
{ {
int fd; int fd;
char buffer[2]; char buffer[3];
strcpy(buffer, gpioPort ); strcpy(buffer, value);
if((fd=open("/sys/class/gpio/export", O_RDWR | O_CREAT | O_TRUNC)) == -1) { if((fd=open(path, O_RDWR | O_CREAT | O_TRUNC)) == -1) {
perror(NULL); perror(NULL);
return EXIT_FAILURE; return EXIT_FAILURE;
} }
@@ -39,13 +41,40 @@ int reserve(char* gpioPort)
return EXIT_SUCCESS; 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() int main()
{ {
reserve(PIN06); reserve(PIN06);
inOrOut(PIN06, OUT);
voltage(PIN06, V0);
return 0; return 0;
} }