]> sigrok.org Git - pulseview.git/blame - pv/samplingbar.cpp
global: Do not assert >= 0 for unsigned values
[pulseview.git] / pv / samplingbar.cpp
CommitLineData
d4984fe7 1/*
b3f22de0 2 * This file is part of the PulseView project.
d4984fe7
JH
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
dde1a563
JH
21#include <assert.h>
22
215f9499
JH
23#include <boost/foreach.hpp>
24
6fb67b27
JH
25extern "C" {
26#include <libsigrok/libsigrok.h>
27}
28
dde1a563
JH
29#include <QAction>
30
d4984fe7
JH
31#include "samplingbar.h"
32
cdb50f67
JH
33#include <pv/dialogs/hwcap.h>
34
51e77110
JH
35namespace pv {
36
215f9499
JH
37const uint64_t SamplingBar::RecordLengths[11] = {
38 1000000,
39 2000000,
40 5000000,
41 10000000,
42 25000000,
43 50000000,
44 100000000,
45 250000000,
46 500000000,
47 1000000000,
48 10000000000
49};
50
d4984fe7 51SamplingBar::SamplingBar(QWidget *parent) :
6fb67b27 52 QToolBar("Sampling Bar", parent),
dde1a563 53 _device_selector(this),
cdb50f67 54 _configure_button(this),
215f9499 55 _record_length_selector(this),
274d4f13 56 _sample_rate_list(this),
f5798068
JH
57 _icon_green(":/icons/status-green.svg"),
58 _icon_grey(":/icons/status-grey.svg"),
274d4f13 59 _run_stop_button(this)
6fb67b27 60{
cdb50f67
JH
61 connect(&_run_stop_button, SIGNAL(clicked()),
62 this, SIGNAL(run_stop()));
dde1a563
JH
63 connect(&_device_selector, SIGNAL(currentIndexChanged (int)),
64 this, SLOT(on_device_selected()));
cdb50f67
JH
65 connect(&_configure_button, SIGNAL(clicked()),
66 this, SLOT(configure()));
dde1a563
JH
67
68 _sample_rate_value.setDecimals(0);
69 _sample_rate_value.setSuffix("Hz");
70
215f9499
JH
71 BOOST_FOREACH(uint64_t l, RecordLengths)
72 {
73 char *const text = sr_si_string_u64(l, " samples");
74 _record_length_selector.addItem(QString(text),
75 qVariantFromValue(l));
76 g_free(text);
77 }
78
6ac96c2e 79 set_sampling(false);
274d4f13 80
688ef645
JH
81 _configure_button.setIcon(QIcon::fromTheme("configure",
82 QIcon(":/icons/configure.png")));
cdb50f67 83
f5798068
JH
84 _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon);
85
6fb67b27 86 addWidget(&_device_selector);
cdb50f67 87 addWidget(&_configure_button);
215f9499 88 addWidget(&_record_length_selector);
dde1a563
JH
89 _sample_rate_list_action = addWidget(&_sample_rate_list);
90 _sample_rate_value_action = addWidget(&_sample_rate_value);
274d4f13 91 addWidget(&_run_stop_button);
6fb67b27
JH
92
93 update_device_selector();
dde1a563 94 update_sample_rate_selector();
6fb67b27
JH
95}
96
97struct sr_dev_inst* SamplingBar::get_selected_device() const
d4984fe7 98{
6fb67b27 99 const int index = _device_selector.currentIndex();
333d5bbc 100 if (index < 0)
6fb67b27
JH
101 return NULL;
102
103 return (sr_dev_inst*)_device_selector.itemData(
104 index).value<void*>();
105}
106
215f9499
JH
107uint64_t SamplingBar::get_record_length() const
108{
109 const int index = _record_length_selector.currentIndex();
333d5bbc 110 if (index < 0)
215f9499
JH
111 return 0;
112
113 return _record_length_selector.itemData(index).value<uint64_t>();
114}
115
dde1a563
JH
116uint64_t SamplingBar::get_sample_rate() const
117{
118 assert(_sample_rate_value_action);
119 assert(_sample_rate_list_action);
120
333d5bbc 121 if (_sample_rate_value_action->isVisible())
dde1a563 122 return (uint64_t)_sample_rate_value.value();
333d5bbc 123 else if (_sample_rate_list_action->isVisible())
dde1a563 124 {
f6be4120 125 const int index = _sample_rate_list.currentIndex();
333d5bbc 126 if (index < 0)
dde1a563
JH
127 return 0;
128
f6be4120 129 return _sample_rate_list.itemData(index).value<uint64_t>();
dde1a563
JH
130 }
131
132 return 0;
133}
134
6ac96c2e
JH
135void SamplingBar::set_sampling(bool sampling)
136{
f5798068 137 _run_stop_button.setIcon(sampling ? _icon_green : _icon_grey);
6ac96c2e
JH
138 _run_stop_button.setText(sampling ? "Stop" : "Run");
139}
140
6fb67b27
JH
141void 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);
d4984fe7 170}
dde1a563
JH
171
172void 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_info_get(sdi->driver, SR_DI_SAMPLERATES,
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
207void SamplingBar::on_device_selected()
208{
209 update_sample_rate_selector();
210}
51e77110 211
cdb50f67
JH
212void SamplingBar::configure()
213{
214 sr_dev_inst *const sdi = get_selected_device();
215 assert(sdi);
216
217 pv::dialogs::HwCap dlg(this, sdi);
218 dlg.exec();
219}
220
51e77110 221} // namespace pv