]> sigrok.org Git - pulseview.git/blame - pv/binding/inputoutput.cpp
Fix #602 by keeping track of device state internally
[pulseview.git] / pv / binding / inputoutput.cpp
CommitLineData
0c913637
JH
1/*
2 * This file is part of the PulseView project.
3 *
4 * Copyright (C) 2015 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 <cassert>
22#include <iostream>
23#include <utility>
24
25#include <boost/none_t.hpp>
26
27#include <libsigrokcxx/libsigrokcxx.hpp>
28
5d2aa2c7 29#include <pv/prop/bool.hpp>
0c913637
JH
30#include <pv/prop/double.hpp>
31#include <pv/prop/enum.hpp>
32#include <pv/prop/int.hpp>
33#include <pv/prop/string.hpp>
34
35#include "inputoutput.hpp"
36
37using boost::none;
38
39using std::make_pair;
40using std::map;
41using std::pair;
42using std::shared_ptr;
43using std::string;
44using std::vector;
45
46using Glib::VariantBase;
47using Glib::VariantType;
48
49using sigrok::Option;
50
5d2aa2c7 51using pv::prop::Bool;
0c913637
JH
52using pv::prop::Double;
53using pv::prop::Enum;
54using pv::prop::Int;
55using pv::prop::Property;
56using pv::prop::String;
57
58namespace pv {
59namespace binding {
60
61InputOutput::InputOutput(
62 const map<string, shared_ptr<Option>> &options)
63{
64 for (pair<string, shared_ptr<Option>> o : options)
65 {
66 const shared_ptr<Option> &opt = o.second;
67 assert(opt);
68
69 const QString name = QString::fromStdString(opt->name());
70 const VariantBase def_val = opt->default_value();
71 const vector<VariantBase> values = opt->values();
72
73 options_[opt->id()] = def_val;
74
75 const Property::Getter get = [&, opt]() {
76 return options_[opt->id()]; };
77 const Property::Setter set = [&, opt](VariantBase value) {
78 options_[opt->id()] = value; };
79
80 shared_ptr<Property> prop;
81
82 if (!opt->values().empty())
83 prop = bind_enum(name, values, get, set);
5d2aa2c7
JH
84 else if (def_val.is_of_type(VariantType("b")))
85 prop = shared_ptr<Property>(new Bool(name, get, set));
0c913637
JH
86 else if (def_val.is_of_type(VariantType("d")))
87 prop = shared_ptr<Property>(new Double(name, 2, "",
ec6cc07f 88 none, none, get, set));
5d2aa2c7
JH
89 else if (def_val.is_of_type(VariantType("i")) ||
90 def_val.is_of_type(VariantType("t")) ||
91 def_val.is_of_type(VariantType("u")))
0c913637
JH
92 prop = shared_ptr<Property>(
93 new Int(name, "", none, get, set));
94 else if (def_val.is_of_type(VariantType("s")))
95 prop = shared_ptr<Property>(
96 new String(name, get, set));
97 else
98 continue;
99
100 properties_.push_back(prop);
101 }
102}
103
104const map<string, VariantBase>& InputOutput::options() const
105{
106 return options_;
107}
108
109shared_ptr<Property> InputOutput::bind_enum(
110 const QString &name, const vector<VariantBase> &values,
111 Property::Getter getter, Property::Setter setter)
112{
113 vector< pair<VariantBase, QString> > enum_vals;
114 for (VariantBase var : values)
115 enum_vals.push_back(make_pair(var, print_gvariant(var)));
116 return shared_ptr<Property>(new Enum(name, enum_vals, getter, setter));
117}
118
119} // namespace binding
120} // namespace pv