]> sigrok.org Git - pulseview.git/blob - pv/toolbars/samplingbar.cpp
bafb9cd406bf05f9c76b54f2d91156e34d593e93
[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
37 using namespace std;
38
39 namespace pv {
40 namespace toolbars {
41
42 const uint64_t SamplingBar::RecordLengths[20] = {
43         1000,
44         2500,
45         5000,
46         10000,
47         25000,
48         50000,
49         100000,
50         250000,
51         500000,
52         1000000,
53         2000000,
54         5000000,
55         10000000,
56         25000000,
57         50000000,
58         100000000,
59         250000000,
60         500000000,
61         1000000000,
62         10000000000ULL,
63 };
64
65 const uint64_t SamplingBar::DefaultRecordLength = 1000000;
66
67 SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
68         QToolBar("Sampling Bar", parent),
69         _session(session),
70         _device_selector(this),
71         _updating_device_selector(false),
72         _configure_button(this),
73         _probes_button(this),
74         _record_length_selector(this),
75         _sample_rate_list(this),
76         _icon_red(":/icons/status-red.svg"),
77         _icon_green(":/icons/status-green.svg"),
78         _icon_grey(":/icons/status-grey.svg"),
79         _run_stop_button(this)
80 {
81         connect(&_run_stop_button, SIGNAL(clicked()),
82                 this, SLOT(on_run_stop()));
83         connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
84                 this, SLOT(on_device_selected()));
85
86         _sample_rate_value.setDecimals(0);
87         _sample_rate_value.setSuffix("Hz");
88
89         for (size_t i = 0; i < countof(RecordLengths); i++)
90         {
91                 const uint64_t &l = RecordLengths[i];
92                 char *const text = sr_si_string_u64(l, " samples");
93                 _record_length_selector.addItem(QString(text),
94                         qVariantFromValue(l));
95                 g_free(text);
96
97                 if (l == DefaultRecordLength)
98                         _record_length_selector.setCurrentIndex(i);
99         }
100
101         set_capture_state(pv::SigSession::Stopped);
102
103         _configure_button.setIcon(QIcon::fromTheme("configure",
104                 QIcon(":/icons/configure.png")));
105         _probes_button.setIcon(QIcon::fromTheme("probes",
106                 QIcon(":/icons/probes.svg")));
107
108         _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
109
110         addWidget(&_device_selector);
111         addWidget(&_configure_button);
112         addWidget(&_probes_button);
113         addWidget(&_record_length_selector);
114         _sample_rate_list_action = addWidget(&_sample_rate_list);
115         _sample_rate_value_action = addWidget(&_sample_rate_value);
116         addWidget(&_run_stop_button);
117
118         connect(&_sample_rate_list, SIGNAL(currentIndexChanged(int)),
119                 this, SLOT(on_sample_rate_changed()));
120         connect(&_sample_rate_value, SIGNAL(editingFinished()),
121                 this, SLOT(on_sample_rate_changed()));
122 }
123
124 void SamplingBar::set_device_list(
125         const std::list<struct sr_dev_inst*> &devices)
126 {
127         _updating_device_selector = true;
128
129         _device_selector.clear();
130
131         BOOST_FOREACH (sr_dev_inst *sdi, devices) {
132                 const string title = DeviceManager::format_device_title(sdi);
133                 _device_selector.addItem(title.c_str(),
134                         qVariantFromValue((void*)sdi));
135         }
136
137         _updating_device_selector = false;
138
139         update_sample_rate_selector();
140 }
141
142 struct sr_dev_inst* SamplingBar::get_selected_device() const
143 {
144         const int index = _device_selector.currentIndex();
145         if (index < 0)
146                 return NULL;
147
148         return (sr_dev_inst*)_device_selector.itemData(
149                 index).value<void*>();
150 }
151
152 void SamplingBar::set_selected_device(struct sr_dev_inst *const sdi)
153 {
154         for (int i = 0; i < _device_selector.count(); i++)
155                 if (sdi == _device_selector.itemData(i).value<void*>()) {
156                         _device_selector.setCurrentIndex(i);
157                         return;
158                 }
159 }
160
161 uint64_t SamplingBar::get_record_length() const
162 {
163         const int index = _record_length_selector.currentIndex();
164         if (index < 0)
165                 return 0;
166
167         return _record_length_selector.itemData(index).value<uint64_t>();
168 }
169
170 void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
171 {
172         const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
173         _run_stop_button.setIcon(*icons[state]);
174         _run_stop_button.setText((state == pv::SigSession::Stopped) ?
175                 tr("Run") : tr("Stop"));
176 }
177
178 void SamplingBar::update_sample_rate_selector()
179 {
180         const sr_dev_inst *const sdi = get_selected_device();
181         GVariant *gvar_dict, *gvar_list;
182         const uint64_t *elements = NULL;
183         gsize num_elements;
184         QAction *selector_action = NULL;
185
186         assert(_sample_rate_value_action);
187         assert(_sample_rate_list_action);
188
189         if (!sdi)
190                 return;
191
192         if (sr_config_list(sdi->driver, SR_CONF_SAMPLERATE,
193                         &gvar_dict, sdi) != SR_OK)
194                 return;
195
196         _sample_rate_list_action->setVisible(false);
197         _sample_rate_value_action->setVisible(false);
198
199         if ((gvar_list = g_variant_lookup_value(gvar_dict,
200                         "samplerate-steps", G_VARIANT_TYPE("at")))) {
201                 elements = (const uint64_t *)g_variant_get_fixed_array(
202                                 gvar_list, &num_elements, sizeof(uint64_t));
203                 _sample_rate_value.setRange(elements[0], elements[1]);
204                 _sample_rate_value.setSingleStep(elements[2]);
205                 g_variant_unref(gvar_list);
206
207                 selector_action = _sample_rate_value_action;
208         }
209         else if ((gvar_list = g_variant_lookup_value(gvar_dict,
210                         "samplerates", G_VARIANT_TYPE("at"))))
211         {
212                 elements = (const uint64_t *)g_variant_get_fixed_array(
213                                 gvar_list, &num_elements, sizeof(uint64_t));
214                 _sample_rate_list.clear();
215
216                 for (unsigned int i = 0; i < num_elements; i++)
217                 {
218                         char *const s = sr_samplerate_string(elements[i]);
219                         _sample_rate_list.addItem(QString(s),
220                                 qVariantFromValue(elements[i]));
221                         g_free(s);
222                 }
223
224                 _sample_rate_list.show();
225                 g_variant_unref(gvar_list);
226
227                 selector_action = _sample_rate_list_action;
228         }
229
230         g_variant_unref(gvar_dict);
231         update_sample_rate_selector_value();
232
233         // We delay showing the action, so that value change events
234         // are ignored.
235         if (selector_action)
236                 selector_action->setVisible(true);
237 }
238
239 void SamplingBar::update_sample_rate_selector_value()
240 {
241         sr_dev_inst *const sdi = get_selected_device();
242         GVariant *gvar;
243         uint64_t samplerate;
244
245         assert(sdi);
246
247         if (sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
248                 &gvar, sdi) != SR_OK) {
249                 qDebug() <<
250                                 "WARNING: Failed to get value of sample rate";
251                 return;
252         }
253         samplerate = g_variant_get_uint64(gvar);
254         g_variant_unref(gvar);
255
256         assert(_sample_rate_value_action);
257         assert(_sample_rate_list_action);
258
259         if (_sample_rate_value_action->isVisible())
260                 _sample_rate_value.setValue(samplerate);
261         else if (_sample_rate_list_action->isVisible())
262         {
263                 for (int i = 0; i < _sample_rate_list.count(); i++)
264                         if (samplerate == _sample_rate_list.itemData(
265                                 i).value<uint64_t>())
266                                 _sample_rate_list.setCurrentIndex(i);
267         }
268 }
269
270 void SamplingBar::commit_sample_rate()
271 {
272         uint64_t sample_rate = 0;
273
274         sr_dev_inst *const sdi = get_selected_device();
275         assert(sdi);
276
277         assert(_sample_rate_value_action);
278         assert(_sample_rate_list_action);
279
280         if (_sample_rate_value_action->isVisible())
281                 sample_rate = (uint64_t)_sample_rate_value.value();
282         else if (_sample_rate_list_action->isVisible())
283         {
284                 const int index = _sample_rate_list.currentIndex();
285                 if (index >= 0)
286                         sample_rate = _sample_rate_list.itemData(
287                                 index).value<uint64_t>();
288         }
289
290         if (sample_rate == 0)
291                 return;
292
293         // Set the samplerate
294         if (sr_config_set(sdi, SR_CONF_SAMPLERATE,
295                 g_variant_new_uint64(sample_rate)) != SR_OK) {
296                 qDebug() << "Failed to configure samplerate.";
297                 return;
298         }
299 }
300
301 void SamplingBar::on_device_selected()
302 {
303         using namespace pv::popups;
304
305         if (_updating_device_selector)
306                 return;
307
308         update_sample_rate_selector();
309
310         sr_dev_inst *const sdi = get_selected_device();
311         _session.set_device(sdi);
312
313         _configure_button.set_popup(new DeviceOptions(sdi, this));
314         _probes_button.set_popup(new Probes(_session, this));
315 }
316
317 void SamplingBar::on_sample_rate_changed()
318 {
319         commit_sample_rate();
320 }
321
322 void SamplingBar::on_run_stop()
323 {
324         commit_sample_rate();   
325         run_stop();
326 }
327
328 } // namespace toolbars
329 } // namespace pv