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 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 <stdio.h>
#include <stdlib.h>
@@ -20,12 +22,12 @@
#include <unistd.h>
#include <fcntl.h>
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;
}