Skip to content
Snippets Groups Projects
Commit 7c3aa759 authored by am0ebe's avatar am0ebe
Browse files

1. add camobserver to listen when cam is plugged/unplugged 2. rearrange files...

1. add camobserver to listen when cam is plugged/unplugged 2. rearrange files in the project (adjust includes/.pro)
parent 04d12220
No related branches found
No related tags found
No related merge requests found
...@@ -6,16 +6,18 @@ CONFIG += c++20 warn_on console ...@@ -6,16 +6,18 @@ CONFIG += c++20 warn_on console
SOURCES += \ SOURCES += \
../src/cmd/main.cpp \ ../src/cmd/main.cpp \
../src/cmd/console.cpp \ ../src/cmd/console.cpp \
../src/cmd/core.cpp \ ../src/core.cpp \
../src/cmd/frameObserver.cpp \ ../src/frameobserver.cpp \
../src/cmd/frameProcessor.cpp \ ../src/frameprocessor.cpp \
../src/camobserver.cpp \
../src/utils.cpp ../src/utils.cpp
HEADERS += \ HEADERS += \
../src/cmd/console.h \ ../src/cmd/console.h \
../src/cmd/core.h \ ../src/core.h \
../src/cmd/frameObserver.h \ ../src/frameobserver.h \
../src/cmd/frameProcessor.h \ ../src/frameprocessor.h \
../src/camobserver.h \
../src/utils.h ../src/utils.h
DEFINES *= QT_USE_QSTRINGBUILDER #converts + to % when building strings DEFINES *= QT_USE_QSTRINGBUILDER #converts + to % when building strings
...@@ -24,7 +26,7 @@ DEFINES *= QT_USE_QSTRINGBUILDER #converts + to % when building strings ...@@ -24,7 +26,7 @@ DEFINES *= QT_USE_QSTRINGBUILDER #converts + to % when building strings
CONFIG += link_pkgconfig CONFIG += link_pkgconfig
PKGCONFIG += opencv4 PKGCONFIG += opencv4
# Vimba # VimbaX
INCLUDEPATH += ../vimbax/api/include INCLUDEPATH += ../vimbax/api/include
VIMBA_LIB_DIR = ../lib VIMBA_LIB_DIR = ../lib
LIBS += -L$${VIMBA_LIB_DIR} -lVmbC -lVmbCPP -Wl,-rpath,\'\$$ORIGIN\' LIBS += -L$${VIMBA_LIB_DIR} -lVmbC -lVmbCPP -Wl,-rpath,\'\$$ORIGIN\'
...@@ -41,4 +43,4 @@ TARGET = cmd ...@@ -41,4 +43,4 @@ TARGET = cmd
DESTDIR = ../bin DESTDIR = ../bin
QMAKE_CLEAN += .cache/clangd/index/* .qmake.stash $${DESTDIR}/*jpg QMAKE_CLEAN += .cache/clangd/index/* .qmake.stash $${DESTDIR}/*jpg
#cant rm dir here, so added in project file #cant rm dir here, so added in sublime-project file
#include "camobserver.h"
#include <VmbCPP/Camera.h>
#include <VmbCPP/VmbCPPCommon.h> //enum
#include <QDebug>
using namespace VmbCPP;
//2D XXX Code to determine the open state
void CamObserver::CameraListChanged(CameraPtr pCam, UpdateTriggerType reason)
{
if( UpdateTriggerPluggedIn == reason || UpdateTriggerPluggedOut == reason )
{
qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << "";
emit cameraListChanged();
}
else // UpdateTriggerOpenStateChanged
{
emit cameraOpenStateChanged(pCam);
qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << "";
}
}
#pragma once
#include <QObject>
#include <VmbCPP/ICameraListObserver.h>
namespace VmbCPP
{
// observer that reacts on camera list changes
class CamObserver : public QObject, public ICameraListObserver
{
Q_OBJECT
signals:
void cameraListChanged();
void cameraOpenStateChanged(CameraPtr);
public:
void CameraListChanged(CameraPtr, UpdateTriggerType);
};
}
...@@ -68,8 +68,8 @@ void Console::printHelp() ...@@ -68,8 +68,8 @@ void Console::printHelp()
str << "HELP!" str << "HELP!"
<< "-------------------------------" << "-------------------------------"
<< "h: print this help" << "h: print this help"
<< "1-9: select camera" << "1-9: set camera"
<< "o: open selected camera" << "o: open current camera"
// XXX add new func // XXX add new func
<< "r: start recording" << "r: start recording"
<< "t: stop recording" << "t: stop recording"
...@@ -106,7 +106,7 @@ void Controller::keyPress(const QChar& key) ...@@ -106,7 +106,7 @@ void Controller::keyPress(const QChar& key)
qDebug().noquote() << __FUNCTION__ << ": " << key; qDebug().noquote() << __FUNCTION__ << ": " << key;
if ( key.isDigit() ) if ( key.isDigit() )
{ {
emit selectCam(key.digitValue()-1); emit setCam(key.digitValue()-1);
} }
else else
{ {
......
...@@ -46,7 +46,7 @@ class Controller : public QObject ...@@ -46,7 +46,7 @@ class Controller : public QObject
// to core // to core
void listCams(); void listCams();
void openCam(); void openCam();
void selectCam(int); void setCam(int);
void startRecording(); void startRecording();
void stopRecording(); void stopRecording();
......
#include "../utils.h"
#include "console.h" #include "console.h"
#include "core.h" #include "../core.h"
#include "qnamespace.h" #include "../utils.h"
// #include "qnamespace.h"
#include <QCoreApplication> #include <QCoreApplication>
#include <QCommandLineParser>
#include <QTimer> #include <QTimer>
int main(int argc, char *argv[]) int main(int argc, char *argv[])
...@@ -20,7 +19,7 @@ int main(int argc, char *argv[]) ...@@ -20,7 +19,7 @@ int main(int argc, char *argv[])
//ui (controller) -> vmb (core) //ui (controller) -> vmb (core)
QObject::connect(&controller, &Controller::listCams, &core, &Core::listCams); QObject::connect(&controller, &Controller::listCams, &core, &Core::listCams);
QObject::connect(&controller, &Controller::openCam, &core, &Core::openCam); QObject::connect(&controller, &Controller::openCam, &core, &Core::openCam);
QObject::connect(&controller, &Controller::selectCam, &core, &Core::selectCam); QObject::connect(&controller, &Controller::setCam, &core, &Core::setCam);
QObject::connect(&controller, &Controller::startRecording, &core, &Core::startRecording); QObject::connect(&controller, &Controller::startRecording, &core, &Core::startRecording);
QObject::connect(&controller, &Controller::stopRecording, &core, &Core::stopRecording); QObject::connect(&controller, &Controller::stopRecording, &core, &Core::stopRecording);
// XXX add new func // XXX add new func
...@@ -35,7 +34,7 @@ int main(int argc, char *argv[]) ...@@ -35,7 +34,7 @@ int main(int argc, char *argv[])
QTimer::singleShot(0, &controller, SIGNAL(operate())); QTimer::singleShot(0, &controller, SIGNAL(operate()));
// QTimer::singleShot(10, controller.console, SLOT(printVersion()), Qt::DirectConnection); // for testing // QTimer::singleShot(10, controller.console, SLOT(printVersion()), Qt::DirectConnection); // for testing
QTimer::singleShot(30, &core, SLOT(listCams())); // for testing QTimer::singleShot(30, &core, SLOT(listCams())); // for testing
QTimer::singleShot(200, &core, SLOT(openCam())); // for testing // QTimer::singleShot(200, &core, SLOT(openCam())); // for testing
return a.exec(); return a.exec();
} }
#include "core.h" #include "core.h"
#include "frameobserver.h" #include "frameobserver.h"
#include "../utils.h" #include "camobserver.h"
#include "utils.h"
#include <iostream> #include <iostream>
#include <vector> #include <vector>
#include "VmbC/VmbCommonTypes.h" #include <VmbC/VmbCommonTypes.h>
#include "VmbCPP/SharedPointerDefines.h" #include <VmbCPP/SharedPointerDefines.h>
#include <VmbCPP/VmbCPP.h> #include <VmbCPP/VmbCPP.h>
#include <VmbCPP/Camera.h> #include <VmbCPP/Camera.h>
...@@ -28,33 +29,20 @@ Core::Core() : ...@@ -28,33 +29,20 @@ Core::Core() :
// frames(FramePtrVector(QThread::idealThreadCount())), // frames(FramePtrVector(QThread::idealThreadCount())),
pixelFormat("") pixelFormat("")
{ {
if ( f(sys.Startup()) ) QTimer::singleShot(0, this, SLOT(init())); //delayed init to allow connections to be established
{
qDebug() << "Couldn't initialize API";
exit(VmbErrorApiNotStarted);
}
qDebug() << "init API :)"; //emitting signals in ctor doesnt Work, cuz their not connected yet
apiStarted = true;
initCams();
} }
void Core::initCams() void Core::init()
{ {
if ( f(sys.GetCameras( cameras ))) if ( f(sys.Startup(), "init API") )
{ exit(VmbErrorApiNotStarted);
qDebug() << "No cameras";
} apiStarted = true;
else
{ auto camObserver = new CamObserver();
qDebug().noquote() << QString("found %1 cameras").arg(cameras.size()); connect( camObserver, SIGNAL(cameraListChanged()), this, SLOT(listCams()) );
if (cameras.size()) f(sys.RegisterCameraListObserver(ICameraListObserverPtr(camObserver)), "register cam observer");
{
camIdx = 0; //dflt to 1rst
curCam = cameras.at(camIdx);
}
}
} }
Core::~Core() Core::~Core()
...@@ -67,11 +55,6 @@ Core::~Core() ...@@ -67,11 +55,6 @@ Core::~Core()
sys.Shutdown(); sys.Shutdown();
} }
CameraPtr Core::cam()
{
return cameras.at(camIdx);
}
//2d //2d
// need to update open / closed states. // need to update open / closed states.
//initially cams are closed, right? XXX how to find out? //initially cams are closed, right? XXX how to find out?
...@@ -82,50 +65,61 @@ void Core::listCams() ...@@ -82,50 +65,61 @@ void Core::listCams()
if( !apiStarted ) if( !apiStarted )
return; return;
// id,name,model,serial,interfaceID,state if ( f(sys.GetCameras( cameras ), "get cameras") )
QStringList allCamInfo; return;
auto idx=0UL;
for ( auto camera : cameras ) qDebug().noquote() << QString("found %1 camera(s)").arg(cameras.size());
if (cameras.size())
{ {
std::string str; if( camIdx == -1UL || camIdx >= cameras.size() )
QString curCamInfo; {
setCam(0);
}
curCamInfo += QString::number(idx+1) + ". "; // id,name,model,serial,interfaceID,state
QStringList allCamInfo;
auto idx=0UL;
for ( auto camera : cameras )
{
std::string str;
QString curCamInfo;
if (idx == camIdx ) curCamInfo += QString::number(idx+1) + ". ";
curCamInfo += "[*]" + DELIM;
else
curCamInfo += "[ ]" + DELIM;
if (idx == camIdx )
curCamInfo += "[*]" + DELIM;
else
curCamInfo += "[ ]" + DELIM;
if( f((camera->GetID( str )))) continue;
curCamInfo += QString::fromStdString(str) + DELIM;
if( f((camera->GetName( str )))) continue; if( f((camera->GetID( str )))) continue;
curCamInfo += QString::fromStdString(str) + DELIM; curCamInfo += QString::fromStdString(str) + DELIM;
if( f((camera->GetModel( str )))) continue; if( f((camera->GetName( str )))) continue;
curCamInfo += QString::fromStdString(str) + DELIM; curCamInfo += QString::fromStdString(str) + DELIM;
if( f((camera->GetSerialNumber( str )))) continue; if( f((camera->GetModel( str )))) continue;
curCamInfo += QString::fromStdString(str) + DELIM; curCamInfo += QString::fromStdString(str) + DELIM;
if( f((camera->GetInterfaceID( str )))) continue; if( f((camera->GetSerialNumber( str )))) continue;
curCamInfo += QString::fromStdString(str) + DELIM; curCamInfo += QString::fromStdString(str) + DELIM;
// curCamInfo += "closed";//xxx if( f((camera->GetInterfaceID( str )))) continue;
curCamInfo += QString::fromStdString(str) + DELIM;
allCamInfo << curCamInfo; // curCamInfo += "closed";//xxx
idx++;
} allCamInfo << curCamInfo;
idx++;
}
if( !cameras.size())
emit error(errNoCams);
else
{
emitPrint("found Cameras",cameras.size()); emitPrint("found Cameras",cameras.size());
emit print(allCamInfo); emit print(allCamInfo);
} }
else
{
emit error(errNoCams);
}
} }
bool Core::f(const VmbErrorType& ret, const QString& msg) bool Core::f(const VmbErrorType& ret, const QString& msg)
...@@ -184,19 +178,23 @@ bool Core::openCam() ...@@ -184,19 +178,23 @@ bool Core::openCam()
return f(curCam->Open( VmbAccessModeFull ),"open cam #"+QString::number(camIdx+1)); return f(curCam->Open( VmbAccessModeFull ),"open cam #"+QString::number(camIdx+1));
} }
void Core::selectCam(const int& camIdx) CameraPtr Core::cam()
{
return curCam;
}
void Core::setCam(const int& newCamIdx)
{ {
try try
{ {
curCam = cameras.at(camIdx); camIdx = newCamIdx;
curCam = cameras.at(newCamIdx);
emitPrint("set cam",newCamIdx+1);
} }
catch (const std::out_of_range& oor) catch (const std::out_of_range& oor)
{ {
emit error(errCamIdx); emit error(errCamIdx);
return;
} }
emitPrint("camSelected",camIdx);
} }
/* Does: /* Does:
...@@ -215,6 +213,9 @@ void Core::selectCam(const int& camIdx) ...@@ -215,6 +213,9 @@ void Core::selectCam(const int& camIdx)
*/ */
void Core::startRecording() void Core::startRecording()
{ {
if( !curCam )
return;
FeaturePtr pFeature; FeaturePtr pFeature;
try try
...@@ -259,6 +260,9 @@ void Core::startRecording() ...@@ -259,6 +260,9 @@ void Core::startRecording()
*/ */
void Core::stopRecording() void Core::stopRecording()
{ {
if( !curCam )
return;
FeaturePtr pFeature; FeaturePtr pFeature;
try try
{ {
......
...@@ -25,12 +25,15 @@ class Core : public QObject ...@@ -25,12 +25,15 @@ class Core : public QObject
public slots: public slots:
void listCams(); void listCams();
bool openCam(); bool openCam();
void selectCam(const int &); void setCam(const int &);
CameraPtr cam(); // make getter setter -> rm curCam and camIdx //or make Camera class wrapper CameraPtr cam(); // make getter setter -> rm curCam and camIdx //or make Camera class wrapper
void startRecording(); void startRecording();
void stopRecording(); void stopRecording();
private slots:
void init();
signals: signals:
void print(const QString &); void print(const QString &);
void print(const QStringList&); void print(const QStringList&);
...@@ -44,7 +47,6 @@ class Core : public QObject ...@@ -44,7 +47,6 @@ class Core : public QObject
void emitPrint(const QString&, const VmbInt64_t& v); void emitPrint(const QString&, const VmbInt64_t& v);
void emitPrint(const QString&, const std::string& v=""); void emitPrint(const QString&, const std::string& v="");
void initCams();
void getCamInfo( const CameraPtr & ); void getCamInfo( const CameraPtr & );
bool apiStarted; bool apiStarted;
......
#include "frameobserver.h" #include "frameobserver.h"
#include "frameprocessor.h" #include "frameprocessor.h"
#include "qnamespace.h"
#include "qthread.h"
#include <VmbCPP/SharedPointerDefines.h> #include <VmbCPP/SharedPointerDefines.h>
#include <VmbCPP/Camera.h> #include <VmbCPP/Camera.h>
// #include "qnamespace.h"
#include <QDebug> #include <QDebug>
#include <QThread> #include <QThread>
#include <QObject> #include <QObject>
......
File moved
...@@ -42,8 +42,7 @@ void FrameProcessor::processFrame() ...@@ -42,8 +42,7 @@ void FrameProcessor::processFrame()
if (pixelFormat != VmbPixelFormatRgb8) if (pixelFormat != VmbPixelFormatRgb8)
{ {
qWarning() << "Pixel format is not RGB8, 2D: implement convert func ... "; qWarning() << "⚠️ Pixel format is not RGB8, 2D: implement convert func ... ";
return;
} }
qDebug() << "Frame received: " << width << "x" << height << "px, format: " << pixelFormat << ", timestamp: " << timestamp; qDebug() << "Frame received: " << width << "x" << height << "px, format: " << pixelFormat << ", timestamp: " << timestamp;
......
File moved
#include <VmbCPP/VmbCPP.h>
#include <iostream>
#include <vector>
#include <opencv2/core.hpp>
#include <opencv2/imgcodecs.hpp>
#include <opencv2/highgui.hpp>
using std::cout;
using std::endl;
// using namespace VmbCPP;
// read_and_display_img("/home/sugu/code/c++/opencv_quark/starry_night.png"); //make HOME user independent
// int read_and_display_img( cv::String img_with_path )
// {
// std::vector<int> vec;
// cv::Mat image;
// image = imread(img_with_path, cv::IMREAD_COLOR); // Read the file
// if( image.empty() ) // Check for invalid input
// {
// cout << "Could not open or find the image" << std::endl ;
// return -1;
// }
// namedWindow( "Display window", cv::WINDOW_AUTOSIZE ); // Create a window for display.
// imshow( "Display window", image ); // Show our image inside it.
// cv::waitKey(0); // Wait for a keystroke in the window
// return 0;
// }
#include "VmbCPP/VimbaCPP.h"
#include "VmbC/VimbaC.h"
#include "VmbCPP/Frame.h"
namespace VmbCPP {
// Constructor for the FrameObserver
class FrameObserver::FrameObserver( CameraPtr pCamera ) : IFrameObserver ( pCamera ){}
// Frame callback notifies about incoming frames
void FrameObserver::FrameReceived ( const FramePtr pFrame )
{
// Send notification to working thread
// Do not apply image processing within this callback ( performance )
// When the frame has been processed , requeue it
m_pCamera -> QueueFrame ( pFrame );
}
void Vimba::RunExample (void)
{
VmbSystem &sys = VmbSystem :: GetInstance (); // Create and get Vimba singleton
CameraPtrVector cameras ; // Holds camera handles
CameraPtr camera ;
VmbInt64_t nPLS; // Payload size value
FeaturePtr pFeature ; // Generic feature pointer
FramePtrVector frames (15); // Frame array
// Start the API , get and open cameras
sys.Startup (); // = VmbErrorSuccess?
sys.GetCameras( cameras ); // = VmbErrorSuccess?
camera = cameras [0]; //iterate over all cams -> auto it = cameras.begin()... (*iter)->GetName( name )==success
camera ->Open( VmbAccessModeFull );
// Get the image size for the required buffer
// Allocate memory for frame buffer
// Register frame observer / callback for each frame
// Announce frame to the API
camera -> GetFeatureByName (" PayloadSize ", pFeature );
pFeature -> GetValue (nPLS );
for( FramePtrVector :: iterator iter= frames .begin (); frames .end () != iter; ++ iter)
{
(* iter ).reset(new Frame (nPLS));
(* iter)-> RegisterObserver ( IFrameObserverPtr (new FrameObserver ( camera )));
camera -> AnnounceFrame (* iter );
}
// Start the capture engine (API)
camera -> StartCapture ();
for( FramePtrVector :: iterator iter= frames .begin (); frames .end ()!= iter; ++ iter)
{
// Put frame into the frame queue
camera->QueueFrame (* iter );
}
// Start the acquisition engine ( camera )
camera -> GetFeatureByName (" AcquisitionStart ", pFeature );
pFeature -> RunCommand ();
// Program runtime , e.g., Sleep (2000);
// Stop the acquisition engine ( camera )
camera -> GetFeatureByName (" AcquisitionStop ", pFeature );
pFeature -> RunCommand ();
// Stop the capture engine (API)
// Flush the frame queue
// Revoke all frames from the API
camera -> EndCapture ();
camera -> FlushQueue ();
camera -> RevokeAllFrames ();
for( FramePtrVector :: iterator iter= frames .begin (); frames .end ()!= iter; ++ iter)
{
// Unregister the frame observer / callback
(* iter)-> UnregisterObserver ();
}
camera ->Close ();
sys.Shutdown (); // Always pair sys. Startup and sys. Shutdown
}
} // namespace VmbCPP
...@@ -64,7 +64,8 @@ const QString errorCodeToMessage( VmbError_t err ) ...@@ -64,7 +64,8 @@ const QString errorCodeToMessage( VmbError_t err )
// ------------------------------- own errCodes ------------------------------- // ------------------------------- own errCodes -------------------------------
case errCamIdx: msg += "Camera index out of range."; break; case errCamIdx: msg += "Camera index out of range."; break;
case errNoCams: msg += "no cameras found."; break; case errNoCams: msg += "no cameras found."; break;
// xx = 12, //!< Unexpected fault in VmbC or driver case errRegisterCamObserver: msg += "cant register camobserver."; break;
// ... add more
} }
return msg; return msg;
} }
......
...@@ -18,6 +18,7 @@ typedef enum camtronErr ...@@ -18,6 +18,7 @@ typedef enum camtronErr
{ {
errCamIdx = 10, //!< camera index out of range errCamIdx = 10, //!< camera index out of range
errNoCams = 11, //!< no cameras found errNoCams = 11, //!< no cameras found
errRegisterCamObserver = 12, //!< cant register camobserver
// errUn = 12, //!< Unexpected fault in VmbC or driver // errUn = 12, //!< Unexpected fault in VmbC or driver
} camtronErr; } camtronErr;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment