123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- #include "Buzzer.h"
- #include <AP_HAL/AP_HAL.h>
- #include "AP_Notify.h"
- #ifndef HAL_BUZZER_ON
- #if !defined(HAL_BUZZER_PIN)
- #define HAL_BUZZER_ON (pNotify->get_buzz_level())
- #define HAL_BUZZER_OFF (!pNotify->get_buzz_level())
- #else
- #define HAL_BUZZER_ON 1
- #define HAL_BUZZER_OFF 0
- #endif
- #endif
- extern const AP_HAL::HAL& hal;
- bool Buzzer::init()
- {
- if (pNotify->buzzer_enabled() == false) {
- return false;
- }
- #if defined(HAL_BUZZER_PIN)
- _pin = HAL_BUZZER_PIN;
- #else
- _pin = pNotify->get_buzz_pin();
- #endif
- if(!_pin) return false;
-
- hal.gpio->pinMode(_pin, HAL_GPIO_OUTPUT);
- on(false);
-
-
- _flags.armed = AP_Notify::flags.armed;
- _flags.failsafe_battery = AP_Notify::flags.failsafe_battery;
- return true;
- }
- void Buzzer::update()
- {
- update_pattern_to_play();
- update_playing_pattern();
- }
- void Buzzer::update_pattern_to_play()
- {
-
- if (AP_Notify::events.arming_failed) {
-
- play_pattern(SINGLE_BUZZ);
- return;
- }
- if (AP_HAL::millis() - _pattern_start_time < _pattern_start_interval_time_ms) {
-
- return;
- }
-
- if (_flags.armed != AP_Notify::flags.armed) {
- _flags.armed = AP_Notify::flags.armed;
- if (_flags.armed) {
-
- play_pattern(ARMING_BUZZ);
- }else{
-
- play_pattern(SINGLE_BUZZ);
- }
- return;
- }
-
- if (_flags.ekf_bad != AP_Notify::flags.ekf_bad) {
- _flags.ekf_bad = AP_Notify::flags.ekf_bad;
- if (_flags.ekf_bad) {
-
- play_pattern(EKF_BAD);
- }
- return;
- }
-
- if (AP_Notify::flags.vehicle_lost) {
- play_pattern(DOUBLE_BUZZ);
- return;
- }
-
- if (AP_Notify::flags.failsafe_battery) {
- play_pattern(SINGLE_BUZZ);
- return;
- }
- }
- void Buzzer::update_playing_pattern()
- {
- if (_pattern == 0UL) {
- return;
- }
- const uint32_t now = AP_HAL::millis();
- const uint32_t delta = now - _pattern_start_time;
- if (delta >= 3200) {
-
- on(false);
- _pattern = 0UL;
- return;
- }
- const uint32_t bit = delta / 100UL;
- on(_pattern & (1U<<(31-bit)));
- }
- void Buzzer::on(bool turn_on)
- {
-
- if (_flags.on == turn_on) {
- return;
- }
-
- _flags.on = turn_on;
-
- hal.gpio->write(_pin, _flags.on? HAL_BUZZER_ON : HAL_BUZZER_OFF);
- }
- void Buzzer::play_pattern(const uint32_t pattern)
- {
- _pattern = pattern;
- _pattern_start_time = AP_HAL::millis();
- }
|