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 2;
00037 
00038 class CameraArgumentParser;
00039 class V4L2CameraData;
00040
00041 class V4L2Camera:
00042   public Camera,
00043   public CameraControlColor,
00044   public CameraControlImage
00045 {
00046   friend class V4LCamera;
00047
00048  public:
00049   V4L2Camera(const char *device_name = "/dev/video0");
00050   V4L2Camera(const CameraArgumentParser *cap);
00051   virtual ~V4L2Camera();
00052
00053   virtual void open();
00054   virtual void start();
00055   virtual void stop();
00056   virtual void close();
00057   virtual void flush();
00058   virtual void capture();
00059   virtual void print_info();
00060   virtual bool ready();
00061
00062   virtual unsigned char *buffer();
00063   virtual unsigned int   buffer_size();
00064   virtual void           dispose_buffer();
00065
00066   virtual unsigned int   pixel_width();
00067   virtual unsigned int   pixel_height();
00068   virtual colorspace_t   colorspace();
00069   virtual fawkes::Time * capture_time();
00070
00071   virtual void           set_image_number(unsigned int n);
00072
00073   virtual bool         auto_gain();
00074   virtual void         set_auto_gain(bool enabled);
00075   virtual bool         auto_white_balance();
00076   virtual void         set_auto_white_balance(bool enabled);
00077   virtual bool         auto_exposure();
00078   virtual void         set_auto_exposure(bool enabled);
00079   virtual int          red_balance();
00080   virtual void         set_red_balance(int red_balance);
00081   virtual int          blue_balance();
00082   virtual void         set_blue_balance(int blue_balance);
00083   virtual int          u_balance();
00084   virtual void         set_u_balance(int u_balance);
00085   virtual int          v_balance();
00086   virtual void         set_v_balance(int v_balance);
00087   virtual unsigned int brightness();
00088   virtual void         set_brightness(unsigned int brightness);
00089   virtual unsigned int contrast();
00090   virtual void         set_contrast(unsigned int contrast);
00091   virtual unsigned int saturation();
00092   virtual void         set_saturation(unsigned int saturation);
00093   virtual int          hue();
00094   virtual void         set_hue(int hue);
00095   virtual unsigned int exposure();
00096   virtual void         set_exposure(unsigned int exposure);
00097   virtual unsigned int gain();
00098   virtual void         set_gain(unsigned int gain);
00099
00100   virtual const char * format();
00101   virtual void         set_format(const char *format);
00102   virtual unsigned int width();
00103   virtual unsigned int height();
00104   virtual void         set_size(unsigned int width,
00105                                 unsigned int height);
00106   virtual bool         horiz_mirror();
00107   virtual bool         vert_mirror();
00108   virtual void         set_horiz_mirror(bool enabled);
00109   virtual void         set_vert_mirror(bool enabled);
00110   virtual unsigned int fps();
00111   virtual void         set_fps(unsigned int fps);
00112   virtual unsigned int lens_x_corr();
00113   virtual unsigned int lens_y_corr();
00114   virtual void         set_lens_x_corr(unsigned int x_corr);
00115   virtual void         set_lens_y_corr(unsigned int y_corr);
00116
00117
00118  protected:
00119   V4L2Camera(const char *device_name, int dev);
00120
00121  private:
00122   virtual void post_open();
00123   virtual void select_read_method();
00124   virtual void select_format();
00125   virtual void set_fps();
00126   virtual void set_controls();
00127   virtual void set_one_control(const char *ctrl, unsigned int id, int value);
00128   virtual int get_one_control(const char *ctrl, unsigned int id);
00129   virtual void create_buffer();
00130   virtual void reset_cropping();
00131
00132  protected:
00133   char *_device_name; ///< Device name
00134 
00135  private:
00136   enum ReadMethod
00137   {
00138     READ, ///< read() input
00139     MMAP, ///< Memory mapping input
00140     UPTR  ///< User pointer input
00141   };
00142
00143   enum TriState
00144   {
00145     NOT_SET, ///< parameter not set
00146     TRUE,    ///< parameter set to true
00147     FALSE    ///< parameter set to false
00148   };
00149
00150   struct FrameBuffer
00151   {
00152     unsigned char *buffer;
00153     unsigned int size;
00154   };
00155
00156   struct ControlParameterInt
00157   {
00158     bool set;
00159     int value;
00160   };
00161
00162   int _dev;                          ///< Device file descriptor
00163 
00164   V4L2CameraData *_data;
00165
00166   ReadMethod _read_method;           ///< Used read method
00167   bool _opened;                      ///< Device has been open()ed
00168   bool _started;                     ///< Device has been start()ed
00169   char _format[5];                   ///< FourCC of the image format
00170   colorspace_t _colorspace;          ///< Used colorspace_t
00171 
00172   unsigned int _width;               ///< Image width
00173   unsigned int _height;              ///< Image height
00174   unsigned int _bytes_per_line;      ///< Image bytes per line
00175   FrameBuffer *_frame_buffers;       ///< Image buffers
00176   unsigned int _buffers_length;      ///< Image buffer size
00177   int _current_buffer;               ///< Current Image buffer (-1 if not set)
00178   fawkes::Time *_capture_time;       ///< Time when last picture was captured
00179 
00180   bool _switch_u_v;                  ///< Switch U and V channels
00181   unsigned int _fps;                 ///< Capture FPS
00182 
00183   TriState _aec;                     ///< Auto Exposition enabled
00184   TriState _awb;                     ///< Auto White Balance enabled
00185   TriState _agc;                     ///< Auto Gain enabled
00186   TriState _h_flip;                  ///< Horizontal mirror
00187   TriState _v_flip;                  ///< Vertical mirror
00188   ControlParameterInt _brightness;   ///< Brightness [0-255] (def. 128)
00189   ControlParameterInt _contrast;     ///< Contrast [0-127] (def. 64)
00190   ControlParameterInt _saturation;   ///< Saturation [0-256] (def. 128)
00191   ControlParameterInt _hue;          ///< Hue [-180-180] (def. 0)
00192   ControlParameterInt _red_balance;  ///< Red Balance [0-255] (def. 128)
00193   ControlParameterInt _blue_balance; ///< Blue Balance [0-255] (def. 128)
00194   ControlParameterInt _exposure;     ///< Exposure [0-65535] (def. 60)
00195   ControlParameterInt _gain;         ///< Gain [0-255] (def. 0)
00196   ControlParameterInt _lens_x;       ///< Lens Correction X [0-255] (def. 0)
00197   ControlParameterInt _lens_y;       ///< Lens Correction Y [0-255] (def. 0)
00198 
00199   bool _nao_hacks;                   ///< Nao-specific hacks (bad driver)
00200 
00201 };
00202
00203 #endif //__FIREVISION_CAMS_V4L2_H_