-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathplotwidget.cpp
356 lines (289 loc) · 10.3 KB
/
plotwidget.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* Copyright (C) 2012 Jorge Aparicio <[email protected]>
*
* This file is part of qSerialTerm.
*
* qSerialTerm 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
* any later version.
* qSerialTerm 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 qSerialTerm. If not, see <http://www.gnu.org/licenses/>.
*/
#include "plotwidget.h"
#include "ui_plotwidget.h"
#include "hexstring.h"
PlotWidget::PlotWidget(QWidget *parent) :
QWidget(parent),
ui(new Ui::PlotWidget),
bytesPerUpdate(0)
{
ui->setupUi(this);
connect(ui->dataTypeComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(updateBytesPerUpdate()));
connect(ui->endiannessComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(updateBytesPerUpdate()));
connect(ui->formatComboBox, SIGNAL(currentIndexChanged(int)),
this, SLOT(updateBytesPerUpdate()));
connect(ui->numberOfPlotsSpinBox, SIGNAL(valueChanged(int)),
this, SLOT(updateBytesPerUpdate()));
ui->dataTypeComboBox->addItem(QLatin1String("uint8"), UINT8);
ui->dataTypeComboBox->addItem(QLatin1String("uint16"), UINT16);
ui->dataTypeComboBox->addItem(QLatin1String("uint32"), UINT32);
ui->dataTypeComboBox->addItem(QLatin1String("int8"), INT8);
ui->dataTypeComboBox->addItem(QLatin1String("int16"), INT16);
ui->dataTypeComboBox->addItem(QLatin1String("int32"), INT32);
ui->dataTypeComboBox->addItem(QLatin1String("float"), FLOAT);
ui->endiannessComboBox->addItem(QLatin1String("Little"), QDataStream::LittleEndian);
ui->endiannessComboBox->addItem(QLatin1String("Big"), QDataStream::BigEndian);
ui->formatComboBox->addItem(QLatin1String("Raw binary"), Binary);
ui->formatComboBox->addItem(QLatin1String("Hex string"), Hexadecimal);
}
PlotWidget::~PlotWidget()
{
delete ui;
}
void PlotWidget::display(QByteArray incomingData)
{
static QByteArray storedData;
if (plotVector.size()) {
storedData.append(incomingData);
for (int i = 0; i < storedData.size() / bytesPerUpdate; i++) {
if (ui->formatComboBox->itemData(ui->formatComboBox->currentIndex()).value<int>() == Hexadecimal)
decode(HexString::toRawBinary(storedData.left(bytesPerUpdate)));
else
decode(storedData.left(bytesPerUpdate));
storedData.remove(0, bytesPerUpdate);
}
}
}
void PlotWidget::on_goPushButton_clicked()
{
int numberOfPlots = plotVector.size();
if (numberOfPlots) {
for (int i = 0; i < numberOfPlots; i++) {
delete plotVector[i];
plotVector[i] = 0;
}
plotVector.clear();
ui->goPushButton->setText("Go");
ui->numberOfPlotsSpinBox->setEnabled(true);
ui->dataTypeComboBox->setEnabled(true);
ui->endiannessComboBox->setEnabled(true);
ui->formatComboBox->setEnabled(true);
} else {
size_t numberOfSamples = ui->samplesShownSpinBox->value();
switch(ui->dataTypeComboBox->itemData(ui->dataTypeComboBox->currentIndex()).value<int>()) {
case UINT8:
for (int i = 0; i < ui->numberOfPlotsSpinBox->value(); i++) {
plotVector.push_back(new Plot(numberOfSamples, 0, 255));
plotVector[i]->setId(QString::number(i + 1));
ui->plotLayout->addWidget(plotVector[i]);
}
break;
case UINT16:
for (int i = 0; i < ui->numberOfPlotsSpinBox->value(); i++) {
plotVector.push_back(new Plot(numberOfSamples, 0, 65535));
plotVector[i]->setId(QString::number(i + 1));
ui->plotLayout->addWidget(plotVector[i]);
}
break;
case UINT32:
for (int i = 0; i < ui->numberOfPlotsSpinBox->value(); i++) {
plotVector.push_back(new Plot(numberOfSamples, 0, 2147483647));
plotVector[i]->setId(QString::number(i + 1));
ui->plotLayout->addWidget(plotVector[i]);
}
break;
case INT8:
for (int i = 0; i < ui->numberOfPlotsSpinBox->value(); i++) {
plotVector.push_back(new Plot(numberOfSamples, -128, 127));
plotVector[i]->setId(QString::number(i + 1));
ui->plotLayout->addWidget(plotVector[i]);
}
break;
case INT16:
for (int i = 0; i < ui->numberOfPlotsSpinBox->value(); i++) {
plotVector.push_back(new Plot(numberOfSamples, -32768, 32768));
plotVector[i]->setId(QString::number(i + 1));
ui->plotLayout->addWidget(plotVector[i]);
}
break;
case INT32:
for (int i = 0; i < ui->numberOfPlotsSpinBox->value(); i++) {
plotVector.push_back(new Plot(numberOfSamples, -2147483648, 2147483647));
plotVector[i]->setId(QString::number(i + 1));
ui->plotLayout->addWidget(plotVector[i]);
}
break;
case FLOAT:
for (int i = 0; i < ui->numberOfPlotsSpinBox->value(); i++) {
plotVector.push_back(new Plot(numberOfSamples, -1, 1));
plotVector[i]->setId(QString::number(i + 1));
ui->plotLayout->addWidget(plotVector[i]);
}
break;
}
ui->goPushButton->setText("Clear");
ui->numberOfPlotsSpinBox->setDisabled(true);
ui->dataTypeComboBox->setDisabled(true);
ui->endiannessComboBox->setDisabled(true);
ui->formatComboBox->setDisabled(true);
}
}
void PlotWidget::on_samplesShownSpinBox_valueChanged(int value)
{
// TODO Clean up these magic numbers
if (value <= 80) {
if (value % 10) {
value = ((value / 10) + 1) * 10;
ui->samplesShownSpinBox->setValue(value);
}
ui->samplesShownSpinBox->setSingleStep(10);
} else if (value <= 160) {
if (value % 20) {
value = ((value / 20) + 1) * 20;
ui->samplesShownSpinBox->setValue(value);
}
ui->samplesShownSpinBox->setSingleStep(20);
} else if (value <= 400) {
if (value % 50) {
value = ((value / 50) + 1) * 50;
ui->samplesShownSpinBox->setValue(value);
}
ui->samplesShownSpinBox->setSingleStep(50);
} else if (value <= 800) {
if (value % 100) {
value = ((value / 100) + 1) * 100;
ui->samplesShownSpinBox->setValue(value);
}
ui->samplesShownSpinBox->setSingleStep(100);
} else if (value <= 1600) {
if (value % 200) {
value = ((value / 200) + 1) * 200;
ui->samplesShownSpinBox->setValue(value);
}
ui->samplesShownSpinBox->setSingleStep(200);
} else if (value <= 4000) {
if (value % 500) {
value = ((value / 500) + 1) * 500;
ui->samplesShownSpinBox->setValue(value);
}
ui->samplesShownSpinBox->setSingleStep(500);
} else if (value <= 8000) {
if (value % 1000) {
value = ((value / 1000) + 1) * 1000;
ui->samplesShownSpinBox->setValue(value);
}
ui->samplesShownSpinBox->setSingleStep(1000);
} else {
if (value % 2000) {
value = ((value / 2000) + 1) * 2000;
ui->samplesShownSpinBox->setValue(value);
}
ui->samplesShownSpinBox->setSingleStep(2000);
}
if (plotVector.size()) {
for (int i = 0; i < plotVector.size(); i++) {
plotVector[i]->setNumberOfSamples(value);
plotVector[i]->replot();
}
}
}
void PlotWidget::updateBytesPerUpdate()
{
bytesPerUpdate = ui->numberOfPlotsSpinBox->value();
switch(ui->dataTypeComboBox->itemData(ui->dataTypeComboBox->currentIndex()).value<int>()) {
case FLOAT:
case UINT32:
case INT32:
bytesPerUpdate *= 4;
break;
case UINT16:
case INT16:
bytesPerUpdate *= 2;
break;
case UINT8:
case INT8:
bytesPerUpdate *= 1;
break;
}
if (ui->formatComboBox->itemData(ui->formatComboBox->currentIndex()).value<int>() == Hexadecimal)
bytesPerUpdate *= 2;
if (bytesPerUpdate == 1)
ui->bytesPerUpdateLabel->setText(QString::number(bytesPerUpdate) +
QLatin1String(" byte"));
else
ui->bytesPerUpdateLabel->setText(QString::number(bytesPerUpdate) +
QLatin1String(" bytes"));
}
void PlotWidget::decode(QByteArray data)
{
if (plotVector.size()) {
QDataStream decoder(&data, QIODevice::ReadOnly);
decoder.setByteOrder(QDataStream::ByteOrder(ui->endiannessComboBox->itemData(ui->endiannessComboBox->currentIndex()).value<int>()));
switch(ui->dataTypeComboBox->itemData(ui->dataTypeComboBox->currentIndex()).value<int>()) {
case UINT8:
quint8 tmpUint8;
for (int i = 0; i < plotVector.size(); i++) {
decoder >> tmpUint8;
plotVector[i]->pushBack(tmpUint8);
plotVector[i]->replot();
}
break;
case UINT16:
quint16 tmpUint16;
for (int i = 0; i < plotVector.size(); i++) {
decoder >> tmpUint16;
plotVector[i]->pushBack(tmpUint16);
plotVector[i]->replot();
}
break;
case UINT32:
quint32 tmpUint32;
for (int i = 0; i < plotVector.size(); i++) {
decoder >> tmpUint32;
plotVector[i]->pushBack(tmpUint32);
plotVector[i]->replot();
}
break;
case INT8:
qint8 tmpInt8;
for (int i = 0; i < plotVector.size(); i++) {
decoder >> tmpInt8;
plotVector[i]->pushBack(tmpInt8);
plotVector[i]->replot();
}
break;
case INT16:
qint16 tmpInt16;
for (int i = 0; i < plotVector.size(); i++) {
decoder >> tmpInt16;
plotVector[i]->pushBack(tmpInt16);
plotVector[i]->replot();
}
break;
case INT32:
qint32 tmpInt32;
for (int i = 0; i < plotVector.size(); i++) {
decoder >> tmpInt32;
plotVector[i]->pushBack(tmpInt32);
plotVector[i]->replot();
}
break;
case FLOAT:
float tmpFloat;
decoder.setFloatingPointPrecision(QDataStream::SinglePrecision);
for (int i = 0; i < plotVector.size(); i++) {
decoder >> tmpFloat;
plotVector[i]->pushBack(tmpFloat);
plotVector[i]->replot();
}
break;
}
}
}