Skip to content

Commit

Permalink
Bugfix/build with newer boost and opencv libs (#20)
Browse files Browse the repository at this point in the history
* Fix build for newer boost lib versions

* Fix build for newer versions of OpenCV

* Use std::string instead of std::__cxx11::string

* Fix boost package dependencies

* Fix path to app executable

* Revert "Fix path to app executable"

This reverts commit 4be17f3 as this breaks the original self-o-mat boxes as described in #20.
  • Loading branch information
maehw authored Nov 27, 2021
1 parent e0fe086 commit 7312ead
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 9 deletions.
9 changes: 8 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ add_custom_command(OUTPUT ${CMAKE_CURRENT_SOURCE_DIR}/src/protobuf/api.pb.cc ${C


find_package(OpenCV REQUIRED)
find_package(Boost REQUIRED COMPONENTS system thread filesystem)
find_package(Boost REQUIRED)
if(Boost_FOUND)
if(${Boost_VERSION} VERSION_GREATER_EQUAL 1.70)
find_package(Boost REQUIRED COMPONENTS system thread filesystem chrono)
else()
find_package(Boost REQUIRED COMPONENTS system thread filesystem)
endif()
endif()
find_package(Cups REQUIRED)
find_package(Protobuf REQUIRED)

Expand Down
14 changes: 14 additions & 0 deletions src/camera/NopCamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,20 @@
#include <opencv2/opencv.hpp>
#include <SFML/System/Clock.hpp>

/* Some OpenCV definitions are outdated/ have been renamed and moved into the `cv` namespace;
* see also: https://stackoverflow.com/questions/57982505/opencv-4-cap-prop-pos-frames-was-not-declared-in-this-scope
* Conditionally defining the macros by the preprocessor if they don't already exist shouldn't
* break the build for older OpenCV versions but fix it for newer ones.
*/
#ifndef CV_CAP_PROP_POS_FRAMES
#define CV_CAP_PROP_POS_FRAMES cv::CAP_PROP_POS_FRAMES
#endif
#ifndef CV_CAP_PROP_FRAME_COUNT
#define CV_CAP_PROP_FRAME_COUNT cv::CAP_PROP_FRAME_COUNT
#endif
#ifndef CV_CAP_PROP_FPS
#define CV_CAP_PROP_FPS cv::CAP_PROP_FPS
#endif

namespace selfomat {
namespace camera {
Expand Down
16 changes: 14 additions & 2 deletions src/camera/OpenCVCamera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@

#include "OpenCVCamera.h"

/* Some OpenCV definitions are outdated/ have been renamed and moved into the `cv` namespace;
* see also: https://stackoverflow.com/questions/57982505/opencv-4-cap-prop-pos-frames-was-not-declared-in-this-scope
* Conditionally defining the macros by the preprocessor if they don't already exist shouldn't
* break the build for older OpenCV versions but fix it for newer ones.
*/
#ifndef CV_CAP_PROP_FRAME_WIDTH
#define CV_CAP_PROP_FRAME_WIDTH cv::CAP_PROP_FRAME_WIDTH
#endif
#ifndef CV_CAP_PROP_FRAME_HEIGHT
#define CV_CAP_PROP_FRAME_HEIGHT cv::CAP_PROP_FRAME_HEIGHT
#endif

using namespace selfomat::camera;

std::string OpenCVCamera::TAG = "OPENCV_CAMERA";
Expand Down Expand Up @@ -133,11 +145,11 @@ bool OpenCVCamera::autofocusBlocking() {


string OpenCVCamera::getCameraName() {
return std::__cxx11::string();
return std::string();
}

string OpenCVCamera::getLensName() {
return std::__cxx11::string();
return std::string();
}

int OpenCVCamera::getExposureCorrection() {
Expand Down
26 changes: 20 additions & 6 deletions src/tools/blocking_reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,18 @@
#include <iostream>
#include <tools/verbose.h>

/* Use the following macro to allow builds for different boost library versions;
* inspired by: https://stackoverflow.com/a/67773642/1627585
*/
#if BOOST_VERSION >= 107000
#define GET_IO_SERVICE(s) ((boost::asio::io_context&)(s).get_executor().context())
#else
#define GET_IO_SERVICE(s) ((s).get_io_service())
#endif

#define BRDR_TAG "BLOCKING_READER"


using namespace selfomat::tools;

class blocking_reader
Expand All @@ -41,10 +53,11 @@ class blocking_reader
// Called when an async read completes or has been cancelled
void read_complete(const boost::system::error_code& error,
size_t bytes_transferred) {
LOG_D(BRDR_TAG, "read complete: bytes_transferred=" + std::to_string(bytes_transferred));
read_error = (error || bytes_transferred == 0);

if(read_error) {
LOG_E("BLOCKING_READER", "error: ", error.message());
LOG_E(BRDR_TAG, "boost error message: ", error.message());
}

// Read has finished, so cancel the
Expand All @@ -54,11 +67,12 @@ class blocking_reader

// Called when the timer's deadline expires.
void time_out(const boost::system::error_code& error) {
// Was the timeout was cancelled?
// Was the timeout cancelled?
if (error) {
// yes
return;
}
LOG_D(BRDR_TAG, "timeout");

// no, we have timed out, so kill
// the read operation
Expand All @@ -73,7 +87,7 @@ class blocking_reader
// a timeout in milliseconds.
blocking_reader(boost::asio::serial_port& port, size_t timeout) :
port(port), timeout(timeout),
timer(port.get_io_service()),
timer( GET_IO_SERVICE(port) ),
read_error(true) {

}
Expand All @@ -86,8 +100,7 @@ class blocking_reader

// After a timeout & cancel it seems we need
// to do a reset for subsequent reads to work.
port.get_io_service().reset();

GET_IO_SERVICE(port).reset();

// Asynchronously read 1 character.
boost::asio::async_read(port, boost::asio::buffer(&c, 1),
Expand All @@ -97,6 +110,7 @@ class blocking_reader
boost::asio::placeholders::bytes_transferred));

// send the request
LOG_D(BRDR_TAG, "Sending request (size: " + std::to_string(request_size) + ")");
port.write_some(boost::asio::buffer(request, request_size));

// Setup a deadline time to implement our timeout.
Expand All @@ -106,7 +120,7 @@ class blocking_reader

// This will block until a character is read
// or until the it is cancelled.
port.get_io_service().run();
GET_IO_SERVICE(port).run();

if (!read_error)
val = c;
Expand Down

0 comments on commit 7312ead

Please sign in to comment.