-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmainwindow.cpp
More file actions
132 lines (106 loc) · 2.99 KB
/
Copy pathmainwindow.cpp
File metadata and controls
132 lines (106 loc) · 2.99 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
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <../JoystickServer/joystickbuttons.h>
#include <../JoystickServer/programprotocol.h>
#include <QTcpSocket>
#include <QDebug>
#include <QKeyEvent>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
this->installEventFilter(this);
}
MainWindow::~MainWindow()
{
delete ui;
}
void MainWindow::on_btnStart_clicked()
{
if(socket.state() == QAbstractSocket::ConnectedState)
{
socket.close();
ui->btnStart->setText("Start");
}else{
const QString address = "127.0.0.1";
const quint16 port = 1060;
socket.setSocketOption(QAbstractSocket::LowDelayOption,1);
socket.connectToHost(address,port);
ui->btnStart->setText("Stop");
}
}
void MainWindow::sendData(int button, bool press)
{
const bool newProtocol = true;
if(newProtocol){
char data[9];
sprintf(data,"MSG004%02d%s",button,press?"S":"N");
socket.write(data,9);
}else{
const int packetSize = Protocol::headerSize() + Protocol::dataSize();
//Armo el packete
Protocol::header_t header;
Protocol::data_t data;
header.size = Protocol::dataSize();
strcpy((char*)&header.validationString,(char*)Protocol::ValidationString);
data.btn = button;
data.press = press ? 1 : 0;
char* buffer = (char*)malloc(packetSize);
memcpy(buffer,&header,Protocol::headerSize());
memcpy(buffer+Protocol::headerSize(),&data,Protocol::dataSize());
socket.write(buffer,packetSize);
free(buffer);
}
}
void MainWindow::on_btnUp_pressed()
{
const int btn = Joystick::BTN_UP;
sendData(btn,true);
}
void MainWindow::on_btnDown_pressed()
{
const int btn = Joystick::BTN_DOWN;
sendData(btn,true);
}
void MainWindow::on_btnLeft_pressed()
{
const int btn = Joystick::BTN_LEFT;
sendData(btn,true);
}
void MainWindow::on_btnRight_pressed()
{
const int btn = Joystick::BTN_RIGHT;
sendData(btn,true);
}
void MainWindow::on_btnUp_released()
{
const int btn = Joystick::BTN_UP;
sendData(btn,false);
}
void MainWindow::on_btnDown_released()
{
const int btn = Joystick::BTN_DOWN;
sendData(btn,false);
}
void MainWindow::on_btnLeft_released()
{
const int btn = Joystick::BTN_LEFT;
sendData(btn,false);
}
void MainWindow::on_btnRight_released()
{
const int btn = Joystick::BTN_RIGHT;
sendData(btn,false);
}
bool MainWindow::eventFilter(QObject *watched, QEvent *event)
{
if(event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease)
{
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
qDebug() << QKeySequence(keyEvent->key()).toString()
<< ((event->type() == QEvent::KeyPress) ? "PRESS" : "RELEASE");
ui->lineEdit->setText(ui->lineEdit->text().append(QKeySequence(keyEvent->key()).toString()));
}
return QMainWindow::eventFilter(watched,event);
}