-
Notifications
You must be signed in to change notification settings - Fork 0
/
ps3eye.h
263 lines (220 loc) · 7.26 KB
/
ps3eye.h
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// source code from https://github.com/inspirit/PS3EYEDriver
#ifndef PS3EYECAM_H
#define PS3EYECAM_H
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <vector>
#include <memory>
// Get rid of annoying zero length structure warnings from libusb.h in MSVC
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4200)
#endif
#include "libusb.h"
#ifdef _MSC_VER
#pragma warning(pop)
#endif
#ifndef __STDC_CONSTANT_MACROS
# define __STDC_CONSTANT_MACROS
#endif
#include <stdint.h>
#if defined(DEBUG)
#define debug(...) fprintf(stdout, __VA_ARGS__)
#else
#define debug(...)
#endif
namespace ps3eye {
class PS3EYECam
{
public:
enum class EOutputFormat
{
Bayer, // Output in Bayer. Destination buffer must be width * height bytes
BGR, // Output in BGR. Destination buffer must be width * height * 3 bytes
RGB , // Output in RGB. Destination buffer must be width * height * 3 bytes
Gray // Output in Grayscale. Destination buffer must be width * height bytes
};
typedef std::shared_ptr<PS3EYECam> PS3EYERef;
static const uint16_t VENDOR_ID;
static const uint16_t PRODUCT_ID;
PS3EYECam(libusb_device *device);
~PS3EYECam();
bool init(uint32_t width = 0, uint32_t height = 0, uint16_t desiredFrameRate = 30, EOutputFormat outputFormat = EOutputFormat::BGR);
void start();
void stop();
// Controls
bool getAutogain() const { return autogain; }
void setAutogain(bool val) {
autogain = val;
if (val) {
sccb_reg_write(0x13, 0xf7); //AGC,AEC,AWB ON
sccb_reg_write(0x64, sccb_reg_read(0x64)|0x03);
} else {
sccb_reg_write(0x13, 0xf0); //AGC,AEC,AWB OFF
sccb_reg_write(0x64, sccb_reg_read(0x64)&0xFC);
setGain(gain);
setExposure(exposure);
}
}
bool getAutoWhiteBalance() const { return awb; }
void setAutoWhiteBalance(bool val) {
awb = val;
if (val) {
sccb_reg_write(0x63, 0xe0); //AWB ON
}else{
sccb_reg_write(0x63, 0xAA); //AWB OFF
}
}
uint8_t getGain() const { return gain; }
void setGain(uint8_t val) {
gain = val;
switch(val & 0x30){
case 0x00:
val &=0x0F;
break;
case 0x10:
val &=0x0F;
val |=0x30;
break;
case 0x20:
val &=0x0F;
val |=0x70;
break;
case 0x30:
val &=0x0F;
val |=0xF0;
break;
}
sccb_reg_write(0x00, val);
}
uint8_t getExposure() const { return exposure; }
void setExposure(uint8_t val) {
exposure = val;
sccb_reg_write(0x08, val>>7);
sccb_reg_write(0x10, val<<1);
}
uint8_t getSharpness() const { return sharpness; }
void setSharpness(uint8_t val) {
sharpness = val;
sccb_reg_write(0x91, val); //vga noise
sccb_reg_write(0x8E, val); //qvga noise
}
uint8_t getContrast() const { return contrast; }
void setContrast(uint8_t val) {
contrast = val;
sccb_reg_write(0x9C, val);
}
uint8_t getBrightness() const { return brightness; }
void setBrightness(uint8_t val) {
brightness = val;
sccb_reg_write(0x9B, val);
}
uint8_t getHue() const { return hue; }
void setHue(uint8_t val) {
hue = val;
sccb_reg_write(0x01, val);
}
uint8_t getRedBalance() const { return redblc; }
void setRedBalance(uint8_t val) {
redblc = val;
sccb_reg_write(0x43, val);
}
uint8_t getBlueBalance() const { return blueblc; }
void setBlueBalance(uint8_t val) {
blueblc = val;
sccb_reg_write(0x42, val);
}
uint8_t getGreenBalance() const { return greenblc; }
void setGreenBalance(uint8_t val) {
greenblc = val;
sccb_reg_write(0x44, val);
}
bool getFlipH() const { return flip_h; }
bool getFlipV() const { return flip_v; }
void setFlip(bool horizontal = false, bool vertical = false) {
flip_h = horizontal;
flip_v = vertical;
uint8_t val = sccb_reg_read(0x0c);
val &= ~0xc0;
if (!horizontal) val |= 0x40;
if (!vertical) val |= 0x80;
sccb_reg_write(0x0c, val);
}
bool getTestPattern() const { return testPattern; }
void setTestPattern(bool enable)
{
testPattern = enable;
uint8_t val = sccb_reg_read(0x0C);
val &= ~0b00000001;
if (testPattern) val |= 0b00000001; // 0x80;
sccb_reg_write(0x0C, val);
}
bool isStreaming() const { return is_streaming; }
bool isInitialized() const { return device_ != NULL && handle_ != NULL && usb_buf != NULL; }
bool getUSBPortPath(char *out_identifier, size_t max_identifier_length) const;
// Get a frame from the camera. Notes:
// - If there is no frame available, this function will block until one is
// - The output buffer must be sized correctly, depending out the output format. See EOutputFormat.
void getFrame(uint8_t* frame);
uint32_t getWidth() const { return frame_width; }
uint32_t getHeight() const { return frame_height; }
uint16_t getFrameRate() const { return frame_rate; }
bool setFrameRate(uint8_t val) {
if (is_streaming) return false;
frame_rate = ov534_set_frame_rate(val, true);
return true;
}
uint32_t getRowBytes() const { return frame_width * getOutputBytesPerPixel(); }
uint32_t getOutputBytesPerPixel() const;
//
static const std::vector<PS3EYERef>& getDevices( bool forceRefresh = false );
libusb_device *device_;
void ov534_set_led(int status);
private:
PS3EYECam(const PS3EYECam&);
void operator=(const PS3EYECam&);
void release();
// usb ops
uint16_t ov534_set_frame_rate(uint16_t frame_rate, bool dry_run = false);
void ov534_reg_write(uint16_t reg, uint8_t val);
uint8_t ov534_reg_read(uint16_t reg);
int sccb_check_status();
void sccb_reg_write(uint8_t reg, uint8_t val);
uint8_t sccb_reg_read(uint16_t reg);
void reg_w_array(const uint8_t (*data)[2], int len);
void sccb_w_array(const uint8_t (*data)[2], int len);
// controls
bool autogain;
uint8_t gain; // 0 <-> 63
uint8_t exposure; // 0 <-> 255
uint8_t sharpness; // 0 <-> 63
uint8_t hue; // 0 <-> 255
bool awb;
uint8_t brightness; // 0 <-> 255
uint8_t contrast; // 0 <-> 255
uint8_t blueblc; // 0 <-> 255
uint8_t redblc; // 0 <-> 255
uint8_t greenblc; // 0 <-> 255
bool flip_h;
bool flip_v;
bool testPattern;
//
bool is_streaming;
std::shared_ptr<class USBMgr> mgrPtr;
static bool devicesEnumerated;
static std::vector<PS3EYERef> devices;
uint32_t frame_width;
uint32_t frame_height;
uint16_t frame_rate;
EOutputFormat frame_output_format;
//usb stuff
// libusb_device *device_;
libusb_device_handle *handle_;
uint8_t *usb_buf;
std::shared_ptr<class URBDesc> urb;
bool open_usb();
void close_usb();
};
} // namespace
#endif