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 #include "../Sensor.h"
00008 #include "Frame.h"
00009 #include "V4L2Sensor.h"
00010
00011
00012 namespace FCam {
00013 namespace F2 {
00014
00015
00016
00017 class Daemon {
00018 public:
00019
00020 class Action {
00021 public:
00022
00023
00024
00025 Time time;
00026 FCam::Action *action;
00027
00028 bool operator<(const Action &other) const {
00029
00030 return time > other.time;
00031 }
00032
00033 bool operator>(const Action &other) const {
00034
00035 return time < other.time;
00036 }
00037 };
00038
00039
00040 Daemon();
00041 ~Daemon();
00042
00043 class RequestGenerator {
00044 public:
00045 virtual void generateRequest() = 0;
00046 };
00047
00048 void setRequestGenerator(RequestGenerator *gen);
00049 RequestGenerator *getRequestGenerator();
00050
00051 int requestFrame(Frame *);
00052
00053
00054 void setDropPolicy(FCam::Sensor::DropPolicy p, int f);
00055
00056 Frame *getNextFrame();
00057 int framesPending() const {return frameQueue.size();}
00058
00059 void debugTiming(bool);
00060
00061 private:
00062
00063
00064 V4L2Sensor *sensor;
00065
00066
00067 int rowSkipDriver(RowSkip::e v) { return static_cast<int>(v)-1; }
00068 int colSkipDriver(ColSkip::e v) { return static_cast<int>(v)-1; }
00069 int rowBinDriver(RowBin::e v) {return static_cast<int>(v)-1; }
00070 int colBinDriver(ColBin::e v) {return static_cast<int>(v)-1; }
00071
00072 RowSkip::e rowSkipFCam(int v) { return static_cast<RowSkip::e>(v+1); }
00073 ColSkip::e colSkipFCam(int v) { return static_cast<ColSkip::e>(v+1); }
00074 RowBin::e rowBinFCam(int v) {return static_cast<RowBin::e>(v+1); }
00075 ColBin::e colBinFCam(int v) {return static_cast<ColBin::e>(v+1); }
00076
00077 bool stop;
00078
00079 void setReadoutParams(Frame *req, bool modeSwitch = false);
00080 void setExposureParams(Frame *req, bool modeSwitch = false);
00081
00082 void setTimes(Frame *req, const Time &, bool modeSwitch = false);
00083
00084
00085
00086 std::queue<Frame *> requestQueue;
00087 pthread_mutex_t requestQueueMutex;
00088
00089
00090
00091 RequestGenerator *generator;
00092
00093
00094
00095 std::queue<Frame *> frameQueue;
00096 pthread_mutex_t frameQueueMutex;
00097 sem_t frameQueueSemaphore;
00098
00099 size_t frameLimit;
00100
00101 FCam::Sensor::DropPolicy dropPolicy;
00102 void enforceDropPolicy();
00103
00104
00105
00106 std::queue<Frame *> inFlightQueue;
00107 pthread_mutex_t inFlightQueueMutex;
00108
00109
00110
00111
00112 pthread_mutex_t cameraMutex;
00113
00114
00115 bool pipelineFlush;
00116
00117
00118
00119 std::priority_queue<Action> actionQueue;
00120 pthread_mutex_t actionQueueMutex;
00121 sem_t actionQueueSemaphore;
00122
00123 void launchThreads();
00124
00125
00126 void runSetter();
00127 pthread_t setterThread;
00128
00129 void tickSetter(Time hs_vs);
00130 bool setterRunning;
00131
00132 Frame current;
00133
00134 Shot prevReq;
00135
00136
00137 void runHandler();
00138 pthread_t handlerThread;
00139 bool handlerRunning;
00140
00141
00142 void runAction();
00143 pthread_t actionThread;
00144 bool actionRunning;
00145
00146 bool waitingForFirstRequest;
00147
00148 bool debugMode;
00149
00150 friend void *daemon_setter_thread_(void *arg);
00151 friend void *daemon_handler_thread_(void *arg);
00152 friend void *daemon_action_thread_(void *arg);
00153 };
00154
00155 }}
00156
00157 #endif