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