Tekkotsu Homepage
Demos
Overview
Downloads
Dev. Resources
Reference
Credits

CameraDriverV4L1.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_CameraDriverV4L1_h_
00003 #define INCLUDED_CameraDriverV4L1_h_
00004 
00005 #include "local/DeviceDriver.h"
00006 #include "local/DataSource.h"
00007 #include "Shared/get_time.h"
00008 #include "IPC/Thread.h"
00009 
00010 #include <linux/types.h> 
00011 #include <linux/videodev.h>
00012 
00013 #include <map>
00014 #include <vector>
00015 
00016 //! description of CameraDriverV4L1
00017 class CameraDriverV4L1 : public virtual DeviceDriver, public virtual plist::PrimitiveListener, public DataSource {
00018 public:
00019   explicit CameraDriverV4L1(const std::string& name)
00020     : DeviceDriver(autoRegisterCameraDriverV4L1,name), DataSource(),
00021       path("/dev/video0"), resolution(1), layer(0), autobright(true),
00022       camfd(-1), img_size(0), vid_caps(), vid_win(), vid_pic(), frameCount(0), isYUVMode(false), blocking(false),
00023       curBuf(), lastBuf(), tmpBuf(), lock()
00024   {
00025     addEntry("Path",path,"Path to the video device, e.g. /dev/video0");
00026     addEntry("Downsample",resolution,"The downsampling level, each increment cuts the image in half (0 is full-size)");
00027     addEntry("Layer",layer,"Controls the resolution layer at which the image should be processed.\n"
00028               "0 indicates \"automatic\" mode (picks layer closest to image's resolution), positive numbers indicate the resolution layer directly.\n"
00029               "Negative values are relative to the number of layers marked available by the vision setup, so that typically -1 would correspond to the \"double\" layer, and -2 would correspond to the \"full\" layer.");
00030     addEntry("Autobright",autobright,"If true, will automatically adjust camera gain");
00031   }
00032   
00033   ~CameraDriverV4L1() {
00034     resolution.removePrimitiveListener(this);
00035     path.removePrimitiveListener(this);
00036   }
00037   
00038   virtual std::string getClassName() const { return autoRegisterCameraDriverV4L1; }
00039   
00040   virtual void getImageSources(std::map<std::string,DataSource*>& sources) {
00041     sources.clear(); sources.insert(std::make_pair("Camera",this));
00042   }
00043 
00044   virtual unsigned int nextTimestamp() { return get_time(); }
00045   virtual const std::string& nextName() { return instanceName; }
00046   virtual unsigned int getData(const char *& payload, unsigned int& payloadSize, unsigned int& timestamp, std::string& name);
00047   virtual void setDataSourceThread(LoadDataThread* th);
00048 
00049   //! watches #path, triggers a close() and re-open() if it changes
00050   virtual void plistValueChanged(const plist::PrimitiveBase& pl);
00051 
00052   plist::Primitive<std::string> path;
00053   plist::Primitive<unsigned int> resolution;
00054   plist::Primitive<int> layer; //!< Controls the resolution layer at which the image should be processed.\n 0 indicates "automatic" mode (picks layer closest to image's resolution), positive numbers indicate the resolution layer directly.\n Negative values are relative to the number of layers marked available by the vision setup, so that typically -1 would correspond to the "double" layer, and -2 would correspond to the "full" layer.
00055   plist::Primitive<bool> autobright;
00056 
00057 protected:
00058   void close_cam();
00059   void open_cam();
00060   void get_cam_info();
00061   void set_cam_info();
00062   void set_resolution();
00063   void auto_bright();
00064   void interleave_yuv_down();
00065   void interleave_yuv_up();
00066 
00067   size_t getWidth() const { return (resolution==0 ? vid_win.width : vid_win.width/2); }
00068   size_t getHeight() const { return (resolution==0 ? vid_win.height : vid_win.height/2); }
00069   size_t getBufferSize() const { size_t a=vid_win.width*vid_win.height; return HEADER_SIZE+(resolution==0 ? a*3 : a*3/2); }
00070 
00071   static const size_t HEADER_SIZE;
00072 
00073   int camfd;
00074     size_t img_size;
00075     struct video_capability vid_caps;
00076     struct video_window vid_win;
00077     struct video_picture vid_pic;
00078     unsigned int frameCount;
00079     bool isYUVMode;
00080   bool blocking;
00081 
00082   std::vector<unsigned char> curBuf; //!< the buffer to write the image into
00083   std::vector<unsigned char> lastBuf; //!< the buffer we returned last time (might still be in use while writing #curBuf)
00084   std::vector<unsigned char> tmpBuf; //!< used during interleave_yuv_up(), swapped back and forth with #curBuf
00085   Thread::Lock lock; // buffer/img_size lock so we can't change resolution while reading
00086 
00087   /*class GrabThread : public Thread {
00088   public:
00089     GrabThread(std::vector<unsigned char>& buf, int cameraFD) : Thread(), curBuf(buf), camfd(cameraFD), lock() {}
00090     Thread::Lock& getLock() { return lock; }
00091     void setImageSize(size_t imageSize) { image_size = imageSize; }
00092   protected:
00093     std::vector<unsigned char>& curBuf;
00094     int camfd;
00095     Thread::Lock lock;
00096     };*/
00097 
00098 private:
00099   //! holds the class name, set via registration with the DeviceDriver registry
00100   static const std::string autoRegisterCameraDriverV4L1;
00101 };
00102 
00103 /*! @file
00104  * @brief 
00105  * @author Ethan Tira-Thompson (ejt) (Creator)
00106  */
00107 
00108 #endif

Tekkotsu Hardware Abstraction Layer 5.1CVS
Generated Mon May 9 05:01:37 2016 by Doxygen 1.6.3