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