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