-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainwindow.cpp.Sw7256
215 lines (181 loc) · 10.1 KB
/
mainwindow.cpp.Sw7256
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
/***************************************************************************
** This file is part of Serial Port Plotter **
** **
** **
** Serial Port Plotter is a program for plotting integer data from **
** serial port using Qt and QCustomPlot **
** **
** 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 "mainwindow.hpp"
#include "ui_mainwindow.h"
/******************************************************************************************************************/
/* Constructor */
/******************************************************************************************************************/
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow),
connected(false), plotting(false), dataPointNumber(0), numberOfAxes(1), STATE(WAIT_START), NUMBER_OF_POINTS(500)
{
ui->setupUi(this);
createUI(); // Create the UI
ui->tabWidget->setCurrentIndex(0);
crearGrafico1(); // Setup plot area
crearGrafico2(); // Setup plot area
ui->stopPlotButton->setEnabled(false); // Plot button is disabled initially
colourSetup();
// Slot for printing coordinates
connect(ui->plot, SIGNAL(mouseRelease(QMouseEvent*)), this, SLOT(onMouseMoveInPlot(QMouseEvent*)));
h = 0;
serialPort = NULL; // Set serial port pointer to NULL initially
connect(&updateTimer, SIGNAL(timeout()), this, SLOT(replot())); // Connect update timer to replot slot
ui->saveJPGButton->setEnabled(1);
ui->comboAxes->setEnabled(1);
}
/******************************************************************************************************************/
/******************************************************************************************************************/
/* Destructor */
/******************************************************************************************************************/
MainWindow::~MainWindow()
{
if(serialPort != NULL) delete serialPort;
delete ui;
}
/******************************************************************************************************************/
/******************************************************************************************************************/
/* Replot */
/******************************************************************************************************************/
void MainWindow::replot()
{
qDebug() <<"Replot";
if(connected) {
ui->plot->xAxis->setRange(dataPointNumber - NUMBER_OF_POINTS, dataPointNumber);
ui->plot->replot();
}
qDebug() <<"END Replot";
}
/******************************************************************************************************************/
/******************************************************************************************************************/
/* Slot for new data from serial port . Data is comming in QStringList and needs to be parsed */
/******************************************************************************************************************/
void MainWindow::onNewDataArrived(QStringList newData)
{
if(plotting)
{
signal.append(newData, CANALES);
int dataListSize = newData.size(); // Get size of received list
dataPointNumber++; // Increment data number
qDebug() <<"Data Arrive. Tamaño = "<<dataListSize;
qDebug() <<"Numero de ejes = "<<numberOfAxes;
this->procesarSignals();
newData = this->signal.lastValueUpdate();
for(int i=0; i < 3; i++)
{
ui->plot->graph(i)->addData(dataPointNumber, newData[i].toInt());
ui->plot->graph(i)->removeDataBefore(dataPointNumber - NUMBER_OF_POINTS);
}
for(int i=0; i < 3; i++)
{
ui->plot2->graph(i)->addData(dataPointNumber, newData[i+3].toInt());
ui->plot2->graph(i)->removeDataBefore(dataPointNumber - NUMBER_OF_POINTS);
}
qDebug() <<"END Data Arrive";
}
}
/******************************************************************************************************************/
/******************************************************************************************************************/
/* Read data for inside serial port */
/******************************************************************************************************************/
void MainWindow::readData()
{
if(serialPort->bytesAvailable()) { // If any bytes are available
QByteArray data = serialPort->readAll(); // Read all data in QByteArray
if(!data.isEmpty()) { // If the byte array is not empty
char *temp = data.data(); // Get a '\0'-terminated char* to the data
for(int i = 0; temp[i] != '\0'; i++) { // Iterate over the char*
switch(STATE) { // Switch the current state of the message
case WAIT_START: // If waiting for start [$], examine each char
if(temp[i] == START_MSG) { // If the char is $, change STATE to IN_MESSAGE
STATE = IN_MESSAGE;
receivedData.clear(); // Clear temporary QString that holds the message
break; // Break out of the switch
}
break;
case IN_MESSAGE: // If state is IN_MESSAGE
if(temp[i] == END_MSG) { // If char examined is ;, switch state to END_MSG
STATE = WAIT_START;
QStringList incomingData = receivedData.split(' '); // Split string received from port and put it into list
emit newData(incomingData); // Emit signal for data received with the list
break;
}
else if(isdigit(temp[i]) || isspace(temp[i]) ) { // If examined char is a digit, and not '$' or ';', append it to temporary string
receivedData.append(temp[i]);
}
break;
default:
{
STATE = WAIT_START;
break;
}
}
}
}
}
}
/******************************************************************************************************************/
void MainWindow::generar()
{
QStringList lista;
int d = h*h;
h++;
dataPointNumber++;
if(h==100)
{
h = 0;
}
lista.append(QString::number(d));
for(int i = 1; i < CANALES; i++)
{
lista.append((QString::number(h*i*10)));
}
qDebug() << "Emitiendo";
emit(newData(lista));
}
void MainWindow::on_GraficoAleatorio_clicked()
{
if(plotting == 0)
{
crearGrafico1();
crearGrafico2();
plotting = 1;
connected = 1;
updateTimer.setInterval(20); // Slot is refreshed 20 times per second
generador.start(5);
updateTimer.start();
connect(this, SIGNAL(newData(QStringList)), this, SLOT(onNewDataArrived(QStringList)));
connect(&generador, SIGNAL(timeout()), this, SLOT(generar())); // Connect update timer to replot slot
qDebug() << "Generado";
}
else
{
plotting = 0;
connected = 0;
disconnect(this, SIGNAL(newData(QStringList)), this, SLOT(onNewDataArrived(QStringList)));
disconnect(&generador, SIGNAL(timeout()),this, SLOT(generar()));
updateTimer.stop();
generador.stop();
}
}