2 * This file is part of the PulseView project.
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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
30 #include "samplingbar.hpp"
32 #include <pv/devicemanager.hpp>
33 #include <pv/popups/deviceoptions.hpp>
34 #include <pv/popups/channels.hpp>
35 #include <pv/util.hpp>
37 #include <libsigrok/libsigrok.hpp>
43 using std::shared_ptr;
46 using sigrok::Capability;
47 using sigrok::ConfigKey;
54 const uint64_t SamplingBar::MinSampleCount = 100ULL;
55 const uint64_t SamplingBar::MaxSampleCount = 1000000000000ULL;
56 const uint64_t SamplingBar::DefaultSampleCount = 1000000;
58 SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
59 QToolBar("Sampling Bar", parent),
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)
76 setObjectName(QString::fromUtf8("SamplingBar"));
78 connect(&run_stop_button_, SIGNAL(clicked()),
79 this, SLOT(on_run_stop()));
80 connect(&device_selector_, SIGNAL(currentIndexChanged (int)),
81 this, SLOT(on_device_selected()));
82 connect(&sample_count_, SIGNAL(value_changed()),
83 this, SLOT(on_sample_count_changed()));
84 connect(&sample_rate_, SIGNAL(value_changed()),
85 this, SLOT(on_sample_rate_changed()));
87 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
89 set_capture_state(pv::SigSession::Stopped);
91 configure_button_.setIcon(QIcon::fromTheme("configure",
92 QIcon(":/icons/configure.png")));
94 channels_button_.setIcon(QIcon::fromTheme("channels",
95 QIcon(":/icons/channels.svg")));
97 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
99 addWidget(&device_selector_);
100 configure_button_action_ = addWidget(&configure_button_);
101 addWidget(&channels_button_);
102 addWidget(&sample_count_);
103 addWidget(&sample_rate_);
105 addWidget(&run_stop_button_);
107 sample_count_.installEventFilter(this);
108 sample_rate_.installEventFilter(this);
111 void SamplingBar::set_device_list(
112 const std::list< std::pair<std::shared_ptr<sigrok::Device>, std::string> > &devices,
113 shared_ptr<Device> selected)
115 int selected_index = -1;
119 updating_device_selector_ = true;
121 device_selector_.clear();
123 for (auto entry : devices) {
124 auto device = entry.first;
125 auto display_name = entry.second;
129 if (selected == device)
130 selected_index = device_selector_.count();
132 device_selector_.addItem(display_name.c_str(),
133 qVariantFromValue(device));
136 // The selected device should have been in the list
137 assert(selected_index != -1);
138 device_selector_.setCurrentIndex(selected_index);
140 update_device_config_widgets();
142 updating_device_selector_ = false;
145 shared_ptr<Device> SamplingBar::get_selected_device() const
147 const int index = device_selector_.currentIndex();
149 return shared_ptr<Device>();
151 return device_selector_.itemData(index).value<shared_ptr<Device>>();
154 void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
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) ?
159 tr("Run") : tr("Stop"));
160 run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space));
163 void SamplingBar::update_sample_rate_selector()
165 Glib::VariantContainerBase gvar_dict;
167 const uint64_t *elements = NULL;
170 if (updating_sample_rate_)
173 const shared_ptr<Device> device = get_selected_device();
177 assert(!updating_sample_rate_);
178 updating_sample_rate_ = true;
180 const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS);
181 const auto iter = keys.find(ConfigKey::SAMPLERATE);
182 if (iter != keys.end() &&
183 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
184 gvar_dict = device->config_list(ConfigKey::SAMPLERATE);
186 sample_rate_.show_none();
187 updating_sample_rate_ = false;
191 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
192 "samplerate-steps", G_VARIANT_TYPE("at"))))
194 elements = (const uint64_t *)g_variant_get_fixed_array(
195 gvar_list, &num_elements, sizeof(uint64_t));
197 const uint64_t min = elements[0];
198 const uint64_t max = elements[1];
199 const uint64_t step = elements[2];
201 g_variant_unref(gvar_list);
209 sample_rate_.show_125_list(min, max);
212 // When the step is not 1, we cam't make a 1-2-5-10
213 // list of sample rates, because we may not be able to
214 // make round numbers. Therefore in this case, show a
216 sample_rate_.show_min_max_step(min, max, step);
219 else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
220 "samplerates", G_VARIANT_TYPE("at"))))
222 elements = (const uint64_t *)g_variant_get_fixed_array(
223 gvar_list, &num_elements, sizeof(uint64_t));
224 sample_rate_.show_list(elements, num_elements);
225 g_variant_unref(gvar_list);
227 updating_sample_rate_ = false;
229 update_sample_rate_selector_value();
232 void SamplingBar::update_sample_rate_selector_value()
234 if (updating_sample_rate_)
237 const shared_ptr<Device> device = get_selected_device();
242 auto gvar = device->config_get(ConfigKey::SAMPLERATE);
243 uint64_t samplerate =
244 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
245 assert(!updating_sample_rate_);
246 updating_sample_rate_ = true;
247 sample_rate_.set_value(samplerate);
248 updating_sample_rate_ = false;
249 } catch (Error error) {
250 qDebug() << "WARNING: Failed to get value of sample rate";
255 void SamplingBar::update_sample_count_selector()
257 if (updating_sample_count_)
260 const shared_ptr<Device> device = get_selected_device();
264 assert(!updating_sample_count_);
265 updating_sample_count_ = true;
267 if (!sample_count_supported_)
269 sample_count_.show_none();
270 updating_sample_count_ = false;
274 uint64_t sample_count = sample_count_.value();
275 uint64_t min_sample_count = 0;
276 uint64_t max_sample_count = MaxSampleCount;
278 if (sample_count == 0)
279 sample_count = DefaultSampleCount;
281 const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS);
282 const auto iter = keys.find(ConfigKey::LIMIT_SAMPLES);
283 if (iter != keys.end() &&
284 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
285 auto gvar = device->config_list(ConfigKey::LIMIT_SAMPLES);
286 g_variant_get(gvar.gobj(), "(tt)",
287 &min_sample_count, &max_sample_count);
290 min_sample_count = min(max(min_sample_count, MinSampleCount),
293 sample_count_.show_125_list(
294 min_sample_count, max_sample_count);
297 auto gvar = device->config_get(ConfigKey::LIMIT_SAMPLES);
298 sample_count = g_variant_get_uint64(gvar.gobj());
299 if (sample_count == 0)
300 sample_count = DefaultSampleCount;
301 sample_count = min(max(sample_count, MinSampleCount),
303 } catch (Error error) {}
305 sample_count_.set_value(sample_count);
307 updating_sample_count_ = false;
310 void SamplingBar::update_device_config_widgets()
312 using namespace pv::popups;
314 const shared_ptr<Device> device = get_selected_device();
318 // Update the configure popup
319 DeviceOptions *const opts = new DeviceOptions(device, this);
320 configure_button_action_->setVisible(
321 !opts->binding().properties().empty());
322 configure_button_.set_popup(opts);
324 // Update the channels popup
325 Channels *const channels = new Channels(session_, this);
326 channels_button_.set_popup(channels);
328 // Update supported options.
329 sample_count_supported_ = false;
332 for (auto entry : device->config_keys(ConfigKey::DEVICE_OPTIONS))
334 auto key = entry.first;
335 auto capabilities = entry.second;
337 case SR_CONF_LIMIT_SAMPLES:
338 if (capabilities.count(Capability::SET))
339 sample_count_supported_ = true;
341 case SR_CONF_LIMIT_FRAMES:
342 if (capabilities.count(Capability::SET))
344 device->config_set(ConfigKey::LIMIT_FRAMES,
345 Glib::Variant<guint64>::create(1));
353 } catch (Error error) {}
355 // Add notification of reconfigure events
356 disconnect(this, SLOT(on_config_changed()));
357 connect(&opts->binding(), SIGNAL(config_changed()),
358 this, SLOT(on_config_changed()));
360 // Update sweep timing widgets.
361 update_sample_count_selector();
362 update_sample_rate_selector();
365 void SamplingBar::commit_sample_count()
367 uint64_t sample_count = 0;
369 if (updating_sample_count_)
372 const shared_ptr<Device> device = get_selected_device();
376 sample_count = sample_count_.value();
378 // Set the sample count
379 assert(!updating_sample_count_);
380 updating_sample_count_ = true;
381 if (sample_count_supported_)
384 device->config_set(ConfigKey::LIMIT_SAMPLES,
385 Glib::Variant<guint64>::create(sample_count));
387 } catch (Error error) {
388 qDebug() << "Failed to configure sample count.";
392 updating_sample_count_ = false;
395 void SamplingBar::commit_sample_rate()
397 uint64_t sample_rate = 0;
399 if (updating_sample_rate_)
402 const shared_ptr<Device> device = get_selected_device();
406 sample_rate = sample_rate_.value();
407 if (sample_rate == 0)
410 // Set the samplerate
411 assert(!updating_sample_rate_);
412 updating_sample_rate_ = true;
414 device->config_set(ConfigKey::SAMPLERATE,
415 Glib::Variant<guint64>::create(sample_rate));
417 } catch (Error error) {
418 qDebug() << "Failed to configure samplerate.";
421 updating_sample_rate_ = false;
424 void SamplingBar::on_device_selected()
426 if (updating_device_selector_)
429 shared_ptr<Device> device = get_selected_device();
433 session_.set_device(device);
435 update_device_config_widgets();
438 void SamplingBar::on_sample_count_changed()
440 commit_sample_count();
443 void SamplingBar::on_sample_rate_changed()
445 commit_sample_rate();
448 void SamplingBar::on_run_stop()
450 commit_sample_count();
451 commit_sample_rate();
455 void SamplingBar::on_config_changed()
457 commit_sample_count();
458 update_sample_count_selector();
459 commit_sample_rate();
460 update_sample_rate_selector();
463 bool SamplingBar::eventFilter(QObject *watched, QEvent *event)
465 if ((watched == &sample_count_ || watched == &sample_rate_) &&
466 (event->type() == QEvent::ToolTip)) {
467 double sec = (double)sample_count_.value() / sample_rate_.value();
468 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
470 QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec));
471 QToolTip::showText(help_event->globalPos(), str);
479 } // namespace toolbars