]> sigrok.org Git - pulseview.git/blame - pv/samplingbar.cpp
Moved device enumeration out of pv::SamplingBar into pv::MainWindow
[pulseview.git] / pv / 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
JH
36namespace pv {
37
09f5d123
JH
38const uint64_t SamplingBar::RecordLengths[20] = {
39 1000,
40 2500,
41 5000,
42 10000,
43 25000,
44 50000,
45 100000,
46 250000,
47 500000,
215f9499
JH
48 1000000,
49 2000000,
50 5000000,
51 10000000,
52 25000000,
53 50000000,
54 100000000,
55 250000000,
56 500000000,
57 1000000000,
d2aad781 58 10000000000ULL,
215f9499
JH
59};
60
09f5d123
JH
61const uint64_t SamplingBar::DefaultRecordLength = 1000000;
62
d4984fe7 63SamplingBar::SamplingBar(QWidget *parent) :
6fb67b27 64 QToolBar("Sampling Bar", parent),
dde1a563 65 _device_selector(this),
cdb50f67 66 _configure_button(this),
215f9499 67 _record_length_selector(this),
274d4f13 68 _sample_rate_list(this),
f5798068
JH
69 _icon_green(":/icons/status-green.svg"),
70 _icon_grey(":/icons/status-grey.svg"),
274d4f13 71 _run_stop_button(this)
6fb67b27 72{
cdb50f67
JH
73 connect(&_run_stop_button, SIGNAL(clicked()),
74 this, SIGNAL(run_stop()));
dde1a563
JH
75 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
76 this, SLOT(on_device_selected()));
cdb50f67
JH
77 connect(&_configure_button, SIGNAL(clicked()),
78 this, SLOT(configure()));
dde1a563
JH
79
80 _sample_rate_value.setDecimals(0);
81 _sample_rate_value.setSuffix("Hz");
82
9ba4ca35 83 for (size_t i = 0; i < countof(RecordLengths); i++)
215f9499 84 {
09f5d123 85 const uint64_t &l = RecordLengths[i];
215f9499
JH
86 char *const text = sr_si_string_u64(l, " samples");
87 _record_length_selector.addItem(QString(text),
88 qVariantFromValue(l));
89 g_free(text);
09f5d123 90
9ba4ca35 91 if (l == DefaultRecordLength)
09f5d123 92 _record_length_selector.setCurrentIndex(i);
215f9499
JH
93 }
94
6ac96c2e 95 set_sampling(false);
274d4f13 96
688ef645
JH
97 _configure_button.setIcon(QIcon::fromTheme("configure",
98 QIcon(":/icons/configure.png")));
cdb50f67 99
f5798068
JH
100 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
101
6fb67b27 102 addWidget(&_device_selector);
cdb50f67 103 addWidget(&_configure_button);
215f9499 104 addWidget(&_record_length_selector);
dde1a563
JH
105 _sample_rate_list_action = addWidget(&_sample_rate_list);
106 _sample_rate_value_action = addWidget(&_sample_rate_value);
274d4f13 107 addWidget(&_run_stop_button);
6fb67b27 108
48888313
JH
109 connect(&_sample_rate_list, SIGNAL(currentIndexChanged(int)),
110 this, SLOT(on_sample_rate_changed()));
18203d86 111 connect(&_sample_rate_value, SIGNAL(editingFinished()),
48888313 112 this, SLOT(on_sample_rate_changed()));
6fb67b27
JH
113}
114
18203d86
JH
115void SamplingBar::set_device_list(
116 const std::list<struct sr_dev_inst*> &devices)
117{
118 _device_selector.clear();
119
120 BOOST_FOREACH (sr_dev_inst *sdi, devices) {
121 QString title;
122 if (sdi->vendor && sdi->vendor[0])
123 title += sdi->vendor + QString(" ");
124 if (sdi->model && sdi->model[0])
125 title += sdi->model + QString(" ");
126 if (sdi->version && sdi->version[0])
127 title += sdi->version + QString(" ");
128
129 _device_selector.addItem(title, qVariantFromValue(
130 (void*)sdi));
131 }
132
133 update_sample_rate_selector();
134}
135
6fb67b27 136struct sr_dev_inst* SamplingBar::get_selected_device() const
d4984fe7 137{
6fb67b27 138 const int index = _device_selector.currentIndex();
333d5bbc 139 if (index < 0)
6fb67b27
JH
140 return NULL;
141
142 return (sr_dev_inst*)_device_selector.itemData(
143 index).value<void*>();
144}
145
215f9499
JH
146uint64_t SamplingBar::get_record_length() const
147{
148 const int index = _record_length_selector.currentIndex();
333d5bbc 149 if (index < 0)
215f9499
JH
150 return 0;
151
152 return _record_length_selector.itemData(index).value<uint64_t>();
153}
154
6ac96c2e
JH
155void SamplingBar::set_sampling(bool sampling)
156{
f5798068 157 _run_stop_button.setIcon(sampling ? _icon_green : _icon_grey);
6ac96c2e
JH
158 _run_stop_button.setText(sampling ? "Stop" : "Run");
159}
160
dde1a563
JH
161void SamplingBar::update_sample_rate_selector()
162{
163 const sr_dev_inst *const sdi = get_selected_device();
164 const struct sr_samplerates *samplerates;
165
166 assert(_sample_rate_value_action);
167 assert(_sample_rate_list_action);
168
ef4d0201
JH
169 if (!sdi)
170 return;
171
c0be28da 172 if (sr_config_list(sdi->driver, SR_CONF_SAMPLERATE,
dde1a563
JH
173 (const void **)&samplerates, sdi) != SR_OK)
174 return;
175
176 _sample_rate_list_action->setVisible(false);
177 _sample_rate_value_action->setVisible(false);
178
179 if (samplerates->step)
180 {
181 _sample_rate_value.setRange(
182 samplerates->low, samplerates->high);
183 _sample_rate_value.setSingleStep(samplerates->step);
184 _sample_rate_value_action->setVisible(true);
185 }
186 else
187 {
188 _sample_rate_list.clear();
189 for (const uint64_t *rate = samplerates->list;
190 *rate; rate++)
191 _sample_rate_list.addItem(
192 sr_samplerate_string(*rate),
193 qVariantFromValue(*rate));
194 _sample_rate_list.show();
195 _sample_rate_list_action->setVisible(true);
196 }
48888313
JH
197
198 update_sample_rate_selector_value();
199}
200
201void SamplingBar::update_sample_rate_selector_value()
202{
203 sr_dev_inst *const sdi = get_selected_device();
204 assert(sdi);
205
eb4008a6
JH
206 uint64_t *samplerate = NULL;
207 if(sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
208 (const void**)&samplerate, sdi) != SR_OK) {
209 qDebug() <<
210 "WARNING: Failed to get value of sample rate";
211 return;
212 }
48888313
JH
213
214 assert(_sample_rate_value_action);
215 assert(_sample_rate_list_action);
216
217 if (_sample_rate_value_action->isVisible())
218 _sample_rate_value.setValue(*samplerate);
219 else if (_sample_rate_list_action->isVisible())
220 {
221 for(int i = 0; i < _sample_rate_list.count(); i++)
222 if(*samplerate == _sample_rate_list.itemData(
223 i).value<uint64_t>())
224 _sample_rate_list.setCurrentIndex(i);
225 }
226}
227
228void SamplingBar::commit_sample_rate()
229{
230 uint64_t sample_rate = 0;
231
232 sr_dev_inst *const sdi = get_selected_device();
233 assert(sdi);
234
235 assert(_sample_rate_value_action);
236 assert(_sample_rate_list_action);
237
238 if (_sample_rate_value_action->isVisible())
239 sample_rate = (uint64_t)_sample_rate_value.value();
240 else if (_sample_rate_list_action->isVisible())
241 {
242 const int index = _sample_rate_list.currentIndex();
243 if (index >= 0)
244 sample_rate = _sample_rate_list.itemData(
245 index).value<uint64_t>();
246 }
247
248 // Set the samplerate
249 if (sr_config_set(sdi, SR_CONF_SAMPLERATE,
250 &sample_rate) != SR_OK) {
251 qDebug() << "Failed to configure samplerate.";
252 return;
253 }
dde1a563
JH
254}
255
256void SamplingBar::on_device_selected()
257{
258 update_sample_rate_selector();
259}
51e77110 260
48888313
JH
261void SamplingBar::on_sample_rate_changed()
262{
263 commit_sample_rate();
264}
265
cdb50f67
JH
266void SamplingBar::configure()
267{
48888313
JH
268 commit_sample_rate();
269
cdb50f67
JH
270 sr_dev_inst *const sdi = get_selected_device();
271 assert(sdi);
272
d755562a 273 pv::dialogs::DeviceOptions dlg(this, sdi);
cdb50f67 274 dlg.exec();
48888313
JH
275
276 update_sample_rate_selector_value();
cdb50f67
JH
277}
278
51e77110 279} // namespace pv