diff --git a/hardwaresetup.cpp b/hardwaresetup.cpp index 713e51c..d8d48e1 100644 --- a/hardwaresetup.cpp +++ b/hardwaresetup.cpp @@ -319,14 +319,15 @@ void HardwareSetup::run() { sleep(1); cout << "Port can't be opened" << endl; fd = open(PORT_PATH, O_RDONLY); + if (this->stop) { + break; + } } - cout << "Port opened" << endl; - // prepare buffer - struct TransCheck * received; + std::shared_ptr received = + std::make_shared(); uint8_t uintBuf[sizeof(struct TransCheck)]; - void * voidBuf; - + // void * voidBuf; // configure among others non-canonical mode (important) struct termios config; tcgetattr(fd, &config); @@ -336,19 +337,25 @@ void HardwareSetup::run() { // read raw data to buffer ssize_t length = read(fd, uintBuf, sizeof(struct TransCheck)); - // flush existing data - if (static_cast(length) < sizeof(struct TransCheck)) { - tcflush(fd, TCIFLUSH); + if (this->stop == 0) { + cout << "Port opened" << endl; + // flush existing data + if (static_cast(length) < + sizeof(struct TransCheck)) { + tcflush(fd, TCIFLUSH); + } } - while (!this->stop) { // read data cout << "try to read data" << endl; read(this->fd, uintBuf, sizeof(struct TransCheck)); cout << "data read" << endl; - voidBuf = static_cast(uintBuf); - received = static_cast(voidBuf); + // voidBuf = static_cast(uintBuf); + memcpy(received.get(), uintBuf, sizeof(struct TransCheck)); + // voidBuf = static_cast(uintBuf); + + // received = static_cast(voidBuf); usleep(50000); // 150ms diff --git a/mainwindow.cpp b/mainwindow.cpp index ac264dc..939f6a0 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -43,7 +43,7 @@ MainWindow::MainWindow(QWidget * parent) QObject::connect(startRennen, SIGNAL(activated()), this, SLOT(WindowRennen())); - this->db = new DataBase; + this->db = std::make_shared(); // vector> daten = db->getData("select * from Fahrer", 2); } @@ -52,17 +52,18 @@ void MainWindow::closeEvent(QCloseEvent * event) { } MainWindow::~MainWindow() { - delete this->db; delete ui; } void MainWindow::WindowTraining() { - this->interfaceTraining = new Training(this, this->db); + // this->interfaceTraining = new Training(this, this->db); + this->interfaceTraining = std::make_shared(this, this->db); this->interfaceTraining->show(); } void MainWindow::NewWindowSettings() { - this->interfaceSettings = new WindowsSettings(this->db, this); + this->interfaceSettings = + std::make_shared(this->db.get(), this); this->interfaceSettings->show(); } @@ -107,15 +108,16 @@ void MainWindow::WindowRennen() { Q_UNUSED(e); } - this->interfaceRace = new WindowRace(this->db, this); + this->interfaceRace = std::make_shared(this->db, this); this->interfaceRace->show(); - this->interfaceRennliste = new WindowRennliste(this->db, this); + this->interfaceRennliste = + std::make_shared(this->db.get(), this); this->interfaceRennliste->show(); this->interfaceRace->setWindowRennliste(this->interfaceRennliste); - this->interfaceRennliste->setWindowRace(this->interfaceRace); + this->interfaceRennliste->setWindowRace(this->interfaceRace.get()); } void MainWindow::WindowEvaluation() { - this->interfaceEvaluation = new Evaluation(this->db); + this->interfaceEvaluation = std::make_shared(this->db.get()); this->interfaceEvaluation->show(); } diff --git a/mainwindow.h b/mainwindow.h index 947c393..ed07dc6 100644 --- a/mainwindow.h +++ b/mainwindow.h @@ -11,6 +11,7 @@ #include "windowrennliste.h" #include "windowrennliste2.h" #include "windowssettings.h" +#include namespace Ui { class MainWindow; @@ -20,7 +21,7 @@ class MainWindow : public QMainWindow { Q_OBJECT public: - explicit MainWindow(QWidget * parent = 0); + explicit MainWindow(QWidget * parent = nullptr); ~MainWindow(); public slots: @@ -30,14 +31,14 @@ class MainWindow : public QMainWindow { void WindowEvaluation(); private: - Training * interfaceTraining; + std::shared_ptr interfaceTraining; void closeEvent(QCloseEvent * event); - DataBase * db; + std::shared_ptr db; Ui::MainWindow * ui; - WindowRace * interfaceRace; - WindowsSettings * interfaceSettings; - WindowRennliste * interfaceRennliste; - Evaluation * interfaceEvaluation; + std::shared_ptr interfaceRace; + std::shared_ptr interfaceSettings; + std::shared_ptr interfaceRennliste; + std::shared_ptr interfaceEvaluation; }; #endif // MAINWINDOW_H diff --git a/training.cpp b/training.cpp index d006b7d..1b7ad1b 100644 --- a/training.cpp +++ b/training.cpp @@ -15,7 +15,7 @@ using std::endl; using std::string; using std::vector; -Training::Training(QWidget * parent, DataBase * db) +Training::Training(QWidget * parent, std::shared_ptr db) : QMainWindow(parent), ui(new Ui::Training) { ui->setupUi(this); this->db = db; @@ -38,12 +38,12 @@ Training::Training(QWidget * parent, DataBase * db) started = true; paused = false; - this->Hardware = new HardwareSetup; + this->Hardware = std::make_shared(); Hardware->start(); - QObject::connect(Hardware, SIGNAL(Dea(int, int)), this, + QObject::connect(Hardware.get(), SIGNAL(Dea(int, int)), this, SLOT(deaSlot(int, int))); - QObject::connect(Hardware, SIGNAL(Shell(int, int)), this, + QObject::connect(Hardware.get(), SIGNAL(Shell(int, int)), this, SLOT(shellSlot(int, int))); // QObject::connect(this->ui->pBNextRace, SIGNAL(clicked()), this, // SLOT(prepareNextRace())); QObject::connect(this->ui->pBStop, @@ -255,6 +255,7 @@ void Training::shellSlot(int time, int sector) { } void Training::closeEvent(QCloseEvent * event) { Hardware->setStop(); + this->close(); Q_UNUSED(event); } @@ -362,6 +363,5 @@ Training::~Training() { cout << "destr Training" << endl; this->Hardware->setStop(); usleep(1000000); - delete this->Hardware; delete ui; } diff --git a/training.h b/training.h index 732b0de..fba2e3a 100644 --- a/training.h +++ b/training.h @@ -5,6 +5,7 @@ #include "hardwaresetup.h" #include "timemodel.h" #include +#include #include #include @@ -16,7 +17,7 @@ class Training : public QMainWindow { Q_OBJECT public: - explicit Training(QWidget * parent, DataBase * db); + explicit Training(QWidget * parent, std::shared_ptr db); ~Training(); private: @@ -26,7 +27,7 @@ class Training : public QMainWindow { void closeEvent(QCloseEvent * event); bool started; - HardwareSetup * Hardware; + std::shared_ptr Hardware; bool firstTimeShell; bool firstTimeDea; QVector> VecShell; @@ -37,7 +38,7 @@ class Training : public QMainWindow { bool finished; Counter counterShell; Counter counterDea; - DataBase * db; + std::shared_ptr db; long minTimeOneRound; int minimumTime; diff --git a/windowrace.cpp b/windowrace.cpp index 96b53bd..638f3b5 100644 --- a/windowrace.cpp +++ b/windowrace.cpp @@ -15,7 +15,7 @@ using std::endl; using std::string; using std::vector; -WindowRace::WindowRace(DataBase * db, QWidget * parent) +WindowRace::WindowRace(std::shared_ptr db, QWidget * parent) : QMainWindow(parent), ui(new Ui::WindowRace) { ui->setupUi(this); this->ui->pBNextRace->setEnabled(false); @@ -27,12 +27,12 @@ WindowRace::WindowRace(DataBase * db, QWidget * parent) vector> res; res = db->getData("select dauer from renndauer", 1); - this->fahrzeit = res[0][0].toInt(); + this->fahrzeit = res.at(0).at(0).toInt(); string statement = "select minimumroundtime from rennen order by id DESC limit 1"; res = db->getData(statement, 1); - this->minimumTime = res[0][0].toInt(); + this->minimumTime = res.at(0).at(0).toInt(); // fill minSecTimes vector statement = "select minsec1, minsec2, minsec3, mindestRundenDauer from " @@ -40,10 +40,10 @@ WindowRace::WindowRace(DataBase * db, QWidget * parent) "DESC limit 1"; // cout << statement << endl; res = db->getData(statement, 4); - this->minSecTime.append(res[0][0].toInt()); - this->minSecTime.append(res[0][1].toInt()); - this->minSecTime.append(res[0][2].toInt()); - this->minSecTime.append(res[0][3].toInt()); + this->minSecTime.append(res.at(0).at(0).toInt()); + this->minSecTime.append(res.at(0).at(1).toInt()); + this->minSecTime.append(res.at(0).at(2).toInt()); + this->minSecTime.append(res.at(0).at(3).toInt()); QVectorHelper::minSec1 = this->minSecTime.at(0); QVectorHelper::minSec2 = this->minSecTime.at(1); @@ -55,28 +55,28 @@ WindowRace::WindowRace(DataBase * db, QWidget * parent) firstTimeShell = true; started = false; countdownValue = this->fahrzeit; - countdown = new Countdown; + countdown = std::make_shared(); - startAmpelThread = new Ampel; + startAmpelThread = std::make_shared(); ampelCounter = 0; ui->pBStart->setFocus(); paused = false; - Hardware = new HardwareSetup; + Hardware = std::make_shared(); Hardware->start(); - QObject::connect(Hardware, SIGNAL(Dea(int, int)), this, + QObject::connect(Hardware.get(), SIGNAL(Dea(int, int)), this, SLOT(deaSlot(int, int))); - QObject::connect(Hardware, SIGNAL(Shell(int, int)), this, + QObject::connect(Hardware.get(), SIGNAL(Shell(int, int)), this, SLOT(shellSlot(int, int))); QObject::connect(ui->pBStart, SIGNAL(clicked()), this, SLOT(go())); - QObject::connect(countdown, SIGNAL(CountdownUpdate()), this, + QObject::connect(countdown.get(), SIGNAL(CountdownUpdate()), this, SLOT(countdownUpdate())); - QObject::connect(startAmpelThread, SIGNAL(ampelUpdate()), this, + QObject::connect(startAmpelThread.get(), SIGNAL(ampelUpdate()), this, SLOT(ampelSlot())); - QObject::connect(startAmpelThread, SIGNAL(ampelUpdate()), this, + QObject::connect(startAmpelThread.get(), SIGNAL(ampelUpdate()), this, SLOT(laufcheck())); QObject::connect(ui->pBBreak, SIGNAL(clicked()), this, SLOT(breakCounter())); @@ -264,18 +264,19 @@ void WindowRace::stopClicked() { this->prepareNextRace(); } -void WindowRace::setWindowRennliste(WindowRennliste * ptrInstance) { +void WindowRace::setWindowRennliste( + std::shared_ptr ptrInstance) { this->wRennliste = ptrInstance; this->wRennlisteSeted = true; - QObject::connect(this->ui->pBNextRace, SIGNAL(clicked()), this->wRennliste, - SLOT(changeSelection())); + QObject::connect(this->ui->pBNextRace, SIGNAL(clicked()), + this->wRennliste.get(), SLOT(changeSelection())); } void WindowRace::setDriverAndCarId(vector vec) { - this->shellDriverId = vec[0].toInt(); - this->shellCarId = vec[1].toInt(); - this->deaDriverId = vec[2].toInt(); - this->deaCarId = vec[3].toInt(); + this->shellDriverId = vec.at(0).toInt(); + this->shellCarId = vec.at(1).toInt(); + this->deaDriverId = vec.at(2).toInt(); + this->deaCarId = vec.at(3).toInt(); } void WindowRace::closeEvent(QCloseEvent * event) { countdown->setStop(); @@ -359,11 +360,7 @@ WindowRace::~WindowRace() { this->startAmpelThread->setStop(); this->countdown->setStop(); this->Hardware->setStop(); - usleep(1000010); // eine Sekunde - - delete this->countdown; - delete this->Hardware; - delete this->startAmpelThread; + usleep(1000000); // 1 second // delete this->db; diff --git a/windowrace.h b/windowrace.h index 43c0739..53368dd 100644 --- a/windowrace.h +++ b/windowrace.h @@ -23,9 +23,10 @@ class WindowRace : public QMainWindow { Q_OBJECT public: - explicit WindowRace(DataBase * db, QWidget * parent = 0); + explicit WindowRace(std::shared_ptr db, + QWidget * parent = nullptr); ~WindowRace(); - void setWindowRennliste(WindowRennliste * ptrInstance); + void setWindowRennliste(std::shared_ptr ptrInstance); void setDriverAndCar(vector vec); void setDriverAndCarId(vector vec); @@ -37,8 +38,8 @@ class WindowRace : public QMainWindow { void closeEvent(QCloseEvent * event); bool started; - Countdown * countdown; - HardwareSetup * Hardware; + std::shared_ptr countdown; + std::shared_ptr Hardware; Ui::WindowRace * ui; Counter counterShell; Counter counterDea; @@ -51,12 +52,12 @@ class WindowRace : public QMainWindow { long getMinimum(std::vector a); QString timeWrapper(long zahl); long countdownValue; // in sec - Ampel * startAmpelThread; + std::shared_ptr startAmpelThread; int ampelCounter; bool paused; - DataBase * db; + std::shared_ptr db; bool finished; - WindowRennliste * wRennliste; + std::shared_ptr wRennliste; bool wRennlisteSeted; int fahrzeit; int renn_id;