]> sigrok.org Git - pulseview.git/blame - pv/prop/binding/deviceoptions.cpp
DeviceOptions::print_vdiv: Replace C-style string formatting with QString arg formatting
[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
4d5d5d6a
BV
21#include <stdint.h>
22
48c88445
JH
23#include <QDebug>
24
3820592a 25#include "deviceoptions.h"
cf47ef78 26
94574501 27#include <pv/device/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 35using boost::optional;
6db73158 36using std::function;
819f4c25
JH
37using std::make_pair;
38using std::pair;
f9abf97e 39using std::shared_ptr;
819f4c25
JH
40using std::string;
41using std::vector;
7b3810db 42
cf47ef78
JH
43namespace pv {
44namespace prop {
45namespace binding {
46
94574501 47DeviceOptions::DeviceOptions(shared_ptr<pv::device::DevInst> dev_inst,
2445160a 48 const sr_channel_group *group) :
19adbc2c 49 _dev_inst(dev_inst),
941c1ba7 50 _group(group)
cf47ef78 51{
19adbc2c 52 assert(dev_inst);
95237c18 53
8dd44190 54 GVariant *gvar_opts;
4d5d5d6a 55 gsize num_opts;
7b3810db 56
8dd44190 57 if (!(gvar_opts = dev_inst->list_config(group, SR_CONF_DEVICE_OPTIONS)))
7b3810db
JH
58 /* Driver supports no device instance options. */
59 return;
60
4d5d5d6a
BV
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++) {
10b1be92 64 const struct sr_config_info *const info =
4d5d5d6a
BV
65 sr_config_info_get(options[i]);
66
de1d99bb
JH
67 if (!info)
68 continue;
69
4d5d5d6a 70 const int key = info->key;
8dd44190 71 GVariant *const gvar_list = dev_inst->list_config(group, key);
7b3810db 72
27e8df22 73 const QString name = QString::fromUtf8(info->name);
4d5d5d6a 74
6db73158
JH
75 const Property::Getter get = [&, key]() {
76 return _dev_inst->get_config(_group, key); };
77 const Property::Setter set = [&, key](GVariant *value) {
78 _dev_inst->set_config(_group, key, value); };
79
4d5d5d6a 80 switch(key)
7b3810db 81 {
9f6af8bd 82 case SR_CONF_SAMPLERATE:
117ef009
JH
83 // Sample rate values are not bound because they are shown
84 // in the SamplingBar
9f6af8bd
JH
85 break;
86
a2b92157 87 case SR_CONF_CAPTURE_RATIO:
6db73158
JH
88 bind_int(name, "%", pair<int64_t, int64_t>(0, 100),
89 get, set);
a2b92157
JH
90 break;
91
10b1be92 92 case SR_CONF_PATTERN_MODE:
10b1be92 93 case SR_CONF_BUFFERSIZE:
10b1be92 94 case SR_CONF_TRIGGER_SOURCE:
5be76b1a 95 case SR_CONF_TRIGGER_SLOPE:
10b1be92 96 case SR_CONF_FILTER:
4d5d5d6a 97 case SR_CONF_COUPLING:
5be76b1a 98 case SR_CONF_CLOCK_EDGE:
6db73158 99 bind_enum(name, key, gvar_list, get, set);
7b3810db
JH
100 break;
101
5be76b1a 102 case SR_CONF_EXTERNAL_CLOCK:
de1d99bb 103 case SR_CONF_RLE:
6db73158 104 bind_bool(name, get, set);
de1d99bb
JH
105 break;
106
4d5d5d6a 107 case SR_CONF_TIMEBASE:
6db73158
JH
108 bind_enum(name, key, gvar_list,
109 get, set, print_timebase);
7b3810db
JH
110 break;
111
4d5d5d6a 112 case SR_CONF_VDIV:
6db73158 113 bind_enum(name, key, gvar_list, get, set, print_vdiv);
7b3810db 114 break;
62874984
MC
115
116 case SR_CONF_VOLTAGE_THRESHOLD:
6db73158
JH
117 bind_enum(name, key, gvar_list,
118 get, set, print_voltage_threshold);
62874984 119 break;
7b3810db 120 }
4d5d5d6a 121
de1d99bb
JH
122 if (gvar_list)
123 g_variant_unref(gvar_list);
7b3810db 124 }
4d5d5d6a 125 g_variant_unref(gvar_opts);
7b3810db
JH
126}
127
6db73158
JH
128void DeviceOptions::bind_bool(const QString &name,
129 Property::Getter getter, Property::Setter setter)
de1d99bb 130{
8dd44190 131 assert(_dev_inst);
6db73158
JH
132 _properties.push_back(shared_ptr<Property>(new Bool(
133 name, getter, setter)));
de1d99bb
JH
134}
135
4d5d5d6a 136void DeviceOptions::bind_enum(const QString &name, int key,
6db73158
JH
137 GVariant *const gvar_list, Property::Getter getter,
138 Property::Setter setter, function<QString (GVariant*)> printer)
9f6af8bd 139{
4d5d5d6a
BV
140 GVariant *gvar;
141 GVariantIter iter;
142 vector< pair<GVariant*, QString> > values;
9f6af8bd 143
8dd44190 144 assert(_dev_inst);
48c88445
JH
145 if (!gvar_list) {
146 qDebug() << "Config key " << key << " was listed, but no "
147 "options were given";
148 return;
149 }
de1d99bb 150
4d5d5d6a
BV
151 g_variant_iter_init (&iter, gvar_list);
152 while ((gvar = g_variant_iter_next_value (&iter)))
153 values.push_back(make_pair(gvar, printer(gvar)));
9f6af8bd 154
8dd44190 155 _properties.push_back(shared_ptr<Property>(new Enum(name, values,
6db73158 156 getter, setter)));
9f6af8bd
JH
157}
158
6db73158
JH
159void DeviceOptions::bind_int(const QString &name, QString suffix,
160 optional< std::pair<int64_t, int64_t> > range,
161 Property::Getter getter, Property::Setter setter)
a2b92157 162{
8dd44190 163 assert(_dev_inst);
19adbc2c 164
8dd44190 165 _properties.push_back(shared_ptr<Property>(new Int(name, suffix, range,
6db73158 166 getter, setter)));
a2b92157
JH
167}
168
4d5d5d6a
BV
169QString DeviceOptions::print_timebase(GVariant *const gvar)
170{
171 uint64_t p, q;
172 g_variant_get(gvar, "(tt)", &p, &q);
27e8df22 173 return QString::fromUtf8(sr_period_string(p * q));
4d5d5d6a
BV
174}
175
176QString DeviceOptions::print_vdiv(GVariant *const gvar)
177{
178 uint64_t p, q;
179 g_variant_get(gvar, "(tt)", &p, &q);
27e8df22 180 return QString::fromUtf8(sr_voltage_string(p, q));
9f6af8bd
JH
181}
182
62874984
MC
183QString DeviceOptions::print_voltage_threshold(GVariant *const gvar)
184{
185 gdouble lo, hi;
62874984 186 g_variant_get(gvar, "(dd)", &lo, &hi);
b47e449a 187 return QString("L<%1V H>%2V").arg(lo, 0, 'f', 1).arg(hi, 0, 'f', 1);
62874984
MC
188}
189
cf47ef78
JH
190} // binding
191} // prop
192} // pv
193