diff --git a/obj/recorder-cmd.pro b/obj/recorder-cmd.pro index 26b22bd616d305ba23d48506ab296db64766bd31..232d796f69c8b58523f2e291f023045d709e627b 100644 --- a/obj/recorder-cmd.pro +++ b/obj/recorder-cmd.pro @@ -9,7 +9,8 @@ SOURCES += $$SRC_DIR/cmd/*.cpp \ HEADERS += $$SRC_DIR/cmd/*.h \ $$SRC_DIR/*.h -DEFINES *= QT_USE_QSTRINGBUILDER #converts + to % when building strings +DEFINES *= QT_USE_QSTRINGBUILDER #converts + to % when building strings #append macro +DEFINES += CONSOLE #add macro # opencv CONFIG += link_pkgconfig diff --git a/src/cam.cpp b/src/cam.cpp index b5f4f5e28faa688630e51c4f8702c2d62dea8548..65cdc247404afe6c815141cae60f5a5cef02000a 100644 --- a/src/cam.cpp +++ b/src/cam.cpp @@ -1,8 +1,9 @@ #include "cam.h" -#include "VmbCPP/SharedPointerDefines.h" #include "utils.h" #include "frameobserver.h" +#include "VmbCPP/SharedPointerDefines.h" + #include <chrono> #include <memory> //shared_ptr #include <unistd.h> @@ -13,8 +14,7 @@ using State = Cam::State; using namespace VmbCPP; -Cam::Cam(CameraPtr pCamera) : -IPrinter(), +Cam::Cam(CameraPtr pCamera) : IPrinter(), _cam(pCamera), _name(""), _state(disconnected), @@ -23,12 +23,6 @@ _payloadSize(0LL), _pixelFormat("") { name(); - - //vmb (core) -> ui (controller.console) - // QObject::connect(&core, SIGNAL(error(const QString&, const int&)), controller.console, SLOT(error(const QString&, const int&)), Qt::DirectConnection); - // QObject::connect(&core, SIGNAL(error(const int&)), controller.console, SLOT(error(const int&)), Qt::DirectConnection); - // QObject::connect(&core, SIGNAL(info(const QString&)), controller.console, SLOT(print(const QString&)), Qt::DirectConnection); - // QObject::connect(&core, SIGNAL(info(const QStringList&)), controller.console, SLOT(print(const QStringList&)), Qt::DirectConnection); } // Copy constructor @@ -45,16 +39,12 @@ _pixelFormat(other._pixelFormat) Cam::~Cam() { qDebug() << __FUNCTION__ << "():" << __LINE__ << "name:" << _name; - // if( _state == recording ) - // stopRecording(); + if( _state == recording ) + stopRecording(); - // if( _state == opened ) - // close(); + if( _state == opened ) + close(); // close() should be called automatically, when the last shared_ptr to the camera is destroyed - - // for ( auto cam : cameras ) - // cam->Close(); - } @@ -128,10 +118,12 @@ void Cam::startRecording(std::chrono::seconds dur) g( _cam->GetFeatureByName ("AcquisitionStart", pFeature ),"get AcquisitionStart"); g( pFeature->RunCommand(),"run AcquisitionStart"); + } catch (const std::runtime_error& xx) { qDebug() << "Exception caught: " << xx.what(); + return; } _state = recording; @@ -166,20 +158,16 @@ void Cam::stopRecording() g(_cam->RevokeAllFrames(),"revoke all frames"); for( FramePtr frame : _frames ) g(frame->UnregisterObserver(),"unregister observer"); + + _state = opened; } catch (const std::runtime_error& xx) - { - qDebug() << "Exception caught: " << xx.what(); - } - - _state = opened; + {} //ignore } // id,name,model,serial,interfaceID,state QString Cam::info() { - // qDebug() << "Camera Name: " << _name; - // qDebug() << "Camera State: " << _state; QString camInfo; try { diff --git a/src/cam.h b/src/cam.h index 7ec7a1930c5a25089ee80ede9fb22fa00c6c1167..4a82eb6d243b7c0f20edf0555464a7d23fd669de 100644 --- a/src/cam.h +++ b/src/cam.h @@ -45,11 +45,11 @@ public: void close(); void startRecording(std::chrono::minutes dur); void startRecording(std::chrono::seconds dur); //use as literals "1s, 1min" - void stopRecording(); QString info(); inline CameraPtr getCameraPtr() const { return _cam; } public slots: + void stopRecording(); void onCameraDisconnected(); private: diff --git a/src/cmd/console.cpp b/src/cmd/console.cpp index bec3933a7f034a5ece118dfeb6151bbacc315a34..6ea261683eb2c24e65db193aef8ffa14680788d8 100644 --- a/src/cmd/console.cpp +++ b/src/cmd/console.cpp @@ -6,9 +6,16 @@ #include <QDebug> #include <QCoreApplication> +#include <QTimer> + #include <iostream> #include <math.h> +Console* Console::getInstance() { + static Console* instance = new Console; // Static local variable ensures single instance + return instance; +} + void Console::listenKeys() { // listen to keyevents in endless loop @@ -105,13 +112,15 @@ void Console::printHelp() // ############################### // Controller -Controller::Controller() : console(new Console()) +Controller::Controller() { + auto console = Console::getInstance(); console->moveToThread(&thread); connect(&thread, &QThread::finished, console, &QObject::deleteLater); connect(this, &Controller::operate, console, &Console::listenKeys); connect(console, &Console::keyPressed, this, &Controller::keyPress); + QTimer::singleShot(0, this, SIGNAL(operate())); thread.start(); } @@ -124,7 +133,7 @@ Controller::~Controller() void Controller::keyPress(const QChar& key) { - + auto console = Console::getInstance(); qDebug().noquote() << __FUNCTION__ << ": " << key; if ( key.isDigit() ) { @@ -171,6 +180,4 @@ void Controller::keyPress(const QChar& key) break; } } - } - diff --git a/src/cmd/console.h b/src/cmd/console.h index b0d1c09a016483f75872bd83079a9af8e1fe3169..9946186ca423a11938b760734157a50b03bf63c3 100644 --- a/src/cmd/console.h +++ b/src/cmd/console.h @@ -11,23 +11,33 @@ class Console : public QObject { Q_OBJECT - public slots: - void listenKeys(); - QString getLine(); - int getNumber(); - void error(const QString&,const int&); - void error(const int&); - void print(const QString&); - void print(const QStringList&); - - void printVersion(); - void printHelp(); - // ... +public: + static Console* getInstance(); + + Console(const Console&) = delete; // Delete copy constructor and assignment operator to prevent copying + Console& operator=(const Console&) = delete; + +private: + Console() : QObject(){}; //// Private constructor to prevent instantiation + +public slots: + void listenKeys(); + QString getLine(); + int getNumber(); + void error(const QString&,const int&); + void error(const int&); + void print(const QString&); + void print(const QStringList&); + + void printVersion(); + void printHelp(); + // ... + +signals: + void keyPressed(const QChar &key); + // void lineEntered(const QString&); + // void numberEntered(const int&); - signals: - void keyPressed(const QChar &key); - // void lineEntered(const QString&); - // void numberEntered(const int&); }; @@ -39,7 +49,6 @@ class Controller : public QObject public: Controller(); ~Controller(); - Console* console; public slots: void keyPress(const QChar &); diff --git a/src/cmd/main.cpp b/src/cmd/main.cpp index ff19440afb5d0ed118be4578b088a3e7e050c426..8b9c5ea43668c6061cc8f4dca9d038b9f3ac96d1 100644 --- a/src/cmd/main.cpp +++ b/src/cmd/main.cpp @@ -24,19 +24,8 @@ int main(int argc, char *argv[]) QObject::connect(&controller, &Controller::startRecording, &core, &Core::startRecording); QObject::connect(&controller, &Controller::stopRecording, &core, &Core::stopRecording); QObject::connect(&controller, &Controller::setDuration, &core, &Core::setDuration); - - - // XXX add new func - //vmb (core) -> ui (controller.console) - QObject::connect(&core, SIGNAL(error(const QString&, const int&)), controller.console, SLOT(error(const QString&, const int&)), Qt::DirectConnection); - QObject::connect(&core, SIGNAL(error(const int&)), controller.console, SLOT(error(const int&)), Qt::DirectConnection); - QObject::connect(&core, SIGNAL(info(const QString&)), controller.console, SLOT(print(const QString&)), Qt::DirectConnection); - QObject::connect(&core, SIGNAL(info(const QStringList&)), controller.console, SLOT(print(const QStringList&)), Qt::DirectConnection); - // sa Controller() for delegating to Console - - QTimer::singleShot(0, &controller, SIGNAL(operate())); // QTimer::singleShot(10, controller.console, SLOT(printVersion()), Qt::DirectConnection); // for testing // QTimer::singleShot(200, &core, SLOT(openCam())); // for testing diff --git a/src/core.cpp b/src/core.cpp index 4ba5ace825926e8fca3a79b6ea36c49152a04c88..9f0110cc686f9feed6f04b168bcc7e39bdd9a8db 100644 --- a/src/core.cpp +++ b/src/core.cpp @@ -21,8 +21,7 @@ using namespace VmbCPP; using namespace std::chrono_literals; //seconds, minutes, hours -Core::Core() : - IPrinter(), +Core::Core() : IPrinter(), _apiStarted( false ), _sys( VmbSystem::GetInstance() ), // create and get Vimba singleton _cameras( CamPtrVector() ), @@ -82,7 +81,8 @@ void Core::updateCameras() } if (!found) { - _cameras.push_back(std::make_shared<Cam>(vcam)); + + _cameras.push_back(std::make_shared<Cam>(vcam)); //create Cam() from CameraPtr vcam print("cam connected"); //vcam->GetName(), threadsPerCam } diff --git a/src/core.h b/src/core.h index cb539ba9887516a9672d1d2d05621be245dfc9d9..4e7aec9e6637adec01d8c955ac9b0815e3504a69 100644 --- a/src/core.h +++ b/src/core.h @@ -14,6 +14,7 @@ using std::chrono::seconds; class Core : public IPrinter { + Q_OBJECT public: diff --git a/src/iprinter.cpp b/src/iprinter.cpp index 5ba85151794b7d169d6853a2a21fdc03e7d3cd6d..e93fe636aba1bac82d8606378256cf85faa724d5 100644 --- a/src/iprinter.cpp +++ b/src/iprinter.cpp @@ -1,7 +1,20 @@ #include "iprinter.h" +#ifdef CONSOLE +#include "cmd/console.h" +#endif + IPrinter::IPrinter(QObject *parent) : QObject(parent) { +#ifdef CONSOLE //defined in .pro + //vmb (core, cam) -> ui (controller.console) + Console* console = Console::getInstance(); + connect(this, SIGNAL(error(const QString&, const int&)), console, SLOT(error(const QString&, const int&)), Qt::DirectConnection); + connect(this, SIGNAL(error(const int&)), console, SLOT(error(const int&)), Qt::DirectConnection); + connect(this, SIGNAL(info(const QString&)), console, SLOT(print(const QString&)), Qt::DirectConnection); + connect(this, SIGNAL(info(const QStringList&)), console, SLOT(print(const QStringList&)), Qt::DirectConnection); +#endif + } IPrinter::~IPrinter()