From baf20feef3380d7ac1193f54ecf761454eb466c6 Mon Sep 17 00:00:00 2001
From: am0ebe <am0ebe@gmx.de>
Date: Wed, 3 Apr 2024 16:50:43 +0200
Subject: [PATCH] more stuff

---
 obj/recorder-cmd.pro   |  2 ++
 src/cam.cpp            | 38 ++++++++++++--------------------------
 src/cam.h              |  4 +---
 src/camobserver.cpp    |  4 ++--
 src/cmd/console.cpp    | 11 ++++++-----
 src/cmd/main.cpp       |  4 ++--
 src/core.cpp           | 28 +++++++++++++---------------
 src/frameprocessor.cpp | 13 ++++++++++---
 src/utils.cpp          | 11 +++++++++--
 9 files changed, 57 insertions(+), 58 deletions(-)

diff --git a/obj/recorder-cmd.pro b/obj/recorder-cmd.pro
index 232d796..6459685 100644
--- a/obj/recorder-cmd.pro
+++ b/obj/recorder-cmd.pro
@@ -12,6 +12,8 @@ HEADERS += 	$$SRC_DIR/cmd/*.h \
 DEFINES *= QT_USE_QSTRINGBUILDER		#converts + to % when building strings 	#append macro
 DEFINES += CONSOLE 	#add macro
 
+#DEFINES += DEBUG
+
 # opencv
 CONFIG += link_pkgconfig
 PKGCONFIG += opencv4
diff --git a/src/cam.cpp b/src/cam.cpp
index 83435c6..fc7cf1d 100644
--- a/src/cam.cpp
+++ b/src/cam.cpp
@@ -17,7 +17,6 @@ using namespace std::chrono;
 
 Cam::Cam(CameraPtr pCamera) : IPrinter(),
 _cam(pCamera),
-_name(""),
 _id(""),
 _state(closed),
 _frames(FramePtrVector(threadsPerCam())),
@@ -27,20 +26,21 @@ _timer(new QTimer(this))
 {
 	_timer->setSingleShot(true);
 	connect(_timer, SIGNAL(timeout()), this, SLOT(stopRecording()));
-	name();
+	id();
 }
 
 // Copy constructor
 Cam::Cam(const Cam& other) :
 IPrinter(),
 _cam(other._cam),
-_name(other._name),
 _id(other._id),
 _state(other._state),
 _frames(other._frames),
 _payloadSize(other._payloadSize),
 _pixelFormat(other._pixelFormat)
-{}
+{
+	qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << "copy constructor";
+}
 
 Cam::~Cam()
 {
@@ -51,17 +51,6 @@ Cam::~Cam()
 	// close() should be called automatically, when the last shared_ptr to the camera is destroyed
 }
 
-QString Cam::name()
-{
-	if( _name.isEmpty() )
-	{
-		std::string str;
-		f(_cam->GetName( str ));
-		_name = QString::fromStdString(str);
-	}
-	return _name;
-}
-
 QString Cam::id()
 {
 	if( _id.isEmpty() )
@@ -80,15 +69,14 @@ State Cam::state() const
 
 void Cam::open()
 {
-	f(_cam->Open( VmbAccessModeFull ),"open cam #"+_name);
+	f(_cam->Open( VmbAccessModeFull ),"open cam #"+_id);
 	//state change will be handled by CamObserver::CameraStateChanged
 }
 
 void Cam::close()
 {
-	f(_cam->Close(),"close cam #"+_name);
-
-
+	f(_cam->Close(),"close cam #"+_id);
+	//state change will be handled by CamObserver::CameraStateChanged
 }
 
 /* Does:
@@ -110,9 +98,10 @@ void Cam::startRecording(seconds dur)
 	{
 		FeaturePtr pFeature;
 
-		g( _cam->GetFeatureByName ("PayloadSize", pFeature ),"get PayloadSize");
+		g( _cam->GetFeatureByName ("PayloadSize", pFeature ));
 		g( pFeature->GetValue (_payloadSize));
 		print("PayloadSize",_payloadSize);
+		//XXX adjust packetSize ??
 
 		g( _cam->GetFeatureByName("PixelFormat", pFeature));
 		g( pFeature->GetValue(_pixelFormat));
@@ -149,10 +138,7 @@ void Cam::startRecording(seconds dur)
 
 void Cam::startRecording(minutes dur)
 {
-	// Convert to seconds
-	auto dur_s = duration_cast<seconds>(dur);
-	startRecording(dur_s);
-
+	startRecording(duration_cast<seconds>(dur)); // Convert to seconds
 }
 
 /* Does:
@@ -235,13 +221,13 @@ QString Cam::stateToString(const State& state)
 
 void Cam::toggleOpenClose()
 {
-	qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << stateToString(_state);
+	// qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << stateToString(_state);
 	if( _state == opened )
 		_state = closed;
 	else if ( _state == closed )
 		_state = opened;
 	else
-		error("toggleOpenClose: cam state is not closed or opened");
+		error("toggleOpenClose: cam state is neither closed nor opened");
 }
 
 void Cam::onCameraDisconnected()
diff --git a/src/cam.h b/src/cam.h
index 72cac15..f959e2e 100644
--- a/src/cam.h
+++ b/src/cam.h
@@ -41,7 +41,6 @@ public:
 	Cam(const Cam&);
 	~Cam();
 
-	QString name();
 	QString id();
 	State state() const;
 	static QString stateToString(const State&);
@@ -50,7 +49,7 @@ public:
 	void open();
 	void close();
 	void startRecording(minutes dur);
-	void startRecording(seconds dur); //use as literals "1s, 1min"
+	void startRecording(seconds dur); //can use literals "1s, 1min"
 	QString info();
 	inline CameraPtr getCameraPtr() const { return _cam; }
 
@@ -60,7 +59,6 @@ public slots:
 
 private:
 	CameraPtr _cam;
-	QString _name;
 	QString _id;
 	State _state;
 	FramePtrVector _frames;
diff --git a/src/camobserver.cpp b/src/camobserver.cpp
index b1c29bf..125cdc3 100644
--- a/src/camobserver.cpp
+++ b/src/camobserver.cpp
@@ -11,12 +11,12 @@ void CamObserver::CameraListChanged(CameraPtr pCam, UpdateTriggerType reason)
 {
 	if( UpdateTriggerPluggedIn == reason || UpdateTriggerPluggedOut == reason )
 	{
-		qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << "";
+		// qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << "";
 		emit cameraListChanged();
 	}
 	else // UpdateTriggerOpenStateChanged
 	{
 		emit cameraStateChanged(pCam);
-		qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << "";
+		// qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << "";
 	}
 }
diff --git a/src/cmd/console.cpp b/src/cmd/console.cpp
index 985d3c6..e1ccf04 100644
--- a/src/cmd/console.cpp
+++ b/src/cmd/console.cpp
@@ -13,7 +13,8 @@
 
 Console::Console(): QObject() {}; // Private constructor to prevent instantiation
 
-Console* Console::getInstance() {
+Console* Console::getInstance()
+{
 	static Console* instance = new Console; // Static local variable ensures single instance
 	return instance;
 }
@@ -30,7 +31,7 @@ void Console::listenKeys()
 		{
 			emit numberEntered(std::stoi(input)); // Number
 		}
-		else if (input.size() == 1)
+		else if (input.size() <= 1)
 		{
 			emit keyPressed(input[0]); // Single char (excluding single digits)
 			if( input[0] == 'q' )
@@ -131,7 +132,7 @@ Controller::~Controller()
 void Controller::onKeyPressed(const QChar& key)
 {
 	auto console = Console::getInstance();
-	qDebug().noquote() << __FUNCTION__ << ": " << key;
+	// qDebug().noquote() << __FUNCTION__ << ": " << key;
 
 	switch (key.unicode())
 	{
@@ -175,9 +176,9 @@ void Controller::onKeyPressed(const QChar& key)
 	}
 }
 
-void Controller::onLineEntered(const QString&)
+void Controller::onLineEntered(const QString& line)
 {
-	// qDebug().noquote() << "Line entered: " << line;
+	qDebug().noquote() << "Line entered: " << line;
 }
 
 void Controller::onNumberEntered(int number)
diff --git a/src/cmd/main.cpp b/src/cmd/main.cpp
index d7ea8b5..5afa7dd 100644
--- a/src/cmd/main.cpp
+++ b/src/cmd/main.cpp
@@ -27,8 +27,8 @@ int main(int argc, char *argv[])
 	QObject::connect(&controller, &Controller::setDuration, 		&core, &Core::setDuration);
 	// XXX add new func
 
-	// QTimer::singleShot(10, controller.console, SLOT(printVersion()), Qt::DirectConnection); // for testing
-	// QTimer::singleShot(200, &core, SLOT(openCam())); // for testing
+	QTimer::singleShot(0, Console::getInstance(), SLOT(printVersion()));
+	QTimer::singleShot(100, &core, SLOT(openCam())); // for testing
 
 	return a.exec();
 }
diff --git a/src/core.cpp b/src/core.cpp
index 54487c6..d056318 100644
--- a/src/core.cpp
+++ b/src/core.cpp
@@ -25,7 +25,7 @@ Core::Core() : IPrinter(),
 	_apiStarted( false ),
 	_sys( VmbSystem::GetInstance() ), // create and get Vimba singleton
 	_cameras( CamPtrVector() ),
-	_recDuration( 16s ) //default
+	_recDuration( 6s ) //default
 {
 	QTimer::singleShot(0, this, SLOT(init())); //delayed init to allow connections to be established -> print/error signals!
 }
@@ -37,6 +37,8 @@ void Core::init()
 
 	_apiStarted = true;
 
+	print("threads",QThread::idealThreadCount());
+
 	auto camObserver = new CamObserver();
 	connect( camObserver, SIGNAL(cameraListChanged()), this, SLOT(listCams()) ); //listcams calls updateCams!
 	connect( camObserver, SIGNAL(cameraStateChanged(CameraPtr)), this, SLOT(toggleCamState(CameraPtr)) ); //
@@ -84,7 +86,7 @@ void Core::updateCameras()
 		if (!found)
 		{
 			_cameras.push_back(std::make_shared<Cam>(vcam)); //create Cam() from CameraPtr vcam
-			print({"cam connected:",_cameras.back()->name()});
+			print(QString("cam connected: %1").arg(_cameras.back()->id()));
 		}
 	}
 
@@ -92,7 +94,7 @@ void Core::updateCameras()
 	auto it = _cameras.begin();
 	while (it != _cameras.end())
 	{
-		qDebug() << "check " << (*it)->id();
+		// qDebug() << "check " << (*it)->id();
 
 		bool found = false;
 		for (const auto& vcam : vmbCameras)
@@ -108,11 +110,11 @@ void Core::updateCameras()
 		if (!found)
 		{
 			qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << " !!! ";
-			print({"cam disconnected",(*it)->name()});
+			print({"cam disconnected",(*it)->id()});
 			it = _cameras.erase(it);
 		}
 	}
-} // now ~Camera() should be called on removed cams. >> camObserver
+} // now ~Camera() should be called on removed cams. >> triggers camObserver
 
 void Core::toggleCamState(CameraPtr vcam)
 {
@@ -127,7 +129,8 @@ void Core::listCams()
 	if( !_apiStarted )
 		return;
 
-	updateCameras();
+	updateCameras();]
+	print("cams",_cameras.size());
 
 	QStringList allCamInfo;
 	auto idx=0;
@@ -188,22 +191,17 @@ CamPtr Core::cam(const unsigned long &idx)
 
 void Core::startRecording()
 {
-	if( cam() == nullptr )
-		return;
-
-	cam()->startRecording( _recDuration ); //XXX dur from user input
+	if( cam() )
+		cam()->startRecording( _recDuration );
 }
 
 void Core::stopRecording()
 {
-	if( cam() == nullptr )
-		return;
-
-	cam()->stopRecording();
+	if( cam() )
+		cam()->stopRecording();
 }
 
 void Core::setDuration(int dur)
 {
-	qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << "";
 	_recDuration = seconds(dur);
 }
diff --git a/src/frameprocessor.cpp b/src/frameprocessor.cpp
index c7e5516..5c2143b 100644
--- a/src/frameprocessor.cpp
+++ b/src/frameprocessor.cpp
@@ -25,6 +25,8 @@ FrameProcessor::~FrameProcessor()
 // post: frame needs to be requeued
 void FrameProcessor::processFrame()
 {
+	static int count = 0;
+	++count;
 	VmbUint32_t width = 0;
 	VmbUint32_t height = 0;
 	unsigned char* pdata = nullptr;
@@ -37,21 +39,26 @@ void FrameProcessor::processFrame()
 	m_pframe->GetTimestamp(timestamp);
 	m_pframe->GetPixelFormat(pixelFormat);
 
+	QString pixelFormatStr = "RGB8";
 	if (pixelFormat != VmbPixelFormatRgb8)
 	{
 		qWarning() << "⚠️  Pixel format is not RGB8, 2D: implement convert func ... ";
+		pixelFormatStr = "???";
 	}
 
-	qDebug() << "Frame received: " << width << "x" << height << "px, format: " << pixelFormat << ", timestamp: " << timestamp;
-
 	cv::Mat frameMat(height, width, CV_8UC3, pdata);
 
 	std::vector<int> params = {cv::IMWRITE_JPEG_QUALITY, 99, cv::IMWRITE_JPEG_OPTIMIZE, 1, cv::IMWRITE_JPEG_RST_INTERVAL,4};
 	QString filename = QString::number(timestamp) + "_frame.jpg";
 	cv::imwrite(filename.toStdString(), frameMat, params);
-	qDebug() << "Frame saved as JPEG: " << filename;
 
+	qDebug() << "rcvd Frame #" << count << ": " << width << "x" << height << "px, format: " << pixelFormatStr << ", timestamp: " << timestamp << ", file: " << filename;
+
+
+#ifdef DEBUG
 	QThread::msleep(2000); //simulate processing time // testing
+#endif
+
 
 	emit frameProcessed(m_pframe);
 }
diff --git a/src/utils.cpp b/src/utils.cpp
index 0f3a270..0623fb0 100644
--- a/src/utils.cpp
+++ b/src/utils.cpp
@@ -21,13 +21,20 @@ using namespace VmbCPP;
 
 int ncam()
 {
+#ifdef DEBUG
 	return 1; //XXX read in from config file
+#else
+	return 1;
+#endif
 }
 
 int threadsPerCam()
 {
-	// return std::ceil(static_cast<double>(QThread::idealThreadCount()) / ncam() ); //round up!
-	return 2; // for testing
+#ifdef DEBUG
+	return 2;
+#else
+	return std::ceil(static_cast<double>(QThread::idealThreadCount()) / ncam() ); //round up!
+#endif
 }
 
 const QString getVersion()
-- 
GitLab