/* Eldob 1.0 * * This code is copyright Elmar Benninghaus 2003 * under the terms of the GNU Public License. * * by Elmar Benninghaus * el@eb-themen.de * June 2003 */ #pragma pack(2) #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "MathLib.c" #include "eldob.h" #include "pdouble.c" #include "strtod.c" // Define the minimum OS version we support #define ourMinVersion sysMakeROMVersion(2,0,3,sysROMStageRelease,0) #define eldobAppID 'ElDb' // 0x74586858 in hexadecimal #define eldobDBType 'Data' // 0x64415441 in hexadecimal /*************** * subfiles ***************/ #include "variables.c" #include "calc.c" #include "convert.c" #include "config.c" #include "objects.c" #include "guideutil.c" #include "serial.c" #include "interface.c" /*********************************************************************** * Prototypes for internal functions **********************************************************************/ static int StartApplication(void); static void StopApplication(void); static void EventLoop(void); static Boolean MainMenuEventHandler(EventPtr event); static Err RomVersionCompatible(DWord requiredVersion, Word launchFlags); static Boolean OpenDatabase(void); static Err RomVersionCompatible(DWord requiredVersion, Word launchFlags) { DWord romVersion; // See if we're on in minimum required version of the ROM or later. FtrGet(sysFtrCreator, sysFtrNumROMVersion, &romVersion); if (romVersion < requiredVersion){ FrmAlert(alertID_incompatiblerom); return (sysErrRomIncompatible); } return (0); } /* **************************************************************************** ***** ***** ***** Pilot Main ***** ***** ***** **************************************************************************** */ DWord PilotMain (Word cmd, Ptr cmdPBP, Word launchFlags) { int error; error = RomVersionCompatible (ourMinVersion, launchFlags); if (error) return (error); if (cmd == sysAppLaunchCmdNormalLaunch) { error = StartApplication(); if (error) return error; EventLoop(); StopApplication(); } return 0; } static int StartApplication(void) { Err error; error = SysLibFind(MathLibName, &MathLibRef); if (error) error = SysLibLoad(LibType, MathLibCreator, &MathLibRef); ErrFatalDisplayIf(error, "Can't find MathLib"); error = MathLibOpen(MathLibRef, MathLibVersion); ErrFatalDisplayIf(error, "Can't open MathLib"); if(! eldobDB){ error = OpenDatabase(); if (error) return error; } // FrmGotoForm(formID_MainMenu); // first we pop up the configvalue, so after it is closed, // main menu is used, and the database is read in. FrmGotoForm(formID_Config); } static void StopApplication( void ) { Err error; UInt usecount; FldSetTextHandle(fieldptr_AzKoor, NULL); FldSetTextHandle(fieldptr_AltKoor, NULL); FldSetTextHandle(fieldptr_RaKoor, NULL); FldSetTextHandle(fieldptr_DecKoor, NULL); error = MathLibClose(MathLibRef, &usecount); ErrFatalDisplayIf(error, "Can't close MathLib"); if (usecount == 0)SysLibRemove(MathLibRef); DmReleaseRecord(eldobDB,26,true); DmReleaseRecord(eldobDB,27,true); DmReleaseRecord(eldobDB,28,true); DmReleaseRecord(eldobDB,29,true); if(eldobDB){ DmCloseDatabase(eldobDB); } } static void EventLoop(void) { short err; int formID; FormPtr form; EventType event; do { EvtGetEvent(&event, 0); if(boardconnection ) { if_trigger_Guide(); if( event.eType == keyDownEvent ) { if( event.data.keyDown.chr == hardPowerChr ) { // pilot - on/off key - ( during connection = switch for keystrokes) - pressed /// if(guiderunning){ guiderunning = 0; continue; } if(scope.motorrun){ if_trigger_StopAll(); } continue; } else if( event.data.keyDown.chr == hard1Chr ) { // pilot - hard key 1 - ( during connection = left slow) - pressed /// if_trigger_GoLeft_Slow(); continue; } else if( event.data.keyDown.chr == hard2Chr ) { // pilot - hard key 2 - ( during connection = up slow ) - pressed /// if_trigger_GoUp_Slow(); continue; } else if( event.data.keyDown.chr == hard3Chr ) { // pilot - hard key 3 - ( during connection = down slow ) - pressed /// if_trigger_GoDown_Slow(); continue; } else if( event.data.keyDown.chr == hard4Chr ) { // pilot - hard key 4 - ( during connection = right slow ) - pressed /// if_trigger_GoRight_Slow(); continue; } else if( event.data.keyDown.chr == launchChr ) { // pilot - event Key launch (upper left graffitikey) - ( during connection = up fast) - pressed /// if_trigger_GoUp_Fast(); continue; } else if( event.data.keyDown.chr == menuChr ) { // pilot - event Key menu (lower left graffitikey) - ( during connection = left fast ) - pressed /// if_trigger_GoLeft_Fast(); continue; } else if( event.data.keyDown.chr == findChr ) { // pilot - event Key finc (lower right graffitikey) - ( during connection = right fast ) - pressed /// if_trigger_GoRight_Fast(); continue; } else if( event.data.keyDown.chr == calcChr ) { // pilot - event Key calc (upper right graffitikey) - ( during connection = down fast ) - pressed /// if_trigger_GoDown_Fast(); continue; } else if( event.data.keyDown.chr == autoOffChr ) { continue; } else if( event.data.keyDown.chr == backlightChr ) { continue; } if( event.data.keyDown.chr == pageDownChr ) { // pilot - hard key pageDown - ( during connection = ?? ) - pressed /// if_trigger_GuideInitialisation(); continue; } else if( event.data.keyDown.chr == pageUpChr ) { // pilot - hard key pageUp - ( during connection = ?? ) - pressed /// StrCopy(&text_azkoor[0],(const char *)if_trigger_GetCurrentAzKoor()); StrCopy(&text_altkoor[0],(const char *)if_trigger_GetCurrentAltKoor()); //WinDrawChars(&text_emptydegree[0],16,20,131); //WinDrawChars(&text_emptydegree[0],16,100,131); // for testing /* StrIToA(&text_azkoor[0], debugticks1); StrIToA(&text_altkoor[0],debugticks3); */ /* printDouble( guidevals.nextazhs, &text_azkoor[0]); printDouble( guideAzhs, &text_altkoor[0]); */ WinDrawChars(&text_azkoor[0],StrLen(text_azkoor),20,131); WinDrawChars(&text_altkoor[0],StrLen(text_altkoor),100,131); /* WinDrawChars(&text_azkoor[0],StrLen(text_azkoor),20,119); WinDrawChars(&text_altkoor[0],StrLen(text_altkoor),100,131); */ continue; } } } if (SysHandleEvent(&event))continue; if (MenuHandleEvent((void *)0, &event, &err)) continue; if (event.eType == frmLoadEvent) { formID = event.data.frmLoad.formID; form = FrmInitForm(formID); FrmSetActiveForm(form); switch (formID) { case formID_MainMenu: FrmSetEventHandler(form, MainMenuEventHandler); break; case formID_Config: FrmSetEventHandler(form, ConfigEventHandler); break; case formID_Objects: FrmSetEventHandler(form, ObjectsEventHandler); break; } } FrmDispatchEvent(&event); } while(event.eType != appStopEvent); } static Boolean MainMenuEventHandler(EventPtr event) { FormPtr frm; FieldPtr fptr; Err error; VoidHand RecHandle; Ptr RecPointer; switch (event->eType) { case frmOpenEvent: WinEraseWindow(); frm = FrmGetActiveForm(); fieldptr_AzKoor = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, fieldID_AzKoor)); fieldptr_AltKoor = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, fieldID_AltKoor)); fieldptr_RaKoor = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, fieldID_RaKoor)); fieldptr_DecKoor = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, fieldID_DecKoor)); FldSetTextHandle (fieldptr_AzKoor, (Handle) DmGetRecord(eldobDB, 26)); FldSetTextHandle (fieldptr_AltKoor, (Handle) DmGetRecord(eldobDB, 27)); FldSetTextHandle (fieldptr_RaKoor, (Handle) DmGetRecord(eldobDB, 28)); FldSetTextHandle (fieldptr_DecKoor, (Handle) DmGetRecord(eldobDB, 29)); FrmDrawForm (frm); if(microStepGuide){ WinDrawChars(text_on,3,135,116); } else { WinDrawChars(text_off,3,135,116); } if(boardconnection){ WinDrawChars(text_connect, StrLen(text_connect), 60, 146); } else { WinDrawChars(text_noconnect, StrLen(text_noconnect), 60, 146); } break; case ctlSelectEvent: switch( event->data.ctlEnter.controlID ){ case buttonID_ConfigStart: if(!boardconnection){ FldSetTextHandle(fieldptr_AzKoor, NULL); FldSetTextHandle(fieldptr_AltKoor, NULL); FldSetTextHandle(fieldptr_RaKoor, NULL); FldSetTextHandle(fieldptr_DecKoor, NULL); DmReleaseRecord(eldobDB,26,true); DmReleaseRecord(eldobDB,27,true); DmReleaseRecord(eldobDB,28,true); DmReleaseRecord(eldobDB,29,true); FrmGotoForm( formID_Config ); } return true; case buttonID_microStepGuide: if(microStepGuide){ microStepGuide = 0; WinDrawChars(text_off,3,135,116); } else { microStepGuide = 1; WinDrawChars(text_on,3,135,116); } return true; case buttonID_ConnectionOn: if( FrmAlert( alertID_confirm ) != 1 ) { return(true); } if(if_trigger_InitBoardConnection() == true){ WinDrawChars(text_connect, StrLen(text_connect), 60, 146); } return true; case buttonID_ConnectionOff: if(if_trigger_StopBoardConnection() == true){ WinDrawChars(text_noconnect, StrLen(text_noconnect), 60, 146); } return true; case buttonID_RaDecConvert: frm = FrmGetActiveForm(); StrRADecToStrAZAlt( FldGetTextPtr(fieldptr_RaKoor), FldGetTextPtr(fieldptr_DecKoor) ); StrCopy(&text_azkoor[0],(const char *)DoubleToStrDegrees(azimuth * 3600, false)); StrCopy(&text_altkoor[0],(const char *)DoubleToStrDegrees(altitude * 3600, false)); FldSetTextHandle(fieldptr_AzKoor, NULL); FldSetTextHandle(fieldptr_AltKoor, NULL); DmReleaseRecord(eldobDB,26,true); DmReleaseRecord(eldobDB,27,true); RecHandle = DmGetRecord(eldobDB,26); RecPointer = MemHandleLock(RecHandle); DmWrite(RecPointer, 0, &text_azkoor[0], 10); MemPtrUnlock(RecPointer); DmReleaseRecord(eldobDB, 26, true); RecHandle = DmGetRecord(eldobDB,27); RecPointer = MemHandleLock(RecHandle); DmWrite(RecPointer, 0, &text_altkoor[0], 10); MemPtrUnlock(RecPointer); DmReleaseRecord(eldobDB, 27, true); fieldptr_AzKoor = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, fieldID_AzKoor)); fieldptr_AltKoor = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, fieldID_AltKoor)); FldSetTextHandle (fieldptr_AzKoor, (Handle) DmGetRecord(eldobDB, 26)); FldSetTextHandle (fieldptr_AltKoor, (Handle) DmGetRecord(eldobDB, 27)); FldDrawField(fieldptr_AzKoor); FldDrawField(fieldptr_AltKoor); return true; case buttonID_AltAzConvert: frm = FrmGetActiveForm(); StrAZAltToStrRADec( FldGetTextPtr(fieldptr_AzKoor), FldGetTextPtr(fieldptr_AltKoor) ); StrCopy(&text_rakoor[0],(const char *)DoubleToStrDegrees(rectaszension * 15 * 3600, true)); StrCopy(&text_deckoor[0],(const char *)DoubleToStrDegrees(declination * 3600, false)); FldSetTextHandle(fieldptr_RaKoor, NULL); FldSetTextHandle(fieldptr_DecKoor, NULL); DmReleaseRecord(eldobDB,28,true); DmReleaseRecord(eldobDB,29,true); RecHandle = DmGetRecord(eldobDB,28); RecPointer = MemHandleLock(RecHandle); DmWrite(RecPointer, 0, &text_rakoor[0], 10); MemPtrUnlock(RecPointer); DmReleaseRecord(eldobDB, 28, true); RecHandle = DmGetRecord(eldobDB,29); RecPointer = MemHandleLock(RecHandle); DmWrite(RecPointer, 0, &text_deckoor[0], 10); MemPtrUnlock(RecPointer); DmReleaseRecord(eldobDB, 29, true); fieldptr_RaKoor = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, fieldID_RaKoor)); fieldptr_DecKoor = FrmGetObjectPtr(frm, FrmGetObjectIndex(frm, fieldID_DecKoor)); FldSetTextHandle (fieldptr_RaKoor, (Handle) DmGetRecord(eldobDB, 28)); FldSetTextHandle (fieldptr_DecKoor, (Handle) DmGetRecord(eldobDB, 29)); FldDrawField(fieldptr_RaKoor); FldDrawField(fieldptr_DecKoor); return true; case buttonID_AltAzGoto: if(! boardconnection){ FrmAlert( alertID_connectionstart ); return true; } if( FrmAlert( alertID_confirm ) != 1 ) { return(true); } if_trigger_AltAzGoto(FldGetTextPtr(fieldptr_AzKoor),FldGetTextPtr(fieldptr_AltKoor)); return true; case buttonID_RaDecObjects: FldSetTextHandle(fieldptr_AzKoor, NULL); FldSetTextHandle(fieldptr_AltKoor, NULL); FldSetTextHandle(fieldptr_RaKoor, NULL); FldSetTextHandle(fieldptr_DecKoor, NULL); DmReleaseRecord(eldobDB,26,true); DmReleaseRecord(eldobDB,27,true); DmReleaseRecord(eldobDB,28,true); DmReleaseRecord(eldobDB,29,true); FrmGotoForm( formID_Objects ); return true; case buttonID_AltAzOrigin: if(! boardconnection){ FrmAlert( alertID_connectionstart ); return true; } if( FrmAlert( alertID_confirm ) != 1 ) { return(true); } if_trigger_AltAzOrigin(FldGetTextPtr(fieldptr_AzKoor),FldGetTextPtr(fieldptr_AltKoor)); return true; default: break; } default: return false; } return false; } static Boolean OpenDatabase(void) { UInt index = 0; VoidHand RecHandle; Ptr RecPointer; eldobDB = DmOpenDatabaseByTypeCreator(eldobDBType, eldobAppID, dmModeReadWrite); if (!eldobDB) { if (DmCreateDatabase(0, eldobDBName, eldobAppID, eldobDBType, false)) return 1; eldobDB = DmOpenDatabaseByTypeCreator(eldobDBType, eldobAppID, dmModeReadWrite); index = 0; while( index < maxDBRecords ) { RecHandle = DmNewRecord(eldobDB, &index, 10); RecPointer = MemHandleLock(RecHandle); DmWrite(RecPointer, 0, &text_nullval, 10); MemPtrUnlock(RecPointer); DmReleaseRecord(eldobDB, index, true); index++; } } return 0; }