]>
Commit | Line | Data |
---|---|---|
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 | 25 | #include <libsigrok/libsigrok.h> |
6fb67b27 | 26 | |
dde1a563 JH |
27 | #include <QAction> |
28 | ||
d4984fe7 JH |
29 | #include "samplingbar.h" |
30 | ||
d755562a | 31 | #include <pv/dialogs/deviceoptions.h> |
cdb50f67 | 32 | |
51e77110 JH |
33 | namespace pv { |
34 | ||
215f9499 JH |
35 | const uint64_t SamplingBar::RecordLengths[11] = { |
36 | 1000000, | |
37 | 2000000, | |
38 | 5000000, | |
39 | 10000000, | |
40 | 25000000, | |
41 | 50000000, | |
42 | 100000000, | |
43 | 250000000, | |
44 | 500000000, | |
45 | 1000000000, | |
d2aad781 | 46 | 10000000000ULL, |
215f9499 JH |
47 | }; |
48 | ||
d4984fe7 | 49 | SamplingBar::SamplingBar(QWidget *parent) : |
6fb67b27 | 50 | QToolBar("Sampling Bar", parent), |
dde1a563 | 51 | _device_selector(this), |
cdb50f67 | 52 | _configure_button(this), |
215f9499 | 53 | _record_length_selector(this), |
274d4f13 | 54 | _sample_rate_list(this), |
f5798068 JH |
55 | _icon_green(":/icons/status-green.svg"), |
56 | _icon_grey(":/icons/status-grey.svg"), | |
274d4f13 | 57 | _run_stop_button(this) |
6fb67b27 | 58 | { |
cdb50f67 JH |
59 | connect(&_run_stop_button, SIGNAL(clicked()), |
60 | this, SIGNAL(run_stop())); | |
dde1a563 JH |
61 | connect(&_device_selector, SIGNAL(currentIndexChanged (int)), |
62 | this, SLOT(on_device_selected())); | |
cdb50f67 JH |
63 | connect(&_configure_button, SIGNAL(clicked()), |
64 | this, SLOT(configure())); | |
dde1a563 JH |
65 | |
66 | _sample_rate_value.setDecimals(0); | |
67 | _sample_rate_value.setSuffix("Hz"); | |
68 | ||
215f9499 JH |
69 | BOOST_FOREACH(uint64_t l, RecordLengths) |
70 | { | |
71 | char *const text = sr_si_string_u64(l, " samples"); | |
72 | _record_length_selector.addItem(QString(text), | |
73 | qVariantFromValue(l)); | |
74 | g_free(text); | |
75 | } | |
76 | ||
6ac96c2e | 77 | set_sampling(false); |
274d4f13 | 78 | |
688ef645 JH |
79 | _configure_button.setIcon(QIcon::fromTheme("configure", |
80 | QIcon(":/icons/configure.png"))); | |
cdb50f67 | 81 | |
f5798068 JH |
82 | _run_stop_button.setToolButtonStyle(Qt::ToolButtonTextBesideIcon); |
83 | ||
6fb67b27 | 84 | addWidget(&_device_selector); |
cdb50f67 | 85 | addWidget(&_configure_button); |
215f9499 | 86 | addWidget(&_record_length_selector); |
dde1a563 JH |
87 | _sample_rate_list_action = addWidget(&_sample_rate_list); |
88 | _sample_rate_value_action = addWidget(&_sample_rate_value); | |
274d4f13 | 89 | addWidget(&_run_stop_button); |
6fb67b27 JH |
90 | |
91 | update_device_selector(); | |
dde1a563 | 92 | update_sample_rate_selector(); |
6fb67b27 JH |
93 | } |
94 | ||
95 | struct sr_dev_inst* SamplingBar::get_selected_device() const | |
d4984fe7 | 96 | { |
6fb67b27 | 97 | const int index = _device_selector.currentIndex(); |
333d5bbc | 98 | if (index < 0) |
6fb67b27 JH |
99 | return NULL; |
100 | ||
101 | return (sr_dev_inst*)_device_selector.itemData( | |
102 | index).value<void*>(); | |
103 | } | |
104 | ||
215f9499 JH |
105 | uint64_t SamplingBar::get_record_length() const |
106 | { | |
107 | const int index = _record_length_selector.currentIndex(); | |
333d5bbc | 108 | if (index < 0) |
215f9499 JH |
109 | return 0; |
110 | ||
111 | return _record_length_selector.itemData(index).value<uint64_t>(); | |
112 | } | |
113 | ||
dde1a563 JH |
114 | uint64_t SamplingBar::get_sample_rate() const |
115 | { | |
116 | assert(_sample_rate_value_action); | |
117 | assert(_sample_rate_list_action); | |
118 | ||
333d5bbc | 119 | if (_sample_rate_value_action->isVisible()) |
dde1a563 | 120 | return (uint64_t)_sample_rate_value.value(); |
333d5bbc | 121 | else if (_sample_rate_list_action->isVisible()) |
dde1a563 | 122 | { |
f6be4120 | 123 | const int index = _sample_rate_list.currentIndex(); |
333d5bbc | 124 | if (index < 0) |
dde1a563 JH |
125 | return 0; |
126 | ||
f6be4120 | 127 | return _sample_rate_list.itemData(index).value<uint64_t>(); |
dde1a563 JH |
128 | } |
129 | ||
130 | return 0; | |
131 | } | |
132 | ||
6ac96c2e JH |
133 | void SamplingBar::set_sampling(bool sampling) |
134 | { | |
f5798068 | 135 | _run_stop_button.setIcon(sampling ? _icon_green : _icon_grey); |
6ac96c2e JH |
136 | _run_stop_button.setText(sampling ? "Stop" : "Run"); |
137 | } | |
138 | ||
6fb67b27 JH |
139 | void SamplingBar::update_device_selector() |
140 | { | |
141 | GSList *devices = NULL; | |
142 | ||
143 | /* Scan all drivers for all devices. */ | |
144 | struct sr_dev_driver **const drivers = sr_driver_list(); | |
145 | for (struct sr_dev_driver **driver = drivers; *driver; driver++) { | |
146 | GSList *tmpdevs = sr_driver_scan(*driver, NULL); | |
147 | for (GSList *l = tmpdevs; l; l = l->next) | |
148 | devices = g_slist_append(devices, l->data); | |
149 | g_slist_free(tmpdevs); | |
150 | } | |
151 | ||
152 | for (GSList *l = devices; l; l = l->next) { | |
153 | sr_dev_inst *const sdi = (sr_dev_inst*)l->data; | |
154 | ||
155 | QString title; | |
156 | if (sdi->vendor && sdi->vendor[0]) | |
157 | title += sdi->vendor + QString(" "); | |
158 | if (sdi->model && sdi->model[0]) | |
159 | title += sdi->model + QString(" "); | |
160 | if (sdi->version && sdi->version[0]) | |
161 | title += sdi->version + QString(" "); | |
162 | ||
163 | _device_selector.addItem(title, qVariantFromValue( | |
164 | (void*)sdi)); | |
165 | } | |
166 | ||
167 | g_slist_free(devices); | |
d4984fe7 | 168 | } |
dde1a563 JH |
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 | ||
c0be28da | 178 | if (sr_config_list(sdi->driver, SR_CONF_SAMPLERATE, |
dde1a563 JH |
179 | (const void **)&samplerates, sdi) != SR_OK) |
180 | return; | |
181 | ||
182 | _sample_rate_list_action->setVisible(false); | |
183 | _sample_rate_value_action->setVisible(false); | |
184 | ||
185 | if (samplerates->step) | |
186 | { | |
187 | _sample_rate_value.setRange( | |
188 | samplerates->low, samplerates->high); | |
189 | _sample_rate_value.setSingleStep(samplerates->step); | |
190 | _sample_rate_value_action->setVisible(true); | |
191 | } | |
192 | else | |
193 | { | |
194 | _sample_rate_list.clear(); | |
195 | for (const uint64_t *rate = samplerates->list; | |
196 | *rate; rate++) | |
197 | _sample_rate_list.addItem( | |
198 | sr_samplerate_string(*rate), | |
199 | qVariantFromValue(*rate)); | |
200 | _sample_rate_list.show(); | |
201 | _sample_rate_list_action->setVisible(true); | |
202 | } | |
203 | } | |
204 | ||
205 | void SamplingBar::on_device_selected() | |
206 | { | |
207 | update_sample_rate_selector(); | |
208 | } | |
51e77110 | 209 | |
cdb50f67 JH |
210 | void SamplingBar::configure() |
211 | { | |
212 | sr_dev_inst *const sdi = get_selected_device(); | |
213 | assert(sdi); | |
214 | ||
d755562a | 215 | pv::dialogs::DeviceOptions dlg(this, sdi); |
cdb50f67 JH |
216 | dlg.exec(); |
217 | } | |
218 | ||
51e77110 | 219 | } // namespace pv |