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),
74 _probes_popup(_session, this),
75 _record_length_selector(this),
76 _sample_rate_action(NULL),
77 _sample_rate_list(this),
78 _icon_red(":/icons/status-red.svg"),
79 _icon_green(":/icons/status-green.svg"),
80 _icon_grey(":/icons/status-grey.svg"),
81 _run_stop_button(this)
83 connect(&_run_stop_button, SIGNAL(clicked()),
84 this, SLOT(on_run_stop()));
85 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
86 this, SLOT(on_device_selected()));
88 _sample_rate_value.setDecimals(0);
89 _sample_rate_value.setSuffix("Hz");
91 for (size_t i = 0; i < countof(RecordLengths); i++)
93 const uint64_t &l = RecordLengths[i];
94 char *const text = sr_si_string_u64(l, " samples");
95 _record_length_selector.addItem(QString::fromUtf8(text),
96 qVariantFromValue(l));
99 if (l == DefaultRecordLength)
100 _record_length_selector.setCurrentIndex(i);
103 set_capture_state(pv::SigSession::Stopped);
105 _configure_button.setIcon(QIcon::fromTheme("configure",
106 QIcon(":/icons/configure.png")));
108 _probes_button.setIcon(QIcon::fromTheme("probes",
109 QIcon(":/icons/probes.svg")));
110 _probes_button.set_popup(&_probes_popup);
112 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
114 addWidget(&_device_selector);
115 addWidget(&_configure_button);
116 addWidget(&_probes_button);
117 addWidget(&_record_length_selector);
118 _sample_rate_list_action = addWidget(&_sample_rate_list);
119 _sample_rate_value_action = addWidget(&_sample_rate_value);
120 addWidget(&_run_stop_button);
122 connect(&_sample_rate_list, SIGNAL(currentIndexChanged(int)),
123 this, SLOT(on_sample_rate_changed()));
124 connect(&_sample_rate_value, SIGNAL(editingFinished()),
125 this, SLOT(on_sample_rate_changed()));
128 void SamplingBar::set_device_list(
129 const std::list<struct sr_dev_inst*> &devices)
131 _updating_device_selector = true;
133 _device_selector.clear();
135 BOOST_FOREACH (sr_dev_inst *sdi, devices) {
136 const string title = DeviceManager::format_device_title(sdi);
137 _device_selector.addItem(title.c_str(),
138 qVariantFromValue((void*)sdi));
141 _updating_device_selector = false;
143 on_device_selected();
146 struct sr_dev_inst* SamplingBar::get_selected_device() const
148 const int index = _device_selector.currentIndex();
152 return (sr_dev_inst*)_device_selector.itemData(
153 index).value<void*>();
156 void SamplingBar::set_selected_device(struct sr_dev_inst *const sdi)
158 for (int i = 0; i < _device_selector.count(); i++)
159 if (sdi == _device_selector.itemData(i).value<void*>()) {
160 _device_selector.setCurrentIndex(i);
165 uint64_t SamplingBar::get_record_length() const
167 const int index = _record_length_selector.currentIndex();
171 return _record_length_selector.itemData(index).value<uint64_t>();
174 void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
176 const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
177 _run_stop_button.setIcon(*icons[state]);
178 _run_stop_button.setText((state == pv::SigSession::Stopped) ?
179 tr("Run") : tr("Stop"));
182 void SamplingBar::update_sample_rate_selector()
184 const sr_dev_inst *const sdi = get_selected_device();
185 GVariant *gvar_dict, *gvar_list;
186 const uint64_t *elements = NULL;
189 assert(_sample_rate_value_action);
190 assert(_sample_rate_list_action);
195 if (sr_config_list(sdi->driver, sdi, NULL,
196 SR_CONF_SAMPLERATE, &gvar_dict) != SR_OK)
199 _sample_rate_list_action->setVisible(false);
200 _sample_rate_value_action->setVisible(false);
202 if ((gvar_list = g_variant_lookup_value(gvar_dict,
203 "samplerate-steps", G_VARIANT_TYPE("at")))) {
204 elements = (const uint64_t *)g_variant_get_fixed_array(
205 gvar_list, &num_elements, sizeof(uint64_t));
206 _sample_rate_value.setRange(elements[0], elements[1]);
207 _sample_rate_value.setSingleStep(elements[2]);
208 g_variant_unref(gvar_list);
210 _sample_rate_action = _sample_rate_value_action;
212 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
213 "samplerates", G_VARIANT_TYPE("at"))))
215 elements = (const uint64_t *)g_variant_get_fixed_array(
216 gvar_list, &num_elements, sizeof(uint64_t));
217 _sample_rate_list.clear();
219 for (unsigned int i = 0; i < num_elements; i++)
221 char *const s = sr_samplerate_string(elements[i]);
222 _sample_rate_list.addItem(QString::fromUtf8(s),
223 qVariantFromValue(elements[i]));
227 _sample_rate_list.show();
228 g_variant_unref(gvar_list);
230 _sample_rate_action = _sample_rate_list_action;
233 g_variant_unref(gvar_dict);
234 update_sample_rate_selector_value();
236 // We delay showing the action, so that value change events
238 if (_sample_rate_action)
239 _sample_rate_action->setVisible(true);
242 void SamplingBar::update_sample_rate_selector_value()
244 sr_dev_inst *const sdi = get_selected_device();
250 if (sr_config_get(sdi->driver, sdi, NULL,
251 SR_CONF_SAMPLERATE, &gvar) != SR_OK) {
253 "WARNING: Failed to get value of sample rate";
256 samplerate = g_variant_get_uint64(gvar);
257 g_variant_unref(gvar);
259 assert(_sample_rate_value_action);
260 assert(_sample_rate_list_action);
262 if (_sample_rate_action == _sample_rate_value_action)
263 _sample_rate_value.setValue(samplerate);
264 else if (_sample_rate_action == _sample_rate_list_action)
266 for (int i = 0; i < _sample_rate_list.count(); i++)
267 if (samplerate == _sample_rate_list.itemData(
268 i).value<uint64_t>())
269 _sample_rate_list.setCurrentIndex(i);
273 void SamplingBar::commit_sample_rate()
275 uint64_t sample_rate = 0;
277 sr_dev_inst *const sdi = get_selected_device();
280 assert(_sample_rate_value_action);
281 assert(_sample_rate_list_action);
283 if (_sample_rate_action == _sample_rate_value_action)
284 sample_rate = (uint64_t)_sample_rate_value.value();
285 else if (_sample_rate_action == _sample_rate_list_action)
287 const int index = _sample_rate_list.currentIndex();
289 sample_rate = _sample_rate_list.itemData(
290 index).value<uint64_t>();
293 if (sample_rate == 0)
296 // Set the samplerate
297 if (sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE,
298 g_variant_new_uint64(sample_rate)) != SR_OK) {
299 qDebug() << "Failed to configure samplerate.";
304 void SamplingBar::on_device_selected()
306 using pv::popups::DeviceOptions;
308 if (_updating_device_selector)
311 update_sample_rate_selector();
313 sr_dev_inst *const sdi = get_selected_device();
314 _session.set_device(sdi);
316 _configure_button.set_popup(new DeviceOptions(sdi, this));
319 void SamplingBar::on_sample_rate_changed()
321 commit_sample_rate();
324 void SamplingBar::on_run_stop()
326 commit_sample_rate();
330 } // namespace toolbars