lorforlinux

BBAI libgpiod-led P8_03 - 07/21/2020

Jul 21st, 2020
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.42 KB | None | 0 0
  1. #include <gpiod.h>
  2. #include <stdio.h>
  3. #include <unistd.h>
  4.  
  5. #ifndef CONSUMER
  6. #define CONSUMER "Consumer"
  7. #endif
  8.  
  9. int main(int argc, char **argv)
  10. {
  11. // BBAI P8_03
  12. char *chipname = "gpiochip0";
  13. unsigned int line_num = 24;
  14. unsigned int val;
  15. struct gpiod_chip *chip;
  16. struct gpiod_line *line;
  17. int i, ret;
  18.  
  19. chip = gpiod_chip_open_by_name(chipname);
  20. if (!chip) {
  21. perror("Open chip failed\n");
  22. goto end;
  23. }
  24.  
  25. line = gpiod_chip_get_line(chip, line_num);
  26. if (!line) {
  27. perror("Get line failed\n");
  28. goto close_chip;
  29. }
  30.  
  31. ret = gpiod_line_request_output(line, CONSUMER, 0);
  32. if (ret < 0) {
  33. perror("Request line as output failed\n");
  34. goto release_line;
  35. }
  36.  
  37. /* Blink 20 times */
  38. val = 0;
  39. for (i = 20; i > 0; i--) {
  40. ret = gpiod_line_set_value(line, val);
  41. if (ret < 0) {
  42. perror("Set line output failed\n");
  43. goto release_line;
  44. }
  45. printf("Output %u on line #%u\n", val, line_num);
  46. sleep(1);
  47. val = !val;
  48. }
  49.  
  50. release_line:
  51. gpiod_line_release(line);
  52. close_chip:
  53. gpiod_chip_close(chip);
  54. end:
  55. return 0;
  56. }
Add Comment
Please, Sign In to add comment