]> sigrok.org Git - pulseview.git/blame - pv/toolbars/samplingbar.cpp
Factored device title printing code out into pv::DeviceManager::format_device_title
[pulseview.git] / pv / toolbars / samplingbar.cpp
CommitLineData
d4984fe7 1/*
b3f22de0 2 * This file is part of the PulseView project.
d4984fe7
JH
3 *
4 * Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
5 *
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.
10 *
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.
15 *
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
19 */
20
09f5d123 21#include <extdef.h>
dde1a563 22
09f5d123 23#include <assert.h>
215f9499 24
18203d86
JH
25#include <boost/foreach.hpp>
26
6fb67b27 27#include <libsigrok/libsigrok.h>
6fb67b27 28
dde1a563 29#include <QAction>
48888313 30#include <QDebug>
dde1a563 31
d4984fe7
JH
32#include "samplingbar.h"
33
dd63af74 34#include <pv/devicemanager.h>
d755562a 35#include <pv/dialogs/deviceoptions.h>
cdb50f67 36
dd63af74
JH
37using namespace std;
38
51e77110 39namespace pv {
f4c92e1c 40namespace toolbars {
51e77110 41
09f5d123
JH
42const uint64_t SamplingBar::RecordLengths[20] = {
43 1000,
44 2500,
45 5000,
46 10000,
47 25000,
48 50000,
49 100000,
50 250000,
51 500000,
215f9499
JH
52 1000000,
53 2000000,
54 5000000,
55 10000000,
56 25000000,
57 50000000,
58 100000000,
59 250000000,
60 500000000,
61 1000000000,
d2aad781 62 10000000000ULL,
215f9499
JH
63};
64
09f5d123
JH
65const uint64_t SamplingBar::DefaultRecordLength = 1000000;
66
d4984fe7 67SamplingBar::SamplingBar(QWidget *parent) :
6fb67b27 68 QToolBar("Sampling Bar", parent),
dde1a563 69 _device_selector(this),
cdb50f67 70 _configure_button(this),
215f9499 71 _record_length_selector(this),
274d4f13 72 _sample_rate_list(this),
f5798068
JH
73 _icon_green(":/icons/status-green.svg"),
74 _icon_grey(":/icons/status-grey.svg"),
274d4f13 75 _run_stop_button(this)
6fb67b27 76{
cdb50f67 77 connect(&_run_stop_button, SIGNAL(clicked()),
9f3d12f3 78 this, SLOT(on_run_stop()));
dde1a563
JH
79 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
80 this, SLOT(on_device_selected()));
cdb50f67 81 connect(&_configure_button, SIGNAL(clicked()),
6374d2f8 82 this, SLOT(on_configure()));
dde1a563
JH
83
84 _sample_rate_value.setDecimals(0);
85 _sample_rate_value.setSuffix("Hz");
86
9ba4ca35 87 for (size_t i = 0; i < countof(RecordLengths); i++)
215f9499 88 {
09f5d123 89 const uint64_t &l = RecordLengths[i];
215f9499
JH
90 char *const text = sr_si_string_u64(l, " samples");
91 _record_length_selector.addItem(QString(text),
92 qVariantFromValue(l));
93 g_free(text);
09f5d123 94
9ba4ca35 95 if (l == DefaultRecordLength)
09f5d123 96 _record_length_selector.setCurrentIndex(i);
215f9499
JH
97 }
98
6ac96c2e 99 set_sampling(false);
274d4f13 100
688ef645
JH
101 _configure_button.setIcon(QIcon::fromTheme("configure",
102 QIcon(":/icons/configure.png")));
cdb50f67 103
f5798068
JH
104 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
105
6fb67b27 106 addWidget(&_device_selector);
cdb50f67 107 addWidget(&_configure_button);
215f9499 108 addWidget(&_record_length_selector);
dde1a563
JH
109 _sample_rate_list_action = addWidget(&_sample_rate_list);
110 _sample_rate_value_action = addWidget(&_sample_rate_value);
274d4f13 111 addWidget(&_run_stop_button);
6fb67b27 112
48888313
JH
113 connect(&_sample_rate_list, SIGNAL(currentIndexChanged(int)),
114 this, SLOT(on_sample_rate_changed()));
18203d86 115 connect(&_sample_rate_value, SIGNAL(editingFinished()),
48888313 116 this, SLOT(on_sample_rate_changed()));
6fb67b27
JH
117}
118
18203d86
JH
119void SamplingBar::set_device_list(
120 const std::list<struct sr_dev_inst*> &devices)
121{
122 _device_selector.clear();
123
124 BOOST_FOREACH (sr_dev_inst *sdi, devices) {
dd63af74
JH
125 const string title = DeviceManager::format_device_title(sdi);
126 _device_selector.addItem(title.c_str(),
127 qVariantFromValue((void*)sdi));
18203d86
JH
128 }
129
130 update_sample_rate_selector();
131}
132
6fb67b27 133struct sr_dev_inst* SamplingBar::get_selected_device() const
d4984fe7 134{
6fb67b27 135 const int index = _device_selector.currentIndex();
333d5bbc 136 if (index < 0)
6fb67b27
JH
137 return NULL;
138
139 return (sr_dev_inst*)_device_selector.itemData(
140 index).value<void*>();
141}
142
dba73e73
JH
143void SamplingBar::set_selected_device(struct sr_dev_inst *const sdi)
144{
793f8096
UH
145 for (int i = 0; i < _device_selector.count(); i++)
146 if (sdi == _device_selector.itemData(i).value<void*>()) {
dba73e73
JH
147 _device_selector.setCurrentIndex(i);
148 return;
149 }
150}
151
215f9499
JH
152uint64_t SamplingBar::get_record_length() const
153{
154 const int index = _record_length_selector.currentIndex();
333d5bbc 155 if (index < 0)
215f9499
JH
156 return 0;
157
158 return _record_length_selector.itemData(index).value<uint64_t>();
159}
160
6ac96c2e
JH
161void SamplingBar::set_sampling(bool sampling)
162{
f5798068 163 _run_stop_button.setIcon(sampling ? _icon_green : _icon_grey);
6ac96c2e
JH
164 _run_stop_button.setText(sampling ? "Stop" : "Run");
165}
166
dde1a563
JH
167void SamplingBar::update_sample_rate_selector()
168{
169 const sr_dev_inst *const sdi = get_selected_device();
488f5d3f
BV
170 GVariant *gvar_dict, *gvar_list;
171 const uint64_t *elements = NULL;
172 gsize num_elements;
f9541bde 173 QAction *selector_action = NULL;
dde1a563
JH
174
175 assert(_sample_rate_value_action);
176 assert(_sample_rate_list_action);
177
ef4d0201
JH
178 if (!sdi)
179 return;
180
c0be28da 181 if (sr_config_list(sdi->driver, SR_CONF_SAMPLERATE,
488f5d3f 182 &gvar_dict, sdi) != SR_OK)
dde1a563
JH
183 return;
184
185 _sample_rate_list_action->setVisible(false);
186 _sample_rate_value_action->setVisible(false);
187
488f5d3f
BV
188 if ((gvar_list = g_variant_lookup_value(gvar_dict,
189 "samplerate-steps", G_VARIANT_TYPE("at")))) {
190 elements = (const uint64_t *)g_variant_get_fixed_array(
191 gvar_list, &num_elements, sizeof(uint64_t));
192 _sample_rate_value.setRange(elements[0], elements[1]);
193 _sample_rate_value.setSingleStep(elements[2]);
488f5d3f 194 g_variant_unref(gvar_list);
f9541bde
JH
195
196 selector_action = _sample_rate_value_action;
dde1a563 197 }
488f5d3f
BV
198 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
199 "samplerates", G_VARIANT_TYPE("at"))))
dde1a563 200 {
488f5d3f
BV
201 elements = (const uint64_t *)g_variant_get_fixed_array(
202 gvar_list, &num_elements, sizeof(uint64_t));
dde1a563 203 _sample_rate_list.clear();
488f5d3f
BV
204
205 for (unsigned int i = 0; i < num_elements; i++)
206 {
207 char *const s = sr_samplerate_string(elements[i]);
208 _sample_rate_list.addItem(QString(s),
209 qVariantFromValue(elements[i]));
210 g_free(s);
211 }
212
dde1a563 213 _sample_rate_list.show();
488f5d3f 214 g_variant_unref(gvar_list);
f9541bde
JH
215
216 selector_action = _sample_rate_list_action;
dde1a563 217 }
48888313 218
488f5d3f 219 g_variant_unref(gvar_dict);
48888313 220 update_sample_rate_selector_value();
f9541bde
JH
221
222 // We delay showing the action, so that value change events
223 // are ignored.
224 if (selector_action)
225 selector_action->setVisible(true);
48888313
JH
226}
227
228void SamplingBar::update_sample_rate_selector_value()
229{
230 sr_dev_inst *const sdi = get_selected_device();
488f5d3f
BV
231 GVariant *gvar;
232 uint64_t samplerate;
233
48888313
JH
234 assert(sdi);
235
793f8096 236 if (sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
488f5d3f 237 &gvar, sdi) != SR_OK) {
eb4008a6
JH
238 qDebug() <<
239 "WARNING: Failed to get value of sample rate";
240 return;
241 }
488f5d3f
BV
242 samplerate = g_variant_get_uint64(gvar);
243 g_variant_unref(gvar);
48888313
JH
244
245 assert(_sample_rate_value_action);
246 assert(_sample_rate_list_action);
247
248 if (_sample_rate_value_action->isVisible())
488f5d3f 249 _sample_rate_value.setValue(samplerate);
48888313
JH
250 else if (_sample_rate_list_action->isVisible())
251 {
793f8096 252 for (int i = 0; i < _sample_rate_list.count(); i++)
488f5d3f 253 if (samplerate == _sample_rate_list.itemData(
48888313
JH
254 i).value<uint64_t>())
255 _sample_rate_list.setCurrentIndex(i);
256 }
257}
258
259void SamplingBar::commit_sample_rate()
260{
261 uint64_t sample_rate = 0;
262
263 sr_dev_inst *const sdi = get_selected_device();
264 assert(sdi);
265
266 assert(_sample_rate_value_action);
267 assert(_sample_rate_list_action);
268
269 if (_sample_rate_value_action->isVisible())
270 sample_rate = (uint64_t)_sample_rate_value.value();
271 else if (_sample_rate_list_action->isVisible())
272 {
273 const int index = _sample_rate_list.currentIndex();
274 if (index >= 0)
275 sample_rate = _sample_rate_list.itemData(
276 index).value<uint64_t>();
277 }
278
f9541bde
JH
279 if (sample_rate == 0)
280 return;
281
48888313
JH
282 // Set the samplerate
283 if (sr_config_set(sdi, SR_CONF_SAMPLERATE,
488f5d3f 284 g_variant_new_uint64(sample_rate)) != SR_OK) {
48888313
JH
285 qDebug() << "Failed to configure samplerate.";
286 return;
287 }
dde1a563
JH
288}
289
290void SamplingBar::on_device_selected()
291{
292 update_sample_rate_selector();
5ac961e3 293 device_selected();
dde1a563 294}
51e77110 295
48888313
JH
296void SamplingBar::on_sample_rate_changed()
297{
298 commit_sample_rate();
299}
300
6374d2f8 301void SamplingBar::on_configure()
cdb50f67 302{
48888313
JH
303 commit_sample_rate();
304
cdb50f67
JH
305 sr_dev_inst *const sdi = get_selected_device();
306 assert(sdi);
307
d755562a 308 pv::dialogs::DeviceOptions dlg(this, sdi);
cdb50f67 309 dlg.exec();
48888313
JH
310
311 update_sample_rate_selector_value();
cdb50f67
JH
312}
313
9f3d12f3
JH
314void SamplingBar::on_run_stop()
315{
316 commit_sample_rate();
317 run_stop();
318}
319
f4c92e1c 320} // namespace toolbars
51e77110 321} // namespace pv