]> sigrok.org Git - pulseview.git/blame_incremental - pv/toolbars/samplingbar.cpp
Probes popup now live applies properly
[pulseview.git] / pv / toolbars / samplingbar.cpp
... / ...
CommitLineData
1/*
2 * This file is part of the PulseView project.
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
21#include <extdef.h>
22
23#include <assert.h>
24
25#include <boost/foreach.hpp>
26
27#include <libsigrok/libsigrok.h>
28
29#include <QAction>
30#include <QDebug>
31
32#include "samplingbar.h"
33
34#include <pv/devicemanager.h>
35#include <pv/popups/deviceoptions.h>
36
37using namespace std;
38
39namespace pv {
40namespace toolbars {
41
42const uint64_t SamplingBar::RecordLengths[20] = {
43 1000,
44 2500,
45 5000,
46 10000,
47 25000,
48 50000,
49 100000,
50 250000,
51 500000,
52 1000000,
53 2000000,
54 5000000,
55 10000000,
56 25000000,
57 50000000,
58 100000000,
59 250000000,
60 500000000,
61 1000000000,
62 10000000000ULL,
63};
64
65const uint64_t SamplingBar::DefaultRecordLength = 1000000;
66
67SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
68 QToolBar("Sampling Bar", parent),
69 _session(session),
70 _device_selector(this),
71 _configure_button(this),
72 _probes_button(this),
73 _record_length_selector(this),
74 _sample_rate_list(this),
75 _icon_red(":/icons/status-red.svg"),
76 _icon_green(":/icons/status-green.svg"),
77 _icon_grey(":/icons/status-grey.svg"),
78 _run_stop_button(this)
79{
80 connect(&_run_stop_button, SIGNAL(clicked()),
81 this, SLOT(on_run_stop()));
82 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
83 this, SLOT(on_device_selected()));
84
85 _sample_rate_value.setDecimals(0);
86 _sample_rate_value.setSuffix("Hz");
87
88 for (size_t i = 0; i < countof(RecordLengths); i++)
89 {
90 const uint64_t &l = RecordLengths[i];
91 char *const text = sr_si_string_u64(l, " samples");
92 _record_length_selector.addItem(QString(text),
93 qVariantFromValue(l));
94 g_free(text);
95
96 if (l == DefaultRecordLength)
97 _record_length_selector.setCurrentIndex(i);
98 }
99
100 set_capture_state(pv::SigSession::Stopped);
101
102 _configure_button.setIcon(QIcon::fromTheme("configure",
103 QIcon(":/icons/configure.png")));
104 _probes_button.setIcon(QIcon::fromTheme("probes",
105 QIcon(":/icons/probes.svg")));
106
107 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
108
109 addWidget(&_device_selector);
110 addWidget(&_configure_button);
111 addWidget(&_probes_button);
112 addWidget(&_record_length_selector);
113 _sample_rate_list_action = addWidget(&_sample_rate_list);
114 _sample_rate_value_action = addWidget(&_sample_rate_value);
115 addWidget(&_run_stop_button);
116
117 connect(&_sample_rate_list, SIGNAL(currentIndexChanged(int)),
118 this, SLOT(on_sample_rate_changed()));
119 connect(&_sample_rate_value, SIGNAL(editingFinished()),
120 this, SLOT(on_sample_rate_changed()));
121}
122
123void SamplingBar::set_device_list(
124 const std::list<struct sr_dev_inst*> &devices)
125{
126 _device_selector.clear();
127
128 BOOST_FOREACH (sr_dev_inst *sdi, devices) {
129 const string title = DeviceManager::format_device_title(sdi);
130 _device_selector.addItem(title.c_str(),
131 qVariantFromValue((void*)sdi));
132 }
133
134 update_sample_rate_selector();
135}
136
137struct sr_dev_inst* SamplingBar::get_selected_device() const
138{
139 const int index = _device_selector.currentIndex();
140 if (index < 0)
141 return NULL;
142
143 return (sr_dev_inst*)_device_selector.itemData(
144 index).value<void*>();
145}
146
147void SamplingBar::set_selected_device(struct sr_dev_inst *const sdi)
148{
149 for (int i = 0; i < _device_selector.count(); i++)
150 if (sdi == _device_selector.itemData(i).value<void*>()) {
151 _device_selector.setCurrentIndex(i);
152 return;
153 }
154}
155
156uint64_t SamplingBar::get_record_length() const
157{
158 const int index = _record_length_selector.currentIndex();
159 if (index < 0)
160 return 0;
161
162 return _record_length_selector.itemData(index).value<uint64_t>();
163}
164
165void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
166{
167 const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
168 _run_stop_button.setIcon(*icons[state]);
169 _run_stop_button.setText((state == pv::SigSession::Stopped) ?
170 tr("Run") : tr("Stop"));
171}
172
173void SamplingBar::update_sample_rate_selector()
174{
175 const sr_dev_inst *const sdi = get_selected_device();
176 GVariant *gvar_dict, *gvar_list;
177 const uint64_t *elements = NULL;
178 gsize num_elements;
179 QAction *selector_action = NULL;
180
181 assert(_sample_rate_value_action);
182 assert(_sample_rate_list_action);
183
184 if (!sdi)
185 return;
186
187 if (sr_config_list(sdi->driver, SR_CONF_SAMPLERATE,
188 &gvar_dict, sdi) != SR_OK)
189 return;
190
191 _sample_rate_list_action->setVisible(false);
192 _sample_rate_value_action->setVisible(false);
193
194 if ((gvar_list = g_variant_lookup_value(gvar_dict,
195 "samplerate-steps", G_VARIANT_TYPE("at")))) {
196 elements = (const uint64_t *)g_variant_get_fixed_array(
197 gvar_list, &num_elements, sizeof(uint64_t));
198 _sample_rate_value.setRange(elements[0], elements[1]);
199 _sample_rate_value.setSingleStep(elements[2]);
200 g_variant_unref(gvar_list);
201
202 selector_action = _sample_rate_value_action;
203 }
204 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
205 "samplerates", G_VARIANT_TYPE("at"))))
206 {
207 elements = (const uint64_t *)g_variant_get_fixed_array(
208 gvar_list, &num_elements, sizeof(uint64_t));
209 _sample_rate_list.clear();
210
211 for (unsigned int i = 0; i < num_elements; i++)
212 {
213 char *const s = sr_samplerate_string(elements[i]);
214 _sample_rate_list.addItem(QString(s),
215 qVariantFromValue(elements[i]));
216 g_free(s);
217 }
218
219 _sample_rate_list.show();
220 g_variant_unref(gvar_list);
221
222 selector_action = _sample_rate_list_action;
223 }
224
225 g_variant_unref(gvar_dict);
226 update_sample_rate_selector_value();
227
228 // We delay showing the action, so that value change events
229 // are ignored.
230 if (selector_action)
231 selector_action->setVisible(true);
232}
233
234void SamplingBar::update_sample_rate_selector_value()
235{
236 sr_dev_inst *const sdi = get_selected_device();
237 GVariant *gvar;
238 uint64_t samplerate;
239
240 assert(sdi);
241
242 if (sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
243 &gvar, sdi) != SR_OK) {
244 qDebug() <<
245 "WARNING: Failed to get value of sample rate";
246 return;
247 }
248 samplerate = g_variant_get_uint64(gvar);
249 g_variant_unref(gvar);
250
251 assert(_sample_rate_value_action);
252 assert(_sample_rate_list_action);
253
254 if (_sample_rate_value_action->isVisible())
255 _sample_rate_value.setValue(samplerate);
256 else if (_sample_rate_list_action->isVisible())
257 {
258 for (int i = 0; i < _sample_rate_list.count(); i++)
259 if (samplerate == _sample_rate_list.itemData(
260 i).value<uint64_t>())
261 _sample_rate_list.setCurrentIndex(i);
262 }
263}
264
265void SamplingBar::commit_sample_rate()
266{
267 uint64_t sample_rate = 0;
268
269 sr_dev_inst *const sdi = get_selected_device();
270 assert(sdi);
271
272 assert(_sample_rate_value_action);
273 assert(_sample_rate_list_action);
274
275 if (_sample_rate_value_action->isVisible())
276 sample_rate = (uint64_t)_sample_rate_value.value();
277 else if (_sample_rate_list_action->isVisible())
278 {
279 const int index = _sample_rate_list.currentIndex();
280 if (index >= 0)
281 sample_rate = _sample_rate_list.itemData(
282 index).value<uint64_t>();
283 }
284
285 if (sample_rate == 0)
286 return;
287
288 // Set the samplerate
289 if (sr_config_set(sdi, SR_CONF_SAMPLERATE,
290 g_variant_new_uint64(sample_rate)) != SR_OK) {
291 qDebug() << "Failed to configure samplerate.";
292 return;
293 }
294}
295
296void SamplingBar::on_device_selected()
297{
298 using namespace pv::popups;
299
300 update_sample_rate_selector();
301
302 sr_dev_inst *const sdi = get_selected_device();
303 _session.set_device(sdi);
304
305 _configure_button.set_popup(new DeviceOptions(sdi, this));
306 _probes_button.set_popup(new Probes(_session, this));
307}
308
309void SamplingBar::on_sample_rate_changed()
310{
311 commit_sample_rate();
312}
313
314void SamplingBar::on_run_stop()
315{
316 commit_sample_rate();
317 run_stop();
318}
319
320} // namespace toolbars
321} // namespace pv