]> sigrok.org Git - pulseview.git/blame - pv/toolbars/samplingbar.cpp
Reworked SamplingBar device selection logic
[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
dde1a563 27#include <QAction>
48888313 28#include <QDebug>
dde1a563 29
d4984fe7
JH
30#include "samplingbar.h"
31
dd63af74 32#include <pv/devicemanager.h>
94574501 33#include <pv/device/devinst.h>
51d4a9ab 34#include <pv/popups/deviceoptions.h>
488f068c 35#include <pv/popups/probes.h>
cdb50f67 36
19adbc2c
JH
37using boost::shared_ptr;
38using std::map;
b50ef520
JH
39using std::max;
40using std::min;
819f4c25 41using std::string;
dd63af74 42
51e77110 43namespace pv {
f4c92e1c 44namespace toolbars {
51e77110 45
b50ef520
JH
46const uint64_t SamplingBar::MinSampleCount = 100ULL;
47const uint64_t SamplingBar::MaxSampleCount = 1000000000000ULL;
48const uint64_t SamplingBar::DefaultSampleCount = 1000000;
09f5d123 49
aca00b1e 50SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
6fb67b27 51 QToolBar("Sampling Bar", parent),
aca00b1e 52 _session(session),
dde1a563 53 _device_selector(this),
95237c18 54 _updating_device_selector(false),
cdb50f67 55 _configure_button(this),
b3e8a5d8 56 _configure_button_action(NULL),
51d4a9ab 57 _probes_button(this),
124d97de 58 _sample_count(" samples", this),
1198b887
JH
59 _sample_rate("Hz", this),
60 _updating_sample_rate(false),
85756012 61 _updating_sample_count(false),
6546a6a7 62 _sample_count_supported(false),
2b49eeb0 63 _icon_red(":/icons/status-red.svg"),
f5798068
JH
64 _icon_green(":/icons/status-green.svg"),
65 _icon_grey(":/icons/status-grey.svg"),
274d4f13 66 _run_stop_button(this)
6fb67b27 67{
cdb50f67 68 connect(&_run_stop_button, SIGNAL(clicked()),
9f3d12f3 69 this, SLOT(on_run_stop()));
dde1a563
JH
70 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
71 this, SLOT(on_device_selected()));
124d97de
ML
72 connect(&_sample_count, SIGNAL(value_changed()),
73 this, SLOT(on_sample_count_changed()));
1198b887
JH
74 connect(&_sample_rate, SIGNAL(value_changed()),
75 this, SLOT(on_sample_rate_changed()));
dde1a563 76
124d97de 77 _sample_count.show_min_max_step(0, UINT64_MAX, 1);
215f9499 78
2b49eeb0 79 set_capture_state(pv::SigSession::Stopped);
274d4f13 80
688ef645
JH
81 _configure_button.setIcon(QIcon::fromTheme("configure",
82 QIcon(":/icons/configure.png")));
b7b659aa 83
51d4a9ab
JH
84 _probes_button.setIcon(QIcon::fromTheme("probes",
85 QIcon(":/icons/probes.svg")));
cdb50f67 86
f5798068
JH
87 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
88
6fb67b27 89 addWidget(&_device_selector);
b3e8a5d8 90 _configure_button_action = addWidget(&_configure_button);
51d4a9ab 91 addWidget(&_probes_button);
124d97de 92 addWidget(&_sample_count);
1198b887 93 addWidget(&_sample_rate);
6fb67b27 94
1198b887 95 addWidget(&_run_stop_button);
6fb67b27
JH
96}
97
18203d86 98void SamplingBar::set_device_list(
e95e8563
JH
99 const std::list< shared_ptr<pv::device::DevInst> > &devices,
100 shared_ptr<pv::device::DevInst> selected)
18203d86 101{
e95e8563
JH
102 int selected_index = -1;
103
104 assert(selected);
105
95237c18
JH
106 _updating_device_selector = true;
107
18203d86 108 _device_selector.clear();
19adbc2c 109 _device_selector_map.clear();
18203d86 110
94574501 111 BOOST_FOREACH (shared_ptr<pv::device::DevInst> dev_inst, devices) {
19adbc2c
JH
112 assert(dev_inst);
113 const string title = dev_inst->format_device_title();
114 const sr_dev_inst *sdi = dev_inst->dev_inst();
115 assert(sdi);
116
e95e8563
JH
117 if (selected == dev_inst)
118 selected_index = _device_selector.count();
119
19adbc2c 120 _device_selector_map[sdi] = dev_inst;
dd63af74
JH
121 _device_selector.addItem(title.c_str(),
122 qVariantFromValue((void*)sdi));
18203d86
JH
123 }
124
e95e8563
JH
125 // The selected device should have been in the list
126 assert(selected_index != -1);
127 _device_selector.setCurrentIndex(selected_index);
128
129 update_device_config_widgets();
130
95237c18 131 _updating_device_selector = false;
18203d86
JH
132}
133
94574501 134shared_ptr<pv::device::DevInst> SamplingBar::get_selected_device() const
d4984fe7 135{
6fb67b27 136 const int index = _device_selector.currentIndex();
333d5bbc 137 if (index < 0)
94574501 138 return shared_ptr<pv::device::DevInst>();
19adbc2c
JH
139
140 const sr_dev_inst *const sdi =
141 (const sr_dev_inst*)_device_selector.itemData(
142 index).value<void*>();
143 assert(sdi);
6fb67b27 144
94574501 145 map<const sr_dev_inst*, boost::weak_ptr<device::DevInst> >::
19adbc2c
JH
146 const_iterator iter = _device_selector_map.find(sdi);
147 if (iter == _device_selector_map.end())
94574501 148 return shared_ptr<pv::device::DevInst>();
19adbc2c 149
94574501 150 return shared_ptr<pv::device::DevInst>((*iter).second);
6fb67b27
JH
151}
152
2b49eeb0 153void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
6ac96c2e 154{
2b49eeb0
JH
155 const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
156 _run_stop_button.setIcon(*icons[state]);
157 _run_stop_button.setText((state == pv::SigSession::Stopped) ?
158 tr("Run") : tr("Stop"));
6ac96c2e
JH
159}
160
dde1a563
JH
161void SamplingBar::update_sample_rate_selector()
162{
488f5d3f
BV
163 GVariant *gvar_dict, *gvar_list;
164 const uint64_t *elements = NULL;
165 gsize num_elements;
dde1a563 166
3d4f16af
JH
167 if (_updating_sample_rate)
168 return;
169
94574501 170 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
19adbc2c 171 if (!dev_inst)
ef4d0201
JH
172 return;
173
3d4f16af 174 assert(!_updating_sample_rate);
1198b887
JH
175 _updating_sample_rate = true;
176
8dd44190 177 if (!(gvar_dict = dev_inst->list_config(NULL, SR_CONF_SAMPLERATE)))
1198b887
JH
178 {
179 _sample_rate.show_none();
180 _updating_sample_rate = false;
dde1a563 181 return;
1198b887 182 }
dde1a563 183
488f5d3f 184 if ((gvar_list = g_variant_lookup_value(gvar_dict,
1198b887
JH
185 "samplerate-steps", G_VARIANT_TYPE("at"))))
186 {
488f5d3f
BV
187 elements = (const uint64_t *)g_variant_get_fixed_array(
188 gvar_list, &num_elements, sizeof(uint64_t));
aafe53af
JH
189
190 const uint64_t min = elements[0];
191 const uint64_t max = elements[1];
192 const uint64_t step = elements[2];
193
488f5d3f 194 g_variant_unref(gvar_list);
aafe53af
JH
195
196 assert(min > 0);
197 assert(max > 0);
198 assert(max > min);
199 assert(step > 0);
200
201 if (step == 1)
202 _sample_rate.show_125_list(min, max);
203 else
204 {
205 // When the step is not 1, we cam't make a 1-2-5-10
206 // list of sample rates, because we may not be able to
207 // make round numbers. Therefore in this case, show a
208 // spin box.
209 _sample_rate.show_min_max_step(min, max, step);
210 }
dde1a563 211 }
488f5d3f
BV
212 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
213 "samplerates", G_VARIANT_TYPE("at"))))
dde1a563 214 {
488f5d3f
BV
215 elements = (const uint64_t *)g_variant_get_fixed_array(
216 gvar_list, &num_elements, sizeof(uint64_t));
1198b887 217 _sample_rate.show_list(elements, num_elements);
488f5d3f 218 g_variant_unref(gvar_list);
dde1a563 219 }
1198b887 220 _updating_sample_rate = false;
48888313 221
488f5d3f 222 g_variant_unref(gvar_dict);
48888313
JH
223 update_sample_rate_selector_value();
224}
225
226void SamplingBar::update_sample_rate_selector_value()
227{
488f5d3f
BV
228 GVariant *gvar;
229 uint64_t samplerate;
230
3d4f16af
JH
231 if (_updating_sample_rate)
232 return;
233
94574501 234 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
19adbc2c
JH
235 if (!dev_inst)
236 return;
237
8dd44190
JH
238 if (!(gvar = dev_inst->get_config(NULL, SR_CONF_SAMPLERATE))) {
239 qDebug() << "WARNING: Failed to get value of sample rate";
eb4008a6
JH
240 return;
241 }
488f5d3f
BV
242 samplerate = g_variant_get_uint64(gvar);
243 g_variant_unref(gvar);
48888313 244
3d4f16af 245 assert(!_updating_sample_rate);
1198b887
JH
246 _updating_sample_rate = true;
247 _sample_rate.set_value(samplerate);
248 _updating_sample_rate = false;
48888313
JH
249}
250
85756012
ML
251void SamplingBar::update_sample_count_selector()
252{
85756012 253 GVariant *gvar;
85756012 254
3d4f16af
JH
255 if (_updating_sample_count)
256 return;
257
94574501 258 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
19adbc2c
JH
259 if (!dev_inst)
260 return;
261
3d4f16af 262 assert(!_updating_sample_count);
b50ef520 263 _updating_sample_count = true;
85756012 264
b50ef520 265 if (_sample_count_supported)
df3c1aa3 266 {
3cbffdcf 267 uint64_t sample_count = _sample_count.value();
1d04852f 268 uint64_t min_sample_count = 0;
b50ef520
JH
269 uint64_t max_sample_count = MaxSampleCount;
270
3cbffdcf
JH
271 if (sample_count == 0)
272 sample_count = DefaultSampleCount;
273
8dd44190
JH
274 if ((gvar = dev_inst->list_config(NULL, SR_CONF_LIMIT_SAMPLES)))
275 {
1d04852f
JH
276 g_variant_get(gvar, "(tt)",
277 &min_sample_count, &max_sample_count);
b50ef520
JH
278 g_variant_unref(gvar);
279 }
280
1d04852f
JH
281 min_sample_count = min(max(min_sample_count, MinSampleCount),
282 max_sample_count);
283
284 _sample_count.show_125_list(
285 min_sample_count, max_sample_count);
b50ef520 286
8dd44190 287 if ((gvar = dev_inst->get_config(NULL, SR_CONF_LIMIT_SAMPLES)))
b50ef520
JH
288 {
289 sample_count = g_variant_get_uint64(gvar);
290 if (sample_count == 0)
291 sample_count = DefaultSampleCount;
292 sample_count = min(max(sample_count, MinSampleCount),
293 max_sample_count);
294
295 g_variant_unref(gvar);
296 }
297
298 _sample_count.set_value(sample_count);
85756012 299 }
b50ef520
JH
300 else
301 _sample_count.show_none();
df3c1aa3 302
df3c1aa3 303 _updating_sample_count = false;
85756012
ML
304}
305
e95e8563
JH
306void SamplingBar::update_device_config_widgets()
307{
308 GVariant *gvar;
309
310 using namespace pv::popups;
311
312 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
313 if (!dev_inst)
314 return;
315
316 // Update the configure popup
317 DeviceOptions *const opts = new DeviceOptions(dev_inst, this);
318 _configure_button_action->setVisible(
319 !opts->binding().properties().empty());
320 _configure_button.set_popup(opts);
321
322 // Update the probes popup
323 Probes *const probes = new Probes(_session, this);
324 _probes_button.set_popup(probes);
325
326 // Update supported options.
327 _sample_count_supported = false;
328
329 if ((gvar = dev_inst->list_config(NULL, SR_CONF_DEVICE_OPTIONS)))
330 {
331 gsize num_opts;
332 const int *const options =
333 (const int32_t *)g_variant_get_fixed_array(
334 gvar, &num_opts, sizeof(int32_t));
335 for (unsigned int i = 0; i < num_opts; i++)
336 {
337 switch (options[i]) {
338 case SR_CONF_LIMIT_SAMPLES:
339 _sample_count_supported = true;
340 break;
341 case SR_CONF_LIMIT_FRAMES:
342 dev_inst->set_config(NULL, SR_CONF_LIMIT_FRAMES,
343 g_variant_new_uint64(1));
344 break;
345 }
346 }
347 }
348
349 // Add notification of reconfigure events
350 disconnect(this, SLOT(on_config_changed()));
351 connect(dev_inst.get(), SIGNAL(config_changed()),
352 this, SLOT(on_config_changed()));
353
354 // Update sweep timing widgets.
355 update_sample_count_selector();
356 update_sample_rate_selector();
357}
358
124d97de
ML
359void SamplingBar::commit_sample_count()
360{
361 uint64_t sample_count = 0;
362
3d4f16af
JH
363 if (_updating_sample_count)
364 return;
365
94574501 366 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
19adbc2c
JH
367 if (!dev_inst)
368 return;
369
124d97de
ML
370 sample_count = _sample_count.value();
371
372 // Set the sample count
3d4f16af
JH
373 assert(!_updating_sample_count);
374 _updating_sample_count = true;
3668e2fe
JH
375 if (_sample_count_supported &&
376 !dev_inst->set_config(NULL, SR_CONF_LIMIT_SAMPLES,
8dd44190 377 g_variant_new_uint64(sample_count))) {
124d97de
ML
378 qDebug() << "Failed to configure sample count.";
379 return;
380 }
3d4f16af 381 _updating_sample_count = false;
124d97de
ML
382}
383
48888313
JH
384void SamplingBar::commit_sample_rate()
385{
386 uint64_t sample_rate = 0;
387
3d4f16af
JH
388 if (_updating_sample_rate)
389 return;
390
94574501 391 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
19adbc2c
JH
392 if (!dev_inst)
393 return;
394
1198b887 395 sample_rate = _sample_rate.value();
f9541bde
JH
396 if (sample_rate == 0)
397 return;
398
48888313 399 // Set the samplerate
3d4f16af
JH
400 assert(!_updating_sample_rate);
401 _updating_sample_rate = true;
8dd44190
JH
402 if (!dev_inst->set_config(NULL, SR_CONF_SAMPLERATE,
403 g_variant_new_uint64(sample_rate))) {
48888313
JH
404 qDebug() << "Failed to configure samplerate.";
405 return;
406 }
3d4f16af 407 _updating_sample_rate = false;
dde1a563
JH
408}
409
410void SamplingBar::on_device_selected()
411{
95237c18
JH
412 if (_updating_device_selector)
413 return;
414
94574501 415 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
19adbc2c
JH
416 if (!dev_inst)
417 return;
418
19adbc2c 419 _session.set_device(dev_inst);
51d4a9ab 420
e95e8563 421 update_device_config_widgets();
dde1a563 422}
51e77110 423
124d97de
ML
424void SamplingBar::on_sample_count_changed()
425{
3d4f16af 426 commit_sample_count();
124d97de
ML
427}
428
48888313
JH
429void SamplingBar::on_sample_rate_changed()
430{
3d4f16af 431 commit_sample_rate();
48888313
JH
432}
433
9f3d12f3
JH
434void SamplingBar::on_run_stop()
435{
b50ef520 436 commit_sample_count();
9f3d12f3
JH
437 commit_sample_rate();
438 run_stop();
439}
440
82afd5e3
JH
441void SamplingBar::on_config_changed()
442{
443 commit_sample_count();
444 update_sample_count_selector();
445 commit_sample_rate();
446 update_sample_rate_selector();
447}
448
f4c92e1c 449} // namespace toolbars
51e77110 450} // namespace pv