Changeset 4076 for trunk/MagicSoft/Cosy/videodev/Camera.cc
- Timestamp:
- 05/15/04 16:46:27 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MagicSoft/Cosy/videodev/Camera.cc
r2518 r4076 17 17 inline int Camera::Ioctl(int req, void *opt, const char *str) 18 18 { 19 int rc = ioctl(fd, req, opt); 20 21 if (rc==-1) 22 { 23 cout << "Error! Ioctl " << req << ": "; 19 while (1) 20 { 21 const int rc = ioctl(fd, req, opt); 22 if (rc>=0) 23 return rc; 24 25 // errno== 4: Interrupted system call 26 // errno==16: Device or resource busy 27 if (errno==4 || errno==16) 28 { 29 cout << "Camera: ioctl returned rc=" << rc << " '"; 30 cout << (str?str:strerror(errno)) << "' (errno = " << errno << ")" << endl; 31 usleep(10); 32 continue; 33 } 34 35 cout << "Error! Ioctl " << hex << req << ": errno=" << dec << errno << " - "; 24 36 cout << (str?str:strerror(errno)) << " (rc=" << rc << ")" << endl; 25 }26 27 return rc;37 return rc; 38 } 39 return -1; 28 40 } 29 41 30 42 void Camera::Error(const char *str, int fatal) 31 43 { 32 cout << endl 33 << (fatal?"Fatal ":"") << "Error! " << str << ": " << strerror(errno)34 44 cout << endl; 45 cout << (fatal?"Fatal ":"") << "Error! " << str << ": " << strerror(errno); 46 cout << endl; 35 47 36 48 if (fatal) … … 39 51 void Camera::SigInit() 40 52 { 41 return; 42 struct sigaction act, old; 43 44 memset(&act, 0, sizeof(act)); 45 46 act.sa_handler = SigAlarm; 47 48 sigemptyset(&act. sa_mask); 49 sigaction(SIGALRM, &act, &old); 50 51 // signal(SIGINT, ctrlc); 53 /* 54 struct sigaction act, old; 55 56 memset(&act, 0, sizeof(act)); 57 58 act.sa_handler = SigAlarm; 59 60 sigemptyset(&act. sa_mask); 61 sigaction(SIGALRM, &act, &old); 62 63 // signal(SIGINT, ctrlc); 64 */ 52 65 } 53 66 … … 74 87 int Camera::StartGrab(unsigned int frame) 75 88 { 89 // We could also get RGB555, RGB565 and RGB32. But we want 90 // RGB24 because we have a 8bit DAC which gives us 8bit per 91 // color ==> RGB24 which is in the following the most simple 92 // to process. 76 93 static struct video_mmap gb = 77 94 { 78 0, 79 rows, cols, 80 VIDEO_PALETTE_RGB24 95 0, // frame 96 rows, cols, // height, width 97 VIDEO_PALETTE_RGB24 // palette 81 98 }; 82 99 … … 95 112 } 96 113 97 Camera::Camera(PixClient &client) : fd(-1), iBufferSize(0), fClient(client), fMutex(), fCond(&fMutex) 98 { 99 cout << "Starting thread..." << flush; 100 //pthread_cond_init(&fCond, NULL); 101 //pthread_mutex_init(&fMux, NULL); 102 //pthread_mutex_lock(&fMux); 103 fMutex.Lock(); 104 pthread_create(&fThread, NULL, MapThread, this); 105 cout << "done." << endl; 106 107 cout << "/dev/video: opening..." << flush; 114 Camera::Camera(PixClient &client, Int_t nch) : fd(-1), iBufferSize(0), fClient(client), fCond(), fMutex(fCond.GetMutex()) 115 { 116 cout << "Camera: " << this << " /dev/video: opening..." << flush; 108 117 109 118 // … … 126 135 // get input channel 0 information 127 136 // 137 128 138 struct video_channel ch; 129 ch.channel = 0;139 ch.channel = nch; 130 140 Ioctl(VIDIOCGCHAN, &ch); 131 141 … … 160 170 cout << "Buffer Size: " << (void*)iBufferSize << endl; 161 171 cout << "grab: use: 768x576 24 bit TrueColor (LE: bgr) = " << (void*)(768*576*3) << "b" << endl; 172 173 // if (fMutex->UnLock()==13) 174 // cout << "Camera::Camera - tried to unlock mutex locked by other thread." << endl; 175 176 cout << "Starting thread..." << flush; 177 //pthread_cond_init(&fCond, NULL); 178 //pthread_mutex_init(&fMux, NULL); 179 //pthread_mutex_lock(&fMux); 180 // if (fMutex->Lock()==13) 181 // cout << "Camera::Camera - mutex is already locked by this thread" << endl; 182 pthread_create(&fThread, NULL, MapThread, this); 183 cout << "done." << endl; 184 162 185 } 163 186 164 187 Camera::~Camera() 165 188 { 166 cout << "Stopping thread..." << flush;167 189 pthread_cancel(fThread); 168 //pthread_mutex_destroy(&fMux); 169 //pthread_cond_destroy(&fCond); 190 191 cout << "/dev/video: closing... " << flush; 192 if (fd != -1) 193 { 194 close(fd); 195 fd = -1; 196 } 170 197 cout << "done." << endl; 171 172 cout << "/dev/video: closing... " << flush;173 198 174 199 // unmap device memory 175 200 if ((int)pMapBuffer != -1) 176 201 munmap(pMapBuffer, iBufferSize); 177 178 if (fd != -1) 179 { 180 close(fd); 181 fd = -1; 182 } 183 184 cout << " Done." << endl; 185 } 186 202 } 187 203 188 204 void *Camera::MapThread(void *arg) … … 198 214 } 199 215 216 void Camera::ExitLoop() 217 { 218 // cout << "ExitLoop..." << endl; 219 fStop = 1; 220 while (IsRunning()) 221 usleep(1); 222 // cout << "Loop exited." << endl; 223 } 224 225 // 226 // flag if the execution is running or not 227 // 228 int Camera::IsRunning() const 229 { 230 const Int_t rc = fMutex->TryLock(); 231 if (rc==0) 232 return false; 233 234 if (rc==13) 235 return false; 236 237 fMutex->UnLock(); 238 return true; 239 } 240 200 241 void Camera::LoopStep(const unsigned long n) 201 242 { … … 217 258 { 218 259 #define IsOdd(i) (2*(i/2)!=i) 260 cout << "Camera::Thread started..." << endl; 219 261 while (1) 220 262 { 221 // pthread_cond_wait(&fCond, &fMux);263 //cout << "Wait..." << flush; 222 264 fCond.Wait(); 265 //cout << "done." << endl; 266 if (fd==-1) 267 break; 223 268 224 269 MStopwatch t; … … 259 304 t.Stop(); 260 305 t.Print(i); 261 262 fRunning = 0; 263 } 306 } 307 308 fMutex->UnLock(); 309 310 cout << "Camera::Thread.. stopped." << endl; 264 311 } 265 312 … … 272 319 } 273 320 274 // 275 // Stop running loop 276 // 277 fStop = 1; 278 279 // 280 // Wait until loop is stopped (pthread_cond_wait is executing) 281 // set new number of frames to process 282 // 283 fMutex.Lock(); 284 //pthread_mutex_lock(&fMux); 285 fNum = nof; 286 fStop = 0; 287 fRunning = 1; 288 fMutex.UnLock(); 289 //pthread_mutex_unlock(&fMux); 321 cout << "Loop..." << endl; 322 ExitLoop(); 323 324 fNum = nof; 325 fStop = 0; 290 326 291 327 // … … 293 329 // 294 330 fCond.Broadcast(); 295 //pthread_cond_signal(&fCond);296 331 } 297 332
Note:
See TracChangeset
for help on using the changeset viewer.