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>
42 const uint64_t SamplingBar::RecordLengths[20] = {
65 const uint64_t SamplingBar::DefaultRecordLength = 1000000;
67 SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
68 QToolBar("Sampling Bar", parent),
70 _device_selector(this),
71 _updating_device_selector(false),
72 _configure_button(this),
73 _configure_button_action(NULL),
75 _probes_popup(_session, this),
76 _record_length_selector(this),
77 _sample_rate_action(NULL),
78 _sample_rate_list(this),
79 _icon_red(":/icons/status-red.svg"),
80 _icon_green(":/icons/status-green.svg"),
81 _icon_grey(":/icons/status-grey.svg"),
82 _run_stop_button(this)
84 connect(&_run_stop_button, SIGNAL(clicked()),
85 this, SLOT(on_run_stop()));
86 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
87 this, SLOT(on_device_selected()));
89 _sample_rate_value.setDecimals(0);
90 _sample_rate_value.setSuffix("Hz");
92 for (size_t i = 0; i < countof(RecordLengths); i++)
94 const uint64_t &l = RecordLengths[i];
95 char *const text = sr_si_string_u64(l, " samples");
96 _record_length_selector.addItem(QString::fromUtf8(text),
97 qVariantFromValue(l));
100 if (l == DefaultRecordLength)
101 _record_length_selector.setCurrentIndex(i);
104 set_capture_state(pv::SigSession::Stopped);
106 _configure_button.setIcon(QIcon::fromTheme("configure",
107 QIcon(":/icons/configure.png")));
109 _probes_button.setIcon(QIcon::fromTheme("probes",
110 QIcon(":/icons/probes.svg")));
111 _probes_button.set_popup(&_probes_popup);
113 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
115 addWidget(&_device_selector);
116 _configure_button_action = addWidget(&_configure_button);
117 addWidget(&_probes_button);
118 addWidget(&_record_length_selector);
119 _sample_rate_list_action = addWidget(&_sample_rate_list);
120 _sample_rate_value_action = addWidget(&_sample_rate_value);
121 addWidget(&_run_stop_button);
123 connect(&_sample_rate_list, SIGNAL(currentIndexChanged(int)),
124 this, SLOT(on_sample_rate_changed()));
125 connect(&_sample_rate_value, SIGNAL(editingFinished()),
126 this, SLOT(on_sample_rate_changed()));
129 void SamplingBar::set_device_list(
130 const std::list<struct sr_dev_inst*> &devices)
132 _updating_device_selector = true;
134 _device_selector.clear();
136 BOOST_FOREACH (sr_dev_inst *sdi, devices) {
137 const string title = DeviceManager::format_device_title(sdi);
138 _device_selector.addItem(title.c_str(),
139 qVariantFromValue((void*)sdi));
142 _updating_device_selector = false;
144 on_device_selected();
147 struct sr_dev_inst* SamplingBar::get_selected_device() const
149 const int index = _device_selector.currentIndex();
153 return (sr_dev_inst*)_device_selector.itemData(
154 index).value<void*>();
157 void SamplingBar::set_selected_device(struct sr_dev_inst *const sdi)
159 for (int i = 0; i < _device_selector.count(); i++)
160 if (sdi == _device_selector.itemData(i).value<void*>()) {
161 _device_selector.setCurrentIndex(i);
166 uint64_t SamplingBar::get_record_length() const
168 const int index = _record_length_selector.currentIndex();
172 return _record_length_selector.itemData(index).value<uint64_t>();
175 void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
177 const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
178 _run_stop_button.setIcon(*icons[state]);
179 _run_stop_button.setText((state == pv::SigSession::Stopped) ?
180 tr("Run") : tr("Stop"));
183 void SamplingBar::update_sample_rate_selector()
185 const sr_dev_inst *const sdi = get_selected_device();
186 GVariant *gvar_dict, *gvar_list;
187 const uint64_t *elements = NULL;
190 assert(_sample_rate_value_action);
191 assert(_sample_rate_list_action);
196 if (sr_config_list(sdi->driver, sdi, NULL,
197 SR_CONF_SAMPLERATE, &gvar_dict) != SR_OK)
200 _sample_rate_list_action->setVisible(false);
201 _sample_rate_value_action->setVisible(false);
203 if ((gvar_list = g_variant_lookup_value(gvar_dict,
204 "samplerate-steps", G_VARIANT_TYPE("at")))) {
205 elements = (const uint64_t *)g_variant_get_fixed_array(
206 gvar_list, &num_elements, sizeof(uint64_t));
207 _sample_rate_value.setRange(elements[0], elements[1]);
208 _sample_rate_value.setSingleStep(elements[2]);
209 g_variant_unref(gvar_list);
211 _sample_rate_action = _sample_rate_value_action;
213 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
214 "samplerates", G_VARIANT_TYPE("at"))))
216 elements = (const uint64_t *)g_variant_get_fixed_array(
217 gvar_list, &num_elements, sizeof(uint64_t));
218 _sample_rate_list.clear();
220 for (unsigned int i = 0; i < num_elements; i++)
222 char *const s = sr_samplerate_string(elements[i]);
223 _sample_rate_list.addItem(QString::fromUtf8(s),
224 qVariantFromValue(elements[i]));
228 _sample_rate_list.show();
229 g_variant_unref(gvar_list);
231 _sample_rate_action = _sample_rate_list_action;
234 g_variant_unref(gvar_dict);
235 update_sample_rate_selector_value();
237 // We delay showing the action, so that value change events
239 if (_sample_rate_action)
240 _sample_rate_action->setVisible(true);
243 void SamplingBar::update_sample_rate_selector_value()
245 sr_dev_inst *const sdi = get_selected_device();
251 if (sr_config_get(sdi->driver, sdi, NULL,
252 SR_CONF_SAMPLERATE, &gvar) != SR_OK) {
254 "WARNING: Failed to get value of sample rate";
257 samplerate = g_variant_get_uint64(gvar);
258 g_variant_unref(gvar);
260 assert(_sample_rate_value_action);
261 assert(_sample_rate_list_action);
263 if (_sample_rate_action == _sample_rate_value_action)
264 _sample_rate_value.setValue(samplerate);
265 else if (_sample_rate_action == _sample_rate_list_action)
267 for (int i = 0; i < _sample_rate_list.count(); i++)
268 if (samplerate == _sample_rate_list.itemData(
269 i).value<uint64_t>())
270 _sample_rate_list.setCurrentIndex(i);
274 void SamplingBar::commit_sample_rate()
276 uint64_t sample_rate = 0;
278 sr_dev_inst *const sdi = get_selected_device();
281 assert(_sample_rate_value_action);
282 assert(_sample_rate_list_action);
284 if (_sample_rate_action == _sample_rate_value_action)
285 sample_rate = (uint64_t)_sample_rate_value.value();
286 else if (_sample_rate_action == _sample_rate_list_action)
288 const int index = _sample_rate_list.currentIndex();
290 sample_rate = _sample_rate_list.itemData(
291 index).value<uint64_t>();
294 if (sample_rate == 0)
297 // Set the samplerate
298 if (sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE,
299 g_variant_new_uint64(sample_rate)) != SR_OK) {
300 qDebug() << "Failed to configure samplerate.";
305 void SamplingBar::on_device_selected()
307 using pv::popups::DeviceOptions;
309 if (_updating_device_selector)
312 update_sample_rate_selector();
314 sr_dev_inst *const sdi = get_selected_device();
315 _session.set_device(sdi);
317 // Update the configure popup
318 DeviceOptions *const opts = new DeviceOptions(sdi, this);
319 _configure_button_action->setVisible(
320 !opts->binding().properties().empty());
321 _configure_button.set_popup(opts);
324 void SamplingBar::on_sample_rate_changed()
326 commit_sample_rate();
329 void SamplingBar::on_run_stop()
331 commit_sample_rate();
335 } // namespace toolbars