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/dialogs/deviceoptions.h>
39 const uint64_t SamplingBar::RecordLengths[20] = {
62 const uint64_t SamplingBar::DefaultRecordLength = 1000000;
64 SamplingBar::SamplingBar(QWidget *parent) :
65 QToolBar("Sampling Bar", parent),
66 _device_selector(this),
67 _configure_button(this),
68 _record_length_selector(this),
69 _sample_rate_list(this),
70 _icon_green(":/icons/status-green.svg"),
71 _icon_grey(":/icons/status-grey.svg"),
72 _run_stop_button(this)
74 connect(&_run_stop_button, SIGNAL(clicked()),
75 this, SLOT(on_run_stop()));
76 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
77 this, SLOT(on_device_selected()));
78 connect(&_configure_button, SIGNAL(clicked()),
79 this, SLOT(on_configure()));
81 _sample_rate_value.setDecimals(0);
82 _sample_rate_value.setSuffix("Hz");
84 for (size_t i = 0; i < countof(RecordLengths); i++)
86 const uint64_t &l = RecordLengths[i];
87 char *const text = sr_si_string_u64(l, " samples");
88 _record_length_selector.addItem(QString(text),
89 qVariantFromValue(l));
92 if (l == DefaultRecordLength)
93 _record_length_selector.setCurrentIndex(i);
98 _configure_button.setIcon(QIcon::fromTheme("configure",
99 QIcon(":/icons/configure.png")));
101 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
103 addWidget(&_device_selector);
104 addWidget(&_configure_button);
105 addWidget(&_record_length_selector);
106 _sample_rate_list_action = addWidget(&_sample_rate_list);
107 _sample_rate_value_action = addWidget(&_sample_rate_value);
108 addWidget(&_run_stop_button);
110 connect(&_sample_rate_list, SIGNAL(currentIndexChanged(int)),
111 this, SLOT(on_sample_rate_changed()));
112 connect(&_sample_rate_value, SIGNAL(editingFinished()),
113 this, SLOT(on_sample_rate_changed()));
116 void SamplingBar::set_device_list(
117 const std::list<struct sr_dev_inst*> &devices)
119 _device_selector.clear();
121 BOOST_FOREACH (sr_dev_inst *sdi, devices) {
123 if (sdi->vendor && sdi->vendor[0])
124 title += sdi->vendor + QString(" ");
125 if (sdi->model && sdi->model[0])
126 title += sdi->model + QString(" ");
127 if (sdi->version && sdi->version[0])
128 title += sdi->version + QString(" ");
130 _device_selector.addItem(title, qVariantFromValue(
134 update_sample_rate_selector();
137 struct sr_dev_inst* SamplingBar::get_selected_device() const
139 const int index = _device_selector.currentIndex();
143 return (sr_dev_inst*)_device_selector.itemData(
144 index).value<void*>();
147 void SamplingBar::set_selected_device(struct sr_dev_inst *const sdi)
149 for (int i = 0; i < _device_selector.count(); i++)
150 if (sdi == _device_selector.itemData(i).value<void*>()) {
151 _device_selector.setCurrentIndex(i);
156 uint64_t SamplingBar::get_record_length() const
158 const int index = _record_length_selector.currentIndex();
162 return _record_length_selector.itemData(index).value<uint64_t>();
165 void SamplingBar::set_sampling(bool sampling)
167 _run_stop_button.setIcon(sampling ? _icon_green : _icon_grey);
168 _run_stop_button.setText(sampling ? "Stop" : "Run");
171 void SamplingBar::update_sample_rate_selector()
173 const sr_dev_inst *const sdi = get_selected_device();
174 GVariant *gvar_dict, *gvar_list;
175 const uint64_t *elements = NULL;
178 assert(_sample_rate_value_action);
179 assert(_sample_rate_list_action);
184 if (sr_config_list(sdi->driver, SR_CONF_SAMPLERATE,
185 &gvar_dict, sdi) != SR_OK)
188 _sample_rate_list_action->setVisible(false);
189 _sample_rate_value_action->setVisible(false);
191 if ((gvar_list = g_variant_lookup_value(gvar_dict,
192 "samplerate-steps", G_VARIANT_TYPE("at")))) {
193 elements = (const uint64_t *)g_variant_get_fixed_array(
194 gvar_list, &num_elements, sizeof(uint64_t));
195 _sample_rate_value.setRange(elements[0], elements[1]);
196 _sample_rate_value.setSingleStep(elements[2]);
197 _sample_rate_value_action->setVisible(true);
198 g_variant_unref(gvar_list);
200 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
201 "samplerates", G_VARIANT_TYPE("at"))))
203 elements = (const uint64_t *)g_variant_get_fixed_array(
204 gvar_list, &num_elements, sizeof(uint64_t));
205 _sample_rate_list.clear();
207 for (unsigned int i = 0; i < num_elements; i++)
209 char *const s = sr_samplerate_string(elements[i]);
210 _sample_rate_list.addItem(QString(s),
211 qVariantFromValue(elements[i]));
215 _sample_rate_list.show();
216 _sample_rate_list_action->setVisible(true);
217 g_variant_unref(gvar_list);
220 g_variant_unref(gvar_dict);
221 update_sample_rate_selector_value();
224 void SamplingBar::update_sample_rate_selector_value()
226 sr_dev_inst *const sdi = get_selected_device();
232 if (sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
233 &gvar, sdi) != SR_OK) {
235 "WARNING: Failed to get value of sample rate";
238 samplerate = g_variant_get_uint64(gvar);
239 g_variant_unref(gvar);
241 assert(_sample_rate_value_action);
242 assert(_sample_rate_list_action);
244 if (_sample_rate_value_action->isVisible())
245 _sample_rate_value.setValue(samplerate);
246 else if (_sample_rate_list_action->isVisible())
248 for (int i = 0; i < _sample_rate_list.count(); i++)
249 if (samplerate == _sample_rate_list.itemData(
250 i).value<uint64_t>())
251 _sample_rate_list.setCurrentIndex(i);
255 void SamplingBar::commit_sample_rate()
257 uint64_t sample_rate = 0;
259 sr_dev_inst *const sdi = get_selected_device();
262 assert(_sample_rate_value_action);
263 assert(_sample_rate_list_action);
265 if (_sample_rate_value_action->isVisible())
266 sample_rate = (uint64_t)_sample_rate_value.value();
267 else if (_sample_rate_list_action->isVisible())
269 const int index = _sample_rate_list.currentIndex();
271 sample_rate = _sample_rate_list.itemData(
272 index).value<uint64_t>();
275 // Set the samplerate
276 if (sr_config_set(sdi, SR_CONF_SAMPLERATE,
277 g_variant_new_uint64(sample_rate)) != SR_OK) {
278 qDebug() << "Failed to configure samplerate.";
283 void SamplingBar::on_device_selected()
285 update_sample_rate_selector();
288 void SamplingBar::on_sample_rate_changed()
290 commit_sample_rate();
293 void SamplingBar::on_configure()
295 commit_sample_rate();
297 sr_dev_inst *const sdi = get_selected_device();
300 pv::dialogs::DeviceOptions dlg(this, sdi);
303 update_sample_rate_selector_value();
306 void SamplingBar::on_run_stop()
308 commit_sample_rate();
312 } // namespace toolbars