loop_alert( char * txt ) { monitor_alert(txt); } void waitTillLoopFree(void) { while( ( prog.loopbusy ) || ( prog.event != EVT_NOTHING ) ){ ; } } void doLoopEvent( unsigned char event ) { prog.event = event; while( prog.loopbusy || ( prog.event != EVT_NOTHING ) ){ ; } } int loop ( void ) { unsigned char event; AZALT azalt; RADEC radec; char *p; if( prog.looprun ) { return(0); } if( if_StartConnection() == 0){ if_StopConnection(); return(0); } prog.looprun = 1; if(prog.debug){ guide.updatecounter = 0; guide.updateseconds = 0; } strcpy(prog.eventstr,"none"); while( prog.looprun && prog.connection ) { event = EVT_NOTHING; prog.loopbusy = 0; if( prog.keypad ) { event = read_keypad( prog.keypad ); if( ( event != EVT_NOTHING ) && ( xephem.keypadspecial) ) { if( (prog.motorrun) && ( event != IFEVT_STOPMOTORS ) ){ event = PREVT_SENDAZALTKOORSTOXEPHEM; } if( (! prog.motorrun ) && ( event == IFEVT_STOPMOTORS ) ){ event = PREVT_SENDAZALTKOORSTOXEPHEM; } } } if( event == EVT_NOTHING ) { event = prog.event; if(event != EVT_NOTHING){ /* don't change this order */ prog.loopbusy = 1; prog.event = EVT_NOTHING; } } if( event == EVT_NOTHING ) { if(if_Guide() == 0){ break; } if( prog.motorrun ) { if( ( p = if_GetCurrentAzKoor()) == NULL ) { prog.looprun = 0; break; } strcpy( prog.azstr,p ); if( ( p = if_GetCurrentAltKoor()) == NULL ) { prog.looprun = 0; break; } strcpy( prog.altstr,p ); } if(prog.debug && prog.guiderunning ){ guide.updatecounter++; } continue; } prog.loopbusy = 1; if(event < IFEVT_ENDVALUES ) { switch( event ) { case IFEVT_ENDALL: strcpy(prog.eventstr,"endall"); prog.looprun = 0; break; case IFEVT_STOPMOTORS: strcpy(prog.eventstr,"stop motors"); if( if_StopMotors() == 0 ) { prog.looprun = 0; } if( prog.debug && (guide.updateseconds > 0) ){ guide.updateseconds = time(NULL) - guide.updateseconds; } break; case IFEVT_GETKOORS: strcpy(prog.eventstr,"get koodinates from telescope"); if( ( p = if_GetCurrentAzKoor()) == NULL ) { prog.looprun = 0; break; } strcpy( prog.azstr,p ); if( ( p = if_GetCurrentAltKoor()) == NULL ) { prog.looprun = 0; break; } strcpy( prog.altstr,p ); break; case IFEVT_STARTGUIDE: if(prog.debug){ guide.updatecounter = 0; guide.updateseconds = time(NULL); } strcpy(prog.eventstr,"start guiding"); if( if_GuideInitialisation() == 0){ prog.looprun = 0; break; } xephemfifooutloopstart(); break; case IFEVT_SLOWLEFT: strcpy(prog.eventstr,"correction left"); if( if_GoLeft_Slow() == 0 ) { prog.looprun = 0; break; } xephemfifooutloopstart(); break; case IFEVT_SLOWRIGHT: strcpy(prog.eventstr,"correction right"); if( if_GoRight_Slow( ) == 0 ) { prog.looprun = 0; break; } xephemfifooutloopstart(); break; case IFEVT_SLOWDOWN: strcpy(prog.eventstr,"correction down"); if( if_GoDown_Slow() == 0 ) { prog.looprun = 0; break; } xephemfifooutloopstart(); break; case IFEVT_SLOWUP: strcpy(prog.eventstr,"correction up"); if( if_GoUp_Slow() == 0 ) { prog.looprun = 0; break; } xephemfifooutloopstart(); break; case IFEVT_FASTLEFT: strcpy(prog.eventstr,"move left fast"); if( if_GoLeft_Fast() == 0 ) { prog.looprun = 0; break; } xephemfifooutloopstart(); break; case IFEVT_FASTRIGHT: strcpy(prog.eventstr,"move right fast"); if( if_GoRight_Fast() == 0 ) { prog.looprun = 0; break; } xephemfifooutloopstart(); break; case IFEVT_FASTDOWN: strcpy(prog.eventstr,"move down fast"); if( if_GoDown_Fast() == 0 ) { prog.looprun = 0; break; } xephemfifooutloopstart(); break; case IFEVT_FASTUP: strcpy(prog.eventstr,"move up fast"); if( if_GoUp_Fast() == 0 ) { prog.looprun = 0; break; } xephemfifooutloopstart(); break; case IFEVT_GOTORADECKOORS: if(! prog.objmark){ break; } strcpy(prog.eventstr,"telescope - goto converted ra-dec koors"); StrRADecToStrAZAlt( prog.rastr, prog.decstr, scope.utcadjust, scope.latitude, scope.longitude, &azalt ); p = DoubleToStrDegrees( azalt.az * 3600.0, 0 ); if( p == NULL){ prog.looprun = 0; break; } strcpy( prog.azstr, p ); p = DoubleToStrDegrees( azalt.alt * 3600.0, 0 ); if( p == NULL){ prog.looprun = 0; break; } strcpy( prog.altstr, p ); if( if_AltAzGoto( prog.azstr, prog.altstr ) == 0){ prog.looprun = 0; break; } xephemfifooutloopstart(); break; case IFEVT_ORIGINRADECKOORS: if(!prog.objmark){ break; } strcpy(prog.eventstr,"teleskope - init with converted ra-dec koors"); StrRADecToStrAZAlt( prog.rastr, prog.decstr, scope.utcadjust, scope.latitude, scope.longitude, &azalt ); p = DoubleToStrDegrees( azalt.az * 3600.0, 0 ); if( p == NULL){ prog.looprun = 0; break; } strcpy( prog.azstr, p ); p = DoubleToStrDegrees( azalt.alt * 3600.0, 0 ); if( p == NULL){ prog.looprun = 0; break; } strcpy( prog.altstr, p ); if( if_AltAzOrigin( prog.azstr, prog.altstr ) == 0){ prog.looprun = 0; } break; default: break; } continue; } if(event >= PREVT_STARTVALUES ) { switch( event ) { case PREVT_MARKRADECKOORS: prog.objmark = 1; break; case PREVT_ORIGINAZALTKOORS: strcpy(prog.eventstr,"teleskope - init with az-alt koors"); if( if_AltAzOrigin( prog.azstr, prog.altstr ) == 0){ prog.looprun = 0; } break; case PREVT_GOTOAZALTKOORS: strcpy(prog.eventstr,"telescope - goto az-alt koors"); if( if_AltAzGoto( prog.azstr, prog.altstr ) == 0){ prog.looprun = 0; break; } xephemfifooutloopstart(); break; case PREVT_SENDAZALTKOORSTOXEPHEM: strcpy(prog.eventstr,"sending current AZ/ALT koordinates to xephem"); if( ( p = if_GetCurrentAzKoor()) == NULL ) { prog.looprun = 0; break; } strcpy( prog.azstr,p ); if( ( p = if_GetCurrentAltKoor()) == NULL ) { prog.looprun = 0; break; } strcpy( prog.altstr,p ); StrAZAltToStrRADec( prog.azstr, prog.altstr, scope.utcadjust, scope.latitude, scope.longitude, &radec ); p = DoubleToStrDegrees( radec.ra * 3600.0 * 15.0, 1 ); if( p == NULL){ prog.looprun = 0; break; } strcpy( prog.rastr, p ); p = DoubleToStrDegrees( radec.dec * 3600.0, 0 ); if( p == NULL){ prog.looprun = 0; break; } strcpy( prog.decstr, p ); sendCurrentKoorsToXephem(); break; case PREVT_SENDRADECKOORSTOXEPHEM: strcpy(prog.eventstr,"sending current RA/DEC koordinates to xephem"); sendCurrentKoorsToXephem(); break; case PREVT_SWITCHOFFVALIDCALCREFRACTION: strcpy(prog.eventstr,"Switch off calculation of refraction"); scope.calcrefraction = 0; break; case PREVT_SWITCHONVALIDCALCREFRACTION: if(cRfValues != NULL ){ scope.calcrefraction = 1; strcpy(prog.eventstr,"Switch on calculation of refraction"); } else { strcpy(prog.eventstr,"Cannot use refraction, because no startuplist"); } break; case PREVT_JOYSTICKKEYPADASPECIALON: if( !prog.keypad ){ strcpy(prog.eventstr,"Cannot init keypadspecial, because keypad not init"); break; } if( xephem.start && xephem.fifooutloop){ xephem.fifooutloopbusy = 0; xephem.fifooutloop = 0; strcpy(prog.eventstr,"Switch off fifooutloop, and init keypadspecial"); } else { strcpy(prog.eventstr,"Switch on keypadspecial"); } xephem.keypadspecial = 1; break; case PREVT_JOYSTICKKEYPADASPECIALOFF: if( !prog.keypad ){ strcpy(prog.eventstr,"Keypad is not init"); break; } strcpy(prog.eventstr,"Switch off keypadspecial"); xephem.keypadspecial = 0; break; case PREVT_XEPHEMOUTLOOPON: if( (! xephem.start) || (! xephem.fifooutloopsave ) ){ strcpy(prog.eventstr,"xephemfifo, outloop is not init at startup"); break; } if( xephem.fifooutloopbusy ){ strcpy(prog.eventstr,"xephemfifo-outloop is active"); break; } if( xephem.keypadspecial ){ strcpy(prog.eventstr,"Switch off keypadspecial, and init xe-fifoout"); xephem.keypadspecial = 0; xephem.fifooutloop = xephem.fifooutloopsave; } else { strcpy(prog.eventstr,"xephemfifo-outloop is init"); xephem.fifooutloop = xephem.fifooutloopsave; } break; case PREVT_XEPHEMOUTLOOPOFF: if( (! xephem.start) || (! xephem.fifooutloopsave ) ){ strcpy(prog.eventstr,"xephemfifo, outloop is not init at startup"); break; } strcpy(prog.eventstr,"switch off xephem-fifooutloop"); xephem.fifooutloopbusy = 0; xephem.fifooutloop = 0; break; case PREVT_INITFIELDROTATOR: strcpy(prog.eventstr,"Init fieldrotator for guiding"); scope.fruse = 1; break; case PREVT_STOPFIELDROTATOR: strcpy(prog.eventstr,"Switch off fieldrotator"); scope.fruse = 0; break; case PREVT_GETFRKOOR: strcpy(prog.eventstr,"Getting fr-koordinate"); if( ( p = if_GetCurrentFrKoor()) == NULL ) { prog.looprun = 0; break; } strcpy( prog.frstr,p ); break; case PREVT_FRTURNLEFT: strcpy(prog.eventstr,"fr turn left"); if( if_FrTurnLeft() == 0 ) { prog.looprun = 0; } break; case PREVT_FRTURNRIGHT: strcpy(prog.eventstr,"fr turn right"); if( if_FrTurnRight() == 0 ) { prog.looprun = 0; } break; case PREVT_TOGGLEEXFIFOFOLLOW: if(exfifo.fiforun){ if(exfifo.follow){ strcpy(prog.eventstr,"exfifo-follow off"); exfifo.follow = 0; } else { strcpy(prog.eventstr,"exfifo-follow on"); exfifo.follow = 1; } } break; case PREVT_TOGGLEENCODERS: if(encoder.type != NOENCODER){ if( encoder.active){ strcpy(prog.eventstr,"encoder off"); encoder.active = 0; } else { strcpy(prog.eventstr,"encoder on"); encoder.active = 1; } } break; default: break; } } } if_StopConnection(); return( prog.looprun ); }