diff options
Diffstat (limited to 'mbglib/common/gpsutils.c')
-rw-r--r-- | mbglib/common/gpsutils.c | 143 |
1 files changed, 134 insertions, 9 deletions
diff --git a/mbglib/common/gpsutils.c b/mbglib/common/gpsutils.c index 225a03e..c5083ef 100644 --- a/mbglib/common/gpsutils.c +++ b/mbglib/common/gpsutils.c @@ -1,7 +1,7 @@ /************************************************************************** * - * $Id: gpsutils.c 1.4.1.2 2004/11/09 14:42:36 martin REL_M $ + * $Id: gpsutils.c 1.9 2013/01/30 16:10:08 martin REL_M $ * * Copyright (c) Meinberg Funkuhren, Bad Pyrmont, Germany * @@ -10,10 +10,18 @@ * * ----------------------------------------------------------------------- * $Log: gpsutils.c $ - * Revision 1.4.1.2 2004/11/09 14:42:36 martin - * Use C99 fixed-size types in swap_double(). - * Revision 1.4.1.1 2003/05/16 08:36:27 MARTIN - * Fixed sprint_dms() to work correctly under QNX. + * Revision 1.9 2013/01/30 16:10:08 martin + * Exclude some code from compiling by default, and + * thus don't require pcpslstr.h by default. + * Revision 1.8 2012/10/15 14:27:05Z martin + * Exclude sprint_fixed_freq() from build except for Borland C / Windows. + * Revision 1.7 2010/07/15 09:32:09 martin + * Use DEG character definition from pcpslstr.h. + * Revision 1.6 2004/12/28 11:21:26Z martin + * Omit trap if fixed_freq is 0. + * Use C99 fixed-size data types were required. + * Revision 1.5 2003/02/04 09:20:04Z MARTIN + * New functions sprint_alt(), sprint_fixed_freq(). * Revision 1.4 2003/01/31 13:45:19 MARTIN * sprint_pos_geo() returns N/A if position not valid. * Revision 1.3 2002/12/12 16:07:04 martin @@ -30,9 +38,17 @@ #include <gpsutils.h> #undef _GPSUTILS +#if !defined( USE_SPRINTF ) + #define USE_SPRINTF 0 +#endif + +#if USE_SPRINTF + #include <pcpslstr.h> +#endif + #include <stdio.h> #include <string.h> - +#include <math.h> #define _eos( _s ) ( &(_s)[strlen( _s )] ) @@ -155,13 +171,14 @@ void swap_pos_doubles( POS *posp ) +#if USE_SPRINTF + /*HDR*/ void sprint_dms( char *s, DMS *pdms, int prec ) { - sprintf( s, "%c %i%c%02i'%02.*f\"", + sprintf( s, "%c %i" DEG "%02i'%02.*f\"", pdms->prefix, pdms->deg, - C_DEGREE, pdms->min, prec, pdms->sec @@ -172,6 +189,15 @@ void sprint_dms( char *s, DMS *pdms, int prec ) /*HDR*/ +void sprint_alt( char *s, double alt ) +{ + sprintf( s, "%.0fm", alt ); + +} /* sprint_dms */ + + + +/*HDR*/ void sprint_pos_geo( char *s, POS *ppos, const char *sep, int prec ) { if ( ppos->lla[LON] && ppos->lla[LAT] && ppos->lla[ALT] ) @@ -180,12 +206,111 @@ void sprint_pos_geo( char *s, POS *ppos, const char *sep, int prec ) strcat( s, sep ); sprint_dms( _eos( s ), &ppos->longitude, prec ); strcat( s, sep ); - sprintf( _eos( s ), "%.0fm", ppos->lla[ALT] ); + sprint_alt( _eos( s ), ppos->lla[ALT] ); } else strcpy( s, "N/A" ); } /* sprint_pos_geo */ +#endif + + + +#if defined( MBG_TGT_WIN32 ) && defined( __BORLANDC__ ) + +/*HDR*/ +void sprint_fixed_freq( char *s, FIXED_FREQ_INFO *p_ff ) +{ + double freq; + int range; + ushort unit; + ushort format; + + // Before re-calculating frequency, range is the base 10 exponent + // to the frequency value which is represented in kHz. + // After calculating range from real frequency, range is represented + // as follows: + // range display format divisor format index calculation + // -3 100mHz 1.000 [/1e-3] -3 % 3 = -3 + 3 = 0 % 3 = 0 + // -2 100mHz 10.00 [/1e-3] -2 % 3 = -2 + 3 = 1 % 3 = 1 + // -1 100mHz 100.0 [/1e-3] -1 % 3 = -1 + 3 = 2 % 3 = 2 + // 0 1Hz 1.000 [/1e0] 0 % 3 = 0 + 3 = 3 % 3 = 0 + // 1 10Hz 10.00 [/1e0] 1 % 3 = 1 + 3 = 1 % 3 = 1 + // 2 100Hz 100.0 [/1e0] 2 % 3 = 2 + 3 = 2 % 3 = 2 + // 3 1kHz 1.000 [/1e3] 3 % 3 = 0 + 3 = 3 % 3 = 0 + // 4 10kHz 10.00 [/1e3] 4 % 3 = 1 + 3 = 4 % 3 = 1 + // 5 100kHz 100.0 [/1e3] 5 % 3 = 2 + 3 = 5 % 3 = 2 + // 6 1MHz 1.000 [/1e6] 6 % 3 = 0 + 3 = 3 % 3 = 0 + // 7 10MHz 10.00 [/1e6] 7 % 3 = 1 + 3 = 4 % 3 = 1 + // 8 100MHz 100.0 [/1e6] 8 % 3 = 2 + 3 = 5 % 3 = 2 + + // format string for fp output + static const char *fmt_str[] = + { + "%4.3lf%s", + "%4.2lf%s", + "%4.1lf%s", + }; + + // Unit index and divisor are calculated as follows: + // range unit index calculation divisor calculation + // -3 mHz ( int )( ( -3 + 3 ) / 3 ) = 0 + // -2 mHz ( int )( ( -2 + 3 ) / 3 ) = 0 + // -1 mHz ( int )( ( -1 + 3 ) / 3 ) = 0 / 10e-3 = 10e( 3 * 0 - 3 ) + // 0 Hz ( int )( ( 0 + 3 ) / 3 ) = 1 + // 1 Hz ( int )( ( 1 + 3 ) / 3 ) = 1 + // 2 Hz ( int )( ( 2 + 3 ) / 3 ) = 1 / 1e0 = 10e( 3 * 1 - 3 ) + // 3 kHz ( int )( ( 3 + 3 ) / 3 ) = 2 + // 4 kHz ( int )( ( 4 + 3 ) / 3 ) = 2 + // 5 kHz ( int )( ( 5 + 3 ) / 3 ) = 2 / 10e3 = 10e( 3 * 2 - 3 ) + // 6 MHz ( int )( ( 6 + 3 ) / 3 ) = 3 + // 7 MHz ( int )( ( 7 + 3 ) / 3 ) = 3 + // 8 MHz ( int )( ( 8 + 3 ) / 3 ) = 3 / 10e6 =10e( 3 * 3 - 3 ) + + // unit string + static const char *unit_str[] = + { + "mHz", + "Hz", + "kHz", + "MHz" + }; + + + if ( p_ff->khz_val ) + { + // calculate frequency in Hz + freq = ( double ) p_ff->khz_val * pow( 10, ( p_ff->range + 3 ) ); + + // calculate decimal exponent + range = ( ( int ) log10( freq ) ); + + // check whether range is in the allowed range + // if so display frequency in broken format + if ( ( range >= -3 ) && ( range <= 8 ) ) + { + // calculate format index ( see above ) + format = ( ( ( range % 3 ) + 3 ) % 3 ); + + // calculate unit index + unit = ( ushort )( range + 3 ) / 3; + + // calculate display value + freq = freq / pow( 10, ( ( 3 * unit ) - 3 ) ); + sprintf( s, fmt_str[format], freq, unit_str[unit] ); + return; + } + else + { + // out of range display fequency in Hz + sprintf( s, "%lfHz", freq ); + } + } + else + strcpy( s, "N/A" ); + +} /* sprint_fixed_freq */ +#endif // defined( MBG_TGT_WIN32 ) && defined( __BORLANDC__ ) |