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