glgenerator.cpp
13.2 KB
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
////////////////////////////////////////////////////////////////////////////////
//
// OpenHantek
// glgenerator.cpp
//
// Copyright (C) 2010 Oliver Haag
// oliver.haag@gmail.com
//
// 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 <QGLWidget>
#include <QMutex>
#include "glgenerator.h"
#include "dataanalyzer.h"
#include "settings.h"
////////////////////////////////////////////////////////////////////////////////
// class GlArray
/// \brief Initializes the array.
GlArray::GlArray() {
this->data = 0;
this->size = 0;
}
/// \brief Deletes the array.
GlArray::~GlArray() {
if(this->data)
delete this->data;
}
/// \brief Get the size of the array.
/// \return Number of array elements.
unsigned long int GlArray::getSize() {
return this->size;
}
/// \brief Set the size of the array.
/// Previous array contents are lost.
/// \param size New number of array elements.
void GlArray::setSize(unsigned long int size) {
if(this->size == size)
return;
if(this->data)
delete[] this->data;
if(size)
this->data = new GLfloat[size];
else
this->data = 0;
this->size = size;
}
////////////////////////////////////////////////////////////////////////////////
// class GlGenerator
/// \brief Initializes the scope widget.
/// \param settings The target settings object.
/// \param parent The parent widget.
GlGenerator::GlGenerator(DsoSettings *settings, QObject *parent) : QObject(parent) {
this->settings = settings;
this->dataAnalyzer = 0;
this->digitalPhosphorDepth = 0;
this->generateGrid();
}
/// \brief Deletes OpenGL objects.
GlGenerator::~GlGenerator() {
// todo: Clean up vaChannel
}
/// \brief Set the data analyzer whose data will be drawn.
/// \param dataAnalyzer Pointer to the DataAnalyzer class.
void GlGenerator::setDataAnalyzer(DataAnalyzer *dataAnalyzer) {
if(this->dataAnalyzer)
disconnect(this->dataAnalyzer, SIGNAL(finished()), this, SLOT(generateGraphs()));
this->dataAnalyzer = dataAnalyzer;
connect(this->dataAnalyzer, SIGNAL(finished()), this, SLOT(generateGraphs()));
}
/// \brief Prepare arrays for drawing the data we get from the data analyzer.
void GlGenerator::generateGraphs() {
if(!this->dataAnalyzer)
return;
// Adapt the number of graphs
for(int mode = Dso::CHANNELMODE_VOLTAGE; mode < Dso::CHANNELMODE_COUNT; mode++) {
for(int channel = this->vaChannel[mode].count(); channel < this->settings->scope.voltage.count(); channel++)
this->vaChannel[mode].append(QList<GlArray *>());
for(int channel = this->settings->scope.voltage.count(); channel < this->vaChannel[mode].count(); channel++)
this->vaChannel[mode].removeLast();
}
// Set digital phosphor depth to one if we don't use it
if(this->settings->view.digitalPhosphor)
this->digitalPhosphorDepth = this->settings->view.digitalPhosphorDepth;
else
this->digitalPhosphorDepth = 1;
// Handle all digital phosphor related list manipulations
for(int mode = Dso::CHANNELMODE_VOLTAGE; mode < Dso::CHANNELMODE_COUNT; mode++) {
for(int channel = 0; channel < this->vaChannel[mode].count(); channel++) {
// Resize lists for vector array if the digital phosphor depth has changed
if(this->vaChannel[mode][channel].count() != this->digitalPhosphorDepth)
for(int index = this->vaChannel[mode][channel].count(); index < this->digitalPhosphorDepth; index++)
this->vaChannel[mode][channel].append(new GlArray());
for(int index = this->digitalPhosphorDepth; index < this->vaChannel[mode][channel].count(); index++) {
delete this->vaChannel[mode][channel].last();
this->vaChannel[mode][channel].removeLast();
}
// Move the last list element to the front
this->vaChannel[mode][channel].move(this->digitalPhosphorDepth -1, 0);
}
}
this->dataAnalyzer->mutex()->lock();
switch(this->settings->scope.horizontal.format) {
case Dso::GRAPHFORMAT_TY:
// Add graphs for channels
for(int mode = Dso::CHANNELMODE_VOLTAGE; mode < Dso::CHANNELMODE_COUNT; mode++) {
for(int channel = 0; channel < this->settings->scope.voltage.count(); channel++) {
// Check if this channel is used and available at the data analyzer
if(((mode == Dso::CHANNELMODE_VOLTAGE) ? this->settings->scope.voltage[channel].used : this->settings->scope.spectrum[channel].used) && this->dataAnalyzer->data(channel) && this->dataAnalyzer->data(channel)->samples.voltage.sample) {
// Check if the sample count has changed
unsigned int neededSize = ((mode == Dso::CHANNELMODE_VOLTAGE) ? this->dataAnalyzer->data(channel)->samples.voltage.count : this->dataAnalyzer->data(channel)->samples.spectrum.count) * 2;
for(int index = 0; index < this->digitalPhosphorDepth; index++) {
if(this->vaChannel[mode][channel][index]->getSize() != neededSize)
this->vaChannel[mode][channel][index]->setSize(0);
}
// Check if the array is allocated
if(!this->vaChannel[mode][channel].first()->data)
this->vaChannel[mode][channel].first()->setSize(neededSize);
GLfloat *vaNewChannel = this->vaChannel[mode][channel].first()->data;
// What's the horizontal distance between sampling points?
double horizontalFactor;
if(mode == Dso::CHANNELMODE_VOLTAGE)
horizontalFactor = this->dataAnalyzer->data(channel)->samples.voltage.interval / this->settings->scope.horizontal.timebase;
else
horizontalFactor = this->dataAnalyzer->data(channel)->samples.spectrum.interval / this->settings->scope.horizontal.frequencybase;
// Fill vector array
unsigned int arrayPosition = 0;
if(mode == Dso::CHANNELMODE_VOLTAGE) {
for(unsigned int position = 0; position < this->dataAnalyzer->data(channel)->samples.voltage.count; position++) {
vaNewChannel[arrayPosition++] = position * horizontalFactor - DIVS_TIME / 2;
vaNewChannel[arrayPosition++] = this->dataAnalyzer->data(channel)->samples.voltage.sample[position] / this->settings->scope.voltage[channel].gain + this->settings->scope.voltage[channel].offset;
}
}
else {
for(unsigned int position = 0; position < this->dataAnalyzer->data(channel)->samples.spectrum.count; position++) {
vaNewChannel[arrayPosition++] = position * horizontalFactor - DIVS_TIME / 2;
vaNewChannel[arrayPosition++] = this->dataAnalyzer->data(channel)->samples.spectrum.sample[position] / this->settings->scope.spectrum[channel].magnitude + this->settings->scope.spectrum[channel].offset;
}
}
}
else {
// Delete all vector arrays
for(int index = 0; index < this->digitalPhosphorDepth; index++)
this->vaChannel[mode][channel][index]->setSize(0);
}
}
}
break;
case Dso::GRAPHFORMAT_XY:
for(int channel = 0; channel < this->settings->scope.voltage.count(); channel ++) {
// For even channel numbers check if this channel is used and this and the following channel are available at the data analyzer
if(channel % 2 == 0 && channel + 1 < this->settings->scope.voltage.count() && this->settings->scope.voltage[channel].used && this->dataAnalyzer->data(channel) && this->dataAnalyzer->data(channel)->samples.voltage.sample && this->dataAnalyzer->data(channel + 1) && this->dataAnalyzer->data(channel + 1)->samples.voltage.sample) {
// Check if the sample count has changed
unsigned int neededSize = qMin(this->dataAnalyzer->data(channel)->samples.voltage.count, this->dataAnalyzer->data(channel + 1)->samples.voltage.count) * 2;
for(int index = 0; index < this->digitalPhosphorDepth; index++) {
if(this->vaChannel[Dso::CHANNELMODE_VOLTAGE][channel][index]->getSize() != neededSize)
this->vaChannel[Dso::CHANNELMODE_VOLTAGE][channel][index]->setSize(0);
}
// Check if the array is allocated
if(!this->vaChannel[Dso::CHANNELMODE_VOLTAGE][channel].first()->data)
this->vaChannel[Dso::CHANNELMODE_VOLTAGE][channel].first()->setSize(neededSize);
GLfloat *vaNewChannel = this->vaChannel[Dso::CHANNELMODE_VOLTAGE][channel].first()->data;
// Fill vector array
unsigned int arrayPosition = 0;
unsigned int xChannel = channel;
unsigned int yChannel = channel + 1;
for(unsigned int position = 0; position < this->dataAnalyzer->data(channel)->samples.voltage.count; position++) {
vaNewChannel[arrayPosition++] = this->dataAnalyzer->data(xChannel)->samples.voltage.sample[position] / this->settings->scope.voltage[xChannel].gain + this->settings->scope.voltage[xChannel].offset;
vaNewChannel[arrayPosition++] = this->dataAnalyzer->data(yChannel)->samples.voltage.sample[position] / this->settings->scope.voltage[yChannel].gain + this->settings->scope.voltage[yChannel].offset;
}
}
else {
// Delete all vector arrays
for(int index = 0; index < this->digitalPhosphorDepth; index++)
this->vaChannel[Dso::CHANNELMODE_VOLTAGE][channel][index]->setSize(0);
}
// Delete all spectrum graphs
for(int index = 0; index < this->digitalPhosphorDepth; index++)
this->vaChannel[Dso::CHANNELMODE_SPECTRUM][channel][index]->setSize(0);
}
break;
default:
break;
}
this->dataAnalyzer->mutex()->unlock();
emit graphsGenerated();
}
/// \brief Create the needed OpenGL vertex arrays for the grid.
void GlGenerator::generateGrid() {
// Grid
this->vaGrid[0].setSize(((DIVS_TIME * DIVS_SUB - 2) * (DIVS_VOLTAGE - 2) + (DIVS_VOLTAGE * DIVS_SUB - 2) * (DIVS_TIME - 2) - ((DIVS_TIME - 2) * (DIVS_VOLTAGE - 2))) * 2);
int pointIndex = 0;
// Draw vertical lines
for(int div = 1; div < DIVS_TIME / 2; div++) {
for(int dot = 1; dot < DIVS_VOLTAGE / 2 * DIVS_SUB; dot++) {
float dotPosition = (float) dot / DIVS_SUB;
this->vaGrid[0].data[pointIndex++] = -div;
this->vaGrid[0].data[pointIndex++] = -dotPosition;
this->vaGrid[0].data[pointIndex++] = -div;
this->vaGrid[0].data[pointIndex++] = dotPosition;
this->vaGrid[0].data[pointIndex++] = div;
this->vaGrid[0].data[pointIndex++] = -dotPosition;
this->vaGrid[0].data[pointIndex++] = div;
this->vaGrid[0].data[pointIndex++] = dotPosition;
}
}
// Draw horizontal lines
for(int div = 1; div < DIVS_VOLTAGE / 2; div++) {
for(int dot = 1; dot < DIVS_TIME / 2 * DIVS_SUB; dot++) {
if(dot % DIVS_SUB == 0)
continue; // Already done by vertical lines
float dotPosition = (float) dot / DIVS_SUB;
this->vaGrid[0].data[pointIndex++] = -dotPosition;
this->vaGrid[0].data[pointIndex++] = -div;
this->vaGrid[0].data[pointIndex++] = dotPosition;
this->vaGrid[0].data[pointIndex++] = -div;
this->vaGrid[0].data[pointIndex++] = -dotPosition;
this->vaGrid[0].data[pointIndex++] = div;
this->vaGrid[0].data[pointIndex++] = dotPosition;
this->vaGrid[0].data[pointIndex++] = div;
}
}
// Axes
this->vaGrid[1].setSize((2 + (DIVS_TIME * DIVS_SUB - 2) + (DIVS_VOLTAGE * DIVS_SUB - 2)) * 4);
pointIndex = 0;
// Horizontal axis
this->vaGrid[1].data[pointIndex++] = -DIVS_TIME / 2;
this->vaGrid[1].data[pointIndex++] = 0;
this->vaGrid[1].data[pointIndex++] = DIVS_TIME / 2;
this->vaGrid[1].data[pointIndex++] = 0;
// Vertical axis
this->vaGrid[1].data[pointIndex++] = 0;
this->vaGrid[1].data[pointIndex++] = -DIVS_VOLTAGE / 2;
this->vaGrid[1].data[pointIndex++] = 0;
this->vaGrid[1].data[pointIndex++] = DIVS_VOLTAGE / 2;
// Subdiv lines on horizontal axis
for(int line = 1; line < DIVS_TIME / 2 * DIVS_SUB; line++) {
float linePosition = (float) line / DIVS_SUB;
this->vaGrid[1].data[pointIndex++] = linePosition;
this->vaGrid[1].data[pointIndex++] = -0.05;
this->vaGrid[1].data[pointIndex++] = linePosition;
this->vaGrid[1].data[pointIndex++] = 0.05;
this->vaGrid[1].data[pointIndex++] = -linePosition;
this->vaGrid[1].data[pointIndex++] = -0.05;
this->vaGrid[1].data[pointIndex++] = -linePosition;
this->vaGrid[1].data[pointIndex++] = 0.05;
}
// Subdiv lines on vertical axis
for(int line = 1; line < DIVS_VOLTAGE / 2 * DIVS_SUB; line++) {
float linePosition = (float) line / DIVS_SUB;
this->vaGrid[1].data[pointIndex++] = -0.05;
this->vaGrid[1].data[pointIndex++] = linePosition;
this->vaGrid[1].data[pointIndex++] = 0.05;
this->vaGrid[1].data[pointIndex++] = linePosition;
this->vaGrid[1].data[pointIndex++] = -0.05;
this->vaGrid[1].data[pointIndex++] = -linePosition;
this->vaGrid[1].data[pointIndex++] = 0.05;
this->vaGrid[1].data[pointIndex++] = -linePosition;
}
// Border
this->vaGrid[2].setSize(4 * 2);
this->vaGrid[2].data[0] = -DIVS_TIME / 2;
this->vaGrid[2].data[1] = -DIVS_VOLTAGE / 2;
this->vaGrid[2].data[2] = DIVS_TIME / 2;
this->vaGrid[2].data[3] = -DIVS_VOLTAGE / 2;
this->vaGrid[2].data[4] = DIVS_TIME / 2;
this->vaGrid[2].data[5] = DIVS_VOLTAGE / 2;
this->vaGrid[2].data[6] = -DIVS_TIME / 2;
this->vaGrid[2].data[7] = DIVS_VOLTAGE / 2;
}