silver2row

Ideas for gpiod and running examples!

Mar 21st, 2022 (edited)
408
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.23 KB | None | 0 0
  1. /*
  2.   Simple gpiod example of toggling a LED connected a gpio line from
  3.   the BeagleBone Black Wireless and RelayCape.
  4.   Exits when CTRL-C is typed. Seth typed this driver with help
  5.   from ICS.com and their GPIO series...
  6. */
  7.  
  8. #include <gpiod.h>
  9. #include <stdio.h>
  10. #include <unistd.h>
  11.  
  12. int main(int argc, char **argv)
  13. {
  14.   const char *chipname = "gpiochip3";
  15.   struct gpiod_chip *chip;
  16.   struct gpiod_line *lineRed;    // Red LED
  17.  
  18.   int i, ret, val;
  19.  
  20.   // Open GPIO chip
  21.   chip = gpiod_chip_open_by_name(chipname);
  22.   if (!chip) {
  23.     perror("Open chip failed\n");
  24.     return 1;
  25.   }
  26.  
  27.   // Open GPIO lines
  28.   lineRed = gpiod_chip_get_line(chip, 16);
  29.   if (!lineRed) {
  30.     perror("Get line failed\n");
  31.     return 1;
  32.   }
  33.  
  34.   // Open LED line for output
  35.   ret = gpiod_line_request_output(lineRed, "MySecond", 0);
  36.   if (ret < 0) {
  37.     perror("Request line as output failed\n");
  38.     return 1;
  39.   }
  40.  
  41.   // Blink a LED
  42.   i = 0;
  43.   while (true) {
  44.     ret = gpiod_line_set_value(lineRed, (i & 1) != 0);
  45.     if (ret < 0) {
  46.       perror("Set line output failed\n");
  47.       return 1;
  48.     }
  49.  
  50.     usleep(1000000);
  51.     i++;
  52.   }
  53.  
  54.   // Release lines and chip
  55.   gpiod_line_release(lineRed);
  56.   gpiod_chip_close(chip);
  57.   return 0;
  58. }
  59.  
  60.  
Add Comment
Please, Sign In to add comment