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