• Main Page
  • Related Pages
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

src/F2/Daemon.h

00001 #ifndef FCAM_F2_DAEMON_H
00002 #define FCAM_F2_DAEMON_H
00003 
00004 #include <queue>
00005 #include <pthread.h>
00006 #include <semaphore.h>
00007 
00008 #include "FCam/F2/Sensor.h"
00009 #include "FCam/F2/Frame.h"
00010 #include "FCam/TSQueue.h"
00011 
00012 #include "V4L2Sensor.h"
00013 
00014 
00015 namespace FCam { namespace F2 {
00016 
00017     // The daemon acts as a layer over /dev/video0. It accepts frame
00018     // requests and returns frames that (hopefully) meet those requests.
00019     class Daemon {
00020     public:
00021         // A class for actions slaved to a frame
00022         class Action {
00023         public:
00024             // When should this action run?
00025             // The run-time will set this value when it knows when the
00026             // start of the associated exposure is
00027             Time time;
00028             FCam::Action *action;
00029                 
00030             bool operator<(const Action &other) const {
00031                 // I'm lower priority than another action if I occur later
00032                 return time > other.time;
00033             }
00034                 
00035             bool operator>(const Action &other) const {
00036                 // I'm higher priority than another action if I occur earlier
00037                 return time < other.time;
00038             }
00039         };
00040             
00041         Daemon(Sensor *sensor);
00042         ~Daemon();
00043                        
00044         // enforce a drop policy on the frame queue
00045         void setDropPolicy(FCam::Sensor::DropPolicy p, int f);
00046       
00047         // The user-space puts requests on this queue. It is consumed by
00048         // the setter thread.
00049         TSQueue<_Frame *> requestQueue;
00050 
00051         // The handler thread puts frames on this queue. It is consumed by
00052         // user-space
00053         TSQueue<_Frame *> frameQueue;
00054 
00055         void launchThreads();
00056 
00057         void debugTiming(bool);
00058 
00059     private:
00060             
00061         // Access to the V4L2 layer of the sensor
00062         V4L2Sensor *v4l2Sensor;
00063 
00064         // Access to the FCam sensor object
00065         Sensor *sensor;
00066 
00067         // Converting user-level values to driver values
00068         int rowSkipDriver(RowSkip::e v) { return static_cast<int>(v)-1; }
00069         int colSkipDriver(ColSkip::e v) { return static_cast<int>(v)-1; }
00070         int rowBinDriver(RowBin::e v) {return static_cast<int>(v)-1; }
00071         int colBinDriver(ColBin::e v) {return static_cast<int>(v)-1; }
00072         // Convert driver values to user-level values
00073         RowSkip::e rowSkipFCam(int v) { return static_cast<RowSkip::e>(v+1); }
00074         ColSkip::e colSkipFCam(int v) { return static_cast<ColSkip::e>(v+1); }
00075         RowBin::e rowBinFCam(int v) {return static_cast<RowBin::e>(v+1); }
00076         ColBin::e colBinFCam(int v) {return static_cast<ColBin::e>(v+1); }
00077             
00078         bool stop;
00079 
00080         void setReadoutParams(_Frame *req, bool modeSwitch = false);
00081         void setExposureParams(_Frame *req, bool modeSwitch = false);
00082             
00083         void setTimes(_Frame *req, const Time &, bool modeSwitch = false);
00084                   
00085         // The frameQueue may not grow beyond this limit
00086         size_t frameLimit;
00087         // What should I do if the frame policy tries to grow beyond the limit
00088         FCam::Sensor::DropPolicy dropPolicy;
00089         void enforceDropPolicy();   
00090             
00091         // The setter thread puts in flight requests on this queue, which
00092         // is consumed by the handler thread
00093         TSQueue<_Frame *> inFlightQueue;
00094 
00095         // Sometimes the setter needs to access the camera exclusively
00096         // (e.g. to do a pipeline flush). This mutex and flag are used for
00097         // coordinating such things.
00098         pthread_mutex_t cameraMutex;
00099         // The setter thread requests a pipeline flush by setting this
00100         // flag high and waiting on the above mutex.
00101         bool pipelineFlush;
00102 
00103         // The setter thread also queues up RT actions on this priority
00104         // queue, which is consumed by the actions thread
00105         std::priority_queue<Action> actionQueue;
00106         pthread_mutex_t actionQueueMutex;
00107         sem_t actionQueueSemaphore;
00108 
00109 
00110         // The component of the daemon that sets exposure and gain
00111         void runSetter();   
00112         pthread_t setterThread;
00113 
00114         void tickSetter(Time hs_vs);
00115         bool setterRunning;
00116 
00117         // The current state of the camera
00118         _Frame current;
00119 
00120         // Previous state of camera
00121         Shot prevShot;
00122 
00123         // The component that handles frames returned by V4L2
00124         void runHandler();
00125         pthread_t handlerThread;
00126         bool handlerRunning;
00127 
00128         // The component that executes RT actions
00129         void runAction();
00130         pthread_t actionThread;
00131         bool actionRunning;
00132 
00133         bool waitingForFirstRequest;
00134             
00135         bool debugMode;
00136 
00137         friend void *daemon_setter_thread_(void *arg);
00138         friend void *daemon_handler_thread_(void *arg);
00139         friend void *daemon_action_thread_(void *arg);
00140     };
00141 
00142     }}
00143 
00144 #endif

Generated on Mon Aug 16 2010 14:25:45 for FCam by  doxygen 1.7.1