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