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