Ignore:
Timestamp:
05/15/04 16:46:27 (20 years ago)
Author:
tbretz
Message:
*** empty log message ***
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/MagicSoft/Cosy/videodev/Camera.cc

    r2518 r4076  
    1717inline int Camera::Ioctl(int req, void *opt, const char *str)
    1818{
    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 << " - ";
    2436        cout << (str?str:strerror(errno)) << " (rc=" << rc << ")" << endl;
    25     }
    26 
    27     return rc;
     37        return rc;
     38    }
     39    return -1;
    2840}
    2941
    3042void Camera::Error(const char *str, int fatal)
    3143{
    32     cout << endl
    33         << (fatal?"Fatal ":"") << "Error! " << str << ": " << strerror(errno)
    34         << endl;
     44    cout << endl;
     45    cout << (fatal?"Fatal ":"") << "Error! " << str << ": " << strerror(errno);
     46    cout << endl;
    3547
    3648    if (fatal)
     
    3951void Camera::SigInit()
    4052{
    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     */
    5265}
    5366
     
    7487int Camera::StartGrab(unsigned int frame)
    7588{
     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.
    7693    static struct video_mmap gb =
    7794    {
    78         0,                     // frame
    79         rows, cols,            // height, width
    80         VIDEO_PALETTE_RGB24    // palette
     95        0,                  // frame
     96        rows, cols,         // height, width
     97        VIDEO_PALETTE_RGB24 // palette
    8198    };
    8299
     
    95112}
    96113
    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;
     114Camera::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;
    108117
    109118    //
     
    126135    // get input channel 0 information
    127136    //
     137
    128138    struct video_channel ch;
    129     ch.channel = 0;
     139    ch.channel = nch;
    130140    Ioctl(VIDIOCGCHAN, &ch);
    131141
     
    160170    cout << "Buffer Size: " << (void*)iBufferSize << endl;
    161171    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
    162185}
    163186
    164187Camera::~Camera()
    165188{
    166     cout << "Stopping thread..." << flush;
    167189    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    }
    170197    cout << "done." << endl;
    171 
    172     cout << "/dev/video: closing... " << flush;
    173198
    174199    // unmap device memory
    175200    if ((int)pMapBuffer != -1)
    176201        munmap(pMapBuffer, iBufferSize);
    177 
    178     if (fd != -1)
    179     {
    180         close(fd);
    181         fd = -1;
    182     }
    183 
    184     cout << " Done." << endl;
    185 }
    186 
     202}
    187203
    188204void *Camera::MapThread(void *arg)
     
    198214}
    199215
     216void 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//
     228int 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
    200241void Camera::LoopStep(const unsigned long n)
    201242{
     
    217258{
    218259#define IsOdd(i) (2*(i/2)!=i)
     260    cout << "Camera::Thread started..." << endl;
    219261    while (1)
    220262    {
    221         //pthread_cond_wait(&fCond, &fMux);
     263        //cout << "Wait..." << flush;
    222264        fCond.Wait();
     265        //cout << "done." << endl;
     266        if (fd==-1)
     267            break;
    223268
    224269        MStopwatch t;
     
    259304        t.Stop();
    260305        t.Print(i);
    261 
    262         fRunning = 0;
    263     }
     306    }
     307
     308    fMutex->UnLock();
     309
     310    cout << "Camera::Thread.. stopped." << endl;
    264311}
    265312
     
    272319    }
    273320
    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;
    290326
    291327    //
     
    293329    //
    294330    fCond.Broadcast();
    295     //pthread_cond_signal(&fCond);
    296331}
    297332
Note: See TracChangeset for help on using the changeset viewer.