-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathgcodeplayer.cpp
243 lines (215 loc) · 6.43 KB
/
gcodeplayer.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
#include <QQmlEngine>
#include <QFile>
#include <QTcpSocket>
#include <QTimer>
#include <QDebug>
#include "gcodeplayer.h"
GcodePlayer::GcodePlayer(QObject *parent) : QObject(parent)
{
m_model = new GcodePlayerModel(this);
m_currentLineNumber = 0;
m_linesCount = 0;
m_state = Stopped;
m_tcp = new QTcpSocket(this);
m_connectionState = Disconnected;
emit connectionStateChanged();
connect(m_tcp, &QTcpSocket::stateChanged,
this, &GcodePlayer::onSocketStateChanged);
connect(m_tcp, QOverload<QAbstractSocket::SocketError>::of(&QAbstractSocket::error),
this, &GcodePlayer::onSocketError);
connect(m_tcp, &QIODevice::readyRead,
this, &GcodePlayer::onMCResponse);
m_querySent = false;
}
void GcodePlayer::registerQmlTypes()
{
qmlRegisterType<GcodePlayer>("tech.vhrd", 1, 0, "GcodePlayer");
}
GcodePlayerModel *GcodePlayer::model() const
{
return m_model;
}
void GcodePlayer::loadFile(const QUrl &fileUrl)
{
if (m_state == Playing || m_state == Paused) {
qWarning() << "Stop first";
return;
}
m_model->removeAll();
QString fileName;
if (fileUrl.isLocalFile())
fileName = fileUrl.toLocalFile();
else
return;
QFile f(fileName);
if (!f.open(QIODevice::ReadOnly)) {
qWarning() << "Can't open" << fileName << f.errorString();
return;
}
int lineNumber = 1;
while (!f.atEnd()) {
QString code = QString::fromLocal8Bit(f.readLine());
m_model->addItem(GcodePlayerItem(GcodePlayerItem::Pending, lineNumber, code));
lineNumber++;
}
m_currentLineNumber = 1;
emit currentLineChanged();
m_linesCount = lineNumber - 1;
emit linesCountChanged();
m_state = Stopped;
emit stateChanged();
}
int GcodePlayer::currentLineNumber() const
{
return m_currentLineNumber;
}
void GcodePlayer::setCurrentLineNumber(int currentLineNumber)
{
m_currentLineNumber = currentLineNumber;
}
int GcodePlayer::linesCount() const
{
return m_linesCount;
}
GcodePlayer::State GcodePlayer::state() const
{
return m_state;
}
GcodePlayer::ConnectionState GcodePlayer::connectionState() const
{
return m_connectionState;
}
void GcodePlayer::send(const QString &command)
{
if (m_connectionState != Disconnected) {
m_tcp->write(command.toLocal8Bit());
}
}
void GcodePlayer::connectToMC()
{
if (m_connectionState == Disconnected) {
m_tcp->connectToHost("192.168.88.77", 23);
QTimer::singleShot(2000, [=](){
if (this->m_connectionState != Connected)
this->m_tcp->abort();
});
}
}
void GcodePlayer::play()
{
if (m_state == Stopped) {
if (m_linesCount > 0) {
m_currentLineNumber = 1;
emit currentLineChanged();
m_model->changeAllStates(GcodePlayerItem::Pending);
sendNextLine();
m_state = Playing;
emit stateChanged();
} else {
qWarning() << "Nothing to play";
}
} else if (m_state == Paused) {
sendNextLine();
m_state = Playing;
emit stateChanged();
} else {
qWarning() << "Can't play from state" << m_state;
}
}
void GcodePlayer::pause()
{
m_state = Paused;
emit stateChanged();
}
void GcodePlayer::stop()
{
m_state = Stopped;
emit stateChanged();
}
void GcodePlayer::onSocketStateChanged(QAbstractSocket::SocketState state)
{
if (state == QAbstractSocket::ConnectedState) {
m_connectionState = Connected;
emit connectionStateChanged(true);
} else if (state == QAbstractSocket::UnconnectedState) {
m_connectionState = Disconnected;
emit connectionStateChanged(false);
} else {
m_connectionState = Connecting;
emit connectionStateChanged(false);
}
emit connectionStateChanged();
}
void GcodePlayer::onSocketError(QAbstractSocket::SocketError error)
{
switch (error) {
case QAbstractSocket::RemoteHostClosedError:
break;
case QAbstractSocket::HostNotFoundError:
// QMessageBox::information(this, tr("Fortune Client"),
// tr("The host was not found. Please check the "
// "host name and port settings."));
break;
case QAbstractSocket::ConnectionRefusedError:
// QMessageBox::information(this, tr("Fortune Client"),
// tr("The connection was refused by the peer. "
// "Make sure the fortune server is running, "
// "and check that the host name and port "
// "settings are correct."));
break;
default:
// QMessageBox::information(this, tr("Fortune Client"),
// tr("The following error occurred: %1.")
// .arg(tcpSocket->errorString()));
break;
}
}
void GcodePlayer::onMCResponse()
{
QByteArray bytes = m_tcp->readAll();
for (int i = 0; i < bytes.length(); ++i) {
if (bytes[i] == '\r')
continue;
if (bytes[i] == '\n') {
if (!m_tcpLine.isEmpty()) {
processMCResponse(m_tcpLine);
m_tcpLine.clear();
}
continue;
}
m_tcpLine.append(QChar(bytes[i]));
}
}
void GcodePlayer::sendNextLine()
{
GcodePlayerItem line = m_model->getItem(m_currentLineNumber - 1);
m_tcp->write(line.m_code.toLocal8Bit());
m_querySent = true;
}
void GcodePlayer::processMCResponse(const QString &line)
{
if (m_querySent) {
qDebug() << "mc q:" << line;
m_querySent = false;
GcodePlayerItem item = m_model->getItem(m_currentLineNumber - 1);
if (line == "ok") {
item.m_status = GcodePlayerItem::Ok;
m_model->replaceItem(m_currentLineNumber - 1, item);
} else {
item.m_response = line;
item.m_status = GcodePlayerItem::Warning;
m_model->replaceItem(m_currentLineNumber - 1, item);
}
m_currentLineNumber++;
if (m_currentLineNumber > m_linesCount) {
m_state = Stopped;
emit stateChanged();
} else {
emit currentLineChanged();
if (m_state == Playing)
sendNextLine();
}
} else {
qDebug() << "mc:" << line;
}
}