core/vidl/vidl_v4l2_control.cxx
Go to the documentation of this file.
00001 //:
00002 // \file
00003 //
00004 // \author Antonio Garrido
00005 // \verbatim
00006 //  Modifications
00007 //     10 Nov 2008 Created (A. Garrido)
00008 //\endverbatim
00009 
00010 
00011 #include "vidl_v4l2_control.h"
00012 #include <vcl_cstdio.h> // for std::snprintf()
00013 #include <vcl_cstring.h>
00014 #include <vcl_iostream.h>
00015 
00016 extern "C" {
00017 #include <sys/ioctl.h>
00018 };
00019 
00020 vidl_v4l2_control * vidl_v4l2_control::new_control(const v4l2_queryctrl& ctr, int f)
00021 {
00022   if ( (ctr.flags & V4L2_CTRL_FLAG_DISABLED)
00023 #ifdef V4L2_CTRL_FLAG_INACTIVE
00024        || (ctr.flags & V4L2_CTRL_FLAG_INACTIVE)
00025 #endif
00026      )
00027     return 0;
00028   switch (ctr.type) {
00029     case V4L2_CTRL_TYPE_INTEGER:
00030       return new vidl_v4l2_control_integer(ctr,f);
00031       break;
00032     case V4L2_CTRL_TYPE_BOOLEAN:
00033       return new vidl_v4l2_control_boolean(ctr,f);
00034       break;
00035     case V4L2_CTRL_TYPE_MENU:
00036       {
00037       vidl_v4l2_control_menu *p= new vidl_v4l2_control_menu(ctr,f);
00038       if (p->n_items()==0) {
00039         delete p;
00040         p= 0;
00041       }
00042       return p;
00043       }
00044       break;
00045     case V4L2_CTRL_TYPE_BUTTON:
00046       return new vidl_v4l2_control_button(ctr,f);
00047       break;
00048     default:
00049       break;
00050   }
00051 
00052   return 0;
00053 }
00054 
00055 void vidl_v4l2_control::set_value(int v) const
00056 {
00057   struct v4l2_control control;
00058   vcl_memset (&control, 0, sizeof (control));
00059   control.id = ctrl_.id;
00060   control.value = v;
00061   ioctl (fd, VIDIOC_S_CTRL, &control); // error ignored
00062 }
00063 
00064 int vidl_v4l2_control::get_value() const
00065 {
00066   struct v4l2_control control;
00067   vcl_memset (&control, 0, sizeof (control));
00068   control.id = ctrl_.id;
00069   ioctl (fd, VIDIOC_G_CTRL, &control); // error ignored
00070   return control.value;
00071 }
00072 
00073 // ----------------- Control integer ---------------
00074 
00075 void vidl_v4l2_control_integer::set(int value) const
00076 {
00077   if (value<ctrl_.minimum) value= ctrl_.minimum;
00078   else if (value>ctrl_.maximum) value= ctrl_.maximum;
00079        else value= ctrl_.minimum+(value-ctrl_.minimum)/ctrl_.step * ctrl_.step;
00080   set_value(value);
00081 }
00082 
00083 void vidl_v4l2_control_integer::set_100(int value) const
00084 {
00085   if (value<=0) value= ctrl_.minimum;
00086   else if (value>=100) value= ctrl_.maximum;
00087        else value= ctrl_.minimum+(ctrl_.maximum-ctrl_.minimum)*value/100;
00088   set_value(value);
00089 }
00090 
00091 vcl_string vidl_v4l2_control_integer::description() const
00092 {
00093   char cad[256];
00094   vcl_snprintf(cad,256,"Control \"%s\": integer (min: %d, max: %d, step: %d, default: %d)",
00095                (const char *) ctrl_.name, minimum(), maximum(), step(), default_value());
00096   return cad;
00097 }
00098 
00099 // ----------------- Control menu ---------------
00100 
00101 vidl_v4l2_control_menu::vidl_v4l2_control_menu(const v4l2_queryctrl& ctr, int f):
00102                                                                 vidl_v4l2_control(ctr,f)
00103 {
00104   struct v4l2_querymenu menu;
00105   vcl_memset(&menu, 0, sizeof (menu));
00106   menu.id= ctrl_.id;
00107   for (menu.index = ctrl_.minimum; (int) menu.index <= ctrl_.maximum;menu.index++) {
00108                 if (0 == ioctl (fd, VIDIOC_QUERYMENU, &menu)) {
00109                         items.push_back((char *)menu.name);
00110                 } else {
00111                         vcl_cerr << "VIDIOC_QUERYMENU\n";
00112                         items.clear(); // control menu is not added to the list
00113                         return;
00114                 }
00115         }
00116 }
00117 
00118 vcl_string vidl_v4l2_control_menu::description() const
00119 {
00120   char cad[256];
00121   vcl_snprintf(cad,256,"Control \"%s\": menu (%d items, default: %d)",
00122                (const char *) ctrl_.name, n_items(), default_value());
00123   return cad;
00124 }
00125 
00126 // ----------------- Control boolean ---------------
00127