2013-11-28 00:28:14 -04:00
|
|
|
/*
|
|
|
|
Buzzer driver
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2016-01-29 00:38:21 -04:00
|
|
|
#include "Buzzer.h"
|
2013-11-28 00:28:14 -04:00
|
|
|
|
2015-08-11 03:28:44 -03:00
|
|
|
#include <AP_HAL/AP_HAL.h>
|
2013-11-28 00:28:14 -04:00
|
|
|
|
|
|
|
#include "AP_Notify.h"
|
|
|
|
|
|
|
|
extern const AP_HAL::HAL& hal;
|
|
|
|
|
2014-11-14 10:14:40 -04:00
|
|
|
bool Buzzer::init()
|
2013-11-28 00:28:14 -04:00
|
|
|
{
|
2018-07-30 01:36:27 -03:00
|
|
|
if (pNotify->buzzer_enabled() == false) {
|
|
|
|
return false;
|
|
|
|
}
|
2018-01-22 05:11:14 -04:00
|
|
|
_pin = pNotify->get_buzz_pin();
|
2023-04-20 21:16:33 -03:00
|
|
|
if (_pin <= 0) {
|
2021-11-14 20:41:33 -04:00
|
|
|
// no buzzer
|
|
|
|
return false;
|
|
|
|
}
|
2013-11-30 10:27:58 -04:00
|
|
|
|
2013-11-28 00:28:14 -04:00
|
|
|
// setup the pin and ensure it's off
|
2018-01-22 05:11:14 -04:00
|
|
|
hal.gpio->pinMode(_pin, HAL_GPIO_OUTPUT);
|
2013-11-28 00:28:14 -04:00
|
|
|
on(false);
|
|
|
|
|
2016-05-12 13:50:58 -03:00
|
|
|
// set initial boot states. This prevents us issuing a arming
|
2013-11-28 00:28:14 -04:00
|
|
|
// warning in plane and rover on every boot
|
|
|
|
_flags.armed = AP_Notify::flags.armed;
|
|
|
|
_flags.failsafe_battery = AP_Notify::flags.failsafe_battery;
|
2014-11-14 10:14:40 -04:00
|
|
|
return true;
|
2013-11-28 00:28:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// update - updates led according to timed_updated. Should be called at 50Hz
|
|
|
|
void Buzzer::update()
|
2018-10-09 03:05:23 -03:00
|
|
|
{
|
|
|
|
update_pattern_to_play();
|
|
|
|
update_playing_pattern();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Buzzer::update_pattern_to_play()
|
2013-11-28 00:28:14 -04:00
|
|
|
{
|
2015-02-02 16:58:20 -04:00
|
|
|
// check for arming failed event
|
|
|
|
if (AP_Notify::events.arming_failed) {
|
|
|
|
// arming failed buzz
|
|
|
|
play_pattern(SINGLE_BUZZ);
|
2018-10-09 03:05:23 -03:00
|
|
|
return;
|
2015-02-02 16:58:20 -04:00
|
|
|
}
|
|
|
|
|
2018-10-09 03:05:23 -03:00
|
|
|
if (AP_HAL::millis() - _pattern_start_time < _pattern_start_interval_time_ms) {
|
|
|
|
// do not interrupt playing patterns / enforce minumum separation
|
2013-11-28 00:28:14 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-04-03 09:42:53 -03:00
|
|
|
// initializing?
|
|
|
|
if (_flags.gyro_calibrated != AP_Notify::flags.gyro_calibrated) {
|
|
|
|
_flags.gyro_calibrated = AP_Notify::flags.gyro_calibrated;
|
|
|
|
play_pattern(INIT_GYRO);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if prearm check are good
|
|
|
|
if (AP_Notify::flags.pre_arm_check && !_flags.pre_arm_check) {
|
|
|
|
_flags.pre_arm_check = true;
|
|
|
|
play_pattern(PRE_ARM_GOOD);
|
|
|
|
}
|
|
|
|
|
2013-11-28 00:28:14 -04:00
|
|
|
// check if armed status has changed
|
|
|
|
if (_flags.armed != AP_Notify::flags.armed) {
|
|
|
|
_flags.armed = AP_Notify::flags.armed;
|
|
|
|
if (_flags.armed) {
|
|
|
|
// double buzz when armed
|
2014-07-25 01:05:45 -03:00
|
|
|
play_pattern(ARMING_BUZZ);
|
2022-11-19 10:30:02 -04:00
|
|
|
} else {
|
2013-11-28 00:28:14 -04:00
|
|
|
// single buzz when disarmed
|
|
|
|
play_pattern(SINGLE_BUZZ);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-28 09:28:34 -03:00
|
|
|
// check ekf bad
|
|
|
|
if (_flags.ekf_bad != AP_Notify::flags.ekf_bad) {
|
|
|
|
_flags.ekf_bad = AP_Notify::flags.ekf_bad;
|
|
|
|
if (_flags.ekf_bad) {
|
|
|
|
// ekf bad warning buzz
|
|
|
|
play_pattern(EKF_BAD);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-05-04 23:48:44 -03:00
|
|
|
// if vehicle lost was enabled, starting beep
|
|
|
|
if (AP_Notify::flags.vehicle_lost) {
|
|
|
|
play_pattern(DOUBLE_BUZZ);
|
2018-10-09 03:05:23 -03:00
|
|
|
return;
|
2016-05-04 23:48:44 -03:00
|
|
|
}
|
|
|
|
|
2013-11-28 00:28:14 -04:00
|
|
|
// if battery failsafe constantly single buzz
|
|
|
|
if (AP_Notify::flags.failsafe_battery) {
|
|
|
|
play_pattern(SINGLE_BUZZ);
|
2018-10-09 03:05:23 -03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
void Buzzer::update_playing_pattern()
|
|
|
|
{
|
|
|
|
if (_pattern == 0UL) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-11-19 10:30:02 -04:00
|
|
|
const uint32_t delta = AP_HAL::millis() - _pattern_start_time;
|
2018-10-09 03:05:23 -03:00
|
|
|
if (delta >= 3200) {
|
|
|
|
// finished playing pattern
|
|
|
|
on(false);
|
|
|
|
_pattern = 0UL;
|
|
|
|
return;
|
2013-11-28 00:28:14 -04:00
|
|
|
}
|
2018-10-09 03:05:23 -03:00
|
|
|
const uint32_t bit = delta / 100UL; // each bit is 100ms
|
|
|
|
on(_pattern & (1U<<(31-bit)));
|
2013-11-28 00:28:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// on - turns the buzzer on or off
|
|
|
|
void Buzzer::on(bool turn_on)
|
|
|
|
{
|
|
|
|
// return immediately if nothing to do
|
|
|
|
if (_flags.on == turn_on) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update state
|
|
|
|
_flags.on = turn_on;
|
|
|
|
|
|
|
|
// pull pin high or low
|
2021-11-14 20:41:33 -04:00
|
|
|
const uint8_t buzz_on = pNotify->get_buzz_level();
|
|
|
|
hal.gpio->write(_pin, _flags.on? buzz_on : !buzz_on);
|
2013-11-28 00:28:14 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// play_pattern - plays the defined buzzer pattern
|
2018-10-09 03:05:23 -03:00
|
|
|
void Buzzer::play_pattern(const uint32_t pattern)
|
2013-11-28 00:28:14 -04:00
|
|
|
{
|
2018-10-09 03:05:23 -03:00
|
|
|
_pattern = pattern;
|
|
|
|
_pattern_start_time = AP_HAL::millis();
|
2014-07-25 01:05:45 -03:00
|
|
|
}
|
2018-01-22 05:11:14 -04:00
|
|
|
|