summaryrefslogtreecommitdiff
path: root/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'main.cpp')
-rw-r--r--main.cpp150
1 files changed, 150 insertions, 0 deletions
diff --git a/main.cpp b/main.cpp
new file mode 100644
index 0000000..6ea0a15
--- /dev/null
+++ b/main.cpp
@@ -0,0 +1,150 @@
+#include <QApplication>
+#include <QNetworkAccessManager>
+#include <QNetworkReply>
+#include <QQmlApplicationEngine>
+#include <QQmlContext>
+#include <QtConcurrent/QtConcurrentRun>
+#include <QTimer>
+
+#include "QmlCustomPlot.h"
+
+int main(int argc, char *argv[])
+{
+ QApplication app(argc, argv);
+
+ QQmlApplicationEngine engine;
+
+ qmlRegisterType<QmlCustomPlot>("QmlCustomPlot", 1, 0, "QmlCustomPlot");
+
+ auto plot = new QCustomPlot();
+ plot->setOpenGl(true);
+ engine.rootContext()->setContextProperty("myPlot", plot);
+ engine.rootContext()->setContextProperty("max_diff", 0.0);
+ engine.rootContext()->setContextProperty("avg_diff", 0.0);
+ engine.rootContext()->setContextProperty("median_diff", 0.0);
+
+ QVector<qreal> x_profile;
+ QVector<qreal> y_profile;
+ QVector<qreal> prev_y_profile;
+
+ for(int x = -640; x < 640; ++x)
+ {
+ double y = double(QRandomGenerator::global()->bounded(0, 80000)) / 100.;
+
+ x_profile << x;
+ y_profile << y;
+ }
+ prev_y_profile = y_profile;
+
+ auto graph = plot->addGraph();
+ graph->setPen(QPen(QBrush(Qt::red), 2));
+ graph->setLineStyle(QCPGraph::lsLine);
+ graph->setScatterStyle(QCPScatterStyle::ssDot);
+ graph->setData(x_profile, y_profile);
+
+ QNetworkRequest request(QUrl("http://rpi5:8081/v1/pixels"));
+ auto manager = new QNetworkAccessManager(&app);
+
+ QTimer pixelsAutoRestartTimer(&app);
+ pixelsAutoRestartTimer.setInterval(1000);
+ pixelsAutoRestartTimer.setSingleShot(false);
+ QObject::connect(&pixelsAutoRestartTimer, &QTimer::timeout,
+ [&](){
+ manager->get(request);
+ });
+
+ QObject::connect(manager, &QNetworkAccessManager::finished,
+ [&](QNetworkReply *reply) {
+ // qDebug() << "replyFinished";
+ if (reply->error()) {
+ qDebug() << "replyFinished:" << reply->errorString();
+ return;
+ }
+
+ auto jsonPixels = QJsonDocument::fromJson(reply->readAll())
+ .object()["pixels"].toArray();
+
+ // y_profile.clear();
+
+ for (int i = 0; i < jsonPixels.count(); ++i) {
+ y_profile[i] = jsonPixels[i].toDouble();
+ }
+
+ graph->setData(x_profile, y_profile);
+
+ QVector<qreal> y_diff(y_profile.count());
+ qreal max_diff { 0. };
+ qreal min_diff { 1000000. };
+ size_t max_diff_idx = 0;
+ size_t min_diff_idx = 0;
+ qreal avg_diff { 0. };
+
+ for (size_t i = 0; i < y_profile.count(); ++i) {
+ y_diff[i] = y_profile[i] - prev_y_profile[i];
+
+ if (y_diff[i] > max_diff) {
+ max_diff = y_diff[i];
+ max_diff_idx = i;
+ }
+
+ if (y_diff[i] < min_diff) {
+ min_diff = y_diff[i];
+ min_diff_idx = i;
+ }
+
+ avg_diff += fabs(y_diff[i]);
+ }
+
+ avg_diff /= y_diff.size();
+
+ // for (size_t i = 0; i < y_profile.size(); ++i) {
+ // if
+ // }
+
+ // // const auto max_diff = std::max_element(y_diff.begin(), y_diff.end());
+ // const auto avg_diff = std::accumulate(y_diff.begin(), y_diff.end(), 0.0) / y_diff.size();
+ std::sort(y_diff.begin(), y_diff.end());
+ const auto median_diff = y_diff[y_diff.size() * 3 / 4];
+
+ engine.rootContext()->setContextProperty("max_diff", max_diff);
+ engine.rootContext()->setContextProperty("max_diff_idx", int(max_diff_idx));
+ engine.rootContext()->setContextProperty("min_diff", min_diff);
+ engine.rootContext()->setContextProperty("min_diff_idx", int(min_diff_idx));
+ engine.rootContext()->setContextProperty("avg_diff", avg_diff);
+ engine.rootContext()->setContextProperty("median_diff", median_diff);
+
+ manager->get(request);
+ pixelsAutoRestartTimer.start();
+
+ prev_y_profile = y_profile;
+ });
+
+ manager->get(request);
+ pixelsAutoRestartTimer.start();
+
+ // auto pixelsRequestFuture = QtConcurrent::run([&](){
+ // QThread::sleep(1);
+ // // x_profile.clear();
+ // y_profile.clear();
+
+ // for(int x = -640; x < 640; ++x)
+ // {
+ // double y = double(QRandomGenerator::global()->bounded(0, 80000)) / 100.;
+
+ // // x_profile << x;
+ // y_profile << y;
+ // }
+
+ // graph->setData(x_profile, y_profile);
+ // });
+
+ QObject::connect(
+ &engine,
+ &QQmlApplicationEngine::objectCreationFailed,
+ &app,
+ []() { QCoreApplication::exit(-1); },
+ Qt::QueuedConnection);
+ engine.loadFromModule("eurydice", "Main");
+
+ return app.exec();
+}