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.
app/sources/speedtimer.cpp

146 lines
3.7 KiB
C++

#include "headers/speedtimer.h"
SpeedTimer * pGlobalSpeedTimer = nullptr;
SpeedTimer::SpeedTimer(QObject *parent) : QObject(parent)
{
this->date = new QDateTime;
this->startTime = 0;
this->stopTime = 0;
this->stoppedTime = 0;
this->reactionTime = 0;
this->state = IDLE;
this->remoteControlled = false;
}
void SpeedTimer::start() {
if(this->state != STARTING){
return;
}
qDebug() << "starting timer";
if(!this->remoteControlled){
this->stopTime = 0;
this->stoppedTime = 0;
this->reactionTime = 0;
this->startTime = this->date->currentMSecsSinceEpoch();
}
this->setState(RUNNING);
//this->startPad->appendCommand("SET_LED_RUNNING");
}
void SpeedTimer::stop(QString type) {
if(this->state != SpeedTimer::STARTING && this->state != SpeedTimer::RUNNING){
return;
}
//qDebug() << "Stopping: " << "start Time: " << startTime << " stopTime: " << stopTime << " stoppedTime: " << stoppedTime << " reactionTime: " << reactionTime;
if(this->remoteControlled){
if(type == "cancel"){
emit startCanceled(false);
}
}
else if(type == "cancel"){
emit startCanceled(false);
this->stoppedTime = 0;
}
else if(type == "manual"){
if(this->state == STARTING){
emit startCanceled(false);
}
this->stopTime = this->date->currentMSecsSinceEpoch();
this->stoppedTime = this->stopTime - this->startTime;
}
else if(type == "topPad"){
//this->stopTime = this->topPad->latest_button_pressed + this->topPad->offset;
this->stoppedTime = this->stopTime - this->startTime;
}
else if(type == "falseStart"){
emit startCanceled(true);
this->stoppedTime = this->reactionTime;
}
this->setState(STOPPED);
qDebug() << "Stopped: " << "start Time: " << startTime << " stopTime: " << stopTime << " stoppedTime: " << stoppedTime << " reactionTime: " << reactionTime;
//this->startPad->appendCommand("SET_LED_STARTING");
}
void SpeedTimer::reset(){
if(this->state != STOPPED){
return;
}
this->startTime = 0;
this->stopTime = 0;
this->stoppedTime = 0;
this->reactionTime = 0;
this->setState(IDLE);
//this->startPad->appendCommand("SET_LED_STARTING");
}
void SpeedTimer::setState(timerState newState){
this->state = newState;
qDebug() << "timer state changed: " << newState;
emit this->stateChanged(newState);
}
QString SpeedTimer::getState(){
switch(state){
case IDLE:
return("IDLE");
case STARTING:
return("STARTING");
case RUNNING:
return("RUNNING");
case STOPPED:
return("STOPPED");
}
}
double SpeedTimer::getCurrTime() {
double currTime;
if(this->state == RUNNING && !this->remoteControlled){
currTime = this->date->currentMSecsSinceEpoch() - this->startTime;
}
else {
currTime = this->stoppedTime;
}
return(currTime);
}
void SpeedTimer::delay(int mSecs){
QEventLoop loop;
QTimer timer;
timer.setSingleShot(true);
// quit the loop when the timer times out
loop.connect(&timer, SIGNAL(timeout()), &loop, SLOT(quit()));
//quit the loop when the connection was established
// start the timer before starting to connect
timer.start(mSecs);
//connect
//wait for the connection to finish (programm gets stuck in here)
loop.exec();
}
SpeedTimer::timerState SpeedTimer::stateFromString(QString state){
if(state == "IDLE"){
return IDLE;
}
else if (state == "STARTING") {
return STARTING;
}
else if (state == "RUNNING") {
return RUNNING;
}
else if (state == "STOPPED") {
return STOPPED;
}
}