]> sigrok.org Git - pulseview.git/blob - pv/toolbars/samplingbar.cpp
Ported to new SR_CONF_LIMIT_SAMPLES config key
[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 <libsigrok/libsigrok.h>
28
29 #include <QAction>
30 #include <QDebug>
31
32 #include "samplingbar.h"
33
34 #include <pv/devicemanager.h>
35 #include <pv/popups/deviceoptions.h>
36 #include <pv/popups/probes.h>
37
38 using std::max;
39 using std::min;
40 using std::string;
41
42 namespace pv {
43 namespace toolbars {
44
45 const uint64_t SamplingBar::MinSampleCount = 100ULL;
46 const uint64_t SamplingBar::MaxSampleCount = 1000000000000ULL;
47 const uint64_t SamplingBar::DefaultSampleCount = 1000000;
48
49 SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
50         QToolBar("Sampling Bar", parent),
51         _session(session),
52         _device_selector(this),
53         _updating_device_selector(false),
54         _configure_button(this),
55         _configure_button_action(NULL),
56         _probes_button(this),
57         _sample_count(" samples", this),
58         _sample_rate("Hz", this),
59         _updating_sample_rate(false),
60         _updating_sample_count(false),
61         _sample_count_supported(false),
62         _icon_red(":/icons/status-red.svg"),
63         _icon_green(":/icons/status-green.svg"),
64         _icon_grey(":/icons/status-grey.svg"),
65         _run_stop_button(this)
66 {
67         connect(&_run_stop_button, SIGNAL(clicked()),
68                 this, SLOT(on_run_stop()));
69         connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
70                 this, SLOT(on_device_selected()));
71         connect(&_sample_count, SIGNAL(value_changed()),
72                 this, SLOT(on_sample_count_changed()));
73         connect(&_sample_rate, SIGNAL(value_changed()),
74                 this, SLOT(on_sample_rate_changed()));
75
76         _sample_count.show_min_max_step(0, UINT64_MAX, 1);
77
78         set_capture_state(pv::SigSession::Stopped);
79
80         _configure_button.setIcon(QIcon::fromTheme("configure",
81                 QIcon(":/icons/configure.png")));
82
83         _probes_button.setIcon(QIcon::fromTheme("probes",
84                 QIcon(":/icons/probes.svg")));
85
86         _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
87
88         addWidget(&_device_selector);
89         _configure_button_action = addWidget(&_configure_button);
90         addWidget(&_probes_button);
91         addWidget(&_sample_count);
92         addWidget(&_sample_rate);
93
94         addWidget(&_run_stop_button);
95 }
96
97 void SamplingBar::set_device_list(
98         const std::list<struct sr_dev_inst*> &devices)
99 {
100         _updating_device_selector = true;
101
102         _device_selector.clear();
103
104         BOOST_FOREACH (sr_dev_inst *sdi, devices) {
105                 const string title = DeviceManager::format_device_title(sdi);
106                 _device_selector.addItem(title.c_str(),
107                         qVariantFromValue((void*)sdi));
108         }
109
110         _updating_device_selector = false;
111
112         on_device_selected();
113 }
114
115 struct sr_dev_inst* SamplingBar::get_selected_device() const
116 {
117         const int index = _device_selector.currentIndex();
118         if (index < 0)
119                 return NULL;
120
121         return (sr_dev_inst*)_device_selector.itemData(
122                 index).value<void*>();
123 }
124
125 void SamplingBar::set_selected_device(struct sr_dev_inst *const sdi)
126 {
127         for (int i = 0; i < _device_selector.count(); i++)
128                 if (sdi == _device_selector.itemData(i).value<void*>()) {
129                         _device_selector.setCurrentIndex(i);
130                         return;
131                 }
132 }
133
134 void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
135 {
136         const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
137         _run_stop_button.setIcon(*icons[state]);
138         _run_stop_button.setText((state == pv::SigSession::Stopped) ?
139                 tr("Run") : tr("Stop"));
140 }
141
142 void SamplingBar::update_sample_rate_selector()
143 {
144         const sr_dev_inst *const sdi = get_selected_device();
145         GVariant *gvar_dict, *gvar_list;
146         const uint64_t *elements = NULL;
147         gsize num_elements;
148
149         if (!sdi)
150                 return;
151
152         _updating_sample_rate = true;
153
154         if (sr_config_list(sdi->driver, sdi, NULL,
155                         SR_CONF_SAMPLERATE, &gvar_dict) != SR_OK)
156         {
157                 _sample_rate.show_none();
158                 _updating_sample_rate = false;
159                 return;
160         }
161
162         if ((gvar_list = g_variant_lookup_value(gvar_dict,
163                         "samplerate-steps", G_VARIANT_TYPE("at"))))
164         {
165                 elements = (const uint64_t *)g_variant_get_fixed_array(
166                                 gvar_list, &num_elements, sizeof(uint64_t));
167
168                 const uint64_t min = elements[0];
169                 const uint64_t max = elements[1];
170                 const uint64_t step = elements[2];
171
172                 g_variant_unref(gvar_list);
173
174                 assert(min > 0);
175                 assert(max > 0);
176                 assert(max > min);
177                 assert(step > 0);
178
179                 if (step == 1)
180                         _sample_rate.show_125_list(min, max);
181                 else
182                 {
183                         // When the step is not 1, we cam't make a 1-2-5-10
184                         // list of sample rates, because we may not be able to
185                         // make round numbers. Therefore in this case, show a
186                         // spin box.
187                         _sample_rate.show_min_max_step(min, max, step);
188                 }
189         }
190         else if ((gvar_list = g_variant_lookup_value(gvar_dict,
191                         "samplerates", G_VARIANT_TYPE("at"))))
192         {
193                 elements = (const uint64_t *)g_variant_get_fixed_array(
194                                 gvar_list, &num_elements, sizeof(uint64_t));
195                 _sample_rate.show_list(elements, num_elements);
196                 g_variant_unref(gvar_list);
197         }
198         _updating_sample_rate = false;
199
200         g_variant_unref(gvar_dict);
201         update_sample_rate_selector_value();
202 }
203
204 void SamplingBar::update_sample_rate_selector_value()
205 {
206         sr_dev_inst *const sdi = get_selected_device();
207         GVariant *gvar;
208         uint64_t samplerate;
209
210         assert(sdi);
211
212         if (sr_config_get(sdi->driver, sdi, NULL,
213                 SR_CONF_SAMPLERATE, &gvar) != SR_OK) {
214                 qDebug() <<
215                                 "WARNING: Failed to get value of sample rate";
216                 return;
217         }
218         samplerate = g_variant_get_uint64(gvar);
219         g_variant_unref(gvar);
220
221         _updating_sample_rate = true;
222         _sample_rate.set_value(samplerate);
223         _updating_sample_rate = false;
224 }
225
226 void SamplingBar::update_sample_count_selector()
227 {
228         sr_dev_inst *const sdi = get_selected_device();
229         GVariant *gvar;
230
231         assert(sdi);
232
233         _updating_sample_count = true;
234
235         if (_sample_count_supported)
236         {
237                 uint64_t sample_count = DefaultSampleCount;
238                 uint64_t min_sample_count = 0;
239                 uint64_t max_sample_count = MaxSampleCount;
240
241                 if (sr_config_list(sdi->driver, sdi, NULL,
242                         SR_CONF_LIMIT_SAMPLES, &gvar) == SR_OK) {
243                         g_variant_get(gvar, "(tt)",
244                                 &min_sample_count, &max_sample_count);
245                         g_variant_unref(gvar);
246                 }
247
248                 min_sample_count = min(max(min_sample_count, MinSampleCount),
249                         max_sample_count);
250
251                 _sample_count.show_125_list(
252                         min_sample_count, max_sample_count);
253
254                 if (sr_config_get(sdi->driver, sdi, NULL,
255                         SR_CONF_LIMIT_SAMPLES, &gvar) == SR_OK)
256                 {
257                         sample_count = g_variant_get_uint64(gvar);
258                         if (sample_count == 0)
259                                 sample_count = DefaultSampleCount;
260                         sample_count = min(max(sample_count, MinSampleCount),
261                                 max_sample_count);
262
263                         g_variant_unref(gvar);
264                 }
265
266                 _sample_count.set_value(sample_count);
267         }
268         else
269                 _sample_count.show_none();
270
271         _updating_sample_count = false;
272 }
273
274 void SamplingBar::commit_sample_count()
275 {
276         uint64_t sample_count = 0;
277
278         sr_dev_inst *const sdi = get_selected_device();
279         assert(sdi);
280
281         sample_count = _sample_count.value();
282
283         // Set the sample count
284         if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES,
285                 g_variant_new_uint64(sample_count)) != SR_OK) {
286                 qDebug() << "Failed to configure sample count.";
287                 return;
288         }
289 }
290
291 void SamplingBar::commit_sample_rate()
292 {
293         uint64_t sample_rate = 0;
294
295         sr_dev_inst *const sdi = get_selected_device();
296         assert(sdi);
297
298         sample_rate = _sample_rate.value();
299         if (sample_rate == 0)
300                 return;
301
302         // Set the samplerate
303         if (sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE,
304                 g_variant_new_uint64(sample_rate)) != SR_OK) {
305                 qDebug() << "Failed to configure samplerate.";
306                 return;
307         }
308 }
309
310 void SamplingBar::on_device_selected()
311 {
312         GVariant *gvar;
313
314         using namespace pv::popups;
315
316         if (_updating_device_selector)
317                 return;
318
319         sr_dev_inst *const sdi = get_selected_device();
320         _session.set_device(sdi);
321
322         // Update the configure popup
323         DeviceOptions *const opts = new DeviceOptions(sdi, 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 (sr_config_list(sdi->driver, sdi, NULL,
336                 SR_CONF_DEVICE_OPTIONS, &gvar) == SR_OK)
337         {
338                 gsize num_opts;
339                 const int *const options = (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                                 sr_config_set(sdi, NULL, SR_CONF_LIMIT_FRAMES,
349                                         g_variant_new_uint64(1));
350                                 break;
351                         }
352                 }
353         }
354
355         // Update sweep timing widgets.
356         update_sample_count_selector();
357         update_sample_rate_selector();
358 }
359
360 void SamplingBar::on_sample_count_changed()
361 {
362         if(!_updating_sample_count)
363                 commit_sample_count();
364 }
365
366 void SamplingBar::on_sample_rate_changed()
367 {
368         if (!_updating_sample_rate)
369                 commit_sample_rate();
370 }
371
372 void SamplingBar::on_run_stop()
373 {
374         commit_sample_count();
375         commit_sample_rate();   
376         run_stop();
377 }
378
379 } // namespace toolbars
380 } // namespace pv