-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchannelController.cpp
332 lines (304 loc) · 11.3 KB
/
channelController.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#include "channelController.h"
#include "utils.h"
#include "settingsController.h"
#include "deviceCommunicationWorker.h"
#include "plotline.h"
#include <QDateTime>
ChannelController::ChannelController(QDeclarativeItem* curve)
: curve(qobject_cast<PlotLine*>(curve))
, updateType(Freeze)
, updateInterval(50)
{
Q_ASSERT(curve && "must pass a PlotLine object as curve");
updateTimer.setSingleShot(true);
updateTimer.setInterval(updateInterval);
}
void ChannelController::setUpdateInterval(int msecs)
{
Q_ASSERT(updateInterval > 0 && "update interval must be positive");
updateInterval = msecs;
updateTimer.stop();
updateTimer.setInterval(updateInterval);
updateTimer.start();
}
void ChannelController::setUpdateType(ChannelController::UpdateType type)
{
updateType = type;
resetTimer();
}
void ChannelController::resetTimer()
{
if ( updateType == Periodically ) {
updateTimer.start();
}
}
void ChannelController::changeChannelMode(QString channel, QString newMode)
{
if ( curve->property("id") == channel ) {
if ( newMode.toLower() == "off" ) {
qDebug() << "disabling channel" << channel;
curve->enabled = false;
}
else {
curve->enabled = true;
resetTimer();
}
}
redraw();
}
void ChannelController::connectToSettingsController(const SettingsController* controller)
{
QObject::connect(controller, SIGNAL(updateTypeChanged(ChannelController::UpdateType)),
this, SLOT(setUpdateType(ChannelController::UpdateType)));
QObject::connect(controller, SIGNAL(updateIntervalChanged(int)),
this, SLOT(setUpdateInterval(int)));
QObject::connect(controller, SIGNAL(dataRangeChangeRequested(QString, Channel::TransformationKind, Channel::Axis, float)),
this, SLOT(changeDataRange(QString, Channel::TransformationKind, Channel::Axis, float)));
QObject::connect(controller, SIGNAL(channelModeChanged(QString,QString)),
this, SLOT(changeChannelMode(QString,QString)));
QObject::connect(controller, SIGNAL(autoRangeRequested()),
this, SLOT(autoDataRange()));
}
void ChannelController::autoDataRange()
{
if ( curve->data->data.size() < 2 ) {
// nothing to scale
return;
}
else {
// scale the curve to fit on 1/8 of the screen vertically
// and on the whole screen horizontally
const float width = curve->data->data.count() - 1;
// ignore the first data point to make scaling work nicely for FFT + DC offset signals
float ymin = curve->data->data[1], ymax = curve->data->data[1];
foreach ( const float value, curve->data->data.values().mid(1) ) {
if ( value < ymin ) ymin = value;
if ( value > ymax ) ymax = value;
}
const float height = ymax-ymin;
const QRectF newRange = QRectF(0, ymin - (8-1.8*curve->getChannelNumber())*height, width, height * 8);
curve->setDataRange(newRange);
}
}
void ChannelController::changeDataRange(QString channel, Channel::TransformationKind kind, Channel::Axis axis, float amount)
{
if ( curve->property("id") == channel ) {
QRectF range = curve->getDataRange();
if ( kind == Channel::Scale ) {
if ( axis == Channel::HorizontalAxis ) {
float change = range.width() * amount / 100;
range.setLeft(range.left() - change / 2);
range.setRight(range.right() + change / 2);
}
else {
float change = range.height() * amount / 100;
range.setTop(range.top() - change*range.top() / range.height());
range.setBottom(range.bottom() - change*range.bottom() / range.height());
}
}
else {
if ( axis == Channel::HorizontalAxis ) {
float adjusted = amount*range.width() / 600;
range.setLeft(range.left() - adjusted);
range.setRight(range.right() - adjusted);
}
else {
float adjusted = amount*range.height() / 600;
range.setTop(range.top() + adjusted);
range.setBottom(range.bottom() + adjusted);
}
}
curve->setDataRange(range);
}
}
void ChannelController::redraw()
{
curve->notifyDataChanged();
}
ScopeChannelController::ScopeChannelController(QDeclarativeItem* curve, DeviceCommunicationWorker* worker)
: ChannelController(curve)
, channel("CHAN1")
, worker(worker)
{
Q_ASSERT(worker && "must provide a valid worker thread");
QObject::connect(&updateTimer, SIGNAL(timeout()), this, SLOT(doSingleUpdate()));
}
void ScopeChannelController::changeChannelMode(QString channel, QString newMode)
{
ChannelController::changeChannelMode(channel, newMode);
if ( curve->property("id") == channel ) {
if ( newMode == "Fake" ) {
fakeMode = true;
}
else {
fakeMode = false;
resetTimer();
}
}
}
void ScopeChannelController::fillCurveWithFakeData()
{
for ( int i = 0; i < 1400; i++ ) {
curve->data->data[i] = cos(i/30.0) * curve->getDataRange().height() / 8.0 + curve->getDataRange().top() + curve->getDataRange().height() / 2.0;
}
ChannelController::redraw();
}
void ScopeChannelController::doSingleUpdate()
{
if ( ! curve->enabled || updateType == Freeze ) {
return;
}
if ( fakeMode ) {
fillCurveWithFakeData();
return;
}
ReadChannelDataCommunicationRequest* req = new ReadChannelDataCommunicationRequest(this, "updateReady");
req->channel = channel;
worker->enqueue(req);
}
void ScopeChannelController::updateReady(CommunicationReply* reply)
{
ScopeChannelDataCommunicationReply* scopeReply = static_cast<ScopeChannelDataCommunicationReply*>(reply);
curve->data->data = Utils::parseScopeChannelReply(reply->reply, scopeReply->yref, scopeReply->scale, scopeReply->offset);
delete reply;
ChannelController::redraw();
resetTimer();
}
JSDefinedChannelController::JSDefinedChannelController(QDeclarativeItem* curve, QDeclarativeItem* textArea, QList< PlotLine* > inputChannels)
: ChannelController(curve)
, inputChannels(inputChannels)
, textArea(textArea)
{
setUpdateType(Freeze);
QObject::connect(textArea, SIGNAL(textChanged(const QString&)), this, SLOT(doUpdate(const QString&)));
foreach ( const PlotLine* chan, inputChannels ) {
QObject::connect(chan, SIGNAL(dataChanged()), this, SLOT(scheduleUpdate()));
QObject::connect(chan, SIGNAL(dataRangeChanged()), this, SLOT(scheduleUpdate()));
}
QObject::connect(&updateTimer, SIGNAL(timeout()), this, SLOT(doUpdate()));
doUpdate();
}
void JSDefinedChannelController::scheduleUpdate()
{
if ( ! updateTimer.isActive() && ! updateType == Freeze ) {
updateTimer.setInterval(updateInterval >= 250 ? updateInterval : updateInterval * 2.5);
updateTimer.start();
}
}
void JSDefinedChannelController::doUpdate()
{
QString text = textArea->property("text").toString();
doUpdate(text);
}
void JSDefinedChannelController::changeChannelMode(QString channel, QString newMode)
{
ChannelController::changeChannelMode(channel, newMode);
if ( newMode == "JS Math" ) {
operationMode = JSMath;
}
else if ( newMode == "JS History" ) {
operationMode = JSMeasurementHistory;
curve->data->data.clear();
}
doUpdate();
}
void JSDefinedChannelController::doUpdate(const QString& text)
{
if ( ! curve->enabled || updateType == Freeze ) {
return;
}
QTime t;
t.start();
if ( operationMode == JSMath ) {
QVariant returnedValue;
float y1, y2;
for ( int i = 0; i < qMin(inputChannels[0]->data->data.size(), inputChannels[1]->data->data.size()); i++ ) {
y1 = inputChannels[0]->data->data[i];
y2 = inputChannels[1]->data->data[i];
QMetaObject::invokeMethod(textArea, "doCalculation", Q_RETURN_ARG(QVariant, returnedValue),
Q_ARG(QVariant, y1), Q_ARG(QVariant, y2), Q_ARG(QVariant, text));
curve->data->data[i] = returnedValue.toFloat();
}
}
else if ( operationMode == JSMeasurementHistory ) {
QVariant returnedValue;
QVector<QVariantList> channels;
foreach ( PlotLine* channel, inputChannels ) {
QVariantList l;
foreach ( float y, channel->data->data.values() ) {
l.append(y);
}
channels << l;
}
QMetaObject::invokeMethod(textArea, "doHistoryCalculation", Q_RETURN_ARG(QVariant, returnedValue),
Q_ARG(QVariant, channels[0]), Q_ARG(QVariant, channels[1]),
Q_ARG(QVariant, channels[2]), Q_ARG(QVariant, text));
curve->data->data[curve->data->data.size()] = returnedValue.toFloat();
}
autoDataRange();
redraw();
}
FixedFunctionChannelController::FixedFunctionChannelController(QDeclarativeItem* curve, const QList<PlotLine*>& channels)
: ChannelController(curve)
, channels(channels)
, operationMode(CrossCorrelation)
, fftTargetChannel(0)
{
updateTimer.setInterval(75);
updateTimer.setSingleShot(true);
QObject::connect(&updateTimer, SIGNAL(timeout()), this, SLOT(doUpdate()));
foreach ( PlotLine* channel, channels ) {
QObject::connect(channel, SIGNAL(dataChanged()), this, SLOT(scheduleUpdate()));
}
}
void FixedFunctionChannelController::scheduleUpdate()
{
// Only restart the timer if it's not running, because otherwise
// frequent changes in the data will prevent updates
if ( ! updateTimer.isActive() ) {
updateTimer.start();
}
}
void FixedFunctionChannelController::changeChannelMode(QString channel, QString newMode)
{
if ( curve->property("id") == channel ) {
bool autoRange = false;
if ( newMode.startsWith("FFT") ) {
// only do auto range if coming from another operation mode
autoRange = operationMode != FourierTransform;
setOperationMode(FourierTransform);
bool ok = false;
int channel = newMode.split(" ").last().toLower().replace("ch", "").toInt(&ok) - 1;
if ( ok && channel < channels.count() ) {
fftTargetChannel = channels.at(channel);
}
else {
fftTargetChannel = 0;
}
doUpdate();
}
else if ( newMode == "CrossCorr" ) {
setOperationMode(CrossCorrelation);
autoRange = true;
}
doUpdate();
if ( autoRange ) {
autoDataRange();
}
}
ChannelController::changeChannelMode(channel, newMode);
}
void FixedFunctionChannelController::setOperationMode(FixedFunctionChannelController::OperationMode newMode)
{
operationMode = newMode;
}
void FixedFunctionChannelController::doUpdate()
{
if ( operationMode == CrossCorrelation && channels.count() >= 2 ) {
curve->data->data = Utils::crossCorrelation(channels[0]->data->data, channels[1]->data->data);
}
if ( operationMode == FourierTransform && fftTargetChannel ) {
curve->data->data = Utils::fastFourierTransform(fftTargetChannel->data->data);
}
redraw();
}