capture.c 872 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include<stdlib.h>
  2. #include<stdio.h>
  3. #include<unistd.h>
  4. #include<sys/types.h>
  5. #include<sys/stat.h>
  6. #include<fcntl.h>
  7. int main(int argc,char *argv[])
  8. {
  9. int ret;
  10. int fd;
  11. char buffer[100];
  12. ssize_t bytes_read;
  13. ret = access("/sys/class/pwm/pwmchip0/pwm0",F_OK);
  14. if(ret != 0 )
  15. {
  16. fd = open("/sys/class/pwm/pwmchip0/export",O_WRONLY);
  17. write(fd,"0",1);
  18. close(fd);
  19. }
  20. fd = open("/sys/class/pwm/pwmchip0/pwm0/capture",O_RDONLY);
  21. if(fd == -1)
  22. {
  23. perror("failed to open capture file");
  24. return EXIT_FAILURE;
  25. }
  26. bytes_read = read(fd,buffer,sizeof(buffer)-1);
  27. if(bytes_read == -1)
  28. {
  29. perror("failed to read capture value");
  30. close(fd);
  31. return EXIT_FAILURE;
  32. }
  33. buffer[bytes_read] = '\0';
  34. printf("capture value:%s\n",buffer);
  35. close(fd);
  36. return 0;
  37. }