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 uint64_t SamplingBar::get_record_length() const
131 return _sample_count.value();
134 void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
136 const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
137 _run_stop_button.setIcon(*icons[state]);
138 _run_stop_button.setText((state == pv::SigSession::Stopped) ?
139 tr("Run") : tr("Stop"));
142 void SamplingBar::update_sample_rate_selector()
144 const sr_dev_inst *const sdi = get_selected_device();
145 GVariant *gvar_dict, *gvar_list;
146 const uint64_t *elements = NULL;
152 _updating_sample_rate = true;
154 if (sr_config_list(sdi->driver, sdi, NULL,
155 SR_CONF_SAMPLERATE, &gvar_dict) != SR_OK)
157 _sample_rate.show_none();
158 _updating_sample_rate = false;
162 if ((gvar_list = g_variant_lookup_value(gvar_dict,
163 "samplerate-steps", G_VARIANT_TYPE("at"))))
165 elements = (const uint64_t *)g_variant_get_fixed_array(
166 gvar_list, &num_elements, sizeof(uint64_t));
167 _sample_rate.show_min_max_step(elements[0], elements[1],
169 g_variant_unref(gvar_list);
171 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
172 "samplerates", G_VARIANT_TYPE("at"))))
174 elements = (const uint64_t *)g_variant_get_fixed_array(
175 gvar_list, &num_elements, sizeof(uint64_t));
176 _sample_rate.show_list(elements, num_elements);
177 g_variant_unref(gvar_list);
179 _updating_sample_rate = false;
181 g_variant_unref(gvar_dict);
182 update_sample_rate_selector_value();
185 void SamplingBar::update_sample_rate_selector_value()
187 sr_dev_inst *const sdi = get_selected_device();
193 if (sr_config_get(sdi->driver, sdi, NULL,
194 SR_CONF_SAMPLERATE, &gvar) != SR_OK) {
196 "WARNING: Failed to get value of sample rate";
199 samplerate = g_variant_get_uint64(gvar);
200 g_variant_unref(gvar);
202 _updating_sample_rate = true;
203 _sample_rate.set_value(samplerate);
204 _updating_sample_rate = false;
207 void SamplingBar::update_sample_count_selector()
209 sr_dev_inst *const sdi = get_selected_device();
211 uint64_t samplecount;
215 if (sr_config_get(sdi->driver, sdi, NULL,
216 SR_CONF_LIMIT_SAMPLES, &gvar) != SR_OK)
218 _sample_count.show_none();
222 _sample_count.show_min_max_step(0, UINT64_MAX, 1);
224 samplecount = g_variant_get_uint64(gvar);
225 g_variant_unref(gvar);
227 _updating_sample_count = true;
228 _sample_count.set_value(samplecount);
229 _updating_sample_count = false;
233 void SamplingBar::commit_sample_count()
235 uint64_t sample_count = 0;
237 sr_dev_inst *const sdi = get_selected_device();
240 sample_count = _sample_count.value();
242 // Set the sample count
243 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES,
244 g_variant_new_uint64(sample_count)) != SR_OK) {
245 qDebug() << "Failed to configure sample count.";
250 void SamplingBar::commit_sample_rate()
252 uint64_t sample_rate = 0;
254 sr_dev_inst *const sdi = get_selected_device();
257 sample_rate = _sample_rate.value();
258 if (sample_rate == 0)
261 // Set the samplerate
262 if (sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE,
263 g_variant_new_uint64(sample_rate)) != SR_OK) {
264 qDebug() << "Failed to configure samplerate.";
269 void SamplingBar::on_device_selected()
271 using namespace pv::popups;
273 if (_updating_device_selector)
276 update_sample_count_selector();
277 update_sample_rate_selector();
279 if (_sample_count.value() == 0)
280 _sample_count.set_value(DefaultRecordLength);
282 sr_dev_inst *const sdi = get_selected_device();
283 _session.set_device(sdi);
285 // Update the configure popup
286 DeviceOptions *const opts = new DeviceOptions(sdi, this);
287 _configure_button_action->setVisible(
288 !opts->binding().properties().empty());
289 _configure_button.set_popup(opts);
291 // Update the probes popup
292 Probes *const probes = new Probes(_session, this);
293 _probes_button.set_popup(probes);
296 void SamplingBar::on_sample_count_changed()
298 if(!_updating_sample_count)
299 commit_sample_count();
302 void SamplingBar::on_sample_rate_changed()
304 if (!_updating_sample_rate)
305 commit_sample_rate();
308 void SamplingBar::on_run_stop()
310 commit_sample_rate();
314 } // namespace toolbars