]> sigrok.org Git - pulseview.git/blame_incremental - pv/toolbars/samplingbar.cpp
Updated the sample rate selector when the config is changed
[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/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::DevInst> > &devices)
100{
101 _updating_device_selector = true;
102
103 _device_selector.clear();
104 _device_selector_map.clear();
105
106 BOOST_FOREACH (shared_ptr<pv::DevInst> dev_inst, devices) {
107 assert(dev_inst);
108 const string title = dev_inst->format_device_title();
109 const sr_dev_inst *sdi = dev_inst->dev_inst();
110 assert(sdi);
111
112 _device_selector_map[sdi] = dev_inst;
113 _device_selector.addItem(title.c_str(),
114 qVariantFromValue((void*)sdi));
115 }
116
117 _updating_device_selector = false;
118
119 on_device_selected();
120}
121
122shared_ptr<pv::DevInst> SamplingBar::get_selected_device() const
123{
124 const int index = _device_selector.currentIndex();
125 if (index < 0)
126 return shared_ptr<pv::DevInst>();
127
128 const sr_dev_inst *const sdi =
129 (const sr_dev_inst*)_device_selector.itemData(
130 index).value<void*>();
131 assert(sdi);
132
133 map<const sr_dev_inst*, boost::weak_ptr<DevInst> >::
134 const_iterator iter = _device_selector_map.find(sdi);
135 if (iter == _device_selector_map.end())
136 return shared_ptr<pv::DevInst>();
137
138 return shared_ptr<pv::DevInst>((*iter).second);
139}
140
141void SamplingBar::set_selected_device(boost::shared_ptr<pv::DevInst> dev_inst)
142{
143 assert(dev_inst);
144
145 for (int i = 0; i < _device_selector.count(); i++)
146 if (dev_inst->dev_inst() ==
147 _device_selector.itemData(i).value<void*>()) {
148 _device_selector.setCurrentIndex(i);
149 return;
150 }
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<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<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<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 = DefaultSampleCount;
268 uint64_t min_sample_count = 0;
269 uint64_t max_sample_count = MaxSampleCount;
270
271 if ((gvar = dev_inst->list_config(NULL, SR_CONF_LIMIT_SAMPLES)))
272 {
273 g_variant_get(gvar, "(tt)",
274 &min_sample_count, &max_sample_count);
275 g_variant_unref(gvar);
276 }
277
278 min_sample_count = min(max(min_sample_count, MinSampleCount),
279 max_sample_count);
280
281 _sample_count.show_125_list(
282 min_sample_count, max_sample_count);
283
284 if ((gvar = dev_inst->get_config(NULL, SR_CONF_LIMIT_SAMPLES)))
285 {
286 sample_count = g_variant_get_uint64(gvar);
287 if (sample_count == 0)
288 sample_count = DefaultSampleCount;
289 sample_count = min(max(sample_count, MinSampleCount),
290 max_sample_count);
291
292 g_variant_unref(gvar);
293 }
294
295 _sample_count.set_value(sample_count);
296 }
297 else
298 _sample_count.show_none();
299
300 _updating_sample_count = false;
301}
302
303void SamplingBar::commit_sample_count()
304{
305 uint64_t sample_count = 0;
306
307 if (_updating_sample_count)
308 return;
309
310 const shared_ptr<DevInst> dev_inst = get_selected_device();
311 if (!dev_inst)
312 return;
313
314 sample_count = _sample_count.value();
315
316 // Set the sample count
317 assert(!_updating_sample_count);
318 _updating_sample_count = true;
319 if (!dev_inst->set_config(NULL, SR_CONF_LIMIT_SAMPLES,
320 g_variant_new_uint64(sample_count))) {
321 qDebug() << "Failed to configure sample count.";
322 return;
323 }
324 _updating_sample_count = false;
325}
326
327void SamplingBar::commit_sample_rate()
328{
329 uint64_t sample_rate = 0;
330
331 if (_updating_sample_rate)
332 return;
333
334 const shared_ptr<DevInst> dev_inst = get_selected_device();
335 if (!dev_inst)
336 return;
337
338 sample_rate = _sample_rate.value();
339 if (sample_rate == 0)
340 return;
341
342 // Set the samplerate
343 assert(!_updating_sample_rate);
344 _updating_sample_rate = true;
345 if (!dev_inst->set_config(NULL, SR_CONF_SAMPLERATE,
346 g_variant_new_uint64(sample_rate))) {
347 qDebug() << "Failed to configure samplerate.";
348 return;
349 }
350 _updating_sample_rate = false;
351}
352
353void SamplingBar::on_device_selected()
354{
355 GVariant *gvar;
356
357 using namespace pv::popups;
358
359 if (_updating_device_selector)
360 return;
361
362 const shared_ptr<DevInst> dev_inst = get_selected_device();
363 if (!dev_inst)
364 return;
365
366 _session.set_device(dev_inst);
367
368 // Update the configure popup
369 DeviceOptions *const opts = new DeviceOptions(dev_inst, this);
370 _configure_button_action->setVisible(
371 !opts->binding().properties().empty());
372 _configure_button.set_popup(opts);
373
374 // Update the probes popup
375 Probes *const probes = new Probes(_session, this);
376 _probes_button.set_popup(probes);
377
378 // Update supported options.
379 _sample_count_supported = false;
380
381 if ((gvar = dev_inst->list_config(NULL, SR_CONF_DEVICE_OPTIONS)))
382 {
383 gsize num_opts;
384 const int *const options =
385 (const int32_t *)g_variant_get_fixed_array(
386 gvar, &num_opts, sizeof(int32_t));
387 for (unsigned int i = 0; i < num_opts; i++)
388 {
389 switch (options[i]) {
390 case SR_CONF_LIMIT_SAMPLES:
391 _sample_count_supported = true;
392 break;
393 case SR_CONF_LIMIT_FRAMES:
394 dev_inst->set_config(NULL, SR_CONF_LIMIT_FRAMES,
395 g_variant_new_uint64(1));
396 break;
397 }
398 }
399 }
400
401 // Add notification of reconfigure events
402 disconnect(this, SLOT(on_config_changed()));
403 connect(dev_inst.get(), SIGNAL(config_changed()),
404 this, SLOT(on_config_changed()));
405
406 // Update sweep timing widgets.
407 update_sample_count_selector();
408 update_sample_rate_selector();
409}
410
411void SamplingBar::on_sample_count_changed()
412{
413 commit_sample_count();
414}
415
416void SamplingBar::on_sample_rate_changed()
417{
418 commit_sample_rate();
419}
420
421void SamplingBar::on_run_stop()
422{
423 commit_sample_count();
424 commit_sample_rate();
425 run_stop();
426}
427
428void SamplingBar::on_config_changed()
429{
430 commit_sample_count();
431 update_sample_count_selector();
432 commit_sample_rate();
433 update_sample_rate_selector();
434}
435
436} // namespace toolbars
437} // namespace pv