]> sigrok.org Git - pulseview.git/blame - pv/toolbars/samplingbar.cpp
Show sample count selector only if setting supported.
[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),
2b49eeb0 57 _icon_red(":/icons/status-red.svg"),
f5798068
JH
58 _icon_green(":/icons/status-green.svg"),
59 _icon_grey(":/icons/status-grey.svg"),
274d4f13 60 _run_stop_button(this)
6fb67b27 61{
cdb50f67 62 connect(&_run_stop_button, SIGNAL(clicked()),
9f3d12f3 63 this, SLOT(on_run_stop()));
dde1a563
JH
64 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
65 this, SLOT(on_device_selected()));
124d97de
ML
66 connect(&_sample_count, SIGNAL(value_changed()),
67 this, SLOT(on_sample_count_changed()));
1198b887
JH
68 connect(&_sample_rate, SIGNAL(value_changed()),
69 this, SLOT(on_sample_rate_changed()));
dde1a563 70
124d97de 71 _sample_count.show_min_max_step(0, UINT64_MAX, 1);
215f9499 72
2b49eeb0 73 set_capture_state(pv::SigSession::Stopped);
274d4f13 74
688ef645
JH
75 _configure_button.setIcon(QIcon::fromTheme("configure",
76 QIcon(":/icons/configure.png")));
b7b659aa 77
51d4a9ab
JH
78 _probes_button.setIcon(QIcon::fromTheme("probes",
79 QIcon(":/icons/probes.svg")));
cdb50f67 80
f5798068
JH
81 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
82
6fb67b27 83 addWidget(&_device_selector);
b3e8a5d8 84 _configure_button_action = addWidget(&_configure_button);
51d4a9ab 85 addWidget(&_probes_button);
124d97de 86 addWidget(&_sample_count);
1198b887 87 addWidget(&_sample_rate);
6fb67b27 88
1198b887 89 addWidget(&_run_stop_button);
6fb67b27
JH
90}
91
18203d86
JH
92void SamplingBar::set_device_list(
93 const std::list<struct sr_dev_inst*> &devices)
94{
95237c18
JH
95 _updating_device_selector = true;
96
18203d86
JH
97 _device_selector.clear();
98
99 BOOST_FOREACH (sr_dev_inst *sdi, devices) {
dd63af74
JH
100 const string title = DeviceManager::format_device_title(sdi);
101 _device_selector.addItem(title.c_str(),
102 qVariantFromValue((void*)sdi));
18203d86
JH
103 }
104
95237c18
JH
105 _updating_device_selector = false;
106
b7b659aa 107 on_device_selected();
18203d86
JH
108}
109
6fb67b27 110struct sr_dev_inst* SamplingBar::get_selected_device() const
d4984fe7 111{
6fb67b27 112 const int index = _device_selector.currentIndex();
333d5bbc 113 if (index < 0)
6fb67b27
JH
114 return NULL;
115
116 return (sr_dev_inst*)_device_selector.itemData(
117 index).value<void*>();
118}
119
dba73e73
JH
120void SamplingBar::set_selected_device(struct sr_dev_inst *const sdi)
121{
793f8096
UH
122 for (int i = 0; i < _device_selector.count(); i++)
123 if (sdi == _device_selector.itemData(i).value<void*>()) {
dba73e73
JH
124 _device_selector.setCurrentIndex(i);
125 return;
126 }
127}
128
215f9499
JH
129uint64_t SamplingBar::get_record_length() const
130{
124d97de 131 return _sample_count.value();
215f9499
JH
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));
1198b887
JH
167 _sample_rate.show_min_max_step(elements[0], elements[1],
168 elements[2]);
488f5d3f 169 g_variant_unref(gvar_list);
dde1a563 170 }
488f5d3f
BV
171 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
172 "samplerates", G_VARIANT_TYPE("at"))))
dde1a563 173 {
488f5d3f
BV
174 elements = (const uint64_t *)g_variant_get_fixed_array(
175 gvar_list, &num_elements, sizeof(uint64_t));
1198b887 176 _sample_rate.show_list(elements, num_elements);
488f5d3f 177 g_variant_unref(gvar_list);
dde1a563 178 }
1198b887 179 _updating_sample_rate = false;
48888313 180
488f5d3f 181 g_variant_unref(gvar_dict);
48888313
JH
182 update_sample_rate_selector_value();
183}
184
185void SamplingBar::update_sample_rate_selector_value()
186{
187 sr_dev_inst *const sdi = get_selected_device();
488f5d3f
BV
188 GVariant *gvar;
189 uint64_t samplerate;
190
48888313
JH
191 assert(sdi);
192
68162c29
BV
193 if (sr_config_get(sdi->driver, sdi, NULL,
194 SR_CONF_SAMPLERATE, &gvar) != SR_OK) {
eb4008a6
JH
195 qDebug() <<
196 "WARNING: Failed to get value of sample rate";
197 return;
198 }
488f5d3f
BV
199 samplerate = g_variant_get_uint64(gvar);
200 g_variant_unref(gvar);
48888313 201
1198b887
JH
202 _updating_sample_rate = true;
203 _sample_rate.set_value(samplerate);
204 _updating_sample_rate = false;
48888313
JH
205}
206
85756012
ML
207void SamplingBar::update_sample_count_selector()
208{
209 sr_dev_inst *const sdi = get_selected_device();
210 GVariant *gvar;
211 uint64_t samplecount;
212
213 assert(sdi);
214
215 if (sr_config_get(sdi->driver, sdi, NULL,
216 SR_CONF_LIMIT_SAMPLES, &gvar) != SR_OK)
217 {
218 _sample_count.show_none();
219 }
220 else
221 {
222 _sample_count.show_min_max_step(0, UINT64_MAX, 1);
223
224 samplecount = g_variant_get_uint64(gvar);
225 g_variant_unref(gvar);
226
227 _updating_sample_count = true;
228 _sample_count.set_value(samplecount);
229 _updating_sample_count = false;
230 }
231}
232
124d97de
ML
233void SamplingBar::commit_sample_count()
234{
235 uint64_t sample_count = 0;
236
237 sr_dev_inst *const sdi = get_selected_device();
238 assert(sdi);
239
240 sample_count = _sample_count.value();
241
242 // Set the sample count
243 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES,
244 g_variant_new_uint64(sample_count)) != SR_OK) {
245 qDebug() << "Failed to configure sample count.";
246 return;
247 }
248}
249
48888313
JH
250void SamplingBar::commit_sample_rate()
251{
252 uint64_t sample_rate = 0;
253
254 sr_dev_inst *const sdi = get_selected_device();
255 assert(sdi);
256
1198b887 257 sample_rate = _sample_rate.value();
f9541bde
JH
258 if (sample_rate == 0)
259 return;
260
48888313 261 // Set the samplerate
68162c29 262 if (sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE,
488f5d3f 263 g_variant_new_uint64(sample_rate)) != SR_OK) {
48888313
JH
264 qDebug() << "Failed to configure samplerate.";
265 return;
266 }
dde1a563
JH
267}
268
269void SamplingBar::on_device_selected()
270{
488f068c 271 using namespace pv::popups;
51d4a9ab 272
95237c18
JH
273 if (_updating_device_selector)
274 return;
275
85756012 276 update_sample_count_selector();
dde1a563 277 update_sample_rate_selector();
51d4a9ab 278
85756012
ML
279 if (_sample_count.value() == 0)
280 _sample_count.set_value(DefaultRecordLength);
281
51d4a9ab 282 sr_dev_inst *const sdi = get_selected_device();
aca00b1e 283 _session.set_device(sdi);
51d4a9ab 284
b3e8a5d8
JH
285 // Update the configure popup
286 DeviceOptions *const opts = new DeviceOptions(sdi, this);
287 _configure_button_action->setVisible(
288 !opts->binding().properties().empty());
289 _configure_button.set_popup(opts);
488f068c
JH
290
291 // Update the probes popup
292 Probes *const probes = new Probes(_session, this);
293 _probes_button.set_popup(probes);
dde1a563 294}
51e77110 295
124d97de
ML
296void SamplingBar::on_sample_count_changed()
297{
85756012
ML
298 if(!_updating_sample_count)
299 commit_sample_count();
124d97de
ML
300}
301
48888313
JH
302void SamplingBar::on_sample_rate_changed()
303{
1198b887
JH
304 if (!_updating_sample_rate)
305 commit_sample_rate();
48888313
JH
306}
307
9f3d12f3
JH
308void SamplingBar::on_run_stop()
309{
310 commit_sample_rate();
311 run_stop();
312}
313
f4c92e1c 314} // namespace toolbars
51e77110 315} // namespace pv