forked from analogdevicesinc/scopy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcalibration.cpp
173 lines (140 loc) · 3.31 KB
/
calibration.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
* Copyright (c) 2019 Analog Devices Inc.
*
* This file is part of Scopy
* (see http://www.github.com/analogdevicesinc/scopy).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "logging_categories.h"
#include "calibration.hpp"
#include <libm2k/m2kexceptions.hpp>
#include <errno.h>
#include <QDebug>
#include <QtGlobal>
#include <iio.h>
#include <QThread>
#include "calibration_api.hpp"
using namespace adiscope;
Calibration::Calibration(struct iio_context *ctx, QJSEngine *engine):
m_api(new Calibration_API(this)),
m_cancel(false),
m_ctx(ctx),
m_initialized(false)
{
m_api->setObjectName("calib");
m_api->js_register(engine);
}
Calibration::~Calibration()
{
delete m_api;
}
bool Calibration::initialize()
{
m_initialized = false;
if (!m_ctx) {
return false;
}
m_m2k = libm2k::context::m2kOpen(m_ctx, "");
if (!m_m2k) {
return false;
}
m_initialized = true;
return m_initialized;
}
bool Calibration::isInitialized() const
{
return m_initialized;
}
bool Calibration::isCalibrated()
{
return m_m2k->isCalibrated();
}
bool Calibration::resetCalibration()
{
if (!m_initialized) {
qDebug(CAT_CALIBRATION) << "Rx path is not initialized for calibration.";
return false;
}
bool ok = m_m2k->resetCalibration();
return ok;
}
bool Calibration::calibrateAll()
{
if (!m_m2k) {
return false;
}
bool ok = false;
try {
ok = m_m2k->calibrateADC();
} catch (libm2k::m2k_exception &e) {
qDebug(CAT_CALIBRATION) << e.what();
ok = false;
}
if(!ok || m_cancel)
goto calibration_fail;
try {
ok = m_m2k->calibrateDAC();
} catch (libm2k::m2k_exception &e) {
qDebug(CAT_CALIBRATION) << e.what();
ok = false;
}
if(!ok || m_cancel)
goto calibration_fail;
return true;
calibration_fail:
m_cancel=false;
return false;
}
bool Calibration::calibrateAdc()
{
return m_m2k->calibrateADC();
}
bool Calibration::calibrateDac()
{
return m_m2k->calibrateDAC();
}
void Calibration::cancelCalibration()
{
m_cancel = true;
}
bool Calibration::hasContextCalibration() const
{
return m_m2k->hasContextCalibration();
}
float Calibration::calibrateFromContext()
{
return m_m2k->calibrateFromContext();
}
/* FIXME: TODO: Move this into a HW class / lib M2k */
double Calibration::getIioDevTemp(const QString& devName) const
{
double temp = -273.15;
struct iio_device *dev = iio_context_find_device(m_ctx,
devName.toLatin1().data());
if (dev) {
struct iio_channel *chn = iio_device_find_channel(dev, "temp0",
false);
if (chn) {
double offset;
double raw;
double scale;
iio_channel_attr_read_double(chn, "offset", &offset);
iio_channel_attr_read_double(chn, "raw", &raw);
iio_channel_attr_read_double(chn, "scale", &scale);
temp = (raw + offset) * scale / 1000;
}
}
return temp;
}