mirror of
https://github.com/PX4/PX4-Autopilot.git
synced 2026-05-20 03:13:44 +08:00
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().
This commit is contained in:
committed by
Beat Küng
parent
acdc81ea0a
commit
6ce839ea1e
+22
-2
@@ -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.)
|
||||
|
||||
@@ -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;
|
||||
|
||||
/**
|
||||
|
||||
@@ -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++;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user