]> sigrok.org Git - pulseview.git/blame - pv/toolbars/samplingbar.cpp
Updated the sample rate selector when the config is changed
[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>
19adbc2c 33#include <pv/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(
19adbc2c 99 const std::list< shared_ptr<pv::DevInst> > &devices)
18203d86 100{
95237c18
JH
101 _updating_device_selector = true;
102
18203d86 103 _device_selector.clear();
19adbc2c 104 _device_selector_map.clear();
18203d86 105
19adbc2c
JH
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;
dd63af74
JH
113 _device_selector.addItem(title.c_str(),
114 qVariantFromValue((void*)sdi));
18203d86
JH
115 }
116
95237c18
JH
117 _updating_device_selector = false;
118
b7b659aa 119 on_device_selected();
18203d86
JH
120}
121
19adbc2c 122shared_ptr<pv::DevInst> SamplingBar::get_selected_device() const
d4984fe7 123{
6fb67b27 124 const int index = _device_selector.currentIndex();
333d5bbc 125 if (index < 0)
19adbc2c
JH
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);
6fb67b27 132
19adbc2c
JH
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);
6fb67b27
JH
139}
140
19adbc2c 141void SamplingBar::set_selected_device(boost::shared_ptr<pv::DevInst> dev_inst)
dba73e73 142{
19adbc2c
JH
143 assert(dev_inst);
144
793f8096 145 for (int i = 0; i < _device_selector.count(); i++)
19adbc2c
JH
146 if (dev_inst->dev_inst() ==
147 _device_selector.itemData(i).value<void*>()) {
dba73e73
JH
148 _device_selector.setCurrentIndex(i);
149 return;
150 }
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
19adbc2c
JH
170 const shared_ptr<DevInst> dev_inst = get_selected_device();
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
19adbc2c
JH
234 const shared_ptr<DevInst> dev_inst = get_selected_device();
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
19adbc2c
JH
258 const shared_ptr<DevInst> dev_inst = get_selected_device();
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 {
b50ef520 267 uint64_t sample_count = DefaultSampleCount;
1d04852f 268 uint64_t min_sample_count = 0;
b50ef520
JH
269 uint64_t max_sample_count = MaxSampleCount;
270
8dd44190
JH
271 if ((gvar = dev_inst->list_config(NULL, SR_CONF_LIMIT_SAMPLES)))
272 {
1d04852f
JH
273 g_variant_get(gvar, "(tt)",
274 &min_sample_count, &max_sample_count);
b50ef520
JH
275 g_variant_unref(gvar);
276 }
277
1d04852f
JH
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);
b50ef520 283
8dd44190 284 if ((gvar = dev_inst->get_config(NULL, SR_CONF_LIMIT_SAMPLES)))
b50ef520
JH
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);
85756012 296 }
b50ef520
JH
297 else
298 _sample_count.show_none();
df3c1aa3 299
df3c1aa3 300 _updating_sample_count = false;
85756012
ML
301}
302
124d97de
ML
303void SamplingBar::commit_sample_count()
304{
305 uint64_t sample_count = 0;
306
3d4f16af
JH
307 if (_updating_sample_count)
308 return;
309
19adbc2c
JH
310 const shared_ptr<DevInst> dev_inst = get_selected_device();
311 if (!dev_inst)
312 return;
313
124d97de
ML
314 sample_count = _sample_count.value();
315
316 // Set the sample count
3d4f16af
JH
317 assert(!_updating_sample_count);
318 _updating_sample_count = true;
8dd44190
JH
319 if (!dev_inst->set_config(NULL, SR_CONF_LIMIT_SAMPLES,
320 g_variant_new_uint64(sample_count))) {
124d97de
ML
321 qDebug() << "Failed to configure sample count.";
322 return;
323 }
3d4f16af 324 _updating_sample_count = false;
124d97de
ML
325}
326
48888313
JH
327void SamplingBar::commit_sample_rate()
328{
329 uint64_t sample_rate = 0;
330
3d4f16af
JH
331 if (_updating_sample_rate)
332 return;
333
19adbc2c
JH
334 const shared_ptr<DevInst> dev_inst = get_selected_device();
335 if (!dev_inst)
336 return;
337
1198b887 338 sample_rate = _sample_rate.value();
f9541bde
JH
339 if (sample_rate == 0)
340 return;
341
48888313 342 // Set the samplerate
3d4f16af
JH
343 assert(!_updating_sample_rate);
344 _updating_sample_rate = true;
8dd44190
JH
345 if (!dev_inst->set_config(NULL, SR_CONF_SAMPLERATE,
346 g_variant_new_uint64(sample_rate))) {
48888313
JH
347 qDebug() << "Failed to configure samplerate.";
348 return;
349 }
3d4f16af 350 _updating_sample_rate = false;
dde1a563
JH
351}
352
353void SamplingBar::on_device_selected()
354{
14cbfcc8
ML
355 GVariant *gvar;
356
488f068c 357 using namespace pv::popups;
51d4a9ab 358
95237c18
JH
359 if (_updating_device_selector)
360 return;
361
19adbc2c
JH
362 const shared_ptr<DevInst> dev_inst = get_selected_device();
363 if (!dev_inst)
364 return;
365
19adbc2c 366 _session.set_device(dev_inst);
51d4a9ab 367
b3e8a5d8 368 // Update the configure popup
19adbc2c 369 DeviceOptions *const opts = new DeviceOptions(dev_inst, this);
b3e8a5d8
JH
370 _configure_button_action->setVisible(
371 !opts->binding().properties().empty());
372 _configure_button.set_popup(opts);
488f068c
JH
373
374 // Update the probes popup
375 Probes *const probes = new Probes(_session, this);
376 _probes_button.set_popup(probes);
df539680 377
14cbfcc8
ML
378 // Update supported options.
379 _sample_count_supported = false;
380
8dd44190 381 if ((gvar = dev_inst->list_config(NULL, SR_CONF_DEVICE_OPTIONS)))
14cbfcc8
ML
382 {
383 gsize num_opts;
8dd44190
JH
384 const int *const options =
385 (const int32_t *)g_variant_get_fixed_array(
386 gvar, &num_opts, sizeof(int32_t));
14cbfcc8
ML
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:
8dd44190 394 dev_inst->set_config(NULL, SR_CONF_LIMIT_FRAMES,
14cbfcc8
ML
395 g_variant_new_uint64(1));
396 break;
397 }
398 }
399 }
400
82afd5e3
JH
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
df539680
ML
406 // Update sweep timing widgets.
407 update_sample_count_selector();
408 update_sample_rate_selector();
dde1a563 409}
51e77110 410
124d97de
ML
411void SamplingBar::on_sample_count_changed()
412{
3d4f16af 413 commit_sample_count();
124d97de
ML
414}
415
48888313
JH
416void SamplingBar::on_sample_rate_changed()
417{
3d4f16af 418 commit_sample_rate();
48888313
JH
419}
420
9f3d12f3
JH
421void SamplingBar::on_run_stop()
422{
b50ef520 423 commit_sample_count();
9f3d12f3
JH
424 commit_sample_rate();
425 run_stop();
426}
427
82afd5e3
JH
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
f4c92e1c 436} // namespace toolbars
51e77110 437} // namespace pv