]> sigrok.org Git - pulseview.git/blame - pv/toolbars/samplingbar.cpp
decode: Do not overflow the chunk buffer
[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),
32c81aef 77 _sample_rate_action(NULL),
274d4f13 78 _sample_rate_list(this),
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()));
88
89 _sample_rate_value.setDecimals(0);
90 _sample_rate_value.setSuffix("Hz");
91
9ba4ca35 92 for (size_t i = 0; i < countof(RecordLengths); i++)
215f9499 93 {
09f5d123 94 const uint64_t &l = RecordLengths[i];
215f9499 95 char *const text = sr_si_string_u64(l, " samples");
27e8df22 96 _record_length_selector.addItem(QString::fromUtf8(text),
215f9499
JH
97 qVariantFromValue(l));
98 g_free(text);
09f5d123 99
9ba4ca35 100 if (l == DefaultRecordLength)
09f5d123 101 _record_length_selector.setCurrentIndex(i);
215f9499
JH
102 }
103
2b49eeb0 104 set_capture_state(pv::SigSession::Stopped);
274d4f13 105
688ef645
JH
106 _configure_button.setIcon(QIcon::fromTheme("configure",
107 QIcon(":/icons/configure.png")));
b7b659aa 108
51d4a9ab
JH
109 _probes_button.setIcon(QIcon::fromTheme("probes",
110 QIcon(":/icons/probes.svg")));
cdb50f67 111
f5798068
JH
112 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
113
6fb67b27 114 addWidget(&_device_selector);
b3e8a5d8 115 _configure_button_action = addWidget(&_configure_button);
51d4a9ab 116 addWidget(&_probes_button);
215f9499 117 addWidget(&_record_length_selector);
dde1a563
JH
118 _sample_rate_list_action = addWidget(&_sample_rate_list);
119 _sample_rate_value_action = addWidget(&_sample_rate_value);
274d4f13 120 addWidget(&_run_stop_button);
6fb67b27 121
48888313
JH
122 connect(&_sample_rate_list, SIGNAL(currentIndexChanged(int)),
123 this, SLOT(on_sample_rate_changed()));
18203d86 124 connect(&_sample_rate_value, SIGNAL(editingFinished()),
48888313 125 this, SLOT(on_sample_rate_changed()));
6fb67b27
JH
126}
127
18203d86
JH
128void SamplingBar::set_device_list(
129 const std::list<struct sr_dev_inst*> &devices)
130{
95237c18
JH
131 _updating_device_selector = true;
132
18203d86
JH
133 _device_selector.clear();
134
135 BOOST_FOREACH (sr_dev_inst *sdi, devices) {
dd63af74
JH
136 const string title = DeviceManager::format_device_title(sdi);
137 _device_selector.addItem(title.c_str(),
138 qVariantFromValue((void*)sdi));
18203d86
JH
139 }
140
95237c18
JH
141 _updating_device_selector = false;
142
b7b659aa 143 on_device_selected();
18203d86
JH
144}
145
6fb67b27 146struct sr_dev_inst* SamplingBar::get_selected_device() const
d4984fe7 147{
6fb67b27 148 const int index = _device_selector.currentIndex();
333d5bbc 149 if (index < 0)
6fb67b27
JH
150 return NULL;
151
152 return (sr_dev_inst*)_device_selector.itemData(
153 index).value<void*>();
154}
155
dba73e73
JH
156void SamplingBar::set_selected_device(struct sr_dev_inst *const sdi)
157{
793f8096
UH
158 for (int i = 0; i < _device_selector.count(); i++)
159 if (sdi == _device_selector.itemData(i).value<void*>()) {
dba73e73
JH
160 _device_selector.setCurrentIndex(i);
161 return;
162 }
163}
164
215f9499
JH
165uint64_t SamplingBar::get_record_length() const
166{
167 const int index = _record_length_selector.currentIndex();
333d5bbc 168 if (index < 0)
215f9499
JH
169 return 0;
170
171 return _record_length_selector.itemData(index).value<uint64_t>();
172}
173
2b49eeb0 174void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
6ac96c2e 175{
2b49eeb0
JH
176 const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
177 _run_stop_button.setIcon(*icons[state]);
178 _run_stop_button.setText((state == pv::SigSession::Stopped) ?
179 tr("Run") : tr("Stop"));
6ac96c2e
JH
180}
181
dde1a563
JH
182void SamplingBar::update_sample_rate_selector()
183{
184 const sr_dev_inst *const sdi = get_selected_device();
488f5d3f
BV
185 GVariant *gvar_dict, *gvar_list;
186 const uint64_t *elements = NULL;
187 gsize num_elements;
dde1a563
JH
188
189 assert(_sample_rate_value_action);
190 assert(_sample_rate_list_action);
191
ef4d0201
JH
192 if (!sdi)
193 return;
194
68162c29
BV
195 if (sr_config_list(sdi->driver, sdi, NULL,
196 SR_CONF_SAMPLERATE, &gvar_dict) != SR_OK)
dde1a563
JH
197 return;
198
199 _sample_rate_list_action->setVisible(false);
200 _sample_rate_value_action->setVisible(false);
201
488f5d3f
BV
202 if ((gvar_list = g_variant_lookup_value(gvar_dict,
203 "samplerate-steps", G_VARIANT_TYPE("at")))) {
204 elements = (const uint64_t *)g_variant_get_fixed_array(
205 gvar_list, &num_elements, sizeof(uint64_t));
206 _sample_rate_value.setRange(elements[0], elements[1]);
207 _sample_rate_value.setSingleStep(elements[2]);
488f5d3f 208 g_variant_unref(gvar_list);
f9541bde 209
32c81aef 210 _sample_rate_action = _sample_rate_value_action;
dde1a563 211 }
488f5d3f
BV
212 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
213 "samplerates", G_VARIANT_TYPE("at"))))
dde1a563 214 {
488f5d3f
BV
215 elements = (const uint64_t *)g_variant_get_fixed_array(
216 gvar_list, &num_elements, sizeof(uint64_t));
dde1a563 217 _sample_rate_list.clear();
488f5d3f
BV
218
219 for (unsigned int i = 0; i < num_elements; i++)
220 {
221 char *const s = sr_samplerate_string(elements[i]);
27e8df22 222 _sample_rate_list.addItem(QString::fromUtf8(s),
488f5d3f
BV
223 qVariantFromValue(elements[i]));
224 g_free(s);
225 }
226
dde1a563 227 _sample_rate_list.show();
488f5d3f 228 g_variant_unref(gvar_list);
f9541bde 229
32c81aef 230 _sample_rate_action = _sample_rate_list_action;
dde1a563 231 }
48888313 232
488f5d3f 233 g_variant_unref(gvar_dict);
48888313 234 update_sample_rate_selector_value();
f9541bde
JH
235
236 // We delay showing the action, so that value change events
237 // are ignored.
32c81aef
JH
238 if (_sample_rate_action)
239 _sample_rate_action->setVisible(true);
48888313
JH
240}
241
242void SamplingBar::update_sample_rate_selector_value()
243{
244 sr_dev_inst *const sdi = get_selected_device();
488f5d3f
BV
245 GVariant *gvar;
246 uint64_t samplerate;
247
48888313
JH
248 assert(sdi);
249
68162c29
BV
250 if (sr_config_get(sdi->driver, sdi, NULL,
251 SR_CONF_SAMPLERATE, &gvar) != SR_OK) {
eb4008a6
JH
252 qDebug() <<
253 "WARNING: Failed to get value of sample rate";
254 return;
255 }
488f5d3f
BV
256 samplerate = g_variant_get_uint64(gvar);
257 g_variant_unref(gvar);
48888313
JH
258
259 assert(_sample_rate_value_action);
260 assert(_sample_rate_list_action);
261
32c81aef 262 if (_sample_rate_action == _sample_rate_value_action)
488f5d3f 263 _sample_rate_value.setValue(samplerate);
32c81aef 264 else if (_sample_rate_action == _sample_rate_list_action)
48888313 265 {
793f8096 266 for (int i = 0; i < _sample_rate_list.count(); i++)
488f5d3f 267 if (samplerate == _sample_rate_list.itemData(
48888313
JH
268 i).value<uint64_t>())
269 _sample_rate_list.setCurrentIndex(i);
270 }
271}
272
273void SamplingBar::commit_sample_rate()
274{
275 uint64_t sample_rate = 0;
276
277 sr_dev_inst *const sdi = get_selected_device();
278 assert(sdi);
279
280 assert(_sample_rate_value_action);
281 assert(_sample_rate_list_action);
282
32c81aef 283 if (_sample_rate_action == _sample_rate_value_action)
48888313 284 sample_rate = (uint64_t)_sample_rate_value.value();
32c81aef 285 else if (_sample_rate_action == _sample_rate_list_action)
48888313
JH
286 {
287 const int index = _sample_rate_list.currentIndex();
288 if (index >= 0)
289 sample_rate = _sample_rate_list.itemData(
290 index).value<uint64_t>();
291 }
292
f9541bde
JH
293 if (sample_rate == 0)
294 return;
295
48888313 296 // Set the samplerate
68162c29 297 if (sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE,
488f5d3f 298 g_variant_new_uint64(sample_rate)) != SR_OK) {
48888313
JH
299 qDebug() << "Failed to configure samplerate.";
300 return;
301 }
dde1a563
JH
302}
303
304void SamplingBar::on_device_selected()
305{
488f068c 306 using namespace pv::popups;
51d4a9ab 307
95237c18
JH
308 if (_updating_device_selector)
309 return;
310
dde1a563 311 update_sample_rate_selector();
51d4a9ab
JH
312
313 sr_dev_inst *const sdi = get_selected_device();
aca00b1e 314 _session.set_device(sdi);
51d4a9ab 315
b3e8a5d8
JH
316 // Update the configure popup
317 DeviceOptions *const opts = new DeviceOptions(sdi, this);
318 _configure_button_action->setVisible(
319 !opts->binding().properties().empty());
320 _configure_button.set_popup(opts);
488f068c
JH
321
322 // Update the probes popup
323 Probes *const probes = new Probes(_session, this);
324 _probes_button.set_popup(probes);
dde1a563 325}
51e77110 326
48888313
JH
327void SamplingBar::on_sample_rate_changed()
328{
329 commit_sample_rate();
330}
331
9f3d12f3
JH
332void SamplingBar::on_run_stop()
333{
334 commit_sample_rate();
335 run_stop();
336}
337
f4c92e1c 338} // namespace toolbars
51e77110 339} // namespace pv