]> sigrok.org Git - pulseview.git/blob - pv/samplingbar.cpp
d3b23c0f21a7586c8acc7cd9d43c3aced74d68f0
[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 (!sdi)
181                 return;
182
183         if (sr_config_list(sdi->driver, SR_CONF_SAMPLERATE,
184                 (const void **)&samplerates, sdi) != SR_OK)
185                 return;
186
187         _sample_rate_list_action->setVisible(false);
188         _sample_rate_value_action->setVisible(false);
189
190         if (samplerates->step)
191         {
192                 _sample_rate_value.setRange(
193                         samplerates->low, samplerates->high);
194                 _sample_rate_value.setSingleStep(samplerates->step);
195                 _sample_rate_value_action->setVisible(true);
196         }
197         else
198         {
199                 _sample_rate_list.clear();
200                 for (const uint64_t *rate = samplerates->list;
201                      *rate; rate++)
202                         _sample_rate_list.addItem(
203                                 sr_samplerate_string(*rate),
204                                 qVariantFromValue(*rate));
205                 _sample_rate_list.show();
206                 _sample_rate_list_action->setVisible(true);
207         }
208
209         update_sample_rate_selector_value();
210 }
211
212 void SamplingBar::update_sample_rate_selector_value()
213 {
214         sr_dev_inst *const sdi = get_selected_device();
215         assert(sdi);
216
217         uint64_t *samplerate = NULL;
218         if(sr_config_get(sdi->driver, SR_CONF_SAMPLERATE,
219                 (const void**)&samplerate, sdi) != SR_OK) {
220                 qDebug() <<
221                                 "WARNING: Failed to get value of sample rate";
222                 return;
223         }
224
225         assert(_sample_rate_value_action);
226         assert(_sample_rate_list_action);
227
228         if (_sample_rate_value_action->isVisible())
229                 _sample_rate_value.setValue(*samplerate);
230         else if (_sample_rate_list_action->isVisible())
231         {
232                 for(int i = 0; i < _sample_rate_list.count(); i++)
233                         if(*samplerate == _sample_rate_list.itemData(
234                                 i).value<uint64_t>())
235                                 _sample_rate_list.setCurrentIndex(i);
236         }
237 }
238
239 void SamplingBar::commit_sample_rate()
240 {
241         uint64_t sample_rate = 0;
242
243         sr_dev_inst *const sdi = get_selected_device();
244         assert(sdi);
245
246         assert(_sample_rate_value_action);
247         assert(_sample_rate_list_action);
248
249         if (_sample_rate_value_action->isVisible())
250                 sample_rate = (uint64_t)_sample_rate_value.value();
251         else if (_sample_rate_list_action->isVisible())
252         {
253                 const int index = _sample_rate_list.currentIndex();
254                 if (index >= 0)
255                         sample_rate = _sample_rate_list.itemData(
256                                 index).value<uint64_t>();
257         }
258
259         // Set the samplerate
260         if (sr_config_set(sdi, SR_CONF_SAMPLERATE,
261                 &sample_rate) != SR_OK) {
262                 qDebug() << "Failed to configure samplerate.";
263                 return;
264         }
265 }
266
267 void SamplingBar::on_device_selected()
268 {
269         update_sample_rate_selector();
270 }
271
272 void SamplingBar::on_sample_rate_changed()
273 {
274         commit_sample_rate();
275 }
276
277 void SamplingBar::configure()
278 {
279         commit_sample_rate();
280
281         sr_dev_inst *const sdi = get_selected_device();
282         assert(sdi);
283
284         pv::dialogs::DeviceOptions dlg(this, sdi);
285         dlg.exec();
286
287         update_sample_rate_selector_value();
288 }
289
290 } // namespace pv