-
Notifications
You must be signed in to change notification settings - Fork 0
/
Optic.cpp
151 lines (129 loc) · 4.08 KB
/
Optic.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "Optic.hpp"
#include <assert.h>
#include <pthread.h>
#include <sys/time.h>
#include <time.h>
#define OPTIC_PCOLOR KYEL
#define OPTIC_CAM_TYPE 1
Optic::Optic(bool postProc, bool print) :
m_print(print),
m_cameraPtr(NULL),
m_params()
{
struct timeval tv;
gettimeofday(&tv,NULL);
DEBUG_PRINT(OPTIC_PCOLOR "\nOptic constructor at time %f\n" KNRM,
tv.tv_sec + tv.tv_usec / 1000000.0);
int stat = 0;
int cameraID = 0;
bool found = false;
int numCameras = camera::getNumberOfCameras();
assert(numCameras > 0);
for (int i = 0; i < numCameras; i++) {
camera::CameraInfo cameraInfo;
stat = camera::getCameraInfo(i, cameraInfo);
assert(stat == 0);
if (cameraInfo.func == static_cast<int>(OPTIC_CAM_TYPE))
{
cameraID = i;
found = true;
}
}
assert(found);
stat = camera::ICameraDevice::createInstance(cameraID, &m_cameraPtr);
assert(stat == 0);
stat = m_params.init(m_cameraPtr);
assert(stat == 0);
if (postProc) {
printf(OPTIC_PCOLOR "\nUsing 8-bit callback in Optic\n" KNRM);
}
else {
printf(OPTIC_PCOLOR "\nUsing 10-bit callback in Optic\n" KNRM);
m_params.set("preview-format", "bayer-rggb");
// even though we do not use takePicture callback, NEED to set this
m_params.set("picture-format", "bayer-mipi-10gbrg");
m_params.set("raw-size", "640x480");
stat = m_params.commit();
assert(stat == 0);
}
assert(0 == pthread_mutex_init(&m_cameraFrameLock, 0));
}
Optic::~Optic() {
struct timeval tv;
gettimeofday(&tv,NULL);
DEBUG_PRINT(OPTIC_PCOLOR "\nOptic destructor at time %f\n" KNRM,
tv.tv_sec + tv.tv_usec / 1000000.0);
if(m_cameraPtr != NULL) {
camera::ICameraDevice::deleteInstance(&m_cameraPtr);
m_cameraPtr = NULL;
}
assert(0 == pthread_mutex_destroy(&m_cameraFrameLock));
}
// Interface functions
void Optic::onError() {
struct timeval tv;
gettimeofday(&tv,NULL);
DEBUG_PRINT(OPTIC_PCOLOR "\nOptic Error callback at time %f\n" KNRM,
tv.tv_sec + tv.tv_usec / 1000000.0);
}
void Optic::onControl(const camera::ControlEvent& control) {
struct timeval tv;
gettimeofday(&tv,NULL);
DEBUG_PRINT(OPTIC_PCOLOR "\nOptic Control callback type %d at time %f\n" KNRM,
control.type,
tv.tv_sec + tv.tv_usec / 1000000.0);
}
void Optic::onPreviewFrame(camera::ICameraFrame *frame) {
if (m_print) {
static struct timeval tv_last;
static bool first = true;
struct timeval tv;
gettimeofday(&tv,NULL);
/*DEBUG_PRINT(OPTIC_PCOLOR "\nOptic Preview callback at time %f; size %u\n" KNRM,
tv.tv_sec + tv.tv_usec / 1000000.0, frame->size);*/
if (first) {
first = false;
}
else {
double elapsed = tv.tv_sec - tv_last.tv_sec
+ tv.tv_usec / 1000000.0 - tv_last.tv_usec / 1000000.0;
/*DEBUG_PRINT(OPTIC_PCOLOR "\nOptic preview callback elapsed time %f\n" KNRM,
elapsed);*/
if ((elapsed < 0.020) || (elapsed > 0.040)) {
DEBUG_PRINT(OPTIC_PCOLOR "\nOptic preview callback elapsed time %f\n" KNRM,
elapsed);
}
}
tv_last = tv;
}
}
void Optic::onVideoFrame(camera::ICameraFrame *frame) {
struct timeval tv;
gettimeofday(&tv,NULL);
DEBUG_PRINT(OPTIC_PCOLOR "\nOptic Video callback at time %f; size %u\n" KNRM,
tv.tv_sec + tv.tv_usec / 1000000.0, frame->size);
}
void Optic::onPictureFrame(camera::ICameraFrame *frame) {
struct timeval tv;
gettimeofday(&tv,NULL);
DEBUG_PRINT(OPTIC_PCOLOR "\nOptic Picture callback at time %f; size %u\n" KNRM,
tv.tv_sec + tv.tv_usec / 1000000.0, frame->size);
}
void Optic::onMetadataFrame(camera::ICameraFrame *frame) {
struct timeval tv;
gettimeofday(&tv,NULL);
DEBUG_PRINT(OPTIC_PCOLOR "\nOptic Metadata callback at time %f; size %u\n" KNRM,
tv.tv_sec + tv.tv_usec / 1000000.0, frame->size);
}
// Private functions
int Optic::activate() {
assert(m_cameraPtr != NULL);
m_cameraPtr->addListener(this);
return m_cameraPtr->startPreview();
}
void Optic::deactivate() {
assert(m_cameraPtr != NULL);
m_cameraPtr->removeListener(this);
m_cameraPtr->stopPreview();
return;
}