]> sigrok.org Git - pulseview.git/blame - pv/toolbars/samplingbar.cpp
Show a 1-2-5 list for the sample count widget
[pulseview.git] / pv / toolbars / samplingbar.cpp
CommitLineData
d4984fe7 1/*
b3f22de0 2 * This file is part of the PulseView project.
d4984fe7
JH
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
09f5d123 21#include <extdef.h>
dde1a563 22
09f5d123 23#include <assert.h>
215f9499 24
18203d86
JH
25#include <boost/foreach.hpp>
26
6fb67b27 27#include <libsigrok/libsigrok.h>
6fb67b27 28
dde1a563 29#include <QAction>
48888313 30#include <QDebug>
dde1a563 31
d4984fe7
JH
32#include "samplingbar.h"
33
dd63af74 34#include <pv/devicemanager.h>
51d4a9ab 35#include <pv/popups/deviceoptions.h>
488f068c 36#include <pv/popups/probes.h>
cdb50f67 37
b50ef520
JH
38using std::max;
39using std::min;
819f4c25 40using std::string;
dd63af74 41
51e77110 42namespace pv {
f4c92e1c 43namespace toolbars {
51e77110 44
b50ef520
JH
45const uint64_t SamplingBar::MinSampleCount = 100ULL;
46const uint64_t SamplingBar::MaxSampleCount = 1000000000000ULL;
47const uint64_t SamplingBar::DefaultSampleCount = 1000000;
09f5d123 48
aca00b1e 49SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
6fb67b27 50 QToolBar("Sampling Bar", parent),
aca00b1e 51 _session(session),
dde1a563 52 _device_selector(this),
95237c18 53 _updating_device_selector(false),
cdb50f67 54 _configure_button(this),
b3e8a5d8 55 _configure_button_action(NULL),
51d4a9ab 56 _probes_button(this),
124d97de 57 _sample_count(" samples", this),
1198b887
JH
58 _sample_rate("Hz", this),
59 _updating_sample_rate(false),
85756012 60 _updating_sample_count(false),
6546a6a7 61 _sample_count_supported(false),
2b49eeb0 62 _icon_red(":/icons/status-red.svg"),
f5798068
JH
63 _icon_green(":/icons/status-green.svg"),
64 _icon_grey(":/icons/status-grey.svg"),
274d4f13 65 _run_stop_button(this)
6fb67b27 66{
cdb50f67 67 connect(&_run_stop_button, SIGNAL(clicked()),
9f3d12f3 68 this, SLOT(on_run_stop()));
dde1a563
JH
69 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
70 this, SLOT(on_device_selected()));
124d97de
ML
71 connect(&_sample_count, SIGNAL(value_changed()),
72 this, SLOT(on_sample_count_changed()));
1198b887
JH
73 connect(&_sample_rate, SIGNAL(value_changed()),
74 this, SLOT(on_sample_rate_changed()));
dde1a563 75
124d97de 76 _sample_count.show_min_max_step(0, UINT64_MAX, 1);
215f9499 77
2b49eeb0 78 set_capture_state(pv::SigSession::Stopped);
274d4f13 79
688ef645
JH
80 _configure_button.setIcon(QIcon::fromTheme("configure",
81 QIcon(":/icons/configure.png")));
b7b659aa 82
51d4a9ab
JH
83 _probes_button.setIcon(QIcon::fromTheme("probes",
84 QIcon(":/icons/probes.svg")));
cdb50f67 85
f5798068
JH
86 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
87
6fb67b27 88 addWidget(&_device_selector);
b3e8a5d8 89 _configure_button_action = addWidget(&_configure_button);
51d4a9ab 90 addWidget(&_probes_button);
124d97de 91 addWidget(&_sample_count);
1198b887 92 addWidget(&_sample_rate);
6fb67b27 93
1198b887 94 addWidget(&_run_stop_button);
6fb67b27
JH
95}
96
18203d86
JH
97void SamplingBar::set_device_list(
98 const std::list<struct sr_dev_inst*> &devices)
99{
95237c18
JH
100 _updating_device_selector = true;
101
18203d86
JH
102 _device_selector.clear();
103
104 BOOST_FOREACH (sr_dev_inst *sdi, devices) {
dd63af74
JH
105 const string title = DeviceManager::format_device_title(sdi);
106 _device_selector.addItem(title.c_str(),
107 qVariantFromValue((void*)sdi));
18203d86
JH
108 }
109
95237c18
JH
110 _updating_device_selector = false;
111
b7b659aa 112 on_device_selected();
18203d86
JH
113}
114
6fb67b27 115struct sr_dev_inst* SamplingBar::get_selected_device() const
d4984fe7 116{
6fb67b27 117 const int index = _device_selector.currentIndex();
333d5bbc 118 if (index < 0)
6fb67b27
JH
119 return NULL;
120
121 return (sr_dev_inst*)_device_selector.itemData(
122 index).value<void*>();
123}
124
dba73e73
JH
125void SamplingBar::set_selected_device(struct sr_dev_inst *const sdi)
126{
793f8096
UH
127 for (int i = 0; i < _device_selector.count(); i++)
128 if (sdi == _device_selector.itemData(i).value<void*>()) {
dba73e73
JH
129 _device_selector.setCurrentIndex(i);
130 return;
131 }
132}
133
2b49eeb0 134void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
6ac96c2e 135{
2b49eeb0
JH
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"));
6ac96c2e
JH
140}
141
dde1a563
JH
142void SamplingBar::update_sample_rate_selector()
143{
144 const sr_dev_inst *const sdi = get_selected_device();
488f5d3f
BV
145 GVariant *gvar_dict, *gvar_list;
146 const uint64_t *elements = NULL;
147 gsize num_elements;
dde1a563 148
ef4d0201
JH
149 if (!sdi)
150 return;
151
1198b887
JH
152 _updating_sample_rate = true;
153
68162c29
BV
154 if (sr_config_list(sdi->driver, sdi, NULL,
155 SR_CONF_SAMPLERATE, &gvar_dict) != SR_OK)
1198b887
JH
156 {
157 _sample_rate.show_none();
158 _updating_sample_rate = false;
dde1a563 159 return;
1198b887 160 }
dde1a563 161
488f5d3f 162 if ((gvar_list = g_variant_lookup_value(gvar_dict,
1198b887
JH
163 "samplerate-steps", G_VARIANT_TYPE("at"))))
164 {
488f5d3f
BV
165 elements = (const uint64_t *)g_variant_get_fixed_array(
166 gvar_list, &num_elements, sizeof(uint64_t));
aafe53af
JH
167
168 const uint64_t min = elements[0];
169 const uint64_t max = elements[1];
170 const uint64_t step = elements[2];
171
488f5d3f 172 g_variant_unref(gvar_list);
aafe53af
JH
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 }
dde1a563 189 }
488f5d3f
BV
190 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
191 "samplerates", G_VARIANT_TYPE("at"))))
dde1a563 192 {
488f5d3f
BV
193 elements = (const uint64_t *)g_variant_get_fixed_array(
194 gvar_list, &num_elements, sizeof(uint64_t));
1198b887 195 _sample_rate.show_list(elements, num_elements);
488f5d3f 196 g_variant_unref(gvar_list);
dde1a563 197 }
1198b887 198 _updating_sample_rate = false;
48888313 199
488f5d3f 200 g_variant_unref(gvar_dict);
48888313
JH
201 update_sample_rate_selector_value();
202}
203
204void SamplingBar::update_sample_rate_selector_value()
205{
206 sr_dev_inst *const sdi = get_selected_device();
488f5d3f
BV
207 GVariant *gvar;
208 uint64_t samplerate;
209
48888313
JH
210 assert(sdi);
211
68162c29
BV
212 if (sr_config_get(sdi->driver, sdi, NULL,
213 SR_CONF_SAMPLERATE, &gvar) != SR_OK) {
eb4008a6
JH
214 qDebug() <<
215 "WARNING: Failed to get value of sample rate";
216 return;
217 }
488f5d3f
BV
218 samplerate = g_variant_get_uint64(gvar);
219 g_variant_unref(gvar);
48888313 220
1198b887
JH
221 _updating_sample_rate = true;
222 _sample_rate.set_value(samplerate);
223 _updating_sample_rate = false;
48888313
JH
224}
225
85756012
ML
226void SamplingBar::update_sample_count_selector()
227{
228 sr_dev_inst *const sdi = get_selected_device();
229 GVariant *gvar;
85756012
ML
230
231 assert(sdi);
232
b50ef520 233 _updating_sample_count = true;
85756012 234
b50ef520 235 if (_sample_count_supported)
df3c1aa3 236 {
b50ef520
JH
237 uint64_t sample_count = DefaultSampleCount;
238 uint64_t max_sample_count = MaxSampleCount;
239
240 if (sr_config_get(sdi->driver, sdi, NULL,
241 SR_CONF_MAX_UNCOMPRESSED_SAMPLES, &gvar) == SR_OK) {
242 max_sample_count = g_variant_get_uint64(gvar);
243 g_variant_unref(gvar);
244 }
245
246 _sample_count.show_125_list(MinSampleCount, max_sample_count);
247
248 if (sr_config_get(sdi->driver, sdi, NULL,
249 SR_CONF_LIMIT_SAMPLES, &gvar) == SR_OK)
250 {
251 sample_count = g_variant_get_uint64(gvar);
252 if (sample_count == 0)
253 sample_count = DefaultSampleCount;
254 sample_count = min(max(sample_count, MinSampleCount),
255 max_sample_count);
256
257 g_variant_unref(gvar);
258 }
259
260 _sample_count.set_value(sample_count);
85756012 261 }
b50ef520
JH
262 else
263 _sample_count.show_none();
df3c1aa3 264
df3c1aa3 265 _updating_sample_count = false;
85756012
ML
266}
267
124d97de
ML
268void SamplingBar::commit_sample_count()
269{
270 uint64_t sample_count = 0;
271
272 sr_dev_inst *const sdi = get_selected_device();
273 assert(sdi);
274
275 sample_count = _sample_count.value();
276
277 // Set the sample count
278 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES,
279 g_variant_new_uint64(sample_count)) != SR_OK) {
280 qDebug() << "Failed to configure sample count.";
281 return;
282 }
283}
284
48888313
JH
285void SamplingBar::commit_sample_rate()
286{
287 uint64_t sample_rate = 0;
288
289 sr_dev_inst *const sdi = get_selected_device();
290 assert(sdi);
291
1198b887 292 sample_rate = _sample_rate.value();
f9541bde
JH
293 if (sample_rate == 0)
294 return;
295
48888313 296 // Set the samplerate
68162c29 297 if (sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE,
488f5d3f 298 g_variant_new_uint64(sample_rate)) != SR_OK) {
48888313
JH
299 qDebug() << "Failed to configure samplerate.";
300 return;
301 }
dde1a563
JH
302}
303
304void SamplingBar::on_device_selected()
305{
14cbfcc8
ML
306 GVariant *gvar;
307
488f068c 308 using namespace pv::popups;
51d4a9ab 309
95237c18
JH
310 if (_updating_device_selector)
311 return;
312
51d4a9ab 313 sr_dev_inst *const sdi = get_selected_device();
aca00b1e 314 _session.set_device(sdi);
51d4a9ab 315
b3e8a5d8
JH
316 // Update the configure popup
317 DeviceOptions *const opts = new DeviceOptions(sdi, this);
318 _configure_button_action->setVisible(
319 !opts->binding().properties().empty());
320 _configure_button.set_popup(opts);
488f068c
JH
321
322 // Update the probes popup
323 Probes *const probes = new Probes(_session, this);
324 _probes_button.set_popup(probes);
df539680 325
14cbfcc8
ML
326 // Update supported options.
327 _sample_count_supported = false;
328
329 if (sr_config_list(sdi->driver, sdi, NULL,
330 SR_CONF_DEVICE_OPTIONS, &gvar) == SR_OK)
331 {
332 gsize num_opts;
333 const int *const options = (const int32_t *)g_variant_get_fixed_array(
334 gvar, &num_opts, sizeof(int32_t));
335 for (unsigned int i = 0; i < num_opts; i++)
336 {
337 switch (options[i]) {
338 case SR_CONF_LIMIT_SAMPLES:
339 _sample_count_supported = true;
340 break;
341 case SR_CONF_LIMIT_FRAMES:
342 sr_config_set(sdi, NULL, SR_CONF_LIMIT_FRAMES,
343 g_variant_new_uint64(1));
344 break;
345 }
346 }
347 }
348
df539680
ML
349 // Update sweep timing widgets.
350 update_sample_count_selector();
351 update_sample_rate_selector();
dde1a563 352}
51e77110 353
124d97de
ML
354void SamplingBar::on_sample_count_changed()
355{
85756012
ML
356 if(!_updating_sample_count)
357 commit_sample_count();
124d97de
ML
358}
359
48888313
JH
360void SamplingBar::on_sample_rate_changed()
361{
1198b887
JH
362 if (!_updating_sample_rate)
363 commit_sample_rate();
48888313
JH
364}
365
9f3d12f3
JH
366void SamplingBar::on_run_stop()
367{
b50ef520 368 commit_sample_count();
9f3d12f3
JH
369 commit_sample_rate();
370 run_stop();
371}
372
f4c92e1c 373} // namespace toolbars
51e77110 374} // namespace pv