From 1ae207a2359afcce71a6c55f5bfbd9417671092f Mon Sep 17 00:00:00 2001 From: Dorian Zedler Date: Sun, 19 Apr 2020 13:09:24 +0200 Subject: [PATCH] some fixes and additions for qml --- ScStwLibraries/headers/ScStw.hpp | 4 +-- ScStwLibraries/headers/scstwclient.h | 10 ++++-- ScStwLibraries/headers/scstwrace.h | 23 +++++++++++++- .../headers/scstwremotemonitorrace.h | 2 ++ ScStwLibraries/headers/scstwtimer.h | 1 + ScStwLibraries/sources/scstwclient.cpp | 12 ++++--- ScStwLibraries/sources/scstwrace.cpp | 10 ++++-- .../sources/scstwremotemonitorrace.cpp | 31 +++++++++++++++++-- 8 files changed, 79 insertions(+), 14 deletions(-) diff --git a/ScStwLibraries/headers/ScStw.hpp b/ScStwLibraries/headers/ScStw.hpp index 62a8d89..f3590b9 100644 --- a/ScStwLibraries/headers/ScStw.hpp +++ b/ScStwLibraries/headers/ScStw.hpp @@ -81,7 +81,7 @@ public: /*! * \brief The ErrorCode enum contains all error codes that can occur when sending a command to the basestation */ - enum ErrorCode { + enum StatusCode { Success = 200, Error = 900, @@ -89,7 +89,7 @@ public: TimeoutError = 911, SettingNotAccessibleError = 901 }; - Q_ENUM(ErrorCode) + Q_ENUM(ScStw::StatusCode) /*! * \brief SOCKET_MESSAGE_START_KEY contains the key, a message is supposed to start with diff --git a/ScStwLibraries/headers/scstwclient.h b/ScStwLibraries/headers/scstwclient.h index bd4686b..67372c9 100644 --- a/ScStwLibraries/headers/scstwclient.h +++ b/ScStwLibraries/headers/scstwclient.h @@ -135,7 +135,13 @@ public slots: */ bool isFirmwareUpToDate(); - /*! helper functions */ + // helper functions + + /*! + * \brief Function to pair all extensions that are currently connected via USB to the base station + * \return whether the pairing was successfull + */ + bool pairConnectedUsbExtensions(); /*! * \brief Function to write a setting on the base station @@ -143,7 +149,7 @@ public slots: * \param value the value to write to * \return the status code returned by the command */ - ScStw::ErrorCode writeRemoteSetting(ScStw::BaseStationSetting key, QString value); + ScStw::StatusCode writeRemoteSetting(ScStw::BaseStationSetting key, QString value); /*! * \brief Function to read a setting on the base station diff --git a/ScStwLibraries/headers/scstwrace.h b/ScStwLibraries/headers/scstwrace.h index 2687b0d..0393eec 100644 --- a/ScStwLibraries/headers/scstwrace.h +++ b/ScStwLibraries/headers/scstwrace.h @@ -8,12 +8,33 @@ #include "scstwtimer.h" #include "scstwsoundplayer.h" +/*! + * \brief The ScStwRace class can be used to measure timings of climbing races with multiple lanes at once. + * + * The ScStwRace is a container to manage multiple timers at a time and introduces a propper start sequence + * with start commands ('At your Marks' and 'Ready') and the official IFSC start signal. + * + * ## Basic usage: + * + * \code + * ScStwRace race; + * + * // add two timers + * race.addTimer(new ScStwTimer()); + * race.addTimer(new ScStwTimer()); + * + * // start a race + * race.start(); + * \endcode + * + */ class ScStwRace : public QObject { Q_OBJECT Q_PROPERTY(RaceState state READ getState NOTIFY stateChanged) Q_PROPERTY(QVariantList timers READ getTimerDetailList NOTIFY timersChanged) Q_PROPERTY(QVariantList nextStartActionDetails READ getNextStartActionDetails NOTIFY nextStartActionDetailsChanged) + public: explicit ScStwRace(QObject *parent = nullptr); @@ -59,7 +80,7 @@ private: public slots: - int start(); + int start(bool asyncronous = true); int stop(); int reset(); void cancelStart(bool falseStart); diff --git a/ScStwLibraries/headers/scstwremotemonitorrace.h b/ScStwLibraries/headers/scstwremotemonitorrace.h index e031694..a721dfb 100644 --- a/ScStwLibraries/headers/scstwremotemonitorrace.h +++ b/ScStwLibraries/headers/scstwremotemonitorrace.h @@ -8,6 +8,7 @@ class ScStwRemoteMonitorRace : public ScStwRace { Q_OBJECT + Q_PROPERTY(QVariantList nextStartActionDetails READ getNextStartActionDetails NOTIFY nextStartActionDetailsChanged) public: ScStwRemoteMonitorRace(ScStwClient *monitorClient, QObject *parent = nullptr); @@ -24,6 +25,7 @@ public slots: int stop(); int reset(); bool addTimer(ScStwTimer *timer); + QVariantList getNextStartActionDetails(); private slots: void handleClientStateChanged(); diff --git a/ScStwLibraries/headers/scstwtimer.h b/ScStwLibraries/headers/scstwtimer.h index 1ee9141..5357e2b 100644 --- a/ScStwLibraries/headers/scstwtimer.h +++ b/ScStwLibraries/headers/scstwtimer.h @@ -14,6 +14,7 @@ * It is recommended to use it in combination with the ScStwRace class. * * ## When using standalone: + * * \code{.cpp} * ScStwTimer timer; * // start the timer diff --git a/ScStwLibraries/sources/scstwclient.cpp b/ScStwLibraries/sources/scstwclient.cpp index ae8007e..69443a1 100644 --- a/ScStwLibraries/sources/scstwclient.cpp +++ b/ScStwLibraries/sources/scstwclient.cpp @@ -397,8 +397,6 @@ bool ScStwClient::isFirmwareUpToDate() { int newFirmwareVersionMinor = newFirmwareVersion.split(".")[1].toInt(); int newFirmwareVersionPatch = newFirmwareVersion.split(".")[2].toInt(); - qDebug() << "App firmware version is: " << newFirmwareVersion; - QString currentFirmwareVersion = this->firmwareVersion; int currentFirmwareVersionMajor = currentFirmwareVersion.split(".")[0].toInt(); int currentFirmwareVersionMinor = currentFirmwareVersion.split(".")[1].toInt(); @@ -411,11 +409,17 @@ bool ScStwClient::isFirmwareUpToDate() { // --- helper functions --- // ------------------------ -ScStw::ErrorCode ScStwClient::writeRemoteSetting(ScStw::BaseStationSetting key, QString value) { +bool ScStwClient::pairConnectedUsbExtensions() { + QVariantMap ret = this->sendCommand(5002, "", 10000); + qDebug() << ret; + return ScStw::StatusCode(ret["status"].toInt()) == ScStw::Success; +} + +ScStw::StatusCode ScStwClient::writeRemoteSetting(ScStw::BaseStationSetting key, QString value) { QJsonArray requestData; requestData.append(key); requestData.append(value); - return ScStw::ErrorCode(this->sendCommand(3000, requestData)["status"].toInt()); + return ScStw::StatusCode(this->sendCommand(3000, requestData)["status"].toInt()); } QString ScStwClient::readRemoteSetting(ScStw::BaseStationSetting key) { diff --git a/ScStwLibraries/sources/scstwrace.cpp b/ScStwLibraries/sources/scstwrace.cpp index 89f0276..6064131 100644 --- a/ScStwLibraries/sources/scstwrace.cpp +++ b/ScStwLibraries/sources/scstwrace.cpp @@ -33,7 +33,7 @@ ScStwRace::ScStwRace(QObject *parent) : QObject(parent) * @brief ScStwRace::startRace * @return {int} 200: OK; 904: MAIN state not matching */ -int ScStwRace::start() { +int ScStwRace::start(bool asyncronous) { if(this->state != IDLE) { return 904; } @@ -42,7 +42,11 @@ int ScStwRace::start() { this->setState(STARTING); - this->playSoundsAndStartTimers(None); + if(asyncronous) { + QTimer::singleShot(1, [=](){this->playSoundsAndStartTimers(None);}); + } + else + this->playSoundsAndStartTimers(None); return 200; } @@ -245,7 +249,7 @@ bool ScStwRace::playSoundsAndStartTimers(StartAction thisAction) { */ void ScStwRace::setState(RaceState newState) { if(newState != this->state) { - qDebug() << "+ [INFO][MAIN] state changed: " << newState; + qDebug() << "[INFO][RACE] state changed: " << newState; this->state = newState; emit this->stateChanged(newState); diff --git a/ScStwLibraries/sources/scstwremotemonitorrace.cpp b/ScStwLibraries/sources/scstwremotemonitorrace.cpp index 5308291..e21cacf 100644 --- a/ScStwLibraries/sources/scstwremotemonitorrace.cpp +++ b/ScStwLibraries/sources/scstwremotemonitorrace.cpp @@ -39,6 +39,7 @@ int ScStwRemoteMonitorRace::start() { } void ScStwRemoteMonitorRace::cancelStart() { + this->stop(); return; } @@ -107,6 +108,8 @@ void ScStwRemoteMonitorRace::handleClientStateChanged() { case ScStwClient::CONNECTED: break; default: + this->timers.clear(); + this->setState(IDLE); break; } } @@ -120,7 +123,7 @@ void ScStwRemoteMonitorRace::handleClientStateChanged() { * @param data */ void ScStwRemoteMonitorRace::handleBaseStationSignal(ScStw::SignalKey key, QVariant data) { - qDebug() << "got signal: " << data; + //qDebug() << "got signal: " << data; switch (key) { case ScStw::RaceStateChanged: { @@ -166,7 +169,10 @@ bool ScStwRemoteMonitorRace::refreshRemoteTimers(QVariantList remoteTimers) { foreach(QVariant remoteTimer, remoteTimers){ // create a local timer for each remote timer - this->timers.append(new ScStwTimer(this, true)); + ScStwTimer * timer = new ScStwTimer(this, true); + this->timers.append(timer); + connect(timer, &ScStwTimer::stateChanged, this, &ScStwRace::timersChanged); + connect(timer, &ScStwTimer::reactionTimeChanged, this, &ScStwRace::timersChanged); } } @@ -192,3 +198,24 @@ bool ScStwRemoteMonitorRace::addTimer(ScStwTimer* timer) { Q_UNUSED(timer) return false; } + +QVariantList ScStwRemoteMonitorRace::getNextStartActionDetails() { + int nextActionDelay = 0; + double nextActionDelayProg = -1; + + if(this->nextStartAction == AtYourMarks || this->nextStartAction == Ready) { + // get the total delay and the delay progress of the next action timer + double elapsed = QDateTime::currentMSecsSinceEpoch() - this->nextStartActionDelayStartedAt; + nextActionDelay = this->nextStartActionTotalDelay; + if(elapsed < 0 || elapsed > nextActionDelay) { + elapsed = nextActionDelay; + } + nextActionDelayProg = elapsed / nextActionDelay; + } + + return { + this->nextStartAction, + nextActionDelay, + nextActionDelayProg + }; +}