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