]> sigrok.org Git - pulseview.git/blob - pv/toolbars/samplingbar.cpp
Moved SamplingBar into pv::toolbars namespace
[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/dialogs/deviceoptions.h>
35
36 namespace pv {
37 namespace toolbars {
38
39 const uint64_t SamplingBar::RecordLengths[20] = {
40         1000,
41         2500,
42         5000,
43         10000,
44         25000,
45         50000,
46         100000,
47         250000,
48         500000,
49         1000000,
50         2000000,
51         5000000,
52         10000000,
53         25000000,
54         50000000,
55         100000000,
56         250000000,
57         500000000,
58         1000000000,
59         10000000000ULL,
60 };
61
62 const uint64_t SamplingBar::DefaultRecordLength = 1000000;
63
64 SamplingBar::SamplingBar(QWidget *parent) :
65         QToolBar("Sampling Bar", parent),
66         _device_selector(this),
67         _configure_button(this),
68         _record_length_selector(this),
69         _sample_rate_list(this),
70         _icon_green(":/icons/status-green.svg"),
71         _icon_grey(":/icons/status-grey.svg"),
72         _run_stop_button(this)
73 {
74         connect(&_run_stop_button, SIGNAL(clicked()),
75                 this, SIGNAL(run_stop()));
76         connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
77                 this, SLOT(on_device_selected()));
78         connect(&_configure_button, SIGNAL(clicked()),
79                 this, SLOT(configure()));
80
81         _sample_rate_value.setDecimals(0);
82         _sample_rate_value.setSuffix("Hz");
83
84         for (size_t i = 0; i < countof(RecordLengths); i++)
85         {
86                 const uint64_t &l = RecordLengths[i];
87                 char *const text = sr_si_string_u64(l, " samples");
88                 _record_length_selector.addItem(QString(text),
89                         qVariantFromValue(l));
90                 g_free(text);
91
92                 if (l == DefaultRecordLength)
93                         _record_length_selector.setCurrentIndex(i);
94         }
95
96         set_sampling(false);
97
98         _configure_button.setIcon(QIcon::fromTheme("configure",
99                 QIcon(":/icons/configure.png")));
100
101         _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
102
103         addWidget(&_device_selector);
104         addWidget(&_configure_button);
105         addWidget(&_record_length_selector);
106         _sample_rate_list_action = addWidget(&_sample_rate_list);
107         _sample_rate_value_action = addWidget(&_sample_rate_value);
108         addWidget(&_run_stop_button);
109
110         connect(&_sample_rate_list, SIGNAL(currentIndexChanged(int)),
111                 this, SLOT(on_sample_rate_changed()));
112         connect(&_sample_rate_value, SIGNAL(editingFinished()),
113                 this, SLOT(on_sample_rate_changed()));
114 }
115
116 void SamplingBar::set_device_list(
117         const std::list<struct sr_dev_inst*> &devices)
118 {
119         _device_selector.clear();
120
121         BOOST_FOREACH (sr_dev_inst *sdi, devices) {
122                 QString title;
123                 if (sdi->vendor && sdi->vendor[0])
124                         title += sdi->vendor + QString(" ");
125                 if (sdi->model && sdi->model[0])
126                         title += sdi->model + QString(" ");
127                 if (sdi->version && sdi->version[0])
128                         title += sdi->version + QString(" ");
129
130                 _device_selector.addItem(title, qVariantFromValue(
131                         (void*)sdi));
132         }
133
134         update_sample_rate_selector();
135 }
136
137 struct 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
147 void 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
156 uint64_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
165 void SamplingBar::set_sampling(bool sampling)
166 {
167         _run_stop_button.setIcon(sampling ? _icon_green : _icon_grey);
168         _run_stop_button.setText(sampling ? "Stop" : "Run");
169 }
170
171 void SamplingBar::update_sample_rate_selector()
172 {
173         const sr_dev_inst *const sdi = get_selected_device();
174         const struct sr_samplerates *samplerates;
175
176         assert(_sample_rate_value_action);
177         assert(_sample_rate_list_action);
178
179         if (!sdi)
180                 return;
181
182         if (sr_config_list(sdi->driver, SR_CONF_SAMPLERATE,
183                 (const void **)&samplerates, sdi) != SR_OK)
184                 return;
185
186         _sample_rate_list_action->setVisible(false);
187         _sample_rate_value_action->setVisible(false);
188
189         if (samplerates->step)
190         {
191                 _sample_rate_value.setRange(
192                         samplerates->low, samplerates->high);
193                 _sample_rate_value.setSingleStep(samplerates->step);
194                 _sample_rate_value_action->setVisible(true);
195         }
196         else
197         {
198                 _sample_rate_list.clear();
199                 for (const uint64_t *rate = samplerates->list;
200                      *rate; rate++)
201                         _sample_rate_list.addItem(
202                                 sr_samplerate_string(*rate),
203                                 qVariantFromValue(*rate));
204                 _sample_rate_list.show();
205                 _sample_rate_list_action->setVisible(true);
206         }
207
208         update_sample_rate_selector_value();
209 }
210
211 void SamplingBar::update_sample_rate_selector_value()
212 {
213         sr_dev_inst *const sdi = get_selected_device();
214         assert(sdi);
215
216         uint64_t *samplerate = NULL;
217         if (sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
218                 (const void**)&samplerate, sdi) != SR_OK) {
219                 qDebug() <<
220                                 "WARNING: Failed to get value of sample rate";
221                 return;
222         }
223
224         assert(_sample_rate_value_action);
225         assert(_sample_rate_list_action);
226
227         if (_sample_rate_value_action->isVisible())
228                 _sample_rate_value.setValue(*samplerate);
229         else if (_sample_rate_list_action->isVisible())
230         {
231                 for (int i = 0; i < _sample_rate_list.count(); i++)
232                         if (*samplerate == _sample_rate_list.itemData(
233                                 i).value<uint64_t>())
234                                 _sample_rate_list.setCurrentIndex(i);
235         }
236 }
237
238 void SamplingBar::commit_sample_rate()
239 {
240         uint64_t sample_rate = 0;
241
242         sr_dev_inst *const sdi = get_selected_device();
243         assert(sdi);
244
245         assert(_sample_rate_value_action);
246         assert(_sample_rate_list_action);
247
248         if (_sample_rate_value_action->isVisible())
249                 sample_rate = (uint64_t)_sample_rate_value.value();
250         else if (_sample_rate_list_action->isVisible())
251         {
252                 const int index = _sample_rate_list.currentIndex();
253                 if (index >= 0)
254                         sample_rate = _sample_rate_list.itemData(
255                                 index).value<uint64_t>();
256         }
257
258         // Set the samplerate
259         if (sr_config_set(sdi, SR_CONF_SAMPLERATE,
260                 &sample_rate) != SR_OK) {
261                 qDebug() << "Failed to configure samplerate.";
262                 return;
263         }
264 }
265
266 void SamplingBar::on_device_selected()
267 {
268         update_sample_rate_selector();
269 }
270
271 void SamplingBar::on_sample_rate_changed()
272 {
273         commit_sample_rate();
274 }
275
276 void SamplingBar::configure()
277 {
278         commit_sample_rate();
279
280         sr_dev_inst *const sdi = get_selected_device();
281         assert(sdi);
282
283         pv::dialogs::DeviceOptions dlg(this, sdi);
284         dlg.exec();
285
286         update_sample_rate_selector_value();
287 }
288
289 } // namespace toolbars
290 } // namespace pv