]> sigrok.org Git - pulseview.git/blob - pv/toolbars/samplingbar.cpp
SamplingBar: Only use DefaultSampleCount if the _sample_count widget is unset.
[pulseview.git] / pv / toolbars / samplingbar.cpp
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
37 using boost::shared_ptr;
38 using std::map;
39 using std::max;
40 using std::min;
41 using std::string;
42
43 namespace pv {
44 namespace toolbars {
45
46 const uint64_t SamplingBar::MinSampleCount = 100ULL;
47 const uint64_t SamplingBar::MaxSampleCount = 1000000000000ULL;
48 const uint64_t SamplingBar::DefaultSampleCount = 1000000;
49
50 SamplingBar::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
98 void SamplingBar::set_device_list(
99         const std::list< shared_ptr<pv::device::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::device::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
120 shared_ptr<pv::device::DevInst> SamplingBar::get_selected_device() const
121 {
122         const int index = _device_selector.currentIndex();
123         if (index < 0)
124                 return shared_ptr<pv::device::DevInst>();
125
126         const sr_dev_inst *const sdi =
127                 (const sr_dev_inst*)_device_selector.itemData(
128                         index).value<void*>();
129         assert(sdi);
130
131         map<const sr_dev_inst*, boost::weak_ptr<device::DevInst> >::
132                 const_iterator iter = _device_selector_map.find(sdi);
133         if (iter == _device_selector_map.end())
134                 return shared_ptr<pv::device::DevInst>();
135
136         return shared_ptr<pv::device::DevInst>((*iter).second);
137 }
138
139 void SamplingBar::set_selected_device(shared_ptr<pv::device::DevInst> dev_inst)
140 {
141         assert(dev_inst);
142
143         for (int i = 0; i < _device_selector.count(); i++)
144                 if (dev_inst->dev_inst() ==
145                         _device_selector.itemData(i).value<void*>()) {
146                         // Calling this leads to on_device_selected being
147                         // invoked, which updates the sampling bar widgets.
148                         _device_selector.setCurrentIndex(i);
149                         return;
150                 }
151 }
152
153 void 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
161 void 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
226 void 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
251 void 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
306 void SamplingBar::commit_sample_count()
307 {
308         uint64_t sample_count = 0;
309
310         if (_updating_sample_count)
311                 return;
312
313         const shared_ptr<device::DevInst> dev_inst = get_selected_device();
314         if (!dev_inst)
315                 return;
316
317         sample_count = _sample_count.value();
318
319         // Set the sample count
320         assert(!_updating_sample_count);
321         _updating_sample_count = true;
322         if (_sample_count_supported &&
323                 !dev_inst->set_config(NULL, SR_CONF_LIMIT_SAMPLES,
324                 g_variant_new_uint64(sample_count))) {
325                 qDebug() << "Failed to configure sample count.";
326                 return;
327         }
328         _updating_sample_count = false;
329 }
330
331 void SamplingBar::commit_sample_rate()
332 {
333         uint64_t sample_rate = 0;
334
335         if (_updating_sample_rate)
336                 return;
337
338         const shared_ptr<device::DevInst> dev_inst = get_selected_device();
339         if (!dev_inst)
340                 return;
341
342         sample_rate = _sample_rate.value();
343         if (sample_rate == 0)
344                 return;
345
346         // Set the samplerate
347         assert(!_updating_sample_rate);
348         _updating_sample_rate = true;
349         if (!dev_inst->set_config(NULL, SR_CONF_SAMPLERATE,
350                 g_variant_new_uint64(sample_rate))) {
351                 qDebug() << "Failed to configure samplerate.";
352                 return;
353         }
354         _updating_sample_rate = false;
355 }
356
357 void SamplingBar::on_device_selected()
358 {
359         GVariant *gvar;
360
361         using namespace pv::popups;
362
363         if (_updating_device_selector)
364                 return;
365
366         const shared_ptr<device::DevInst> dev_inst = get_selected_device();
367         if (!dev_inst)
368                 return;
369
370         _session.set_device(dev_inst);
371
372         // Update the configure popup
373         DeviceOptions *const opts = new DeviceOptions(dev_inst, this);
374         _configure_button_action->setVisible(
375                 !opts->binding().properties().empty());
376         _configure_button.set_popup(opts);
377
378         // Update the probes popup
379         Probes *const probes = new Probes(_session, this);
380         _probes_button.set_popup(probes);
381
382         // Update supported options.
383         _sample_count_supported = false;
384
385         if ((gvar = dev_inst->list_config(NULL, SR_CONF_DEVICE_OPTIONS)))
386         {
387                 gsize num_opts;
388                 const int *const options =
389                         (const int32_t *)g_variant_get_fixed_array(
390                                 gvar, &num_opts, sizeof(int32_t));
391                 for (unsigned int i = 0; i < num_opts; i++)
392                 {
393                         switch (options[i]) {
394                         case SR_CONF_LIMIT_SAMPLES:
395                                 _sample_count_supported = true;
396                                 break;
397                         case SR_CONF_LIMIT_FRAMES:
398                                 dev_inst->set_config(NULL, SR_CONF_LIMIT_FRAMES,
399                                         g_variant_new_uint64(1));
400                                 break;
401                         }
402                 }
403         }
404
405         // Add notification of reconfigure events
406         disconnect(this, SLOT(on_config_changed()));
407         connect(dev_inst.get(), SIGNAL(config_changed()),
408                 this, SLOT(on_config_changed()));
409
410         // Update sweep timing widgets.
411         update_sample_count_selector();
412         update_sample_rate_selector();
413 }
414
415 void SamplingBar::on_sample_count_changed()
416 {
417         commit_sample_count();
418 }
419
420 void SamplingBar::on_sample_rate_changed()
421 {
422         commit_sample_rate();
423 }
424
425 void SamplingBar::on_run_stop()
426 {
427         commit_sample_count();
428         commit_sample_rate();   
429         run_stop();
430 }
431
432 void SamplingBar::on_config_changed()
433 {
434         commit_sample_count();
435         update_sample_count_selector(); 
436         commit_sample_rate();   
437         update_sample_rate_selector();
438 }
439
440 } // namespace toolbars
441 } // namespace pv