lib/tunes: never play tunes if circuit breaker is set

This commit is contained in:
Daniel Agar 2021-10-11 14:04:26 -04:00
parent b08f20806b
commit 33954a904f
3 changed files with 38 additions and 3 deletions

View File

@ -1,6 +1,6 @@
############################################################################
#
# Copyright (c) 2017 PX4 Development Team. All rights reserved.
# Copyright (c) 2017-2021 PX4 Development Team. All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
@ -32,6 +32,9 @@
############################################################################
px4_add_library(tunes
tunes.cpp
default_tunes.cpp
tune_definition.h
tunes.cpp
tunes.h
)
target_link_libraries(tunes PRIVATE circuit_breaker)

View File

@ -38,6 +38,9 @@
#include "tunes.h"
#include <px4_platform_common/log.h>
#include <lib/circuit_breaker/circuit_breaker.h>
#include <math.h>
#include <ctype.h>
#include <errno.h>
@ -56,6 +59,10 @@ Tunes::Tunes(unsigned default_note_length, NoteMode default_note_mode,
_default_octave(default_octave),
_default_tempo(default_tempo)
{
if (circuit_breaker_enabled("CBRK_BUZZER", CBRK_BUZZER_KEY)) {
_tunes_disabled = true;
}
reset(false);
}
@ -152,6 +159,14 @@ void Tunes::set_string(const char *const string, uint8_t volume)
Tunes::Status Tunes::get_next_note(unsigned &frequency, unsigned &duration, unsigned &silence, uint8_t &volume)
{
if (_tunes_disabled) {
frequency = 0;
duration = 0;
silence = 0;
volume = 0;
return Tunes::Status::Stop;
}
Tunes::Status ret = get_next_note(frequency, duration, silence);
// Check if note should not be heard -> adjust volume to 0 to be safe.
@ -167,6 +182,13 @@ Tunes::Status Tunes::get_next_note(unsigned &frequency, unsigned &duration, unsi
Tunes::Status Tunes::get_next_note(unsigned &frequency, unsigned &duration, unsigned &silence)
{
if (_tunes_disabled) {
frequency = 0;
duration = 0;
silence = 0;
return Tunes::Status::Stop;
}
// Return the values for frequency and duration if the custom msg was received.
if (_using_custom_msg) {
_using_custom_msg = false;
@ -350,6 +372,10 @@ Tunes::Status Tunes::get_next_note(unsigned &frequency, unsigned &duration, unsi
Tunes::Status Tunes::tune_end()
{
if (_tunes_disabled) {
return Tunes::Status::Stop;
}
// Restore intial parameters.
reset(_repeat);
@ -364,6 +390,10 @@ Tunes::Status Tunes::tune_end()
Tunes::Status Tunes::tune_error()
{
if (_tunes_disabled) {
return Tunes::Status::Stop;
}
// The tune appears to be bad (unexpected EOF, bad character, etc.).
_repeat = false; // Don't loop on error.
reset(_repeat);

View File

@ -232,4 +232,6 @@ private:
uint8_t _volume = 0;
bool _using_custom_msg = false;
bool _tunes_disabled{false};
};