]> sigrok.org Git - pulseview.git/blame_incremental - pv/toolbars/samplingbar.cpp
Reworked SamplingBar device selection logic
[pulseview.git] / pv / toolbars / samplingbar.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
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
21#include <extdef.h>
22
23#include <assert.h>
24
25#include <boost/foreach.hpp>
26
27#include <QAction>
28#include <QDebug>
29
30#include "samplingbar.h"
31
32#include <pv/devicemanager.h>
33#include <pv/device/devinst.h>
34#include <pv/popups/deviceoptions.h>
35#include <pv/popups/probes.h>
36
37using boost::shared_ptr;
38using std::map;
39using std::max;
40using std::min;
41using std::string;
42
43namespace pv {
44namespace toolbars {
45
46const uint64_t SamplingBar::MinSampleCount = 100ULL;
47const uint64_t SamplingBar::MaxSampleCount = 1000000000000ULL;
48const uint64_t SamplingBar::DefaultSampleCount = 1000000;
49
50SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
51 QToolBar("Sampling Bar", parent),
52 _session(session),
53 _device_selector(this),
54 _updating_device_selector(false),
55 _configure_button(this),
56 _configure_button_action(NULL),
57 _probes_button(this),
58 _sample_count(" samples", this),
59 _sample_rate("Hz", this),
60 _updating_sample_rate(false),
61 _updating_sample_count(false),
62 _sample_count_supported(false),
63 _icon_red(":/icons/status-red.svg"),
64 _icon_green(":/icons/status-green.svg"),
65 _icon_grey(":/icons/status-grey.svg"),
66 _run_stop_button(this)
67{
68 connect(&_run_stop_button, SIGNAL(clicked()),
69 this, SLOT(on_run_stop()));
70 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
71 this, SLOT(on_device_selected()));
72 connect(&_sample_count, SIGNAL(value_changed()),
73 this, SLOT(on_sample_count_changed()));
74 connect(&_sample_rate, SIGNAL(value_changed()),
75 this, SLOT(on_sample_rate_changed()));
76
77 _sample_count.show_min_max_step(0, UINT64_MAX, 1);
78
79 set_capture_state(pv::SigSession::Stopped);
80
81 _configure_button.setIcon(QIcon::fromTheme("configure",
82 QIcon(":/icons/configure.png")));
83
84 _probes_button.setIcon(QIcon::fromTheme("probes",
85 QIcon(":/icons/probes.svg")));
86
87 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
88
89 addWidget(&_device_selector);
90 _configure_button_action = addWidget(&_configure_button);
91 addWidget(&_probes_button);
92 addWidget(&_sample_count);
93 addWidget(&_sample_rate);
94
95 addWidget(&_run_stop_button);
96}
97
98void SamplingBar::set_device_list(
99 const std::list< shared_ptr<pv::device::DevInst> > &devices,
100 shared_ptr<pv::device::DevInst> selected)
101{
102 int selected_index = -1;
103
104 assert(selected);
105
106 _updating_device_selector = true;
107
108 _device_selector.clear();
109 _device_selector_map.clear();
110
111 BOOST_FOREACH (shared_ptr<pv::device::DevInst> dev_inst, devices) {
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
117 if (selected == dev_inst)
118 selected_index = _device_selector.count();
119
120 _device_selector_map[sdi] = dev_inst;
121 _device_selector.addItem(title.c_str(),
122 qVariantFromValue((void*)sdi));
123 }
124
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
131 _updating_device_selector = false;
132}
133
134shared_ptr<pv::device::DevInst> SamplingBar::get_selected_device() const
135{
136 const int index = _device_selector.currentIndex();
137 if (index < 0)
138 return shared_ptr<pv::device::DevInst>();
139
140 const sr_dev_inst *const sdi =
141 (const sr_dev_inst*)_device_selector.itemData(
142 index).value<void*>();
143 assert(sdi);
144
145 map<const sr_dev_inst*, boost::weak_ptr<device::DevInst> >::
146 const_iterator iter = _device_selector_map.find(sdi);
147 if (iter == _device_selector_map.end())
148 return shared_ptr<pv::device::DevInst>();
149
150 return shared_ptr<pv::device::DevInst>((*iter).second);
151}
152
153void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
154{
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"));
159}
160
161void SamplingBar::update_sample_rate_selector()
162{
163 GVariant *gvar_dict, *gvar_list;
164 const uint64_t *elements = NULL;
165 gsize num_elements;
166
167 if (_updating_sample_rate)
168 return;
169
170 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
171 if (!dev_inst)
172 return;
173
174 assert(!_updating_sample_rate);
175 _updating_sample_rate = true;
176
177 if (!(gvar_dict = dev_inst->list_config(NULL, SR_CONF_SAMPLERATE)))
178 {
179 _sample_rate.show_none();
180 _updating_sample_rate = false;
181 return;
182 }
183
184 if ((gvar_list = g_variant_lookup_value(gvar_dict,
185 "samplerate-steps", G_VARIANT_TYPE("at"))))
186 {
187 elements = (const uint64_t *)g_variant_get_fixed_array(
188 gvar_list, &num_elements, sizeof(uint64_t));
189
190 const uint64_t min = elements[0];
191 const uint64_t max = elements[1];
192 const uint64_t step = elements[2];
193
194 g_variant_unref(gvar_list);
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 }
211 }
212 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
213 "samplerates", G_VARIANT_TYPE("at"))))
214 {
215 elements = (const uint64_t *)g_variant_get_fixed_array(
216 gvar_list, &num_elements, sizeof(uint64_t));
217 _sample_rate.show_list(elements, num_elements);
218 g_variant_unref(gvar_list);
219 }
220 _updating_sample_rate = false;
221
222 g_variant_unref(gvar_dict);
223 update_sample_rate_selector_value();
224}
225
226void SamplingBar::update_sample_rate_selector_value()
227{
228 GVariant *gvar;
229 uint64_t samplerate;
230
231 if (_updating_sample_rate)
232 return;
233
234 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
235 if (!dev_inst)
236 return;
237
238 if (!(gvar = dev_inst->get_config(NULL, SR_CONF_SAMPLERATE))) {
239 qDebug() << "WARNING: Failed to get value of sample rate";
240 return;
241 }
242 samplerate = g_variant_get_uint64(gvar);
243 g_variant_unref(gvar);
244
245 assert(!_updating_sample_rate);
246 _updating_sample_rate = true;
247 _sample_rate.set_value(samplerate);
248 _updating_sample_rate = false;
249}
250
251void SamplingBar::update_sample_count_selector()
252{
253 GVariant *gvar;
254
255 if (_updating_sample_count)
256 return;
257
258 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
259 if (!dev_inst)
260 return;
261
262 assert(!_updating_sample_count);
263 _updating_sample_count = true;
264
265 if (_sample_count_supported)
266 {
267 uint64_t sample_count = _sample_count.value();
268 uint64_t min_sample_count = 0;
269 uint64_t max_sample_count = MaxSampleCount;
270
271 if (sample_count == 0)
272 sample_count = DefaultSampleCount;
273
274 if ((gvar = dev_inst->list_config(NULL, SR_CONF_LIMIT_SAMPLES)))
275 {
276 g_variant_get(gvar, "(tt)",
277 &min_sample_count, &max_sample_count);
278 g_variant_unref(gvar);
279 }
280
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);
286
287 if ((gvar = dev_inst->get_config(NULL, SR_CONF_LIMIT_SAMPLES)))
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);
299 }
300 else
301 _sample_count.show_none();
302
303 _updating_sample_count = false;
304}
305
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
359void SamplingBar::commit_sample_count()
360{
361 uint64_t sample_count = 0;
362
363 if (_updating_sample_count)
364 return;
365
366 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
367 if (!dev_inst)
368 return;
369
370 sample_count = _sample_count.value();
371
372 // Set the sample count
373 assert(!_updating_sample_count);
374 _updating_sample_count = true;
375 if (_sample_count_supported &&
376 !dev_inst->set_config(NULL, SR_CONF_LIMIT_SAMPLES,
377 g_variant_new_uint64(sample_count))) {
378 qDebug() << "Failed to configure sample count.";
379 return;
380 }
381 _updating_sample_count = false;
382}
383
384void SamplingBar::commit_sample_rate()
385{
386 uint64_t sample_rate = 0;
387
388 if (_updating_sample_rate)
389 return;
390
391 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
392 if (!dev_inst)
393 return;
394
395 sample_rate = _sample_rate.value();
396 if (sample_rate == 0)
397 return;
398
399 // Set the samplerate
400 assert(!_updating_sample_rate);
401 _updating_sample_rate = true;
402 if (!dev_inst->set_config(NULL, SR_CONF_SAMPLERATE,
403 g_variant_new_uint64(sample_rate))) {
404 qDebug() << "Failed to configure samplerate.";
405 return;
406 }
407 _updating_sample_rate = false;
408}
409
410void SamplingBar::on_device_selected()
411{
412 if (_updating_device_selector)
413 return;
414
415 const shared_ptr<device::DevInst> dev_inst = get_selected_device();
416 if (!dev_inst)
417 return;
418
419 _session.set_device(dev_inst);
420
421 update_device_config_widgets();
422}
423
424void SamplingBar::on_sample_count_changed()
425{
426 commit_sample_count();
427}
428
429void SamplingBar::on_sample_rate_changed()
430{
431 commit_sample_rate();
432}
433
434void SamplingBar::on_run_stop()
435{
436 commit_sample_count();
437 commit_sample_rate();
438 run_stop();
439}
440
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
449} // namespace toolbars
450} // namespace pv