This repository was archived by the owner on Feb 4, 2023. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
78 lines (71 loc) · 2.37 KB
/
Copy pathmainwindow.cpp
File metadata and controls
78 lines (71 loc) · 2.37 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QUrl>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->colorFrame->setStyleSheet(lamp_off);
connect(&socket, &QTcpSocket::connected, this, &MainWindow::socket_connected);
connect(&socket, &QTcpSocket::disconnected, this, &MainWindow::socket_disconnected);
connect(&socket, &QTcpSocket::readyRead, this, &MainWindow::data_recieved);
}
MainWindow::~MainWindow()
{
socket.disconnectFromHost();
delete ui;
}
void MainWindow::on_connectButton_clicked()
{
if (ui->connectButton->text() == "Connect"){
QUrl url (ui->urlEdit->text());
socket.connectToHost(url.host(),url.port());
}
else{
socket.disconnectFromHost();
}
}
void MainWindow::socket_connected()
{
ui->connectButton->setText("Disconnect");
qDebug() << "Connected fine";
}
void MainWindow::socket_disconnected()
{
ui->connectButton->setText("Connect");
qDebug() << "Disconnected";
}
void MainWindow::data_recieved(){
QByteArray data = socket.readAll();
// Assume data.at(1) is always zero as we're never going to recieve more than 3 bytes of data
// Nothing bad happens otherwise, so let's just skeep the test
switch (data.at(0)){
case 0x12:
// Turn the lamp on
if (data.at(2) != 0)
qDebug() << "Unknown command extention detected: lamp_on. Perhaps you should update this app.";
ui->colorFrame->setStyleSheet(lamp_on_no_color);
break;
case 0x13:
// Turn the lamp off
if (data.at(2) != 0)
qDebug() << "Unknown command extention detected: lamp_off. Perhaps you should update this app.";
ui->colorFrame->setStyleSheet(lamp_off);
break;
case 0x20:
if (data.at(2) != 3){
qDebug() << "Malformed command: color switch. Nothing done. Perhaps you should update this app.";
}
else {
QString red = QString::number((unsigned char) data.at(3));
QString green = QString::number((unsigned char) data.at(4));
QString blue = QString::number((unsigned char) data.at(5));
QString QSS = lamp_on_colored.arg(red).arg(green).arg(blue);
ui->colorFrame->setStyleSheet(QSS);
}
break;
default:
qDebug() << "Unknown command detected. Update the app.";
}
}