]>
Commit | Line | Data |
---|---|---|
d4984fe7 | 1 | /* |
b3f22de0 | 2 | * This file is part of the PulseView project. |
d4984fe7 JH |
3 | * |
4 | * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
5 | * | |
6 | * This program is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with this program; if not, write to the Free Software | |
18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
19 | */ | |
20 | ||
09f5d123 | 21 | #include <extdef.h> |
dde1a563 | 22 | |
09f5d123 | 23 | #include <assert.h> |
215f9499 | 24 | |
dde1a563 | 25 | #include <QAction> |
48888313 | 26 | #include <QDebug> |
40065ab6 JS |
27 | #include <QHelpEvent> |
28 | #include <QToolTip> | |
dde1a563 | 29 | |
2acdb232 | 30 | #include "samplingbar.hpp" |
d4984fe7 | 31 | |
2acdb232 JH |
32 | #include <pv/devicemanager.hpp> |
33 | #include <pv/popups/deviceoptions.hpp> | |
34 | #include <pv/popups/channels.hpp> | |
35 | #include <pv/util.hpp> | |
cdb50f67 | 36 | |
e8d00928 ML |
37 | #include <libsigrok/libsigrok.hpp> |
38 | ||
19adbc2c | 39 | using std::map; |
e8d00928 | 40 | using std::vector; |
b50ef520 JH |
41 | using std::max; |
42 | using std::min; | |
f9abf97e | 43 | using std::shared_ptr; |
819f4c25 | 44 | using std::string; |
dd63af74 | 45 | |
e8d00928 ML |
46 | using sigrok::Capability; |
47 | using sigrok::ConfigKey; | |
48 | using sigrok::Device; | |
49 | using sigrok::Error; | |
50 | ||
51e77110 | 51 | namespace pv { |
f4c92e1c | 52 | namespace toolbars { |
51e77110 | 53 | |
b50ef520 JH |
54 | const uint64_t SamplingBar::MinSampleCount = 100ULL; |
55 | const uint64_t SamplingBar::MaxSampleCount = 1000000000000ULL; | |
56 | const uint64_t SamplingBar::DefaultSampleCount = 1000000; | |
09f5d123 | 57 | |
aca00b1e | 58 | SamplingBar::SamplingBar(SigSession &session, QWidget *parent) : |
6fb67b27 | 59 | QToolBar("Sampling Bar", parent), |
8dbbc7f0 JH |
60 | session_(session), |
61 | device_selector_(this), | |
62 | updating_device_selector_(false), | |
63 | configure_button_(this), | |
64 | configure_button_action_(NULL), | |
65 | channels_button_(this), | |
66 | sample_count_(" samples", this), | |
67 | sample_rate_("Hz", this), | |
68 | updating_sample_rate_(false), | |
69 | updating_sample_count_(false), | |
70 | sample_count_supported_(false), | |
71 | icon_red_(":/icons/status-red.svg"), | |
72 | icon_green_(":/icons/status-green.svg"), | |
73 | icon_grey_(":/icons/status-grey.svg"), | |
74 | run_stop_button_(this) | |
6fb67b27 | 75 | { |
0c7cdea2 SA |
76 | setObjectName(QString::fromUtf8("SamplingBar")); |
77 | ||
8dbbc7f0 | 78 | connect(&run_stop_button_, SIGNAL(clicked()), |
9f3d12f3 | 79 | this, SLOT(on_run_stop())); |
8dbbc7f0 | 80 | connect(&device_selector_, SIGNAL(currentIndexChanged (int)), |
dde1a563 | 81 | this, SLOT(on_device_selected())); |
8dbbc7f0 | 82 | connect(&sample_count_, SIGNAL(value_changed()), |
124d97de | 83 | this, SLOT(on_sample_count_changed())); |
8dbbc7f0 | 84 | connect(&sample_rate_, SIGNAL(value_changed()), |
1198b887 | 85 | this, SLOT(on_sample_rate_changed())); |
dde1a563 | 86 | |
8dbbc7f0 | 87 | sample_count_.show_min_max_step(0, UINT64_MAX, 1); |
215f9499 | 88 | |
2b49eeb0 | 89 | set_capture_state(pv::SigSession::Stopped); |
274d4f13 | 90 | |
8dbbc7f0 | 91 | configure_button_.setIcon(QIcon::fromTheme("configure", |
688ef645 | 92 | QIcon(":/icons/configure.png"))); |
b7b659aa | 93 | |
8dbbc7f0 | 94 | channels_button_.setIcon(QIcon::fromTheme("channels", |
6ac6242b | 95 | QIcon(":/icons/channels.svg"))); |
cdb50f67 | 96 | |
8dbbc7f0 | 97 | run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon); |
f5798068 | 98 | |
8dbbc7f0 JH |
99 | addWidget(&device_selector_); |
100 | configure_button_action_ = addWidget(&configure_button_); | |
101 | addWidget(&channels_button_); | |
102 | addWidget(&sample_count_); | |
103 | addWidget(&sample_rate_); | |
6fb67b27 | 104 | |
8dbbc7f0 | 105 | addWidget(&run_stop_button_); |
40065ab6 | 106 | |
8dbbc7f0 JH |
107 | sample_count_.installEventFilter(this); |
108 | sample_rate_.installEventFilter(this); | |
6fb67b27 JH |
109 | } |
110 | ||
18203d86 | 111 | void SamplingBar::set_device_list( |
4fe06f49 | 112 | const std::list< std::pair<std::shared_ptr<sigrok::Device>, std::string> > &devices, |
e8d00928 | 113 | shared_ptr<Device> selected) |
18203d86 | 114 | { |
e95e8563 JH |
115 | int selected_index = -1; |
116 | ||
117 | assert(selected); | |
118 | ||
8dbbc7f0 | 119 | updating_device_selector_ = true; |
95237c18 | 120 | |
8dbbc7f0 | 121 | device_selector_.clear(); |
18203d86 | 122 | |
4fe06f49 | 123 | for (auto entry : devices) { |
e8d00928 | 124 | auto device = entry.first; |
4fe06f49 | 125 | auto display_name = entry.second; |
19adbc2c | 126 | |
e8d00928 ML |
127 | assert(device); |
128 | ||
129 | if (selected == device) | |
8dbbc7f0 | 130 | selected_index = device_selector_.count(); |
e95e8563 | 131 | |
8dbbc7f0 | 132 | device_selector_.addItem(display_name.c_str(), |
e8d00928 | 133 | qVariantFromValue(device)); |
18203d86 JH |
134 | } |
135 | ||
e95e8563 JH |
136 | // The selected device should have been in the list |
137 | assert(selected_index != -1); | |
8dbbc7f0 | 138 | device_selector_.setCurrentIndex(selected_index); |
e95e8563 JH |
139 | |
140 | update_device_config_widgets(); | |
141 | ||
8dbbc7f0 | 142 | updating_device_selector_ = false; |
18203d86 JH |
143 | } |
144 | ||
e8d00928 | 145 | shared_ptr<Device> SamplingBar::get_selected_device() const |
d4984fe7 | 146 | { |
8dbbc7f0 | 147 | const int index = device_selector_.currentIndex(); |
333d5bbc | 148 | if (index < 0) |
e8d00928 | 149 | return shared_ptr<Device>(); |
6fb67b27 | 150 | |
8dbbc7f0 | 151 | return device_selector_.itemData(index).value<shared_ptr<Device>>(); |
6fb67b27 JH |
152 | } |
153 | ||
2b49eeb0 | 154 | void SamplingBar::set_capture_state(pv::SigSession::capture_state state) |
6ac96c2e | 155 | { |
8dbbc7f0 JH |
156 | const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_}; |
157 | run_stop_button_.setIcon(*icons[state]); | |
158 | run_stop_button_.setText((state == pv::SigSession::Stopped) ? | |
2b49eeb0 | 159 | tr("Run") : tr("Stop")); |
8dbbc7f0 | 160 | run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space)); |
6ac96c2e JH |
161 | } |
162 | ||
dde1a563 JH |
163 | void SamplingBar::update_sample_rate_selector() |
164 | { | |
e8d00928 ML |
165 | Glib::VariantContainerBase gvar_dict; |
166 | GVariant *gvar_list; | |
488f5d3f BV |
167 | const uint64_t *elements = NULL; |
168 | gsize num_elements; | |
dde1a563 | 169 | |
8dbbc7f0 | 170 | if (updating_sample_rate_) |
3d4f16af JH |
171 | return; |
172 | ||
e8d00928 ML |
173 | const shared_ptr<Device> device = get_selected_device(); |
174 | if (!device) | |
ef4d0201 JH |
175 | return; |
176 | ||
8dbbc7f0 JH |
177 | assert(!updating_sample_rate_); |
178 | updating_sample_rate_ = true; | |
1198b887 | 179 | |
e8d00928 ML |
180 | try { |
181 | gvar_dict = device->config_list(ConfigKey::SAMPLERATE); | |
182 | } catch (Error error) { | |
8dbbc7f0 JH |
183 | sample_rate_.show_none(); |
184 | updating_sample_rate_ = false; | |
dde1a563 | 185 | return; |
1198b887 | 186 | } |
dde1a563 | 187 | |
e8d00928 | 188 | if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(), |
1198b887 JH |
189 | "samplerate-steps", G_VARIANT_TYPE("at")))) |
190 | { | |
488f5d3f BV |
191 | elements = (const uint64_t *)g_variant_get_fixed_array( |
192 | gvar_list, &num_elements, sizeof(uint64_t)); | |
aafe53af JH |
193 | |
194 | const uint64_t min = elements[0]; | |
195 | const uint64_t max = elements[1]; | |
196 | const uint64_t step = elements[2]; | |
197 | ||
488f5d3f | 198 | g_variant_unref(gvar_list); |
aafe53af JH |
199 | |
200 | assert(min > 0); | |
201 | assert(max > 0); | |
202 | assert(max > min); | |
203 | assert(step > 0); | |
204 | ||
205 | if (step == 1) | |
8dbbc7f0 | 206 | sample_rate_.show_125_list(min, max); |
aafe53af JH |
207 | else |
208 | { | |
209 | // When the step is not 1, we cam't make a 1-2-5-10 | |
210 | // list of sample rates, because we may not be able to | |
211 | // make round numbers. Therefore in this case, show a | |
212 | // spin box. | |
8dbbc7f0 | 213 | sample_rate_.show_min_max_step(min, max, step); |
aafe53af | 214 | } |
dde1a563 | 215 | } |
e8d00928 | 216 | else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(), |
488f5d3f | 217 | "samplerates", G_VARIANT_TYPE("at")))) |
dde1a563 | 218 | { |
488f5d3f BV |
219 | elements = (const uint64_t *)g_variant_get_fixed_array( |
220 | gvar_list, &num_elements, sizeof(uint64_t)); | |
8dbbc7f0 | 221 | sample_rate_.show_list(elements, num_elements); |
488f5d3f | 222 | g_variant_unref(gvar_list); |
dde1a563 | 223 | } |
8dbbc7f0 | 224 | updating_sample_rate_ = false; |
48888313 JH |
225 | |
226 | update_sample_rate_selector_value(); | |
227 | } | |
228 | ||
229 | void SamplingBar::update_sample_rate_selector_value() | |
230 | { | |
8dbbc7f0 | 231 | if (updating_sample_rate_) |
3d4f16af JH |
232 | return; |
233 | ||
e8d00928 ML |
234 | const shared_ptr<Device> device = get_selected_device(); |
235 | if (!device) | |
19adbc2c JH |
236 | return; |
237 | ||
e8d00928 ML |
238 | try { |
239 | auto gvar = device->config_get(ConfigKey::SAMPLERATE); | |
240 | uint64_t samplerate = | |
15289d5c | 241 | Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get(); |
8dbbc7f0 JH |
242 | assert(!updating_sample_rate_); |
243 | updating_sample_rate_ = true; | |
244 | sample_rate_.set_value(samplerate); | |
245 | updating_sample_rate_ = false; | |
e8d00928 | 246 | } catch (Error error) { |
8dd44190 | 247 | qDebug() << "WARNING: Failed to get value of sample rate"; |
eb4008a6 JH |
248 | return; |
249 | } | |
48888313 JH |
250 | } |
251 | ||
85756012 ML |
252 | void SamplingBar::update_sample_count_selector() |
253 | { | |
8dbbc7f0 | 254 | if (updating_sample_count_) |
3d4f16af JH |
255 | return; |
256 | ||
e8d00928 ML |
257 | const shared_ptr<Device> device = get_selected_device(); |
258 | if (!device) | |
19adbc2c JH |
259 | return; |
260 | ||
8dbbc7f0 JH |
261 | assert(!updating_sample_count_); |
262 | updating_sample_count_ = true; | |
85756012 | 263 | |
3e7636f9 | 264 | if (!sample_count_supported_) |
df3c1aa3 | 265 | { |
3e7636f9 JH |
266 | sample_count_.show_none(); |
267 | updating_sample_count_ = false; | |
268 | return; | |
269 | } | |
b50ef520 | 270 | |
3e7636f9 JH |
271 | uint64_t sample_count = sample_count_.value(); |
272 | uint64_t min_sample_count = 0; | |
273 | uint64_t max_sample_count = MaxSampleCount; | |
3cbffdcf | 274 | |
3e7636f9 JH |
275 | if (sample_count == 0) |
276 | sample_count = DefaultSampleCount; | |
b50ef520 | 277 | |
3e7636f9 JH |
278 | try { |
279 | auto gvar = device->config_list(ConfigKey::LIMIT_SAMPLES); | |
280 | g_variant_get(gvar.gobj(), "(tt)", | |
281 | &min_sample_count, &max_sample_count); | |
282 | } catch (Error error) {} | |
1d04852f | 283 | |
3e7636f9 JH |
284 | min_sample_count = min(max(min_sample_count, MinSampleCount), |
285 | max_sample_count); | |
b50ef520 | 286 | |
3e7636f9 JH |
287 | sample_count_.show_125_list( |
288 | min_sample_count, max_sample_count); | |
b50ef520 | 289 | |
3e7636f9 JH |
290 | try { |
291 | auto gvar = device->config_get(ConfigKey::LIMIT_SAMPLES); | |
292 | sample_count = g_variant_get_uint64(gvar.gobj()); | |
293 | if (sample_count == 0) | |
294 | sample_count = DefaultSampleCount; | |
295 | sample_count = min(max(sample_count, MinSampleCount), | |
296 | max_sample_count); | |
297 | } catch (Error error) {} | |
298 | ||
299 | sample_count_.set_value(sample_count); | |
df3c1aa3 | 300 | |
8dbbc7f0 | 301 | updating_sample_count_ = false; |
85756012 ML |
302 | } |
303 | ||
e95e8563 JH |
304 | void SamplingBar::update_device_config_widgets() |
305 | { | |
e95e8563 JH |
306 | using namespace pv::popups; |
307 | ||
e8d00928 ML |
308 | const shared_ptr<Device> device = get_selected_device(); |
309 | if (!device) | |
e95e8563 JH |
310 | return; |
311 | ||
312 | // Update the configure popup | |
e8d00928 | 313 | DeviceOptions *const opts = new DeviceOptions(device, this); |
8dbbc7f0 | 314 | configure_button_action_->setVisible( |
e95e8563 | 315 | !opts->binding().properties().empty()); |
8dbbc7f0 | 316 | configure_button_.set_popup(opts); |
e95e8563 | 317 | |
6ac6242b | 318 | // Update the channels popup |
8dbbc7f0 JH |
319 | Channels *const channels = new Channels(session_, this); |
320 | channels_button_.set_popup(channels); | |
e95e8563 JH |
321 | |
322 | // Update supported options. | |
8dbbc7f0 | 323 | sample_count_supported_ = false; |
e95e8563 | 324 | |
e8d00928 ML |
325 | try { |
326 | for (auto entry : device->config_keys(ConfigKey::DEVICE_OPTIONS)) | |
e95e8563 | 327 | { |
e8d00928 ML |
328 | auto key = entry.first; |
329 | auto capabilities = entry.second; | |
330 | switch (key->id()) { | |
e95e8563 | 331 | case SR_CONF_LIMIT_SAMPLES: |
e8d00928 | 332 | if (capabilities.count(Capability::SET)) |
8dbbc7f0 | 333 | sample_count_supported_ = true; |
e95e8563 JH |
334 | break; |
335 | case SR_CONF_LIMIT_FRAMES: | |
e8d00928 ML |
336 | if (capabilities.count(Capability::SET)) |
337 | { | |
338 | device->config_set(ConfigKey::LIMIT_FRAMES, | |
15289d5c | 339 | Glib::Variant<guint64>::create(1)); |
e8d00928 ML |
340 | on_config_changed(); |
341 | } | |
342 | break; | |
343 | default: | |
e95e8563 JH |
344 | break; |
345 | } | |
346 | } | |
e8d00928 | 347 | } catch (Error error) {} |
e95e8563 JH |
348 | |
349 | // Add notification of reconfigure events | |
350 | disconnect(this, SLOT(on_config_changed())); | |
e8d00928 | 351 | connect(&opts->binding(), SIGNAL(config_changed()), |
e95e8563 JH |
352 | this, SLOT(on_config_changed())); |
353 | ||
354 | // Update sweep timing widgets. | |
355 | update_sample_count_selector(); | |
356 | update_sample_rate_selector(); | |
357 | } | |
358 | ||
124d97de ML |
359 | void SamplingBar::commit_sample_count() |
360 | { | |
361 | uint64_t sample_count = 0; | |
362 | ||
8dbbc7f0 | 363 | if (updating_sample_count_) |
3d4f16af JH |
364 | return; |
365 | ||
e8d00928 ML |
366 | const shared_ptr<Device> device = get_selected_device(); |
367 | if (!device) | |
19adbc2c JH |
368 | return; |
369 | ||
8dbbc7f0 | 370 | sample_count = sample_count_.value(); |
124d97de ML |
371 | |
372 | // Set the sample count | |
8dbbc7f0 JH |
373 | assert(!updating_sample_count_); |
374 | updating_sample_count_ = true; | |
375 | if (sample_count_supported_) | |
e8d00928 ML |
376 | { |
377 | try { | |
378 | device->config_set(ConfigKey::LIMIT_SAMPLES, | |
15289d5c | 379 | Glib::Variant<guint64>::create(sample_count)); |
e8d00928 ML |
380 | on_config_changed(); |
381 | } catch (Error error) { | |
382 | qDebug() << "Failed to configure sample count."; | |
383 | return; | |
384 | } | |
124d97de | 385 | } |
8dbbc7f0 | 386 | updating_sample_count_ = false; |
124d97de ML |
387 | } |
388 | ||
48888313 JH |
389 | void SamplingBar::commit_sample_rate() |
390 | { | |
391 | uint64_t sample_rate = 0; | |
392 | ||
8dbbc7f0 | 393 | if (updating_sample_rate_) |
3d4f16af JH |
394 | return; |
395 | ||
e8d00928 ML |
396 | const shared_ptr<Device> device = get_selected_device(); |
397 | if (!device) | |
19adbc2c JH |
398 | return; |
399 | ||
8dbbc7f0 | 400 | sample_rate = sample_rate_.value(); |
f9541bde JH |
401 | if (sample_rate == 0) |
402 | return; | |
403 | ||
48888313 | 404 | // Set the samplerate |
8dbbc7f0 JH |
405 | assert(!updating_sample_rate_); |
406 | updating_sample_rate_ = true; | |
e8d00928 ML |
407 | try { |
408 | device->config_set(ConfigKey::SAMPLERATE, | |
15289d5c | 409 | Glib::Variant<guint64>::create(sample_rate)); |
e8d00928 ML |
410 | on_config_changed(); |
411 | } catch (Error error) { | |
48888313 JH |
412 | qDebug() << "Failed to configure samplerate."; |
413 | return; | |
414 | } | |
8dbbc7f0 | 415 | updating_sample_rate_ = false; |
dde1a563 JH |
416 | } |
417 | ||
418 | void SamplingBar::on_device_selected() | |
419 | { | |
8dbbc7f0 | 420 | if (updating_device_selector_) |
95237c18 JH |
421 | return; |
422 | ||
e8d00928 ML |
423 | shared_ptr<Device> device = get_selected_device(); |
424 | if (!device) | |
19adbc2c JH |
425 | return; |
426 | ||
8dbbc7f0 | 427 | session_.set_device(device); |
51d4a9ab | 428 | |
e95e8563 | 429 | update_device_config_widgets(); |
dde1a563 | 430 | } |
51e77110 | 431 | |
124d97de ML |
432 | void SamplingBar::on_sample_count_changed() |
433 | { | |
3d4f16af | 434 | commit_sample_count(); |
124d97de ML |
435 | } |
436 | ||
48888313 JH |
437 | void SamplingBar::on_sample_rate_changed() |
438 | { | |
3d4f16af | 439 | commit_sample_rate(); |
48888313 JH |
440 | } |
441 | ||
9f3d12f3 JH |
442 | void SamplingBar::on_run_stop() |
443 | { | |
b50ef520 | 444 | commit_sample_count(); |
9f3d12f3 JH |
445 | commit_sample_rate(); |
446 | run_stop(); | |
447 | } | |
448 | ||
82afd5e3 JH |
449 | void SamplingBar::on_config_changed() |
450 | { | |
451 | commit_sample_count(); | |
452 | update_sample_count_selector(); | |
453 | commit_sample_rate(); | |
454 | update_sample_rate_selector(); | |
455 | } | |
456 | ||
40065ab6 JS |
457 | bool SamplingBar::eventFilter(QObject *watched, QEvent *event) |
458 | { | |
8dbbc7f0 | 459 | if ((watched == &sample_count_ || watched == &sample_rate_) && |
40065ab6 | 460 | (event->type() == QEvent::ToolTip)) { |
8dbbc7f0 | 461 | double sec = (double)sample_count_.value() / sample_rate_.value(); |
40065ab6 | 462 | QHelpEvent *help_event = static_cast<QHelpEvent*>(event); |
62974f45 JS |
463 | |
464 | QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec)); | |
40065ab6 JS |
465 | QToolTip::showText(help_event->globalPos(), str); |
466 | ||
467 | return true; | |
468 | } | |
469 | ||
470 | return false; | |
471 | } | |
472 | ||
f4c92e1c | 473 | } // namespace toolbars |
51e77110 | 474 | } // namespace pv |