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