]> sigrok.org Git - pulseview.git/blob - pv/toolbars/samplingbar.cpp
Commit sample count after setting default.
[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         _icon_red(":/icons/status-red.svg"),
58         _icon_green(":/icons/status-green.svg"),
59         _icon_grey(":/icons/status-grey.svg"),
60         _run_stop_button(this)
61 {
62         connect(&_run_stop_button, SIGNAL(clicked()),
63                 this, SLOT(on_run_stop()));
64         connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
65                 this, SLOT(on_device_selected()));
66         connect(&_sample_count, SIGNAL(value_changed()),
67                 this, SLOT(on_sample_count_changed()));
68         connect(&_sample_rate, SIGNAL(value_changed()),
69                 this, SLOT(on_sample_rate_changed()));
70
71         _sample_count.show_min_max_step(0, UINT64_MAX, 1);
72
73         set_capture_state(pv::SigSession::Stopped);
74
75         _configure_button.setIcon(QIcon::fromTheme("configure",
76                 QIcon(":/icons/configure.png")));
77
78         _probes_button.setIcon(QIcon::fromTheme("probes",
79                 QIcon(":/icons/probes.svg")));
80
81         _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
82
83         addWidget(&_device_selector);
84         _configure_button_action = addWidget(&_configure_button);
85         addWidget(&_probes_button);
86         addWidget(&_sample_count);
87         addWidget(&_sample_rate);
88
89         addWidget(&_run_stop_button);
90 }
91
92 void SamplingBar::set_device_list(
93         const std::list<struct sr_dev_inst*> &devices)
94 {
95         _updating_device_selector = true;
96
97         _device_selector.clear();
98
99         BOOST_FOREACH (sr_dev_inst *sdi, devices) {
100                 const string title = DeviceManager::format_device_title(sdi);
101                 _device_selector.addItem(title.c_str(),
102                         qVariantFromValue((void*)sdi));
103         }
104
105         _updating_device_selector = false;
106
107         on_device_selected();
108 }
109
110 struct sr_dev_inst* SamplingBar::get_selected_device() const
111 {
112         const int index = _device_selector.currentIndex();
113         if (index < 0)
114                 return NULL;
115
116         return (sr_dev_inst*)_device_selector.itemData(
117                 index).value<void*>();
118 }
119
120 void SamplingBar::set_selected_device(struct sr_dev_inst *const sdi)
121 {
122         for (int i = 0; i < _device_selector.count(); i++)
123                 if (sdi == _device_selector.itemData(i).value<void*>()) {
124                         _device_selector.setCurrentIndex(i);
125                         return;
126                 }
127 }
128
129 void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
130 {
131         const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
132         _run_stop_button.setIcon(*icons[state]);
133         _run_stop_button.setText((state == pv::SigSession::Stopped) ?
134                 tr("Run") : tr("Stop"));
135 }
136
137 void SamplingBar::update_sample_rate_selector()
138 {
139         const sr_dev_inst *const sdi = get_selected_device();
140         GVariant *gvar_dict, *gvar_list;
141         const uint64_t *elements = NULL;
142         gsize num_elements;
143
144         if (!sdi)
145                 return;
146
147         _updating_sample_rate = true;
148
149         if (sr_config_list(sdi->driver, sdi, NULL,
150                         SR_CONF_SAMPLERATE, &gvar_dict) != SR_OK)
151         {
152                 _sample_rate.show_none();
153                 _updating_sample_rate = false;
154                 return;
155         }
156
157         if ((gvar_list = g_variant_lookup_value(gvar_dict,
158                         "samplerate-steps", G_VARIANT_TYPE("at"))))
159         {
160                 elements = (const uint64_t *)g_variant_get_fixed_array(
161                                 gvar_list, &num_elements, sizeof(uint64_t));
162                 _sample_rate.show_min_max_step(elements[0], elements[1],
163                         elements[2]);
164                 g_variant_unref(gvar_list);
165         }
166         else if ((gvar_list = g_variant_lookup_value(gvar_dict,
167                         "samplerates", G_VARIANT_TYPE("at"))))
168         {
169                 elements = (const uint64_t *)g_variant_get_fixed_array(
170                                 gvar_list, &num_elements, sizeof(uint64_t));
171                 _sample_rate.show_list(elements, num_elements);
172                 g_variant_unref(gvar_list);
173         }
174         _updating_sample_rate = false;
175
176         g_variant_unref(gvar_dict);
177         update_sample_rate_selector_value();
178 }
179
180 void SamplingBar::update_sample_rate_selector_value()
181 {
182         sr_dev_inst *const sdi = get_selected_device();
183         GVariant *gvar;
184         uint64_t samplerate;
185
186         assert(sdi);
187
188         if (sr_config_get(sdi->driver, sdi, NULL,
189                 SR_CONF_SAMPLERATE, &gvar) != SR_OK) {
190                 qDebug() <<
191                                 "WARNING: Failed to get value of sample rate";
192                 return;
193         }
194         samplerate = g_variant_get_uint64(gvar);
195         g_variant_unref(gvar);
196
197         _updating_sample_rate = true;
198         _sample_rate.set_value(samplerate);
199         _updating_sample_rate = false;
200 }
201
202 void SamplingBar::update_sample_count_selector()
203 {
204         sr_dev_inst *const sdi = get_selected_device();
205         GVariant *gvar;
206         uint64_t samplecount;
207
208         assert(sdi);
209
210         if (sr_config_get(sdi->driver, sdi, NULL,
211                 SR_CONF_LIMIT_SAMPLES, &gvar) != SR_OK)
212         {
213                 _sample_count.show_none();
214         }
215         else
216         {
217                 _sample_count.show_min_max_step(0, UINT64_MAX, 1);
218
219                 samplecount = g_variant_get_uint64(gvar);
220                 g_variant_unref(gvar);
221
222                 _updating_sample_count = true;
223                 _sample_count.set_value(samplecount);
224                 _updating_sample_count = false;
225         }
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         using namespace pv::popups;
267
268         if (_updating_device_selector)
269                 return;
270
271         sr_dev_inst *const sdi = get_selected_device();
272         _session.set_device(sdi);
273
274         // Update the configure popup
275         DeviceOptions *const opts = new DeviceOptions(sdi, this);
276         _configure_button_action->setVisible(
277                 !opts->binding().properties().empty());
278         _configure_button.set_popup(opts);
279
280         // Update the probes popup
281         Probes *const probes = new Probes(_session, this);
282         _probes_button.set_popup(probes);
283
284         // Update sweep timing widgets.
285         update_sample_count_selector();
286         update_sample_rate_selector();
287
288         if (_sample_count.value() == 0) {
289                 _sample_count.set_value(DefaultRecordLength);
290                 commit_sample_count();
291         }
292 }
293
294 void SamplingBar::on_sample_count_changed()
295 {
296         if(!_updating_sample_count)
297                 commit_sample_count();
298 }
299
300 void SamplingBar::on_sample_rate_changed()
301 {
302         if (!_updating_sample_rate)
303                 commit_sample_rate();
304 }
305
306 void SamplingBar::on_run_stop()
307 {
308         commit_sample_rate();   
309         run_stop();
310 }
311
312 } // namespace toolbars
313 } // namespace pv