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