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