/* * 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 #include #include #include #include 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); inOrOut(PIN06, OUT); voltage(PIN06, V0); return 0; }