core/vidl/vidl_v4l2_control.h
Go to the documentation of this file.
00001 // This is core/vidl/vidl_v4l2_control.h
00002 #ifndef vidl_v4l2_control_h_
00003 #define vidl_v4l2_control_h_
00004 //:
00005 // \file
00006 // \brief A base control class for user controls
00007 //
00008 // \author Antonio Garrido
00009 // \verbatim
00010 //  Modifications
00011 //     10 Nov 2008 Created (A. Garrido)
00012 //\endverbatim
00013 
00014 #include <vcl_string.h>
00015 #include <vcl_vector.h>
00016 
00017 extern "C" {
00018 //#include <asm/types.h>          /* for videodev2.h */
00019 #include <sys/time.h>
00020 #include <linux/videodev2.h>
00021 };
00022 
00023 //: A base class for handle a control.
00024 class vidl_v4l2_control
00025 {
00026   protected:
00027     int fd;
00028     struct v4l2_queryctrl ctrl_;
00029     vidl_v4l2_control(const v4l2_queryctrl& ctr, int f):  fd(f), ctrl_(ctr) {}
00030     void set_value(int v) const;
00031     int get_value() const;
00032   public:
00033     virtual ~vidl_v4l2_control() {}
00034     //: Factory method to create new controls
00035     // \param ctr details from VIDIOC_QUERYCTRL
00036     // \param f associated file descriptor
00037     // \return pointer to base class of new control
00038     static vidl_v4l2_control * new_control(const v4l2_queryctrl& ctr, int f);
00039     //: Type of control
00040     // \return type as indicated in v4l2 specification
00041     v4l2_ctrl_type type() const { return ctrl_.type; }
00042     //: Name of control
00043     // \return name the driver assign to the control
00044     vcl_string name() const { return  (const char *) ctrl_.name; }
00045     //: A 1-line brief description
00046     virtual vcl_string description() const= 0;
00047     //: Id of control
00048     // \return ID (for example, V4L2_CID_BRIGHTNESS corresponds to brightness)
00049     int id() const { return ctrl_.id; }
00050     //: Control is read only
00051     bool read_only() const 
00052     { 
00053 #ifdef V4L2_CTRL_FLAG_READ_ONLY
00054       return ctrl_.flags & V4L2_CTRL_FLAG_READ_ONLY; 
00055 #else
00056       return false;    
00057 #endif
00058     }
00059     //: Control can change value of other controls
00060     bool affect_other_controls() const
00061     { 
00062 #ifdef V4L2_CTRL_FLAG_UPDATE
00063       return ctrl_.flags & V4L2_CTRL_FLAG_UPDATE;
00064 #else
00065       return false;
00066 #endif
00067      }
00068     //: Reset control
00069     // \note no-op fot button controls
00070     virtual void reset() const {}
00071 };
00072 
00073 //: A class for handle a control of type integer
00074 class vidl_v4l2_control_integer: public vidl_v4l2_control
00075 {
00076   public:
00077     vidl_v4l2_control_integer(const v4l2_queryctrl& ctr, int f): vidl_v4l2_control(ctr,f) {}
00078     //: Minimum value of the control
00079     int minimum() const { return ctrl_.minimum; }
00080     //: Maximum value of the control
00081     int maximum() const { return ctrl_.maximum; }
00082     //: Step size
00083     // Indicates the increment between values which are actually different on the hardware
00084     int step() const { return ctrl_.step; }
00085     //: Default value of this control
00086     int default_value() const { return ctrl_.default_value; }
00087     //: Set the value of the control 
00088     // \param value to be set in range determined by driver
00089     void set(int value) const;
00090     //: Change control 
00091     // \param value to be set in range 0-100
00092     void set_100 ( int value) const;
00093     //: Get the value of the control 
00094     // \return value in range determined by driver
00095     int get() const { return get_value(); }
00096     //: Get the value of the control 
00097     // \return value in range 0-100
00098     int get_100() const { return (get_value()-ctrl_.minimum)*100/(ctrl_.maximum-ctrl_.minimum); }
00099     //: A 1-line brief description
00100     virtual vcl_string description() const;
00101     //: Reset control
00102     virtual void reset() const { set(default_value()); }
00103 };
00104 
00105 //: A class for handle a control of type menu
00106 class vidl_v4l2_control_menu: public vidl_v4l2_control
00107 {
00108     vcl_vector<vcl_string> items;
00109   public:
00110     vidl_v4l2_control_menu(const v4l2_queryctrl& ctr, int f);
00111     //: Number of items
00112     // \return number of entries in the menu
00113     unsigned int n_items() const { return items.size();}
00114     //: Item i
00115     // \return string assigned to item i
00116     vcl_string get(unsigned int i) const { return items[i]; }
00117     //: Select item i
00118     void set(unsigned int i) const { if (i<n_items()) set_value((int) i); }
00119     //: Get the value of the control 
00120     // \return index in the menu
00121     unsigned int get() const { return (unsigned int) get_value(); }
00122     //: Default value of this control
00123     unsigned int default_value() const { return ctrl_.default_value; }
00124     //: A 1-line brief description
00125     virtual vcl_string description() const;
00126     //: Reset control
00127     virtual void reset() const { set(default_value()); }
00128 };
00129 
00130 //: A class for handle a control of type boolean
00131 class vidl_v4l2_control_boolean: public vidl_v4l2_control
00132 {
00133   public:
00134     vidl_v4l2_control_boolean(const v4l2_queryctrl& ctr, int f): vidl_v4l2_control(ctr,f) {}
00135     //: Set the value of the control 
00136     void set(bool v) const { set_value(v?1:0); }
00137     //: Get the value of the control 
00138     bool get() const { return get_value(); }
00139     //: Default value of this control
00140     bool default_value() const { return ctrl_.default_value; }
00141     //: A 1-line brief description
00142     virtual vcl_string description() const 
00143        { return "Control \""+name()+"\": boolean (default: "+(default_value()?"true":"false")+")"; }
00144     //: Reset control
00145     virtual void reset() const { set(default_value()); }
00146 };
00147 
00148 //: A class for handle a control of type button
00149 class vidl_v4l2_control_button: public vidl_v4l2_control
00150 {
00151   public:
00152     vidl_v4l2_control_button(const v4l2_queryctrl& ctr, int f): vidl_v4l2_control(ctr,f) {}
00153     //: Push button
00154     void push() const { set_value(1); }
00155     //: A 1-line brief description
00156     virtual vcl_string description() const { return "Control \""+name()+"\": button"; }
00157 };
00158 
00159 #endif // vidl_v4l2_control_h_