-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwin_main.cpp
executable file
·262 lines (234 loc) · 6.64 KB
/
win_main.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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include "win_main.h"
#include "ui_win_main.h"
win_main::win_main(QWidget *parent) :
QWidget(parent),
ui(new Ui::win_main)
{
ui->setupUi(this);
sPort = new QSerialPort();
sPort->setBaudRate(38400);
isConnected = false;
calibrePhase = true;
calibreCounter = 0;
gx_bias = 0;
gy_bias = 0;
gz_bias = 0;
LUx = -1;
LUy = -1;
detectDevice();
}
win_main::~win_main()
{
delete ui;
}
void win_main::receiveData()
{
inputData.append(sPort->readAll());
handleData();
}
void win_main::detectDevice()
{
if (isConnected)
{
ui->textEdit->append("\t|--ERROR You are already connected to " + currentPort);
return;
}
Q_FOREACH(QSerialPortInfo port, QSerialPortInfo::availablePorts())
{
ui->textEdit->append("Try on port: " + port.portName());
sPort->setPortName(port.portName());
if (!sPort->open(QIODevice::ReadWrite))
{
ui->textEdit->append(sPort->errorString());
return;
}
else
{
connect(sPort, SIGNAL(readyRead()), this, SLOT(receiveData()));
ui->textEdit->append("\t|--SUCCESS Successfully connected.");
isConnected = true;
currentPort = port.portName();
return;
}
}
ui->textEdit->append("\t|--ERROR No device detected!");
}
void win_main::handleData()
{
QStringList data = inputData.split('\n');
inputData.clear();
for (int i = 0; i < data.size(); i++)
{
if (data[i].size() == 0)
continue;
if (!data[i].contains('\r'))
inputData.append(data[i]);
else
{
data[i].replace("\r", "");
QStringList params = data[i].split('\t');
float x = 0;
if (params.size() != 8)
{
qDebug() << "\t|--ERROR Ambiguous on split, size is " << params.size();
break;
}
//ax
x = QVariant(params[1]).toFloat();
x /= 16384;
if (calibrePhase)
calibre(1, x);
else
updateData(1, x);
//ay
x = QVariant(params[2]).toFloat();
x /= 16384;
if (calibrePhase)
calibre(2, x);
else
updateData(2, x);
//az
x = QVariant(params[3]).toFloat();
x /= 16384;
if (calibrePhase)
calibre(3, x);
else
updateData(3, x);
//gx
x = QVariant(params[4]).toFloat();
x /= 131;
if (calibrePhase)
calibre(4, x);
else
updateData(4, x);
//gy
x = QVariant(params[5]).toFloat();
x /= 131;
if (calibrePhase)
calibre(5, x);
else
updateData(5, x);
//gz
x = QVariant(params[6]).toFloat();
x /= -131;
if (calibrePhase)
calibre(6, x);
else
updateData(6, x);
//button state
bool bState = QVariant(params[7]).toBool();
mouseLeftClick(bState);
}
}
}
void win_main::updateData(int index, float x)
{
float data = 0;
switch (index)
{
case 1:
data = ui->lineEdit_ax->text().toFloat();
data = ALPHA * data + (1 - ALPHA) * x;
ui->lineEdit_ax->setText(QString::number(data, 'f', 2));
break;
case 2:
data = ui->lineEdit_ay->text().toFloat();
data = ALPHA * data + (1 - ALPHA) * x;
ui->lineEdit_ay->setText(QString::number(data, 'f', 2));
break;
case 3:
data = ui->lineEdit_az->text().toFloat();
data = ALPHA * data + (1 - ALPHA) * x;
ui->lineEdit_az->setText(QString::number(data, 'f', 2));
break;
case 4:
data = ui->lineEdit_gx->text().toFloat();
data = ALPHA * data + (1 - ALPHA) * (x - gx_bias);
ui->lineEdit_gx->setText(QString::number(data, 'f', 2));
break;
case 5:
data = ui->lineEdit_gy->text().toFloat();
data = ALPHA * data + (1 - ALPHA) * (x - gy_bias);
ui->lineEdit_gy->setText(QString::number(data, 'f', 2));
moveMouseY((int) data);
break;
case 6:
data = ui->lineEdit_gz->text().toFloat();
data = ALPHA * data + (1 - ALPHA) * (x - gz_bias);
ui->lineEdit_gz->setText(QString::number(data, 'f', 2));
moveMouseX((int) data);
break;
default:
break;
}
}
void win_main::calibre(int index, float x)
{
switch (index)
{
case 4:
gx_bias = gx_bias + x;
calibreCounter++;
break;
case 5:
gy_bias = gy_bias + x;
break;
case 6:
gz_bias = gz_bias + x;
break;
default:
break;
}
if (calibreCounter == CALIBRE_COUNTER)
{
gx_bias /= calibreCounter;
gy_bias /= calibreCounter;
gz_bias /= calibreCounter;
calibrePhase = false;
ui->textEdit->append("\t|--SUCCESS Calibration finished");
ui->textEdit->append("\t\t| gx = " + QString::number(gx_bias, 'f', 2));
ui->textEdit->append("\t\t| gy = " + QString::number(gy_bias, 'f', 2));
ui->textEdit->append("\t\t| gz = " + QString::number(gz_bias, 'f', 2));
}
}
void win_main::moveMouseX(int vx)
{
if (LUx == -1)
LUx = QTime::currentTime().msecsSinceStartOfDay();
else
{
int diff = QTime::currentTime().msecsSinceStartOfDay() - LUx;
LUx += diff;
QPoint pos = QCursor::pos();
pos.setX(pos.x() + SENSIVITY * (vx * diff) / 1000);
QCursor::setPos(pos);
}
}
void win_main::moveMouseY(int vy)
{
if (LUy == -1)
LUy = QTime::currentTime().msecsSinceStartOfDay();
else
{
int diff = QTime::currentTime().msecsSinceStartOfDay() - LUy;
LUy += diff;
QPoint pos = QCursor::pos();
pos.setY(pos.y() + 50 * (vy * diff) / 1000);
QCursor::setPos(pos);
}
}
void win_main::mouseLeftClick(bool clicked)
{
INPUT input[2] = {0};
if (clicked)
{
input[0].type = INPUT_MOUSE;
input[0].mi.dwFlags = MOUSEEVENTF_LEFTDOWN;
}
else
{
input[0].type = INPUT_MOUSE;
input[0].mi.dwFlags = MOUSEEVENTF_LEFTUP;
}
SendInput(1, input, sizeof(INPUT));
}