2020-04-11 23:41:34 +02:00
|
|
|
#ifndef SCSTWTIMER_H
|
|
|
|
#define SCSTWTIMER_H
|
|
|
|
|
|
|
|
#include <QObject>
|
|
|
|
#include <QDateTime>
|
|
|
|
#include <QDebug>
|
2020-04-14 00:08:58 +02:00
|
|
|
#include <QTimer>
|
2020-04-11 23:41:34 +02:00
|
|
|
#include "ScStw.hpp"
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief The ScStwTimer class is used for advanced time measurement.
|
|
|
|
*
|
|
|
|
* It does not work on its own though.
|
|
|
|
* It is recommended to use it in combination with the ScStwRace class.
|
|
|
|
*
|
|
|
|
* ## When using standalone:
|
2020-04-19 13:09:24 +02:00
|
|
|
*
|
2020-04-15 19:21:32 +02:00
|
|
|
* \code{.cpp}
|
|
|
|
* ScStwTimer timer;
|
|
|
|
* // start the timer
|
|
|
|
* timer.start();
|
|
|
|
* // stop the timer
|
|
|
|
* timer.stop();
|
|
|
|
* \endcode
|
|
|
|
*
|
|
|
|
* The timer will now go into ScStw::WAITING state.
|
|
|
|
* That indicates that the timer has stopped and the final result has to be assigned by an external handler.
|
|
|
|
*
|
|
|
|
* \code{.cpp}
|
|
|
|
* // assign result 'won'
|
|
|
|
* timer.setResult(ScStwTimer::WON);
|
|
|
|
* \endcode
|
|
|
|
*
|
|
|
|
* The timer is now in ScStwTimer::WON state.
|
|
|
|
*
|
|
|
|
* \code{.cpp}
|
|
|
|
* // reset the timer
|
|
|
|
* timer.reset();
|
|
|
|
* \endcode
|
|
|
|
*
|
|
|
|
* The timer is not in ScStwTimer::IDLE state again.
|
|
|
|
*/
|
2020-04-11 23:41:34 +02:00
|
|
|
class ScStwTimer : public QObject
|
|
|
|
{
|
|
|
|
Q_OBJECT
|
|
|
|
public:
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief ScStwTimer constructor
|
|
|
|
* \param parent the parent object
|
2020-04-18 14:25:48 +02:00
|
|
|
* \param directControlEnabled Defines if protected properties (startTimer, stopTime, reactionTime and state) can be directly set from outside.
|
2020-04-15 19:21:32 +02:00
|
|
|
*/
|
2020-04-18 14:25:48 +02:00
|
|
|
explicit ScStwTimer(QObject *parent = nullptr, bool directControlEnabled = false);
|
2020-04-11 23:41:34 +02:00
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief The TimerState enum contains all state the timer can be in
|
|
|
|
*/
|
|
|
|
enum TimerState {
|
|
|
|
IDLE, /*!< Timer is waiting to be started */
|
|
|
|
STARTING, /*!< Timer is starting and will react with a false start if the climber starts */
|
|
|
|
RUNNING, /*!< Timer is running */
|
|
|
|
WAITING, /*!< Timer was stopped and is waiting for the result */
|
|
|
|
WON, /*!< Timer has won */
|
|
|
|
LOST, /*!< Timer has lost */
|
|
|
|
FAILED, /*!< A false start occured */
|
|
|
|
CANCELLED, /*!< Timer was cancelled */
|
|
|
|
DISABLED /*!< Timer is disabled */
|
|
|
|
};
|
2020-04-17 19:57:00 +02:00
|
|
|
Q_ENUM(TimerState);
|
2020-04-15 19:21:32 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief The StopReason enum contains all possible reasons for a stop
|
|
|
|
*/
|
|
|
|
enum StopReason {
|
|
|
|
ManualStop, /*!< Timer was stopped manually */
|
|
|
|
CancelStop, /*!< Timer was cancelled */
|
|
|
|
FailStop, /*!< A false start occured */
|
|
|
|
TopPadStop /*!< Timer was stopped by a physical trigger (eg. a ScStwExtension) */
|
|
|
|
};
|
2020-04-11 23:41:34 +02:00
|
|
|
|
|
|
|
protected:
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief The current state of the timer
|
|
|
|
*/
|
2020-04-11 23:41:34 +02:00
|
|
|
TimerState state;
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief The time the timer was started at
|
|
|
|
*/
|
2020-04-11 23:41:34 +02:00
|
|
|
double startTime;
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief The time the timer was stopped at
|
|
|
|
*/
|
|
|
|
double stopTime;
|
2020-04-11 23:41:34 +02:00
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief the reaction time of the climber
|
|
|
|
*/
|
|
|
|
double reactionTime;
|
2020-04-11 23:41:34 +02:00
|
|
|
|
2020-04-18 14:25:48 +02:00
|
|
|
/*!
|
|
|
|
* \brief Defines if protected properties (startTimer, stopTime, reactionTime and state) can be directly set from outside.
|
|
|
|
*/
|
|
|
|
bool directControlEnabled;
|
|
|
|
|
2020-04-11 23:41:34 +02:00
|
|
|
public slots:
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to start the timer
|
|
|
|
*
|
|
|
|
* To do this, the timer has to be in ScStwTimer::STARTING state!
|
|
|
|
*
|
|
|
|
* \return false if the timer was not in the required state and therefore not started, true otherwise
|
|
|
|
*/
|
2020-04-11 23:41:34 +02:00
|
|
|
bool start();
|
2020-04-15 19:21:32 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Function to start the timer at a given point in time (present or future)
|
|
|
|
*
|
|
|
|
* To do this, the timer has to be in ScStwTimer::STARTING state!
|
|
|
|
*
|
|
|
|
* \param timeOfStart the point in time (msecs since epoch) when the timer is supposted to be started
|
|
|
|
* \return false if the timer was not in the required state and therefore not started, true otherwise
|
|
|
|
*/
|
2020-04-14 00:08:58 +02:00
|
|
|
bool start(double timeOfStart);
|
2020-04-15 19:21:32 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Function to cancel the timer
|
|
|
|
*
|
|
|
|
* To do this, the timer has to be in ScStwTimer::IDLE, ScStwTimer::STARTING or ScStwTimer::RUNNING state!
|
|
|
|
*
|
|
|
|
* \return false if the timer was not in the required state and therefore not cancelled, true otherwise
|
|
|
|
*/
|
2020-04-11 23:41:34 +02:00
|
|
|
bool cancel();
|
2020-04-15 19:21:32 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Function to stop the timer
|
|
|
|
*
|
|
|
|
* To do this, the timer has to be in ScStwTimer::RUNNING state!
|
|
|
|
*
|
|
|
|
* \return false if the timer was not in the required state and therefore not stopped, true otherwise
|
|
|
|
*/
|
2020-04-11 23:41:34 +02:00
|
|
|
bool stop();
|
2020-04-15 19:21:32 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Function to stop the timer at a given point in time (past or future)
|
|
|
|
*
|
|
|
|
* To do this, the timer has to be in ScStwTimer::RUNNING state!
|
|
|
|
*
|
|
|
|
* \param timeOfStop the point in time (msecs since epoch) when the timer is supposted to be stopped
|
|
|
|
*
|
|
|
|
* \return false if the timer was not in the required state and therefore not stopped, true otherwise
|
|
|
|
*/
|
|
|
|
bool stop(double timeOfStop);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Function to assing the result of the race to the timer
|
|
|
|
*
|
|
|
|
* To do this, the timer has to be in ScStwTimer::WAITING state!
|
|
|
|
*
|
|
|
|
* \return false if the timer was not in the required state and the result therefore not set, true otherwise
|
|
|
|
*/
|
|
|
|
bool setResult(TimerState);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Function to reset the timer
|
|
|
|
*
|
|
|
|
* To do this, the timer has to be in ScStwTimer::WON or ScSTw::LOST state!
|
|
|
|
*
|
|
|
|
* \return false if the timer was not in the required state and therefore not reset, true otherwise
|
|
|
|
*/
|
2020-04-11 23:41:34 +02:00
|
|
|
bool reset();
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
// -- helper functions --
|
|
|
|
/*!
|
|
|
|
* \brief Function to get the current state of the timer
|
|
|
|
* \return current state of the timer
|
|
|
|
* \see ScStwTimer::TimerState
|
|
|
|
*/
|
2020-04-11 23:41:34 +02:00
|
|
|
TimerState getState();
|
2020-04-15 19:21:32 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Function to get the current time of the timer
|
|
|
|
*
|
|
|
|
* To do this, the timer has to be in ScStwTimer::RUNNING, ScStwTimer::WAITING, ScStwTimer::WON or ScSTw::LOST state!
|
|
|
|
*
|
|
|
|
* \return The current / final time of the timer or -1 if it is not in the required state
|
|
|
|
*/
|
2020-04-11 23:41:34 +02:00
|
|
|
double getCurrentTime();
|
2020-04-15 19:21:32 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Function to get the reaction time of the climber.
|
|
|
|
* \return The climbers reaction time
|
|
|
|
*/
|
2020-04-11 23:41:34 +02:00
|
|
|
double getReactionTime();
|
2020-04-15 19:21:32 +02:00
|
|
|
|
2020-04-17 19:57:00 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to get the text, a timer display is supposed to show
|
|
|
|
* \return The text to show
|
|
|
|
*/
|
|
|
|
QString getText();
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to set if the timer is supposed to be disabled
|
|
|
|
*
|
|
|
|
* !!! CAUTION use this function with care, it immidiately changes the state of the timer !!!
|
|
|
|
* It is recommended to only use this function to change the timers state after the
|
|
|
|
* ScStwTimer::requestTimerEnableChange() signal was called, during the race,
|
|
|
|
* the timer is used in, is in IDLE state.
|
|
|
|
*
|
|
|
|
* \param disabled if the timer is supposed to be diabled
|
|
|
|
*/
|
2020-04-11 23:41:34 +02:00
|
|
|
void setDisabled(bool disabled);
|
|
|
|
|
2020-04-18 14:25:48 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to change the state of the timer
|
|
|
|
*
|
|
|
|
* Only works when directControlEnabled is set to true!
|
|
|
|
* Doing this will emit the ScStwTimer::stateChanged() signal (only if the new state differs from the current one)
|
|
|
|
*
|
|
|
|
* \param newState The new state
|
|
|
|
* \param force whether to force the state change (just to distinguish between protected and public function)
|
|
|
|
* \return false when directControlEnabled or force is set to false and the startTime was therefore not modified, true otherwise
|
|
|
|
*/
|
|
|
|
bool setState(TimerState newState, bool force);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Function to dircetly change the start time
|
|
|
|
*
|
|
|
|
* Only works when directControlEnabled is set to true!
|
|
|
|
*
|
|
|
|
* \param startTime the time to change to
|
|
|
|
* \return false when directControlEnabled is set to false and the startTime was therefore not modified, true otherwise
|
|
|
|
*/
|
|
|
|
bool setStartTime(double startTime);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Function to dircetly change the stop time
|
|
|
|
*
|
|
|
|
* Only works when directControlEnabled is set to true!
|
|
|
|
*
|
|
|
|
* \param stopTime the time to change to
|
|
|
|
* \return false when directControlEnabled is set to false and the stopTime was therefore not modified, true otherwise
|
|
|
|
*/
|
|
|
|
bool setStopTime(double stopTime);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Function to dircetly change the rection time
|
|
|
|
*
|
|
|
|
* Only works when directControlEnabled is set to true!
|
|
|
|
*
|
|
|
|
* \param reactionTime the time to change to
|
|
|
|
* \return false when directControlEnabled is set to false and the reactionTime was therefore not modified, true otherwise
|
|
|
|
*/
|
|
|
|
bool setReactionTime(double rectionTime);
|
|
|
|
|
2020-04-11 23:41:34 +02:00
|
|
|
protected slots:
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief slot to call when the climber has started
|
|
|
|
* \param timeOfStart time (msecs since epoch) when the climber started
|
|
|
|
*/
|
2020-04-14 00:08:58 +02:00
|
|
|
void handleClimberStart(double timeOfStart);
|
2020-04-15 19:21:32 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Function to stop the timer at a given point in time (past or future)
|
|
|
|
*
|
|
|
|
* To do this, the timer has to be in ScStwTimer::RUNNING state!
|
|
|
|
*
|
|
|
|
* \param reason reason for the timer stop
|
|
|
|
* \param timeOfStop the point in time (msecs since epoch) when the timer is supposted to be stopped
|
|
|
|
*
|
|
|
|
* \return false if the timer was not in the required state and therefore not stopped, true otherwise
|
|
|
|
*
|
|
|
|
* \see ScStwTimer::StopReason
|
|
|
|
*/
|
|
|
|
bool stop(StopReason reason, double timeOfStop);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Function to stop the timer
|
|
|
|
*
|
|
|
|
* To do this, the timer has to be in ScStwTimer::RUNNING state!
|
|
|
|
*
|
|
|
|
* \param reason reason for the timer stop
|
|
|
|
*
|
|
|
|
* \return false if the timer was not in the required state and therefore not stopped, true otherwise
|
|
|
|
*
|
|
|
|
* \see ScStwTimer::StopReason
|
|
|
|
*/
|
2020-04-11 23:41:34 +02:00
|
|
|
bool stop(StopReason reason);
|
|
|
|
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Function to change the state of the timer
|
|
|
|
*
|
|
|
|
* Doing this will emit the ScStwTimer::stateChanged() signal (only if the new state differs from the current one)
|
|
|
|
*
|
|
|
|
* \param newState The new state
|
|
|
|
*/
|
2020-04-11 23:41:34 +02:00
|
|
|
void setState(TimerState newState);
|
|
|
|
|
2020-04-18 14:25:48 +02:00
|
|
|
|
2020-04-11 23:41:34 +02:00
|
|
|
signals:
|
2020-04-15 19:21:32 +02:00
|
|
|
/*!
|
|
|
|
* \brief Emitted when the state of the timer changed
|
|
|
|
*/
|
2020-04-11 23:41:34 +02:00
|
|
|
void stateChanged();
|
2020-04-15 19:21:32 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Emitted when the reaction time changed
|
|
|
|
*/
|
2020-04-11 23:41:34 +02:00
|
|
|
void reactionTimeChanged();
|
2020-04-15 19:21:32 +02:00
|
|
|
|
|
|
|
/*!
|
|
|
|
* \brief Emitted when the timer wants its state to be changed by the external handler
|
|
|
|
* \param timer the timer object
|
|
|
|
*/
|
2020-04-14 00:08:58 +02:00
|
|
|
void requestEnableChange(ScStwTimer* timer);
|
2020-04-11 23:41:34 +02:00
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // SCSTWTIMER_H
|