00001 #include <pthread.h>
00002
00003 #include "FCam/Action.h"
00004 #include "FCam/F2/Sensor.h"
00005
00006 #include "FCam/F2/Platform.h"
00007 #include "Daemon.h"
00008 #include "linux/mt9p031.h"
00009 #include "../Debug.h"
00010
00011
00012
00013 namespace FCam {
00014 namespace F2 {
00015
00016
00017 Sensor::Sensor() : FCam::Sensor(), daemon(NULL), shotsPending_(0) {
00018 pthread_mutex_init(&requestMutex, NULL);
00019 }
00020
00021 Sensor::~Sensor() {
00022 stop();
00023 pthread_mutex_destroy(&requestMutex);
00024 }
00025
00026 void Sensor::start() {
00027 if (daemon) { return; }
00028 daemon = new Daemon(this);
00029 daemon->launchThreads();
00030 }
00031
00032 void Sensor::stop() {
00033 if (!daemon) { return; }
00034 delete daemon;
00035 daemon = NULL;
00036 }
00037
00038 void Sensor::capture(const FCam::Shot &s) {
00039 Shot F2s(s);
00040 F2s.id = s.id;
00041 capture(F2s);
00042 }
00043
00044 void Sensor::capture(const Shot &shot) {
00045 start();
00046
00047
00048 _Frame *f = new _Frame;
00049 f->_shot = shot;
00050 f->_shot.id = shot.id;
00051
00052
00053 pthread_mutex_lock(&requestMutex);
00054 shotsPending_++;
00055 daemon->requestQueue.push(f);
00056 pthread_mutex_unlock(&requestMutex);
00057 }
00058
00059 void Sensor::capture(const std::vector<FCam::Shot> &burst) {
00060 std::vector<Shot> F2Burst;
00061 F2Burst.reserve(burst.size());
00062 for (unsigned int i=0; i < burst.size(); i++) {
00063 F2Burst.push_back(Shot(burst[i]));
00064 F2Burst[i].id = burst[i].id;
00065 }
00066 capture(F2Burst);
00067 }
00068
00069 void Sensor::capture(const std::vector<Shot> &burst) {
00070 start();
00071
00072 std::vector<_Frame *> frames;
00073
00074 for (size_t i = 0; i < burst.size(); i++) {
00075
00076
00077 _Frame *f = new _Frame;
00078 f->_shot = burst[i];
00079 f->_shot.id = burst[i].id;
00080
00081 frames.push_back(f);
00082 }
00083
00084 pthread_mutex_lock(&requestMutex);
00085 for (size_t i = 0; i < frames.size(); i++) {
00086 shotsPending_++;
00087 daemon->requestQueue.push(frames[i]);
00088 }
00089 pthread_mutex_unlock(&requestMutex);
00090 }
00091
00092 void Sensor::stream(const FCam::Shot &s) {
00093 Shot F2s(s);
00094 F2s.id = s.id;
00095 stream(F2s);
00096 }
00097
00098 void Sensor::stream(const Shot &shot) {
00099
00100
00101 pthread_mutex_lock(&requestMutex);
00102 streamingShot.clear();
00103
00104 streamingShot.push_back(shot);
00105 streamingShot[0].id = shot.id;
00106 pthread_mutex_unlock(&requestMutex);
00107
00108 start();
00109 if (daemon->requestQueue.size() == 0) { capture(streamingShot); }
00110 }
00111
00112 void Sensor::stream(const std::vector<FCam::Shot> &burst) {
00113 std::vector<Shot> F2Burst;
00114 F2Burst.reserve(burst.size());
00115 for (unsigned int i=0; i < burst.size(); i++) {
00116 F2Burst.push_back(Shot(burst[i]));
00117 F2Burst[i].id = burst[i].id;
00118 }
00119 stream(F2Burst);
00120 }
00121
00122 void Sensor::stream(const std::vector<Shot> &burst) {
00123 pthread_mutex_lock(&requestMutex);
00124
00125 streamingShot = burst;
00126
00127
00128 for (unsigned int i=0; i<burst.size(); i++) {
00129 streamingShot[i].id = burst[i].id;
00130 }
00131 pthread_mutex_unlock(&requestMutex);
00132
00133 start();
00134 if (daemon->requestQueue.size() == 0) { capture(streamingShot); }
00135 }
00136
00137 bool Sensor::streaming() {
00138 return streamingShot.size() > 0;
00139 }
00140
00141 void Sensor::stopStreaming() {
00142 pthread_mutex_lock(&requestMutex);
00143 streamingShot.clear();
00144 pthread_mutex_unlock(&requestMutex);
00145 }
00146
00147 FCam::F2::Frame Sensor::getFrame() {
00148
00149 start();
00150
00151 _Frame *_f;
00152 _f = daemon->frameQueue.pull();
00153
00154 Frame frame(_f);
00155 FCam::Sensor::tagFrame(frame);
00156 for (size_t i=0; i< devices.size(); i++) {
00157 devices[i]->tagFrame(frame);
00158 }
00159 pthread_mutex_lock(&requestMutex);
00160 shotsPending_--;
00161 pthread_mutex_unlock(&requestMutex);
00162
00163 return frame;
00164 }
00165
00166
00167 Size Sensor::minImageSize() const {
00168 return Size(640, 480);
00169 }
00170 Size Sensor::maxImageSize() const {
00171 return Size(MT9P031_ACTIVE_WIDTH, MT9P031_ACTIVE_HEIGHT);
00172 }
00173 Size Sensor::pixelArraySize() {
00174 return Size(MT9P031_ARRAY_WIDTH, MT9P031_ARRAY_HEIGHT);
00175 }
00176
00177 Rect Sensor::activeArrayRect() {
00178 return Rect(0,0,
00179 MT9P031_ACTIVE_WIDTH, MT9P031_ACTIVE_HEIGHT);
00180 }
00181 Rect Sensor::pixelArrayRect() {
00182 return Rect(MT9P031_MIN_COL,
00183 MT9P031_MIN_ROW,
00184 MT9P031_ARRAY_WIDTH,
00185 MT9P031_ARRAY_HEIGHT);
00186 }
00187
00188 int Sensor::rollingShutterTime(const Shot &s) const {
00189
00190 if (s.image.height() > 960) { return 77000; }
00191 else { return 33000; }
00192 }
00193
00194 int Sensor::rollingShutterTime(const FCam::Shot &s) const {
00195
00196 if (s.image.height() > 960) { return 77000; }
00197 else { return 33000; }
00198 }
00199
00200
00201 void Sensor::generateRequest() {
00202 pthread_mutex_lock(&requestMutex);
00203 if (streamingShot.size()) {
00204 std::vector<_Frame *> frames;
00205
00206 for (size_t i = 0; i < streamingShot.size(); i++) {
00207
00208
00209 _Frame *f = new _Frame;
00210 f->_shot = streamingShot[i];
00211 f->_shot.id = streamingShot[i].id;
00212
00213 frames.push_back(f);
00214 }
00215
00216 for (size_t i = 0; i < frames.size(); i++) {
00217 shotsPending_++;
00218 daemon->requestQueue.push(frames[i]);
00219 }
00220 }
00221 pthread_mutex_unlock(&requestMutex);
00222 }
00223
00224 void Sensor::enforceDropPolicy() {
00225 if (!daemon) { return; }
00226 daemon->setDropPolicy(dropPolicy, frameLimit);
00227 }
00228
00229 int Sensor::framesPending() const {
00230 if (!daemon) { return 0; }
00231 return daemon->frameQueue.size();
00232 }
00233
00234 int Sensor::shotsPending() const {
00235 if (!daemon) { return 0; }
00236 return shotsPending_;
00237 }
00238
00239 void Sensor::debugTiming(bool enable) {
00240 start();
00241 daemon->debugTiming(enable);
00242 }
00243
00244 }
00245 }