-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainwindow.cpp
245 lines (208 loc) · 7.27 KB
/
mainwindow.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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "temp.h"
#include <QtCharts/QChart>
#include <QtCharts/QLineSeries>
#include <QtCharts/QChartView>
#include <QtCharts/QXYSeries>
#include <QDateTime>
#include <QtSerialPort/QSerialPortInfo>
#include <QSerialPort>
#include <QDebug>
#include <QtCharts/QDateTimeAxis>
#include <QtCharts/QValueAxis>
#include <QColor>
#include <QGradient>
#include <QLinearGradient>
#include <QPen>
/**
* @brief Creates the ui and a chart, initializes the serial port.
*/
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow)
{
ui->setupUi(this);
// INIT CHART
chart->legend()->hide();
chart->setTitle("Temperature");
//chart->setAnimationOptions(QtCharts::QChart::SeriesAnimations);
QtCharts::QChartView *chartView = new QtCharts::QChartView(chart);
chartView->setRenderHint(QPainter::Antialiasing);
ui->graph_layout->addWidget(chartView);
// init chart mean line
/*QPainter meanLine;
meanLine.setPen(QPen(QRgb(0xFFFFFF)));
meanLine.drawLine(0, 100, 0, 100);*/
// Set the options for the pen an background of the chart
QPen pen(QRgb(0xFFFFFF));
pen.setWidth(3);
series->setPen(pen);
QLinearGradient backgroundGradient;
backgroundGradient.setStart(QPointF(0, 0));
backgroundGradient.setFinalStop(QPointF(0, 1));
backgroundGradient.setColorAt(1.0, QRgb(0x2980b9));
backgroundGradient.setColorAt(0.5, QRgb(0x27ae60));
backgroundGradient.setColorAt(0.0, QRgb(0xc0392b));
backgroundGradient.setCoordinateMode(QGradient::ObjectBoundingMode);
chart->setBackgroundBrush(backgroundGradient);
getComPorts();
// INIT SERIAL PORT
serial = new QSerialPort(this);
// SIGNALS_N_SLOTS
connect(serial, SIGNAL(error(QSerialPort::SerialPortError)), this, SLOT(handleErrors(QSerialPort::SerialPortError)));
connect(serial, SIGNAL(readyRead()), this, SLOT(readData()));
connect(ui->dd_ports, SIGNAL(highlighted(int)), this, SLOT(getComPorts()));
}
/**
* @brief closes the port a deletes the ui
*/
MainWindow::~MainWindow()
{
if(serial->isOpen())
serial->close();
delete ui;
}
// BTN
void MainWindow::on_btn_connect_clicked()
{
if(!serial->isOpen()){
serial->setPortName(ui->dd_ports->itemText(0));
serial->setBaudRate(QSerialPort::Baud9600);
serial->setDataBits(QSerialPort::Data8);
serial->setParity(QSerialPort::NoParity);
serial->setStopBits(QSerialPort::OneStop);
serial->setFlowControl(QSerialPort::NoFlowControl);
serial->open(QIODevice::ReadWrite);
if(serial->error()) {
if(serial->error() == QSerialPort::SerialPortError::DeviceNotFoundError)
addToTerminal("<b>Error: Port not found</b>");
else
addToTerminal("<b>Error happend!</b>");
}
} else {
serial->close();
addToTerminal("<b>Connection closed</b>");
ui->btn_connect->setText("Connect");
}
if(serial->isOpen()) {
addToTerminal("<b>Connection made</b>");
ui->btn_connect->setText("Disconnect");
// set start Time
startTime = QDateTime::currentDateTime().toMSecsSinceEpoch();
}
}
void MainWindow::on_btn_update_clicked()
{
// Send Settings to the Arduino
if(serial->isWritable())
serial->write(QString::number(ui->spin_interval->value()).toStdString().c_str());
}
// FUNC
void MainWindow::readData(){
if(serial->canReadLine()) {
QByteArray data = serial->readLine();
QString str = data.trimmed();
if(str.length() == 5) {
double temp = str.toDouble();
// store Temperature Values
QDateTime momentInTime = QDateTime::currentDateTime();
Temp temperatureObject(temp, momentInTime, ui->spin_interval->value());
tempData.append(temperatureObject);
// update labels to show the data
showTemp(temp);
// add data to chart series
addDataToChart(temp);
}
//ui->txt_terminal->append(str);
addToTerminal(str);
}
}
void MainWindow::handleErrors(QSerialPort::SerialPortError error){
if(error == QSerialPort::ResourceError){
serial->close();
//ui->txt_terminal->append("Error happened.");
addToTerminal("<b>Error happened!</b>");
}
}
void MainWindow::showTemp(double temp) {
QString temp_str = "<font color='%1'>%2 °C</font>";
QString hourTemp_str = "<font color='%1'>%2 °C</font>";
QString dayTemp_str = "<font color='%1'>%2 °C</font>";
double hourTemp = Avg(3600);
double dayTemp = Avg(86400);
// Create Color for the current temperatur label
QColor color = QColor(0,0,0);
QColor hourColor = QColor(0,0,0);
QColor dayColor = QColor(0,0,0);
int lowTemp = 15;
//int normalTemp = 20;
int highTemp = 25;
int colorHue = 120;
int hourColorHue = 120;
int dayColorHue = 120;
if(temp < lowTemp)
colorHue = 240;
if(temp > highTemp)
colorHue = 0;
if(temp > lowTemp && temp < highTemp)
colorHue = 120;
color.setHsv(colorHue, 255, 200, 255);
if(hourTemp < lowTemp)
hourColorHue = 240;
if(hourTemp > highTemp)
hourColorHue = 0;
if(hourTemp > lowTemp && hourTemp < highTemp)
hourColorHue = 120;
hourColor.setHsv(hourColorHue, 255, 200, 255);
if(dayTemp < lowTemp)
dayColorHue = 240;
if(dayTemp > highTemp)
dayColorHue = 0;
if(dayTemp > lowTemp && dayTemp < highTemp)
dayColorHue = 120;
dayColor.setHsv(dayColorHue, 255, 200, 255);
// Set new Text for the temperature labels
ui->lbl_currentTemp->setText(temp_str.arg(color.name(), QString::number(temp)));
ui->lbl_hourAvg->setText(hourTemp_str.arg(hourColor.name(), QString::number(hourTemp,'f', 2)));
ui->lbl_dayAvg->setText(dayTemp_str.arg(dayColor.name(), QString::number(dayTemp,'f', 2)));
}
void MainWindow::addDataToChart(double temp) {
int momentInTime = QDateTime::currentDateTime().toMSecsSinceEpoch();
series->append((momentInTime - startTime)/1000, temp);
if(chart->series().count() != 0)
chart->removeSeries(series);
chart->addSeries(series);
chart->createDefaultAxes();
}
void MainWindow::addToTerminal(QString data) {
QDateTime currentTime = QDateTime::currentDateTime();
QString string = currentTime.toString() + ": " + data;
ui->txt_terminal->append(string);
}
double MainWindow::Avg(int seconds) {
int totalSeconds = 0;
double tempSum = 0;
int totalSets = 0;
for(int i=tempData.length()-1; i >= 0; i--) {
totalSeconds += (tempData.at(i).interval * 5);
tempSum += tempData.at(i).temp;
totalSets++;
if(totalSeconds >= seconds)
break;
}
return tempSum / totalSets;
}
void MainWindow::getComPorts() {
// GET AVAILABLE COM PORTS
QList<QSerialPortInfo> ports = QSerialPortInfo::availablePorts();
if(ports.length() > 0) {
ui->dd_ports->removeItem(0);
for(int i = 0; i < ports.length(); i++)
ui->dd_ports->addItem(ports[i].portName());
} else {
addToTerminal("<b>No available ports found!</b>");
for(int i = 0; i < ui->dd_ports->count(); i++){
ui->dd_ports->removeItem(i);
}
ui->dd_ports->addItem("COM");
}
}