2018-10-04 18:35:29 +02:00
|
|
|
#include "headers/speedtimer.h"
|
|
|
|
|
|
|
|
SpeedTimer::SpeedTimer(QObject *parent) : QObject(parent)
|
|
|
|
{
|
2018-10-14 18:39:39 +02:00
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
this->date = new QDateTime;
|
|
|
|
|
|
|
|
this->startTime = 0;
|
|
|
|
this->stopTime = 0;
|
|
|
|
this->stoppedTime = 0;
|
|
|
|
this->reactionTime = 0;
|
|
|
|
this->state = IDLE;
|
|
|
|
}
|
|
|
|
|
2019-03-07 17:18:24 +01:00
|
|
|
bool SpeedTimer::start(bool force) {
|
|
|
|
if(this->state != STARTING && !force){
|
|
|
|
return false;
|
2018-10-04 18:35:29 +02:00
|
|
|
}
|
2018-10-14 18:39:39 +02:00
|
|
|
qDebug() << "starting timer";
|
2019-03-07 17:18:24 +01:00
|
|
|
if(!force){
|
2018-10-14 18:39:39 +02:00
|
|
|
this->stopTime = 0;
|
|
|
|
this->stoppedTime = 0;
|
|
|
|
this->reactionTime = 0;
|
|
|
|
this->startTime = this->date->currentMSecsSinceEpoch();
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
this->setState(RUNNING);
|
2019-03-07 17:18:24 +01:00
|
|
|
|
|
|
|
return true;
|
2018-10-04 18:35:29 +02:00
|
|
|
}
|
|
|
|
|
2019-03-07 17:18:24 +01:00
|
|
|
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;
|
2018-10-04 18:35:29 +02:00
|
|
|
}
|
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
//qDebug() << "Stopping: " << "start Time: " << startTime << " stopTime: " << stopTime << " stoppedTime: " << stoppedTime << " reactionTime: " << reactionTime;
|
2018-10-04 18:35:29 +02:00
|
|
|
|
2019-03-07 17:18:24 +01:00
|
|
|
switch (type) {
|
|
|
|
case 0:
|
|
|
|
{
|
2018-10-04 18:35:29 +02:00
|
|
|
this->stopTime = this->date->currentMSecsSinceEpoch();
|
|
|
|
this->stoppedTime = this->stopTime - this->startTime;
|
2019-09-08 00:50:03 +02:00
|
|
|
this->setState(WON);
|
2019-03-07 17:18:24 +01:00
|
|
|
break;
|
2018-10-04 18:35:29 +02:00
|
|
|
}
|
2019-03-07 17:18:24 +01:00
|
|
|
case 1:
|
|
|
|
{
|
|
|
|
this->stoppedTime = 0;
|
|
|
|
this->setState(CANCELLED);
|
|
|
|
break;
|
2018-10-04 18:35:29 +02:00
|
|
|
}
|
2019-03-07 17:18:24 +01:00
|
|
|
case 2:
|
|
|
|
{
|
2018-10-04 18:35:29 +02:00
|
|
|
this->stoppedTime = this->reactionTime;
|
2019-03-07 17:18:24 +01:00
|
|
|
this->setState(FAILED);
|
|
|
|
break;
|
|
|
|
}
|
2018-10-04 18:35:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
qDebug() << "Stopped: " << "start Time: " << startTime << " stopTime: " << stopTime << " stoppedTime: " << stoppedTime << " reactionTime: " << reactionTime;
|
2019-03-07 17:18:24 +01:00
|
|
|
|
|
|
|
return true;
|
2018-10-04 18:35:29 +02:00
|
|
|
//this->startPad->appendCommand("SET_LED_STARTING");
|
|
|
|
}
|
|
|
|
|
2019-03-07 17:18:24 +01:00
|
|
|
bool SpeedTimer::reset(bool force){
|
2019-09-08 00:50:03 +02:00
|
|
|
if( ( this->state < WON ) && !force){
|
2019-03-07 17:18:24 +01:00
|
|
|
return false;
|
2018-10-04 18:35:29 +02:00
|
|
|
}
|
2018-10-14 18:39:39 +02:00
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
this->startTime = 0;
|
|
|
|
this->stopTime = 0;
|
|
|
|
this->stoppedTime = 0;
|
|
|
|
this->reactionTime = 0;
|
|
|
|
this->setState(IDLE);
|
2019-03-07 17:18:24 +01:00
|
|
|
|
|
|
|
return true;
|
2018-10-04 18:35:29 +02:00
|
|
|
//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);
|
|
|
|
}
|
2018-10-04 18:35:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString SpeedTimer::getState(){
|
|
|
|
switch(state){
|
|
|
|
case IDLE:
|
2019-09-08 15:08:50 +02:00
|
|
|
return "IDLE";
|
2018-10-04 18:35:29 +02:00
|
|
|
case STARTING:
|
2019-09-08 15:08:50 +02:00
|
|
|
return "STARTING";
|
2019-05-02 22:39:22 +02:00
|
|
|
case WAITING:
|
2019-09-08 15:08:50 +02:00
|
|
|
return "WAITING";
|
2018-10-04 18:35:29 +02:00
|
|
|
case RUNNING:
|
2019-09-08 15:08:50 +02:00
|
|
|
return "RUNNING";
|
2019-09-08 00:50:03 +02:00
|
|
|
case WON:
|
|
|
|
return "WON";
|
|
|
|
case LOST:
|
|
|
|
return "LOST";
|
2019-03-07 22:31:23 +01:00
|
|
|
case FAILED:
|
2019-09-08 15:08:50 +02:00
|
|
|
return "FAILED";
|
2019-03-07 22:31:23 +01:00
|
|
|
case CANCELLED:
|
2019-09-08 15:08:50 +02:00
|
|
|
return "CANCELLED";
|
2019-09-08 00:50:03 +02:00
|
|
|
case DISABLED:
|
|
|
|
return "DISABLED";
|
2018-10-04 18:35:29 +02:00
|
|
|
}
|
2019-08-20 10:19:35 +02:00
|
|
|
return "ERROR";
|
2018-10-04 18:35:29 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
double SpeedTimer::getCurrTime() {
|
|
|
|
double currTime;
|
2019-03-07 17:18:24 +01:00
|
|
|
if(this->state == RUNNING && this->startTime > 0){
|
2018-10-04 18:35:29 +02:00
|
|
|
currTime = this->date->currentMSecsSinceEpoch() - this->startTime;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
currTime = this->stoppedTime;
|
|
|
|
}
|
|
|
|
|
|
|
|
return(currTime);
|
|
|
|
}
|
|
|
|
|
2019-03-07 17:18:24 +01:00
|
|
|
QString SpeedTimer::getText() {
|
|
|
|
//qDebug() << this->getState();
|
|
|
|
QString newText;
|
|
|
|
switch (this->state) {
|
|
|
|
case SpeedTimer::IDLE:
|
2019-09-08 15:08:50 +02:00
|
|
|
newText = "0.000 sec";
|
2019-03-07 17:18:24 +01:00
|
|
|
break;
|
|
|
|
case SpeedTimer::STARTING:
|
|
|
|
newText = "0.000 sec";
|
|
|
|
break;
|
2019-04-15 13:33:31 +02:00
|
|
|
case SpeedTimer::WAITING:
|
2019-10-02 19:12:06 +02:00
|
|
|
newText = "please wait...";
|
2019-04-15 13:33:31 +02:00
|
|
|
break;
|
2019-03-07 17:18:24 +01:00
|
|
|
case SpeedTimer::RUNNING:
|
|
|
|
newText = QString::number( this->getCurrTime() / 1000.0, 'f', 3 ) + " sec";
|
|
|
|
break;
|
2019-09-08 00:50:03 +02:00
|
|
|
case SpeedTimer::WON:
|
|
|
|
newText = QString::number( this->stoppedTime / 1000.0, 'f', 3 ) + " sec";
|
|
|
|
break;
|
|
|
|
case SpeedTimer::LOST:
|
2019-03-07 17:18:24 +01:00
|
|
|
newText = QString::number( this->stoppedTime / 1000.0, 'f', 3 ) + " sec";
|
|
|
|
break;
|
|
|
|
case SpeedTimer::FAILED:
|
2019-10-02 19:12:06 +02:00
|
|
|
newText = "false start";
|
2019-03-07 17:18:24 +01:00
|
|
|
break;
|
|
|
|
case SpeedTimer::CANCELLED:
|
2019-10-02 19:12:06 +02:00
|
|
|
newText = "cancelled";
|
2019-03-07 17:18:24 +01:00
|
|
|
break;
|
2019-09-08 00:50:03 +02:00
|
|
|
case SpeedTimer::DISABLED:
|
|
|
|
newText = "---";
|
|
|
|
break;
|
2019-03-07 17:18:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return newText;
|
|
|
|
}
|
|
|
|
|
2018-10-04 18:35:29 +02:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
2018-10-14 18:39:39 +02:00
|
|
|
SpeedTimer::timerState SpeedTimer::stateFromString(QString state){
|
|
|
|
|
|
|
|
if(state == "IDLE"){
|
|
|
|
return IDLE;
|
|
|
|
}
|
|
|
|
else if (state == "STARTING") {
|
|
|
|
return STARTING;
|
|
|
|
}
|
|
|
|
else if (state == "RUNNING") {
|
|
|
|
return RUNNING;
|
|
|
|
}
|
2019-09-08 00:50:03 +02:00
|
|
|
else if (state == "WON") {
|
|
|
|
return WON;
|
|
|
|
}
|
|
|
|
else if (state == "LOST") {
|
|
|
|
return LOST;
|
2018-10-14 18:39:39 +02:00
|
|
|
}
|
2019-03-07 22:31:23 +01:00
|
|
|
else if (state == "FAILED") {
|
|
|
|
return FAILED;
|
|
|
|
}
|
2019-09-08 00:50:03 +02:00
|
|
|
else if(state == "DISABLED") {
|
|
|
|
return DISABLED;
|
|
|
|
}
|
2019-03-07 22:31:23 +01:00
|
|
|
else {
|
|
|
|
return CANCELLED;
|
|
|
|
}
|
2018-10-14 18:39:39 +02:00
|
|
|
}
|
|
|
|
|