Common Vision Blox 15.0
Loading...
Searching...
No Matches
Image Manager/Cvb++/QmlFirmwareUpdater

This example program is located in your CVB installation under %CVB%Tutorial/Image Manager/Cvb++/QmlFirmwareUpdater.

backend.cpp:

#include "backend.hpp"
#include "QDebug"
#include <cvb/data_type.hpp>
#include <cvb/device.hpp>
#include <cvb/device_factory.hpp>
BackEnd::BackEnd(QQuickView *view, QObject *parent)
: QObject(parent), view_{view} {
// Register view to queue commands over threads
qRegisterMetaType<QWindow::Visibility>();
}
BackEnd::~BackEnd() {}
void BackEnd::ShowStatus(const std::string &statusMessage) {
auto statusText = view_->findChild<QObject *>("status");
if (statusText)
statusText->setProperty("text", statusMessage.c_str());
}
void BackEnd::ShowGufContent(const std::string &statusMessage) {
auto statusText = view_->findChild<QObject *>("gufContent");
if (statusText)
statusText->setProperty("text", statusMessage.c_str());
}
const QVariantList BackEnd::CameraCombo() { return cameraCombo_; }
const QVariantList BackEnd::GufEntriesCombo() { return gufEntriesCombo_; }
void BackEnd::SetCameraCombo(const QVariantList &cameraCombo) {
if (cameraCombo_ != cameraCombo) {
cameraCombo_ = cameraCombo;
emit CameraComboChanged();
}
}
void BackEnd::SetGufEntriesCombo(const QVariantList &gufEntriesCombo) {
if (gufEntriesCombo_ != gufEntriesCombo) {
gufEntriesCombo_ = gufEntriesCombo;
emit GufEntriesComboChanged();
}
}
void BackEnd::UpdateGufEntryInfo(const int &gufEntryIdx) {
if (fwupdater_) {
try {
ShowStatus(CVB_LIT("Loading guf file."));
auto entries = fwupdater_->UpdateFileInfos(gufEntryIdx);
std::stringstream ss;
for (const auto &entry : entries) {
ss << entry << "\n";
}
ShowGufContent(ss.str());
ShowStatus(CVB_LIT("Done."));
} catch (...) {
ShowStatus(
CVB_LIT("Failed to retrieve available update file infos from guf."));
}
}
}
void BackEnd::BtnOpenGuf(const QString &path) {
qDebug() << "Using guf file " << path;
gufFilePath_ = path.toLocal8Bit().constData();
try {
ShowStatus("Reading guf content.");
fwupdater_ = Cvb::GenApi::FWUpdater::Create(gufFilePath_);
ShowStatus("Done");
} catch (const std::exception &e) {
std::stringstream error;
error << CVB_LIT("Error creating firmware updater using guf file.") +
path.toStdString() + CVB_LIT("\n")
<< e.what() << "\n Maybe check the Control.xml in the guf file.";
ShowStatus(error.str());
} catch (...) {
ShowStatus("Failed to create firmware updater.");
}
QVariantList list;
list.clear();
if (fwupdater_) {
try {
gufFileEntries_ = fwupdater_->AvailableUpdateFiles();
for (const auto &entry : gufFileEntries_) {
std::stringstream ss;
ss << entry;
list.append(std::string(ss.str()).c_str());
}
SetGufEntriesCombo(list);
} catch (...) {
ShowStatus(CVB_LIT("Failed to retieve available update files from guf."));
}
}
}
void BackEnd::ListAvailableDevices() {
deviceMap_.clear();
discover_ =
Cvb::DeviceFactory::Discover(Cvb::Driver::DiscoverFlags::IgnoreGevSD);
for (const auto &device : discover_) {
Cvb::String devModel{"Unset"};
if (device.TryGetProperty(Cvb::Driver::DiscoveryProperties::DeviceModel,
devModel))
deviceMap_[devModel] = static_cast<int32_t>(device.Index());
}
}
// Scan for available devices and update list of possible updates, call update
// list to update dropdown
void BackEnd::BtnScan() {
qDebug() << "Starting scan of available devices for guf file.";
QVariantList list;
list.clear();
ListAvailableDevices();
for (auto const &device : deviceMap_) {
std::stringstream ss;
ss << device.first;
list.append(std::string(ss.str()).c_str());
}
SetCameraCombo(list);
}
void BackEnd::BtnStartUpdate(const int gufEntryIdx, const QString &selCamera) {
if (gufEntryIdx < 0) {
ShowStatus(CVB_LIT("No guf entry set."));
return;
}
if (selCamera.isEmpty()) {
ShowStatus(CVB_LIT("No camera selected."));
return;
}
auto selDeviceIdx = deviceMap_[selCamera.toStdString()];
qDebug() << "Starting update for device " << selCamera << " with update file "
<< QString::number(gufEntryIdx) << " \""
<< gufFileEntries_[gufEntryIdx].c_str() << "\".";
ShowStatus(CVB_LIT("Updating camera."));
try {
auto tmpDevPtr{
Cvb::DeviceFactory::Open(discover_[selDeviceIdx].AccessToken(),
Cvb::AcquisitionStack::PreferGenTL)};
fwupdater_->Update(std::move(tmpDevPtr), gufEntryIdx);
ShowStatus(CVB_LIT("Update successful."));
} catch (const std::exception &e) {
std::stringstream error;
error << CVB_LIT("Update failed: ") << e.what();
ShowStatus(error.str());
} catch (...) {
ShowStatus(CVB_LIT(
"Sorry, something unknown and unexpected has happened. Following the "
"mainstream we could have ignored this leaving you even more puzzled. "
"However we must admit that we have no clue either."));
}
}
static std::vector< DiscoveryInformation > Discover()
static std::shared_ptr< T > Open(const String &provider, AcquisitionStack acquisitionStack=AcquisitionStack::PreferVin)
static std::unique_ptr< FWUpdater > Create(const String &filename, bool verify=true)
std::string String

backend.hpp:

#pragma once
#include "QVariantList"
#include <QObject>
#include <QQmlContext>
#include <QQuickItem>
#include <QQuickView>
#include <QString>
#include <cvb/genapi/fw_updater.hpp>
#include <cvb/image.hpp>
class BackEnd : public QObject {
// This macro is required for QObject support. You should get a compiler
// error if you don't include this.
Q_OBJECT
Q_PROPERTY(QVariantList CameraCombo READ CameraCombo WRITE SetCameraCombo
NOTIFY CameraComboChanged)
Q_PROPERTY(QVariantList GufEntriesCombo READ GufEntriesCombo WRITE
SetGufEntriesCombo NOTIFY GufEntriesComboChanged)
public:
// QObjects are expected to support a parent/child hierarchy
explicit BackEnd(QQuickView *view, QObject *parent = 0);
~BackEnd();
const QVariantList CameraCombo();
void SetCameraCombo(const QVariantList &cameraCombo);
const QVariantList GufEntriesCombo();
void SetGufEntriesCombo(const QVariantList &cameraCombo);
signals:
void CameraComboChanged();
void GufEntriesComboChanged();
public slots:
void BtnScan();
void BtnOpenGuf(const QString &path);
void BtnStartUpdate(const int gufEntryIdx, const QString &selCamera);
void UpdateGufEntryInfo(const int &gufEntryIdx);
private:
void ShowStatus(const std::string &statusMessage);
void ShowGufContent(const std::string &statusMessage);
void ListAvailableDevices();
QQuickView *view_;
QQmlContext *context_;
QVariantList cameraCombo_;
QVariantList gufEntriesCombo_;
Cvb::String gufFilePath_;
std::map<Cvb::String, int32_t> deviceMap_;
std::vector<Cvb::String> gufFileEntries_;
std::unique_ptr<Cvb::GenApi::FWUpdater> fwupdater_;
std::vector<Cvb::DiscoveryInformation> discover_;
};

main.cpp:

// The QmlFirmwareUpdater example starts a QML FirmwareUpdater which can load a device and stream the
// device image.
// ---------------------------------------------------------------------------
// ---------------------------------------------------------------------------
#include <iostream>
#include <QApplication>
#include <QIcon>
#include <QMetaType>
#include <QQmlContext>
#include <QQuickStyle>
#include <QQuickView>
#include "backend.hpp"
int main(int argc, char *argv[]) {
try {
QQuickStyle::setStyle("Windows");
QQuickStyle::setFallbackStyle("Universal");
QApplication app(argc, argv);
app.setOrganizationName("STEMMER IMAGING");
app.setOrganizationDomain("https://www.stemmer-imaging.com/");
app.setApplicationName("FirmwareUpdater C++ tutorial");
QQuickView *view = new QQuickView;
view->setResizeMode(QQuickView::SizeRootObjectToView);
view->setIcon(QIcon(":/qttutorial.png"));
view->resize(640, 390);
view->show();
// Set context property before creating QML file
BackEnd be(view);
view->rootContext()->setContextProperty("backEnd", &be);
// Load main QML file
view->setSource(QUrl::fromLocalFile("../main.qml"));
return app.exec();
} catch (const std::exception &error) {
std::cout << error.what() << std::endl;
}
}