]> sigrok.org Git - pulseview.git/blob - pv/toolbars/samplingbar.cpp
05c730bf528fe35efdab94acea14be9183838fd6
[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 #include <QHelpEvent>
30 #include <QToolTip>
31
32 #include "samplingbar.h"
33
34 #include <pv/devicemanager.h>
35 #include <pv/device/devinst.h>
36 #include <pv/popups/deviceoptions.h>
37 #include <pv/popups/probes.h>
38
39 using boost::shared_ptr;
40 using std::map;
41 using std::max;
42 using std::min;
43 using std::string;
44
45 namespace pv {
46 namespace toolbars {
47
48 const uint64_t SamplingBar::MinSampleCount = 100ULL;
49 const uint64_t SamplingBar::MaxSampleCount = 1000000000000ULL;
50 const uint64_t SamplingBar::DefaultSampleCount = 1000000;
51
52 SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
53         QToolBar("Sampling Bar", parent),
54         _session(session),
55         _device_selector(this),
56         _updating_device_selector(false),
57         _configure_button(this),
58         _configure_button_action(NULL),
59         _probes_button(this),
60         _sample_count(" samples", this),
61         _sample_rate("Hz", this),
62         _updating_sample_rate(false),
63         _updating_sample_count(false),
64         _sample_count_supported(false),
65         _icon_red(":/icons/status-red.svg"),
66         _icon_green(":/icons/status-green.svg"),
67         _icon_grey(":/icons/status-grey.svg"),
68         _run_stop_button(this)
69 {
70         connect(&_run_stop_button, SIGNAL(clicked()),
71                 this, SLOT(on_run_stop()));
72         connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
73                 this, SLOT(on_device_selected()));
74         connect(&_sample_count, SIGNAL(value_changed()),
75                 this, SLOT(on_sample_count_changed()));
76         connect(&_sample_rate, SIGNAL(value_changed()),
77                 this, SLOT(on_sample_rate_changed()));
78
79         _sample_count.show_min_max_step(0, UINT64_MAX, 1);
80
81         set_capture_state(pv::SigSession::Stopped);
82
83         _configure_button.setIcon(QIcon::fromTheme("configure",
84                 QIcon(":/icons/configure.png")));
85
86         _probes_button.setIcon(QIcon::fromTheme("probes",
87                 QIcon(":/icons/probes.svg")));
88
89         _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
90
91         addWidget(&_device_selector);
92         _configure_button_action = addWidget(&_configure_button);
93         addWidget(&_probes_button);
94         addWidget(&_sample_count);
95         addWidget(&_sample_rate);
96
97         addWidget(&_run_stop_button);
98
99         _sample_count.installEventFilter(this);
100         _sample_rate.installEventFilter(this);
101 }
102
103 void SamplingBar::set_device_list(
104         const std::list< shared_ptr<pv::device::DevInst> > &devices,
105         shared_ptr<pv::device::DevInst> selected)
106 {
107         int selected_index = -1;
108
109         assert(selected);
110
111         _updating_device_selector = true;
112
113         _device_selector.clear();
114         _device_selector_map.clear();
115
116         BOOST_FOREACH (shared_ptr<pv::device::DevInst> dev_inst, devices) {
117                 assert(dev_inst);
118                 const string title = dev_inst->format_device_title();
119                 const sr_dev_inst *sdi = dev_inst->dev_inst();
120                 assert(sdi);
121
122                 if (selected == dev_inst)
123                         selected_index = _device_selector.count();
124
125                 _device_selector_map[sdi] = dev_inst;
126                 _device_selector.addItem(title.c_str(),
127                         qVariantFromValue((void*)sdi));
128         }
129
130         // The selected device should have been in the list
131         assert(selected_index != -1);
132         _device_selector.setCurrentIndex(selected_index);
133
134         update_device_config_widgets();
135
136         _updating_device_selector = false;
137 }
138
139 shared_ptr<pv::device::DevInst> SamplingBar::get_selected_device() const
140 {
141         const int index = _device_selector.currentIndex();
142         if (index < 0)
143                 return shared_ptr<pv::device::DevInst>();
144
145         const sr_dev_inst *const sdi =
146                 (const sr_dev_inst*)_device_selector.itemData(
147                         index).value<void*>();
148         assert(sdi);
149
150         map<const sr_dev_inst*, boost::weak_ptr<device::DevInst> >::
151                 const_iterator iter = _device_selector_map.find(sdi);
152         if (iter == _device_selector_map.end())
153                 return shared_ptr<pv::device::DevInst>();
154
155         return shared_ptr<pv::device::DevInst>((*iter).second);
156 }
157
158 void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
159 {
160         const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
161         _run_stop_button.setIcon(*icons[state]);
162         _run_stop_button.setText((state == pv::SigSession::Stopped) ?
163                 tr("Run") : tr("Stop"));
164 }
165
166 void SamplingBar::update_sample_rate_selector()
167 {
168         GVariant *gvar_dict, *gvar_list;
169         const uint64_t *elements = NULL;
170         gsize num_elements;
171
172         if (_updating_sample_rate)
173                 return;
174
175         const shared_ptr<device::DevInst> dev_inst = get_selected_device();
176         if (!dev_inst)
177                 return;
178
179         assert(!_updating_sample_rate);
180         _updating_sample_rate = true;
181
182         if (!(gvar_dict = dev_inst->list_config(NULL, SR_CONF_SAMPLERATE)))
183         {
184                 _sample_rate.show_none();
185                 _updating_sample_rate = false;
186                 return;
187         }
188
189         if ((gvar_list = g_variant_lookup_value(gvar_dict,
190                         "samplerate-steps", G_VARIANT_TYPE("at"))))
191         {
192                 elements = (const uint64_t *)g_variant_get_fixed_array(
193                                 gvar_list, &num_elements, sizeof(uint64_t));
194
195                 const uint64_t min = elements[0];
196                 const uint64_t max = elements[1];
197                 const uint64_t step = elements[2];
198
199                 g_variant_unref(gvar_list);
200
201                 assert(min > 0);
202                 assert(max > 0);
203                 assert(max > min);
204                 assert(step > 0);
205
206                 if (step == 1)
207                         _sample_rate.show_125_list(min, max);
208                 else
209                 {
210                         // When the step is not 1, we cam't make a 1-2-5-10
211                         // list of sample rates, because we may not be able to
212                         // make round numbers. Therefore in this case, show a
213                         // spin box.
214                         _sample_rate.show_min_max_step(min, max, step);
215                 }
216         }
217         else if ((gvar_list = g_variant_lookup_value(gvar_dict,
218                         "samplerates", G_VARIANT_TYPE("at"))))
219         {
220                 elements = (const uint64_t *)g_variant_get_fixed_array(
221                                 gvar_list, &num_elements, sizeof(uint64_t));
222                 _sample_rate.show_list(elements, num_elements);
223                 g_variant_unref(gvar_list);
224         }
225         _updating_sample_rate = false;
226
227         g_variant_unref(gvar_dict);
228         update_sample_rate_selector_value();
229 }
230
231 void SamplingBar::update_sample_rate_selector_value()
232 {
233         GVariant *gvar;
234         uint64_t samplerate;
235
236         if (_updating_sample_rate)
237                 return;
238
239         const shared_ptr<device::DevInst> dev_inst = get_selected_device();
240         if (!dev_inst)
241                 return;
242
243         if (!(gvar = dev_inst->get_config(NULL, SR_CONF_SAMPLERATE))) {
244                 qDebug() << "WARNING: Failed to get value of sample rate";
245                 return;
246         }
247         samplerate = g_variant_get_uint64(gvar);
248         g_variant_unref(gvar);
249
250         assert(!_updating_sample_rate);
251         _updating_sample_rate = true;
252         _sample_rate.set_value(samplerate);
253         _updating_sample_rate = false;
254 }
255
256 void SamplingBar::update_sample_count_selector()
257 {
258         GVariant *gvar;
259
260         if (_updating_sample_count)
261                 return;
262
263         const shared_ptr<device::DevInst> dev_inst = get_selected_device();
264         if (!dev_inst)
265                 return;
266
267         assert(!_updating_sample_count);
268         _updating_sample_count = true;
269
270         if (_sample_count_supported)
271         {
272                 uint64_t sample_count = _sample_count.value();
273                 uint64_t min_sample_count = 0;
274                 uint64_t max_sample_count = MaxSampleCount;
275
276                 if (sample_count == 0)
277                         sample_count = DefaultSampleCount;
278
279                 if ((gvar = dev_inst->list_config(NULL, SR_CONF_LIMIT_SAMPLES)))
280                 {
281                         g_variant_get(gvar, "(tt)",
282                                 &min_sample_count, &max_sample_count);
283                         g_variant_unref(gvar);
284                 }
285
286                 min_sample_count = min(max(min_sample_count, MinSampleCount),
287                         max_sample_count);
288
289                 _sample_count.show_125_list(
290                         min_sample_count, max_sample_count);
291
292                 if ((gvar = dev_inst->get_config(NULL, SR_CONF_LIMIT_SAMPLES)))
293                 {
294                         sample_count = g_variant_get_uint64(gvar);
295                         if (sample_count == 0)
296                                 sample_count = DefaultSampleCount;
297                         sample_count = min(max(sample_count, MinSampleCount),
298                                 max_sample_count);
299
300                         g_variant_unref(gvar);
301                 }
302
303                 _sample_count.set_value(sample_count);
304         }
305         else
306                 _sample_count.show_none();
307
308         _updating_sample_count = false;
309 }
310
311 void SamplingBar::update_device_config_widgets()
312 {
313         GVariant *gvar;
314
315         using namespace pv::popups;
316
317         const shared_ptr<device::DevInst> dev_inst = get_selected_device();
318         if (!dev_inst)
319                 return;
320
321         // Update the configure popup
322         DeviceOptions *const opts = new DeviceOptions(dev_inst, this);
323         _configure_button_action->setVisible(
324                 !opts->binding().properties().empty());
325         _configure_button.set_popup(opts);
326
327         // Update the probes popup
328         Probes *const probes = new Probes(_session, this);
329         _probes_button.set_popup(probes);
330
331         // Update supported options.
332         _sample_count_supported = false;
333
334         if ((gvar = dev_inst->list_config(NULL, SR_CONF_DEVICE_OPTIONS)))
335         {
336                 gsize num_opts;
337                 const int *const options =
338                         (const int32_t *)g_variant_get_fixed_array(
339                                 gvar, &num_opts, sizeof(int32_t));
340                 for (unsigned int i = 0; i < num_opts; i++)
341                 {
342                         switch (options[i]) {
343                         case SR_CONF_LIMIT_SAMPLES:
344                                 _sample_count_supported = true;
345                                 break;
346                         case SR_CONF_LIMIT_FRAMES:
347                                 dev_inst->set_config(NULL, SR_CONF_LIMIT_FRAMES,
348                                         g_variant_new_uint64(1));
349                                 break;
350                         }
351                 }
352         }
353
354         // Add notification of reconfigure events
355         disconnect(this, SLOT(on_config_changed()));
356         connect(dev_inst.get(), SIGNAL(config_changed()),
357                 this, SLOT(on_config_changed()));
358
359         // Update sweep timing widgets.
360         update_sample_count_selector();
361         update_sample_rate_selector();
362 }
363
364 void SamplingBar::commit_sample_count()
365 {
366         uint64_t sample_count = 0;
367
368         if (_updating_sample_count)
369                 return;
370
371         const shared_ptr<device::DevInst> dev_inst = get_selected_device();
372         if (!dev_inst)
373                 return;
374
375         sample_count = _sample_count.value();
376
377         // Set the sample count
378         assert(!_updating_sample_count);
379         _updating_sample_count = true;
380         if (_sample_count_supported &&
381                 !dev_inst->set_config(NULL, SR_CONF_LIMIT_SAMPLES,
382                 g_variant_new_uint64(sample_count))) {
383                 qDebug() << "Failed to configure sample count.";
384                 return;
385         }
386         _updating_sample_count = false;
387 }
388
389 void SamplingBar::commit_sample_rate()
390 {
391         uint64_t sample_rate = 0;
392
393         if (_updating_sample_rate)
394                 return;
395
396         const shared_ptr<device::DevInst> dev_inst = get_selected_device();
397         if (!dev_inst)
398                 return;
399
400         sample_rate = _sample_rate.value();
401         if (sample_rate == 0)
402                 return;
403
404         // Set the samplerate
405         assert(!_updating_sample_rate);
406         _updating_sample_rate = true;
407         if (!dev_inst->set_config(NULL, SR_CONF_SAMPLERATE,
408                 g_variant_new_uint64(sample_rate))) {
409                 qDebug() << "Failed to configure samplerate.";
410                 return;
411         }
412         _updating_sample_rate = false;
413 }
414
415 void SamplingBar::on_device_selected()
416 {
417         if (_updating_device_selector)
418                 return;
419
420         const shared_ptr<device::DevInst> dev_inst = get_selected_device();
421         if (!dev_inst)
422                 return;
423
424         _session.set_device(dev_inst);
425
426         update_device_config_widgets();
427 }
428
429 void SamplingBar::on_sample_count_changed()
430 {
431         commit_sample_count();
432 }
433
434 void SamplingBar::on_sample_rate_changed()
435 {
436         commit_sample_rate();
437 }
438
439 void SamplingBar::on_run_stop()
440 {
441         commit_sample_count();
442         commit_sample_rate();   
443         run_stop();
444 }
445
446 void SamplingBar::on_config_changed()
447 {
448         commit_sample_count();
449         update_sample_count_selector(); 
450         commit_sample_rate();   
451         update_sample_rate_selector();
452 }
453
454 bool SamplingBar::eventFilter(QObject *watched, QEvent *event)
455 {
456         if ((watched == &_sample_count || watched == &_sample_rate) &&
457                 (event->type() == QEvent::ToolTip)) {
458                 double sec = (double)_sample_count.value() / _sample_rate.value();
459
460                 QString str;
461                 QTextStream(&str)
462                         << tr("Total sampling time: ")
463                         << fixed
464                         << qSetRealNumberPrecision(1)
465                         << sec
466                         << "s";
467
468                 QHelpEvent *help_event = static_cast<QHelpEvent*>(event);
469                 QToolTip::showText(help_event->globalPos(), str);
470
471                 return true;
472         }
473
474         return false;
475 }
476
477 } // namespace toolbars
478 } // namespace pv