diff --git a/RennbahnZeitmessung.pro b/RennbahnZeitmessung.pro index 8ec8c2e..65a63b5 100644 --- a/RennbahnZeitmessung.pro +++ b/RennbahnZeitmessung.pro @@ -26,7 +26,8 @@ SOURCES += main.cpp\ database.cpp \ windowrennliste.cpp \ training.cpp \ - timemodel.cpp + timemodel.cpp \ + qvectorhelper.cpp HEADERS += \ mainwindow.h \ @@ -39,7 +40,8 @@ HEADERS += \ database.h \ windowrennliste.h \ training.h \ - timemodel.h + timemodel.h \ + qvectorhelper.h FORMS += mainwindow.ui \ windowrace.ui \ diff --git a/RennbahnZeitmessung.pro.user b/RennbahnZeitmessung.pro.user index 3d47973..415f167 100644 --- a/RennbahnZeitmessung.pro.user +++ b/RennbahnZeitmessung.pro.user @@ -1,6 +1,6 @@ - + EnvironmentId diff --git a/hardwaresetup.cpp b/hardwaresetup.cpp index acefc12..b239737 100644 --- a/hardwaresetup.cpp +++ b/hardwaresetup.cpp @@ -169,7 +169,7 @@ void HardwareSetup::run(){ while(!this->stop){ usleep(100000); // 100ms - while(1){ + //while(1){ nBytes = usb_control_msg(this->handle, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_DATA_OUT, 0, 0, (char *)buffer, sizeof(buffer), 5000); @@ -203,46 +203,11 @@ void HardwareSetup::run(){ // if(buffer[0].update != 0){ // cout << "Got " << nBytes << " bytes: " << (int)buffer[2].time << ", " << (int)buffer[2].id << endl; // } - -// if((int)buffer->update == 1){ -// //if((int) buffer->id) -// emit Dea((int)buffer->time, 1); -// } - } + //} if(nBytes < 0) fprintf(stderr, "USB error: %sn", usb_strerror()); - //if(getDea()){ - if(1){ - - // if(!deaBefore){ - if(1){ - index = 1; - deaBefore = true; - //emit Dea(5); - } - else{ - index += 1; - //Debug - //std::cout << index << std::endl; - } - - } - else{ - deaBefore = false; - } - -// if(getShell()){ - if(1){ - if(!shellBefore){ - shellBefore = true; - //emit Shell(); - } - } - else{ - shellBefore = false; - } } } diff --git a/qvectorhelper.cpp b/qvectorhelper.cpp new file mode 100644 index 0000000..34d10b8 --- /dev/null +++ b/qvectorhelper.cpp @@ -0,0 +1,37 @@ +#include "qvectorhelper.h" +#include + +QVectorHelper::QVectorHelper() +{ + +} + +int QVectorHelper::getCurTime(const QVector x) { + if(x.size() >= 3){ + return x.at(0)+ x.at(1) + x.at(2); + } + else{ + return 9999; + } +} + +int QVectorHelper::getMin(const QVector> x){ + int min; + if(x.size() > 0){ + if(x.at(0).size() >= 3){ + min = getCurTime(x.at(0)); + } + else{ + return 9999; + } + } + else{ + return 9999; + } + for(int i = 1; i < x.size(); i++){ + if(getCurTime(x.at(i)) < min){ + min = getCurTime(x.at(i)); + } + } + return min; +} diff --git a/qvectorhelper.h b/qvectorhelper.h new file mode 100644 index 0000000..38459c3 --- /dev/null +++ b/qvectorhelper.h @@ -0,0 +1,13 @@ +#ifndef QVECTORHELPER_H +#define QVECTORHELPER_H +#include + +class QVectorHelper +{ +public: + QVectorHelper(); + static int getCurTime(const QVector x); + static int getMin(const QVector> x); +}; + +#endif // QVECTORHELPER_H diff --git a/timemodel.cpp b/timemodel.cpp index ecb2c4b..ba05dc0 100644 --- a/timemodel.cpp +++ b/timemodel.cpp @@ -58,7 +58,9 @@ QColor TimeModel::getColor(const QVector> data, const int col, cons if(data.at(row).size() > col){ // check for enough cols QVector compare; for(int i = 0; i <= row; i++){ - compare.append(data.at(i).at(col)); + if(data.at(i).size() > col){ + compare.append(data.at(i).at(col)); + } } if(data.at(row).at(col) == getMin(compare)){ return QColor(Qt::green); diff --git a/training.cpp b/training.cpp index 524e0c3..05ed6b0 100644 --- a/training.cpp +++ b/training.cpp @@ -179,7 +179,8 @@ void Training::shellSlot(int time, int sector){ } } void Training::closeEvent(QCloseEvent *event){ - + Hardware->setStop(); + delete this->Hardware; } void Training::deaSlot(int time, int sector){ @@ -218,10 +219,10 @@ void Training::deaSlot(int time, int sector){ //cout << "Time sec 3: " << time << endl; if(VecDea.last().size() == 2){ // cout << time << sector << endl; - VecDea[VecDea.size()-1].push_back(time); + VecDea.last().push_back(time); // add sum to vector - VecDea[VecDea.size()-1].push_back(getCurTime(VecDea.last())); + VecDea.last().push_back(getCurTime(VecDea.last())); diff --git a/windowrace.cpp b/windowrace.cpp index 2db16ac..4b325a4 100644 --- a/windowrace.cpp +++ b/windowrace.cpp @@ -7,6 +7,8 @@ #include #include "unistd.h" #include +#include "qvectorhelper.h" +#include "timemodel.h" using std::vector; using std::string; @@ -50,8 +52,8 @@ WindowRace::WindowRace(DataBase *db, QWidget *parent) : Hardware = new HardwareSetup; Hardware->start(); - QObject::connect(Hardware, SIGNAL(Dea()), this, SLOT(deaSlot())); - QObject::connect(Hardware, SIGNAL(Shell()), this, SLOT(shellSlot())); + QObject::connect(Hardware, SIGNAL(Dea(int, int)), this, SLOT(deaSlot(int, int))); + QObject::connect(Hardware, SIGNAL(Shell(int, int)), this, SLOT(shellSlot(int, int))); QObject::connect(ui->pBStart, SIGNAL(clicked()), this, SLOT(go())); QObject::connect(countdown, SIGNAL(CountdownUpdate()), this, SLOT(countdownUpdate())); QObject::connect(startAmpelThread, SIGNAL(ampelUpdate()), this, SLOT(ampelSlot())); @@ -156,8 +158,8 @@ void WindowRace::prepareNextRace(){ this->firstTimeShell = true; this->firstTimeDea = true; - this->ui->lWShellTime->clear(); - this->ui->lWDeaTime->clear(); + // this->ui->lWShellTime->clear(); + // this->ui->lWDeaTime->clear(); this->ui->lBestZeitDea->setText("∞"); this->ui->lBestZeitShell->setText("∞"); @@ -167,6 +169,13 @@ void WindowRace::prepareNextRace(){ this->VecShell.clear(); this->VecDea.clear(); + // clear tableview shell + timeModelShell = new TimeModel(VecShell); + ui->lWShellTime->setModel(timeModelShell); + + // clear tableview dea + timeModelDea = new TimeModel(VecDea); + ui->lWDeaTime->setModel(timeModelDea); this->finished = false; this->paused = false; @@ -230,6 +239,8 @@ void WindowRace::setDriverAndCarId(vector vec){ } void WindowRace::closeEvent(QCloseEvent *event){ + countdown->setStop(); + QMessageBox msgBox; msgBox.setText("Wirklich schliessen?"); msgBox.setInformativeText(""); @@ -292,7 +303,7 @@ void WindowRace::countdownUpdate(){ } if(countdownValue <= -1){ this->finished = true; - this->wRennliste->setBesttime(getMinimum(this->VecShell),getMinimum(this->VecDea)); + this->wRennliste->setBesttime(QVectorHelper::getMin(VecShell),QVectorHelper::getMin(this->VecDea)); this->ui->pBNextRace->setEnabled(true); } else{ @@ -302,6 +313,7 @@ void WindowRace::countdownUpdate(){ } } + WindowRace::~WindowRace() { this->startAmpelThread->setStop(); @@ -317,67 +329,144 @@ WindowRace::~WindowRace() delete ui; } -void WindowRace::shellSlot(){ +void WindowRace::shellSlot(int time, int sector){ if(started && !paused && !this->finished){ if(firstTimeShell){ firstTimeShell = false; - counterShell.start(); + // counterShell.start(); } else{ - long zeit = counterShell.getTime(); - VecShell.push_back(zeit); - QListWidgetItem *item = new QListWidgetItem; - if(getMinimum(VecShell) == zeit && VecShell.size() > 1){ - item->setBackgroundColor(Qt::green); + // long zeit = counterShell.getTime(); + switch(sector){ + case 1: + VecShell.append(QVector()); + VecShell.last().append(time); + + // update tableview + this->timeModelShell = new TimeModel(VecShell, this); +// this->ui->lWShellTime->setModel(this->timeModelShell); + ui->lWShellTime->setModel(this->timeModelShell); + break; + + case 2: + if(VecShell.size() > 0){ + if(VecShell.last().size() == 1){ + VecShell.last().append(time); + } + // update tableview + timeModelShell = new TimeModel(VecShell, this); + this->ui->lWShellTime->setModel(timeModelShell); + } + break; + case 3: + if(VecShell.size() > 0){ + if(VecShell.last().size() == 2){ + VecShell.last().append(time); + VecShell.last().append(QVectorHelper::getCurTime(VecShell.last())); + + if(QVectorHelper::getCurTime(VecShell.last()) <= QVectorHelper::getMin(VecShell)){ + ui->lBestZeitShell->setText(QString::number((double)QVectorHelper::getMin(VecShell)/1000)); + } + ui->lCurRoundTimeShell->setText(QString::number((double)QVectorHelper::getCurTime(VecShell.last())/1000)); + + // update tableview + timeModelShell = new TimeModel(VecShell, this); + this->ui->lWShellTime->setModel(timeModelShell); + int zeit = QVectorHelper::getMin(VecShell); + QString statement = "insert into Zeiten (id_rennen, id_fahrer, id_auto, id_bahn, zeit) values ("+QString::number(this->renn_id)+ + ", "+QString::number(this->shellDriverId)+", "+QString::number(this->shellCarId)+", 1, "+QString::number(zeit)+")"; + cout << statement.toStdString() << endl; + + //this->db->setData(statement.toStdString()); + } + } + + + + break; + } - if(zeit < this->minimumTime){ - item->setBackgroundColor(Qt::red); - this->firstTimeShell = true; + if(sector == 3){ } - item->setText(QString::number((double)zeit/1000)); - ui->lWShellTime->addItem(item); - QString statement = "insert into Zeiten (id_rennen, id_fahrer, id_auto, id_bahn, zeit) values ("+QString::number(this->renn_id)+ - ", "+QString::number(this->shellDriverId)+", "+QString::number(this->shellCarId)+", 1, "+QString::number(zeit)+")"; - //cout << statement.toStdString() << endl; - this->db->setData(statement.toStdString()); + + ui->lWShellTime->scrollToBottom(); - if(getMinimum(VecShell) > 0){ - ui->lBestZeitShell->setText(QString::number((double)getMinimum(VecShell)/1000)); - } - ui->lCurRoundTimeShell->setText(QString::number((double)zeit/1000)); } } + ui->lWShellTime->scrollToBottom(); + ui->lWShellTime->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); } -void WindowRace::deaSlot(){ +void WindowRace::deaSlot(int time, int sector){ if(started && !paused && !this->finished){ if(firstTimeDea){ firstTimeDea = false; counterDea.start(); } else{ - long zeit = counterDea.getTime(); - VecDea.push_back(zeit); - QListWidgetItem *item = new QListWidgetItem; - if(getMinimum(VecDea) == zeit && VecDea.size() > 1){ - item->setBackgroundColor(Qt::green); + + switch(sector){ + case 1: + VecDea.push_back(QVector()); + VecDea.last().push_back(time); + cout << "Dea Sektor 1" << endl; + timeModelDea = new TimeModel(VecDea, this); + this->ui->lWDeaTime->setModel(timeModelDea); + + break; + case 2: + if(VecDea.size() > 0){ + if(VecDea.last().size() == 1){ + cout << time << sector << endl; + VecDea[VecDea.size()-1].push_back(time); + timeModelDea = new TimeModel(VecDea, this); + this->ui->lWDeaTime->setModel(timeModelDea); + } + else{ + //VecDea[VecDea.size()-1].append(9999); + } + } + break; + case 3: + if(VecDea.size() > 0){ + //cout << "Time sec 3: " << time << endl; + if(VecDea.last().size() == 2){ +// cout << time << sector << endl; + VecDea.last().push_back(time); + + // add sum to vector + VecDea.last().push_back(QVectorHelper::getCurTime(VecDea.last())); + + + + timeModelDea = new TimeModel(VecDea, this); + this->ui->lWDeaTime->setModel(timeModelDea); + + + } + + + + // best time on widget + cout << "cur time: " << QVectorHelper::getCurTime(VecDea.last()) << endl; + cout << "min time: " << QVectorHelper::getMin(VecDea) << endl; + if(QVectorHelper::getCurTime(VecDea.last()) <= QVectorHelper::getMin(VecDea)){ + ui->lBestZeitDea->setText(QString::number((double)QVectorHelper::getMin(VecDea)/1000)); + } + ui->lCurRoundDea->setText(QString::number((double)QVectorHelper::getCurTime(VecDea.last())/1000)); + } + int zeit = QVectorHelper::getMin(VecDea); + QString statement = "insert into Zeiten (id_rennen, id_fahrer, id_auto, id_bahn, zeit) values ("+QString::number(this->renn_id)+ + ", "+QString::number(this->deaDriverId)+", "+QString::number(this->deaCarId)+", 2, "+QString::number(zeit)+")"; + cout << statement.toStdString() << endl; + //this->db->setData(statement.toStdString()); + ui->lWDeaTime->scrollToBottom(); + + break; } - if(zeit < this->minimumTime){ - item->setBackgroundColor(Qt::red); - this->firstTimeDea = true; - } - item->setText(QString::number((double)zeit/1000)); - ui->lWDeaTime->addItem(item); - QString statement = "insert into Zeiten (id_rennen, id_fahrer, id_auto, id_bahn, zeit) values ("+QString::number(this->renn_id)+ - ", "+QString::number(this->deaDriverId)+", "+QString::number(this->deaCarId)+", 2, "+QString::number(zeit)+")"; - //cout << statement.toStdString() << endl; - this->db->setData(statement.toStdString()); - ui->lWDeaTime->scrollToBottom(); - if(getMinimum(VecDea) > 0){ - ui->lBestZeitDea->setText(QString::number((double)getMinimum(VecDea)/1000)); - } - ui->lCurRoundDea->setText(QString::number((double)zeit/1000)); } } + ui->lWDeaTime->scrollToBottom(); + ui->lWDeaTime->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); } long WindowRace::getMinimum(std::vector a){ diff --git a/windowrace.h b/windowrace.h index 5285c6d..845c71e 100644 --- a/windowrace.h +++ b/windowrace.h @@ -11,6 +11,7 @@ #include "database.h" #include "windowrennliste.h" #include +#include "timemodel.h" using std::string; @@ -44,8 +45,10 @@ private: Counter counterDea; bool firstTimeShell; bool firstTimeDea; - std::vector VecShell; - std::vector VecDea; + QVector> VecShell; + QVector> VecDea; + TimeModel *timeModelShell; + TimeModel *timeModelDea; long getMinimum(std::vector a); QString timeWrapper(long zahl); long countdownValue; //in sec @@ -68,8 +71,8 @@ public slots: void go(); void ampelSlot(); void laufcheck(); - void deaSlot(); - void shellSlot(); + void deaSlot(int, int); + void shellSlot(int, int); void ReturnPress(); }; diff --git a/windowrace.ui b/windowrace.ui index 813d0a6..77fcc82 100644 --- a/windowrace.ui +++ b/windowrace.ui @@ -33,7 +33,7 @@ - 300 + 16777215 16777215 @@ -103,10 +103,10 @@ - + - 300 + 16777215 16777215 @@ -514,7 +514,7 @@ - 300 + 16777215 16777215 @@ -584,10 +584,10 @@ - + - 300 + 16777215 16777215 @@ -640,7 +640,7 @@ 0 0 840 - 20 + 25