]>
Commit | Line | Data |
---|---|---|
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 <QAction> | |
26 | #include <QDebug> | |
27 | #include <QHelpEvent> | |
28 | #include <QToolTip> | |
29 | ||
30 | #include "samplingbar.hpp" | |
31 | ||
32 | #include <pv/devicemanager.hpp> | |
33 | #include <pv/mainwindow.hpp> | |
34 | #include <pv/popups/deviceoptions.hpp> | |
35 | #include <pv/popups/channels.hpp> | |
36 | #include <pv/util.hpp> | |
37 | ||
38 | #include <libsigrok/libsigrok.hpp> | |
39 | ||
40 | using std::map; | |
41 | using std::vector; | |
42 | using std::max; | |
43 | using std::min; | |
44 | using std::shared_ptr; | |
45 | using std::string; | |
46 | ||
47 | using sigrok::Capability; | |
48 | using sigrok::ConfigKey; | |
49 | using sigrok::Device; | |
50 | using sigrok::Error; | |
51 | ||
52 | namespace pv { | |
53 | namespace toolbars { | |
54 | ||
55 | const uint64_t SamplingBar::MinSampleCount = 100ULL; | |
56 | const uint64_t SamplingBar::MaxSampleCount = 1000000000000ULL; | |
57 | const uint64_t SamplingBar::DefaultSampleCount = 1000000; | |
58 | ||
59 | SamplingBar::SamplingBar(Session &session, MainWindow &main_window) : | |
60 | QToolBar("Sampling Bar", &main_window), | |
61 | session_(session), | |
62 | main_window_(main_window), | |
63 | device_selector_(this), | |
64 | updating_device_selector_(false), | |
65 | configure_button_(this), | |
66 | configure_button_action_(NULL), | |
67 | channels_button_(this), | |
68 | sample_count_(" samples", this), | |
69 | sample_rate_("Hz", this), | |
70 | updating_sample_rate_(false), | |
71 | updating_sample_count_(false), | |
72 | sample_count_supported_(false), | |
73 | icon_red_(":/icons/status-red.svg"), | |
74 | icon_green_(":/icons/status-green.svg"), | |
75 | icon_grey_(":/icons/status-grey.svg"), | |
76 | run_stop_button_(this) | |
77 | { | |
78 | setObjectName(QString::fromUtf8("SamplingBar")); | |
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 | connect(&sample_count_, SIGNAL(value_changed()), | |
85 | this, SLOT(on_sample_count_changed())); | |
86 | connect(&sample_rate_, SIGNAL(value_changed()), | |
87 | this, SLOT(on_sample_rate_changed())); | |
88 | ||
89 | sample_count_.show_min_max_step(0, UINT64_MAX, 1); | |
90 | ||
91 | set_capture_state(pv::Session::Stopped); | |
92 | ||
93 | configure_button_.setIcon(QIcon::fromTheme("configure", | |
94 | QIcon(":/icons/configure.png"))); | |
95 | ||
96 | channels_button_.setIcon(QIcon::fromTheme("channels", | |
97 | QIcon(":/icons/channels.svg"))); | |
98 | ||
99 | run_stop_button_.setToolButtonStyle(Qt::ToolButtonTextBesideIcon); | |
100 | ||
101 | addWidget(&device_selector_); | |
102 | configure_button_action_ = addWidget(&configure_button_); | |
103 | addWidget(&channels_button_); | |
104 | addWidget(&sample_count_); | |
105 | addWidget(&sample_rate_); | |
106 | ||
107 | addWidget(&run_stop_button_); | |
108 | ||
109 | sample_count_.installEventFilter(this); | |
110 | sample_rate_.installEventFilter(this); | |
111 | } | |
112 | ||
113 | void SamplingBar::set_device_list( | |
114 | const std::list< std::shared_ptr<sigrok::Device> > &devices, | |
115 | shared_ptr<Device> selected) | |
116 | { | |
117 | int selected_index = -1; | |
118 | ||
119 | assert(selected); | |
120 | ||
121 | updating_device_selector_ = true; | |
122 | ||
123 | device_selector_.clear(); | |
124 | ||
125 | for (auto device : devices) { | |
126 | assert(device); | |
127 | ||
128 | string display_name = | |
129 | session_.device_manager().get_display_name(device); | |
130 | ||
131 | if (selected == device) | |
132 | selected_index = device_selector_.count(); | |
133 | ||
134 | device_selector_.addItem(display_name.c_str(), | |
135 | qVariantFromValue(device)); | |
136 | } | |
137 | ||
138 | // The selected device should have been in the list | |
139 | assert(selected_index != -1); | |
140 | device_selector_.setCurrentIndex(selected_index); | |
141 | ||
142 | update_device_config_widgets(); | |
143 | ||
144 | updating_device_selector_ = false; | |
145 | } | |
146 | ||
147 | shared_ptr<Device> SamplingBar::get_selected_device() const | |
148 | { | |
149 | const int index = device_selector_.currentIndex(); | |
150 | if (index < 0) | |
151 | return shared_ptr<Device>(); | |
152 | ||
153 | return device_selector_.itemData(index).value<shared_ptr<Device>>(); | |
154 | } | |
155 | ||
156 | void SamplingBar::set_capture_state(pv::Session::capture_state state) | |
157 | { | |
158 | const QIcon *icons[] = {&icon_grey_, &icon_red_, &icon_green_}; | |
159 | run_stop_button_.setIcon(*icons[state]); | |
160 | run_stop_button_.setText((state == pv::Session::Stopped) ? | |
161 | tr("Run") : tr("Stop")); | |
162 | run_stop_button_.setShortcut(QKeySequence(Qt::Key_Space)); | |
163 | } | |
164 | ||
165 | void SamplingBar::update_sample_rate_selector() | |
166 | { | |
167 | Glib::VariantContainerBase gvar_dict; | |
168 | GVariant *gvar_list; | |
169 | const uint64_t *elements = NULL; | |
170 | gsize num_elements; | |
171 | ||
172 | if (updating_sample_rate_) | |
173 | return; | |
174 | ||
175 | const shared_ptr<Device> device = get_selected_device(); | |
176 | if (!device) | |
177 | return; | |
178 | ||
179 | assert(!updating_sample_rate_); | |
180 | updating_sample_rate_ = true; | |
181 | ||
182 | const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS); | |
183 | const auto iter = keys.find(ConfigKey::SAMPLERATE); | |
184 | if (iter != keys.end() && | |
185 | (*iter).second.find(sigrok::LIST) != (*iter).second.end()) { | |
186 | gvar_dict = device->config_list(ConfigKey::SAMPLERATE); | |
187 | } else { | |
188 | sample_rate_.show_none(); | |
189 | updating_sample_rate_ = false; | |
190 | return; | |
191 | } | |
192 | ||
193 | if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(), | |
194 | "samplerate-steps", G_VARIANT_TYPE("at")))) | |
195 | { | |
196 | elements = (const uint64_t *)g_variant_get_fixed_array( | |
197 | gvar_list, &num_elements, sizeof(uint64_t)); | |
198 | ||
199 | const uint64_t min = elements[0]; | |
200 | const uint64_t max = elements[1]; | |
201 | const uint64_t step = elements[2]; | |
202 | ||
203 | g_variant_unref(gvar_list); | |
204 | ||
205 | assert(min > 0); | |
206 | assert(max > 0); | |
207 | assert(max > min); | |
208 | assert(step > 0); | |
209 | ||
210 | if (step == 1) | |
211 | sample_rate_.show_125_list(min, max); | |
212 | else | |
213 | { | |
214 | // When the step is not 1, we cam't make a 1-2-5-10 | |
215 | // list of sample rates, because we may not be able to | |
216 | // make round numbers. Therefore in this case, show a | |
217 | // spin box. | |
218 | sample_rate_.show_min_max_step(min, max, step); | |
219 | } | |
220 | } | |
221 | else if ((gvar_list = g_variant_lookup_value(gvar_dict.gobj(), | |
222 | "samplerates", G_VARIANT_TYPE("at")))) | |
223 | { | |
224 | elements = (const uint64_t *)g_variant_get_fixed_array( | |
225 | gvar_list, &num_elements, sizeof(uint64_t)); | |
226 | sample_rate_.show_list(elements, num_elements); | |
227 | g_variant_unref(gvar_list); | |
228 | } | |
229 | updating_sample_rate_ = false; | |
230 | ||
231 | update_sample_rate_selector_value(); | |
232 | } | |
233 | ||
234 | void SamplingBar::update_sample_rate_selector_value() | |
235 | { | |
236 | if (updating_sample_rate_) | |
237 | return; | |
238 | ||
239 | const shared_ptr<Device> device = get_selected_device(); | |
240 | if (!device) | |
241 | return; | |
242 | ||
243 | try { | |
244 | auto gvar = device->config_get(ConfigKey::SAMPLERATE); | |
245 | uint64_t samplerate = | |
246 | Glib::VariantBase::cast_dynamic<Glib::Variant<guint64>>(gvar).get(); | |
247 | assert(!updating_sample_rate_); | |
248 | updating_sample_rate_ = true; | |
249 | sample_rate_.set_value(samplerate); | |
250 | updating_sample_rate_ = false; | |
251 | } catch (Error error) { | |
252 | qDebug() << "WARNING: Failed to get value of sample rate"; | |
253 | return; | |
254 | } | |
255 | } | |
256 | ||
257 | void SamplingBar::update_sample_count_selector() | |
258 | { | |
259 | if (updating_sample_count_) | |
260 | return; | |
261 | ||
262 | const shared_ptr<Device> device = get_selected_device(); | |
263 | if (!device) | |
264 | return; | |
265 | ||
266 | assert(!updating_sample_count_); | |
267 | updating_sample_count_ = true; | |
268 | ||
269 | if (!sample_count_supported_) | |
270 | { | |
271 | sample_count_.show_none(); | |
272 | updating_sample_count_ = false; | |
273 | return; | |
274 | } | |
275 | ||
276 | uint64_t sample_count = sample_count_.value(); | |
277 | uint64_t min_sample_count = 0; | |
278 | uint64_t max_sample_count = MaxSampleCount; | |
279 | ||
280 | if (sample_count == 0) | |
281 | sample_count = DefaultSampleCount; | |
282 | ||
283 | const auto keys = device->config_keys(ConfigKey::DEVICE_OPTIONS); | |
284 | const auto iter = keys.find(ConfigKey::LIMIT_SAMPLES); | |
285 | if (iter != keys.end() && | |
286 | (*iter).second.find(sigrok::LIST) != (*iter).second.end()) { | |
287 | auto gvar = device->config_list(ConfigKey::LIMIT_SAMPLES); | |
288 | g_variant_get(gvar.gobj(), "(tt)", | |
289 | &min_sample_count, &max_sample_count); | |
290 | } | |
291 | ||
292 | min_sample_count = min(max(min_sample_count, MinSampleCount), | |
293 | max_sample_count); | |
294 | ||
295 | sample_count_.show_125_list( | |
296 | min_sample_count, max_sample_count); | |
297 | ||
298 | try { | |
299 | auto gvar = device->config_get(ConfigKey::LIMIT_SAMPLES); | |
300 | sample_count = g_variant_get_uint64(gvar.gobj()); | |
301 | if (sample_count == 0) | |
302 | sample_count = DefaultSampleCount; | |
303 | sample_count = min(max(sample_count, MinSampleCount), | |
304 | max_sample_count); | |
305 | } catch (Error error) {} | |
306 | ||
307 | sample_count_.set_value(sample_count); | |
308 | ||
309 | updating_sample_count_ = false; | |
310 | } | |
311 | ||
312 | void SamplingBar::update_device_config_widgets() | |
313 | { | |
314 | using namespace pv::popups; | |
315 | ||
316 | const shared_ptr<Device> device = get_selected_device(); | |
317 | if (!device) | |
318 | return; | |
319 | ||
320 | // Update the configure popup | |
321 | DeviceOptions *const opts = new DeviceOptions(device, this); | |
322 | configure_button_action_->setVisible( | |
323 | !opts->binding().properties().empty()); | |
324 | configure_button_.set_popup(opts); | |
325 | ||
326 | // Update the channels popup | |
327 | Channels *const channels = new Channels(session_, this); | |
328 | channels_button_.set_popup(channels); | |
329 | ||
330 | // Update supported options. | |
331 | sample_count_supported_ = false; | |
332 | ||
333 | try { | |
334 | for (auto entry : device->config_keys(ConfigKey::DEVICE_OPTIONS)) | |
335 | { | |
336 | auto key = entry.first; | |
337 | auto capabilities = entry.second; | |
338 | switch (key->id()) { | |
339 | case SR_CONF_LIMIT_SAMPLES: | |
340 | if (capabilities.count(Capability::SET)) | |
341 | sample_count_supported_ = true; | |
342 | break; | |
343 | case SR_CONF_LIMIT_FRAMES: | |
344 | if (capabilities.count(Capability::SET)) | |
345 | { | |
346 | device->config_set(ConfigKey::LIMIT_FRAMES, | |
347 | Glib::Variant<guint64>::create(1)); | |
348 | on_config_changed(); | |
349 | } | |
350 | break; | |
351 | default: | |
352 | break; | |
353 | } | |
354 | } | |
355 | } catch (Error error) {} | |
356 | ||
357 | // Add notification of reconfigure events | |
358 | disconnect(this, SLOT(on_config_changed())); | |
359 | connect(&opts->binding(), SIGNAL(config_changed()), | |
360 | this, SLOT(on_config_changed())); | |
361 | ||
362 | // Update sweep timing widgets. | |
363 | update_sample_count_selector(); | |
364 | update_sample_rate_selector(); | |
365 | } | |
366 | ||
367 | void SamplingBar::commit_sample_count() | |
368 | { | |
369 | uint64_t sample_count = 0; | |
370 | ||
371 | if (updating_sample_count_) | |
372 | return; | |
373 | ||
374 | const shared_ptr<Device> device = get_selected_device(); | |
375 | if (!device) | |
376 | return; | |
377 | ||
378 | sample_count = sample_count_.value(); | |
379 | ||
380 | // Set the sample count | |
381 | assert(!updating_sample_count_); | |
382 | updating_sample_count_ = true; | |
383 | if (sample_count_supported_) | |
384 | { | |
385 | try { | |
386 | device->config_set(ConfigKey::LIMIT_SAMPLES, | |
387 | Glib::Variant<guint64>::create(sample_count)); | |
388 | on_config_changed(); | |
389 | } catch (Error error) { | |
390 | qDebug() << "Failed to configure sample count."; | |
391 | return; | |
392 | } | |
393 | } | |
394 | updating_sample_count_ = false; | |
395 | } | |
396 | ||
397 | void SamplingBar::commit_sample_rate() | |
398 | { | |
399 | uint64_t sample_rate = 0; | |
400 | ||
401 | if (updating_sample_rate_) | |
402 | return; | |
403 | ||
404 | const shared_ptr<Device> device = get_selected_device(); | |
405 | if (!device) | |
406 | return; | |
407 | ||
408 | sample_rate = sample_rate_.value(); | |
409 | if (sample_rate == 0) | |
410 | return; | |
411 | ||
412 | // Set the samplerate | |
413 | assert(!updating_sample_rate_); | |
414 | updating_sample_rate_ = true; | |
415 | try { | |
416 | device->config_set(ConfigKey::SAMPLERATE, | |
417 | Glib::Variant<guint64>::create(sample_rate)); | |
418 | on_config_changed(); | |
419 | } catch (Error error) { | |
420 | qDebug() << "Failed to configure samplerate."; | |
421 | return; | |
422 | } | |
423 | updating_sample_rate_ = false; | |
424 | } | |
425 | ||
426 | void SamplingBar::on_device_selected() | |
427 | { | |
428 | if (updating_device_selector_) | |
429 | return; | |
430 | ||
431 | shared_ptr<Device> device = get_selected_device(); | |
432 | if (!device) | |
433 | return; | |
434 | ||
435 | main_window_.select_device(device); | |
436 | ||
437 | update_device_config_widgets(); | |
438 | } | |
439 | ||
440 | void SamplingBar::on_sample_count_changed() | |
441 | { | |
442 | commit_sample_count(); | |
443 | } | |
444 | ||
445 | void SamplingBar::on_sample_rate_changed() | |
446 | { | |
447 | commit_sample_rate(); | |
448 | } | |
449 | ||
450 | void SamplingBar::on_run_stop() | |
451 | { | |
452 | commit_sample_count(); | |
453 | commit_sample_rate(); | |
454 | main_window_.run_stop(); | |
455 | } | |
456 | ||
457 | void SamplingBar::on_config_changed() | |
458 | { | |
459 | commit_sample_count(); | |
460 | update_sample_count_selector(); | |
461 | commit_sample_rate(); | |
462 | update_sample_rate_selector(); | |
463 | } | |
464 | ||
465 | bool SamplingBar::eventFilter(QObject *watched, QEvent *event) | |
466 | { | |
467 | if ((watched == &sample_count_ || watched == &sample_rate_) && | |
468 | (event->type() == QEvent::ToolTip)) { | |
469 | double sec = (double)sample_count_.value() / sample_rate_.value(); | |
470 | QHelpEvent *help_event = static_cast<QHelpEvent*>(event); | |
471 | ||
472 | QString str = tr("Total sampling time: %1").arg(pv::util::format_second(sec)); | |
473 | QToolTip::showText(help_event->globalPos(), str); | |
474 | ||
475 | return true; | |
476 | } | |
477 | ||
478 | return false; | |
479 | } | |
480 | ||
481 | } // namespace toolbars | |
482 | } // namespace pv |