]>
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 <boost/bind.hpp> | |
22 | ||
23 | #include <QDebug> | |
24 | #include <QObject> | |
25 | ||
26 | #include <stdint.h> | |
27 | ||
28 | #include "deviceoptions.h" | |
29 | ||
30 | #include <pv/devinst.h> | |
31 | #include <pv/prop/bool.h> | |
32 | #include <pv/prop/double.h> | |
33 | #include <pv/prop/enum.h> | |
34 | #include <pv/prop/int.h> | |
35 | ||
36 | #include <libsigrok/libsigrok.h> | |
37 | ||
38 | using boost::bind; | |
39 | using boost::function; | |
40 | using boost::optional; | |
41 | using boost::shared_ptr; | |
42 | using std::make_pair; | |
43 | using std::pair; | |
44 | using std::string; | |
45 | using std::vector; | |
46 | ||
47 | namespace pv { | |
48 | namespace prop { | |
49 | namespace binding { | |
50 | ||
51 | DeviceOptions::DeviceOptions(shared_ptr<pv::DevInst> dev_inst, | |
52 | const sr_probe_group *group) : | |
53 | _dev_inst(dev_inst), | |
54 | _group(group) | |
55 | { | |
56 | assert(dev_inst); | |
57 | sr_dev_inst *const sdi = dev_inst->dev_inst(); | |
58 | assert(sdi); | |
59 | ||
60 | GVariant *gvar_opts, *gvar_list; | |
61 | gsize num_opts; | |
62 | ||
63 | if ((sr_config_list(sdi->driver, sdi, group, SR_CONF_DEVICE_OPTIONS, | |
64 | &gvar_opts) != SR_OK)) | |
65 | /* Driver supports no device instance options. */ | |
66 | return; | |
67 | ||
68 | const int *const options = (const int32_t *)g_variant_get_fixed_array( | |
69 | gvar_opts, &num_opts, sizeof(int32_t)); | |
70 | for (unsigned int i = 0; i < num_opts; i++) { | |
71 | const struct sr_config_info *const info = | |
72 | sr_config_info_get(options[i]); | |
73 | ||
74 | if (!info) | |
75 | continue; | |
76 | ||
77 | const int key = info->key; | |
78 | ||
79 | if (sr_config_list(sdi->driver, sdi, group, | |
80 | key, &gvar_list) != SR_OK) | |
81 | gvar_list = NULL; | |
82 | ||
83 | const QString name = QString::fromUtf8(info->name); | |
84 | ||
85 | switch(key) | |
86 | { | |
87 | case SR_CONF_SAMPLERATE: | |
88 | // Sample rate values are not bound because they are shown | |
89 | // in the SamplingBar | |
90 | break; | |
91 | ||
92 | case SR_CONF_CAPTURE_RATIO: | |
93 | bind_int(name, key, "%", pair<int64_t, int64_t>(0, 100)); | |
94 | break; | |
95 | ||
96 | case SR_CONF_PATTERN_MODE: | |
97 | case SR_CONF_BUFFERSIZE: | |
98 | case SR_CONF_TRIGGER_SOURCE: | |
99 | case SR_CONF_TRIGGER_SLOPE: | |
100 | case SR_CONF_FILTER: | |
101 | case SR_CONF_COUPLING: | |
102 | case SR_CONF_CLOCK_EDGE: | |
103 | bind_enum(name, key, gvar_list); | |
104 | break; | |
105 | ||
106 | case SR_CONF_EXTERNAL_CLOCK: | |
107 | case SR_CONF_RLE: | |
108 | bind_bool(name, key); | |
109 | break; | |
110 | ||
111 | case SR_CONF_TIMEBASE: | |
112 | bind_enum(name, key, gvar_list, print_timebase); | |
113 | break; | |
114 | ||
115 | case SR_CONF_VDIV: | |
116 | bind_enum(name, key, gvar_list, print_vdiv); | |
117 | break; | |
118 | ||
119 | case SR_CONF_VOLTAGE_THRESHOLD: | |
120 | bind_enum(name, key, gvar_list, print_voltage_threshold); | |
121 | break; | |
122 | } | |
123 | ||
124 | if (gvar_list) | |
125 | g_variant_unref(gvar_list); | |
126 | } | |
127 | g_variant_unref(gvar_opts); | |
128 | } | |
129 | ||
130 | GVariant* DeviceOptions::config_getter( | |
131 | const sr_dev_inst *sdi, const sr_probe_group *group, int key) | |
132 | { | |
133 | GVariant *data = NULL; | |
134 | if (sr_config_get(sdi->driver, sdi, group, key, &data) != SR_OK) { | |
135 | qDebug() << | |
136 | "WARNING: Failed to get value of config id" << key; | |
137 | return NULL; | |
138 | } | |
139 | return data; | |
140 | } | |
141 | ||
142 | void DeviceOptions::config_setter( | |
143 | const struct sr_dev_inst *sdi, const sr_probe_group *group, int key, | |
144 | GVariant* value) | |
145 | { | |
146 | if (sr_config_set(sdi, group, key, value) != SR_OK) | |
147 | qDebug() << "WARNING: Failed to set value of sample rate"; | |
148 | } | |
149 | ||
150 | void DeviceOptions::bind_bool(const QString &name, int key) | |
151 | { | |
152 | sr_dev_inst *const sdi = _dev_inst->dev_inst(); | |
153 | assert(sdi); | |
154 | ||
155 | _properties.push_back(shared_ptr<Property>( | |
156 | new Bool(name, bind(config_getter, sdi, _group, key), | |
157 | bind(config_setter, sdi, _group, key, _1)))); | |
158 | } | |
159 | ||
160 | void DeviceOptions::bind_enum(const QString &name, int key, | |
161 | GVariant *const gvar_list, function<QString (GVariant*)> printer) | |
162 | { | |
163 | GVariant *gvar; | |
164 | GVariantIter iter; | |
165 | vector< pair<GVariant*, QString> > values; | |
166 | ||
167 | assert(gvar_list); | |
168 | ||
169 | sr_dev_inst *const sdi = _dev_inst->dev_inst(); | |
170 | assert(sdi); | |
171 | ||
172 | g_variant_iter_init (&iter, gvar_list); | |
173 | while ((gvar = g_variant_iter_next_value (&iter))) | |
174 | values.push_back(make_pair(gvar, printer(gvar))); | |
175 | ||
176 | _properties.push_back(shared_ptr<Property>( | |
177 | new Enum(name, values, | |
178 | bind(config_getter, sdi, _group, key), | |
179 | bind(config_setter, sdi, _group, key, _1)))); | |
180 | } | |
181 | ||
182 | void DeviceOptions::bind_int(const QString &name, int key, QString suffix, | |
183 | optional< std::pair<int64_t, int64_t> > range) | |
184 | { | |
185 | sr_dev_inst *const sdi = _dev_inst->dev_inst(); | |
186 | assert(sdi); | |
187 | ||
188 | _properties.push_back(shared_ptr<Property>( | |
189 | new Int(name, suffix, range, | |
190 | bind(config_getter, sdi, _group, key), | |
191 | bind(config_setter, sdi, _group, key, _1)))); | |
192 | } | |
193 | ||
194 | QString DeviceOptions::print_gvariant(GVariant *const gvar) | |
195 | { | |
196 | QString s; | |
197 | ||
198 | if (g_variant_is_of_type(gvar, G_VARIANT_TYPE("s"))) | |
199 | s = QString::fromUtf8(g_variant_get_string(gvar, NULL)); | |
200 | else | |
201 | { | |
202 | gchar *const text = g_variant_print(gvar, FALSE); | |
203 | s = QString::fromUtf8(text); | |
204 | g_free(text); | |
205 | } | |
206 | ||
207 | return s; | |
208 | } | |
209 | ||
210 | QString DeviceOptions::print_timebase(GVariant *const gvar) | |
211 | { | |
212 | uint64_t p, q; | |
213 | g_variant_get(gvar, "(tt)", &p, &q); | |
214 | return QString::fromUtf8(sr_period_string(p * q)); | |
215 | } | |
216 | ||
217 | QString DeviceOptions::print_vdiv(GVariant *const gvar) | |
218 | { | |
219 | uint64_t p, q; | |
220 | g_variant_get(gvar, "(tt)", &p, &q); | |
221 | return QString::fromUtf8(sr_voltage_string(p, q)); | |
222 | } | |
223 | ||
224 | QString DeviceOptions::print_voltage_threshold(GVariant *const gvar) | |
225 | { | |
226 | gdouble lo, hi; | |
227 | char buf[64]; | |
228 | g_variant_get(gvar, "(dd)", &lo, &hi); | |
229 | snprintf(buf, sizeof(buf), "L<%.1fV H>%.1fV", lo, hi); | |
230 | return QString::fromUtf8(buf); | |
231 | } | |
232 | ||
233 | } // binding | |
234 | } // prop | |
235 | } // pv | |
236 |