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