Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <unistd.h>
  5. #include <sys/stat.h>
  6. #include <string.h>
  7. #define SW_DIR_PATH      "/sys/class/gpio/gpio248/direction"
  8.  
  9. int configure_pins();
  10. int pinunexport();
  11. int iniciar0();
  12. int iniciar1();
  13. void close_fd(int);
  14. int ok;
  15.  
  16. int main(int argc, char *argv[]){
  17.     int fd;
  18.     configure_pins();
  19.     printf("Pinos configurados como Saida\n");
  20.     iniciar0();
  21.     usleep(10000);
  22.  
  23.     do{
  24.  
  25.       iniciar1();
  26.  
  27.         }while(ok = 1);
  28.  
  29.         if (ok = 0){
  30.  
  31.         pinunexport();
  32.         printf("Pinos configurados como inutilizavél no momento, encerrar programa\n");
  33.         return EXIT_FAILURE;
  34. }
  35.         ok = 0;
  36.         return EXIT_SUCCESS;
  37.     }
  38.  
  39.  
  40.  
  41. int configure_pins(){
  42.     int fd;
  43.     int ok;
  44.  
  45.     fd = open("/sys/class/gpio/export", O_WRONLY);
  46.     write(fd, "250", 3);
  47.     close(fd);
  48.  
  49.     fd = open("/sys/class/gpio/gpio250/direction", O_WRONLY);
  50.     write(fd, "out", 3);
  51.     close(fd);
  52.  
  53.     fd = open("/sys/class/gpio/export", O_WRONLY);
  54.     write(fd, "248", 3);
  55.     close(fd);
  56.  
  57.     fd = open("/sys/class/gpio/gpio248/direction", O_WRONLY);
  58.     write(fd, "out", 3);
  59.     close(fd);
  60.  
  61.     fd = open("/sys/class/gpio/export", O_WRONLY);
  62.     write(fd, "173", 3);
  63.     close(fd);
  64.  
  65.     fd = open("/sys/class/gpio/gpio173/direction", O_WRONLY);
  66.     write(fd, "out", 3);
  67.     close(fd);
  68.  
  69.     fd = open("/sys/class/gpio/export", O_WRONLY);
  70.     write(fd, "172", 3);
  71.     close(fd);
  72.  
  73.     fd = open("/sys/class/gpio/gpio172/direction", O_WRONLY);
  74.     write(fd, "out", 3);
  75.     close(fd);
  76.  
  77.     ok = 1;
  78.  
  79.     return EXIT_SUCCESS;
  80.  
  81. }
  82.  
  83. int pinunexport(){
  84. int fd;
  85.    if(fd = open(SW_DIR_PATH, O_WRONLY) <=0){
  86.         perror("Não é possível abrir o arquivo de direção para o PIN \n");
  87.         ok = 0;
  88.         return EXIT_FAILURE;
  89.     }
  90.  
  91.         else {
  92. fd = open("/sys/class/gpio/unexport", O_WRONLY);
  93.     write(fd, "250", 3);
  94.     close(fd);
  95.  
  96. fd = open("/sys/class/gpio/unexport", O_WRONLY);
  97.     write(fd, "248", 3);
  98.     close(fd);
  99.  
  100. fd = open("/sys/class/gpio/unexport", O_WRONLY);
  101.     write(fd, "173", 3);
  102.     close(fd);
  103.  
  104. fd = open("/sys/class/gpio/unexport", O_WRONLY);
  105.     write(fd, "172", 3);
  106.     close(fd);
  107.   }
  108.     return 0;
  109. }
  110.  
  111. int iniciar0(){
  112.     int fd;
  113.         fd = open("/sys/class/gpio/gpio250/value", O_WRONLY | O_SYNC);
  114.         write(fd, "0", 1);
  115.         close(fd);
  116.         fd = open("/sys/class/gpio/gpio248/value", O_WRONLY | O_SYNC);
  117.         write(fd, "0", 1);
  118.         close(fd);
  119.         fd = open("/sys/class/gpio/gpio173/value", O_WRONLY | O_SYNC);
  120.         write(fd, "0", 1);
  121.         close(fd);
  122.         fd = open("/sys/class/gpio/gpio172/value", O_WRONLY | O_SYNC);
  123.         write(fd, "0", 1);
  124.         close(fd);
  125. }
  126.  
  127. int iniciar1(){
  128.     int fd;
  129.         fd = open("/sys/class/gpio/gpio250/value", O_WRONLY | O_SYNC);
  130.         write(fd, "1", 1);
  131.         close(fd);
  132.         fd = open("/sys/class/gpio/gpio248/value", O_WRONLY | O_SYNC);
  133.         write(fd, "1", 1);
  134.         close(fd);
  135.         fd = open("/sys/class/gpio/gpio173/value", O_WRONLY | O_SYNC);
  136.         write(fd, "1", 1);
  137.         close(fd);
  138.         fd = open("/sys/class/gpio/gpio172/value", O_WRONLY | O_SYNC);
  139.         write(fd, "1", 1);
  140.         close(fd);
  141. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement