-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.cpp
113 lines (94 loc) · 2.65 KB
/
widget.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
#include "widget.h"
#include "./ui_widget.h"
#include <unistd.h>
Widget::Widget(QWidget *parent)
: QWidget(parent)
, ui(new Ui::Widget)
{
ui->setupUi(this);
rot_model_t rotModel = ROT_MODEL_ANDROIDSENSOR;
rot = rot_init(rotModel);
timer = new QTimer(this);
connect(timer, &QTimer::timeout, this, &Widget::timeout);
timer->start(20);
server = new QTcpServer(this);
connect(server, &QTcpServer::newConnection, this, &Widget::serverNewConnection);
server->listen(QHostAddress::Any, 4533);
client = NULL;
connect(ui->pushButton_2, &QPushButton::toggled, ui->polarWidget, &PolarWidget::setRotationUnlock);
connect(ui->pushButton_3, &QPushButton::clicked, ui->polarWidget, &PolarWidget::resetRotationAngle);
}
Widget::~Widget()
{
delete ui;
}
void Widget::serverNewConnection()
{
while (server->hasPendingConnections()) {
if(client != NULL) {
disconnect(client, &QTcpSocket::readyRead, this, &Widget::clientReadyRead);
client->close();
client->deleteLater();
}
client = server->nextPendingConnection();
connect(client, &QTcpSocket::readyRead, this, &Widget::clientReadyRead);
}
}
void Widget::clientReadyRead()
{
float az, el;
QTextStream readText(client->readAll());
char head = readText.read(1).toLatin1().at(0);
switch (head) {
case 'P':{
readText >> az >> el;
rot->caps->set_position(rot, az, el);
client->write("RPRT 0\n");
break;
}
case 'p':
{
rot->caps->get_position(rot, &az, &el);
QString writeText = QString::number(az,'f',2) + "\n" + QString::number(el,'f',2) + "\n";
client->write(writeText.toLatin1());
break;
}
case 'S':
{
client->write("RPRT 0\n");
break;
}
case 'q':
{
disconnect(client, &QTcpSocket::readyRead, this, &Widget::clientReadyRead);
client->close();
client->deleteLater();
client = NULL;
break;
}
default:
{
client->write("RPRT -1\n");
break;
}
}
}
void Widget::timeout()
{
struct androidsensor_rot_priv_data
{
void *imu;
azimuth_t target_az;
elevation_t target_el;
};
struct androidsensor_rot_priv_data *priv = (struct androidsensor_rot_priv_data*)rot->state.priv;
float az, el;
rot->caps->get_position(rot, &az, &el);
ui->polarWidget->setCrosshairPoint(az, el);
ui->polarWidget->setCirclePoint(priv->target_az, priv->target_el);
}
void Widget::on_pushButton_clicked()
{
server->close();
server->listen(QHostAddress::Any, ui->lineEdit->text().toUInt());
}