GPS4Palm

Source Code Documentation


ApproachForm.c

Go to the documentation of this file.
00001 /*****************************************************************************
00002  *
00003  * $RCSfile: ApproachForm_8c-source.html,v $
00004  *
00005  * GPS4Palm Approach Form
00006  *
00007  * This program is Copyright (C) 03/2003 Matthias Prinke
00008  * <matthias.prinke@surfeu.de> and covered by GNU's GPL.
00009  * In particular, this program is free software and comes WITHOUT
00010  * ANY WARRANTY.
00011  *
00012  * $Author: mp $
00013  *
00014  * $Date: 2007-10-08 20:40:32 $
00015  *
00016  * $Revision: 1.6.2.1 $
00017  *
00018  * $Log: ApproachForm_8c-source.html,v $
00018  * Revision 1.6.2.1  2007-10-08 20:40:32  mp
00018  * updated for gps4palm V0.9.5 beta
00018  *
00019  * Revision 1.2  2004-11-25 19:47:37  mp
00020  * updated descriptions
00021  *
00022  * Revision 1.1  2004/11/25 19:44:05  mp
00023  * initial version
00024  *
00025  *
00026  ****************************************************************************/
00027 #include <PalmOS.h>                     
00028 #include "ResourceDefines.h"
00029 #include "Serial.h"
00030 #include "ApproachForm.h"
00031 #include "Utils.h"
00032 #include "Data.h"
00033 #include "GPS.h"
00034 #include "fp.h"
00035 #include "geo.h"
00036 #include "common.h"
00037 
00038 /* Global Variables */
00039 extern PrefsType        gPrefs;                 /* Preferences data structure */
00040 extern DmOpenRef        gWaypointDB;            /* Wpt. Data Base reference */
00041 extern GPSType          gGPSData;               /* GPS Data */
00042 
00043 
00044 /* Static Functions */
00045 static void ApproachFormInit(FormPtr form);
00046 static void ApproachFormDeinit();
00047 
00048 
00049 /*****************************************************************************
00050  * FUNCTION:    ApproachFormInit
00051  *
00052  * DESCRIPTION: Approach Form init function
00053  *
00054  * PARAMETERS:  form            - Form Pointer (unused)
00055  *
00056  * RETURNED:    %
00057  *
00058  ****************************************************************************/
00059 static 
00060 void ApproachFormInit(FormPtr form)
00061 {
00062 #pragma unused(form)
00063   // warning-- don't do any drawing in this routine.
00064   // Also, don't call FrmSetFocus from here (it must be called *after*
00065   // FrmDrawForm)
00066 }
00067 
00068 
00069 /*****************************************************************************
00070  * FUNCTION:    ApproachFormDeinit
00071  *
00072  * DESCRIPTION: Approach Form deinit function
00073  *
00074  * PARAMETERS:  %
00075  *
00076  * RETURNED:    %
00077  *
00078  ****************************************************************************/
00079 static 
00080 void ApproachFormDeinit(void)
00081 {
00082 
00083 }
00084 
00085 
00086 /****************************************************************************/
00087 /**
00088  * \brief       Approach Form event handler.
00089  *
00090  *              The Approach Form shows the Active Waypoint's ID
00091  *              and the current distance to the waypoint.
00092  *              If the GPS data is valid, the distance display is updated
00093  *              periodically. If enabled in the application preferences,
00094  *              an alarm sound is generated periodically.
00095  *
00096  * \param       eventP  Pointer to event structure
00097  *
00098  * \return      Status flag: event handled
00099  ****************************************************************************/
00100 Boolean ApproachFormHandleEvent(EventPtr eventP)
00101 {
00102   packed_waypoint_t     *packed_waypoint;       /* packed waypoint ptr */
00103   waypoint_t            waypoint;               /* unpacked waypoint */
00104   MemHandle             waypointDBEntry;        /* waypoint record handle */
00105   UInt16                rec;                    /* record index */
00106   static double         wpt_lat;                /* waypoint latitude */
00107   static double         wpt_lon;                /* waypoint longitude */
00108   double                wpt_dst;                /* distance to waypoint */
00109   Boolean               handled = false;        /* flag: event handled */
00110   FormPtr               frmP;                   /* form ptr */
00111   Char                  str[10];                /* string buffer */
00112   Err                   err;                    /* error code */
00113         
00114   switch (eventP->eType) {
00115     case frmOpenEvent:
00116       frmP = FrmGetActiveForm();
00117       ApproachFormInit(frmP);
00118 
00119       /* find active waypoint */
00120       err = DmFindRecordByID(gWaypointDB, gPrefs.act_wpt.waypointID,
00121         &rec);
00122         
00123       if (!err) {
00124         /* active waypoint found */
00125         waypointDBEntry = DmQueryRecord(gWaypointDB, rec);
00126         packed_waypoint =
00127           (packed_waypoint_t *)MemHandleLock(waypointDBEntry);
00128         UnpackWaypoint(&waypoint, packed_waypoint);
00129 
00130         /* get waypoint position */
00131         wpt_lat = semi2deg(waypoint.posn.lat);
00132         wpt_lon = semi2deg(waypoint.posn.lon);
00133         
00134         /* display WPT ID */
00135         MemSet(str, 7, 0);
00136         StrNCopy(str, (Char *)&waypoint.ident, 6);
00137         SetFieldText(ApproachWptField, str, false, true);
00138         
00139         /* display waypoint distance */
00140         wpt_dst = gc_dist_sphere(gGPSData.lat, gGPSData.lon,
00141           wpt_lat, wpt_lon) * calcR(gGPSData.lat);
00142         format_number(wpt_dst, 1, str);
00143         SetFieldText(ApproachDstField, str, false, true);
00144         
00145         MemHandleUnlock(waypointDBEntry);
00146       }
00147       
00148       FrmDrawForm(frmP);
00149 
00150       handled = true;
00151       break;
00152 
00153     case ctlSelectEvent:
00154       if (eventP->data.ctlSelect.controlID == ApproachOKButton) {
00155         
00156         /* return to previous form */
00157         FrmGotoForm(gPrefs.form);
00158         handled = true;
00159       }
00160       break;
00161 
00162     case nilEvent:
00163       handled = true;
00164 
00165       /* throw away anything in the buffer-- we want fresh data */
00166       DoReceiveFlush(gPortID, 1);
00167 
00168       /*
00169        * Get GPS Data and Waypoint Distance
00170        */
00171       ReadFromGPS();
00172       
00173       if (gGPSData.valid) {
00174         /* get distance to active waypoint */
00175         wpt_dst = gc_dist_sphere(gGPSData.lat, gGPSData.lon,
00176           wpt_lat, wpt_lon) * calcR(gGPSData.lat);
00177         format_number(wpt_dst, 1, str);
00178         SetFieldText(ApproachDstField, str, false, true);      
00179       }
00180 
00181       if (gPrefs.approach_sound) 
00182         SndPlaySystemSound(sndAlarm);
00183       
00184       break;
00185           
00186     case frmCloseEvent:
00187       /* Deallocate Fields' Text Memory */
00188       SetFieldText(ApproachWptField, NULL, false, true);
00189       SetFieldText(ApproachDstField, NULL, false, true);
00190                 
00191       ApproachFormDeinit();
00192       
00193       handled = false;
00194       break;
00195 
00196   } /* switch (eventP->eType) */
00197   return(handled);
00198 }

Created: Mon, 08 Oct 2007 22:33:16 +0200
Copyright ©2004 M. Prinke