85 lines
1.5 KiB
C
85 lines
1.5 KiB
C
/*
|
|
* switch.c
|
|
* How to register an button and count this
|
|
* author: Simon Wörner, Manuel Vögele,
|
|
* Siegfried Kienzle
|
|
* 24-March-2017
|
|
*
|
|
* */
|
|
|
|
#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>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
#include <fcntl.h>
|
|
|
|
int writeIntoFile(char* value, char* path)
|
|
{
|
|
int fd;
|
|
char buffer[3];
|
|
strcpy(buffer, value);
|
|
if((fd=open(path, O_RDWR | O_CREAT | O_TRUNC)) == -1) {
|
|
perror(NULL);
|
|
return EXIT_FAILURE;
|
|
}
|
|
|
|
if((write(fd, &buffer, sizeof(buffer))) == -1) {
|
|
perror("Cannot write into file");
|
|
return EXIT_FAILURE;
|
|
}
|
|
printf("Success");
|
|
close(fd);
|
|
return EXIT_SUCCESS;
|
|
}
|
|
|
|
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);
|
|
reserve(PIN01);
|
|
reserve(PIN11);
|
|
inOrOut(PIN01, OUT);
|
|
inOrOut(PIN11, IN);
|
|
voltage(PIN06, V0);
|
|
voltage(PIN01, V33);
|
|
return 0;
|
|
}
|