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

212 lines
4.9 KiB
C++
Raw Normal View History

#include "headers/speedtimer.h"
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;
}
bool SpeedTimer::start(bool force) {
if(this->state != STARTING && !force){
return false;
}
qDebug() << "starting timer";
if(!force){
this->stopTime = 0;
this->stoppedTime = 0;
this->reactionTime = 0;
this->startTime = this->date->currentMSecsSinceEpoch();
}
this->setState(RUNNING);
return true;
}
bool SpeedTimer::stop(int type, bool force) {
// type can be:
// 0: stopped
// 1: cancelled
// 2: failed (fase start)
if( ( this->state != SpeedTimer::STARTING && this->state != SpeedTimer::RUNNING && this->state ) && !force ){
return false;
}
//qDebug() << "Stopping: " << "start Time: " << startTime << " stopTime: " << stopTime << " stoppedTime: " << stoppedTime << " reactionTime: " << reactionTime;
switch (type) {
case 0:
{
this->stopTime = this->date->currentMSecsSinceEpoch();
this->stoppedTime = this->stopTime - this->startTime;
this->setState(WON);
break;
}
case 1:
{
this->stoppedTime = 0;
this->setState(CANCELLED);
break;
}
case 2:
{
this->stoppedTime = this->reactionTime;
this->setState(FAILED);
break;
}
}
qDebug() << "Stopped: " << "start Time: " << startTime << " stopTime: " << stopTime << " stoppedTime: " << stoppedTime << " reactionTime: " << reactionTime;
return true;
//this->startPad->appendCommand("SET_LED_STARTING");
}
bool SpeedTimer::reset(bool force){
if( ( this->state < WON ) && !force){
return false;
}
this->startTime = 0;
this->stopTime = 0;
this->stoppedTime = 0;
this->reactionTime = 0;
this->setState(IDLE);
return true;
//this->startPad->appendCommand("SET_LED_STARTING");
}
void SpeedTimer::setState(timerState newState){
2019-03-09 15:06:48 +01:00
if(this->state != 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 WAITING:
return "WAITING";
case RUNNING:
return "RUNNING";
case WON:
return "WON";
case LOST:
return "LOST";
2019-03-07 22:31:23 +01:00
case FAILED:
return "FAILED";
2019-03-07 22:31:23 +01:00
case CANCELLED:
return "CANCELLED";
case DISABLED:
return "DISABLED";
}
return "ERROR";
}
double SpeedTimer::getCurrTime() {
double currTime;
if(this->state == RUNNING && this->startTime > 0){
currTime = this->date->currentMSecsSinceEpoch() - this->startTime;
}
else {
currTime = this->stoppedTime;
}
return(currTime);
}
QString SpeedTimer::getText() {
//qDebug() << this->getState();
QString newText;
switch (this->state) {
case SpeedTimer::IDLE:
newText = "0.000 sec";
break;
case SpeedTimer::STARTING:
newText = "0.000 sec";
break;
case SpeedTimer::WAITING:
newText = "please wait...";
break;
case SpeedTimer::RUNNING:
newText = QString::number( this->getCurrTime() / 1000.0, 'f', 3 ) + " sec";
break;
case SpeedTimer::WON:
newText = QString::number( this->stoppedTime / 1000.0, 'f', 3 ) + " sec";
break;
case SpeedTimer::LOST:
newText = QString::number( this->stoppedTime / 1000.0, 'f', 3 ) + " sec";
break;
case SpeedTimer::FAILED:
newText = "false start";
break;
case SpeedTimer::CANCELLED:
newText = "cancelled";
break;
case SpeedTimer::DISABLED:
newText = "---";
break;
}
return newText;
}
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 == "WON") {
return WON;
}
else if (state == "LOST") {
return LOST;
}
2019-03-07 22:31:23 +01:00
else if (state == "FAILED") {
return FAILED;
}
else if(state == "DISABLED") {
return DISABLED;
}
2019-03-07 22:31:23 +01:00
else {
return CANCELLED;
}
}