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
|
|
|
//this->startPad->appendCommand("SET_LED_RUNNING");
|
|
|
|
}
|
|
|
|
|
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-03-07 17:18:24 +01:00
|
|
|
this->setState(STOPPED);
|
|
|
|
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){
|
|
|
|
if( ( this->state != STOPPED && this->state != FAILED && this->state != CANCELLED ) && !force){
|
|
|
|
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:
|
|
|
|
return("IDLE");
|
|
|
|
case STARTING:
|
|
|
|
return("STARTING");
|
|
|
|
case RUNNING:
|
|
|
|
return("RUNNING");
|
|
|
|
case STOPPED:
|
|
|
|
return("STOPPED");
|
2019-03-07 22:31:23 +01:00
|
|
|
case FAILED:
|
|
|
|
return("FAILED");
|
|
|
|
case CANCELLED:
|
|
|
|
return("CANCELLED");
|
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:
|
|
|
|
newText = "Click Start to start";
|
|
|
|
break;
|
|
|
|
case SpeedTimer::STARTING:
|
|
|
|
newText = "0.000 sec";
|
|
|
|
break;
|
|
|
|
case SpeedTimer::RUNNING:
|
|
|
|
newText = QString::number( this->getCurrTime() / 1000.0, 'f', 3 ) + " sec";
|
|
|
|
break;
|
|
|
|
case SpeedTimer::STOPPED:
|
|
|
|
newText = QString::number( this->stoppedTime / 1000.0, 'f', 3 ) + " sec";
|
|
|
|
break;
|
|
|
|
case SpeedTimer::FAILED:
|
|
|
|
newText = "False Start";
|
|
|
|
break;
|
|
|
|
case SpeedTimer::CANCELLED:
|
|
|
|
newText = "Cancelled";
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
else if (state == "STOPPED") {
|
|
|
|
return STOPPED;
|
|
|
|
}
|
2019-03-07 22:31:23 +01:00
|
|
|
else if (state == "FAILED") {
|
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return CANCELLED;
|
|
|
|
}
|
2018-10-14 18:39:39 +02:00
|
|
|
}
|
|
|
|
|