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/mainwindow.hpp>
34 #include <pv/popups/deviceoptions.hpp>
35 #include <pv/popups/channels.hpp>
36 #include <pv/util.hpp>
38 #include <libsigrok/libsigrok.hpp>
44 using std::shared_ptr;
47 using sigrok::Capability;
48 using sigrok::ConfigKey;
55 const uint64_t SamplingBar::MinSampleCount = 100ULL;
56 const uint64_t SamplingBar::MaxSampleCount = 1000000000000ULL;
57 const uint64_t SamplingBar::DefaultSampleCount = 1000000;
59 SamplingBar::SamplingBar(Session &session, MainWindow &main_window) :
60 QToolBar("Sampling Bar", &main_window),
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)
78 setObjectName(QString::fromUtf8("SamplingBar"));
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()));
89 sample_count_.show_min_max_step(0, UINT64_MAX, 1);
91 set_capture_state(pv::Session::Stopped);
93 configure_button_.setIcon(QIcon::fromTheme("configure",
94 QIcon(":/icons/configure.png")));
96 channels_button_.setIcon(QIcon::fromTheme("channels",
97 QIcon(":/icons/channels.svg")));
99 run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
101 addWidget(&device_selector_);
102 configure_button_action_ = addWidget(&configure_button_);
103 addWidget(&channels_button_);
104 addWidget(&sample_count_);
105 addWidget(&sample_rate_);
107 addWidget(&run_stop_button_);
109 sample_count_.installEventFilter(this);
110 sample_rate_.installEventFilter(this);
113 void SamplingBar::set_device_list(
114 const std::list< std::shared_ptr<sigrok::Device> > &devices,
115 shared_ptr<Device> selected)
117 int selected_index = -1;
121 updating_device_selector_ = true;
123 device_selector_.clear();
125 for (auto device : devices) {
128 string display_name =
129 session_.device_manager().get_display_name(device);
131 if (selected == device)
132 selected_index = device_selector_.count();
134 device_selector_.addItem(display_name.c_str(),
135 qVariantFromValue(device));
138 // The selected device should have been in the list
139 assert(selected_index != -1);
140 device_selector_.setCurrentIndex(selected_index);
142 update_device_config_widgets();
144 updating_device_selector_ = false;
147 shared_ptr<Device> SamplingBar::get_selected_device() const
149 const int index = device_selector_.currentIndex();
151 return shared_ptr<Device>();
153 return device_selector_.itemData(index).value<shared_ptr<Device>>();
156 void SamplingBar::set_capture_state(pv::Session::capture_state state)
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));
165 void SamplingBar::update_sample_rate_selector()
167 Glib::VariantContainerBase gvar_dict;
169 const uint64_t *elements = NULL;
172 if (updating_sample_rate_)
175 const shared_ptr<Device> device = get_selected_device();
179 assert(!updating_sample_rate_);
180 updating_sample_rate_ = true;
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 gvar_dict = device->config_list(ConfigKey::SAMPLERATE);
188 sample_rate_.show_none();
189 updating_sample_rate_ = false;
193 if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
194 "samplerate-steps", G_VARIANT_TYPE("at"))))
196 elements = (const uint64_t *)g_variant_get_fixed_array(
197 gvar_list, &num_elements, sizeof(uint64_t));
199 const uint64_t min = elements[0];
200 const uint64_t max = elements[1];
201 const uint64_t step = elements[2];
203 g_variant_unref(gvar_list);
211 sample_rate_.show_125_list(min, max);
214 // When the step is not 1, we cam't make a 1-2-5-10
215 // list of sample rates, because we may not be able to
216 // make round numbers. Therefore in this case, show a
218 sample_rate_.show_min_max_step(min, max, step);
221 else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(),
222 "samplerates", G_VARIANT_TYPE("at"))))
224 elements = (const uint64_t *)g_variant_get_fixed_array(
225 gvar_list, &num_elements, sizeof(uint64_t));
226 sample_rate_.show_list(elements, num_elements);
227 g_variant_unref(gvar_list);
229 updating_sample_rate_ = false;
231 update_sample_rate_selector_value();
234 void SamplingBar::update_sample_rate_selector_value()
236 if (updating_sample_rate_)
239 const shared_ptr<Device> device = get_selected_device();
244 auto gvar = device->config_get(ConfigKey::SAMPLERATE);
245 uint64_t samplerate =
246 Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get();
247 assert(!updating_sample_rate_);
248 updating_sample_rate_ = true;
249 sample_rate_.set_value(samplerate);
250 updating_sample_rate_ = false;
251 } catch (Error error) {
252 qDebug() << "WARNING: Failed to get value of sample rate";
257 void SamplingBar::update_sample_count_selector()
259 if (updating_sample_count_)
262 const shared_ptr<Device> device = get_selected_device();
266 assert(!updating_sample_count_);
267 updating_sample_count_ = true;
269 if (!sample_count_supported_)
271 sample_count_.show_none();
272 updating_sample_count_ = false;
276 uint64_t sample_count = sample_count_.value();
277 uint64_t min_sample_count = 0;
278 uint64_t max_sample_count = MaxSampleCount;
280 if (sample_count == 0)
281 sample_count = DefaultSampleCount;
283 const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS);
284 const auto iter = keys.find(ConfigKey::LIMIT_SAMPLES);
285 if (iter != keys.end() &&
286 (*iter).second.find(sigrok::LIST) != (*iter).second.end()) {
287 auto gvar = device->config_list(ConfigKey::LIMIT_SAMPLES);
288 g_variant_get(gvar.gobj(), "(tt)",
289 &min_sample_count, &max_sample_count);
292 min_sample_count = min(max(min_sample_count, MinSampleCount),
295 sample_count_.show_125_list(
296 min_sample_count, max_sample_count);
299 auto gvar = device->config_get(ConfigKey::LIMIT_SAMPLES);
300 sample_count = g_variant_get_uint64(gvar.gobj());
301 if (sample_count == 0)
302 sample_count = DefaultSampleCount;
303 sample_count = min(max(sample_count, MinSampleCount),
305 } catch (Error error) {}
307 sample_count_.set_value(sample_count);
309 updating_sample_count_ = false;
312 void SamplingBar::update_device_config_widgets()
314 using namespace pv::popups;
316 const shared_ptr<Device> device = get_selected_device();
320 // Update the configure popup
321 DeviceOptions *const opts = new DeviceOptions(device, this);
322 configure_button_action_->setVisible(
323 !opts->binding().properties().empty());
324 configure_button_.set_popup(opts);
326 // Update the channels popup
327 Channels *const channels = new Channels(session_, this);
328 channels_button_.set_popup(channels);
330 // Update supported options.
331 sample_count_supported_ = false;
334 for (auto entry : device->config_keys(ConfigKey::DEVICE_OPTIONS))
336 auto key = entry.first;
337 auto capabilities = entry.second;
339 case SR_CONF_LIMIT_SAMPLES:
340 if (capabilities.count(Capability::SET))
341 sample_count_supported_ = true;
343 case SR_CONF_LIMIT_FRAMES:
344 if (capabilities.count(Capability::SET))
346 device->config_set(ConfigKey::LIMIT_FRAMES,
347 Glib::Variant<guint64>::create(1));
355 } catch (Error error) {}
357 // Add notification of reconfigure events
358 disconnect(this, SLOT(on_config_changed()));
359 connect(&opts->binding(), SIGNAL(config_changed()),
360 this, SLOT(on_config_changed()));
362 // Update sweep timing widgets.
363 update_sample_count_selector();
364 update_sample_rate_selector();
367 void SamplingBar::commit_sample_count()
369 uint64_t sample_count = 0;
371 if (updating_sample_count_)
374 const shared_ptr<Device> device = get_selected_device();
378 sample_count = sample_count_.value();
380 // Set the sample count
381 assert(!updating_sample_count_);
382 updating_sample_count_ = true;
383 if (sample_count_supported_)
386 device->config_set(ConfigKey::LIMIT_SAMPLES,
387 Glib::Variant<guint64>::create(sample_count));
389 } catch (Error error) {
390 qDebug() << "Failed to configure sample count.";
394 updating_sample_count_ = false;
397 void SamplingBar::commit_sample_rate()
399 uint64_t sample_rate = 0;
401 if (updating_sample_rate_)
404 const shared_ptr<Device> device = get_selected_device();
408 sample_rate = sample_rate_.value();
409 if (sample_rate == 0)
412 // Set the samplerate
413 assert(!updating_sample_rate_);
414 updating_sample_rate_ = true;
416 device->config_set(ConfigKey::SAMPLERATE,
417 Glib::Variant<guint64>::create(sample_rate));
419 } catch (Error error) {
420 qDebug() << "Failed to configure samplerate.";
423 updating_sample_rate_ = false;
426 void SamplingBar::on_device_selected()
428 if (updating_device_selector_)
431 shared_ptr<Device> device = get_selected_device();
435 main_window_.select_device(device);
437 update_device_config_widgets();
440 void SamplingBar::on_sample_count_changed()
442 commit_sample_count();
445 void SamplingBar::on_sample_rate_changed()
447 commit_sample_rate();
450 void SamplingBar::on_run_stop()
452 commit_sample_count();
453 commit_sample_rate();
454 main_window_.run_stop();
457 void SamplingBar::on_config_changed()
459 commit_sample_count();
460 update_sample_count_selector();
461 commit_sample_rate();
462 update_sample_rate_selector();
465 bool SamplingBar::eventFilter(QObject *watched, QEvent *event)
467 if ((watched == &sample_count_ || watched == &sample_rate_) &&
468 (event->type() == QEvent::ToolTip)) {
469 double sec = (double)sample_count_.value() / sample_rate_.value();
470 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
472 QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec));
473 QToolTip::showText(help_event->globalPos(), str);
481 } // namespace toolbars