diff options
author | Gary E. Miller <gem@rellim.com> | 2019-02-22 15:44:05 -0800 |
---|---|---|
committer | Gary E. Miller <gem@rellim.com> | 2019-02-22 15:44:05 -0800 |
commit | f1be39110e39e5c8d095200231a75aea3afecd57 (patch) | |
tree | 77d43f78b8235d2a9e085d80c3a2631d12505d03 | |
parent | 1db29a3fa49eca5929ba03346eac7cd49f58e86d (diff) | |
download | gpsd-f1be39110e39e5c8d095200231a75aea3afecd57.tar.gz |
deg_to_s() and calls to it: Allow negative degrees, use fabs().
Every call to deg_to_s() was preceded by a fabs(deg). So move
the fabs() into deg_to_s().
-rw-r--r-- | cgps.c | 4 | ||||
-rw-r--r-- | doc/explan_gpsdclient.c.xml | 14 | ||||
-rw-r--r-- | gpsdclient.c | 3 | ||||
-rw-r--r-- | gpsdclient.h | 3 | ||||
-rw-r--r-- | lcdgps.c | 12 | ||||
-rw-r--r-- | monitor_nmea0183.c | 6 | ||||
-rw-r--r-- | tests/test_gpsdclient.c | 22 | ||||
-rwxr-xr-x | xgps | 8 |
8 files changed, 48 insertions, 24 deletions
@@ -668,7 +668,7 @@ static void update_gps_panel(struct gps_data_t *gpsdata, char *message) /* Fill in the latitude. */ if (gpsdata->fix.mode >= MODE_2D && isfinite(gpsdata->fix.latitude) != 0) { (void)snprintf(scr, sizeof(scr), " %s %c", - deg_to_str(deg_type, fabs(gpsdata->fix.latitude)), + deg_to_str(deg_type, gpsdata->fix.latitude), (gpsdata->fix.latitude < 0) ? 'S' : 'N'); } else (void)snprintf(scr, sizeof(scr), "n/a"); @@ -677,7 +677,7 @@ static void update_gps_panel(struct gps_data_t *gpsdata, char *message) /* Fill in the longitude. */ if (gpsdata->fix.mode >= MODE_2D && isfinite(gpsdata->fix.longitude) != 0) { (void)snprintf(scr, sizeof(scr), " %s %c", - deg_to_str(deg_type, fabs(gpsdata->fix.longitude)), + deg_to_str(deg_type, gpsdata->fix.longitude), (gpsdata->fix.longitude < 0) ? 'W' : 'E'); } else (void)snprintf(scr, sizeof(scr), "n/a"); diff --git a/doc/explan_gpsdclient.c.xml b/doc/explan_gpsdclient.c.xml index 98ef22fa..babd2499 100644 --- a/doc/explan_gpsdclient.c.xml +++ b/doc/explan_gpsdclient.c.xml @@ -19,8 +19,18 @@ <tbody> <row> - <entry><function>char *deg_to_str( enum deg_str_type type, double f)</function></entry> - <entry><para>Convert double degrees to a static string and return a pointer to it.</para><para>Makes a simple check on invalid degree values (less than 0 or more than 360) and returns "nan" if found.</para><para>For valid values, it generates the appropriate string according to the string type enumeration, defaulting to DD MM SS.sss</para></entry> + <entry> + <function>char *deg_to_str(enum deg_str_type type, double f)</function> + </entry> + <entry> + <para>Convert the absolute value of double degrees to a static string + and return a pointer to it.</para> + <para>Makes a simple check on invalid degree values (not more + than 360) and returns "nan" on error.</para> + <para>For valid values, it generates the appropriate string according + to the string type enumeration: dd, ddmm or ddmmss.</para> + <para>Warning: not thread safe.</para> + </entry> </row> <row> <entry><function>enum unit gpsd_units(void)</function></entry> diff --git a/gpsdclient.c b/gpsdclient.c index 3705f6f2..1e585c74 100644 --- a/gpsdclient.c +++ b/gpsdclient.c @@ -52,7 +52,8 @@ char *deg_to_str(enum deg_str_type type, double f) int dsec, sec, deg, min; double fdsec, fsec, fdeg, fmin; - if (0.0 > f || 360.0 < f) { + f = fabs(f); + if (360.0 < f) { (void)strlcpy(str, "nan", sizeof(str)); return str; } diff --git a/gpsdclient.h b/gpsdclient.h index d988b751..e4e5dae2 100644 --- a/gpsdclient.h +++ b/gpsdclient.h @@ -34,7 +34,8 @@ enum unit gpsd_units(void); enum deg_str_type { deg_dd, deg_ddmm, deg_ddmmss }; float true2magnetic(double, double, double); -extern char *deg_to_str( enum deg_str_type type, double f); +/* Warning: not thread safe */ +extern char *deg_to_str(enum deg_str_type type, double f); extern void gpsd_source_spec(const char *fromstring, struct fixsource_t *source); @@ -189,12 +189,16 @@ static void update_lcd(struct gps_data_t *gpsdata) int track; char *s; - s = deg_to_str(deg_type, fabs(gpsdata->fix.latitude)); - snprintf(tmpbuf, sizeof(tmpbuf) - 1, "widget_set gpsd one 1 1 {Lat: %s %c}\n", s, (gpsdata->fix.latitude < 0) ? 'S' : 'N'); + s = deg_to_str(deg_type, gpsdata->fix.latitude); + snprintf(tmpbuf, sizeof(tmpbuf) - 1, + "widget_set gpsd one 1 1 {Lat: %s %c}\n", s, + (gpsdata->fix.latitude < 0) ? 'S' : 'N'); send_lcd(tmpbuf); - s = deg_to_str(deg_type, fabs(gpsdata->fix.longitude)); - snprintf(tmpbuf, sizeof(tmpbuf) - 1, "widget_set gpsd two 1 2 {Lon: %s %c}\n", s, (gpsdata->fix.longitude < 0) ? 'W' : 'E'); + s = deg_to_str(deg_type, gpsdata->fix.longitude); + snprintf(tmpbuf, sizeof(tmpbuf) - 1, + "widget_set gpsd two 1 2 {Lon: %s %c}\n", s, + (gpsdata->fix.longitude < 0) ? 'W' : 'E'); send_lcd(tmpbuf); /* As a pilot, a heading of "0" gives me the heebie-jeebies (ie, 0 diff --git a/monitor_nmea0183.c b/monitor_nmea0183.c index 016998e4..920bdf9b 100644 --- a/monitor_nmea0183.c +++ b/monitor_nmea0183.c @@ -166,8 +166,7 @@ static void cooked_pvt(void) if (session.gpsdata.fix.mode >= MODE_2D && isfinite(session.gpsdata.fix.latitude) != 0) { (void)snprintf(scr, sizeof(scr), "%s %c", - deg_to_str(deg_ddmmss, - fabs(session.gpsdata.fix.latitude)), + deg_to_str(deg_ddmmss, session.gpsdata.fix.latitude), (session.gpsdata.fix.latitude < 0) ? 'S' : 'N'); } else (void)snprintf(scr, sizeof(scr), "n/a"); @@ -176,8 +175,7 @@ static void cooked_pvt(void) if (session.gpsdata.fix.mode >= MODE_2D && isfinite(session.gpsdata.fix.longitude) != 0) { (void)snprintf(scr, sizeof(scr), "%s %c", - deg_to_str(deg_ddmmss, - fabs(session.gpsdata.fix.longitude)), + deg_to_str(deg_ddmmss, session.gpsdata.fix.longitude), (session.gpsdata.fix.longitude < 0) ? 'W' : 'E'); } else (void)snprintf(scr, sizeof(scr), "n/a"); diff --git a/tests/test_gpsdclient.c b/tests/test_gpsdclient.c index c65b35f6..7f31ae21 100644 --- a/tests/test_gpsdclient.c +++ b/tests/test_gpsdclient.c @@ -65,16 +65,28 @@ struct test tests[] = { " 12 02.050000'", " 12 02' 02.99999\""}, /* not rounded up */ /* -44.99999999999 */ - /* nan because not positive degrees */ - {-44.99999999999, - "nan", - "nan", - "nan"}, + /* fabs() */ + {-44.0, + " 44.00000000", + " 44 00.000000'", + " 44 00' 00.00000\""}, /* 359.99999999999 */ {359.99999999999, " 0.00000000", /* rounded up, and rolled over */ " 0 00.000000'", " 0 00' 00.00000\""}, + /* 361 */ + /* nan because out of range */ + {361, + "nan", + "nan", + "nan"}, + /* -361 */ + /* nan because out of range */ + {361, + "nan", + "nan", + "nan"}, }; @@ -623,7 +623,6 @@ class AISView(object): latsuff = "N" else: latsuff = "" - lat = abs(lat) lat = gps.clienthelpers.deg_to_str(self.deg_type, lat) if lon < 0: lonsuff = "W" @@ -631,7 +630,6 @@ class AISView(object): lonsuff = "E" else: lonsuff = "" - lon = abs(lon) lon = gps.clienthelpers.deg_to_str(gps.clienthelpers.deg_ddmmss, lon) return lat + latsuff + "/" + lon + lonsuff @@ -977,7 +975,7 @@ class Base(object): def update_latitude(self, data): "Update latitude" if data.mode >= gps.MODE_2D and hasattr(data, "lat"): - lat = gps.clienthelpers.deg_to_str(self.deg_type, abs(data.lat)) + lat = gps.clienthelpers.deg_to_str(self.deg_type, data.lat) if data.lat < 0: ns = 'S' else: @@ -989,7 +987,7 @@ class Base(object): def update_longitude(self, data): "Update longitude" if data.mode >= gps.MODE_2D and hasattr(data, "lon"): - lon = gps.clienthelpers.deg_to_str(self.deg_type, abs(data.lon)) + lon = gps.clienthelpers.deg_to_str(self.deg_type, data.lon) if data.lon < 0: ew = 'W' else: @@ -1029,7 +1027,7 @@ class Base(object): "Update track" if hasattr(data, "track"): return "%14s °" % ( - gps.clienthelpers.deg_to_str(self.deg_type, abs(data.track))) + gps.clienthelpers.deg_to_str(self.deg_type, data.track)) return "n/a" |