Fawkes API  Fawkes Development Version
v4l2.h
00001 
00002 /***************************************************************************
00003  *  v4l2.h - Video4Linux 2 camera access
00004  *
00005  *  Generated: Sat Jul  5 20:40:20 2008
00006  *  Copyright  2008 Tobias Kellner
00007  *
00008  ****************************************************************************/
00009 
00010 /*  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; either version 2 of the License, or
00013  *  (at your option) any later version. A runtime exception applies to
00014  *  this software (see LICENSE.GPL_WRE file mentioned below for details).
00015  *
00016  *  This program is distributed in the hope that it will be useful,
00017  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00018  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019  *  GNU Library General Public License for more details.
00020  *
00021  *  Read the full text in the LICENSE.GPL_WRE file in the doc directory.
00022  */
00023 
00024 #ifndef __FIREVISION_CAMS_V4L2_H_
00025 #define __FIREVISION_CAMS_V4L2_H_
00026 
00027 #include <cams/camera.h>
00028 
00029 #include <linux/types.h>
00030 #include <linux/videodev2.h>
00031 
00032 #include <cams/control/color.h>
00033 #include <cams/control/image.h>
00034 
00035 /* Number of buffers to use for memory mapped IO */
00036 #define MMAP_NUM_BUFFERS 4;
00037 
00038 namespace firevision {
00039 #if 0 /* just to make Emacs auto-indent happy */
00040 }
00041 #endif
00042 
00043 class CameraArgumentParser;
00044 class V4L2CameraData;
00045 
00046 class V4L2Camera:
00047   public Camera,
00048   public CameraControlColor,
00049   public CameraControlImage
00050 {
00051   friend class V4LCamera;
00052 
00053  public:
00054   V4L2Camera(const char *device_name = "/dev/video0");
00055   V4L2Camera(const CameraArgumentParser *cap);
00056   virtual ~V4L2Camera();
00057 
00058   virtual void open();
00059   virtual void start();
00060   virtual void stop();
00061   virtual void close();
00062   virtual void flush();
00063   virtual void capture();
00064   virtual void print_info();
00065   virtual bool ready();
00066 
00067   virtual unsigned char *buffer();
00068   virtual unsigned int   buffer_size();
00069   virtual void           dispose_buffer();
00070 
00071   virtual unsigned int   pixel_width();
00072   virtual unsigned int   pixel_height();
00073   virtual colorspace_t   colorspace();
00074   virtual fawkes::Time * capture_time();
00075 
00076   virtual void           set_image_number(unsigned int n);
00077 
00078   virtual bool         auto_gain();
00079   virtual void         set_auto_gain(bool enabled);
00080   virtual bool         auto_white_balance();
00081   virtual void         set_auto_white_balance(bool enabled);
00082   virtual bool         auto_exposure();
00083   virtual void         set_auto_exposure(bool enabled);
00084   virtual int          red_balance();
00085   virtual void         set_red_balance(int red_balance);
00086   virtual int          blue_balance();
00087   virtual void         set_blue_balance(int blue_balance);
00088   virtual int          u_balance();
00089   virtual void         set_u_balance(int u_balance);
00090   virtual int          v_balance();
00091   virtual void         set_v_balance(int v_balance);
00092   virtual unsigned int brightness();
00093   virtual void         set_brightness(unsigned int brightness);
00094   virtual unsigned int contrast();
00095   virtual void         set_contrast(unsigned int contrast);
00096   virtual unsigned int saturation();
00097   virtual void         set_saturation(unsigned int saturation);
00098   virtual int          hue();
00099   virtual void         set_hue(int hue);
00100   virtual unsigned int exposure();
00101   virtual void         set_exposure(unsigned int exposure);
00102   virtual unsigned int gain();
00103   virtual void         set_gain(unsigned int gain);
00104 
00105   virtual const char * format();
00106   virtual void         set_format(const char *format);
00107   virtual unsigned int width();
00108   virtual unsigned int height();
00109   virtual void         set_size(unsigned int width,
00110                                 unsigned int height);
00111   virtual bool         horiz_mirror();
00112   virtual bool         vert_mirror();
00113   virtual void         set_horiz_mirror(bool enabled);
00114   virtual void         set_vert_mirror(bool enabled);
00115   virtual unsigned int fps();
00116   virtual void         set_fps(unsigned int fps);
00117   virtual unsigned int lens_x_corr();
00118   virtual unsigned int lens_y_corr();
00119   virtual void         set_lens_x_corr(unsigned int x_corr);
00120   virtual void         set_lens_y_corr(unsigned int y_corr);
00121 
00122 
00123  protected:
00124   V4L2Camera(const char *device_name, int dev);
00125   virtual void set_one_control(const char *ctrl, unsigned int id, int value);
00126   virtual int get_one_control(const char *ctrl, unsigned int id);
00127 
00128  private:
00129   virtual void post_open();
00130   virtual void select_read_method();
00131   virtual void select_format();
00132   virtual void set_fps();
00133   virtual void set_controls();
00134   virtual void create_buffer();
00135   virtual void reset_cropping();
00136 
00137  protected:
00138   char *_device_name; ///< Device name
00139 
00140  private:
00141   enum ReadMethod
00142   {
00143     READ, ///< read() input
00144     MMAP, ///< Memory mapping input
00145     UPTR  ///< User pointer input
00146   };
00147 
00148   enum TriState
00149   {
00150     NOT_SET, ///< parameter not set
00151     TRUE,    ///< parameter set to true
00152     FALSE    ///< parameter set to false
00153   };
00154 
00155   struct FrameBuffer
00156   {
00157     unsigned char *buffer;
00158     unsigned int size;
00159   };
00160 
00161   struct ControlParameterInt
00162   {
00163     bool set;
00164     int value;
00165   };
00166 
00167   int _dev;                          ///< Device file descriptor
00168 
00169   V4L2CameraData *_data;
00170 
00171   ReadMethod _read_method;           ///< Used read method
00172   bool _opened;                      ///< Device has been open()ed
00173   bool _started;                     ///< Device has been start()ed
00174   char _format[5];                   ///< FourCC of the image format
00175   colorspace_t _colorspace;          ///< Used colorspace_t
00176 
00177   unsigned int _width;               ///< Image width
00178   unsigned int _height;              ///< Image height
00179   unsigned int _bytes_per_line;      ///< Image bytes per line
00180   FrameBuffer *_frame_buffers;       ///< Image buffers
00181   unsigned int _buffers_length;      ///< Image buffer size
00182   int _current_buffer;               ///< Current Image buffer (-1 if not set)
00183   fawkes::Time *_capture_time;       ///< Time when last picture was captured
00184 
00185   bool _switch_u_v;                  ///< Switch U and V channels
00186   unsigned int _fps;                 ///< Capture FPS
00187 
00188   TriState _aec;                     ///< Auto Exposition enabled
00189   TriState _awb;                     ///< Auto White Balance enabled
00190   TriState _agc;                     ///< Auto Gain enabled
00191   TriState _h_flip;                  ///< Horizontal mirror
00192   TriState _v_flip;                  ///< Vertical mirror
00193   ControlParameterInt _brightness;   ///< Brightness [0-255] (def. 128)
00194   ControlParameterInt _contrast;     ///< Contrast [0-127] (def. 64)
00195   ControlParameterInt _saturation;   ///< Saturation [0-256] (def. 128)
00196   ControlParameterInt _hue;          ///< Hue [-180-180] (def. 0)
00197   ControlParameterInt _red_balance;  ///< Red Balance [0-255] (def. 128)
00198   ControlParameterInt _blue_balance; ///< Blue Balance [0-255] (def. 128)
00199   ControlParameterInt _exposure;     ///< Exposure [0-65535] (def. 60)
00200   ControlParameterInt _gain;         ///< Gain [0-255] (def. 0)
00201   ControlParameterInt _lens_x;       ///< Lens Correction X [0-255] (def. 0)
00202   ControlParameterInt _lens_y;       ///< Lens Correction Y [0-255] (def. 0)
00203 
00204   bool _nao_hacks;                   ///< Nao-specific hacks (bad driver)
00205 
00206 };
00207 
00208 } // end namespace firevision
00209 
00210 #endif //__FIREVISION_CAMS_V4L2_H_