]> sigrok.org Git - pulseview.git/blame_incremental - pv/toolbars/samplingbar.cpp
Use a SweepTimingWidget for sample count limit.
[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#include <pv/popups/probes.h>
37
38using std::string;
39
40namespace pv {
41namespace toolbars {
42
43const uint64_t SamplingBar::DefaultRecordLength = 1000000;
44
45SamplingBar::SamplingBar(SigSession &session, QWidget *parent) :
46 QToolBar("Sampling Bar", parent),
47 _session(session),
48 _device_selector(this),
49 _updating_device_selector(false),
50 _configure_button(this),
51 _configure_button_action(NULL),
52 _probes_button(this),
53 _sample_count(" samples", this),
54 _sample_rate("Hz", this),
55 _updating_sample_rate(false),
56 _icon_red(":/icons/status-red.svg"),
57 _icon_green(":/icons/status-green.svg"),
58 _icon_grey(":/icons/status-grey.svg"),
59 _run_stop_button(this)
60{
61 connect(&_run_stop_button, SIGNAL(clicked()),
62 this, SLOT(on_run_stop()));
63 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
64 this, SLOT(on_device_selected()));
65 connect(&_sample_count, SIGNAL(value_changed()),
66 this, SLOT(on_sample_count_changed()));
67 connect(&_sample_rate, SIGNAL(value_changed()),
68 this, SLOT(on_sample_rate_changed()));
69
70 _sample_count.show_min_max_step(0, UINT64_MAX, 1);
71 _sample_count.set_value(DefaultRecordLength);
72
73 set_capture_state(pv::SigSession::Stopped);
74
75 _configure_button.setIcon(QIcon::fromTheme("configure",
76 QIcon(":/icons/configure.png")));
77
78 _probes_button.setIcon(QIcon::fromTheme("probes",
79 QIcon(":/icons/probes.svg")));
80
81 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
82
83 addWidget(&_device_selector);
84 _configure_button_action = addWidget(&_configure_button);
85 addWidget(&_probes_button);
86 addWidget(&_sample_count);
87 addWidget(&_sample_rate);
88
89 addWidget(&_run_stop_button);
90}
91
92void SamplingBar::set_device_list(
93 const std::list<struct sr_dev_inst*> &devices)
94{
95 _updating_device_selector = true;
96
97 _device_selector.clear();
98
99 BOOST_FOREACH (sr_dev_inst *sdi, devices) {
100 const string title = DeviceManager::format_device_title(sdi);
101 _device_selector.addItem(title.c_str(),
102 qVariantFromValue((void*)sdi));
103 }
104
105 _updating_device_selector = false;
106
107 on_device_selected();
108}
109
110struct sr_dev_inst* SamplingBar::get_selected_device() const
111{
112 const int index = _device_selector.currentIndex();
113 if (index < 0)
114 return NULL;
115
116 return (sr_dev_inst*)_device_selector.itemData(
117 index).value<void*>();
118}
119
120void SamplingBar::set_selected_device(struct sr_dev_inst *const sdi)
121{
122 for (int i = 0; i < _device_selector.count(); i++)
123 if (sdi == _device_selector.itemData(i).value<void*>()) {
124 _device_selector.setCurrentIndex(i);
125 return;
126 }
127}
128
129uint64_t SamplingBar::get_record_length() const
130{
131 return _sample_count.value();
132}
133
134void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
135{
136 const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
137 _run_stop_button.setIcon(*icons[state]);
138 _run_stop_button.setText((state == pv::SigSession::Stopped) ?
139 tr("Run") : tr("Stop"));
140}
141
142void SamplingBar::update_sample_rate_selector()
143{
144 const sr_dev_inst *const sdi = get_selected_device();
145 GVariant *gvar_dict, *gvar_list;
146 const uint64_t *elements = NULL;
147 gsize num_elements;
148
149 if (!sdi)
150 return;
151
152 _updating_sample_rate = true;
153
154 if (sr_config_list(sdi->driver, sdi, NULL,
155 SR_CONF_SAMPLERATE, &gvar_dict) != SR_OK)
156 {
157 _sample_rate.show_none();
158 _updating_sample_rate = false;
159 return;
160 }
161
162 if ((gvar_list = g_variant_lookup_value(gvar_dict,
163 "samplerate-steps", G_VARIANT_TYPE("at"))))
164 {
165 elements = (const uint64_t *)g_variant_get_fixed_array(
166 gvar_list, &num_elements, sizeof(uint64_t));
167 _sample_rate.show_min_max_step(elements[0], elements[1],
168 elements[2]);
169 g_variant_unref(gvar_list);
170 }
171 else if ((gvar_list = g_variant_lookup_value(gvar_dict,
172 "samplerates", G_VARIANT_TYPE("at"))))
173 {
174 elements = (const uint64_t *)g_variant_get_fixed_array(
175 gvar_list, &num_elements, sizeof(uint64_t));
176 _sample_rate.show_list(elements, num_elements);
177 g_variant_unref(gvar_list);
178 }
179 _updating_sample_rate = false;
180
181 g_variant_unref(gvar_dict);
182 update_sample_rate_selector_value();
183}
184
185void SamplingBar::update_sample_rate_selector_value()
186{
187 sr_dev_inst *const sdi = get_selected_device();
188 GVariant *gvar;
189 uint64_t samplerate;
190
191 assert(sdi);
192
193 if (sr_config_get(sdi->driver, sdi, NULL,
194 SR_CONF_SAMPLERATE, &gvar) != SR_OK) {
195 qDebug() <<
196 "WARNING: Failed to get value of sample rate";
197 return;
198 }
199 samplerate = g_variant_get_uint64(gvar);
200 g_variant_unref(gvar);
201
202 _updating_sample_rate = true;
203 _sample_rate.set_value(samplerate);
204 _updating_sample_rate = false;
205}
206
207void SamplingBar::commit_sample_count()
208{
209 uint64_t sample_count = 0;
210
211 sr_dev_inst *const sdi = get_selected_device();
212 assert(sdi);
213
214 sample_count = _sample_count.value();
215
216 // Set the sample count
217 if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES,
218 g_variant_new_uint64(sample_count)) != SR_OK) {
219 qDebug() << "Failed to configure sample count.";
220 return;
221 }
222}
223
224void SamplingBar::commit_sample_rate()
225{
226 uint64_t sample_rate = 0;
227
228 sr_dev_inst *const sdi = get_selected_device();
229 assert(sdi);
230
231 sample_rate = _sample_rate.value();
232 if (sample_rate == 0)
233 return;
234
235 // Set the samplerate
236 if (sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE,
237 g_variant_new_uint64(sample_rate)) != SR_OK) {
238 qDebug() << "Failed to configure samplerate.";
239 return;
240 }
241}
242
243void SamplingBar::on_device_selected()
244{
245 using namespace pv::popups;
246
247 if (_updating_device_selector)
248 return;
249
250 update_sample_rate_selector();
251
252 sr_dev_inst *const sdi = get_selected_device();
253 _session.set_device(sdi);
254
255 // Update the configure popup
256 DeviceOptions *const opts = new DeviceOptions(sdi, this);
257 _configure_button_action->setVisible(
258 !opts->binding().properties().empty());
259 _configure_button.set_popup(opts);
260
261 // Update the probes popup
262 Probes *const probes = new Probes(_session, this);
263 _probes_button.set_popup(probes);
264}
265
266void SamplingBar::on_sample_count_changed()
267{
268 commit_sample_count();
269}
270
271void SamplingBar::on_sample_rate_changed()
272{
273 if (!_updating_sample_rate)
274 commit_sample_rate();
275}
276
277void SamplingBar::on_run_stop()
278{
279 commit_sample_rate();
280 run_stop();
281}
282
283} // namespace toolbars
284} // namespace pv