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
|
#include "printerclient.h"
#include <QCoreApplication>
#include <QDebug>
#include <QFile>
#include <QNetworkReply>
#include <QSerialPort>
#include <QSerialPortInfo>
#include <QThread>
#include <QUrlQuery>
#include "macro.h"
QString getFirstTtyUSB()
{
auto ports = QSerialPortInfo::availablePorts();
std::remove_if(ports.begin(), ports.end(), [](const auto& port) {
return !port.portName().contains("ttyUSB");
});
return ports.isEmpty() ? "" : ports.first().portName();
}
PrinterClient::PrinterClient(QObject* parent)
: QObject{parent} // , m_serialPort{new QSerialPort{"/dev/ttyUSB0", this}}
, m_serialPort{new QSerialPort{getFirstTtyUSB(), this}}
{
if (!m_serialPort->setBaudRate(QSerialPort::Baud115200)) {
throw std::runtime_error(
"serial port: cannot set baud rate: " +
m_serialPort->errorString().toStdString());
return;
}
if (!m_serialPort->open(QFile::ReadWrite)) {
throw std::runtime_error(
"cannot open serial port: " +
m_serialPort->errorString().toStdString());
return;
}
qDebug() << "serial port baud rate:" << m_serialPort->baudRate();
qDebug() << "serial port data bits:" << m_serialPort->dataBits();
qDebug() << "serial port parity:" << m_serialPort->parity();
qDebug() << "serial port stop bits:" << m_serialPort->stopBits();
QObject::connect(m_serialPort, &QSerialPort::readyRead,
this, &PrinterClient::onReadyRead);
QObject::connect(m_serialPort, &QSerialPort::errorOccurred,
this, &PrinterClient::onErrorOccured);
// m_serialPort->write(QByteArray { "G91\n" });
// m_serialPort->flush();
// m_serialPort->write(QByteArray { "G1 Z10\n" });
// m_serialPort->flush();
sendCommand("G91");
// sendCommand("G1 Z10");
// sendCommand("G1 Z-10");
onErrorOccured(QSerialPort::SerialPortError::PermissionError);
}
void PrinterClient::sendCommand(const QString command)
{
const auto written = m_serialPort->write(command.toUtf8() + "\n");
qDebug() << QString("serialPort: send '%1': (written %2 bytes)")
.arg(command).arg(written);
m_serialPort->flush();
}
void PrinterClient::onReadyRead()
{
const auto data = m_serialPort->readAll();
qDebug() << "serialPort: " << data;
// emit newData(data);
}
void PrinterClient::onErrorOccured(QSerialPort::SerialPortError error)
{
qWarning() << "serial port error:" << m_serialPort->errorString()
<< "-" << error;
}
Esp32Stand::Esp32Stand(const QHostAddress &address, const uint32_t port, const uint32_t stepsPerMm)
: m_apiRoot{QStringLiteral("http://") + address.toString() + ':' + QString::number(port)}
, INIT_FIELD(stepsPerMm)
// , m_manager{new QNetworkAccessManager{this}}
// WARNING: memleak
, m_manager{new QNetworkAccessManager{}}
{}
bool Esp32Stand::resetPosSteps()
{
m_posSteps = 0;
return true;
}
int Esp32Stand::posSteps()
{
return m_posSteps;
}
double Esp32Stand::posMm()
{
return m_posSteps / double(m_stepsPerMm);
}
bool Esp32Stand::moveMm(const double mm)
{
return moveSteps(mm * m_stepsPerMm);
}
bool Esp32Stand::moveSteps(const int steps)
{
qDebug() << __func__ << "move" << steps << "steps";
// QElapsedTimer t;
// t.start();
QUrlQuery query;
query.addQueryItem(QStringLiteral("steps"), QString::number(steps));
// qDebug() << "here-2";
QUrl url{m_apiRoot + QStringLiteral("/move")};
url.setQuery(query);
std::mutex mtx;
// qDebug() << "here-1";
const auto reply = m_manager->get(QNetworkRequest{url});
// qDebug() << "here-123";
mtx.lock();
// qDebug() << "here";
connect(reply, &QNetworkReply::finished, this, [reply, &mtx]() {
qDebug() << reply->readAll();
mtx.unlock();
});
// qDebug() << "here2";
while (!reply->isFinished()) {
reply->waitForReadyRead(100);
qApp->processEvents();
}
// TODO: fix this shit, EPS should reply when finished
// QThread::msleep(500);
std::lock_guard g{mtx};
// qDebug() << __func__ << "moved" << steps << "steps" << t.elapsed();
m_posSteps += steps;
emit moveFinished();
return true;
}
|