ToneAlarm.cpp 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #include "ToneAlarm.h"
  2. #include <fcntl.h>
  3. #include <iostream>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <sys/stat.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. #include <AP_Math/AP_Math.h>
  10. #include <AP_HAL/AP_HAL.h>
  11. using namespace Linux;
  12. extern const AP_HAL::HAL& hal;
  13. ToneAlarm::ToneAlarm()
  14. {
  15. #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BLUE || \
  16. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_PXF || \
  17. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BBBMINI
  18. period_fd = open("/sys/devices/ocp.3/pwm_test_P8_36.12/period",O_WRONLY|O_CLOEXEC);
  19. duty_fd = open("/sys/devices/ocp.3/pwm_test_P8_36.12/duty",O_WRONLY|O_CLOEXEC);
  20. run_fd = open("/sys/devices/ocp.3/pwm_test_P8_36.12/run",O_WRONLY|O_CLOEXEC);
  21. #endif
  22. }
  23. bool ToneAlarm::init()
  24. {
  25. if ((period_fd == -1) || (duty_fd == -1) || (run_fd == -1)) {
  26. #if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BLUE || \
  27. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_PXF || \
  28. CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BBBMINI
  29. hal.console->printf("ToneAlarm: Error!! please check if PWM overlays are loaded correctly");
  30. #endif
  31. return false;
  32. }
  33. return true;
  34. }
  35. void ToneAlarm::set_buzzer_tone(float frequency, float volume, uint32_t duration_ms)
  36. {
  37. if (is_zero(frequency) || is_zero(volume)) {
  38. dprintf(run_fd,"0");
  39. } else {
  40. dprintf(run_fd,"0");
  41. dprintf(period_fd,"%u",(unsigned int)roundf(1000000000/frequency));
  42. dprintf(duty_fd,"%u",(unsigned int)roundf(volume*500000000/frequency));
  43. dprintf(run_fd,"1");
  44. }
  45. }