]> sigrok.org Git - pulseview.git/blame - pv/toolbars/samplingbar.cpp
Added DevInst::config_changed signal
[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
19adbc2c
JH
167 const shared_ptr<DevInst> dev_inst = get_selected_device();
168 if (!dev_inst)
ef4d0201
JH
169 return;
170
1198b887
JH
171 _updating_sample_rate = true;
172
8dd44190 173 if (!(gvar_dict = dev_inst->list_config(NULL, SR_CONF_SAMPLERATE)))
1198b887
JH
174 {
175 _sample_rate.show_none();
176 _updating_sample_rate = false;
dde1a563 177 return;
1198b887 178 }
dde1a563 179
488f5d3f 180 if ((gvar_list = g_variant_lookup_value(gvar_dict,
1198b887
JH
181 "samplerate-steps", G_VARIANT_TYPE("at"))))
182 {
488f5d3f
BV
183 elements = (const uint64_t *)g_variant_get_fixed_array(
184 gvar_list, &num_elements, sizeof(uint64_t));
aafe53af
JH
185
186 const uint64_t min = elements[0];
187 const uint64_t max = elements[1];
188 const uint64_t step = elements[2];
189
488f5d3f 190 g_variant_unref(gvar_list);
aafe53af
JH
191
192 assert(min > 0);
193 assert(max > 0);
194 assert(max > min);
195 assert(step > 0);
196
197 if (step == 1)
198 _sample_rate.show_125_list(min, max);
199 else
200 {
201 // When the step is not 1, we cam't make a 1-2-5-10
202 // list of sample rates, because we may not be able to
203 // make round numbers. Therefore in this case, show a
204 // spin box.
205 _sample_rate.show_min_max_step(min, max, step);
206 }
dde1a563 207 }
488f5d3f
BV
208 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
209 "samplerates", G_VARIANT_TYPE("at"))))
dde1a563 210 {
488f5d3f
BV
211 elements = (const uint64_t *)g_variant_get_fixed_array(
212 gvar_list, &num_elements, sizeof(uint64_t));
1198b887 213 _sample_rate.show_list(elements, num_elements);
488f5d3f 214 g_variant_unref(gvar_list);
dde1a563 215 }
1198b887 216 _updating_sample_rate = false;
48888313 217
488f5d3f 218 g_variant_unref(gvar_dict);
48888313
JH
219 update_sample_rate_selector_value();
220}
221
222void SamplingBar::update_sample_rate_selector_value()
223{
488f5d3f
BV
224 GVariant *gvar;
225 uint64_t samplerate;
226
19adbc2c
JH
227 const shared_ptr<DevInst> dev_inst = get_selected_device();
228 if (!dev_inst)
229 return;
230
8dd44190
JH
231 if (!(gvar = dev_inst->get_config(NULL, SR_CONF_SAMPLERATE))) {
232 qDebug() << "WARNING: Failed to get value of sample rate";
eb4008a6
JH
233 return;
234 }
488f5d3f
BV
235 samplerate = g_variant_get_uint64(gvar);
236 g_variant_unref(gvar);
48888313 237
1198b887
JH
238 _updating_sample_rate = true;
239 _sample_rate.set_value(samplerate);
240 _updating_sample_rate = false;
48888313
JH
241}
242
85756012
ML
243void SamplingBar::update_sample_count_selector()
244{
85756012 245 GVariant *gvar;
85756012 246
19adbc2c
JH
247 const shared_ptr<DevInst> dev_inst = get_selected_device();
248 if (!dev_inst)
249 return;
250
b50ef520 251 _updating_sample_count = true;
85756012 252
b50ef520 253 if (_sample_count_supported)
df3c1aa3 254 {
b50ef520 255 uint64_t sample_count = DefaultSampleCount;
1d04852f 256 uint64_t min_sample_count = 0;
b50ef520
JH
257 uint64_t max_sample_count = MaxSampleCount;
258
8dd44190
JH
259 if ((gvar = dev_inst->list_config(NULL, SR_CONF_LIMIT_SAMPLES)))
260 {
1d04852f
JH
261 g_variant_get(gvar, "(tt)",
262 &min_sample_count, &max_sample_count);
b50ef520
JH
263 g_variant_unref(gvar);
264 }
265
1d04852f
JH
266 min_sample_count = min(max(min_sample_count, MinSampleCount),
267 max_sample_count);
268
269 _sample_count.show_125_list(
270 min_sample_count, max_sample_count);
b50ef520 271
8dd44190 272 if ((gvar = dev_inst->get_config(NULL, SR_CONF_LIMIT_SAMPLES)))
b50ef520
JH
273 {
274 sample_count = g_variant_get_uint64(gvar);
275 if (sample_count == 0)
276 sample_count = DefaultSampleCount;
277 sample_count = min(max(sample_count, MinSampleCount),
278 max_sample_count);
279
280 g_variant_unref(gvar);
281 }
282
283 _sample_count.set_value(sample_count);
85756012 284 }
b50ef520
JH
285 else
286 _sample_count.show_none();
df3c1aa3 287
df3c1aa3 288 _updating_sample_count = false;
85756012
ML
289}
290
124d97de
ML
291void SamplingBar::commit_sample_count()
292{
293 uint64_t sample_count = 0;
294
19adbc2c
JH
295 const shared_ptr<DevInst> dev_inst = get_selected_device();
296 if (!dev_inst)
297 return;
298
124d97de
ML
299 sample_count = _sample_count.value();
300
301 // Set the sample count
8dd44190
JH
302 if (!dev_inst->set_config(NULL, SR_CONF_LIMIT_SAMPLES,
303 g_variant_new_uint64(sample_count))) {
124d97de
ML
304 qDebug() << "Failed to configure sample count.";
305 return;
306 }
307}
308
48888313
JH
309void SamplingBar::commit_sample_rate()
310{
311 uint64_t sample_rate = 0;
312
19adbc2c
JH
313 const shared_ptr<DevInst> dev_inst = get_selected_device();
314 if (!dev_inst)
315 return;
316
1198b887 317 sample_rate = _sample_rate.value();
f9541bde
JH
318 if (sample_rate == 0)
319 return;
320
48888313 321 // Set the samplerate
8dd44190
JH
322 if (!dev_inst->set_config(NULL, SR_CONF_SAMPLERATE,
323 g_variant_new_uint64(sample_rate))) {
48888313
JH
324 qDebug() << "Failed to configure samplerate.";
325 return;
326 }
dde1a563
JH
327}
328
329void SamplingBar::on_device_selected()
330{
14cbfcc8
ML
331 GVariant *gvar;
332
488f068c 333 using namespace pv::popups;
51d4a9ab 334
95237c18
JH
335 if (_updating_device_selector)
336 return;
337
19adbc2c
JH
338 const shared_ptr<DevInst> dev_inst = get_selected_device();
339 if (!dev_inst)
340 return;
341
19adbc2c 342 _session.set_device(dev_inst);
51d4a9ab 343
b3e8a5d8 344 // Update the configure popup
19adbc2c 345 DeviceOptions *const opts = new DeviceOptions(dev_inst, this);
b3e8a5d8
JH
346 _configure_button_action->setVisible(
347 !opts->binding().properties().empty());
348 _configure_button.set_popup(opts);
488f068c
JH
349
350 // Update the probes popup
351 Probes *const probes = new Probes(_session, this);
352 _probes_button.set_popup(probes);
df539680 353
14cbfcc8
ML
354 // Update supported options.
355 _sample_count_supported = false;
356
8dd44190 357 if ((gvar = dev_inst->list_config(NULL, SR_CONF_DEVICE_OPTIONS)))
14cbfcc8
ML
358 {
359 gsize num_opts;
8dd44190
JH
360 const int *const options =
361 (const int32_t *)g_variant_get_fixed_array(
362 gvar, &num_opts, sizeof(int32_t));
14cbfcc8
ML
363 for (unsigned int i = 0; i < num_opts; i++)
364 {
365 switch (options[i]) {
366 case SR_CONF_LIMIT_SAMPLES:
367 _sample_count_supported = true;
368 break;
369 case SR_CONF_LIMIT_FRAMES:
8dd44190 370 dev_inst->set_config(NULL, SR_CONF_LIMIT_FRAMES,
14cbfcc8
ML
371 g_variant_new_uint64(1));
372 break;
373 }
374 }
375 }
376
df539680
ML
377 // Update sweep timing widgets.
378 update_sample_count_selector();
379 update_sample_rate_selector();
dde1a563 380}
51e77110 381
124d97de
ML
382void SamplingBar::on_sample_count_changed()
383{
85756012
ML
384 if(!_updating_sample_count)
385 commit_sample_count();
124d97de
ML
386}
387
48888313
JH
388void SamplingBar::on_sample_rate_changed()
389{
1198b887
JH
390 if (!_updating_sample_rate)
391 commit_sample_rate();
48888313
JH
392}
393
9f3d12f3
JH
394void SamplingBar::on_run_stop()
395{
b50ef520 396 commit_sample_count();
9f3d12f3
JH
397 commit_sample_rate();
398 run_stop();
399}
400
f4c92e1c 401} // namespace toolbars
51e77110 402} // namespace pv