- some bug fixes n race and sound player

- added cancelRace command
This commit is contained in:
Dorian Zedler 2020-10-02 15:20:11 +02:00
parent 41818ff308
commit d1f132d0b0
Signed by: dorian
GPG key ID: D3B255CB8BC7CD37
5 changed files with 47 additions and 32 deletions

View file

@ -79,6 +79,7 @@ public:
StartRaceCommand = 1000, StartRaceCommand = 1000,
StopRaceCommand = 1001, StopRaceCommand = 1001,
ResetRaceCommand = 1002, ResetRaceCommand = 1002,
CancelRaceCommand = 1003,
GetRaceStateCommand = 2000, GetRaceStateCommand = 2000,
GetNextStartActionCommand = 2005, GetNextStartActionCommand = 2005,

View file

@ -106,21 +106,21 @@ public slots:
* or if if should wait until the start sequence is over and quit after that (false) * or if if should wait until the start sequence is over and quit after that (false)
* \return 200: OK; 904: state not matching * \return 200: OK; 904: state not matching
*/ */
int start(bool asyncronous = true); virtual ScStw::StatusCode start(bool asyncronous = true);
/*! /*!
* \brief Function to stop the currently running race * \brief Function to stop the currently running race
* *
* \return 200: OK; 904: state not matching * \return 200: OK; 904: state not matching
*/ */
virtual int stop(); virtual ScStw::StatusCode stop();
/*! /*!
* \brief Function to reset a stopped race * \brief Function to reset a stopped race
* \return * \return
*/ */
virtual int reset(); virtual ScStw::StatusCode reset();
virtual int cancel(); virtual ScStw::StatusCode cancel();
// setters // setters
bool writeStartSoundSetting(ScStwSoundPlayer::StartSound sound, bool enabled, int delay); bool writeStartSoundSetting(ScStwSoundPlayer::StartSound sound, bool enabled, int delay);

View file

@ -42,9 +42,10 @@ public:
explicit ScStwSoundPlayer(QObject *parent = nullptr); explicit ScStwSoundPlayer(QObject *parent = nullptr);
enum StartSound { enum StartSound {
Start, AtYourMarks,
Ready, Ready,
AtYourMarks Start,
FalseStart
}; };
private: private:
@ -55,7 +56,7 @@ private:
* 2: StartSound * 2: StartSound
* 3: FalseStartSound * 3: FalseStartSound
*/ */
QMap<int, QVariantMap> soundFiles; QMap<StartSound, QVariantMap> soundFiles;
/*! /*!
* \brief The sound effect object * \brief The sound effect object
@ -75,7 +76,7 @@ private:
/*! /*!
* \brief The action that is currently played * \brief The action that is currently played
*/ */
int currentlyPlayingAction; StartSound currentlyPlayingSound;
/*! /*!
* \brief Holds the time the playback started at * \brief Holds the time the playback started at
@ -91,7 +92,7 @@ public slots:
* \param timeOfStop The time the playback actually started (msecs since epoch) * \param timeOfStop The time the playback actually started (msecs since epoch)
* \return true if the playback was successfully started, false otherwise * \return true if the playback was successfully started, false otherwise
*/ */
bool play(int action, double volume, double *timeOfStart = nullptr); bool play(StartSound sound, double volume, double *timeOfStart = nullptr);
/*! /*!
* \brief Function to wait for the playback to finish * \brief Function to wait for the playback to finish

View file

@ -206,6 +206,7 @@ bool ScStwRace::playSoundsAndStartTimers() {
return true; return true;
// The check if all timers are ready has already happened at this point // The check if all timers are ready has already happened at this point
qDebug() << "now playing at marks sound";
if(!this->doDelayAndSoundOfCurrentStartState()) if(!this->doDelayAndSoundOfCurrentStartState())
return false; return false;
@ -213,6 +214,7 @@ bool ScStwRace::playSoundsAndStartTimers() {
if(!this->isStarting()) if(!this->isStarting())
return false; return false;
qDebug() << "Now in waiting state";
this->setState(WAITING); this->setState(WAITING);
// do climber readiness tests // do climber readiness tests
@ -220,7 +222,7 @@ bool ScStwRace::playSoundsAndStartTimers() {
// wait until both climbers are ready // wait until both climbers are ready
// if the automatic ready tone is enabled, wait for the climbers to become ready // if the automatic ready tone is enabled, wait for the climbers to become ready
if(this->startSoundSettings.contains(ScStwSoundPlayer::Ready) && this->startSoundSettings[ScStwSoundPlayer::Ready]["Enabled"].toBool()) { if(this->competitionMode && this->startSoundSettings.contains(ScStwSoundPlayer::Ready) && this->startSoundSettings[ScStwSoundPlayer::Ready]["Enabled"].toBool()) {
qDebug() << "[RACE][INFO] Now waiting for climbers"; qDebug() << "[RACE][INFO] Now waiting for climbers";
@ -245,11 +247,15 @@ bool ScStwRace::playSoundsAndStartTimers() {
else { else {
this->startDelayTimer->stop(); this->startDelayTimer->stop();
this->startDelayTimer->start(); this->startDelayTimer->start();
timerTriggered = true;
} }
emit this->currentStartDelayChanged(); emit this->currentStartDelayChanged();
qDebug() << "entering wait loop...";
int loopExitCode = this->startWaitLoop->exec(); int loopExitCode = this->startWaitLoop->exec();
qDebug() << "wait loop exited with code " << loopExitCode;
switch (loopExitCode) { switch (loopExitCode) {
case LoopAutomaticExit: case LoopAutomaticExit:
@ -262,6 +268,8 @@ bool ScStwRace::playSoundsAndStartTimers() {
return false; return false;
} }
qDebug() << "At end of loop: remaining time: " << this->startDelayTimer->remainingTime() << " timer triggered: " << timerTriggered << " ready for next state: " << this->isReadyForNextState();
} while(this->startDelayTimer->remainingTime() > 0 || !timerTriggered || !this->isReadyForNextState()); } while(this->startDelayTimer->remainingTime() > 0 || !timerTriggered || !this->isReadyForNextState());
qDebug() << "[RACE][DEBUG] Initial wait finished"; qDebug() << "[RACE][DEBUG] Initial wait finished";
@ -276,7 +284,7 @@ bool ScStwRace::playSoundsAndStartTimers() {
if(!this->soundPlayer->play(ScStwSoundPlayer::Ready, this->soundVolume)) if(!this->soundPlayer->play(ScStwSoundPlayer::Ready, this->soundVolume))
return false; return false;
} }
else { else if(this->competitionMode) {
// wait for climbers and manual start // wait for climbers and manual start
int loopExitCode; int loopExitCode;
do { do {
@ -287,11 +295,19 @@ bool ScStwRace::playSoundsAndStartTimers() {
} while(loopExitCode != LoopManualExit || !this->isReadyForNextState()); } while(loopExitCode != LoopManualExit || !this->isReadyForNextState());
} }
else {
qDebug() << "now playing ready sound";
if(!this->doDelayAndSoundOfCurrentStartState())
return false;
}
qDebug() << "now in starting state";
// enter starting state // enter starting state
this->setState(STARTING); this->setState(STARTING);
// play start tone // play start tone
qDebug() << "now playing start sound";
double timeOfSoundPlaybackStart; double timeOfSoundPlaybackStart;
this->doDelayAndSoundOfCurrentStartState(&timeOfSoundPlaybackStart); this->doDelayAndSoundOfCurrentStartState(&timeOfSoundPlaybackStart);
@ -343,8 +359,6 @@ bool ScStwRace::doDelayAndSoundOfCurrentStartState(double *timeOfSoundPlaybackSt
} }
if(this->startSoundSettings.contains(sound) && this->startSoundSettings[sound]["Enabled"].toBool()) { if(this->startSoundSettings.contains(sound) && this->startSoundSettings[sound]["Enabled"].toBool()) {
if(sound != ScStwSoundPlayer::Start && this->startSoundSettings[sound]["Delay"].toInt() > 0) { if(sound != ScStwSoundPlayer::Start && this->startSoundSettings[sound]["Delay"].toInt() > 0) {
// perform the delay before the start // perform the delay before the start
@ -426,6 +440,10 @@ void ScStwRace::refreshTimerStates() {
// ------------------------ // ------------------------
bool ScStwRace::isReadyForNextState() { bool ScStwRace::isReadyForNextState() {
if(!this->competitionMode) {
return true;
}
switch (this->state) { switch (this->state) {
case IDLE: { case IDLE: {
foreach (ScStwTimer *timer, this->timers) { foreach (ScStwTimer *timer, this->timers) {
@ -434,11 +452,6 @@ bool ScStwRace::isReadyForNextState() {
if(timer->getReadyState() == ScStwTimer::ExtensionIsNotConnected || timer->getReadyState() == ScStwTimer::ExtensionBatteryNotFine) { if(timer->getReadyState() == ScStwTimer::ExtensionIsNotConnected || timer->getReadyState() == ScStwTimer::ExtensionBatteryNotFine) {
if(!this->competitionMode) {
timer->setDisabled(true);
continue;
}
qDebug() << "Timer ready state is: " << timer->getReadyState(); qDebug() << "Timer ready state is: " << timer->getReadyState();
timer->technicalIncident(); timer->technicalIncident();

View file

@ -23,10 +23,10 @@ ScStwSoundPlayer::ScStwSoundPlayer(QObject *parent) : QObject(parent)
this->waitLoop = new QEventLoop(this); this->waitLoop = new QEventLoop(this);
this->waitTimer = new QTimer(this); this->waitTimer = new QTimer(this);
this->soundFiles.insert(0, {{"path","qrc:/sound/AtYourMarksSound.wav"}, {"duration", 1000}}); this->soundFiles.insert(AtYourMarks, {{"path","qrc:/sound/AtYourMarksSound.wav"}, {"duration", 1000}});
this->soundFiles.insert(1, {{"path","qrc:/sound/ReadySound.wav"}, {"duration", 570}}); this->soundFiles.insert(Ready, {{"path","qrc:/sound/ReadySound.wav"}, {"duration", 570}});
this->soundFiles.insert(2, {{"path","qrc:/sound/StartsignalSoundExtended.wav"}, {"duration", 3200}}); this->soundFiles.insert(Start, {{"path","qrc:/sound/StartsignalSoundExtended.wav"}, {"duration", 3200}});
this->soundFiles.insert(3, {{"path","qrc:/sound/FalseStartSound.wav"}, {"duration", 2000}}); this->soundFiles.insert(FalseStart, {{"path","qrc:/sound/FalseStartSound.wav"}, {"duration", 2000}});
this->soundEffect = new QSoundEffect(this); this->soundEffect = new QSoundEffect(this);
this->soundEffect->setLoopCount(1); this->soundEffect->setLoopCount(1);
@ -35,11 +35,11 @@ ScStwSoundPlayer::ScStwSoundPlayer(QObject *parent) : QObject(parent)
connect(this->soundEffect, &QSoundEffect::playingChanged, this->waitLoop, &QEventLoop::quit); connect(this->soundEffect, &QSoundEffect::playingChanged, this->waitLoop, &QEventLoop::quit);
} }
bool ScStwSoundPlayer::play(int action, double volume, double *timeOfStart) { bool ScStwSoundPlayer::play(ScStwSoundPlayer::StartSound sound, double volume, double *timeOfStart) {
if(!this->soundFiles.contains(action)) if(!this->soundFiles.contains(sound))
return false; return false;
if(action > 2 || action < 0) if(sound > Start || sound < AtYourMarks)
return false; return false;
// stop playback // stop playback
@ -47,13 +47,13 @@ bool ScStwSoundPlayer::play(int action, double volume, double *timeOfStart) {
this->soundEffect->stop(); this->soundEffect->stop();
// update currently playing action // update currently playing action
this->currentlyPlayingAction = action; this->currentlyPlayingSound = sound;
// update volume // update volume
this->soundEffect->setVolume(volume); this->soundEffect->setVolume(volume);
// load // load
this->soundEffect->setSource(this->soundFiles[action]["path"].toString()); this->soundEffect->setSource(this->soundFiles[sound]["path"].toString());
// wait for the effect to load // wait for the effect to load
QEventLoop loop; QEventLoop loop;
@ -76,7 +76,7 @@ bool ScStwSoundPlayer::play(int action, double volume, double *timeOfStart) {
*timeOfStart = this->playingStartedAt; *timeOfStart = this->playingStartedAt;
} }
if(action < 2) if(sound < 2)
return this->waitForSoundFinish(); return this->waitForSoundFinish();
return true; return true;
@ -90,14 +90,14 @@ bool ScStwSoundPlayer::cancel(double volume) {
this->soundEffect->stop(); this->soundEffect->stop();
this->waitLoop->quit(); this->waitLoop->quit();
if(this->currentlyPlayingAction != 2) if(this->currentlyPlayingSound != 2)
return true; return true;
// update volume // update volume
this->soundEffect->setVolume(volume); this->soundEffect->setVolume(volume);
// load // load
this->soundEffect->setSource(this->soundFiles[3]["path"].toString()); this->soundEffect->setSource(this->soundFiles[FalseStart]["path"].toString());
// play // play
this->soundEffect->play(); this->soundEffect->play();
@ -114,7 +114,7 @@ bool ScStwSoundPlayer::waitForSoundFinish(double *timeOfStop) {
// wait until the sound is actually over // wait until the sound is actually over
// the timeOffset is the buffer time before the audio started! // the timeOffset is the buffer time before the audio started!
int timeOffset = this->soundFiles[this->currentlyPlayingAction]["duration"].toDouble() - (QDateTime::currentMSecsSinceEpoch() - playingStartedAt); int timeOffset = this->soundFiles[this->currentlyPlayingSound]["duration"].toDouble() - (QDateTime::currentMSecsSinceEpoch() - playingStartedAt);
if(timeOffset > 0) { if(timeOffset > 0) {
QTimer timer; QTimer timer;
@ -124,7 +124,7 @@ bool ScStwSoundPlayer::waitForSoundFinish(double *timeOfStop) {
// calculate the point in time where the sound playback actually ended // calculate the point in time where the sound playback actually ended
if(timeOfStop != nullptr) { if(timeOfStop != nullptr) {
int latency = this->playingStartedAt + this->soundFiles[this->currentlyPlayingAction]["duration"].toDouble(); int latency = this->playingStartedAt + this->soundFiles[this->currentlyPlayingSound]["duration"].toDouble();
*timeOfStop = QDateTime::currentMSecsSinceEpoch() - latency; *timeOfStop = QDateTime::currentMSecsSinceEpoch() - latency;
} }