]> sigrok.org Git - pulseview.git/blame - pv/samplingbar.cpp
Only allow monotonic devices in the device list
[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
c0be28da 180 if (sr_config_list(sdi->driver, SR_CONF_SAMPLERATE,
dde1a563
JH
181 (const void **)&samplerates, sdi) != SR_OK)
182 return;
183
184 _sample_rate_list_action->setVisible(false);
185 _sample_rate_value_action->setVisible(false);
186
187 if (samplerates->step)
188 {
189 _sample_rate_value.setRange(
190 samplerates->low, samplerates->high);
191 _sample_rate_value.setSingleStep(samplerates->step);
192 _sample_rate_value_action->setVisible(true);
193 }
194 else
195 {
196 _sample_rate_list.clear();
197 for (const uint64_t *rate = samplerates->list;
198 *rate; rate++)
199 _sample_rate_list.addItem(
200 sr_samplerate_string(*rate),
201 qVariantFromValue(*rate));
202 _sample_rate_list.show();
203 _sample_rate_list_action->setVisible(true);
204 }
48888313
JH
205
206 update_sample_rate_selector_value();
207}
208
209void SamplingBar::update_sample_rate_selector_value()
210{
211 sr_dev_inst *const sdi = get_selected_device();
212 assert(sdi);
213
214 uint64_t *samplerate = NULL;
215 if(sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
216 (const void**)&samplerate, sdi) != SR_OK) {
217 qDebug() <<
218 "WARNING: Failed to get value of sample rate";
219 return;
220 }
221
222 assert(_sample_rate_value_action);
223 assert(_sample_rate_list_action);
224
225 if (_sample_rate_value_action->isVisible())
226 _sample_rate_value.setValue(*samplerate);
227 else if (_sample_rate_list_action->isVisible())
228 {
229 for(int i = 0; i < _sample_rate_list.count(); i++)
230 if(*samplerate == _sample_rate_list.itemData(
231 i).value<uint64_t>())
232 _sample_rate_list.setCurrentIndex(i);
233 }
234}
235
236void SamplingBar::commit_sample_rate()
237{
238 uint64_t sample_rate = 0;
239
240 sr_dev_inst *const sdi = get_selected_device();
241 assert(sdi);
242
243 assert(_sample_rate_value_action);
244 assert(_sample_rate_list_action);
245
246 if (_sample_rate_value_action->isVisible())
247 sample_rate = (uint64_t)_sample_rate_value.value();
248 else if (_sample_rate_list_action->isVisible())
249 {
250 const int index = _sample_rate_list.currentIndex();
251 if (index >= 0)
252 sample_rate = _sample_rate_list.itemData(
253 index).value<uint64_t>();
254 }
255
256 // Set the samplerate
257 if (sr_config_set(sdi, SR_CONF_SAMPLERATE,
258 &sample_rate) != SR_OK) {
259 qDebug() << "Failed to configure samplerate.";
260 return;
261 }
dde1a563
JH
262}
263
264void SamplingBar::on_device_selected()
265{
266 update_sample_rate_selector();
267}
51e77110 268
48888313
JH
269void SamplingBar::on_sample_rate_changed()
270{
271 commit_sample_rate();
272}
273
cdb50f67
JH
274void SamplingBar::configure()
275{
48888313
JH
276 commit_sample_rate();
277
cdb50f67
JH
278 sr_dev_inst *const sdi = get_selected_device();
279 assert(sdi);
280
d755562a 281 pv::dialogs::DeviceOptions dlg(this, sdi);
cdb50f67 282 dlg.exec();
48888313
JH
283
284 update_sample_rate_selector_value();
cdb50f67
JH
285}
286
51e77110 287} // namespace pv