/* main.c --------- Co. E.Benninghaus Pfaffenhoffentstr. 1 78166 Donaueschingen created: 08.09.2000 last Work: 08.09.2000 */ #ifndef __main_cxx #define __main_cxx #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include int run = 0; int img = 0; pthread_t wthread = NULL; pthread_t ethread = NULL; #include "elcam.h" #include "config.c" #include "calc.c" #include "filter.c" #include "cam.c" #include "ecif.c" #ifdef VIEW #include "win.cxx" #include #include #include "callback.cxx" #endif typedef void ( *sighandler_t)(int); sighandler_t sighandler_old = SIG_ERR; void sigterminate(int sig) { if(sig != SIGTERM)return; if(sighandler_old != SIG_ERR){ signal(SIGTERM,sighandler_old); sighandler_old = SIG_ERR; } endall(); exit(0); } void sigrestore(void) { if(sighandler_old == SIG_ERR)return; signal(SIGTERM,sighandler_old); sighandler_old = SIG_ERR; } int sigset(void) { sigrestore(); sighandler_old = signal(SIGTERM,sigterminate); if(sighandler_old == SIG_ERR)return(-1); return(0); } FILE * fifoout = NULL; void DoFifoOut( int ok ) { int azdir,altdir,az,alt; if(cfg.test){ if(ok){ fprintf(stdout,"ok\n"); } else { fprintf(stdout,"%d:%d:%d:%d\n",cv.left,cv.right,cv.up,cv.down); } return; } fifoout = fopen(cfg.fifofile,"wt"); if(fifoout == NULL){ fprintf(stderr,"Cannot open fifooutfile: %s\n",cfg.fifofile); return; } if(ok){ fprintf(fifoout,"ok\n"); } else { fprintf(fifoout,"%d:%d:%d:%d\n",cv.left,cv.right,cv.up,cv.down); } fclose(fifoout); fifoout = NULL; } int correctactive = 0; int threaded_work_busy = 0; int threaded_grab_busy = 0; void endall( void ) { if(fifoout != NULL){ fclose(fifoout); } run = 0; while( threaded_work_busy || threaded_grab_busy ){ sleep(1); } elcamtcpserverstop(); close_video(); freeBuffers(); sigrestore(); #ifdef VIEW if(cfgwin != NULL){ cfgwin->~Fl_Window(); } if(mainwin != NULL){ mainwin->~Fl_Window(); } #else exit(0); #endif } int __prevShowCol = 0; void ShowFlag( int col ) { if(col == __prevShowCol){ return; } __prevShowCol = col; #ifdef VIEW mainwin->make_current(); #endif switch(col){ #ifdef VIEW case 0: fl_rectf(5, 150, 160, 15, 0,0,0); break; case 1: fl_rectf(5, 150, 160, 15, 0,255,0); break; case 2: fl_rectf(5, 150, 160, 15, 0,0,255); break; case 3: fl_rectf(5, 150, 160, 15, 255,0,0); break; #else case 0: break; case 1: break; case 2: break; case 3: break; #endif default: break; } } long coverinterval = 0; int correctresult = 0; void threaded_work(char * data) { int ct,x,y,fifoflag; threaded_work_busy = 1; fifoflag = 0; while( run ){ if( img && prepare_image() ){ /*-- start of calculations -----*/ if( count <= cfg.calcstart ){ /* prepare also the first picture, which can then be compared with the following*/ filterfirst( v_firstbuf ); #ifdef VIEW txt_mincol->value( fv.mincol ); txt_maxcol->value( fv.maxcol ); #endif if( calcfirst( v_firstbuf) ){ ; } else { ShowFlag(3); /* no valid params found */ ; } } else { filter( v_imagebuf ); if( ( correctresult = calc( v_imagebuf ) ) > 0 ) { /* and here we do the stuff of calculations results*/ if(correctresult == 1 ){ /* all is ok, and picture on correct position */ if(! correctactive){ correctactive = 1; } ShowFlag(1); if( fifoflag && cfg.control ){ DoFifoOut( 1 ); fifoflag = 0; } if( cfg.snap && run ){ /*get greater picture, and save it on correct position*/ snap_image(); } } else { /* picture is no more in correct position */ ShowFlag(2); if(cfg.control){ if( (cfg.fifo) && ( correctactive) ){ if( fifoflag && cv.changed ){ fifoflag = 0; } if(! fifoflag){ DoFifoOut( 0 ); fifoflag = 1; } } } } if(cfg.showcross && cfg.view){ if(cfg.strategy == 0){ if(cfg.savebuf){ drawCross( v_savebuf, cv.spot[0].x, cv.spot[0].y, 10 , 180 ); } else { drawCross( v_imagebuf, cv.spot[0].x, cv.spot[0].y, 10 , 180 ); } } else if(cfg.strategy == 1){ ct = 0; while( ( ct < cfg.maxcalckoor ) && ( cv.k[ct].x >= 0 ) && ( cv.k[ct].y >= 0 ) ) { x = cv.k[ct].x; y = cv.k[ct].y; if(cfg.savebuf){ drawCross( v_savebuf,x, y, 5 , 180 ); } else { drawCross( v_imagebuf, x, y, 5 , 180 ); } ct++; } } } /* ----------------------------------------------- */ } } /*--- end of calculations ------*/ img = 0; #ifdef VIEW if(( mainwin != NULL) && (cfg.view) ){ mainwin->make_current(); if(cfg.savebuf){ fl_draw_image((const uchar *)v_savebuf,5,25, WIDTH ,HEIGHT,1,0); } else { fl_draw_image((const uchar *)v_imagebuf,5,25,WIDTH ,HEIGHT,1,0); } } #endif if( ( cfg.strategy == 2 ) && ( cfg.coverinterval > 0 ) && (count > cfg.calcstart ) ){ coverinterval++; if(coverinterval > cfg.coverinterval){ coverinterval = 0; correctactive = 0; count = 0; } } } } threaded_work_busy = 0; img = 0; } void threaded_grabbing(char * data) { ShowFlag(0); if(! createBuffers()){ return; } correctactive = 0; coverinterval = 0; run = 1; img = 0; count = 0; wthread = NULL; if(pthread_create(&wthread, NULL, (void *)&threaded_work, NULL) != 0 ){ fprintf(stderr,"\nCannot create working thread\n"); run = 0; freeBuffers(); return; } threaded_grab_busy = 1; while(run){ if(( ! img ) && ( get_video_image() )){ img = 1; } } correctactive = 0; freeBuffers(); run = 0; img = 0; threaded_grab_busy = 0; return; } int main(int argc, char * argv[]) { int arg; char *p; if(! parsecommandline( argc, argv )){ exit(0); } ecif.initport = cfg.tcpport; if( sigset() < 0){ fprintf(stderr,"Cannot install signalhandler\n"); exit(-1); } if(! open_video()){ fprintf(stderr,"Cannot open video\n"); sigrestore(); exit(0); } #ifdef VIEW if(cfg.ecif){ if( ! elcamtcpserverstart(1) ){ cfg.ecif = 0; fprintf(stderr,"Cannot start tcp-server\n"); } } make_window(); if(cfg.savebuf){ btn_realview->value(1); } else { btn_realview->value(0); } if( cfg.snap ){ btn_snap->value(1); } else { btn_snap->value(0); } if( cfg.view ){ btn_view->value(1); } else { btn_view->value(0); } if( cfg.control && cfg.fifo ){ btn_control->value(1); } else { btn_control->value(0); } ch_strategy->add("Spot"); ch_strategy->add("Spread"); ch_strategy->add("Cover"); ch_strategy->value(cfg.strategy); ch_koortype->add("Rect"); ch_koortype->add("Cross"); ch_koortype->value(cfg.koortype); ch_snapsize->add("160x120 QSIF"); ch_snapsize->add("176x144 QCIF"); ch_snapsize->add("320x240 SIF"); ch_snapsize->add("352x288 CIF"); ch_snapsize->add("640x480 VGA"); ch_snapsize->value(cfg.snapsize); mainwin->end(); Fl::visual(FL_RGB); mainwin->show(); return Fl::run(); #else if(cfg.ecif){ if( ! elcamtcpserverstart(0) ){ cfg.ecif = 0; fprintf(stderr,"Cannot start tcp-server\n"); } } #endif endall(); } #endif /*__main_cxx*/