]> sigrok.org Git - pulseview.git/blame_incremental - pv/toolbars/samplingbar.cpp
SamplingBar: Handle a failure to list the SAMPLERATE key
[pulseview.git] / pv / toolbars / samplingbar.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
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
21#include <extdef.h>
22
23#include <assert.h>
24
25#include <QAction>
26#include <QDebug>
27#include <QHelpEvent>
28#include <QToolTip>
29
30#include "samplingbar.hpp"
31
32#include <pv/devicemanager.hpp>
33#include <pv/mainwindow.hpp>
34#include <pv/popups/deviceoptions.hpp>
35#include <pv/popups/channels.hpp>
36#include <pv/util.hpp>
37
38#include <libsigrok/libsigrok.hpp>
39
40using std::map;
41using std::vector;
42using std::max;
43using std::min;
44using std::shared_ptr;
45using std::string;
46
47using sigrok::Capability;
48using sigrok::ConfigKey;
49using sigrok::Device;
50using sigrok::Error;
51
52namespace pv {
53namespace toolbars {
54
55const uint64_t SamplingBar::MinSampleCount = 100ULL;
56const uint64_t SamplingBar::MaxSampleCount = 1000000000000ULL;
57const uint64_t SamplingBar::DefaultSampleCount = 1000000;
58
59SamplingBar::SamplingBar(Session &session, MainWindow &main_window) :
60 QToolBar("Sampling Bar", &main_window),
61 session_(session),
62 main_window_(main_window),
63 device_selector_(this),
64 updating_device_selector_(false),
65 configure_button_(this),
66 configure_button_action_(NULL),
67 channels_button_(this),
68 sample_count_(" samples", this),
69 sample_rate_("Hz", this),
70 updating_sample_rate_(false),
71 updating_sample_count_(false),
72 sample_count_supported_(false),
73 icon_red_(":/icons/status-red.svg"),
74 icon_green_(":/icons/status-green.svg"),
75 icon_grey_(":/icons/status-grey.svg"),
76 run_stop_button_(this)
77{
78 setObjectName(QString::fromUtf8("SamplingBar"));
79
80 connect(&run_stop_button_, SIGNAL(clicked()),
81 this, SLOT(on_run_stop()));
82 connect(&device_selector_, SIGNAL(currentIndexChanged (int)),
83 this, SLOT(on_device_selected()));
84 connect(&sample_count_, SIGNAL(value_changed()),
85 this, SLOT(on_sample_count_changed()));
86 connect(&sample_rate_, SIGNAL(value_changed()),
87 this, SLOT(on_sample_rate_changed()));
88
89 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
90
91 set_capture_state(pv::Session::Stopped);
92
93 configure_button_.setIcon(QIcon::fromTheme("configure",
94 QIcon(":/icons/configure.png")));
95
96 channels_button_.setIcon(QIcon::fromTheme("channels",
97 QIcon(":/icons/channels.svg")));
98
99 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
100
101 addWidget(&device_selector_);
102 configure_button_action_ = addWidget(&configure_button_);
103 addWidget(&channels_button_);
104 addWidget(&sample_count_);
105 addWidget(&sample_rate_);
106
107 addWidget(&run_stop_button_);
108
109 sample_count_.installEventFilter(this);
110 sample_rate_.installEventFilter(this);
111}
112
113void SamplingBar::set_device_list(
114 const std::list< std::shared_ptr<sigrok::Device> > &devices,
115 shared_ptr<Device> selected)
116{
117 int selected_index = -1;
118
119 assert(selected);
120
121 updating_device_selector_ = true;
122
123 device_selector_.clear();
124
125 for (auto device : devices) {
126 assert(device);
127
128 string display_name =
129 session_.device_manager().get_display_name(device);
130
131 if (selected == device)
132 selected_index = device_selector_.count();
133
134 device_selector_.addItem(display_name.c_str(),
135 qVariantFromValue(device));
136 }
137
138 // The selected device should have been in the list
139 assert(selected_index != -1);
140 device_selector_.setCurrentIndex(selected_index);
141
142 update_device_config_widgets();
143
144 updating_device_selector_ = false;
145}
146
147shared_ptr<Device> SamplingBar::get_selected_device() const
148{
149 const int index = device_selector_.currentIndex();
150 if (index < 0)
151 return shared_ptr<Device>();
152
153 return device_selector_.itemData(index).value<shared_ptr<Device>>();
154}
155
156void SamplingBar::set_capture_state(pv::Session::capture_state state)
157{
158 const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_};
159 run_stop_button_.setIcon(*icons[state]);
160 run_stop_button_.setText((state == pv::Session::Stopped) ?
161 tr("Run") : tr("Stop"));
162 run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
163}
164
165void SamplingBar::update_sample_rate_selector()
166{
167 Glib::VariantContainerBase gvar_dict;
168 GVariant *gvar_list;
169 const uint64_t *elements = NULL;
170 gsize num_elements;
171
172 if (updating_sample_rate_)
173 return;
174
175 const shared_ptr<Device> device = get_selected_device();
176 if (!device)
177 return;
178
179 assert(!updating_sample_rate_);
180 updating_sample_rate_ = true;
181
182 const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS);
183 const auto iter = keys.find(ConfigKey::SAMPLERATE);
184 if (iter != keys.end() &&
185 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
186 const auto keys = device->config_keys(
187 ConfigKey::DEVICE_OPTIONS);
188 try {
189 gvar_dict = device->config_list(ConfigKey::SAMPLERATE);
190 } catch(const sigrok::Error &e) {
191 // Failed to enunmerate samplerate
192 (void)e;
193 }
194 }
195
196 if (!gvar_dict) {
197 sample_rate_.show_none();
198 updating_sample_rate_ = false;
199 return;
200 }
201
202 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
203 "samplerate-steps", G_VARIANT_TYPE("at"))))
204 {
205 elements = (const uint64_t *)g_variant_get_fixed_array(
206 gvar_list, &num_elements, sizeof(uint64_t));
207
208 const uint64_t min = elements[0];
209 const uint64_t max = elements[1];
210 const uint64_t step = elements[2];
211
212 g_variant_unref(gvar_list);
213
214 assert(min > 0);
215 assert(max > 0);
216 assert(max > min);
217 assert(step > 0);
218
219 if (step == 1)
220 sample_rate_.show_125_list(min, max);
221 else
222 {
223 // When the step is not 1, we cam't make a 1-2-5-10
224 // list of sample rates, because we may not be able to
225 // make round numbers. Therefore in this case, show a
226 // spin box.
227 sample_rate_.show_min_max_step(min, max, step);
228 }
229 }
230 else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
231 "samplerates", G_VARIANT_TYPE("at"))))
232 {
233 elements = (const uint64_t *)g_variant_get_fixed_array(
234 gvar_list, &num_elements, sizeof(uint64_t));
235 sample_rate_.show_list(elements, num_elements);
236 g_variant_unref(gvar_list);
237 }
238 updating_sample_rate_ = false;
239
240 update_sample_rate_selector_value();
241}
242
243void SamplingBar::update_sample_rate_selector_value()
244{
245 if (updating_sample_rate_)
246 return;
247
248 const shared_ptr<Device> device = get_selected_device();
249 if (!device)
250 return;
251
252 try {
253 auto gvar = device->config_get(ConfigKey::SAMPLERATE);
254 uint64_t samplerate =
255 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
256 assert(!updating_sample_rate_);
257 updating_sample_rate_ = true;
258 sample_rate_.set_value(samplerate);
259 updating_sample_rate_ = false;
260 } catch (Error error) {
261 qDebug() << "WARNING: Failed to get value of sample rate";
262 return;
263 }
264}
265
266void SamplingBar::update_sample_count_selector()
267{
268 if (updating_sample_count_)
269 return;
270
271 const shared_ptr<Device> device = get_selected_device();
272 if (!device)
273 return;
274
275 assert(!updating_sample_count_);
276 updating_sample_count_ = true;
277
278 if (!sample_count_supported_)
279 {
280 sample_count_.show_none();
281 updating_sample_count_ = false;
282 return;
283 }
284
285 uint64_t sample_count = sample_count_.value();
286 uint64_t min_sample_count = 0;
287 uint64_t max_sample_count = MaxSampleCount;
288
289 if (sample_count == 0)
290 sample_count = DefaultSampleCount;
291
292 const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS);
293 const auto iter = keys.find(ConfigKey::LIMIT_SAMPLES);
294 if (iter != keys.end() &&
295 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
296 auto gvar = device->config_list(ConfigKey::LIMIT_SAMPLES);
297 g_variant_get(gvar.gobj(), "(tt)",
298 &min_sample_count, &max_sample_count);
299 }
300
301 min_sample_count = min(max(min_sample_count, MinSampleCount),
302 max_sample_count);
303
304 sample_count_.show_125_list(
305 min_sample_count, max_sample_count);
306
307 try {
308 auto gvar = device->config_get(ConfigKey::LIMIT_SAMPLES);
309 sample_count = g_variant_get_uint64(gvar.gobj());
310 if (sample_count == 0)
311 sample_count = DefaultSampleCount;
312 sample_count = min(max(sample_count, MinSampleCount),
313 max_sample_count);
314 } catch (Error error) {}
315
316 sample_count_.set_value(sample_count);
317
318 updating_sample_count_ = false;
319}
320
321void SamplingBar::update_device_config_widgets()
322{
323 using namespace pv::popups;
324
325 const shared_ptr<Device> device = get_selected_device();
326 if (!device)
327 return;
328
329 // Update the configure popup
330 DeviceOptions *const opts = new DeviceOptions(device, this);
331 configure_button_action_->setVisible(
332 !opts->binding().properties().empty());
333 configure_button_.set_popup(opts);
334
335 // Update the channels popup
336 Channels *const channels = new Channels(session_, this);
337 channels_button_.set_popup(channels);
338
339 // Update supported options.
340 sample_count_supported_ = false;
341
342 try {
343 for (auto entry : device->config_keys(ConfigKey::DEVICE_OPTIONS))
344 {
345 auto key = entry.first;
346 auto capabilities = entry.second;
347 switch (key->id()) {
348 case SR_CONF_LIMIT_SAMPLES:
349 if (capabilities.count(Capability::SET))
350 sample_count_supported_ = true;
351 break;
352 case SR_CONF_LIMIT_FRAMES:
353 if (capabilities.count(Capability::SET))
354 {
355 device->config_set(ConfigKey::LIMIT_FRAMES,
356 Glib::Variant<guint64>::create(1));
357 on_config_changed();
358 }
359 break;
360 default:
361 break;
362 }
363 }
364 } catch (Error error) {}
365
366 // Add notification of reconfigure events
367 disconnect(this, SLOT(on_config_changed()));
368 connect(&opts->binding(), SIGNAL(config_changed()),
369 this, SLOT(on_config_changed()));
370
371 // Update sweep timing widgets.
372 update_sample_count_selector();
373 update_sample_rate_selector();
374}
375
376void SamplingBar::commit_sample_count()
377{
378 uint64_t sample_count = 0;
379
380 if (updating_sample_count_)
381 return;
382
383 const shared_ptr<Device> device = get_selected_device();
384 if (!device)
385 return;
386
387 sample_count = sample_count_.value();
388
389 // Set the sample count
390 assert(!updating_sample_count_);
391 updating_sample_count_ = true;
392 if (sample_count_supported_)
393 {
394 try {
395 device->config_set(ConfigKey::LIMIT_SAMPLES,
396 Glib::Variant<guint64>::create(sample_count));
397 on_config_changed();
398 } catch (Error error) {
399 qDebug() << "Failed to configure sample count.";
400 return;
401 }
402 }
403 updating_sample_count_ = false;
404}
405
406void SamplingBar::commit_sample_rate()
407{
408 uint64_t sample_rate = 0;
409
410 if (updating_sample_rate_)
411 return;
412
413 const shared_ptr<Device> device = get_selected_device();
414 if (!device)
415 return;
416
417 sample_rate = sample_rate_.value();
418 if (sample_rate == 0)
419 return;
420
421 // Set the samplerate
422 assert(!updating_sample_rate_);
423 updating_sample_rate_ = true;
424 try {
425 device->config_set(ConfigKey::SAMPLERATE,
426 Glib::Variant<guint64>::create(sample_rate));
427 on_config_changed();
428 } catch (Error error) {
429 qDebug() << "Failed to configure samplerate.";
430 return;
431 }
432 updating_sample_rate_ = false;
433}
434
435void SamplingBar::on_device_selected()
436{
437 if (updating_device_selector_)
438 return;
439
440 shared_ptr<Device> device = get_selected_device();
441 if (!device)
442 return;
443
444 main_window_.select_device(device);
445
446 update_device_config_widgets();
447}
448
449void SamplingBar::on_sample_count_changed()
450{
451 commit_sample_count();
452}
453
454void SamplingBar::on_sample_rate_changed()
455{
456 commit_sample_rate();
457}
458
459void SamplingBar::on_run_stop()
460{
461 commit_sample_count();
462 commit_sample_rate();
463 main_window_.run_stop();
464}
465
466void SamplingBar::on_config_changed()
467{
468 commit_sample_count();
469 update_sample_count_selector();
470 commit_sample_rate();
471 update_sample_rate_selector();
472}
473
474bool SamplingBar::eventFilter(QObject *watched, QEvent *event)
475{
476 if ((watched == &sample_count_ || watched == &sample_rate_) &&
477 (event->type() == QEvent::ToolTip)) {
478 double sec = (double)sample_count_.value() / sample_rate_.value();
479 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
480
481 QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec));
482 QToolTip::showText(help_event->globalPos(), str);
483
484 return true;
485 }
486
487 return false;
488}
489
490} // namespace toolbars
491} // namespace pv