diff --git a/src/lib/tunes/CMakeLists.txt b/src/lib/tunes/CMakeLists.txt index 24b8557cf30..834747b90e5 100644 --- a/src/lib/tunes/CMakeLists.txt +++ b/src/lib/tunes/CMakeLists.txt @@ -38,3 +38,5 @@ px4_add_library(tunes tunes.h ) target_link_libraries(tunes PRIVATE circuit_breaker) + +px4_add_functional_gtest(SRC TunesTest.cpp LINKLIBS tunes) diff --git a/src/lib/tunes/TunesTest.cpp b/src/lib/tunes/TunesTest.cpp new file mode 100644 index 00000000000..49002dddfa6 --- /dev/null +++ b/src/lib/tunes/TunesTest.cpp @@ -0,0 +1,108 @@ +/**************************************************************************** + * + * Copyright (c) 2026 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 + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name PX4 nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/** + * @file TunesTest.cpp + * + * Every Tunes::Status::Continue must carry a defined frequency, duration and silence, because the + * consumers publish them as they are and schedule the next note from them. + */ + +#include + +#include "tunes.h" + +static constexpr unsigned kSentinel = 0xDEADBEEF; + +TEST(TunesTest, RestWithN0HasDefinedNoteFields) +{ + Tunes tunes; + tunes.set_string("N0", tune_control_s::VOLUME_LEVEL_DEFAULT); + + unsigned frequency = kSentinel; + unsigned duration = kSentinel; + unsigned silence = kSentinel; + uint8_t volume = 0xAA; + + EXPECT_EQ(tunes.get_next_note(frequency, duration, silence, volume), Tunes::Status::Continue); + EXPECT_EQ(frequency, 0u); + EXPECT_EQ(duration, 0u); + EXPECT_GT(silence, 0u); + EXPECT_EQ(volume, 0); +} + +TEST(TunesTest, RepeatingTuneRestartsWithDefinedNoteFields) +{ + Tunes tunes; + tunes.set_string("MBC", tune_control_s::VOLUME_LEVEL_DEFAULT); + + unsigned frequency = kSentinel; + unsigned duration = kSentinel; + unsigned silence = kSentinel; + uint8_t volume = 0xAA; + + // The note C + EXPECT_EQ(tunes.get_next_note(frequency, duration, silence, volume), Tunes::Status::Continue); + EXPECT_GT(frequency, 0u); + EXPECT_GT(duration, 0u); + + // End of the string: the tune repeats, which is reported as a zero-length rest + frequency = kSentinel; + duration = kSentinel; + silence = kSentinel; + volume = 0xAA; + EXPECT_EQ(tunes.get_next_note(frequency, duration, silence, volume), Tunes::Status::Continue); + EXPECT_EQ(frequency, 0u); + EXPECT_EQ(duration, 0u); + EXPECT_EQ(silence, 0u); + EXPECT_EQ(volume, 0); + + // The note C again + EXPECT_EQ(tunes.get_next_note(frequency, duration, silence, volume), Tunes::Status::Continue); + EXPECT_GT(frequency, 0u); + EXPECT_GT(duration, 0u); +} + +TEST(TunesTest, NonRepeatingTuneStops) +{ + Tunes tunes; + tunes.set_string("C", tune_control_s::VOLUME_LEVEL_DEFAULT); + + unsigned frequency = 0; + unsigned duration = 0; + unsigned silence = 0; + uint8_t volume = 0; + + EXPECT_EQ(tunes.get_next_note(frequency, duration, silence, volume), Tunes::Status::Continue); + EXPECT_EQ(tunes.get_next_note(frequency, duration, silence, volume), Tunes::Status::Stop); +} diff --git a/src/lib/tunes/tunes.cpp b/src/lib/tunes/tunes.cpp index 1516a93f351..a4ea3d891d6 100644 --- a/src/lib/tunes/tunes.cpp +++ b/src/lib/tunes/tunes.cpp @@ -212,6 +212,9 @@ Tunes::Status Tunes::get_next_note(unsigned &frequency, unsigned &duration, unsi int c = next_char(); if (c == 0) { + // A repeating tune continues with a zero-length rest, so leave nothing undefined + frequency = 0; + duration = 0; silence = 0; return tune_end(); } @@ -324,6 +327,8 @@ Tunes::Status Tunes::get_next_note(unsigned &frequency, unsigned &duration, unsi if (note == 0) { // This is a rest - pause for the current note length. + frequency = 0; + duration = 0; silence = rest_duration(_note_length, next_dots()); return Tunes::Status::Continue; }