Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 432 lines (401 sloc) 14.957 kB
c729c0f Importing existing code
Diego Dompe authored
1 using V4l2;
2 #if (RRAEW)
3 using rraew;
4 #endif
5 [DBus (name = "com.ridgerun.mt9p031Interface")]
6 public class Ipiped_mt9p031 : AbstcSensor{
7 /**
8 * Create a new instance of a Ipipe daemon
9 */
10 public Ipiped_mt9p031(){
11 /* Initialize private variables */
12 this.debug = false;
13 this.capture_fd = -1;
14 this.owner_capture_fd = 1;
15 }
16
17 /**
18 * Open sensor
19 * Open the video capture device, obtain the correspondly file
20 * descriptor */
21 private int open_sensor(){
22 /* Open the sensor device*/
23 this.capture_fd = Posix.open("/dev/video0", Posix.O_RDWR | Posix.O_NONBLOCK, 0);
24 if (this.capture_fd < 0){
25 Posix.stderr.printf("Failed to open sensor\n");
26 return -1;
27 }
28 this.owner_capture_fd = 0;
29 return 0;
30 }
31
32 /**
33 * Sets sensor gain for each color R, Gr, Gb, B (mt9p031)
34 * Each gain component can range from 1 to 128 in steps of:
35 * 0.125 if gain between 1 and 4
36 * 0.250 if gain between 4.25 and 8
37 * 1.000 if gain betwewn 8 and 128
38 * @param fd structure with file descriptors and owner flags
39 * @param r_gain red gain [1, 128]
40 * @param g_gain green gain [1, 128]
41 * @param b_gain blue gain [1, 128]
42 */
43 private int set_sensor_gain_i(int *capture_fd, char *owner_capture_fd,
44 uint32 q10r_gain, uint32 q10g_gain, uint32 q10b_gain){
45
46 Control ctrl = Control();
47 if (this.capture_fd < 0){
48 if (open_sensor() < 0)
49 return -1;
50 }
51
52 ctrl.id =UserClassControlIds.RED_BALANCE;
53 ctrl.value =(int)(q10r_gain >> 7);
54 if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_S_CTRL, &ctrl)) {
55 if (debug)
56 Posix.stderr.printf("ioctl:VIDIOC_S_CTRL failed:red\n");
57 return -1;
58 }
59
60 ctrl.id =UserClassControlIds.BRIGHTNESS; //V4L2_CID_GREEN1_BALANCE;
61 ctrl.value =(int)(q10g_gain >> 7);
62 if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_S_CTRL, &ctrl)) {
63 if (debug)
64 Posix.stderr.printf("ioctl:VIDIOC_S_CTRL failed:green1\n");
65 return -1;
66 }
67
68 ctrl.id =UserClassControlIds.AUTOGAIN; //V4L2_CID_GREEN2_BALANCE;
69 if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_S_CTRL, &ctrl)) {
70 if (debug)
71 Posix.stderr.printf("ioctl:VIDIOC_S_CTRL failed:green2\n");
72 return -1;
73 }
74
75 ctrl.id =UserClassControlIds.BLUE_BALANCE;
76 ctrl.value =(int)(q10b_gain >> 7);
77 if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_S_CTRL, &ctrl)) {
78 if (debug)
79 Posix.stderr.printf("ioctl:VIDIOC_S_CTRL failed:blue\n");
80 return -1;
81 }
82
83 if (debug)
84 Posix.stdout.printf("Set gain in the sensor r=%f, g=%f ,b=%f\n",
85 (float)(q10r_gain)/1024, (float)(q10g_gain)/1024,
86 (float)(q10b_gain)/1024);
87 return 0;
88 }
89
90 /**
91 * Set sensor gain
92 * Cast the float RGB gains to fixed-point q3 and use the
93 * set_sensor_gain_st function to ajust the sensor gains
94 */
95 public int set_sensor_gain(double r_gain, double g_gain, double b_gain) throws IOError{
96 if (this.capture_fd < 0){
97 if (open_sensor() < 0)
98 return -1;
99 }
100 uint32 q10r_gain = (uint32)((r_gain + 0.125/2) * 1024);
101 uint32 q10g_gain = (uint32)((g_gain + 0.125/2) * 1024);
102 uint32 q10b_gain = (uint32)((b_gain+ 0.125/2) * 1024);
103 return set_sensor_gain_i(&this.capture_fd, &this.owner_capture_fd,
104 q10r_gain, q10g_gain, q10b_gain);
105 }
106
107 /**
108 * Get the combined gain of each color in the sensor registers.
109 * Each gain component can range from 1 to 128 in steps of:
110 * 0.125 if gain between 1 and 4
111 * 0.250 if gain between 4.25 and 8
112 * 1.000 if gain betwewn 8 and 128
113 * @param fd structure with file descriptors and owner flags
114 * @param r_gain pointer to get the red gain
115 * @param g_gain pointer to get the green gain
116 * @param b_gain pointer to get the blue gain
117 */
118 private int get_sensor_gain_i(int *capture_fd, char *owner_capture_fd,
119 uint32 *q10r_gain, uint32 *q10g_gain, uint32 *q10b_gain){
120 Control ctrl = Control();
121 if (this.capture_fd < 0){
122 Posix.stderr.printf("Fail get sensor gain\n");
123 return -1;
124 }
125 /* Get Red Gain */
126 ctrl.id = UserClassControlIds.RED_BALANCE;
127 if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_G_CTRL, &ctrl)){
128 if (debug)
129 Posix.stderr.printf("ioctl:VIDIOC_G_CTRL failed: get red balance\n");
130 return -1;
131 }
132 *q10r_gain = ctrl.value << 7;
133 /* Get Blue Gain */
134 ctrl.id = UserClassControlIds.BLUE_BALANCE;
135 if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_G_CTRL, &ctrl)){
136 if (debug)
137 Posix.stderr.printf("ioctl:VIDIOC_G_CTRL failed: get red balance\n");
138 return -1;
139 }
140 *q10b_gain = ctrl.value << 7;
141 /* Get Green1 Gain */
142 ctrl.id = UserClassControlIds.BRIGHTNESS;
143 if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_G_CTRL, &ctrl)){
144 if (debug)
145 Posix.stderr.printf("ioctl:VIDIOC_G_CTRL failed: get red balance\n");
146 return -1;
147 }
148 *q10g_gain = ctrl.value << 7;
149 return 0;
150 }
151 /**
152 * Get sensor gain
153 * Use the get_sensor_gain_i function to obtain the RGB sensor gains
154 * and print them
155 */
9637d54 @mmontero Fix get functions to return values
mmontero authored
156 public bool get_sensor_gain(out double _red_gain, out double _green_gain,
157 out double _blue_gain) throws IOError{
c729c0f Importing existing code
Diego Dompe authored
158 uint32 q10red_gain=0,q10green_gain=0,q10blue_gain=0;
159 if (this.capture_fd < 0){
160 if (open_sensor() < 0)
9637d54 @mmontero Fix get functions to return values
mmontero authored
161 return false;
c729c0f Importing existing code
Diego Dompe authored
162 }
163
164 if (get_sensor_gain_i(&this.capture_fd, &this.owner_capture_fd,
9637d54 @mmontero Fix get functions to return values
mmontero authored
165 &q10red_gain, &q10green_gain, &q10blue_gain) != 0) {
166 Posix.stderr.printf("Error:\n Failed to get the sensor gain\n");
167 return false;
168 } else {
169 _red_gain = ((double)(q10red_gain))/1024;
170 _green_gain= ((double)(q10green_gain))/1024;
171 _blue_gain = ((double)(q10blue_gain))/1024;
172 return true;
c729c0f Importing existing code
Diego Dompe authored
173 }
174 }
175
176 /**
177 * Flip the image vetically
178 * Change the state of the vertical flip flag to reverse the image's
179 * rows. When mirroring the rows, the bayer pattern is preserved for
180 * the pixel order, that means the bayer pattern readout is reversed as the pixels.
181 * @param state is an on/off flag. If the value is 1 image's columns
182 * must be reversed, if 0 they must not
183 */
184 public int sensor_flip_vertically(string state) throws IOError{
185 Control ctrl = Control();
186 if (this.capture_fd < 0){
187 if (open_sensor() < 0)
188 return -1;
189 }
190 if (state == "ON"){
191 ctrl.value = 1;
192 }else if (state == "OFF"){
193 ctrl.value = 0;
194 }else {
195 if (debug)
196 Posix.stderr.printf("Ipiped: Invalid vertical flip state\n");
197 return -1;
198 }
199 ctrl.id = UserClassControlIds.VFLIP;
200 if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_S_CTRL, &ctrl)){
201 if (debug)
202 Posix.stderr.printf("ioctl:VIDIOC_S_CTRL failed:" +
203 "vertical flip\n");
204 return -1;
205 }
206 return 0;
207 }
208
209 /**
210 * Flip the image horizontally
211 * Change the state of the horizontal flip flag to reverse the image's
212 * columns. When mirroring the columns, the bayer pattern is preserved
213 * for the pixel order, that means the bayer pattern readout is reversed
214 * as the pixels.
215 * @param state is an on/off flag. If the value is 1 image's columns
216 * must be reversed, if 0 they must not
217 */
218 public int sensor_flip_horizontally(string state) throws IOError{
219 Control ctrl = Control();
220
221 if (this.capture_fd < 0){
222 if (open_sensor() < 0)
223 return -1;
224 }
225 if (state == "ON"){
226 ctrl.value = 1;
227 }else if (state == "OFF"){
228 ctrl.value = 0;
229 }else {
230 if (debug)
231 Posix.stderr.printf("Ipiped: Invalid horizontal flip state\n");
232 return -1;
233 }
234 ctrl.id = UserClassControlIds.HFLIP;
235 if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_S_CTRL, &ctrl)){
236 if (debug)
237 Posix.stderr.printf("ioctl:VIDIOC_S_CTRL failed: " +
238 "horizontal flip\n");
239 return -1;
240 }
241 return 0;
242 }
243
244 /**
245 * Sets exposure time
246 * Sets the effective shutter time that is the integration time for
247 * the light in the CMOS sensor receptors
248 * @param fd structure with file descriptors and owner flags
249 * @param exp_time exposure time in us
250 */
251 private int set_exposure_time_i(int *capture_fd, char *owner_capture_fd,
252 uint32 exp_time){
253 Control ctrl = Control();
254 ctrl.id = UserClassControlIds.EXPOSURE;
255 ctrl.value = (int32)(exp_time)<<8;
256 if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_S_CTRL, &ctrl)){
257 if (debug)
258 Posix.stderr.printf("ioctl:VIDIOC_S_CTRL failed: set exposure time\n");
259 return -1;
260 }
261 if (debug)
262 Posix.stdout.printf("Set exposure time to: %u\n", exp_time);
263 return 0;
264 }
265
266 public int set_exposure_time(int _exp_time) throws IOError{
267 if (this.capture_fd < 0){
268 if (open_sensor() < 0)
269 return -1;
270 }
271 return set_exposure_time_i(&this.capture_fd, &this.owner_capture_fd,
272 _exp_time);
273 }
274
275 /**
276 * Get exposure time
277 * Obtain from the sensor the actual ligth integration time
278 * @param fd structure with file descriptors and owner flags
279 * @param exp_time pointer where the function leave the exposure time in us
280 */
281 private int get_exposure_time_i(int *capture_fd, char *owner_capture_fd,
282 uint32* exp){
283 Control ctrl = Control();
284 ctrl.id = UserClassControlIds.EXPOSURE;
285 if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_G_CTRL, &ctrl)){
286 if (debug == true)
287 Posix.stderr.printf("ioctl:VIDIOC_G_CTRL failed: get exposure"+
288 " time\n");
289 return -1;
290 }
291 *exp = ctrl.value>>8;
292 return 0;
293 }
294
295
9637d54 @mmontero Fix get functions to return values
mmontero authored
296 public bool get_exposure_time(out int exp_time) throws IOError{
c729c0f Importing existing code
Diego Dompe authored
297 if (this.capture_fd < 0){
298 if (open_sensor() < 0)
9637d54 @mmontero Fix get functions to return values
mmontero authored
299 return false;
c729c0f Importing existing code
Diego Dompe authored
300 }
9637d54 @mmontero Fix get functions to return values
mmontero authored
301
302 if (get_exposure_time_i(&this.capture_fd, &this.owner_capture_fd, &exp_time) != 0) {
303 Posix.stderr.printf("Error:\n Failed to get the exposure time\n");
304 return false;
305 }
306
307 return true;
c729c0f Importing existing code
Diego Dompe authored
308 }
309
310 #if (RRAEW)
311
312 /*Internal methods*/
313 /**
314 * Get the frame vertical orientation (mt9p031)
315 * @param vf is an on/off flag. If the value is 1 image's rows are
316 * reversed, if 0 they are not
317 */
318 private int get_vertical_flip(int *vf){
319 Control ctrl = Control();
320 if (this.capture_fd < 0){
321 if (open_sensor() < 0)
322 return -1;
323 }
324 /* Get vertical flip of the sensor */
325 ctrl.id = UserClassControlIds.VFLIP;
326 if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_G_CTRL, &ctrl)){
327 if (debug)
328 Posix.stderr.printf("ioctl:VIDIOC_G_CTRL failed: get vertical flip\n");
329 return -1;
330 }
331 *vf = ctrl.value;
332 return 0;
333 }
334
335 /**
336 * Get the frame horizontal orientation (mt9p031)
337 * @param hf is an on/off flag. If the value is 1 image's columns are
338 * reversed, if 0 they are not
339 */
340 private int get_horizontal_flip(int *hf){
341 Control ctrl = Control();
342 if (this.capture_fd < 0){
343 if (open_sensor() < 0)
344 return -1;
345 }
346 /* Get horizontal flip of the sensor */
347 ctrl.id = UserClassControlIds.HFLIP;
348 if (-1 == Posix.ioctl(this.capture_fd , VIDIOC_G_CTRL, &ctrl)){
349 if (debug)
350 Posix.stderr.printf("ioctl:VIDIOC_G_CTRL failed: get vertical flip\n");
351 return -1;
352 }
353 *hf = ctrl.value;
354 return 0;
355 }
356
357 /**
358 * Get the pixel position for each color pattern
359 * @param color_pattern, array that contains the pixel position
360 * of R, Gr, Gb and B
361 */
362 private bool get_color_pattern(ColorPattern color_pattern){
363 int vf=0, hf=0;
364 int ret = 0;
365
366 /* Get the frame orientation */
367 ret = get_vertical_flip(&vf);
368 ret += get_horizontal_flip(&hf);
369 if (ret != 0) {
370 if (debug)
371 Posix.stderr.printf ("Error:\n Failed to get frame orientation\n");
372 return false;
373 }
374 /* Define pixels order */
375 if (vf==1 && hf==1){
376 /* Pattern:
377 * Gb B
378 * R Gr */
379 color_pattern = colorptn_GbBRGr;
380 } else if (vf==0 && hf ==1){
381 /* Pattern:
382 * R Gr
383 * Gb B */
384 color_pattern = colorptn_RGrGbB;
385 } else if (vf==1 && hf ==0){
386 /* Pattern:
387 * B Gb
388 * Gr R*/
389 color_pattern = colorptn_BGbGrR;
390 } else {
391 /* Pattern:
392 * Gr R
393 * B Gb */
394 color_pattern = colorptn_GrRBGb;
395 }
396 return true;
397 }
398
399 public override int get_sensor_data(Sensor *sensor){
400 ColorPattern colorptn = ColorPattern();
401 if (!get_color_pattern (colorptn)){
402 Posix.stderr.printf ("Error:\n Failed to get color pattern\n");
403 return -1;
404 }
405 sensor.colorptn = colorptn;
406 sensor.max_exp_time = 2500;
407 sensor.min_exp_time = 1;
408 sensor.max_gain = 128;
409 sensor.min_gain = 1;
410 sensor.n_gain_steps = 3;
411 sensor.gain_steps = (GainStep*)malloc(sensor.n_gain_steps*sizeof(GainStep));
412 sensor.gain_steps[0].range_end = 4;
413 sensor.gain_steps[0].step_n = 1;
414 sensor.gain_steps[0].step_d = 8;
415
416 sensor.gain_steps[1].range_end = 8;
417 sensor.gain_steps[1].step_n = 1;
418 sensor.gain_steps[1].step_d = 4;
419
420 sensor.gain_steps[2].range_end = 128;
421 sensor.gain_steps[2].step_n = 1;
422 sensor.gain_steps[2].step_d = 1;
423
424 sensor.set_gain = set_sensor_gain_i;
425 sensor.get_gain = get_sensor_gain_i;
426 sensor.set_exposure = set_exposure_time_i;
427 sensor.get_exposure = get_exposure_time_i;
428 return 0;
429 }
430 #endif
431 }
Something went wrong with that request. Please try again.