From 6ce839ea1e0dc2ec8873937ec22d2305065cbb12 Mon Sep 17 00:00:00 2001
From: Alessandro Simovic <alessandro@yuneecresearch.com>
Date: Wed, 24 Jan 2018 08:25:34 +0100
Subject: [PATCH] libtunes: added tone strength as state and output to libtunes

Since the tune library also contains logic how tunes can be overriden,
a user of the tunes library cannot know the strength of the current tune
without replicating some logic. Easy solution is to add strength to the
output of Tunes::get_next_tune().
---
 src/lib/tunes/tunes.cpp                      | 24 ++++++++++++++++++--
 src/lib/tunes/tunes.h                        | 13 +++++++++++
 src/systemcmds/tune_control/tune_control.cpp | 10 ++++----
 3 files changed, 41 insertions(+), 6 deletions(-)

diff --git a/src/lib/tunes/tunes.cpp b/src/lib/tunes/tunes.cpp
index 91b790a607..a1548b4634 100644
--- a/src/lib/tunes/tunes.cpp
+++ b/src/lib/tunes/tunes.cpp
@@ -96,6 +96,10 @@ int Tunes::set_control(const tune_control_s &tune_control)
 		_repeat = false;
 		_using_custom_msg = false;
 		_tune = nullptr;
+
+		// New tune will be played. This is the place to store the strength
+		// which will remain valid for the entire tune, unless interrupted.
+		_strength = (unsigned)tune_control.strength;
 	}
 
 	if (tune_control.tune_id < _default_tunes_size) {
@@ -151,7 +155,24 @@ void Tunes::set_string(const char *const string)
 	}
 }
 
-int Tunes::get_next_tune(unsigned &frequency, unsigned &duration, unsigned &silence)
+int Tunes::get_next_tune(unsigned &frequency, unsigned &duration,
+			 unsigned &silence, unsigned &strength)
+{
+	int ret = get_next_tune(frequency, duration, silence);
+
+	// Check if note should not be heard -> adjust strength to 0 to be safe
+	if (frequency == 0 || duration == 0) {
+		strength = 0;
+
+	} else {
+		strength = _strength;
+	}
+
+	return ret;
+}
+
+int Tunes::get_next_tune(unsigned &frequency, unsigned &duration,
+			 unsigned &silence)
 {
 	// Return the vaules for frequency and duration if the custom msg was received
 	if (_using_custom_msg) {
@@ -331,7 +352,6 @@ int Tunes::get_next_tune(unsigned &frequency, unsigned &duration, unsigned &sile
 
 	// compute the note frequency
 	frequency = note_to_frequency(note);
-
 	return TUNE_CONTINUE;
 
 	// tune looks bad (unexpected EOF, bad character, etc.)
diff --git a/src/lib/tunes/tunes.h b/src/lib/tunes/tunes.h
index 7fd843958c..7b4b5507f0 100644
--- a/src/lib/tunes/tunes.h
+++ b/src/lib/tunes/tunes.h
@@ -105,6 +105,18 @@ public:
 	 */
 	int get_next_tune(unsigned &frequency, unsigned &duration, unsigned &silence);
 
+	/**
+	 * Get next note in the current tune, which has been provided by either
+	 * set_control or play_string
+	 * @param  frequency return frequency value (Hz)
+	 * @param  duration  return duration of the tone (us)
+	 * @param  silence   return silence duration (us)
+	 * @param  strength  return the strength of the note (between 0-100)
+	 * @return           -1 for error, 0 for play one tone and 1 for continue a sequence
+	 */
+	int get_next_tune(unsigned &frequency, unsigned &duration, unsigned &silence,
+			  unsigned &strength);
+
 	/**
 	 *  Get the number of default tunes. This is useful for when a tune is
 	 *  requested via its tune ID.
@@ -136,6 +148,7 @@ private:
 	unsigned _frequency;
 	unsigned _duration;
 	unsigned _silence;
+	unsigned _strength;
 	bool _using_custom_msg = false;
 
 	/**
diff --git a/src/systemcmds/tune_control/tune_control.cpp b/src/systemcmds/tune_control/tune_control.cpp
index 592ae1f401..d5f35fa3b4 100644
--- a/src/systemcmds/tune_control/tune_control.cpp
+++ b/src/systemcmds/tune_control/tune_control.cpp
@@ -170,7 +170,7 @@ tune_control_main(int argc, char *argv[])
 		return 1;
 	}
 
-	unsigned frequency, duration, silence;
+	unsigned frequency, duration, silence, strength;
 	int exit_counter = 0;
 
 	if (!strcmp(argv[myoptind], "play")) {
@@ -178,11 +178,12 @@ tune_control_main(int argc, char *argv[])
 			PX4_INFO("Start playback...");
 			tunes.set_string(tune_string);
 
-			while (tunes.get_next_tune(frequency, duration, silence) > 0) {
+			while (tunes.get_next_tune(frequency, duration, silence, strength) > 0) {
 				tune_control.tune_id = 0;
 				tune_control.frequency = (uint16_t)frequency;
 				tune_control.duration = (uint32_t)duration;
 				tune_control.silence = (uint32_t)silence;
+				tune_control.strength = (uint32_t)strength;
 				publish_tune_control(tune_control);
 				usleep(duration + silence);
 				exit_counter++;
@@ -207,8 +208,9 @@ tune_control_main(int argc, char *argv[])
 	} else if (!strcmp(argv[myoptind], "libtest")) {
 		tunes.set_control(tune_control);
 
-		while (tunes.get_next_tune(frequency, duration, silence) > 0) {
-			PX4_INFO("frequency: %d, duration %d, silence %d", frequency, duration, silence);
+		while (tunes.get_next_tune(frequency, duration, silence, strength) > 0) {
+			PX4_INFO("frequency: %d, duration %d, silence %d, strength%d",
+				 frequency, duration, silence, strength);
 			usleep(500000);
 			exit_counter++;
 
-- 
GitLab