-
Notifications
You must be signed in to change notification settings - Fork 0
/
rgbcontroller.cpp
121 lines (65 loc) · 2.46 KB
/
rgbcontroller.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
#include "rgbcontroller.h"
#include <QDebug>
#include <QColor>
RGBController::RGBController()
{
}
void RGBController::setConnectionParameters(QString comName, int baud){
this->comName = comName;
this->baud = baud;
}
void RGBController::connectToDevice(){
qDebug () << "Connecting...";
if (!this->isOpen()){
this->setPortName(comName);
this->setBaudRate(baud);
this->setFlowControl(QSerialPort::NoFlowControl);
this->setDataBits(QSerialPort::Data8);
this->setStopBits(QSerialPort::OneStop);
emit isConnecting();
this->open(QIODevice::ReadWrite);
emit isConnectingFinish();
}
connect(this,SIGNAL(readyRead()),this,SLOT(getTemperatureSlot()));
}
void RGBController::disconnectFromDevice(){
this->close();
}
bool RGBController::getConnectionStatus(){
return this->isOpen();
}
void RGBController::sendCommand(QString command){
const QByteArray &commandArray = command.toLocal8Bit();
this->write(commandArray);
}
void RGBController::setRGB(int r, int g, int b){
this->sendCommand("AT+RGB="+QString::number(r)+","+QString::number(g)+","+QString::number(b)+"\r\n");
}
void RGBController::setHSV(int h, int s, int v){
this->sendCommand("AT+HSV="+QString::number(h)+","+QString::number(s)+","+QString::number(v)+"\r\n");
}
void RGBController::setAnimation(int type, int speed, int step){
this->sendCommand("AT+ANIM="+QString::number(type)+","+QString::number(speed)+","+QString::number(step)+"\r\n");
}
void RGBController::setAnimation(int type, int speed, int step, int red, int green, int blue){
qDebug () << "Type: " << type << " Speed: " << speed << " Step: " << step << " Red: " << red << " Green: " << green << " Blue: " << blue;
QColor color;
color.setRgb(red,green,blue);
int h;
int s;
int v;
color.getHsv(&h,&s,&v);
int sVal = (s * 100) / 255;
int vVal = (v * 100) / 255;
qDebug () << "H: " << h << " S: " << sVal << " V: "<< vVal;
this->sendCommand("AT+ANIM="+QString::number(type)+","+QString::number(speed)+","+QString::number(step)+","+QString::number(h)+","+QString::number(sVal)+","+QString::number(vVal)+"\r\n");
}
void RGBController::sendGetTemperatureRequest(){
this->sendCommand("AT+TEMP?\r\n");
}
void RGBController::getTemperatureSlot(){
if (this->canReadLine()){
QString temperature = QString::fromLatin1(this->readAll());
emit getTemperature(temperature);
}
}