]> sigrok.org Git - pulseview.git/blame - pv/devices/device.cpp
GlobalSettings: Always use Fusion style on Windows for dark themes
[pulseview.git] / pv / devices / device.cpp
CommitLineData
da30ecb7
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
efdec55a 17 * along with this program; if not, see <http://www.gnu.org/licenses/>.
da30ecb7
JH
18 */
19
20#include <cassert>
46ebcd3f
SA
21#include <type_traits>
22
23#include <QApplication>
24#include <QDebug>
25#include <QString>
da30ecb7
JH
26
27#include <libsigrokcxx/libsigrokcxx.hpp>
28
29#include "device.hpp"
30
46ebcd3f 31using std::is_same;
6f925ba9 32using std::shared_ptr;
b48daed6
JH
33
34using sigrok::ConfigKey;
7bb0fbf4 35using sigrok::Capability;
b48daed6
JH
36
37using Glib::VariantBase;
38using Glib::Variant;
39
da30ecb7
JH
40namespace pv {
41namespace devices {
42
2ad82c2e
UH
43Device::~Device()
44{
da30ecb7
JH
45 if (session_)
46 session_->remove_datafeed_callbacks();
47}
48
6f925ba9 49shared_ptr<sigrok::Session> Device::session() const
2ad82c2e 50{
da30ecb7
JH
51 return session_;
52}
53
6f925ba9 54shared_ptr<sigrok::Device> Device::device() const
2ad82c2e 55{
da30ecb7
JH
56 return device_;
57}
58
46ebcd3f 59template uint64_t Device::read_config(const sigrok::ConfigKey*, const uint64_t);
b48daed6
JH
60
61template<typename T>
62T Device::read_config(const ConfigKey *key, const T default_value)
63{
64 assert(key);
b48daed6
JH
65
66 if (!device_)
67 return default_value;
68
46ebcd3f
SA
69 if (!device_->config_check(key, Capability::GET)) {
70 qWarning() << QApplication::tr("Querying config key %1 is not allowed")
71 .arg(QString::fromStdString(key->identifier()));
b48daed6 72 return default_value;
46ebcd3f
SA
73 }
74
75 VariantBase value;
76 try {
77 value = device_->config_get(key);
78 } catch (const sigrok::Error &e) {
79 qWarning() << QApplication::tr("Querying config key %1 resulted in %2")
80 .arg(QString::fromStdString(key->identifier()), e.what());
81 return default_value;
82 }
83
84 if (is_same<T, uint32_t>::value)
85 return VariantBase::cast_dynamic<Glib::Variant<guint32>>(value).get();
86 if (is_same<T, int32_t>::value)
87 return VariantBase::cast_dynamic<Glib::Variant<gint32>>(value).get();
88 if (is_same<T, uint64_t>::value)
89 return VariantBase::cast_dynamic<Glib::Variant<guint64>>(value).get();
90 if (is_same<T, int64_t>::value)
91 return VariantBase::cast_dynamic<Glib::Variant<gint64>>(value).get();
92
93 qWarning() << QApplication::tr("Unknown type supplied when attempting to query %1")
94 .arg(QString::fromStdString(key->identifier()));
95 return default_value;
b48daed6
JH
96}
97
2ad82c2e
UH
98void Device::start()
99{
5237f0c5
JH
100 assert(session_);
101 session_->start();
102}
103
2ad82c2e
UH
104void Device::run()
105{
da30ecb7
JH
106 assert(device_);
107 assert(session_);
108 session_->run();
109}
110
2ad82c2e
UH
111void Device::stop()
112{
da30ecb7
JH
113 assert(session_);
114 session_->stop();
115}
116
117} // namespace devices
118} // namespace pv