test_beep.c 1.3 KB

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