]>
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 JH |
25 | extern "C" { |
26 | #include <libsigrok/libsigrok.h> | |
27 | } | |
28 | ||
dde1a563 JH |
29 | #include <QAction> |
30 | ||
d4984fe7 JH |
31 | #include "samplingbar.h" |
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, | |
46 | 10000000000 | |
47 | }; | |
48 | ||
d4984fe7 | 49 | SamplingBar::SamplingBar(QWidget *parent) : |
6fb67b27 | 50 | QToolBar("Sampling Bar", parent), |
dde1a563 | 51 | _device_selector(this), |
215f9499 | 52 | _record_length_selector(this), |
274d4f13 JH |
53 | _sample_rate_list(this), |
54 | _run_stop_button(this) | |
6fb67b27 | 55 | { |
274d4f13 | 56 | connect(&_run_stop_button, SIGNAL(clicked()), this, SIGNAL(run_stop())); |
dde1a563 JH |
57 | connect(&_device_selector, SIGNAL(currentIndexChanged (int)), |
58 | this, SLOT(on_device_selected())); | |
59 | ||
60 | _sample_rate_value.setDecimals(0); | |
61 | _sample_rate_value.setSuffix("Hz"); | |
62 | ||
215f9499 JH |
63 | BOOST_FOREACH(uint64_t l, RecordLengths) |
64 | { | |
65 | char *const text = sr_si_string_u64(l, " samples"); | |
66 | _record_length_selector.addItem(QString(text), | |
67 | qVariantFromValue(l)); | |
68 | g_free(text); | |
69 | } | |
70 | ||
6ac96c2e | 71 | set_sampling(false); |
274d4f13 | 72 | |
6fb67b27 | 73 | addWidget(&_device_selector); |
215f9499 | 74 | addWidget(&_record_length_selector); |
dde1a563 JH |
75 | _sample_rate_list_action = addWidget(&_sample_rate_list); |
76 | _sample_rate_value_action = addWidget(&_sample_rate_value); | |
274d4f13 | 77 | addWidget(&_run_stop_button); |
6fb67b27 JH |
78 | |
79 | update_device_selector(); | |
dde1a563 | 80 | update_sample_rate_selector(); |
6fb67b27 JH |
81 | } |
82 | ||
83 | struct sr_dev_inst* SamplingBar::get_selected_device() const | |
d4984fe7 | 84 | { |
6fb67b27 JH |
85 | const int index = _device_selector.currentIndex(); |
86 | if(index < 0) | |
87 | return NULL; | |
88 | ||
89 | return (sr_dev_inst*)_device_selector.itemData( | |
90 | index).value<void*>(); | |
91 | } | |
92 | ||
215f9499 JH |
93 | uint64_t SamplingBar::get_record_length() const |
94 | { | |
95 | const int index = _record_length_selector.currentIndex(); | |
96 | if(index < 0) | |
97 | return 0; | |
98 | ||
99 | return _record_length_selector.itemData(index).value<uint64_t>(); | |
100 | } | |
101 | ||
dde1a563 JH |
102 | uint64_t SamplingBar::get_sample_rate() const |
103 | { | |
104 | assert(_sample_rate_value_action); | |
105 | assert(_sample_rate_list_action); | |
106 | ||
107 | if(_sample_rate_value_action->isVisible()) | |
108 | return (uint64_t)_sample_rate_value.value(); | |
109 | else if(_sample_rate_list_action->isVisible()) | |
110 | { | |
f6be4120 | 111 | const int index = _sample_rate_list.currentIndex(); |
dde1a563 JH |
112 | if(index < 0) |
113 | return 0; | |
114 | ||
f6be4120 | 115 | return _sample_rate_list.itemData(index).value<uint64_t>(); |
dde1a563 JH |
116 | } |
117 | ||
118 | return 0; | |
119 | } | |
120 | ||
6ac96c2e JH |
121 | void SamplingBar::set_sampling(bool sampling) |
122 | { | |
123 | _run_stop_button.setText(sampling ? "Stop" : "Run"); | |
124 | } | |
125 | ||
6fb67b27 JH |
126 | void SamplingBar::update_device_selector() |
127 | { | |
128 | GSList *devices = NULL; | |
129 | ||
130 | /* Scan all drivers for all devices. */ | |
131 | struct sr_dev_driver **const drivers = sr_driver_list(); | |
132 | for (struct sr_dev_driver **driver = drivers; *driver; driver++) { | |
133 | GSList *tmpdevs = sr_driver_scan(*driver, NULL); | |
134 | for (GSList *l = tmpdevs; l; l = l->next) | |
135 | devices = g_slist_append(devices, l->data); | |
136 | g_slist_free(tmpdevs); | |
137 | } | |
138 | ||
139 | for (GSList *l = devices; l; l = l->next) { | |
140 | sr_dev_inst *const sdi = (sr_dev_inst*)l->data; | |
141 | ||
142 | QString title; | |
143 | if (sdi->vendor && sdi->vendor[0]) | |
144 | title += sdi->vendor + QString(" "); | |
145 | if (sdi->model && sdi->model[0]) | |
146 | title += sdi->model + QString(" "); | |
147 | if (sdi->version && sdi->version[0]) | |
148 | title += sdi->version + QString(" "); | |
149 | ||
150 | _device_selector.addItem(title, qVariantFromValue( | |
151 | (void*)sdi)); | |
152 | } | |
153 | ||
154 | g_slist_free(devices); | |
d4984fe7 | 155 | } |
dde1a563 JH |
156 | |
157 | void SamplingBar::update_sample_rate_selector() | |
158 | { | |
159 | const sr_dev_inst *const sdi = get_selected_device(); | |
160 | const struct sr_samplerates *samplerates; | |
161 | ||
162 | assert(_sample_rate_value_action); | |
163 | assert(_sample_rate_list_action); | |
164 | ||
165 | if (sr_info_get(sdi->driver, SR_DI_SAMPLERATES, | |
166 | (const void **)&samplerates, sdi) != SR_OK) | |
167 | return; | |
168 | ||
169 | _sample_rate_list_action->setVisible(false); | |
170 | _sample_rate_value_action->setVisible(false); | |
171 | ||
172 | if (samplerates->step) | |
173 | { | |
174 | _sample_rate_value.setRange( | |
175 | samplerates->low, samplerates->high); | |
176 | _sample_rate_value.setSingleStep(samplerates->step); | |
177 | _sample_rate_value_action->setVisible(true); | |
178 | } | |
179 | else | |
180 | { | |
181 | _sample_rate_list.clear(); | |
182 | for (const uint64_t *rate = samplerates->list; | |
183 | *rate; rate++) | |
184 | _sample_rate_list.addItem( | |
185 | sr_samplerate_string(*rate), | |
186 | qVariantFromValue(*rate)); | |
187 | _sample_rate_list.show(); | |
188 | _sample_rate_list_action->setVisible(true); | |
189 | } | |
190 | } | |
191 | ||
192 | void SamplingBar::on_device_selected() | |
193 | { | |
194 | update_sample_rate_selector(); | |
195 | } | |
51e77110 JH |
196 | |
197 | } // namespace pv |