Skip to content
Newer
Older
100644 461 lines (423 sloc) 17 KB
c729c0f Importing existing code
Diego Dompe authored Apr 19, 2011
1 /**
2 * ipiped/src/ipiped.vala
3 *
4 * Ipipe class - framework for controlling and configuring ipipe using D-Bus messages
5 *
6 * Copyright(c) 2010, RidgeRun
7 * All rights reserved.
8 */
9
10 using Posix,V4l2,ImpPrev,dm365aew;
11 #if (RRAEW)
12 using rraew;
13 #endif
14 [DBus(name = "com.ridgerun.ipiped.IpipeInterface", signals = "Error")]
15 public class Ipipe:GLib.Object {
16
17 /* Private data */
18 /** Flag to enable/disable debug information */
19 private static bool debug = false;
20 /** Flag to indicate if the previewer is initialized*/
21 private static bool initialized = false;
22 private string sensor = "";
23 private string video_processor = "";
24 public AbstcVideoProcessor video_processor_abstract;
25 public AbstcSensor sensor_abstract;
26 public signal void Error(string err_message);
27 #if (RRAEW)
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
28 /** Struct that contains auto white balance configuration*/
29 awbConfiguration awb_config;
30 /** Flag that indicates if auto white balance is configured or not*/
31 bool awb_configured;
32 /** Struct that contains auto exposure configuration*/
33 aeConfiguration ae_config;
34 /** Flag that indicates if auto exposure's configuration status*/
35 bool ae_configured;
36 /** Struct that contains general aew configuration*/
37 aewConfiguration aew_config;
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
38 /** Thread for aew*/
39 unowned Thread<void*> thread;
40 /** Flag to indicate if the aew thread is running*/
41 bool aew_running = false;
42 /** Indicates if we can close the execution thread wihtout failing **/
43 bool clean_close = false;
44 /** Pointer to aew structure*/
45 void *rraew = null;
46 /** Time between aew iterations*/
47 int wait_time=0;
48 #endif
49 /**
50 * Create a new instance of a Ipipe daemon
51 */
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
52 public Ipipe(string _sensor, string _video_processor,
53 AbstcVideoProcessor _video_processor_abstract,
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
54 AbstcSensor _sensor_abstract){
55 /* Initialize private variables */
56 initialized = false;
57 sensor =_sensor;
58 video_processor=_video_processor;
59 video_processor_abstract=_video_processor_abstract;
60 sensor_abstract=_sensor_abstract;
61 this.debug = false;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
62 this.ae_configured = false;
63 this.awb_configured = false;
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
64 }
65 /**
66 * Destroy a instance of Ipipe
67 */
68 ~Ipipe(){
69 /* Destroy the ipipe instance */
70 }
71
72 /**
73 * Debug
74 * Enable/Disable the debug information in all the functions
75 * @param enable if true print debug information
76 */
77 public void enable_debug(bool enable) throws IOError{
78 this.debug = enable;
79 video_processor_abstract.debug = true;
80 sensor_abstract.debug = true;
81 }
82 /**
83 * Ping
84 * Show to the ipipe-client that an instance of the ipiped daemon is
85 * alive
86 */
87 public bool ping() throws IOError{
88 return true;
89 }
90
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
91 /**
92 * get_sensor
93 * Return the sensor that is being used
94 */
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
95 public string get_sensor() throws IOError{
96 return sensor;
97 }
98
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
99 /**
100 * get_video_processor
101 * Return the video processor that is being used*/
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
102 public string get_video_processor() throws IOError{
103 return video_processor;
104 }
105
106 #if (RRAEW)
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
107 /* aew_thread_func
108 * This function consists on a loop that iterates
109 * the execution of the rraew adjustments.
110 * */
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
111 private void* aew_thread_func(){
112 try {
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
113 /* When the flag aew_running is false
114 * stop aew*/
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
115 while (aew_running) {
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
116 /*Wait the configured time between aew iterations*/
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
117 Thread.usleep(wait_time);
118 if (run_rraew(rraew)<0){
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
119 /*When an error occurs close aew*/
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
120 clean_close = false;
121 close_aew();
122 }
123 }
124 }catch(IOError e) {
125 Posix.stderr.printf("Fail to execute:%s\n", e.message);
126 }
127 return null;
128 }
129
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
130 /**
131 * set_auto_exposure_configuration
132 * Initialize the ae struct for the auto exposure configuration of
133 * librraew.
134 * @param ae string that chooses the auto exposure algorithm, can be
135 * "electronic-centric" or "none" (not apply an auto exposure).
136 * @param meter string that chooses the metering system for the auto
137 * exposure algorithm, can be "partial", "center-weigthed", "segmented"
138 * or "average"
139 * @param rect_percentage defines the percentage of the image width
140 * and height to be used as the center size. Default: 40
141 * @param xrect x coordinate for the center point of the rectangle
142 * of interest.
143 * @param yrect y coordinate for the center point of the rectangle
144 * of interest.
145 */
146 public int set_auto_exposure_configuration(string ae, string meter,
147 int rect_percentage, int xrect, int yrect) throws IOError{
148
149 this.ae_config = aeConfiguration();
150
151 /* Define the ae algorithm*/
152 if (ae == "electronic-centric")
153 ae_config.algorithm = ExposureAlgo.EC;
154 else if(ae == "none"){
155 ae_config.algorithm = ExposureAlgo.NONE;
156 this.ae_configured = true;
157 return 0;
158 } else {
159 Posix.stderr.printf("\nIpiped:Invalid auto exposure algorithm\n");
160 return -1;
161 }
162
163 /* Define the metering method*/
164 if (meter == "partial")
165 ae_config.meter_type = MeteringType.PARTIAL_AREA;
166 else if (meter == "weighted")
167 ae_config.meter_type = MeteringType.RECT_WEIGHTED;
168 else if (meter == "average")
169 ae_config.meter_type = MeteringType.AVERAGE;
170 else if (meter == "segmented")
171 ae_config.meter_type = MeteringType.SEGMENT;
172 else{
173 Posix.stderr.printf("\nIpiped:Invalid metering type\n");
174 return -1;
175 }
176 /* Only the metering systems partial, rectangular weighted and
177 * average require the definition of the rectangle of interest*/
178 if ((ae_config.meter_type == MeteringType.PARTIAL_AREA) ||
179 (ae_config.meter_type == MeteringType.RECT_WEIGHTED) ||
180 (ae_config.meter_type == MeteringType.AVERAGE)){
181
182 /*Sets the center point*/
183 ae_config.rect_center_point.x = xrect;
184 ae_config.rect_center_point.y = yrect;
185
186 /* The point (-1, -1) es used as identifier for the
187 * center of the image. So the auto exposure is
188 * configured to center the rectangle*/
189 if ((xrect == -1) && (yrect == -1)){
190 ae_config.rect_center_point.centered = 1;
191 ae_config.rect_center_point.x = 0;
192 ae_config.rect_center_point.y = 0;
193 }
194 /*Check percentage limits*/
195 if ((rect_percentage > 100)|| (rect_percentage) < 1) {
196 Posix.stderr.printf("\nIpiped: rect_pecentage must be" +
197 " between 1 and 100\n");
198 return -1;
199 }
200 ae_config.rect_percentage = rect_percentage;
201 }
202 this.ae_configured = true;
203 return 0;
204 }
205
206 /**
207 * set_auto_white_balance_configuration
208 * Initialize the awb struct for the auto white balance configuration of
209 * librraew.
210 * @param wb string that chooses the auto white balance algorithm,
211 * can be "gray-world", "white-patch", "white-patch2" or "none" (not
212 * apply an auto white balance).
213 * @param g string that chooses the gain type, can be "sensor" for sensor
214 * gain or "digital" for digital gain on the SOC.
215 */
216 public int set_auto_white_balance_configuration(string wb, string g)
217 throws IOError{
218
219 this.awb_config = awbConfiguration();
220
221 /* Define the awb algorithm*/
222 if (wb == "white-patch")
223 awb_config.algorithm = WhiteBalanceAlgo.WHITE_PATCH;
224 else if (wb == "white-patch2")
225 awb_config.algorithm = WhiteBalanceAlgo.WHITE_PATCH_2;
226 else if (wb == "gray-world")
227 awb_config.algorithm = WhiteBalanceAlgo.GRAY_WORLD;
228 else if (wb == "none") {
229 awb_config.algorithm = WhiteBalanceAlgo.NONE;
230 this.awb_configured = true;
231 return 0;
232 } else{
233 Posix.stderr.printf("\nIpiped:Invalid white-balance algorithm\n");
234 return -1;
235 }
236
237 /* Define the gain module to be used*/
238 if (g == "sensor")
239 awb_config.gain_type = GainType.SENSOR;
240 else if (g == "digital")
241 awb_config.gain_type = GainType.DIGITAL;
242 else if (g == "default"){
243 /*Digital is the default gain*/
244 awb_config.gain_type = GainType.DIGITAL;
245 } else {
246 Posix.stderr.printf("\nIpiped:Invalid gain type\n");
247 return -1;
248 }
249 this.awb_configured = true;
250 return 0;
251 }
252
253 /**
254 * get_auto_exposure_configuration
255 * Return current auto exposure's configuration
256 */
257 public int get_auto_exposure_configuration(out string ae, out string meter,
258 out int rect_percentage, out int xrect, out int yrect) throws IOError{
259
260 if (ae_config.algorithm == ExposureAlgo.EC)
261 ae = "electronic-centric";
262 else
263 ae = "none";
264
265 if ( ae_config.meter_type == MeteringType.PARTIAL_AREA )
266 meter = "partial";
267 else if (ae_config.meter_type == MeteringType.RECT_WEIGHTED)
268 meter = "weighted";
269 else if (ae_config.meter_type == MeteringType.AVERAGE)
270 meter = "average";
271 else if (ae_config.meter_type == MeteringType.SEGMENT)
272 meter = "segmented";
273
274 rect_percentage = ae_config.rect_percentage;
275
276 if (ae_config.rect_center_point.centered == 1){
277 xrect = -1;
278 yrect = -1;
279 } else {
280 xrect = (int)ae_config.rect_center_point.x;
281 yrect = (int)ae_config.rect_center_point.y;
282 }
283
284 if (!ae_configured)
285 return -1;
286 else
287 return 0;
288 }
289 /**
290 * get_auto_white_balance_configuration
291 * Return the current configuration of the awb
292 */
293 public int get_auto_white_balance_configuration(out string wb,
294 out string g) throws IOError{
295 if (!awb_configured){
296 wb = "";
297 g = "";
298 return -1;
299 }
300 if (awb_config.algorithm == WhiteBalanceAlgo.WHITE_PATCH)
301 wb = "white-patch";
302 else if (awb_config.algorithm == WhiteBalanceAlgo.WHITE_PATCH_2)
303 wb = "white-patch2";
304 else if (awb_config.algorithm == WhiteBalanceAlgo.GRAY_WORLD)
305 wb = "gray-world";
306 else if (awb_config.algorithm == WhiteBalanceAlgo.NONE )
307 wb = "none";
308
309 if (awb_config.gain_type == GainType.SENSOR)
310 g = "sensor";
311 else if (awb_config.gain_type == GainType.DIGITAL)
312 g = "digital";
313
314 return 0;
315 }
316
317 /**
318 * get_aew_status
319 * Return the current configuration of the rraew library
320 */
321 public bool get_aew_status(out bool awb_config, out bool ae_config,
322 out int time, out int width, out int height,
323 out int segment_factor) throws IOError{
324
325 awb_config = awb_configured;
326 ae_config = ae_configured;
327 time = wait_time;
328 width = aew_config.width;
329 height = aew_config.height;
330 segment_factor = aew_config.segmentation_factor;
331 return aew_running;
332 }
333
334 /**
335 * get_ae_rectangle_coordinates
336 * Get the rectangle of interest's coordinates, only if aew is running
337 * and return it.
338 */
339 public int get_ae_rectangle_coordinates(out uint right, out uint left,
340 out uint top, out uint bottom) throws IOError{
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
341
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
342 if (aew_running) {
343 if (get_rectangle_coordinates(rraew, &right, &left,
344 &top, &bottom) == 0){
345 return 0;
346 }
347 }
348 right = 0;
349 left = 0;
350 top = 0;
351 bottom = 0;
352 return -1;
353 }
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
354 /**
355 * AEW Algorithm initialization
356 * Uses the parameters given by the user to configure the aew library,
357 * sets the functions to access the sensor and the dm365 interface
358 * (vpfe ipipe). Creates an aew structure and a thread for the aew loop
359 * @param time it is the time between aew algorithms iterations
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
360 * @param width image width
361 * @param height image height
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
362 * @param segment_factor a percentage of the maximun image divisions
363 */
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
364 public int init_aew(int time, int width, int height,
365 int segment_factor) throws IOError
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
366 {
367 /** File descriptors information*/
368 FileDescriptors fd = FileDescriptors();
369 wait_time = time;
370 Sensor sensor = Sensor();
371 Interface interf = Interface();
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
372 aew_config = aewConfiguration();
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
373
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
374 /* If an instance of librraew is running close it
375 * before create a new one */
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
376 if(aew_running) {
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
377 close_aew();
378 /*Wait for aew engine disable*/
379 usleep(300000);
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
380 }
381
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
382 /* Defining general rraew params */
383 aew_config.height = height;
384 aew_config.width = width;
385 aew_config.segmentation_factor = segment_factor;
386
387 /* Check params limits */
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
388 if (time > 1000000) {
389 Posix.stderr.printf("\nIpiped:Wait time is greater than the maximum\n");
390 return -1;
391 }
392
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
393 if ((segment_factor > 100)|| (segment_factor) < 1) {
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
394 Posix.stderr.printf("\nIpiped:Segmentation factor must be between 1 and 100\n");
395 return -1;
396 }
397
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
398 /* Get sensor information */
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
399 sensor_abstract.get_sensor_data(&sensor);
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
400 /* Get video processor information */
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
401 if (!video_processor_abstract.get_video_processor_data(&interf)) {
402 Posix.stderr.printf ("Can't get video processor data\n");
403 return -1;
404 }
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
405 /* Defining ipiped file descriptors */
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
406 fd.previewer_fd = video_processor_abstract.previewer_fd;
407 fd.owner_previewer_fd = video_processor_abstract.owner_previewer_fd;
408 fd.aew_fd = video_processor_abstract.aew_fd;
409 fd.owner_aew_fd = video_processor_abstract.owner_aew_fd;
410 fd.capture_fd = sensor_abstract.capture_fd;
411 fd.owner_capture_fd = sensor_abstract.owner_capture_fd;
412
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
413 /* Create rraew handler */
414 rraew = create_rraew(&awb_config, &ae_config, &aew_config,
415 &sensor, &interf, &fd);
416 if (rraew == null){
417 Posix.stderr.printf ("Can't create rraew handler\n");
418 Posix.stdout.printf ("Can't create rraew handler\n");
419 return -1;
420 }
421 /* Enable aew flag */
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
422 aew_running = true;
423 clean_close = true;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
424
425 /* Create a new thread for the aew loop*/
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
426 if (!Thread.supported ()) {
427 error ("Cannot run without thread support");
428 }
429 try {
430 thread = Thread.create<void*>(aew_thread_func, true);
431 } catch (ThreadError e) {
432 return -1;
433 }
434 return 0;
435 }
436
437 /**
438 * End AEW Algorithm, disable AEW engine
439 */
440 public void close_aew() throws IOError{
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
441 /*Clean the flag that indicates aew
442 * is running*/
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
443 aew_running = false;
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
444 /*Check if the aew wasn't close by
445 *librraew error*/
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
446 if(clean_close) {
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
447 /*join the thread*/
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
448 thread.join();
449 clean_close = false;
450 }
531f9f8 @mmontero -Change ipiped version to 1.1
mmontero authored Jul 28, 2011
451 /*If the rraew handler was initialized
452 *destroy it*/
c729c0f Importing existing code
Diego Dompe authored Apr 20, 2011
453 if (rraew != null){
454 destroy_rraew(rraew);
455 rraew = null;
456 }
457 }
458 #endif
459 }
460
Something went wrong with that request. Please try again.