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