diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/camera/icamera.h | 2 | ||||
| -rw-r--r-- | src/camera/veyeimx287m.cpp | 26 | ||||
| -rw-r--r-- | src/camera/veyeimx287m.h | 5 | ||||
| -rw-r--r-- | src/iscanner.cpp | 4 | ||||
| -rw-r--r-- | src/iscanner.h | 3 | ||||
| -rw-r--r-- | src/main.cpp | 32 | ||||
| -rw-r--r-- | src/protocols/httpserver.cpp | 39 | ||||
| -rw-r--r-- | src/protocols/httpserver.h | 4 | ||||
| -rw-r--r-- | src/protocols/iprotocol.cpp | 2 | ||||
| -rw-r--r-- | src/protocols/iprotocol.h | 6 | ||||
| -rw-r--r-- | src/scanner.cpp | 8 | ||||
| -rw-r--r-- | src/scanner.h | 11 |
12 files changed, 93 insertions, 49 deletions
diff --git a/src/camera/icamera.h b/src/camera/icamera.h index bd08786..44ed4b3 100644 --- a/src/camera/icamera.h +++ b/src/camera/icamera.h @@ -54,6 +54,8 @@ public: virtual bool set_gain(const float value) = 0; virtual std::optional<float> get_gain() = 0; + virtual bool getImage(Image *image) = 0; + public: std::function<void(std::shared_ptr<Pixels>)> newPixelsCallback; std::function<void(Image &)> newImageCallback; diff --git a/src/camera/veyeimx287m.cpp b/src/camera/veyeimx287m.cpp index 7b6302c..21d95c6 100644 --- a/src/camera/veyeimx287m.cpp +++ b/src/camera/veyeimx287m.cpp @@ -294,8 +294,8 @@ bool VeyeIMX287m::init() return false; } - if (!initHttpServer()) - return false; + // if (!initHttpServer()) + // return false; return true; } @@ -600,12 +600,12 @@ bool VeyeIMX287m::initI2C() return m_i2c != nullptr && m_i2c->open(); } -bool VeyeIMX287m::initHttpServer() -{ - m_httpServer = std::make_shared<HttpServer>(this); +// bool VeyeIMX287m::initHttpServer() +// { +// m_httpServer = std::make_shared<HttpServer>(this); - return m_httpServer != nullptr; -} +// return m_httpServer != nullptr; +// } void VeyeIMX287m::calcFrameLoop(std::stop_token stopToken) { @@ -749,8 +749,14 @@ bool VeyeIMX287m::dequeueImageBuffer(size_t &imageIndex) return true; } -bool VeyeIMX287m::getImage(Image &image) +bool VeyeIMX287m::getImage(Image *image) { + if (!image) { + std::cerr << __func__ << ": image is nullptr" << std::endl; + + return false; + } + size_t bufferIdx{}; if (!dequeueImageBuffer(bufferIdx)) { @@ -759,13 +765,13 @@ bool VeyeIMX287m::getImage(Image &image) // TODO: remove this bullshit. return ptr to image or copy image metainfo // only, then copy data - image = std::move(m_images[bufferIdx]); + *image = std::move(m_images[bufferIdx]); { QElapsedTimer t; t.start(); std::lock_guard lock{m_imageMutexes[bufferIdx]}; auto &src = *(Image::radxa_data_t *) m_videoBuffers[bufferIdx]; - auto &dst = image.data; + auto &dst = image->data; Image::copy(dst, src); get_elapsed_ns += t.nsecsElapsed(); } diff --git a/src/camera/veyeimx287m.h b/src/camera/veyeimx287m.h index 9b96afe..1f293d6 100644 --- a/src/camera/veyeimx287m.h +++ b/src/camera/veyeimx287m.h @@ -36,7 +36,8 @@ public: bool startStream() override; bool dequeueImageBuffer(size_t &image); - bool getImage(Image &image); + // bool getImage(Image &image); + bool getImage(Image *image); bool init(); @@ -66,7 +67,7 @@ private: bool initCam(); bool initI2C(); - bool initHttpServer(); + // bool initHttpServer(); void calcFrameLoop(std::stop_token stopToken); diff --git a/src/iscanner.cpp b/src/iscanner.cpp index 87eab9a..c52bb9d 100644 --- a/src/iscanner.cpp +++ b/src/iscanner.cpp @@ -1,5 +1,7 @@ #include "iscanner.h" -IScanner::IScanner(std::shared_ptr<ICamera> camera) +IScanner::IScanner(std::shared_ptr<ICamera> camera, + std::vector<std::shared_ptr<IProtocol>> protocols) : m_camera{camera} + , m_protocols{protocols} {} diff --git a/src/iscanner.h b/src/iscanner.h index e01f7b5..9ce03b7 100644 --- a/src/iscanner.h +++ b/src/iscanner.h @@ -10,7 +10,8 @@ class ICamera; class IScanner { public: - explicit IScanner(std::shared_ptr<ICamera> camera); + explicit IScanner(std::shared_ptr<ICamera> camera, + std::vector<std::shared_ptr<IProtocol>> protocols); virtual ~IScanner() = default; protected: diff --git a/src/main.cpp b/src/main.cpp index 99ad57b..6986751 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -32,6 +32,7 @@ #include "imagealgos.h" #include "profile.h" #include "protocols/httpserver.h" +#include "scanner.h" // #include "rapidjson/document.h" @@ -181,7 +182,7 @@ int main(int argc, char *argv[]) // FIXME: don't use one var for everything int ret; - const auto cameras = VeyeIMX287m::search(); + auto cameras = VeyeIMX287m::search(); if (cameras.empty()) { std::cerr << "No cameras were identified on the system." << std::endl; @@ -201,31 +202,14 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - QHttpServer qHttpServer; - auto httpGetImage = [&]() -> QHttpServerResponse { - auto cam = dynamic_cast<VeyeIMX287m *>(camera.get()); - - if (!cam) { - qDebug() << "NO CAM"; - return QHttpServerResponse::StatusCode::ServiceUnavailable; - } + auto httpServer = std::make_shared<HttpServer>(camera); - static Image img; + const auto scanner + = std::make_shared<Scanner>(camera, + std::vector<std::shared_ptr<IProtocol>>{ + httpServer}); - if (!cam->getImage(img)) { - qDebug() << "cannot get image"; - return QHttpServerResponse::StatusCode::ServiceUnavailable; - } - - pgm_save(&img); - std::lock_guard<std::mutex> lg(pgm_image_mtx); - - 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 qHttpServer; qHttpServer.route("/v1/profile", [&]() -> QHttpServerResponse { // std::cout << "http: profile" << std::endl; diff --git a/src/protocols/httpserver.cpp b/src/protocols/httpserver.cpp index 8819490..164ce54 100644 --- a/src/protocols/httpserver.cpp +++ b/src/protocols/httpserver.cpp @@ -22,8 +22,7 @@ extern uint8_t pgm_image[64 + img_width * img_height * sizeof(uint8_t)]; extern size_t pgm_image_size; extern std::mutex pgm_image_mtx; -HttpServer::HttpServer(ICamera *camera, - // QObject *parent, +HttpServer::HttpServer(std::shared_ptr<ICamera> camera, const QHostAddress &address, const uint16_t port) : IProtocol{camera} @@ -44,10 +43,34 @@ HttpServer::HttpServer(ICamera *camera, [this](const QHttpServerRequest &request) { return POST_params(request); }); + m_server->route(apiPrefix + QStringLiteral("/sensor/image"), + QHttpServerRequest::Method::Get, + [this]() { return GET_image(); }); + m_server->route(apiPrefix + QStringLiteral("/sensor/image2"), + QHttpServerRequest::Method::Get, + [this]() { return GET_image(); }); qDebug().noquote() << Q_FUNC_INFO << ": listen: " << m_server->listen(m_address, m_port); } +QHttpServerResponse HttpServer::GET_image() +{ + static Image img; + + if (!m_camera->getImage(&img)) { + qCritical() << "cannot get image"; + return QHttpServerResponse::StatusCode::ServiceUnavailable; + } + + pgm_save(&img); + std::lock_guard<std::mutex> lg(pgm_image_mtx); + + return QHttpServerResponse{QByteArray{(const char *) (pgm_image), + static_cast<qsizetype>( + pgm_image_size)}, + QHttpServerResponse::StatusCode::Ok}; +} + QHttpServerResponse HttpServer::GET_pixels() { QElapsedTimer t; @@ -69,22 +92,26 @@ QHttpServerResponse HttpServer::GET_pixels() // const auto sharedCam = m_camera.lock(); // FIME: don't cast anything, use interface // auto cam = dynamic_cast<VeyeIMX287m *>(sharedCam.get()); - auto cam = dynamic_cast<VeyeIMX287m *>(m_camera); + // auto cam = dynamic_cast<VeyeIMX287m *>(m_camera); - if (!cam) { + // if (!cam) { + if (!m_camera) { qWarning() << "NO CAM"; return QHttpServerResponse::StatusCode::ServiceUnavailable; } // yeaah // ::img = &img; - if (!cam->getImage(img)) { + // if (!cam->getImage(img)) { + if (!m_camera->getImage(&img)) { qWarning() << "cannot get image"; return QHttpServerResponse::StatusCode::ServiceUnavailable; } // ::pixels = std::move(img.pixels()); - ++cam->processedCounter; + // ++cam->processedCounter; + // FIXME: take into account + // ++m_camera->processedCounter; } // FIXME: not thread-safe, don't use this static shared_ptr at all diff --git a/src/protocols/httpserver.h b/src/protocols/httpserver.h index 64f7987..600ead4 100644 --- a/src/protocols/httpserver.h +++ b/src/protocols/httpserver.h @@ -24,14 +24,14 @@ public: static constexpr uint16_t DefaultPort{8080}; public: - explicit HttpServer(ICamera *camera, - // QObject *parent = nullptr, + explicit HttpServer(std::shared_ptr<ICamera> camera, const QHostAddress &address = DefaultAddress, const uint16_t port = DefaultPort); ~HttpServer() override = default; // TODO: methods starting with GET_/POST_ will be routed automatically public: + QHttpServerResponse GET_image(); QHttpServerResponse GET_pixels(); QHttpServerResponse POST_params(const QHttpServerRequest &request); diff --git a/src/protocols/iprotocol.cpp b/src/protocols/iprotocol.cpp index d021fb7..5422a9d 100644 --- a/src/protocols/iprotocol.cpp +++ b/src/protocols/iprotocol.cpp @@ -1,5 +1,5 @@ #include "iprotocol.h" -IProtocol::IProtocol(ICamera *camera) +IProtocol::IProtocol(std::shared_ptr<ICamera> camera) : m_camera{camera} {} diff --git a/src/protocols/iprotocol.h b/src/protocols/iprotocol.h index 1643036..2d65dac 100644 --- a/src/protocols/iprotocol.h +++ b/src/protocols/iprotocol.h @@ -1,13 +1,15 @@ #pragma once +#include <memory> + class ICamera; class IProtocol { public: - explicit IProtocol(ICamera *camera); + explicit IProtocol(std::shared_ptr<ICamera> camera); virtual ~IProtocol() = default; protected: - ICamera *m_camera{nullptr}; + std::shared_ptr<ICamera> m_camera; }; diff --git a/src/scanner.cpp b/src/scanner.cpp new file mode 100644 index 0000000..1f14ca0 --- /dev/null +++ b/src/scanner.cpp @@ -0,0 +1,8 @@ +#include "scanner.h" + +Scanner::Scanner(std::shared_ptr<ICamera> camera, + std::vector<std::shared_ptr<IProtocol>> protocols) + : IScanner{camera, protocols} +{ + // m_protocols.push_back() +} diff --git a/src/scanner.h b/src/scanner.h new file mode 100644 index 0000000..f1c1f52 --- /dev/null +++ b/src/scanner.h @@ -0,0 +1,11 @@ +#pragma once + +#include "iscanner.h" + +class Scanner : public IScanner +{ +public: + explicit Scanner(std::shared_ptr<ICamera> camera, + std::vector<std::shared_ptr<IProtocol>> protocols); + ~Scanner() override = default; +}; |
