added usb-racemode, database needs to be reconfigured and colors in timemodel needs to be adapted

This commit is contained in:
2017-09-09 19:26:48 +02:00
parent 8c635096ec
commit 631b1bc6c5
10 changed files with 213 additions and 101 deletions
+4 -2
View File
@@ -26,7 +26,8 @@ SOURCES += main.cpp\
database.cpp \ database.cpp \
windowrennliste.cpp \ windowrennliste.cpp \
training.cpp \ training.cpp \
timemodel.cpp timemodel.cpp \
qvectorhelper.cpp
HEADERS += \ HEADERS += \
mainwindow.h \ mainwindow.h \
@@ -39,7 +40,8 @@ HEADERS += \
database.h \ database.h \
windowrennliste.h \ windowrennliste.h \
training.h \ training.h \
timemodel.h timemodel.h \
qvectorhelper.h
FORMS += mainwindow.ui \ FORMS += mainwindow.ui \
windowrace.ui \ windowrace.ui \
+1 -1
View File
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QtCreatorProject> <!DOCTYPE QtCreatorProject>
<!-- Written by QtCreator 4.3.1, 2017-09-07T16:51:17. --> <!-- Written by QtCreator 4.4.0, 2017-09-09T12:01:57. -->
<qtcreator> <qtcreator>
<data> <data>
<variable>EnvironmentId</variable> <variable>EnvironmentId</variable>
+2 -37
View File
@@ -169,7 +169,7 @@ void HardwareSetup::run(){
while(!this->stop){ while(!this->stop){
usleep(100000); // 100ms usleep(100000); // 100ms
while(1){ //while(1){
nBytes = usb_control_msg(this->handle, nBytes = usb_control_msg(this->handle,
USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN, USB_TYPE_VENDOR | USB_RECIP_DEVICE | USB_ENDPOINT_IN,
USB_DATA_OUT, 0, 0, (char *)buffer, sizeof(buffer), 5000); USB_DATA_OUT, 0, 0, (char *)buffer, sizeof(buffer), 5000);
@@ -203,46 +203,11 @@ void HardwareSetup::run(){
// if(buffer[0].update != 0){ // if(buffer[0].update != 0){
// cout << "Got " << nBytes << " bytes: " << (int)buffer[2].time << ", " << (int)buffer[2].id << endl; // 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) if(nBytes < 0)
fprintf(stderr, "USB error: %sn", usb_strerror()); 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;
}
} }
} }
+37
View File
@@ -0,0 +1,37 @@
#include "qvectorhelper.h"
#include <qvector.h>
QVectorHelper::QVectorHelper()
{
}
int QVectorHelper::getCurTime(const QVector<int> x) {
if(x.size() >= 3){
return x.at(0)+ x.at(1) + x.at(2);
}
else{
return 9999;
}
}
int QVectorHelper::getMin(const QVector<QVector<int>> 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;
}
+13
View File
@@ -0,0 +1,13 @@
#ifndef QVECTORHELPER_H
#define QVECTORHELPER_H
#include <qvector.h>
class QVectorHelper
{
public:
QVectorHelper();
static int getCurTime(const QVector<int> x);
static int getMin(const QVector<QVector<int>> x);
};
#endif // QVECTORHELPER_H
+3 -1
View File
@@ -58,7 +58,9 @@ QColor TimeModel::getColor(const QVector<QVector<int>> data, const int col, cons
if(data.at(row).size() > col){ // check for enough cols if(data.at(row).size() > col){ // check for enough cols
QVector<int> compare; QVector<int> compare;
for(int i = 0; i <= row; i++){ 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)){ if(data.at(row).at(col) == getMin(compare)){
return QColor(Qt::green); return QColor(Qt::green);
+4 -3
View File
@@ -179,7 +179,8 @@ void Training::shellSlot(int time, int sector){
} }
} }
void Training::closeEvent(QCloseEvent *event){ void Training::closeEvent(QCloseEvent *event){
Hardware->setStop();
delete this->Hardware;
} }
void Training::deaSlot(int time, int sector){ void Training::deaSlot(int time, int sector){
@@ -218,10 +219,10 @@ void Training::deaSlot(int time, int sector){
//cout << "Time sec 3: " << time << endl; //cout << "Time sec 3: " << time << endl;
if(VecDea.last().size() == 2){ if(VecDea.last().size() == 2){
// cout << time << sector << endl; // cout << time << sector << endl;
VecDea[VecDea.size()-1].push_back(time); VecDea.last().push_back(time);
// add sum to vector // add sum to vector
VecDea[VecDea.size()-1].push_back(getCurTime(VecDea.last())); VecDea.last().push_back(getCurTime(VecDea.last()));
+135 -46
View File
@@ -7,6 +7,8 @@
#include <iostream> #include <iostream>
#include "unistd.h" #include "unistd.h"
#include <QKeySequence> #include <QKeySequence>
#include "qvectorhelper.h"
#include "timemodel.h"
using std::vector; using std::vector;
using std::string; using std::string;
@@ -50,8 +52,8 @@ WindowRace::WindowRace(DataBase *db, QWidget *parent) :
Hardware = new HardwareSetup; Hardware = new HardwareSetup;
Hardware->start(); Hardware->start();
QObject::connect(Hardware, SIGNAL(Dea()), this, SLOT(deaSlot())); QObject::connect(Hardware, SIGNAL(Dea(int, int)), this, SLOT(deaSlot(int, int)));
QObject::connect(Hardware, SIGNAL(Shell()), this, SLOT(shellSlot())); QObject::connect(Hardware, SIGNAL(Shell(int, int)), this, SLOT(shellSlot(int, int)));
QObject::connect(ui->pBStart, SIGNAL(clicked()), this, SLOT(go())); QObject::connect(ui->pBStart, SIGNAL(clicked()), this, SLOT(go()));
QObject::connect(countdown, SIGNAL(CountdownUpdate()), this, SLOT(countdownUpdate())); QObject::connect(countdown, SIGNAL(CountdownUpdate()), this, SLOT(countdownUpdate()));
QObject::connect(startAmpelThread, SIGNAL(ampelUpdate()), this, SLOT(ampelSlot())); QObject::connect(startAmpelThread, SIGNAL(ampelUpdate()), this, SLOT(ampelSlot()));
@@ -156,8 +158,8 @@ void WindowRace::prepareNextRace(){
this->firstTimeShell = true; this->firstTimeShell = true;
this->firstTimeDea = true; this->firstTimeDea = true;
this->ui->lWShellTime->clear(); // this->ui->lWShellTime->clear();
this->ui->lWDeaTime->clear(); // this->ui->lWDeaTime->clear();
this->ui->lBestZeitDea->setText(""); this->ui->lBestZeitDea->setText("");
this->ui->lBestZeitShell->setText(""); this->ui->lBestZeitShell->setText("");
@@ -167,6 +169,13 @@ void WindowRace::prepareNextRace(){
this->VecShell.clear(); this->VecShell.clear();
this->VecDea.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->finished = false;
this->paused = false; this->paused = false;
@@ -230,6 +239,8 @@ void WindowRace::setDriverAndCarId(vector<QString> vec){
} }
void WindowRace::closeEvent(QCloseEvent *event){ void WindowRace::closeEvent(QCloseEvent *event){
countdown->setStop();
QMessageBox msgBox; QMessageBox msgBox;
msgBox.setText("Wirklich schliessen?"); msgBox.setText("Wirklich schliessen?");
msgBox.setInformativeText(""); msgBox.setInformativeText("");
@@ -292,7 +303,7 @@ void WindowRace::countdownUpdate(){
} }
if(countdownValue <= -1){ if(countdownValue <= -1){
this->finished = true; 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); this->ui->pBNextRace->setEnabled(true);
} }
else{ else{
@@ -302,6 +313,7 @@ void WindowRace::countdownUpdate(){
} }
} }
WindowRace::~WindowRace() WindowRace::~WindowRace()
{ {
this->startAmpelThread->setStop(); this->startAmpelThread->setStop();
@@ -317,67 +329,144 @@ WindowRace::~WindowRace()
delete ui; delete ui;
} }
void WindowRace::shellSlot(){ void WindowRace::shellSlot(int time, int sector){
if(started && !paused && !this->finished){ if(started && !paused && !this->finished){
if(firstTimeShell){ if(firstTimeShell){
firstTimeShell = false; firstTimeShell = false;
counterShell.start(); // counterShell.start();
} }
else{ else{
long zeit = counterShell.getTime(); // long zeit = counterShell.getTime();
VecShell.push_back(zeit); switch(sector){
QListWidgetItem *item = new QListWidgetItem; case 1:
if(getMinimum(VecShell) == zeit && VecShell.size() > 1){ VecShell.append(QVector<int>());
item->setBackgroundColor(Qt::green); 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){ if(sector == 3){
item->setBackgroundColor(Qt::red);
this->firstTimeShell = true;
} }
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(); 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(started && !paused && !this->finished){
if(firstTimeDea){ if(firstTimeDea){
firstTimeDea = false; firstTimeDea = false;
counterDea.start(); counterDea.start();
} }
else{ else{
long zeit = counterDea.getTime();
VecDea.push_back(zeit); switch(sector){
QListWidgetItem *item = new QListWidgetItem; case 1:
if(getMinimum(VecDea) == zeit && VecDea.size() > 1){ VecDea.push_back(QVector<int>());
item->setBackgroundColor(Qt::green); 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<long> a){ long WindowRace::getMinimum(std::vector<long> a){
+7 -4
View File
@@ -11,6 +11,7 @@
#include "database.h" #include "database.h"
#include "windowrennliste.h" #include "windowrennliste.h"
#include <QMessageBox> #include <QMessageBox>
#include "timemodel.h"
using std::string; using std::string;
@@ -44,8 +45,10 @@ private:
Counter counterDea; Counter counterDea;
bool firstTimeShell; bool firstTimeShell;
bool firstTimeDea; bool firstTimeDea;
std::vector<long> VecShell; QVector<QVector<int>> VecShell;
std::vector<long> VecDea; QVector<QVector<int>> VecDea;
TimeModel *timeModelShell;
TimeModel *timeModelDea;
long getMinimum(std::vector<long> a); long getMinimum(std::vector<long> a);
QString timeWrapper(long zahl); QString timeWrapper(long zahl);
long countdownValue; //in sec long countdownValue; //in sec
@@ -68,8 +71,8 @@ public slots:
void go(); void go();
void ampelSlot(); void ampelSlot();
void laufcheck(); void laufcheck();
void deaSlot(); void deaSlot(int, int);
void shellSlot(); void shellSlot(int, int);
void ReturnPress(); void ReturnPress();
}; };
+7 -7
View File
@@ -33,7 +33,7 @@
<widget class="QGroupBox" name="gbShell"> <widget class="QGroupBox" name="gbShell">
<property name="maximumSize"> <property name="maximumSize">
<size> <size>
<width>300</width> <width>16777215</width>
<height>16777215</height> <height>16777215</height>
</size> </size>
</property> </property>
@@ -103,10 +103,10 @@
</layout> </layout>
</item> </item>
<item> <item>
<widget class="QListWidget" name="lWShellTime"> <widget class="QTableView" name="lWShellTime">
<property name="maximumSize"> <property name="maximumSize">
<size> <size>
<width>300</width> <width>16777215</width>
<height>16777215</height> <height>16777215</height>
</size> </size>
</property> </property>
@@ -514,7 +514,7 @@
<widget class="QGroupBox" name="gbDea"> <widget class="QGroupBox" name="gbDea">
<property name="maximumSize"> <property name="maximumSize">
<size> <size>
<width>300</width> <width>16777215</width>
<height>16777215</height> <height>16777215</height>
</size> </size>
</property> </property>
@@ -584,10 +584,10 @@
</layout> </layout>
</item> </item>
<item> <item>
<widget class="QListWidget" name="lWDeaTime"> <widget class="QTableView" name="lWDeaTime">
<property name="maximumSize"> <property name="maximumSize">
<size> <size>
<width>300</width> <width>16777215</width>
<height>16777215</height> <height>16777215</height>
</size> </size>
</property> </property>
@@ -640,7 +640,7 @@
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>840</width> <width>840</width>
<height>20</height> <height>25</height>
</rect> </rect>
</property> </property>
</widget> </widget>