]> sigrok.org Git - pulseview.git/blob - pv/toolbars/samplingbar.cpp
Added AwaitTrigger capture state
[pulseview.git] / pv / toolbars / samplingbar.cpp
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/dialogs/deviceoptions.h>
36
37 using namespace std;
38
39 namespace pv {
40 namespace toolbars {
41
42 const 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
65 const uint64_t SamplingBar::DefaultRecordLength = 1000000;
66
67 SamplingBar::SamplingBar(QWidget *parent) :
68         QToolBar("Sampling Bar", parent),
69         _device_selector(this),
70         _configure_button(this),
71         _record_length_selector(this),
72         _sample_rate_list(this),
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         connect(&_run_stop_button, SIGNAL(clicked()),
79                 this, SLOT(on_run_stop()));
80         connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
81                 this, SLOT(on_device_selected()));
82         connect(&_configure_button, SIGNAL(clicked()),
83                 this, SLOT(on_configure()));
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
105         _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
106
107         addWidget(&_device_selector);
108         addWidget(&_configure_button);
109         addWidget(&_record_length_selector);
110         _sample_rate_list_action = addWidget(&_sample_rate_list);
111         _sample_rate_value_action = addWidget(&_sample_rate_value);
112         addWidget(&_run_stop_button);
113
114         connect(&_sample_rate_list, SIGNAL(currentIndexChanged(int)),
115                 this, SLOT(on_sample_rate_changed()));
116         connect(&_sample_rate_value, SIGNAL(editingFinished()),
117                 this, SLOT(on_sample_rate_changed()));
118 }
119
120 void SamplingBar::set_device_list(
121         const std::list<struct sr_dev_inst*> &devices)
122 {
123         _device_selector.clear();
124
125         BOOST_FOREACH (sr_dev_inst *sdi, devices) {
126                 const string title = DeviceManager::format_device_title(sdi);
127                 _device_selector.addItem(title.c_str(),
128                         qVariantFromValue((void*)sdi));
129         }
130
131         update_sample_rate_selector();
132 }
133
134 struct sr_dev_inst* SamplingBar::get_selected_device() const
135 {
136         const int index = _device_selector.currentIndex();
137         if (index < 0)
138                 return NULL;
139
140         return (sr_dev_inst*)_device_selector.itemData(
141                 index).value<void*>();
142 }
143
144 void SamplingBar::set_selected_device(struct sr_dev_inst *const sdi)
145 {
146         for (int i = 0; i < _device_selector.count(); i++)
147                 if (sdi == _device_selector.itemData(i).value<void*>()) {
148                         _device_selector.setCurrentIndex(i);
149                         return;
150                 }
151 }
152
153 uint64_t SamplingBar::get_record_length() const
154 {
155         const int index = _record_length_selector.currentIndex();
156         if (index < 0)
157                 return 0;
158
159         return _record_length_selector.itemData(index).value<uint64_t>();
160 }
161
162 void SamplingBar::set_capture_state(pv::SigSession::capture_state state)
163 {
164         const QIcon *icons[] = {&_icon_grey, &_icon_red, &_icon_green};
165         _run_stop_button.setIcon(*icons[state]);
166         _run_stop_button.setText((state == pv::SigSession::Stopped) ?
167                 tr("Run") : tr("Stop"));
168 }
169
170 void SamplingBar::update_sample_rate_selector()
171 {
172         const sr_dev_inst *const sdi = get_selected_device();
173         GVariant *gvar_dict, *gvar_list;
174         const uint64_t *elements = NULL;
175         gsize num_elements;
176         QAction *selector_action = NULL;
177
178         assert(_sample_rate_value_action);
179         assert(_sample_rate_list_action);
180
181         if (!sdi)
182                 return;
183
184         if (sr_config_list(sdi->driver, SR_CONF_SAMPLERATE,
185                         &gvar_dict, sdi) != SR_OK)
186                 return;
187
188         _sample_rate_list_action->setVisible(false);
189         _sample_rate_value_action->setVisible(false);
190
191         if ((gvar_list = g_variant_lookup_value(gvar_dict,
192                         "samplerate-steps", G_VARIANT_TYPE("at")))) {
193                 elements = (const uint64_t *)g_variant_get_fixed_array(
194                                 gvar_list, &num_elements, sizeof(uint64_t));
195                 _sample_rate_value.setRange(elements[0], elements[1]);
196                 _sample_rate_value.setSingleStep(elements[2]);
197                 g_variant_unref(gvar_list);
198
199                 selector_action = _sample_rate_value_action;
200         }
201         else if ((gvar_list = g_variant_lookup_value(gvar_dict,
202                         "samplerates", G_VARIANT_TYPE("at"))))
203         {
204                 elements = (const uint64_t *)g_variant_get_fixed_array(
205                                 gvar_list, &num_elements, sizeof(uint64_t));
206                 _sample_rate_list.clear();
207
208                 for (unsigned int i = 0; i < num_elements; i++)
209                 {
210                         char *const s = sr_samplerate_string(elements[i]);
211                         _sample_rate_list.addItem(QString(s),
212                                 qVariantFromValue(elements[i]));
213                         g_free(s);
214                 }
215
216                 _sample_rate_list.show();
217                 g_variant_unref(gvar_list);
218
219                 selector_action = _sample_rate_list_action;
220         }
221
222         g_variant_unref(gvar_dict);
223         update_sample_rate_selector_value();
224
225         // We delay showing the action, so that value change events
226         // are ignored.
227         if (selector_action)
228                 selector_action->setVisible(true);
229 }
230
231 void SamplingBar::update_sample_rate_selector_value()
232 {
233         sr_dev_inst *const sdi = get_selected_device();
234         GVariant *gvar;
235         uint64_t samplerate;
236
237         assert(sdi);
238
239         if (sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
240                 &gvar, sdi) != SR_OK) {
241                 qDebug() <<
242                                 "WARNING: Failed to get value of sample rate";
243                 return;
244         }
245         samplerate = g_variant_get_uint64(gvar);
246         g_variant_unref(gvar);
247
248         assert(_sample_rate_value_action);
249         assert(_sample_rate_list_action);
250
251         if (_sample_rate_value_action->isVisible())
252                 _sample_rate_value.setValue(samplerate);
253         else if (_sample_rate_list_action->isVisible())
254         {
255                 for (int i = 0; i < _sample_rate_list.count(); i++)
256                         if (samplerate == _sample_rate_list.itemData(
257                                 i).value<uint64_t>())
258                                 _sample_rate_list.setCurrentIndex(i);
259         }
260 }
261
262 void SamplingBar::commit_sample_rate()
263 {
264         uint64_t sample_rate = 0;
265
266         sr_dev_inst *const sdi = get_selected_device();
267         assert(sdi);
268
269         assert(_sample_rate_value_action);
270         assert(_sample_rate_list_action);
271
272         if (_sample_rate_value_action->isVisible())
273                 sample_rate = (uint64_t)_sample_rate_value.value();
274         else if (_sample_rate_list_action->isVisible())
275         {
276                 const int index = _sample_rate_list.currentIndex();
277                 if (index >= 0)
278                         sample_rate = _sample_rate_list.itemData(
279                                 index).value<uint64_t>();
280         }
281
282         if (sample_rate == 0)
283                 return;
284
285         // Set the samplerate
286         if (sr_config_set(sdi, SR_CONF_SAMPLERATE,
287                 g_variant_new_uint64(sample_rate)) != SR_OK) {
288                 qDebug() << "Failed to configure samplerate.";
289                 return;
290         }
291 }
292
293 void SamplingBar::on_device_selected()
294 {
295         update_sample_rate_selector();
296         device_selected();
297 }
298
299 void SamplingBar::on_sample_rate_changed()
300 {
301         commit_sample_rate();
302 }
303
304 void SamplingBar::on_configure()
305 {
306         commit_sample_rate();
307
308         sr_dev_inst *const sdi = get_selected_device();
309         assert(sdi);
310
311         pv::dialogs::DeviceOptions dlg(this, sdi);
312         dlg.exec();
313
314         update_sample_rate_selector_value();
315 }
316
317 void SamplingBar::on_run_stop()
318 {
319         commit_sample_rate();   
320         run_stop();
321 }
322
323 } // namespace toolbars
324 } // namespace pv