This repository was archived by the owner on Oct 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathimager.cpp
More file actions
169 lines (147 loc) · 4.8 KB
/
imager.cpp
File metadata and controls
169 lines (147 loc) · 4.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* Copyright (C) 2016 Marco Gulino <marco@gulinux.net>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "imager.h"
#include "Qt/qt_strings_helper.h"
#include "commons/utils.h"
#include <QCoreApplication>
#include <QMutex>
using namespace std;
using namespace std::placeholders;
DPTR_IMPL(Imager) {
const ImageHandlerPtr image_handler;
ImagerThreadPtr imager_thread;
LOG_C_SCOPE(Imager);
unique_ptr<QHash<Imager::Capability, bool>> capabilities;
bool destroyed = false;
Configuration::CaptureEndianess captureEndianess = Configuration::CaptureEndianess::CameraDefault;
};
Imager::Imager(const ImageHandlerPtr& image_handler) : QObject(nullptr), dptr(image_handler)
{
static bool metatypes_registered = false;
if(!metatypes_registered) {
metatypes_registered = true;
qRegisterMetaType<Imager::Control>("Imager::Control");
}
connect(this, &Imager::changed, this, [=](const Imager::Control &c) { if(c.is_exposure) update_exposure(); });
}
Imager::~Imager()
{
destroy();
}
void Imager::import_controls(const QVariantList& controls, bool by_id)
{
qDebug() << "Importing controls: " << controls << ", by id: " << by_id;
map<QVariant, QVariantMap> controls_mapped;
transform(begin(controls), end(controls), inserter(controls_mapped, controls_mapped.begin()), [&](const QVariant &item){
QVariantMap map = item.toMap();
return make_pair(by_id ? map["id"] : map["name"], map);
});
auto device_controls = this->controls();
Controls changed_controls;
for(auto &control: device_controls) {
QVariant key = by_id ? QVariant{static_cast<qlonglong>(control.id)} : QVariant{control.name};
if(controls_mapped.count(key)) {
Control changed = control;
qDebug() << "Importing control: " << control.id << ", " << control.name;
changed.import(controls_mapped[key]);
if(! changed.same_value(control)) {
qDebug() << "control " << control << " has changed to " << changed;
changed_controls.push_back(changed);
}
}
}
setControls(changed_controls);
}
void Imager::setControls(const Controls& controls)
{
for(auto control: controls) {
setControl(control);
}
}
QVariantList Imager::export_controls() const
{
QVariantList controls_qvariant;
auto controls = this->controls();
transform(controls.begin(), controls.end(), back_inserter(controls_qvariant), bind(&Control::asMap, _1));
return controls_qvariant;
}
void Imager::destroy()
{
if(d->destroyed)
return;
d->imager_thread.reset();
emit disconnected();
this->deleteLater();
d->destroyed = true;
}
void Imager::readTemperature()
{
}
void Imager::restart(const ImagerThread::Worker::factory& worker)
{
LOG_F_SCOPE
d->imager_thread.reset();
d->imager_thread = make_shared<ImagerThread>(worker(), this, d->image_handler, d->captureEndianess);
update_exposure();
d->imager_thread->start();
}
void Imager::update_exposure()
{
for(auto control: controls()) {
if(control.is_duration && control.is_exposure) {
const chrono::duration<double> exposure = control.seconds();
qDebug() << "Setting exposure: " << exposure.count();
if(d->imager_thread)
d->imager_thread->set_exposure(exposure);
emit exposure_changed(control);
break;
}
}
}
void Imager::wait_for(const std::shared_ptr<QWaitCondition>& wait_condition) const
{
if(! wait_condition)
return;
QMutex wait_mutex;
wait_mutex.lock();
wait_condition->wait(&wait_mutex);
wait_mutex.unlock();
}
shared_ptr<QWaitCondition> Imager::push_job_on_thread(const ImagerThread::Job& job)
{
if(! d->imager_thread) {
qWarning() << "Requested job without imager thread started";
return {};
}
return d->imager_thread->push_job(job);
}
bool Imager::supports(Capability capability) const
{
if(! d->capabilities) {
d->capabilities = make_unique<QHash<Capability, bool>>();
for(auto capability: properties().capabilities)
(*d->capabilities)[capability] = true;
}
return d->capabilities->value(capability, false);
}
void Imager::setCaptureEndianess(Configuration::CaptureEndianess captureEndianess)
{
d->captureEndianess = captureEndianess;
if (d->imager_thread)
wait_for(push_job_on_thread([=]() { d->imager_thread->setCaptureEndianess(d->captureEndianess); }));
}