This repository has been archived on 2024-06-03. You can view files and clone it, but cannot push or open issues or pull requests.
shared-libraries/ScStwLibraries/sources/scstwtimer.cpp

179 lines
4.4 KiB
C++

#include "../headers/scstwtimer.h"
ScStwTimer::ScStwTimer(QObject *parent) : QObject(parent)
{
this->startTime = 0;
this->stopTime = 0;
this->stoppedTime = 0;
this->reactionTime = 0;
this->state = IDLE;
}
bool ScStwTimer::start() {
return this->start(QDateTime::currentMSecsSinceEpoch());
}
bool ScStwTimer::start(double timeOfStart) {
switch (this->state) {
case IDLE: {
// in case of IDLE, start the race!
this->startTime = timeOfStart;
this->stopTime = 0;
this->stoppedTime = 0;
if(timeOfStart - QDateTime::currentMSecsSinceEpoch() > 0) {
this->setState(STARTING);
QTimer::singleShot(timeOfStart - QDateTime::currentMSecsSinceEpoch(), [=](){
if(this->state == STARTING)
this->setState(RUNNING);
});
QTimer::singleShot(timeOfStart - QDateTime::currentMSecsSinceEpoch() - 1000, [=](){
//this->handleClimberStart(QDateTime::currentMSecsSinceEpoch());
});
}
else
this->setState(RUNNING);
return true;
}
default: {
// otherwise the timer is not supposed to be started!
return false;
}
}
}
void ScStwTimer::handleClimberStart(double timeOfStart) {
this->reactionTime = timeOfStart - this->startTime;
qDebug() << "+ [INFO][TIMER] reaction time: " << this->reactionTime;
if(this->reactionTime <= 0){
this->stop(FailStop);
return;
}
// TODO: check if necessary: emit this->reactionTimeChanged();
}
bool ScStwTimer::cancel() {
if(!(this->state == IDLE || this->state == STARTING || this->state == RUNNING))
return false;
this->setState(CANCELLED);
return true;
}
bool ScStwTimer::stop() {
return this->stop(ManualStop);
}
bool ScStwTimer::stop(TimerState result) {
switch (result) {
case WON:
this->setState(WON);
return true;
case LOST:
this->setState(LOST);
return false;
default:
return false;
}
}
bool ScStwTimer::stop(StopReason reason) {
if(this->state != STARTING && this->state != RUNNING && this->state != WAITING){
return false;
}
switch (reason) {
case ManualStop: {
if(this->state == STARTING){
emit startCanceled(false);
this->setState(CANCELLED);
}
else {
this->stopTime = this->date->currentMSecsSinceEpoch();
this->stoppedTime = this->stopTime - this->startTime;
// trigger an external state refresh to set the state to either WON or LOST depending on the other timers values (see MainActivity::refreshTimerStates())
emit this->stopRequested();
}
break;
}
case FailStop: {
this->stoppedTime = 0;
qDebug() << "+ [INFO][TIMER] False Start detected: " << "start Time: " << startTime << " reactionTime: " << reactionTime;
this->setState(FAILED);
emit startCanceled(true);
return true;
}
default: {
return false;
}
}
qDebug() << "+ [INFO][TIMER] Stopped: " << "start Time: " << startTime << " stopTime: " << stopTime << " stoppedTime: " << stoppedTime << " reactionTime: " << reactionTime;
return true;
}
bool ScStwTimer::reset(){
if( this->state < WON || this->state == DISABLED ){
return false;
}
this->startTime = 0;
this->stopTime = 0;
this->stoppedTime = 0;
this->reactionTime = 0;
this->startPadTriggerTime = 0;
this->setState(IDLE);
return true;
}
// ------------------------
// --- helper functions ---
// ------------------------
void ScStwTimer::setState(TimerState newState){
if(this->state == DISABLED && newState != IDLE)
return;
if(this->state != newState) {
this->state = newState;
qDebug() << "+ [INFO][TIMER] timer state changed: " << newState;
emit this->stateChanged();
}
}
ScStwTimer::TimerState ScStwTimer::getState() {
return this->state;
}
double ScStwTimer::getCurrentTime() {
if(this->state == RUNNING){
return this->date->currentMSecsSinceEpoch() - this->startTime;
}
else {
return this->stoppedTime;
}
}
double ScStwTimer::getReactionTime() {
return this->reactionTime;
}
void ScStwTimer::setDisabled(bool disabled) {
if(disabled)
this->setState(DISABLED);
else
this->setState(IDLE);
}