test_beep.c 1.1 KB

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