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
25 #include <boost/foreach.hpp>
27 #include <libsigrok/libsigrok.h>
32 #include "samplingbar.h"
34 #include <pv/devicemanager.h>
35 #include <pv/popups/deviceoptions.h>
36 #include <pv/popups/probes.h>
43 const uint64_t SamplingBar::DefaultRecordLength = 1000000;
45 SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
46 QToolBar("Sampling Bar", parent),
48 _device_selector(this),
49 _updating_device_selector(false),
50 _configure_button(this),
51 _configure_button_action(NULL),
53 _sample_count(" samples", this),
54 _sample_rate("Hz", this),
55 _updating_sample_rate(false),
56 _updating_sample_count(false),
57 _icon_red(":/icons/status-red.svg"),
58 _icon_green(":/icons/status-green.svg"),
59 _icon_grey(":/icons/status-grey.svg"),
60 _run_stop_button(this)
62 connect(&_run_stop_button, SIGNAL(clicked()),
63 this, SLOT(on_run_stop()));
64 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
65 this, SLOT(on_device_selected()));
66 connect(&_sample_count, SIGNAL(value_changed()),
67 this, SLOT(on_sample_count_changed()));
68 connect(&_sample_rate, SIGNAL(value_changed()),
69 this, SLOT(on_sample_rate_changed()));
71 _sample_count.show_min_max_step(0, UINT64_MAX, 1);
73 set_capture_state(pv::SigSession::Stopped);
75 _configure_button.setIcon(QIcon::fromTheme("configure",
76 QIcon(":/icons/configure.png")));
78 _probes_button.setIcon(QIcon::fromTheme("probes",
79 QIcon(":/icons/probes.svg")));
81 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
83 addWidget(&_device_selector);
84 _configure_button_action = addWidget(&_configure_button);
85 addWidget(&_probes_button);
86 addWidget(&_sample_count);
87 addWidget(&_sample_rate);
89 addWidget(&_run_stop_button);
92 void SamplingBar::set_device_list(
93 const std::list<struct sr_dev_inst*> &devices)
95 _updating_device_selector = true;
97 _device_selector.clear();
99 BOOST_FOREACH (sr_dev_inst *sdi, devices) {
100 const string title = DeviceManager::format_device_title(sdi);
101 _device_selector.addItem(title.c_str(),
102 qVariantFromValue((void*)sdi));
105 _updating_device_selector = false;
107 on_device_selected();
110 struct sr_dev_inst* SamplingBar::get_selected_device() const
112 const int index = _device_selector.currentIndex();
116 return (sr_dev_inst*)_device_selector.itemData(
117 index).value<void*>();
120 void SamplingBar::set_selected_device(struct sr_dev_inst *const sdi)
122 for (int i = 0; i < _device_selector.count(); i++)
123 if (sdi == _device_selector.itemData(i).value<void*>()) {
124 _device_selector.setCurrentIndex(i);
129 void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
131 const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
132 _run_stop_button.setIcon(*icons[state]);
133 _run_stop_button.setText((state == pv::SigSession::Stopped) ?
134 tr("Run") : tr("Stop"));
137 void SamplingBar::update_sample_rate_selector()
139 const sr_dev_inst *const sdi = get_selected_device();
140 GVariant *gvar_dict, *gvar_list;
141 const uint64_t *elements = NULL;
147 _updating_sample_rate = true;
149 if (sr_config_list(sdi->driver, sdi, NULL,
150 SR_CONF_SAMPLERATE, &gvar_dict) != SR_OK)
152 _sample_rate.show_none();
153 _updating_sample_rate = false;
157 if ((gvar_list = g_variant_lookup_value(gvar_dict,
158 "samplerate-steps", G_VARIANT_TYPE("at"))))
160 elements = (const uint64_t *)g_variant_get_fixed_array(
161 gvar_list, &num_elements, sizeof(uint64_t));
162 _sample_rate.show_min_max_step(elements[0], elements[1],
164 g_variant_unref(gvar_list);
166 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
167 "samplerates", G_VARIANT_TYPE("at"))))
169 elements = (const uint64_t *)g_variant_get_fixed_array(
170 gvar_list, &num_elements, sizeof(uint64_t));
171 _sample_rate.show_list(elements, num_elements);
172 g_variant_unref(gvar_list);
174 _updating_sample_rate = false;
176 g_variant_unref(gvar_dict);
177 update_sample_rate_selector_value();
180 void SamplingBar::update_sample_rate_selector_value()
182 sr_dev_inst *const sdi = get_selected_device();
188 if (sr_config_get(sdi->driver, sdi, NULL,
189 SR_CONF_SAMPLERATE, &gvar) != SR_OK) {
191 "WARNING: Failed to get value of sample rate";
194 samplerate = g_variant_get_uint64(gvar);
195 g_variant_unref(gvar);
197 _updating_sample_rate = true;
198 _sample_rate.set_value(samplerate);
199 _updating_sample_rate = false;
202 void SamplingBar::update_sample_count_selector()
204 sr_dev_inst *const sdi = get_selected_device();
206 uint64_t samplecount;
210 if (sr_config_get(sdi->driver, sdi, NULL,
211 SR_CONF_LIMIT_SAMPLES, &gvar) != SR_OK)
213 _sample_count.show_none();
217 _sample_count.show_min_max_step(0, UINT64_MAX, 1);
219 samplecount = g_variant_get_uint64(gvar);
220 g_variant_unref(gvar);
222 _updating_sample_count = true;
223 _sample_count.set_value(samplecount);
224 _updating_sample_count = false;
228 void SamplingBar::commit_sample_count()
230 uint64_t sample_count = 0;
232 sr_dev_inst *const sdi = get_selected_device();
235 sample_count = _sample_count.value();
237 // Set the sample count
238 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES,
239 g_variant_new_uint64(sample_count)) != SR_OK) {
240 qDebug() << "Failed to configure sample count.";
245 void SamplingBar::commit_sample_rate()
247 uint64_t sample_rate = 0;
249 sr_dev_inst *const sdi = get_selected_device();
252 sample_rate = _sample_rate.value();
253 if (sample_rate == 0)
256 // Set the samplerate
257 if (sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE,
258 g_variant_new_uint64(sample_rate)) != SR_OK) {
259 qDebug() << "Failed to configure samplerate.";
264 void SamplingBar::on_device_selected()
266 using namespace pv::popups;
268 if (_updating_device_selector)
271 sr_dev_inst *const sdi = get_selected_device();
272 _session.set_device(sdi);
274 // Update the configure popup
275 DeviceOptions *const opts = new DeviceOptions(sdi, this);
276 _configure_button_action->setVisible(
277 !opts->binding().properties().empty());
278 _configure_button.set_popup(opts);
280 // Update the probes popup
281 Probes *const probes = new Probes(_session, this);
282 _probes_button.set_popup(probes);
284 // Update sweep timing widgets.
285 update_sample_count_selector();
286 update_sample_rate_selector();
288 if (_sample_count.value() == 0)
289 _sample_count.set_value(DefaultRecordLength);
292 void SamplingBar::on_sample_count_changed()
294 if(!_updating_sample_count)
295 commit_sample_count();
298 void SamplingBar::on_sample_rate_changed()
300 if (!_updating_sample_rate)
301 commit_sample_rate();
304 void SamplingBar::on_run_stop()
306 commit_sample_rate();
310 } // namespace toolbars