Skip to content

Commit

Permalink
Update LwGPS to improve code size
Browse files Browse the repository at this point in the history
  • Loading branch information
MaJerle committed Dec 21, 2022
1 parent a01ec6d commit 20f6f88
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 34 deletions.
31 changes: 15 additions & 16 deletions examples/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,21 @@ lwgps_t hgps;
/**
* \brief Dummy data from GPS receiver
*/
const char
gps_rx_data[] = ""
"$GPRMC,183729,A,3907.356,N,12102.482,W,000.0,360.0,080301,015.5,E*6F\r\n"
"$GPRMB,A,,,,,,,,,,,,V*71\r\n"
"$GPGGA,183730,3907.356,N,12102.482,W,1,05,1.6,646.4,M,-24.1,M,,*75\r\n"
"$GPGSA,A,3,02,,,07,,09,24,26,,,,,1.6,1.6,1.0*3D\r\n"
"$GPGSV,2,1,08,02,43,088,38,04,42,145,00,05,11,291,00,07,60,043,35*71\r\n"
"$GPGSV,2,2,08,08,02,145,00,09,46,303,47,24,16,178,32,26,18,231,43*77\r\n"
"$PGRME,22.0,M,52.9,M,51.0,M*14\r\n"
"$GPGLL,3907.360,N,12102.481,W,183730,A*33\r\n"
"$PGRMZ,2062,f,3*2D\r\n"
"$PGRMM,WGS84*06\r\n"
"$GPBOD,,T,,M,,*47\r\n"
"$GPRTE,1,1,c,0*07\r\n"
"$GPRMC,183731,A,3907.482,N,12102.436,W,000.0,360.0,080301,015.5,E*67\r\n"
"$GPRMB,A,,,,,,,,,,,,V*71\r\n";
const char gps_rx_data[] = ""
"$GPRMC,183729,A,3907.356,N,12102.482,W,000.0,360.0,080301,015.5,E*6F\r\n"
"$GPRMB,A,,,,,,,,,,,,V*71\r\n"
"$GPGGA,183730,3907.356,N,12102.482,W,1,05,1.6,646.4,M,-24.1,M,,*75\r\n"
"$GPGSA,A,3,02,,,07,,09,24,26,,,,,1.6,1.6,1.0*3D\r\n"
"$GPGSV,2,1,08,02,43,088,38,04,42,145,00,05,11,291,00,07,60,043,35*71\r\n"
"$GPGSV,2,2,08,08,02,145,00,09,46,303,47,24,16,178,32,26,18,231,43*77\r\n"
"$PGRME,22.0,M,52.9,M,51.0,M*14\r\n"
"$GPGLL,3907.360,N,12102.481,W,183730,A*33\r\n"
"$PGRMZ,2062,f,3*2D\r\n"
"$PGRMM,WGS84*06\r\n"
"$GPBOD,,T,,M,,*47\r\n"
"$GPRTE,1,1,c,0*07\r\n"
"$GPRMC,183731,A,3907.482,N,12102.436,W,000.0,360.0,080301,015.5,E*67\r\n"
"$GPRMB,A,,,,,,,,,,,,V*71\r\n";

int
main() {
Expand Down
17 changes: 8 additions & 9 deletions examples/test_code.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@ lwgps_t hgps;
/**
* \brief Dummy data from GPS receiver
*/
const char
gps_rx_data[] = ""
"$GPRMC,183729,A,3907.356,N,12102.482,W,000.0,360.0,080301,015.5,E*6F\r\n"
"$GPGGA,183730,3907.356,N,12102.482,W,1,05,1.6,646.4,M,-24.1,M,,*75\r\n"
"$GPGSA,A,3,02,,,07,,09,24,26,,,,,1.6,1.6,1.0*3D\r\n"
"$GPGSV,2,1,08,02,43,088,38,04,42,145,00,05,11,291,00,07,60,043,35*71\r\n"
"$GPGSV,2,2,08,08,02,145,00,09,46,303,47,24,16,178,32,26,18,231,43*77\r\n"
"";
const char gps_rx_data[] = ""
"$GPRMC,183729,A,3907.356,N,12102.482,W,000.0,360.0,080301,015.5,E*6F\r\n"
"$GPGGA,183730,3907.356,N,12102.482,W,1,05,1.6,646.4,M,-24.1,M,,*75\r\n"
"$GPGSA,A,3,02,,,07,,09,24,26,,,,,1.6,1.6,1.0*3D\r\n"
"$GPGSV,2,1,08,02,43,088,38,04,42,145,00,05,11,291,00,07,60,043,35*71\r\n"
"$GPGSV,2,2,08,08,02,145,00,09,46,303,47,24,16,178,32,26,18,231,43*77\r\n"
"";

/**
* \brief Run the test of raw input data
*/
void
run_tests() {
lwgps_init(&hgps); /* Init GPS */
lwgps_init(&hgps); /* Init GPS */

/* Process all input data */
lwgps_process(&hgps, gps_rx_data, strlen(gps_rx_data));
Expand Down
9 changes: 9 additions & 0 deletions lwgps/src/include/lwgps/lwgps_opt.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,15 @@ extern "C" {
#define LWGPS_CFG_CRC 1
#endif

/**
* \brief Enables `1` or disables `0` distance and bearing calculation
*
* \note When not enabled, corresponding function is disabled
*/
#ifndef LWESP_CFG_DISTANCE_BEARING
#define LWESP_CFG_DISTANCE_BEARING 1
#endif

/* Guard against accidental parser breakage */
#if LWGPS_CFG_STATEMENT_PUBX_TIME && !LWGPS_CFG_STATEMENT_PUBX
#error LWGPS_CFG_STATEMENT_PUBX must be enabled when enabling LWGPS_CFG_STATEMENT_PUBX_TIME
Expand Down
35 changes: 26 additions & 9 deletions lwgps/src/lwgps/lwgps.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,12 @@
* Author: Tilen MAJERLE <tilen@majerle.eu>
* Version: v2.1.0
*/
#include <math.h>
#include <stdlib.h>
#include <string.h>
#include "lwgps/lwgps.h"
#if LWESP_CFG_DISTANCE_BEARING
#include <math.h>
#endif

#define FLT(x) ((lwgps_float_t)(x))
#define D2R(x) FLT(FLT(x) * FLT(0.01745329251994)) /*!< Degrees to radians */
Expand Down Expand Up @@ -98,20 +100,31 @@ prv_parse_number(lwgps_t* gh, const char* t) {
*/
static lwgps_float_t
prv_parse_float_number(lwgps_t* gh, const char* t) {
lwgps_float_t res;
lwgps_float_t value = (lwgps_float_t)0, power = (lwgps_float_t)1;
int sign = 1;

if (t == NULL) {
t = gh->p.term_str;
}
for (; t != NULL && *t == ' '; ++t) {} /* Strip leading spaces */

#if LWGPS_CFG_DOUBLE
res = strtod(t, NULL); /* Parse string to double */
#else /* LWGPS_CFG_DOUBLE */
res = strtof(t, NULL); /* Parse string to float */
#endif /* !LWGPS_CFG_DOUBLE */

return FLT(res); /* Return casted value, based on float size */
if (*t == '-') { /* Check sign */
sign = -1;
++t;
}
while (CIN(*t)) { /* Convert main part */
value = value * (lwgps_float_t)10 + CTN(*t);
++t;
}
if (*t == '.') { /* Skip the dot character */
++t;
}
while (CIN(*t)) { /* Get the power */
value = value * (lwgps_float_t)10 + CTN(*t);
power *= 10.0;
++t;
}
return sign * value / power;
}

/**
Expand Down Expand Up @@ -496,6 +509,8 @@ lwgps_process(lwgps_t* gh, const void* data, size_t len) {
return 1;
}

#if LWESP_CFG_DISTANCE_BEARING || __DOXYGEN__

/**
* \brief Calculate distance and bearing between `2` latitude and longitude coordinates
* \param[in] las: Latitude start coordinate, in units of degrees
Expand Down Expand Up @@ -574,6 +589,8 @@ lwgps_distance_bearing(lwgps_float_t las, lwgps_float_t los, lwgps_float_t lae,
return 1;
}

#endif /* LWESP_CFG_DISTANCE_BEARING || __DOXYGEN__ */

/**
* \brief Convert NMEA GPS speed (in knots = nautical mile per hour) to different speed format
* \param[in] sik: Speed in knots, received from GPS NMEA statement
Expand Down

0 comments on commit 20f6f88

Please sign in to comment.