]>
Commit | Line | Data |
---|---|---|
cf47ef78 JH |
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 | ||
7b3810db JH |
21 | #include <boost/bind.hpp> |
22 | ||
d71bca8b | 23 | #include <QDebug> |
9f6af8bd | 24 | #include <QObject> |
d71bca8b | 25 | |
3820592a | 26 | #include "deviceoptions.h" |
cf47ef78 | 27 | |
9f6af8bd | 28 | #include <pv/prop/double.h> |
7b3810db JH |
29 | #include <pv/prop/enum.h> |
30 | ||
31 | using namespace boost; | |
32 | using namespace std; | |
33 | ||
cf47ef78 JH |
34 | namespace pv { |
35 | namespace prop { | |
36 | namespace binding { | |
37 | ||
3820592a | 38 | DeviceOptions::DeviceOptions(struct sr_dev_inst *sdi) : |
cf47ef78 JH |
39 | _sdi(sdi) |
40 | { | |
10b1be92 | 41 | const int *options; |
7b3810db | 42 | |
10b1be92 JH |
43 | if ((sr_config_list(sdi->driver, SR_CONF_DEVICE_OPTIONS, |
44 | (const void **)&options, sdi) != SR_OK) || !options) | |
7b3810db JH |
45 | /* Driver supports no device instance options. */ |
46 | return; | |
47 | ||
10b1be92 JH |
48 | for (int cap = 0; options[cap]; cap++) { |
49 | const struct sr_config_info *const info = | |
50 | sr_config_info_get(options[cap]); | |
7b3810db | 51 | |
10b1be92 | 52 | if (!info) |
7b3810db JH |
53 | continue; |
54 | ||
10b1be92 | 55 | switch(info->key) |
7b3810db | 56 | { |
9f6af8bd JH |
57 | case SR_CONF_SAMPLERATE: |
58 | bind_samplerate(info); | |
59 | break; | |
60 | ||
10b1be92 JH |
61 | case SR_CONF_PATTERN_MODE: |
62 | bind_stropt(info, SR_CONF_PATTERN_MODE); | |
7b3810db JH |
63 | break; |
64 | ||
10b1be92 JH |
65 | case SR_CONF_BUFFERSIZE: |
66 | bind_buffer_size(info); | |
7b3810db JH |
67 | break; |
68 | ||
10b1be92 JH |
69 | case SR_CONF_TIMEBASE: |
70 | bind_time_base(info); | |
7b3810db JH |
71 | break; |
72 | ||
10b1be92 JH |
73 | case SR_CONF_TRIGGER_SOURCE: |
74 | bind_stropt(info, SR_CONF_TRIGGER_SOURCE); | |
7b3810db JH |
75 | break; |
76 | ||
10b1be92 JH |
77 | case SR_CONF_FILTER: |
78 | bind_stropt(info, SR_CONF_FILTER); | |
7b3810db JH |
79 | break; |
80 | ||
10b1be92 JH |
81 | case SR_CONF_VDIV: |
82 | bind_vdiv(info); | |
7b3810db JH |
83 | break; |
84 | ||
10b1be92 JH |
85 | case SR_CONF_COUPLING: |
86 | bind_stropt(info, SR_CONF_FILTER); | |
7b3810db JH |
87 | break; |
88 | } | |
89 | } | |
90 | } | |
91 | ||
3820592a | 92 | void DeviceOptions::expose_enum(const struct sr_config_info *info, |
10b1be92 | 93 | const vector< pair<const void*, QString> > &values, int key) |
7b3810db JH |
94 | { |
95 | _properties.push_back(shared_ptr<Property>( | |
10b1be92 | 96 | new Enum(QString(info->name), values, |
9f6af8bd | 97 | bind(enum_getter, _sdi, key), |
10b1be92 | 98 | bind(sr_config_set, _sdi, key, _1)))); |
7b3810db JH |
99 | } |
100 | ||
3820592a JH |
101 | void DeviceOptions::bind_stropt( |
102 | const struct sr_config_info *info, int key) | |
7b3810db JH |
103 | { |
104 | const char **stropts; | |
10b1be92 | 105 | if (sr_config_list(_sdi->driver, key, |
7b3810db JH |
106 | (const void **)&stropts, _sdi) != SR_OK) |
107 | return; | |
108 | ||
109 | vector< pair<const void*, QString> > values; | |
110 | for (int i = 0; stropts[i]; i++) | |
111 | values.push_back(make_pair(stropts[i], stropts[i])); | |
112 | ||
10b1be92 | 113 | expose_enum(info, values, key); |
7b3810db JH |
114 | } |
115 | ||
9f6af8bd JH |
116 | void DeviceOptions::bind_samplerate(const struct sr_config_info *info) |
117 | { | |
118 | const struct sr_samplerates *samplerates; | |
119 | ||
120 | if (sr_config_list(_sdi->driver, SR_CONF_SAMPLERATE, | |
121 | (const void **)&samplerates, _sdi) != SR_OK) | |
122 | return; | |
123 | ||
124 | if (samplerates->step) { | |
125 | _properties.push_back(shared_ptr<Property>( | |
126 | new Double(QString(info->name), | |
127 | 0, QObject::tr("Hz"), | |
128 | make_pair((double)samplerates->low, | |
129 | (double)samplerates->high), | |
130 | (double)samplerates->step, | |
131 | bind(samplerate_value_getter, _sdi), | |
132 | bind(samplerate_value_setter, _sdi, _1)))); | |
133 | } else { | |
134 | vector< pair<const void*, QString> > values; | |
135 | for (const uint64_t *rate = samplerates->list; | |
136 | *rate; rate++) | |
137 | values.push_back(make_pair( | |
138 | (const void*)rate, | |
139 | QString(sr_samplerate_string(*rate)))); | |
140 | ||
141 | _properties.push_back(shared_ptr<Property>( | |
142 | new Enum(QString(info->name), values, | |
143 | bind(samplerate_list_getter, _sdi), | |
144 | bind(samplerate_list_setter, _sdi, _1)))); | |
145 | } | |
146 | } | |
147 | ||
3820592a | 148 | void DeviceOptions::bind_buffer_size(const struct sr_config_info *info) |
7b3810db JH |
149 | { |
150 | const uint64_t *sizes; | |
10b1be92 | 151 | if (sr_config_list(_sdi->driver, SR_CONF_BUFFERSIZE, |
7b3810db JH |
152 | (const void **)&sizes, _sdi) != SR_OK) |
153 | return; | |
154 | ||
155 | vector< pair<const void*, QString> > values; | |
156 | for (int i = 0; sizes[i]; i++) | |
157 | values.push_back(make_pair(sizes + i, | |
158 | QString("%1").arg(sizes[i]))); | |
159 | ||
10b1be92 | 160 | expose_enum(info, values, SR_CONF_BUFFERSIZE); |
7b3810db JH |
161 | } |
162 | ||
3820592a | 163 | void DeviceOptions::bind_time_base(const struct sr_config_info *info) |
7b3810db JH |
164 | { |
165 | struct sr_rational *timebases; | |
10b1be92 | 166 | if (sr_config_list(_sdi->driver, SR_CONF_TIMEBASE, |
7b3810db JH |
167 | (const void **)&timebases, _sdi) != SR_OK) |
168 | return; | |
169 | ||
170 | vector< pair<const void*, QString> > values; | |
171 | for (int i = 0; timebases[i].p && timebases[i].q; i++) | |
172 | values.push_back(make_pair(timebases + i, | |
173 | QString(sr_period_string( | |
174 | timebases[i].p * timebases[i].q)))); | |
175 | ||
10b1be92 | 176 | expose_enum(info, values, SR_CONF_TIMEBASE); |
7b3810db JH |
177 | } |
178 | ||
3820592a | 179 | void DeviceOptions::bind_vdiv(const struct sr_config_info *info) |
7b3810db JH |
180 | { |
181 | struct sr_rational *vdivs; | |
10b1be92 | 182 | if (sr_config_list(_sdi->driver, SR_CONF_VDIV, |
7b3810db JH |
183 | (const void **)&vdivs, _sdi) != SR_OK) |
184 | return; | |
185 | ||
186 | vector< pair<const void*, QString> > values; | |
187 | for (int i = 0; vdivs[i].p && vdivs[i].q; i++) | |
188 | values.push_back(make_pair(vdivs + i, | |
189 | QString(sr_voltage_string(vdivs + i)))); | |
190 | ||
10b1be92 | 191 | expose_enum(info, values, SR_CONF_VDIV); |
cf47ef78 JH |
192 | } |
193 | ||
d71bca8b JH |
194 | const void* DeviceOptions::enum_getter( |
195 | const struct sr_dev_inst *sdi, int key) | |
196 | { | |
197 | const void *data = NULL; | |
793f8096 | 198 | if (sr_config_get(sdi->driver, key, &data, sdi) != SR_OK) { |
d71bca8b JH |
199 | qDebug() << |
200 | "WARNING: Failed to get value of config id" << key; | |
201 | return NULL; | |
202 | } | |
203 | return data; | |
204 | } | |
205 | ||
9f6af8bd JH |
206 | double DeviceOptions::samplerate_value_getter( |
207 | const struct sr_dev_inst *sdi) | |
208 | { | |
209 | uint64_t *samplerate = NULL; | |
793f8096 | 210 | if (sr_config_get(sdi->driver, SR_CONF_SAMPLERATE, |
9f6af8bd JH |
211 | (const void**)&samplerate, sdi) != SR_OK) { |
212 | qDebug() << | |
213 | "WARNING: Failed to get value of sample rate"; | |
214 | return 0.0; | |
215 | } | |
216 | return (double)*samplerate; | |
217 | } | |
218 | ||
219 | void DeviceOptions::samplerate_value_setter( | |
220 | struct sr_dev_inst *sdi, double value) | |
221 | { | |
222 | uint64_t samplerate = value; | |
793f8096 | 223 | if (sr_config_set(sdi, SR_CONF_SAMPLERATE, |
9f6af8bd JH |
224 | &samplerate) != SR_OK) |
225 | qDebug() << | |
226 | "WARNING: Failed to set value of sample rate"; | |
227 | } | |
228 | ||
229 | const void* DeviceOptions::samplerate_list_getter( | |
230 | const struct sr_dev_inst *sdi) | |
231 | { | |
232 | const struct sr_samplerates *samplerates; | |
233 | uint64_t *samplerate = NULL; | |
234 | ||
235 | if (sr_config_list(sdi->driver, SR_CONF_SAMPLERATE, | |
236 | (const void **)&samplerates, sdi) != SR_OK) { | |
237 | qDebug() << | |
238 | "WARNING: Failed to get enumerate sample rates"; | |
239 | return NULL; | |
240 | } | |
241 | ||
793f8096 | 242 | if (sr_config_get(sdi->driver, SR_CONF_SAMPLERATE, |
9f6af8bd JH |
243 | (const void**)&samplerate, sdi) != SR_OK || |
244 | !samplerate) { | |
245 | qDebug() << | |
246 | "WARNING: Failed to get value of sample rate"; | |
247 | return NULL; | |
248 | } | |
249 | ||
250 | for (const uint64_t *rate = samplerates->list; *rate; rate++) | |
793f8096 | 251 | if (*rate == *samplerate) |
9f6af8bd JH |
252 | return (const void*)rate; |
253 | ||
254 | return NULL; | |
255 | } | |
256 | ||
257 | void DeviceOptions::samplerate_list_setter( | |
258 | struct sr_dev_inst *sdi, const void *value) | |
259 | { | |
260 | if (sr_config_set(sdi, SR_CONF_SAMPLERATE, | |
261 | (uint64_t*)value) != SR_OK) | |
262 | qDebug() << | |
263 | "WARNING: Failed to set value of sample rate"; | |
264 | } | |
265 | ||
cf47ef78 JH |
266 | } // binding |
267 | } // prop | |
268 | } // pv | |
269 |