]> sigrok.org Git - pulseview.git/blob - pv/toolbars/samplingbar.cpp
35de916b6ef76465535f9852c65a4dc88437960c
[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/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::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::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         on_device_selected();
120 }
121
122 shared_ptr<pv::DevInst> SamplingBar::get_selected_device() const
123 {
124         const int index = _device_selector.currentIndex();
125         if (index < 0)
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);
132
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);
139 }
140
141 void SamplingBar::set_selected_device(boost::shared_ptr<pv::DevInst> dev_inst)
142 {
143         assert(dev_inst);
144
145         for (int i = 0; i < _device_selector.count(); i++)
146                 if (dev_inst->dev_inst() ==
147                         _device_selector.itemData(i).value<void*>()) {
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         const shared_ptr<DevInst> dev_inst = get_selected_device();
168         if (!dev_inst)
169                 return;
170
171         _updating_sample_rate = true;
172
173         if (!(gvar_dict = dev_inst->list_config(NULL, SR_CONF_SAMPLERATE)))
174         {
175                 _sample_rate.show_none();
176                 _updating_sample_rate = false;
177                 return;
178         }
179
180         if ((gvar_list = g_variant_lookup_value(gvar_dict,
181                         "samplerate-steps", G_VARIANT_TYPE("at"))))
182         {
183                 elements = (const uint64_t *)g_variant_get_fixed_array(
184                                 gvar_list, &num_elements, sizeof(uint64_t));
185
186                 const uint64_t min = elements[0];
187                 const uint64_t max = elements[1];
188                 const uint64_t step = elements[2];
189
190                 g_variant_unref(gvar_list);
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                 }
207         }
208         else if ((gvar_list = g_variant_lookup_value(gvar_dict,
209                         "samplerates", G_VARIANT_TYPE("at"))))
210         {
211                 elements = (const uint64_t *)g_variant_get_fixed_array(
212                                 gvar_list, &num_elements, sizeof(uint64_t));
213                 _sample_rate.show_list(elements, num_elements);
214                 g_variant_unref(gvar_list);
215         }
216         _updating_sample_rate = false;
217
218         g_variant_unref(gvar_dict);
219         update_sample_rate_selector_value();
220 }
221
222 void SamplingBar::update_sample_rate_selector_value()
223 {
224         GVariant *gvar;
225         uint64_t samplerate;
226
227         const shared_ptr<DevInst> dev_inst = get_selected_device();
228         if (!dev_inst)
229                 return;
230
231         if (!(gvar = dev_inst->get_config(NULL, SR_CONF_SAMPLERATE))) {
232                 qDebug() << "WARNING: Failed to get value of sample rate";
233                 return;
234         }
235         samplerate = g_variant_get_uint64(gvar);
236         g_variant_unref(gvar);
237
238         _updating_sample_rate = true;
239         _sample_rate.set_value(samplerate);
240         _updating_sample_rate = false;
241 }
242
243 void SamplingBar::update_sample_count_selector()
244 {
245         GVariant *gvar;
246
247         const shared_ptr<DevInst> dev_inst = get_selected_device();
248         if (!dev_inst)
249                 return;
250
251         _updating_sample_count = true;
252
253         if (_sample_count_supported)
254         {
255                 uint64_t sample_count = DefaultSampleCount;
256                 uint64_t min_sample_count = 0;
257                 uint64_t max_sample_count = MaxSampleCount;
258
259                 if ((gvar = dev_inst->list_config(NULL, SR_CONF_LIMIT_SAMPLES)))
260                 {
261                         g_variant_get(gvar, "(tt)",
262                                 &min_sample_count, &max_sample_count);
263                         g_variant_unref(gvar);
264                 }
265
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);
271
272                 if ((gvar = dev_inst->get_config(NULL, SR_CONF_LIMIT_SAMPLES)))
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);
284         }
285         else
286                 _sample_count.show_none();
287
288         _updating_sample_count = false;
289 }
290
291 void SamplingBar::commit_sample_count()
292 {
293         uint64_t sample_count = 0;
294
295         const shared_ptr<DevInst> dev_inst = get_selected_device();
296         if (!dev_inst)
297                 return;
298
299         sample_count = _sample_count.value();
300
301         // Set the sample count
302         if (!dev_inst->set_config(NULL, SR_CONF_LIMIT_SAMPLES,
303                 g_variant_new_uint64(sample_count))) {
304                 qDebug() << "Failed to configure sample count.";
305                 return;
306         }
307 }
308
309 void SamplingBar::commit_sample_rate()
310 {
311         uint64_t sample_rate = 0;
312
313         const shared_ptr<DevInst> dev_inst = get_selected_device();
314         if (!dev_inst)
315                 return;
316
317         sample_rate = _sample_rate.value();
318         if (sample_rate == 0)
319                 return;
320
321         // Set the samplerate
322         if (!dev_inst->set_config(NULL, SR_CONF_SAMPLERATE,
323                 g_variant_new_uint64(sample_rate))) {
324                 qDebug() << "Failed to configure samplerate.";
325                 return;
326         }
327 }
328
329 void SamplingBar::on_device_selected()
330 {
331         GVariant *gvar;
332
333         using namespace pv::popups;
334
335         if (_updating_device_selector)
336                 return;
337
338         const shared_ptr<DevInst> dev_inst = get_selected_device();
339         if (!dev_inst)
340                 return;
341
342         _session.set_device(dev_inst);
343
344         // Update the configure popup
345         DeviceOptions *const opts = new DeviceOptions(dev_inst, this);
346         _configure_button_action->setVisible(
347                 !opts->binding().properties().empty());
348         _configure_button.set_popup(opts);
349
350         // Update the probes popup
351         Probes *const probes = new Probes(_session, this);
352         _probes_button.set_popup(probes);
353
354         // Update supported options.
355         _sample_count_supported = false;
356
357         if ((gvar = dev_inst->list_config(NULL, SR_CONF_DEVICE_OPTIONS)))
358         {
359                 gsize num_opts;
360                 const int *const options =
361                         (const int32_t *)g_variant_get_fixed_array(
362                                 gvar, &num_opts, sizeof(int32_t));
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:
370                                 dev_inst->set_config(NULL, SR_CONF_LIMIT_FRAMES,
371                                         g_variant_new_uint64(1));
372                                 break;
373                         }
374                 }
375         }
376
377         // Update sweep timing widgets.
378         update_sample_count_selector();
379         update_sample_rate_selector();
380 }
381
382 void SamplingBar::on_sample_count_changed()
383 {
384         if(!_updating_sample_count)
385                 commit_sample_count();
386 }
387
388 void SamplingBar::on_sample_rate_changed()
389 {
390         if (!_updating_sample_rate)
391                 commit_sample_rate();
392 }
393
394 void SamplingBar::on_run_stop()
395 {
396         commit_sample_count();
397         commit_sample_rate();   
398         run_stop();
399 }
400
401 } // namespace toolbars
402 } // namespace pv