]>
Commit | Line | Data |
---|---|---|
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> |
19adbc2c | 35 | #include <pv/devinst.h> |
51d4a9ab | 36 | #include <pv/popups/deviceoptions.h> |
488f068c | 37 | #include <pv/popups/probes.h> |
cdb50f67 | 38 | |
19adbc2c JH |
39 | using boost::shared_ptr; |
40 | using std::map; | |
b50ef520 JH |
41 | using std::max; |
42 | using std::min; | |
819f4c25 | 43 | using std::string; |
dd63af74 | 44 | |
51e77110 | 45 | namespace pv { |
f4c92e1c | 46 | namespace toolbars { |
51e77110 | 47 | |
b50ef520 JH |
48 | const uint64_t SamplingBar::MinSampleCount = 100ULL; |
49 | const uint64_t SamplingBar::MaxSampleCount = 1000000000000ULL; | |
50 | const uint64_t SamplingBar::DefaultSampleCount = 1000000; | |
09f5d123 | 51 | |
aca00b1e | 52 | SamplingBar::SamplingBar(SigSession &session, QWidget *parent) : |
6fb67b27 | 53 | QToolBar("Sampling Bar", parent), |
aca00b1e | 54 | _session(session), |
dde1a563 | 55 | _device_selector(this), |
95237c18 | 56 | _updating_device_selector(false), |
cdb50f67 | 57 | _configure_button(this), |
b3e8a5d8 | 58 | _configure_button_action(NULL), |
51d4a9ab | 59 | _probes_button(this), |
124d97de | 60 | _sample_count(" samples", this), |
1198b887 JH |
61 | _sample_rate("Hz", this), |
62 | _updating_sample_rate(false), | |
85756012 | 63 | _updating_sample_count(false), |
6546a6a7 | 64 | _sample_count_supported(false), |
2b49eeb0 | 65 | _icon_red(":/icons/status-red.svg"), |
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 | 70 | connect(&_run_stop_button, SIGNAL(clicked()), |
9f3d12f3 | 71 | this, SLOT(on_run_stop())); |
dde1a563 JH |
72 | connect(&_device_selector, SIGNAL(currentIndexChanged (int)), |
73 | this, SLOT(on_device_selected())); | |
124d97de ML |
74 | connect(&_sample_count, SIGNAL(value_changed()), |
75 | this, SLOT(on_sample_count_changed())); | |
1198b887 JH |
76 | connect(&_sample_rate, SIGNAL(value_changed()), |
77 | this, SLOT(on_sample_rate_changed())); | |
dde1a563 | 78 | |
124d97de | 79 | _sample_count.show_min_max_step(0, UINT64_MAX, 1); |
215f9499 | 80 | |
2b49eeb0 | 81 | set_capture_state(pv::SigSession::Stopped); |
274d4f13 | 82 | |
688ef645 JH |
83 | _configure_button.setIcon(QIcon::fromTheme("configure", |
84 | QIcon(":/icons/configure.png"))); | |
b7b659aa | 85 | |
51d4a9ab JH |
86 | _probes_button.setIcon(QIcon::fromTheme("probes", |
87 | QIcon(":/icons/probes.svg"))); | |
cdb50f67 | 88 | |
f5798068 JH |
89 | _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon); |
90 | ||
6fb67b27 | 91 | addWidget(&_device_selector); |
b3e8a5d8 | 92 | _configure_button_action = addWidget(&_configure_button); |
51d4a9ab | 93 | addWidget(&_probes_button); |
124d97de | 94 | addWidget(&_sample_count); |
1198b887 | 95 | addWidget(&_sample_rate); |
6fb67b27 | 96 | |
1198b887 | 97 | addWidget(&_run_stop_button); |
6fb67b27 JH |
98 | } |
99 | ||
18203d86 | 100 | void SamplingBar::set_device_list( |
19adbc2c | 101 | const std::list< shared_ptr<pv::DevInst> > &devices) |
18203d86 | 102 | { |
95237c18 JH |
103 | _updating_device_selector = true; |
104 | ||
18203d86 | 105 | _device_selector.clear(); |
19adbc2c | 106 | _device_selector_map.clear(); |
18203d86 | 107 | |
19adbc2c JH |
108 | BOOST_FOREACH (shared_ptr<pv::DevInst> dev_inst, devices) { |
109 | assert(dev_inst); | |
110 | const string title = dev_inst->format_device_title(); | |
111 | const sr_dev_inst *sdi = dev_inst->dev_inst(); | |
112 | assert(sdi); | |
113 | ||
114 | _device_selector_map[sdi] = dev_inst; | |
dd63af74 JH |
115 | _device_selector.addItem(title.c_str(), |
116 | qVariantFromValue((void*)sdi)); | |
18203d86 JH |
117 | } |
118 | ||
95237c18 JH |
119 | _updating_device_selector = false; |
120 | ||
b7b659aa | 121 | on_device_selected(); |
18203d86 JH |
122 | } |
123 | ||
19adbc2c | 124 | shared_ptr<pv::DevInst> SamplingBar::get_selected_device() const |
d4984fe7 | 125 | { |
6fb67b27 | 126 | const int index = _device_selector.currentIndex(); |
333d5bbc | 127 | if (index < 0) |
19adbc2c JH |
128 | return shared_ptr<pv::DevInst>(); |
129 | ||
130 | const sr_dev_inst *const sdi = | |
131 | (const sr_dev_inst*)_device_selector.itemData( | |
132 | index).value<void*>(); | |
133 | assert(sdi); | |
6fb67b27 | 134 | |
19adbc2c JH |
135 | map<const sr_dev_inst*, boost::weak_ptr<DevInst> >:: |
136 | const_iterator iter = _device_selector_map.find(sdi); | |
137 | if (iter == _device_selector_map.end()) | |
138 | return shared_ptr<pv::DevInst>(); | |
139 | ||
140 | return shared_ptr<pv::DevInst>((*iter).second); | |
6fb67b27 JH |
141 | } |
142 | ||
19adbc2c | 143 | void SamplingBar::set_selected_device(boost::shared_ptr<pv::DevInst> dev_inst) |
dba73e73 | 144 | { |
19adbc2c JH |
145 | assert(dev_inst); |
146 | ||
793f8096 | 147 | for (int i = 0; i < _device_selector.count(); i++) |
19adbc2c JH |
148 | if (dev_inst->dev_inst() == |
149 | _device_selector.itemData(i).value<void*>()) { | |
dba73e73 JH |
150 | _device_selector.setCurrentIndex(i); |
151 | return; | |
152 | } | |
153 | } | |
154 | ||
2b49eeb0 | 155 | void SamplingBar::set_capture_state(pv::SigSession::capture_state state) |
6ac96c2e | 156 | { |
2b49eeb0 JH |
157 | const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green}; |
158 | _run_stop_button.setIcon(*icons[state]); | |
159 | _run_stop_button.setText((state == pv::SigSession::Stopped) ? | |
160 | tr("Run") : tr("Stop")); | |
6ac96c2e JH |
161 | } |
162 | ||
dde1a563 JH |
163 | void SamplingBar::update_sample_rate_selector() |
164 | { | |
488f5d3f BV |
165 | GVariant *gvar_dict, *gvar_list; |
166 | const uint64_t *elements = NULL; | |
167 | gsize num_elements; | |
dde1a563 | 168 | |
19adbc2c JH |
169 | const shared_ptr<DevInst> dev_inst = get_selected_device(); |
170 | if (!dev_inst) | |
ef4d0201 JH |
171 | return; |
172 | ||
19adbc2c JH |
173 | const sr_dev_inst *const sdi = dev_inst->dev_inst(); |
174 | assert(sdi); | |
175 | ||
1198b887 JH |
176 | _updating_sample_rate = true; |
177 | ||
68162c29 BV |
178 | if (sr_config_list(sdi->driver, sdi, NULL, |
179 | SR_CONF_SAMPLERATE, &gvar_dict) != SR_OK) | |
1198b887 JH |
180 | { |
181 | _sample_rate.show_none(); | |
182 | _updating_sample_rate = false; | |
dde1a563 | 183 | return; |
1198b887 | 184 | } |
dde1a563 | 185 | |
488f5d3f | 186 | if ((gvar_list = g_variant_lookup_value(gvar_dict, |
1198b887 JH |
187 | "samplerate-steps", G_VARIANT_TYPE("at")))) |
188 | { | |
488f5d3f BV |
189 | elements = (const uint64_t *)g_variant_get_fixed_array( |
190 | gvar_list, &num_elements, sizeof(uint64_t)); | |
aafe53af JH |
191 | |
192 | const uint64_t min = elements[0]; | |
193 | const uint64_t max = elements[1]; | |
194 | const uint64_t step = elements[2]; | |
195 | ||
488f5d3f | 196 | g_variant_unref(gvar_list); |
aafe53af JH |
197 | |
198 | assert(min > 0); | |
199 | assert(max > 0); | |
200 | assert(max > min); | |
201 | assert(step > 0); | |
202 | ||
203 | if (step == 1) | |
204 | _sample_rate.show_125_list(min, max); | |
205 | else | |
206 | { | |
207 | // When the step is not 1, we cam't make a 1-2-5-10 | |
208 | // list of sample rates, because we may not be able to | |
209 | // make round numbers. Therefore in this case, show a | |
210 | // spin box. | |
211 | _sample_rate.show_min_max_step(min, max, step); | |
212 | } | |
dde1a563 | 213 | } |
488f5d3f BV |
214 | else if ((gvar_list = g_variant_lookup_value(gvar_dict, |
215 | "samplerates", G_VARIANT_TYPE("at")))) | |
dde1a563 | 216 | { |
488f5d3f BV |
217 | elements = (const uint64_t *)g_variant_get_fixed_array( |
218 | gvar_list, &num_elements, sizeof(uint64_t)); | |
1198b887 | 219 | _sample_rate.show_list(elements, num_elements); |
488f5d3f | 220 | g_variant_unref(gvar_list); |
dde1a563 | 221 | } |
1198b887 | 222 | _updating_sample_rate = false; |
48888313 | 223 | |
488f5d3f | 224 | g_variant_unref(gvar_dict); |
48888313 JH |
225 | update_sample_rate_selector_value(); |
226 | } | |
227 | ||
228 | void SamplingBar::update_sample_rate_selector_value() | |
229 | { | |
488f5d3f BV |
230 | GVariant *gvar; |
231 | uint64_t samplerate; | |
232 | ||
19adbc2c JH |
233 | const shared_ptr<DevInst> dev_inst = get_selected_device(); |
234 | if (!dev_inst) | |
235 | return; | |
236 | ||
237 | const sr_dev_inst *const sdi = dev_inst->dev_inst(); | |
48888313 JH |
238 | assert(sdi); |
239 | ||
68162c29 BV |
240 | if (sr_config_get(sdi->driver, sdi, NULL, |
241 | SR_CONF_SAMPLERATE, &gvar) != SR_OK) { | |
eb4008a6 JH |
242 | qDebug() << |
243 | "WARNING: Failed to get value of sample rate"; | |
244 | return; | |
245 | } | |
488f5d3f BV |
246 | samplerate = g_variant_get_uint64(gvar); |
247 | g_variant_unref(gvar); | |
48888313 | 248 | |
1198b887 JH |
249 | _updating_sample_rate = true; |
250 | _sample_rate.set_value(samplerate); | |
251 | _updating_sample_rate = false; | |
48888313 JH |
252 | } |
253 | ||
85756012 ML |
254 | void SamplingBar::update_sample_count_selector() |
255 | { | |
85756012 | 256 | GVariant *gvar; |
85756012 | 257 | |
19adbc2c JH |
258 | const shared_ptr<DevInst> dev_inst = get_selected_device(); |
259 | if (!dev_inst) | |
260 | return; | |
261 | ||
262 | const sr_dev_inst *const sdi = dev_inst->dev_inst(); | |
85756012 ML |
263 | assert(sdi); |
264 | ||
b50ef520 | 265 | _updating_sample_count = true; |
85756012 | 266 | |
b50ef520 | 267 | if (_sample_count_supported) |
df3c1aa3 | 268 | { |
b50ef520 | 269 | uint64_t sample_count = DefaultSampleCount; |
1d04852f | 270 | uint64_t min_sample_count = 0; |
b50ef520 JH |
271 | uint64_t max_sample_count = MaxSampleCount; |
272 | ||
1d04852f JH |
273 | if (sr_config_list(sdi->driver, sdi, NULL, |
274 | SR_CONF_LIMIT_SAMPLES, &gvar) == SR_OK) { | |
275 | g_variant_get(gvar, "(tt)", | |
276 | &min_sample_count, &max_sample_count); | |
b50ef520 JH |
277 | g_variant_unref(gvar); |
278 | } | |
279 | ||
1d04852f JH |
280 | min_sample_count = min(max(min_sample_count, MinSampleCount), |
281 | max_sample_count); | |
282 | ||
283 | _sample_count.show_125_list( | |
284 | min_sample_count, max_sample_count); | |
b50ef520 JH |
285 | |
286 | if (sr_config_get(sdi->driver, sdi, NULL, | |
287 | SR_CONF_LIMIT_SAMPLES, &gvar) == SR_OK) | |
288 | { | |
289 | sample_count = g_variant_get_uint64(gvar); | |
290 | if (sample_count == 0) | |
291 | sample_count = DefaultSampleCount; | |
292 | sample_count = min(max(sample_count, MinSampleCount), | |
293 | max_sample_count); | |
294 | ||
295 | g_variant_unref(gvar); | |
296 | } | |
297 | ||
298 | _sample_count.set_value(sample_count); | |
85756012 | 299 | } |
b50ef520 JH |
300 | else |
301 | _sample_count.show_none(); | |
df3c1aa3 | 302 | |
df3c1aa3 | 303 | _updating_sample_count = false; |
85756012 ML |
304 | } |
305 | ||
124d97de ML |
306 | void SamplingBar::commit_sample_count() |
307 | { | |
308 | uint64_t sample_count = 0; | |
309 | ||
19adbc2c JH |
310 | const shared_ptr<DevInst> dev_inst = get_selected_device(); |
311 | if (!dev_inst) | |
312 | return; | |
313 | ||
314 | const sr_dev_inst *const sdi = dev_inst->dev_inst(); | |
124d97de ML |
315 | assert(sdi); |
316 | ||
317 | sample_count = _sample_count.value(); | |
318 | ||
319 | // Set the sample count | |
320 | if (sr_config_set(sdi, NULL, SR_CONF_LIMIT_SAMPLES, | |
321 | g_variant_new_uint64(sample_count)) != SR_OK) { | |
322 | qDebug() << "Failed to configure sample count."; | |
323 | return; | |
324 | } | |
325 | } | |
326 | ||
48888313 JH |
327 | void SamplingBar::commit_sample_rate() |
328 | { | |
329 | uint64_t sample_rate = 0; | |
330 | ||
19adbc2c JH |
331 | const shared_ptr<DevInst> dev_inst = get_selected_device(); |
332 | if (!dev_inst) | |
333 | return; | |
334 | ||
335 | const sr_dev_inst *const sdi = dev_inst->dev_inst(); | |
48888313 JH |
336 | assert(sdi); |
337 | ||
1198b887 | 338 | sample_rate = _sample_rate.value(); |
f9541bde JH |
339 | if (sample_rate == 0) |
340 | return; | |
341 | ||
48888313 | 342 | // Set the samplerate |
68162c29 | 343 | if (sr_config_set(sdi, NULL, SR_CONF_SAMPLERATE, |
488f5d3f | 344 | g_variant_new_uint64(sample_rate)) != SR_OK) { |
48888313 JH |
345 | qDebug() << "Failed to configure samplerate."; |
346 | return; | |
347 | } | |
dde1a563 JH |
348 | } |
349 | ||
350 | void SamplingBar::on_device_selected() | |
351 | { | |
14cbfcc8 ML |
352 | GVariant *gvar; |
353 | ||
488f068c | 354 | using namespace pv::popups; |
51d4a9ab | 355 | |
95237c18 JH |
356 | if (_updating_device_selector) |
357 | return; | |
358 | ||
19adbc2c JH |
359 | const shared_ptr<DevInst> dev_inst = get_selected_device(); |
360 | if (!dev_inst) | |
361 | return; | |
362 | ||
363 | const sr_dev_inst *const sdi = dev_inst->dev_inst(); | |
364 | assert(sdi); | |
365 | ||
366 | _session.set_device(dev_inst); | |
51d4a9ab | 367 | |
b3e8a5d8 | 368 | // Update the configure popup |
19adbc2c | 369 | DeviceOptions *const opts = new DeviceOptions(dev_inst, this); |
b3e8a5d8 JH |
370 | _configure_button_action->setVisible( |
371 | !opts->binding().properties().empty()); | |
372 | _configure_button.set_popup(opts); | |
488f068c JH |
373 | |
374 | // Update the probes popup | |
375 | Probes *const probes = new Probes(_session, this); | |
376 | _probes_button.set_popup(probes); | |
df539680 | 377 | |
14cbfcc8 ML |
378 | // Update supported options. |
379 | _sample_count_supported = false; | |
380 | ||
381 | if (sr_config_list(sdi->driver, sdi, NULL, | |
382 | SR_CONF_DEVICE_OPTIONS, &gvar) == SR_OK) | |
383 | { | |
384 | gsize num_opts; | |
385 | const int *const options = (const int32_t *)g_variant_get_fixed_array( | |
386 | gvar, &num_opts, sizeof(int32_t)); | |
387 | for (unsigned int i = 0; i < num_opts; i++) | |
388 | { | |
389 | switch (options[i]) { | |
390 | case SR_CONF_LIMIT_SAMPLES: | |
391 | _sample_count_supported = true; | |
392 | break; | |
393 | case SR_CONF_LIMIT_FRAMES: | |
394 | sr_config_set(sdi, NULL, SR_CONF_LIMIT_FRAMES, | |
395 | g_variant_new_uint64(1)); | |
396 | break; | |
397 | } | |
398 | } | |
399 | } | |
400 | ||
df539680 ML |
401 | // Update sweep timing widgets. |
402 | update_sample_count_selector(); | |
403 | update_sample_rate_selector(); | |
dde1a563 | 404 | } |
51e77110 | 405 | |
124d97de ML |
406 | void SamplingBar::on_sample_count_changed() |
407 | { | |
85756012 ML |
408 | if(!_updating_sample_count) |
409 | commit_sample_count(); | |
124d97de ML |
410 | } |
411 | ||
48888313 JH |
412 | void SamplingBar::on_sample_rate_changed() |
413 | { | |
1198b887 JH |
414 | if (!_updating_sample_rate) |
415 | commit_sample_rate(); | |
48888313 JH |
416 | } |
417 | ||
9f3d12f3 JH |
418 | void SamplingBar::on_run_stop() |
419 | { | |
b50ef520 | 420 | commit_sample_count(); |
9f3d12f3 JH |
421 | commit_sample_rate(); |
422 | run_stop(); | |
423 | } | |
424 | ||
f4c92e1c | 425 | } // namespace toolbars |
51e77110 | 426 | } // namespace pv |