Skip to content

Commit 74017ef

Browse files
authored
Merge pull request #144 from EmixamPP/py_wrap_cpp
Commands written in C++ and Python wrapping C++
2 parents eedb487 + 32bd8fb commit 74017ef

38 files changed

Lines changed: 592 additions & 656 deletions

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
# Thu Oct 19 2023 - 5.2.0
2+
- New test command
3+
- Improve efficiency
4+
- Significant reduction in installation size
15
# Mon Oct 9 2023 - 5.0.4
26
- New advice and hint messages
37
- Update OpenCV to v4.8.1

boot_service/base_boot_service.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import subprocess
44
from abc import ABCMeta, abstractmethod
55

6-
from globals import UDEV_RULE_PATH, get_index, get_kernels
6+
from utils import UDEV_RULE_PATH, get_index, get_kernels
77

88

99
class BaseBootService(metaclass=ABCMeta):

boot_service/openrc/openrc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import subprocess
33

44
from boot_service import BaseBootService
5-
from globals import BOOT_SERVICE_NAME
5+
from utils import BOOT_SERVICE_NAME
66

77

88
class Openrc(BaseBootService):

boot_service/systemd/systemd.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import subprocess
33

44
from boot_service import BaseBootService
5-
from globals import BOOT_SERVICE_NAME
5+
from utils import BOOT_SERVICE_NAME
66

77

88
class Systemd(BaseBootService):

camera/autocamera.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
using namespace std;
1010

1111
#include "opencv.hpp"
12+
#include "globals.hpp"
1213

1314
/**
1415
* @brief Obtain the intensity variation sum of camera captures
@@ -70,7 +71,6 @@ long long unsigned AutoCamera::intensityVariationSum()
7071
return sum;
7172
}
7273

73-
7474
/**
7575
* @brief Check if the emitter is working,
7676
* without asking for manual confirmation
@@ -94,3 +94,28 @@ bool AutoCamera::isEmitterWorking()
9494
}
9595

9696
AutoCamera::AutoCamera(const string &device, unsigned captureTimeMs) : Camera(device), captureTimeMs(captureTimeMs), refIntesityVarSum(intensityVariationSum()) {}
97+
98+
/**
99+
* @brief Find a grayscale camera.
100+
*
101+
* @return path to the graycale device,
102+
* nullptr if unable to find such device
103+
*/
104+
shared_ptr<AutoCamera> AutoCamera::findGrayscaleCamera()
105+
{
106+
auto v4lDevices = get_v4l_devices();
107+
for (auto &device : *v4lDevices)
108+
{
109+
try
110+
{
111+
auto camera = make_shared<AutoCamera>(device);
112+
if (camera->isGrayscale())
113+
return camera;
114+
}
115+
catch (CameraException &e)
116+
{ // ignore them
117+
}
118+
}
119+
120+
return nullptr;
121+
}

camera/autocamera.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,9 @@ class AutoCamera : public Camera
3333

3434
AutoCamera &operator=(AutoCamera &&other) = delete;
3535

36-
AutoCamera(AutoCamera && other) = delete;
36+
AutoCamera(AutoCamera &&other) = delete;
37+
38+
static shared_ptr<AutoCamera> findGrayscaleCamera();
3739
};
3840

3941
#endif

camera/camera.cpp

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,63 @@
1414
using namespace std;
1515

1616
#include "opencv.hpp"
17-
1817
#include "camerainstruction.hpp"
18+
#include "globals.hpp"
1919
#include "../utils/logger.hpp"
2020

2121
constexpr int OK_KEY = 121;
2222
constexpr int NOK_KEY = 110;
2323

24+
/**
25+
* @brief Determine if the camera is in grayscale.
26+
*
27+
* @throw CameraException if unable to open the camera device
28+
*
29+
* @return true if so, otheriwse false.
30+
*/
31+
bool Camera::isGrayscale()
32+
{
33+
unique_ptr<cv::Mat> frame = read1();
34+
35+
if (frame->channels() != 3)
36+
return false;
37+
38+
for (int r = 0; r < frame->rows; ++r)
39+
for (int c = 0; c < frame->cols; ++c)
40+
{
41+
const cv::Vec3b &pixel = frame->at<cv::Vec3b>(r, c);
42+
if (pixel[0] != pixel[1] || pixel[0] != pixel[2])
43+
return false;
44+
}
45+
46+
return true;
47+
}
48+
49+
/**
50+
* @brief Find a grayscale camera.
51+
*
52+
* @return path to the graycale device,
53+
* nullptr if unable to find such device
54+
*/
55+
shared_ptr<Camera> Camera::findGrayscaleCamera()
56+
{
57+
auto v4lDevices = get_v4l_devices();
58+
for (auto &device : *v4lDevices)
59+
{
60+
auto camera = make_shared<Camera>(device);
61+
try
62+
{
63+
if (camera->isGrayscale())
64+
return camera;
65+
}
66+
catch (CameraException &e)
67+
{ // ignore
68+
}
69+
}
70+
71+
return nullptr;
72+
}
73+
2474
/**
2575
* @brief Get the file descriptor previously opened
2676
*
@@ -46,16 +96,14 @@ shared_ptr<cv::VideoCapture> Camera::getCap() const noexcept
4696
*
4797
* @param device path to the camera
4898
*
49-
* @throw runtime_error if unable to obtain the /dev/videoX path
50-
*
5199
* @return the device id
52100
*/
53101
int Camera::deviceId(const string &device)
54102
{
55103
std::unique_ptr<char[], decltype(&free)> devDevice(realpath(device.c_str(), nullptr), &free);
56-
int id;
104+
int id = 0;
57105
if (devDevice == nullptr || sscanf(devDevice.get(), "/dev/video%d", &id) != 1)
58-
throw runtime_error("CRITICAL: Unable to obtain the /dev/videoX path");
106+
Logger::critical(ExitCode::FAILURE, "Unable to obtain the /dev/videoX path");
59107
return id;
60108
}
61109

@@ -340,7 +388,7 @@ uint16_t Camera::lenUvcQuery(uint8_t unit, uint8_t selector)
340388
return len;
341389
}
342390

343-
CameraException::CameraException(const string &device) : message("CRITICAL: Cannot access to " + device) {}
391+
CameraException::CameraException(const string &device) : message("Cannot access to " + device) {}
344392

345393
const char *CameraException::what() const noexcept
346394
{

camera/camera.hpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,10 @@ class Camera
6666
int getUvcQuery(uint8_t query_type, uint8_t unit, uint8_t selector, vector<uint8_t> &control);
6767

6868
uint16_t lenUvcQuery(uint8_t unit, uint8_t selector);
69+
70+
bool isGrayscale();
71+
72+
static shared_ptr<Camera> findGrayscaleCamera();
6973
};
7074

7175
class CameraException : public exception

camera/camerainstruction.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ CameraInstruction::CameraInstruction(Camera &camera, uint8_t unit, uint8_t selec
6767
minCtrl.assign(curCtrl.begin(), curCtrl.end());
6868
}
6969

70-
Logger::debug(("unit: " + to_string(unit) + " selector: " + to_string(selector)));
70+
Logger::debug("unit: ", to_string(unit), " selector: ", to_string(selector));
7171
logDebugCtrl("current:", curCtrl);
7272
logDebugCtrl("minimum:", minCtrl);
7373
logDebugCtrl("maximum:", maxCtrl);
@@ -91,10 +91,10 @@ CameraInstruction::CameraInstruction(uint8_t unit, uint8_t selector, const vecto
9191
* false if the maximum control has already been set
9292
*/
9393
bool CameraInstruction::next() noexcept
94-
{
94+
{
9595
if (curCtrl == maxCtrl)
9696
return false;
97-
97+
9898
for (unsigned i = 0; i < curCtrl.size(); ++i)
9999
{
100100
uint16_t nextCtrli = static_cast<uint16_t>(curCtrl[i] + 1);
@@ -180,7 +180,7 @@ bool CameraInstruction::setMaxAsCur() noexcept
180180
}
181181

182182
CameraInstructionException::CameraInstructionException(const string &device, uint8_t unit, uint8_t selector)
183-
: message("ERROR: Impossible to obtain the instruction on " + device + " for unit: " + to_string(unit) + " selector:" + to_string(selector)) {}
183+
: message("Impossible to obtain the instruction on " + device + " for unit: " + to_string(unit) + " selector:" + to_string(selector)) {}
184184

185185
const char *CameraInstructionException::what() const noexcept
186186
{

camera/is-emitter-working.cpp

Lines changed: 0 additions & 31 deletions
This file was deleted.

0 commit comments

Comments
 (0)