Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <string.h>
- #define SW_DIR_PATH "/sys/class/gpio/gpio248/direction"
- int configure_pins();
- int pinunexport();
- int iniciar0();
- int iniciar1();
- void close_fd(int);
- int ok;
- int main(int argc, char *argv[]){
- int fd;
- configure_pins();
- printf("Pinos configurados como Saida\n");
- iniciar0();
- usleep(10000);
- do{
- iniciar1();
- }while(ok = 1);
- if (ok = 0){
- pinunexport();
- printf("Pinos configurados como inutilizavél no momento, encerrar programa\n");
- return EXIT_FAILURE;
- }
- ok = 0;
- return EXIT_SUCCESS;
- }
- int configure_pins(){
- int fd;
- int ok;
- fd = open("/sys/class/gpio/export", O_WRONLY);
- write(fd, "250", 3);
- close(fd);
- fd = open("/sys/class/gpio/gpio250/direction", O_WRONLY);
- write(fd, "out", 3);
- close(fd);
- fd = open("/sys/class/gpio/export", O_WRONLY);
- write(fd, "248", 3);
- close(fd);
- fd = open("/sys/class/gpio/gpio248/direction", O_WRONLY);
- write(fd, "out", 3);
- close(fd);
- fd = open("/sys/class/gpio/export", O_WRONLY);
- write(fd, "173", 3);
- close(fd);
- fd = open("/sys/class/gpio/gpio173/direction", O_WRONLY);
- write(fd, "out", 3);
- close(fd);
- fd = open("/sys/class/gpio/export", O_WRONLY);
- write(fd, "172", 3);
- close(fd);
- fd = open("/sys/class/gpio/gpio172/direction", O_WRONLY);
- write(fd, "out", 3);
- close(fd);
- ok = 1;
- return EXIT_SUCCESS;
- }
- int pinunexport(){
- int fd;
- if(fd = open(SW_DIR_PATH, O_WRONLY) <=0){
- perror("Não é possível abrir o arquivo de direção para o PIN \n");
- ok = 0;
- return EXIT_FAILURE;
- }
- else {
- fd = open("/sys/class/gpio/unexport", O_WRONLY);
- write(fd, "250", 3);
- close(fd);
- fd = open("/sys/class/gpio/unexport", O_WRONLY);
- write(fd, "248", 3);
- close(fd);
- fd = open("/sys/class/gpio/unexport", O_WRONLY);
- write(fd, "173", 3);
- close(fd);
- fd = open("/sys/class/gpio/unexport", O_WRONLY);
- write(fd, "172", 3);
- close(fd);
- }
- return 0;
- }
- int iniciar0(){
- int fd;
- fd = open("/sys/class/gpio/gpio250/value", O_WRONLY | O_SYNC);
- write(fd, "0", 1);
- close(fd);
- fd = open("/sys/class/gpio/gpio248/value", O_WRONLY | O_SYNC);
- write(fd, "0", 1);
- close(fd);
- fd = open("/sys/class/gpio/gpio173/value", O_WRONLY | O_SYNC);
- write(fd, "0", 1);
- close(fd);
- fd = open("/sys/class/gpio/gpio172/value", O_WRONLY | O_SYNC);
- write(fd, "0", 1);
- close(fd);
- }
- int iniciar1(){
- int fd;
- fd = open("/sys/class/gpio/gpio250/value", O_WRONLY | O_SYNC);
- write(fd, "1", 1);
- close(fd);
- fd = open("/sys/class/gpio/gpio248/value", O_WRONLY | O_SYNC);
- write(fd, "1", 1);
- close(fd);
- fd = open("/sys/class/gpio/gpio173/value", O_WRONLY | O_SYNC);
- write(fd, "1", 1);
- close(fd);
- fd = open("/sys/class/gpio/gpio172/value", O_WRONLY | O_SYNC);
- write(fd, "1", 1);
- close(fd);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement