summaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp259
1 files changed, 47 insertions, 212 deletions
diff --git a/src/main.cpp b/src/main.cpp
index 2ec6c0c..422a476 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -444,10 +444,6 @@ int main(int argc, char* argv[])
QHttpServer qHttpServer;
auto httpGetImage = [&]() -> QHttpServerResponse {
- // return QHttpServerResponse::StatusCode::ServiceUnavailable;
- // std::cout << "http: image" << std::endl << std::flush;
- // FILE *f = fopen("/tmp/img.pgm", "w");
- // static bool save = false;
auto cam = dynamic_cast<VeyeIMX287m *>(camera.get());
if (!cam) {
@@ -456,28 +452,21 @@ int main(int argc, char* argv[])
}
static Image img;
- // yeaah
- // ::img = &img;
+
if (!cam->getImage(img)) {
qDebug() << "cannot get image";
return QHttpServerResponse::StatusCode::ServiceUnavailable;
}
- // std::cout << "http: got image" << std::endl << std::flush;
- // pgm_save(::img);
+
pgm_save(&img);
std::lock_guard<std::mutex> lg(pgm_image_mtx);
- // save = false;
- // qDebug() << "mutex locked";
- // qDebug() << "image saved to array";
+
return QHttpServerResponse{QByteArray((const char *) pgm_image, pgm_image_size),
QHttpServerResponse::StatusCode::Ok};
};
qHttpServer.route("/v1/sensor/image", httpGetImage);
qHttpServer.route("/v1/sensor/image2", httpGetImage);
- // qHttpServer.route("/v1/sensor/exposureTimeUs", [&]() {
- // // std::lock_guard<std::mutex> lg(pgm_image_mtx);
- // return "123";
- // });
+
qHttpServer.route("/v1/pixels", [&]() -> QHttpServerResponse {
// std::cout << "http: pixels" << std::endl;
// return QHttpServerResponse::StatusCode::ServiceUnavailable;
@@ -498,99 +487,51 @@ int main(int argc, char* argv[])
}
::pixels = std::move(img.pixels());
-
- // std::lock_guard<std::mutex> lg(pgm_image_mtx);
+ ++cam->processedCounter;
// qt json does not allow to limit double precision
-
- QJsonArray pixels;
-
- for (size_t i = 0; i < img_width; ++i) {
- // pixels << img_height - img.pixels[i];
- pixels << ::pixels.pixels[i];
- // rjPixels.PushBack(::pixels.pixels[i], jd.GetAllocator());
- // rjPixels.PushBack(rapidjson::Value(::pixels.pixels[i]), al);
- }
-
- QJsonObject json;
- json["pixels"] = pixels;
- // json["encoderPosition"] = qint64{encoder.position()};
- // FIXME: get prom pixels struct
- json["measurementCounter"] = qint64{img.counters.measurementCounter};
- json["timestampUs"] = qint64(img.counters.timestampUs);
-
const auto lines = pixelsToLines(::pixels);
- // qDebug() << "lines count is " << lines.count();
+ const auto nan2zero = [](const auto &value) { return qIsNaN(value) ? 0 : value; };
+ rapidjson::Document jd;
+ jd.SetObject();
+ auto &al = jd.GetAllocator();
- QJsonArray jsonLines;
+ rapidjson::Value rjPixels{rapidjson::kArrayType};
- for (const auto& l : lines)
- {
- const auto p1 = (l.p1() * 1000).toPoint().toPointF() / 1000.;
- const auto p2 = (l.p2() * 1000).toPoint().toPointF() / 1000.;
- // jsonLines << QJsonArray{QJsonArray{l.p1().x(), l.p1().y()},
- // QJsonArray{l.p2().x(), l.p2().y()}};
- jsonLines << QJsonArray{QJsonArray{p1.x(), p1.y()}, QJsonArray{p2.x(), p2.y()}};
+ for (size_t i = 0; i < img_width; ++i) {
+ rjPixels.PushBack(nan2zero(::pixels.pixels[i]), al);
}
- json["lines"] = jsonLines;
+ rapidjson::Value rjLines{rapidjson::kArrayType};
- const auto result = QJsonDocument(json).toJson();
- // qDebug() << "pixels answer size is" << result.size();
- // static bool done{false};
- // if (!done) {
- // qDebug().noquote() << result;
- // done = true;
- // }
-
- {
- rapidjson::Document jd;
- jd.SetObject();
- auto &al = jd.GetAllocator();
- rapidjson::Value rjPixels{rapidjson::kArrayType};
- for (size_t i = 0; i < img_width; ++i) {
- if (qIsNaN(::pixels.pixels[i])) {
- rjPixels.PushBack(double(0), al);
- } else {
- rjPixels.PushBack(double(::pixels.pixels[i]), al);
- }
- }
- jd.AddMember("measurementCounter", img.counters.measurementCounter, al);
- jd.AddMember("timestampUs", img.counters.timestampUs, al);
- jd.AddMember("pixels", rjPixels.Move(), al);
- rapidjson::StringBuffer buffer;
- rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
- writer.SetMaxDecimalPlaces(2);
- jd.Accept(writer);
- QString res{(const char *) buffer.GetString()};
- // qDebug() << "size:" << res.size();
- // qDebug().noquote() << "ret pix";
+ for (const auto &l : lines) {
+ rapidjson::Value rjLineP1{rapidjson::kArrayType};
+ rjLineP1.PushBack(nan2zero(l.p1().x()), al).PushBack(nan2zero(l.p1().y()), al);
+ rapidjson::Value rjLineP2{rapidjson::kArrayType};
+ rjLineP2.PushBack(nan2zero(l.p2().x()), al).PushBack(nan2zero(l.p2().y()), al);
+ rapidjson::Value rjLinePoints{rapidjson::kArrayType};
+ rjLinePoints.PushBack(rjLineP1, al).PushBack(rjLineP2, al);
- return QHttpServerResponse{res};
+ rjLines.PushBack(rjLinePoints, al);
}
- // {
- // std::srand(static_cast<unsigned>(std::time(nullptr)));
- // rapidjson::Document doc;
- // doc.SetObject();
- // rapidjson::Document::AllocatorType &allocator = doc.GetAllocator();
- // rapidjson::Value valuesArray(rapidjson::kArrayType);
- // // for (int i = 0; i < 10; ++i) {
- // // valuesArray.PushBack(rapidjson::Value(rand() % 100), allocator);
- // // }
- // for (size_t i = 0; i < img_width; ++i) {
- // valuesArray.PushBack(rapidjson::Value(::pixels.pixels[i]), allocator);
- // }
- // doc.AddMember("values", valuesArray, allocator);
- // rapidjson::StringBuffer buffer;
- // rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
- // doc.Accept(writer);
- // std::cout << buffer.GetString() << "\n";
- // }
+ // jd.AddMember("encoderPosition", qint64{encoder.position()});
+ // FIXME: get prom pixels struct
+ jd.AddMember("measurementCounter", img.counters.measurementCounter, al);
+ jd.AddMember("timestampUs", img.counters.timestampUs, al);
+ jd.AddMember("pixels", rjPixels.Move(), al);
+ jd.AddMember("lines", rjLines.Move(), al);
- // qDebug() << "size:" << result.size();
- return QHttpServerResponse(result);
+ rapidjson::StringBuffer buffer;
+ rapidjson::Writer<rapidjson::StringBuffer> writer(buffer);
+ writer.SetMaxDecimalPlaces(2);
+ jd.Accept(writer);
+ QString res{(const char *) buffer.GetString()};
+ // qDebug() << "size:" << res.size();
+ // qDebug().noquote() << "ret pix";
+
+ return QHttpServerResponse{res};
});
qHttpServer.route("/v1/profile", [&]() -> QHttpServerResponse {
@@ -644,41 +585,21 @@ int main(int argc, char* argv[])
return QHttpServerResponse::StatusCode::Ok;
});
- qHttpServer
- .route("/v1/commands/gCode",
- [&](const QHttpServerRequest& request) -> QHttpServerResponse {
- std::cout << "http: gCode" << std::endl;
- if (request.method() != QHttpServerRequest::Method::Post)
- {
- return QHttpServerResponse::StatusCode::NotFound;
- }
+ qHttpServer.route("/v1/commands/gCode",
+ [&](const QHttpServerRequest &request) -> QHttpServerResponse {
+ std::cout << "http: gCode" << std::endl;
+ if (request.method() != QHttpServerRequest::Method::Post) {
+ return QHttpServerResponse::StatusCode::NotFound;
+ }
- const auto command = request.body();
+ const auto command = request.body();
- qDebug() << "send gCode:" << command;
+ qDebug() << "send gCode:" << command;
- // printerClient.sendCommand(command);
+ // printerClient.sendCommand(command);
- return QHttpServerResponse::StatusCode::Ok;
- });
-
- // qHttpServer
- // .route("/v1/commands/startCalibration",
- // [&](const QHttpServerRequest& request) -> QHttpServerResponse {
- // std::cout << "http: startCalibration" << std::endl;
- // if (request.method() != QHttpServerRequest::Method::Post)
- // {
- // return QHttpServerResponse::StatusCode::NotFound;
- // }
-
- // const auto command = request.body();
-
- // qDebug() << "send gCode:" << command;
-
- // // printerClient.sendCommand(command);
-
- // return QHttpServerResponse::StatusCode::Ok;
- // });
+ return QHttpServerResponse::StatusCode::Ok;
+ });
qHttpServer.route(
"/v1/sensor/params",
@@ -690,30 +611,6 @@ int main(int argc, char* argv[])
std::lock_guard<std::mutex> lg(pgm_image_mtx);
QJsonObject json;
- // const libcamera::ControlIdMap& ctrlIdMap =
- // camera->controls().idmap();
-
- // qDebug() << "readParams:" << lastControls.size();
- // qDebug() << request.method();
-
- // for (const auto& [id, value] : lastControls)
- // {
- // const libcamera::ControlId* controlId = ctrlIdMap.at(id);
- // auto name = QString::fromStdString(controlId->name());
- // const auto valueStr =
- // QString::fromStdString(value.toString());
- // qDebug()
- // << "\t param:" << controlId->id() << name << valueStr;
-
- // name[0] = name[0].toLower();
- // json[name] = valueStr;
- // }
-
- // json[laserLevelKey] = requested_params.laserLevel;
-
- // qDebug() << "response body:" << json;
-
- // QHttpServerResponse
return QHttpServerResponse(QJsonDocument(json).toJson());
}
@@ -748,9 +645,6 @@ int main(int argc, char* argv[])
return QHttpServerResponse::StatusCode::
RequestRangeNotSatisfiable;
- // qDebug() << "set gain:" << value;
-
- // requested_params.exposureTime = value;
if (!camera->setGain(value))
return QHttpServerResponse::StatusCode::
RequestRangeNotSatisfiable;
@@ -760,37 +654,11 @@ int main(int argc, char* argv[])
{
const int32_t value{json[laserLevelKey].toInt()};
- // if (value == 0)
- // {
- // return QHttpServerResponse::StatusCode::NotFound;
- // }
-
- // qDebug() << "set new laserLevel:" << value;
if (!camera->setLaserLevel(value))
return QHttpServerResponse::StatusCode::
RequestRangeNotSatisfiable;
requested_params.laserLevel = value;
-
- // const QString laserLevelFile{
- // "/sys/class/pwm/pwmchip2/pwm1/duty_cycle"};
- // QFile f{laserLevelFile};
-
- // if (!f.open(QFile::ReadWrite))
- // {
- // qDebug() << "cannot open laser level file:"
- // << f.errorString();
- // qDebug() << "file path is" << f.fileName();
- // return QHttpServerResponse::StatusCode::InternalServerError;
- // }
-
- // QTextStream s{&f};
-
- // s << value;
-
- // s >> requested_params.laserLevel;
-
- // qDebug() << "done with laser level";
}
return QHttpServerResponse(request.body());
@@ -804,46 +672,13 @@ int main(int argc, char* argv[])
qDebug() << "listen: " << qHttpServer.listen(QHostAddress::Any, 8081);
- // QFuture<void> future = QtConcurrent::run([]() {
- // Port port(8080);
- // Address addr(Ipv4::any(), port);
-
- // HttpService httpService(addr);
-
- // size_t threads_count = 1;
- // httpService.init(threads_count);
- // httpService.start();
- // });
-
////////////////////////////////////////////////////////////////////////////
std::clog << std::flush;
std::cerr << std::flush;
std::cout << "ok for now" << std::endl << std::flush;
- // camera->stop();
- // camera->release();
- // cm->stop();
-
auto result = app.exec();
- // future.cancel();
- // future.waitForFinished();
-
- // for (auto& [fd, mem] : mappedBuffers_)
- // {
- // munmap(mem.first, mem.second);
- // }
-
- // FIXME: crash somewhere here. proper libcamera finishing needed
- // requests.clear();
- // mappedBuffers_.clear();
-
- // camera->stop();
- // config.reset();
- // allocator->free(stream);
- // allocator.reset();
- // camera->release();
- // camera.reset();
#ifndef INNO_MAKER
cm->stop();
#endif