2016-03-10 11:28:22 -04:00
|
|
|
#include "ToneAlarm.h"
|
|
|
|
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <iostream>
|
2014-08-23 04:52:43 -03:00
|
|
|
#include <stdio.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <stdlib.h>
|
2014-08-23 04:52:43 -03:00
|
|
|
#include <sys/stat.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <sys/types.h>
|
2014-08-23 04:52:43 -03:00
|
|
|
#include <unistd.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
|
2018-07-25 19:28:50 -03:00
|
|
|
#include <AP_Math/AP_Math.h>
|
2016-05-17 23:26:57 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2014-08-23 04:52:43 -03:00
|
|
|
|
|
|
|
using namespace Linux;
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
2014-11-12 02:43:52 -04:00
|
|
|
|
2014-11-15 01:51:21 -04:00
|
|
|
ToneAlarm::ToneAlarm()
|
2014-08-23 04:52:43 -03:00
|
|
|
{
|
2017-09-27 20:43:08 -03:00
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BLUE || \
|
|
|
|
CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_PXF || \
|
|
|
|
CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BBBMINI
|
2016-10-30 10:22:29 -03:00
|
|
|
period_fd = open("/sys/devices/ocp.3/pwm_test_P8_36.12/period",O_WRONLY|O_CLOEXEC);
|
|
|
|
duty_fd = open("/sys/devices/ocp.3/pwm_test_P8_36.12/duty",O_WRONLY|O_CLOEXEC);
|
|
|
|
run_fd = open("/sys/devices/ocp.3/pwm_test_P8_36.12/run",O_WRONLY|O_CLOEXEC);
|
2017-09-27 20:43:08 -03:00
|
|
|
#endif
|
2014-08-23 04:52:43 -03:00
|
|
|
}
|
2014-11-15 01:51:21 -04:00
|
|
|
|
|
|
|
bool ToneAlarm::init()
|
2014-08-23 04:52:43 -03:00
|
|
|
{
|
2017-09-27 20:43:08 -03:00
|
|
|
if ((period_fd == -1) || (duty_fd == -1) || (run_fd == -1)) {
|
|
|
|
#if CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BLUE || \
|
|
|
|
CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_PXF || \
|
|
|
|
CONFIG_HAL_BOARD_SUBTYPE == HAL_BOARD_SUBTYPE_LINUX_BBBMINI
|
2014-11-15 01:58:58 -04:00
|
|
|
hal.console->printf("ToneAlarm: Error!! please check if PWM overlays are loaded correctly");
|
2017-09-27 20:43:08 -03:00
|
|
|
#endif
|
2014-11-15 01:51:21 -04:00
|
|
|
return false;
|
2014-08-23 04:52:43 -03:00
|
|
|
}
|
2014-11-15 01:51:21 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2018-07-27 04:12:48 -03:00
|
|
|
void ToneAlarm::set_buzzer_tone(float frequency, float volume, uint32_t duration_ms)
|
2014-08-23 04:52:43 -03:00
|
|
|
{
|
2018-07-25 19:28:50 -03:00
|
|
|
if (is_zero(frequency) || is_zero(volume)) {
|
|
|
|
dprintf(run_fd,"0");
|
|
|
|
} else {
|
2014-11-09 15:51:53 -04:00
|
|
|
dprintf(run_fd,"0");
|
2018-07-25 19:28:50 -03:00
|
|
|
dprintf(period_fd,"%u",(unsigned int)roundf(1000000000/frequency));
|
|
|
|
dprintf(duty_fd,"%u",(unsigned int)roundf(volume*500000000/frequency));
|
2014-11-09 15:51:53 -04:00
|
|
|
dprintf(run_fd,"1");
|
2014-11-12 06:01:21 -04:00
|
|
|
}
|
2014-08-23 04:52:43 -03:00
|
|
|
}
|