test_beep.c 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8. #define DEVICE_PATH "/dev/buzzer"
  9. int main(int argc, char *argv[])
  10. {
  11. unsigned int flag = 0;
  12. unsigned int freq = 0;
  13. unsigned int duration = 0;
  14. unsigned char buf[4];
  15. int fd = 0;
  16. int ret = 0;
  17. if(argc != 4 && argc != 2)
  18. {
  19. printf("Usage: %s on 2 2400\n", argv[0]);
  20. return 1;
  21. }
  22. if(strcmp(argv[1], "on") == 0 && argc == 4)
  23. {
  24. flag = 1;
  25. freq = atoi(argv[3]);
  26. duration = atoi(argv[2]);
  27. }
  28. else if(strcmp(argv[1], "off") == 0)
  29. {
  30. flag = 0;
  31. }
  32. else
  33. {
  34. printf("Usage: %s on 2 2400\n", argv[0]);
  35. return 1;
  36. }
  37. memset(buf, 0, sizeof(buf));
  38. buf[0] = flag;
  39. buf[1] = freq&0xff;
  40. buf[2] = (freq>>8)&0xff;
  41. buf[3] = duration&0xff;
  42. fd = open(DEVICE_PATH, O_RDWR);
  43. if(fd < 0)
  44. {
  45. printf("open /dev/buzzer failed\n");
  46. return 1;
  47. }
  48. ret = write(fd, buf, sizeof(buf));
  49. if(ret < 0)
  50. {
  51. printf("write /dev/buzzer failed\n");
  52. return 1;
  53. }
  54. close(fd);
  55. }