tunes: Improve logic for interrupting tunes

src/lib/tunes already knows which tunes are interruptable, so add
return values to expose than and use than in tone_alarm.
This fixes the issue that repeating tunes cannot be interrupted without
tune_override set to true.

Signed-off-by: Alex Mikhalev <alex@corvus-robotics.com>
This commit is contained in:
Alex Mikhalev
2020-11-30 16:56:13 -05:00
committed by Kabir Mohammed
parent bd943d872c
commit 378b41af6a
4 changed files with 86 additions and 58 deletions
+5 -4
View File
@@ -78,11 +78,11 @@ void Tunes::reset(bool repeat_flag)
_tempo = _default_tempo;
}
int Tunes::set_control(const tune_control_s &tune_control)
Tunes::ControlResult Tunes::set_control(const tune_control_s &tune_control)
{
// Sanity check
if (tune_control.tune_id >= _default_tunes_size) {
return -EINVAL;
return ControlResult::InvalidTune;
}
// Accept new tune or a stop?
@@ -93,7 +93,7 @@ int Tunes::set_control(const tune_control_s &tune_control)
// Check if this exact tune is already being played back
if (tune_control.tune_id != static_cast<int>(TuneID::CUSTOM) &&
_tune == _default_tunes[tune_control.tune_id]) {
return OK; // Nothing to do
return ControlResult::AlreadyPlaying; // Nothing to do
}
// Reset repeat flag. Can jump to true again while tune is being parsed later
@@ -126,9 +126,10 @@ int Tunes::set_control(const tune_control_s &tune_control)
}
_current_tune_id = tune_control.tune_id;
return ControlResult::Success;
}
return OK;
return ControlResult::WouldInterrupt;
}
void Tunes::set_string(const char *const string, uint8_t volume)
+13 -2
View File
@@ -37,6 +37,7 @@
#pragma once
#include <errno.h>
#include <uORB/uORB.h>
#include <uORB/topics/tune_control.h>
#include "tune_definition.h"
@@ -64,6 +65,13 @@ public:
Error = -1,
};
enum class ControlResult {
Success = 0,
AlreadyPlaying = 1,
InvalidTune = -EINVAL,
WouldInterrupt = -EBUSY,
};
/**
* Constructor with the default parameters set to:
* default_tempo: TUNE_DEFAULT_TEMPO
@@ -83,9 +91,12 @@ public:
* the call to this function will be ignored, unless the override flag is set
* or the tune being already played is a repeated tune.
* @param tune_control struct containig the uORB message
* @return return -EINVAL if the default tune does not exist.
* @return return ControlResult::InvalidTune if the default tune does not exist,
* ControlResult::WouldInterrupt if tune was already playing and not interruptable,
* ControlResult::AlreadyPlaying if same tune was already playing,
* ControlResult::Success if new tune was set.
*/
int set_control(const tune_control_s &tune_control);
ControlResult set_control(const tune_control_s &tune_control);
/**
* Set tune to be played using a string.