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(QWidget *parent) :
68 QToolBar("Sampling Bar", parent),
69 _device_selector(this),
70 _configure_button(this),
72 _record_length_selector(this),
73 _sample_rate_list(this),
74 _icon_red(":/icons/status-red.svg"),
75 _icon_green(":/icons/status-green.svg"),
76 _icon_grey(":/icons/status-grey.svg"),
77 _run_stop_button(this)
79 connect(&_run_stop_button, SIGNAL(clicked()),
80 this, SLOT(on_run_stop()));
81 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
82 this, SLOT(on_device_selected()));
84 _sample_rate_value.setDecimals(0);
85 _sample_rate_value.setSuffix("Hz");
87 for (size_t i = 0; i < countof(RecordLengths); i++)
89 const uint64_t &l = RecordLengths[i];
90 char *const text = sr_si_string_u64(l, " samples");
91 _record_length_selector.addItem(QString(text),
92 qVariantFromValue(l));
95 if (l == DefaultRecordLength)
96 _record_length_selector.setCurrentIndex(i);
99 set_capture_state(pv::SigSession::Stopped);
101 _configure_button.setIcon(QIcon::fromTheme("configure",
102 QIcon(":/icons/configure.png")));
103 _probes_button.setIcon(QIcon::fromTheme("probes",
104 QIcon(":/icons/probes.svg")));
106 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
108 addWidget(&_device_selector);
109 addWidget(&_configure_button);
110 addWidget(&_probes_button);
111 addWidget(&_record_length_selector);
112 _sample_rate_list_action = addWidget(&_sample_rate_list);
113 _sample_rate_value_action = addWidget(&_sample_rate_value);
114 addWidget(&_run_stop_button);
116 connect(&_sample_rate_list, SIGNAL(currentIndexChanged(int)),
117 this, SLOT(on_sample_rate_changed()));
118 connect(&_sample_rate_value, SIGNAL(editingFinished()),
119 this, SLOT(on_sample_rate_changed()));
122 void SamplingBar::set_device_list(
123 const std::list<struct sr_dev_inst*> &devices)
125 _device_selector.clear();
127 BOOST_FOREACH (sr_dev_inst *sdi, devices) {
128 const string title = DeviceManager::format_device_title(sdi);
129 _device_selector.addItem(title.c_str(),
130 qVariantFromValue((void*)sdi));
133 update_sample_rate_selector();
136 struct sr_dev_inst* SamplingBar::get_selected_device() const
138 const int index = _device_selector.currentIndex();
142 return (sr_dev_inst*)_device_selector.itemData(
143 index).value<void*>();
146 void SamplingBar::set_selected_device(struct sr_dev_inst *const sdi)
148 for (int i = 0; i < _device_selector.count(); i++)
149 if (sdi == _device_selector.itemData(i).value<void*>()) {
150 _device_selector.setCurrentIndex(i);
155 uint64_t SamplingBar::get_record_length() const
157 const int index = _record_length_selector.currentIndex();
161 return _record_length_selector.itemData(index).value<uint64_t>();
164 void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
166 const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
167 _run_stop_button.setIcon(*icons[state]);
168 _run_stop_button.setText((state == pv::SigSession::Stopped) ?
169 tr("Run") : tr("Stop"));
172 void SamplingBar::update_sample_rate_selector()
174 const sr_dev_inst *const sdi = get_selected_device();
175 GVariant *gvar_dict, *gvar_list;
176 const uint64_t *elements = NULL;
178 QAction *selector_action = NULL;
180 assert(_sample_rate_value_action);
181 assert(_sample_rate_list_action);
186 if (sr_config_list(sdi->driver, SR_CONF_SAMPLERATE,
187 &gvar_dict, sdi) != SR_OK)
190 _sample_rate_list_action->setVisible(false);
191 _sample_rate_value_action->setVisible(false);
193 if ((gvar_list = g_variant_lookup_value(gvar_dict,
194 "samplerate-steps", G_VARIANT_TYPE("at")))) {
195 elements = (const uint64_t *)g_variant_get_fixed_array(
196 gvar_list, &num_elements, sizeof(uint64_t));
197 _sample_rate_value.setRange(elements[0], elements[1]);
198 _sample_rate_value.setSingleStep(elements[2]);
199 g_variant_unref(gvar_list);
201 selector_action = _sample_rate_value_action;
203 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
204 "samplerates", G_VARIANT_TYPE("at"))))
206 elements = (const uint64_t *)g_variant_get_fixed_array(
207 gvar_list, &num_elements, sizeof(uint64_t));
208 _sample_rate_list.clear();
210 for (unsigned int i = 0; i < num_elements; i++)
212 char *const s = sr_samplerate_string(elements[i]);
213 _sample_rate_list.addItem(QString(s),
214 qVariantFromValue(elements[i]));
218 _sample_rate_list.show();
219 g_variant_unref(gvar_list);
221 selector_action = _sample_rate_list_action;
224 g_variant_unref(gvar_dict);
225 update_sample_rate_selector_value();
227 // We delay showing the action, so that value change events
230 selector_action->setVisible(true);
233 void SamplingBar::update_sample_rate_selector_value()
235 sr_dev_inst *const sdi = get_selected_device();
241 if (sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
242 &gvar, sdi) != SR_OK) {
244 "WARNING: Failed to get value of sample rate";
247 samplerate = g_variant_get_uint64(gvar);
248 g_variant_unref(gvar);
250 assert(_sample_rate_value_action);
251 assert(_sample_rate_list_action);
253 if (_sample_rate_value_action->isVisible())
254 _sample_rate_value.setValue(samplerate);
255 else if (_sample_rate_list_action->isVisible())
257 for (int i = 0; i < _sample_rate_list.count(); i++)
258 if (samplerate == _sample_rate_list.itemData(
259 i).value<uint64_t>())
260 _sample_rate_list.setCurrentIndex(i);
264 void SamplingBar::commit_sample_rate()
266 uint64_t sample_rate = 0;
268 sr_dev_inst *const sdi = get_selected_device();
271 assert(_sample_rate_value_action);
272 assert(_sample_rate_list_action);
274 if (_sample_rate_value_action->isVisible())
275 sample_rate = (uint64_t)_sample_rate_value.value();
276 else if (_sample_rate_list_action->isVisible())
278 const int index = _sample_rate_list.currentIndex();
280 sample_rate = _sample_rate_list.itemData(
281 index).value<uint64_t>();
284 if (sample_rate == 0)
287 // Set the samplerate
288 if (sr_config_set(sdi, SR_CONF_SAMPLERATE,
289 g_variant_new_uint64(sample_rate)) != SR_OK) {
290 qDebug() << "Failed to configure samplerate.";
295 void SamplingBar::on_device_selected()
297 using namespace pv::popups;
299 update_sample_rate_selector();
301 sr_dev_inst *const sdi = get_selected_device();
303 _configure_button.set_popup(new DeviceOptions(sdi, this));
304 _probes_button.set_popup(new Probes(sdi, this));
309 void SamplingBar::on_sample_rate_changed()
311 commit_sample_rate();
314 void SamplingBar::on_run_stop()
316 commit_sample_rate();
320 } // namespace toolbars