Skip to content

Commit 33954a9

Browse files
committed
lib/tunes: never play tunes if circuit breaker is set
1 parent b08f208 commit 33954a9

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

src/lib/tunes/CMakeLists.txt

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
############################################################################
22
#
3-
# Copyright (c) 2017 PX4 Development Team. All rights reserved.
3+
# Copyright (c) 2017-2021 PX4 Development Team. All rights reserved.
44
#
55
# Redistribution and use in source and binary forms, with or without
66
# modification, are permitted provided that the following conditions
@@ -32,6 +32,9 @@
3232
############################################################################
3333

3434
px4_add_library(tunes
35-
tunes.cpp
3635
default_tunes.cpp
37-
)
36+
tune_definition.h
37+
tunes.cpp
38+
tunes.h
39+
)
40+
target_link_libraries(tunes PRIVATE circuit_breaker)

src/lib/tunes/tunes.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
#include "tunes.h"
3939

4040
#include <px4_platform_common/log.h>
41+
42+
#include <lib/circuit_breaker/circuit_breaker.h>
43+
4144
#include <math.h>
4245
#include <ctype.h>
4346
#include <errno.h>
@@ -56,6 +59,10 @@ Tunes::Tunes(unsigned default_note_length, NoteMode default_note_mode,
5659
_default_octave(default_octave),
5760
_default_tempo(default_tempo)
5861
{
62+
if (circuit_breaker_enabled("CBRK_BUZZER", CBRK_BUZZER_KEY)) {
63+
_tunes_disabled = true;
64+
}
65+
5966
reset(false);
6067
}
6168

@@ -152,6 +159,14 @@ void Tunes::set_string(const char *const string, uint8_t volume)
152159

153160
Tunes::Status Tunes::get_next_note(unsigned &frequency, unsigned &duration, unsigned &silence, uint8_t &volume)
154161
{
162+
if (_tunes_disabled) {
163+
frequency = 0;
164+
duration = 0;
165+
silence = 0;
166+
volume = 0;
167+
return Tunes::Status::Stop;
168+
}
169+
155170
Tunes::Status ret = get_next_note(frequency, duration, silence);
156171

157172
// 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
167182

168183
Tunes::Status Tunes::get_next_note(unsigned &frequency, unsigned &duration, unsigned &silence)
169184
{
185+
if (_tunes_disabled) {
186+
frequency = 0;
187+
duration = 0;
188+
silence = 0;
189+
return Tunes::Status::Stop;
190+
}
191+
170192
// Return the values for frequency and duration if the custom msg was received.
171193
if (_using_custom_msg) {
172194
_using_custom_msg = false;
@@ -350,6 +372,10 @@ Tunes::Status Tunes::get_next_note(unsigned &frequency, unsigned &duration, unsi
350372

351373
Tunes::Status Tunes::tune_end()
352374
{
375+
if (_tunes_disabled) {
376+
return Tunes::Status::Stop;
377+
}
378+
353379
// Restore intial parameters.
354380
reset(_repeat);
355381

@@ -364,6 +390,10 @@ Tunes::Status Tunes::tune_end()
364390

365391
Tunes::Status Tunes::tune_error()
366392
{
393+
if (_tunes_disabled) {
394+
return Tunes::Status::Stop;
395+
}
396+
367397
// The tune appears to be bad (unexpected EOF, bad character, etc.).
368398
_repeat = false; // Don't loop on error.
369399
reset(_repeat);

src/lib/tunes/tunes.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,4 +232,6 @@ class Tunes
232232
uint8_t _volume = 0;
233233

234234
bool _using_custom_msg = false;
235+
236+
bool _tunes_disabled{false};
235237
};

0 commit comments

Comments
 (0)