]> sigrok.org Git - pulseview.git/blame - pv/toolbars/samplingbar.cpp
Created pv::widgets::SampleTimingWidget, and ported the SamplingBar to use it.
[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
dd63af74 34#include <pv/devicemanager.h>
51d4a9ab 35#include <pv/popups/deviceoptions.h>
488f068c 36#include <pv/popups/probes.h>
cdb50f67 37
819f4c25 38using std::string;
dd63af74 39
51e77110 40namespace pv {
f4c92e1c 41namespace toolbars {
51e77110 42
09f5d123
JH
43const uint64_t SamplingBar::RecordLengths[20] = {
44 1000,
45 2500,
46 5000,
47 10000,
48 25000,
49 50000,
50 100000,
51 250000,
52 500000,
215f9499
JH
53 1000000,
54 2000000,
55 5000000,
56 10000000,
57 25000000,
58 50000000,
59 100000000,
60 250000000,
61 500000000,
62 1000000000,
d2aad781 63 10000000000ULL,
215f9499
JH
64};
65
09f5d123
JH
66const uint64_t SamplingBar::DefaultRecordLength = 1000000;
67
aca00b1e 68SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
6fb67b27 69 QToolBar("Sampling Bar", parent),
aca00b1e 70 _session(session),
dde1a563 71 _device_selector(this),
95237c18 72 _updating_device_selector(false),
cdb50f67 73 _configure_button(this),
b3e8a5d8 74 _configure_button_action(NULL),
51d4a9ab 75 _probes_button(this),
215f9499 76 _record_length_selector(this),
1198b887
JH
77 _sample_rate("Hz", this),
78 _updating_sample_rate(false),
2b49eeb0 79 _icon_red(":/icons/status-red.svg"),
f5798068
JH
80 _icon_green(":/icons/status-green.svg"),
81 _icon_grey(":/icons/status-grey.svg"),
274d4f13 82 _run_stop_button(this)
6fb67b27 83{
cdb50f67 84 connect(&_run_stop_button, SIGNAL(clicked()),
9f3d12f3 85 this, SLOT(on_run_stop()));
dde1a563
JH
86 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
87 this, SLOT(on_device_selected()));
1198b887
JH
88 connect(&_sample_rate, SIGNAL(value_changed()),
89 this, SLOT(on_sample_rate_changed()));
dde1a563 90
9ba4ca35 91 for (size_t i = 0; i < countof(RecordLengths); i++)
215f9499 92 {
09f5d123 93 const uint64_t &l = RecordLengths[i];
215f9499 94 char *const text = sr_si_string_u64(l, " samples");
27e8df22 95 _record_length_selector.addItem(QString::fromUtf8(text),
215f9499
JH
96 qVariantFromValue(l));
97 g_free(text);
09f5d123 98
9ba4ca35 99 if (l == DefaultRecordLength)
09f5d123 100 _record_length_selector.setCurrentIndex(i);
215f9499
JH
101 }
102
2b49eeb0 103 set_capture_state(pv::SigSession::Stopped);
274d4f13 104
688ef645
JH
105 _configure_button.setIcon(QIcon::fromTheme("configure",
106 QIcon(":/icons/configure.png")));
b7b659aa 107
51d4a9ab
JH
108 _probes_button.setIcon(QIcon::fromTheme("probes",
109 QIcon(":/icons/probes.svg")));
cdb50f67 110
f5798068
JH
111 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
112
6fb67b27 113 addWidget(&_device_selector);
b3e8a5d8 114 _configure_button_action = addWidget(&_configure_button);
51d4a9ab 115 addWidget(&_probes_button);
215f9499 116 addWidget(&_record_length_selector);
1198b887 117 addWidget(&_sample_rate);
6fb67b27 118
1198b887 119 addWidget(&_run_stop_button);
6fb67b27
JH
120}
121
18203d86
JH
122void SamplingBar::set_device_list(
123 const std::list<struct sr_dev_inst*> &devices)
124{
95237c18
JH
125 _updating_device_selector = true;
126
18203d86
JH
127 _device_selector.clear();
128
129 BOOST_FOREACH (sr_dev_inst *sdi, devices) {
dd63af74
JH
130 const string title = DeviceManager::format_device_title(sdi);
131 _device_selector.addItem(title.c_str(),
132 qVariantFromValue((void*)sdi));
18203d86
JH
133 }
134
95237c18
JH
135 _updating_device_selector = false;
136
b7b659aa 137 on_device_selected();
18203d86
JH
138}
139
6fb67b27 140struct sr_dev_inst* SamplingBar::get_selected_device() const
d4984fe7 141{
6fb67b27 142 const int index = _device_selector.currentIndex();
333d5bbc 143 if (index < 0)
6fb67b27
JH
144 return NULL;
145
146 return (sr_dev_inst*)_device_selector.itemData(
147 index).value<void*>();
148}
149
dba73e73
JH
150void SamplingBar::set_selected_device(struct sr_dev_inst *const sdi)
151{
793f8096
UH
152 for (int i = 0; i < _device_selector.count(); i++)
153 if (sdi == _device_selector.itemData(i).value<void*>()) {
dba73e73
JH
154 _device_selector.setCurrentIndex(i);
155 return;
156 }
157}
158
215f9499
JH
159uint64_t SamplingBar::get_record_length() const
160{
161 const int index = _record_length_selector.currentIndex();
333d5bbc 162 if (index < 0)
215f9499
JH
163 return 0;
164
165 return _record_length_selector.itemData(index).value<uint64_t>();
166}
167
2b49eeb0 168void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
6ac96c2e 169{
2b49eeb0
JH
170 const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
171 _run_stop_button.setIcon(*icons[state]);
172 _run_stop_button.setText((state == pv::SigSession::Stopped) ?
173 tr("Run") : tr("Stop"));
6ac96c2e
JH
174}
175
dde1a563
JH
176void SamplingBar::update_sample_rate_selector()
177{
178 const sr_dev_inst *const sdi = get_selected_device();
488f5d3f
BV
179 GVariant *gvar_dict, *gvar_list;
180 const uint64_t *elements = NULL;
181 gsize num_elements;
dde1a563 182
ef4d0201
JH
183 if (!sdi)
184 return;
185
1198b887
JH
186 _updating_sample_rate = true;
187
68162c29
BV
188 if (sr_config_list(sdi->driver, sdi, NULL,
189 SR_CONF_SAMPLERATE, &gvar_dict) != SR_OK)
1198b887
JH
190 {
191 _sample_rate.show_none();
192 _updating_sample_rate = false;
dde1a563 193 return;
1198b887 194 }
dde1a563 195
488f5d3f 196 if ((gvar_list = g_variant_lookup_value(gvar_dict,
1198b887
JH
197 "samplerate-steps", G_VARIANT_TYPE("at"))))
198 {
488f5d3f
BV
199 elements = (const uint64_t *)g_variant_get_fixed_array(
200 gvar_list, &num_elements, sizeof(uint64_t));
1198b887
JH
201 _sample_rate.show_min_max_step(elements[0], elements[1],
202 elements[2]);
488f5d3f 203 g_variant_unref(gvar_list);
dde1a563 204 }
488f5d3f
BV
205 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
206 "samplerates", G_VARIANT_TYPE("at"))))
dde1a563 207 {
488f5d3f
BV
208 elements = (const uint64_t *)g_variant_get_fixed_array(
209 gvar_list, &num_elements, sizeof(uint64_t));
1198b887 210 _sample_rate.show_list(elements, num_elements);
488f5d3f 211 g_variant_unref(gvar_list);
dde1a563 212 }
1198b887 213 _updating_sample_rate = false;
48888313 214
488f5d3f 215 g_variant_unref(gvar_dict);
48888313
JH
216 update_sample_rate_selector_value();
217}
218
219void SamplingBar::update_sample_rate_selector_value()
220{
221 sr_dev_inst *const sdi = get_selected_device();
488f5d3f
BV
222 GVariant *gvar;
223 uint64_t samplerate;
224
48888313
JH
225 assert(sdi);
226
68162c29
BV
227 if (sr_config_get(sdi->driver, sdi, NULL,
228 SR_CONF_SAMPLERATE, &gvar) != SR_OK) {
eb4008a6
JH
229 qDebug() <<
230 "WARNING: Failed to get value of sample rate";
231 return;
232 }
488f5d3f
BV
233 samplerate = g_variant_get_uint64(gvar);
234 g_variant_unref(gvar);
48888313 235
1198b887
JH
236 _updating_sample_rate = true;
237 _sample_rate.set_value(samplerate);
238 _updating_sample_rate = false;
48888313
JH
239}
240
241void SamplingBar::commit_sample_rate()
242{
243 uint64_t sample_rate = 0;
244
245 sr_dev_inst *const sdi = get_selected_device();
246 assert(sdi);
247
1198b887 248 sample_rate = _sample_rate.value();
f9541bde
JH
249 if (sample_rate == 0)
250 return;
251
48888313 252 // Set the samplerate
68162c29 253 if (sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE,
488f5d3f 254 g_variant_new_uint64(sample_rate)) != SR_OK) {
48888313
JH
255 qDebug() << "Failed to configure samplerate.";
256 return;
257 }
dde1a563
JH
258}
259
260void SamplingBar::on_device_selected()
261{
488f068c 262 using namespace pv::popups;
51d4a9ab 263
95237c18
JH
264 if (_updating_device_selector)
265 return;
266
dde1a563 267 update_sample_rate_selector();
51d4a9ab
JH
268
269 sr_dev_inst *const sdi = get_selected_device();
aca00b1e 270 _session.set_device(sdi);
51d4a9ab 271
b3e8a5d8
JH
272 // Update the configure popup
273 DeviceOptions *const opts = new DeviceOptions(sdi, this);
274 _configure_button_action->setVisible(
275 !opts->binding().properties().empty());
276 _configure_button.set_popup(opts);
488f068c
JH
277
278 // Update the probes popup
279 Probes *const probes = new Probes(_session, this);
280 _probes_button.set_popup(probes);
dde1a563 281}
51e77110 282
48888313
JH
283void SamplingBar::on_sample_rate_changed()
284{
1198b887
JH
285 if (!_updating_sample_rate)
286 commit_sample_rate();
48888313
JH
287}
288
9f3d12f3
JH
289void SamplingBar::on_run_stop()
290{
291 commit_sample_rate();
292 run_stop();
293}
294
f4c92e1c 295} // namespace toolbars
51e77110 296} // namespace pv