-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
291 lines (272 loc) · 7.68 KB
/
mainwindow.cpp
File metadata and controls
291 lines (272 loc) · 7.68 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
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
#include "mainwindow.h"
#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->btn_motor_stop->setStyleSheet(BACKGROUND_COLOR_GREEN);
ui->lcd_humi->setNumDigits(8);
ui->lcd_humi->setPalette(Qt::black);
ui->lcd_temp->setNumDigits(8);
ui->lcd_temp->setPalette(Qt::black);
ui->lcd_lig->setNumDigits(8);
ui->lcd_lig->setPalette(Qt::black);
ui->text_log->setReadOnly(true);
ui->horizontalSlider_pwm->setRange(1, 9);
ui->horizontalSlider_pwm->setTickPosition(QSlider::TicksRight);
ui->horizontalSlider_pwm->setPageStep(1);
pe_black.setColor(QPalette::WindowText,Qt::black);
pe_red.setColor(QPalette::WindowText,Qt::red);
serial_server = new SerialService();
socket_server = new SocketService();
temp_humi_light = new Temp_Humi_Light();
motor = new Motor();
relays = new Relays();
touch = new Touch();
smoke = new Smoke();
infray = new InfRay();
pwm = new Pwm();
ultra = new Ultra();
shake = new Shake();
connect(ui->horizontalSlider_pwm, SIGNAL(valueChanged(int)), this, SLOT(changePwm(int)));
connect(this->serial_server, SIGNAL(receiveMsgFromSerial(QByteArray)), this, SLOT(processMsgFromSerial(QByteArray)));
connect(this->socket_server, SIGNAL(receiveMsgFromSocket(QString)), this, SLOT(processMsgFromSocket(QString)));
connect(this, SIGNAL(addLog(QString)), this, SLOT(showLog(QString)));
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::showLog(QString log)
{
ui->text_log->append(log);
}
void MainWindow::updateTempHumiLig()
{
ui->lcd_temp->display(temp_humi_light->getTemperature());
ui->lcd_humi->display(temp_humi_light->getHumidity());
ui->lcd_lig->display(QString::number(temp_humi_light->getLight(), 'f', 1));
}
void MainWindow::processMsgFromSerial(QByteArray msg)
{
if(msg.length() < 6)
return;
//温湿亮度
if(msg[3] == 0x02 && msg[4] == 0x01)
{
temp_humi_light->setValue(msg);
updateTempHumiLig();
}
//触摸事件
else if(msg[3] == 0x07 && msg[4] == 0x01)
{
if(msg[5] == 0x01)
{
touch->setState(1);
ui->label_touch->setPalette(pe_red);
ui->label_touch->setText("touch");
}
else if(msg[5] == 0x00)
{
touch->setState(0);
ui->label_touch->setPalette(pe_black);
ui->label_touch->setText("no touch");
}
}
//烟雾事件
else if(msg[3] == 0x04 && msg[4] == 0x01)
{
if(msg[5] == 0x01)
{
smoke->setState(1);
ui->label_fire->setPalette(pe_red);
ui->label_fire->setText("smoke");
}
else if(msg[5] == 0x00)
{
smoke->setState(0);
ui->label_fire->setPalette(pe_black);
ui->label_fire->setText("no smoke");
}
}
//热释红外事件
else if(msg[3] == 0x05 && msg[4] == 0x01)
{
if(msg[5] == 0x01)
{
infray->setState(1);
ui->label_inf_ray->setPalette(pe_red);
ui->label_inf_ray->setText("signal");
}
else if(msg[5] == 0x00)
{
infray->setState(0);
ui->label_inf_ray->setPalette(pe_black);
ui->label_inf_ray->setText("no signal");
}
}
//振动事件
else if(msg[3] == 0x03 && msg[4] == 0x01)
{
if(msg[5] == 0x01)
{
shake->setState(1);
ui->label_shake->setPalette(pe_red);
ui->label_shake->setText("shake");
}
else if(msg[5] == 0x00)
{
shake->setState(0);
ui->label_shake->setPalette(pe_black);
ui->label_shake->setText("no shake");
}
}
//超声波事件
else if(msg[3] == 0x08 && msg[4] == 0x01)
{
int len = msg[5] * 256 + msg[6];
ui->label_ultra->setText(QString::number(len));
ultra->setState(len);
}
emit addLog(msg.toHex());
}
void MainWindow::processMsgFromSocket(QString msg)
{
//测试用,无协议
if(msg == "0")
changeMotor(0);
if(msg == "1")
changeMotor(1);
if(msg == "2")
changeMotor(2);
}
void MainWindow::changePwm(int value)
{
pwm->setState(value);
unsigned char range = Pwm::RANGE[value];
Pwm::MSG_PWM[4] = range;
Pwm::MSG_PWM[5] = 0x50 + range;
if(-1 == serial_server->writeToSerial(Pwm::MSG_PWM))
{
emit addLog("failed");
return;
}
emit addLog("success");
}
void MainWindow::on_btn_relay_clicked()
{
changeRelay(relays->getState());
}
void MainWindow::changeRelay(int state)
{
//对传入的参数取反操作
if(1 == state)
{
if(-1 == serial_server->writeToSerial(Relays::MSG_RELAY_CLOSE))
{
emit addLog("failed");
return;
}
relays->setState(0);
ui->btn_relay->setStyleSheet(BACKGROUND_COLOR_WHITE);
ui->btn_relay->setText("open");
emit addLog("success");
}
else if(0 == state)
{
if(-1 == serial_server->writeToSerial(Relays::MSG_RELAY_OPEN))
{
emit addLog("failed");
return;
}
relays->setState(1);
ui->btn_relay->setStyleSheet(BACKGROUND_COLOR_GREEN);
ui->btn_relay->setText("close");
emit addLog("success");
}
}
void MainWindow::on_btn_serial_clicked()
{
changeSerial(serial_server->getComState());
}
void MainWindow::changeSerial(int state)
{
//对传入的参数取反操作
if(1 == state)
{
if(serial_server->closeCom())
{
ui->btn_serial->setText("open");
ui->btn_serial->setStyleSheet(BACKGROUND_COLOR_WHITE);
emit addLog("close serial port success");
}
else
emit addLog("close serial port failed");
}
else if(0 == state)
{
if(serial_server->openCom())
{
ui->btn_serial->setText("close");
ui->btn_serial->setStyleSheet(BACKGROUND_COLOR_GREEN);
emit addLog("open serial port success");
}
else
emit addLog("open serial port failed");
}
}
void MainWindow::on_btn_motor_f_clicked()
{
changeMotor(1);
}
void MainWindow::on_btn_motor_b_clicked()
{
changeMotor(2);
}
void MainWindow::on_btn_motor_stop_clicked()
{
changeMotor(0);
}
void MainWindow::changeMotor(int state)
{
if(0 == state)
{
if(-1 == serial_server->writeToSerial(Motor::MSG_MOTOR_STOP))
{
emit addLog("failed");
return;
}
motor->setState(0);
ui->btn_motor_stop->setStyleSheet(BACKGROUND_COLOR_GREEN);
ui->btn_motor_b->setStyleSheet(BACKGROUND_COLOR_WHITE);
ui->btn_motor_f->setStyleSheet(BACKGROUND_COLOR_WHITE);
emit addLog("success");
}
else if(1 == state)
{
if(-1 == serial_server->writeToSerial(Motor::MSG_MOTOR_FORE))
{
emit addLog("failed");
return;
}
motor->setState(1);
ui->btn_motor_f->setStyleSheet(BACKGROUND_COLOR_GREEN);
ui->btn_motor_b->setStyleSheet(BACKGROUND_COLOR_WHITE);
ui->btn_motor_stop->setStyleSheet(BACKGROUND_COLOR_WHITE);
emit addLog("success");
}
else if(2 == state)
{
if(-1 == serial_server->writeToSerial(Motor::MSG_MOTOR_BACK))
{
emit addLog("failed");
return;
}
motor->setState(2);
ui->btn_motor_b->setStyleSheet(BACKGROUND_COLOR_GREEN);
ui->btn_motor_f->setStyleSheet(BACKGROUND_COLOR_WHITE);
ui->btn_motor_stop->setStyleSheet(BACKGROUND_COLOR_WHITE);
emit addLog("success");
}
}