summaryrefslogtreecommitdiff
path: root/src/printerclient.cpp
diff options
context:
space:
mode:
authorNikita Kostovsky <nikita@kostovsky.me>2026-02-26 16:33:12 +0100
committerNikita Kostovsky <nikita@kostovsky.me>2026-02-26 16:33:12 +0100
commita688e436f03309d5813b68a375f694412018ca0b (patch)
tree8a5cdbb170fbb8417de42a09832d2eaf527ff61e /src/printerclient.cpp
parent1664027209ea3b8eb327b7755e4111577e66a2ee (diff)
add sync moveSteps
Diffstat (limited to 'src/printerclient.cpp')
-rw-r--r--src/printerclient.cpp55
1 files changed, 53 insertions, 2 deletions
diff --git a/src/printerclient.cpp b/src/printerclient.cpp
index 4fed38f..3a548e9 100644
--- a/src/printerclient.cpp
+++ b/src/printerclient.cpp
@@ -1,11 +1,15 @@
#include "printerclient.h"
-#include <exception>
-
+#include <QCoreApplication>
#include <QDebug>
#include <QFile>
+#include <QNetworkReply>
#include <QSerialPort>
#include <QSerialPortInfo>
+#include <QThread>
+#include <QUrlQuery>
+
+#include "macro.h"
QString getFirstTtyUSB()
{
@@ -84,3 +88,50 @@ 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}}
+{}
+
+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));
+
+ QUrl url{m_apiRoot + QStringLiteral("/move")};
+ url.setQuery(query);
+
+ std::mutex mtx;
+
+ const auto reply = m_manager->get(QNetworkRequest{url});
+ mtx.lock();
+
+ connect(reply, &QNetworkReply::finished, this, [reply, &mtx]() {
+ qDebug() << reply->readAll();
+ mtx.unlock();
+ });
+
+ 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();
+
+ return true;
+}