minmea.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #ifndef MINMEA_H
  2. #define MINMEA_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif
  6. #include <stdio.h>
  7. #include <stdint.h>
  8. #include <stdbool.h>
  9. #include <errno.h>
  10. #include <time.h>
  11. #include <math.h>
  12. #include <sys/time.h>
  13. #define MINMEA_MAX_LENGTH 80
  14. enum minmea_type {
  15. MINMEA_INVALID = -1,
  16. MINMEA_UNKNOWN = 0,
  17. MINMEA_GPRMC,
  18. MINMEA_GPGGA,
  19. };
  20. struct minmea_date {
  21. int day;
  22. int month;
  23. int year;
  24. };
  25. struct minmea_time {
  26. int hours;
  27. int minutes;
  28. int seconds;
  29. int microseconds;
  30. };
  31. struct minmea_gprmc {
  32. struct minmea_time time;
  33. bool valid;
  34. int latitude, latitude_scale;
  35. int longitude, longitude_scale;
  36. int speed, speed_scale;
  37. int course, course_scale;
  38. struct minmea_date date;
  39. int variation, variation_scale;
  40. };
  41. struct minmea_gpgga {
  42. struct minmea_time time;
  43. int latitude, latitude_scale;
  44. int longitude, longitude_scale;
  45. int fix_quality;
  46. int satellites_tracked;
  47. int hdop, hdop_scale;
  48. int altitude, altitude_scale; char altitude_units;
  49. int height, height_scale; char height_units;
  50. int dgps_age;
  51. };
  52. /**
  53. * Check sequence validity and checksum. Returns true for valid sequences.
  54. */
  55. bool minmea_check(const char *sentence);
  56. /**
  57. * Determine sequence type.
  58. */
  59. enum minmea_type minmea_type(const char *sequence);
  60. /**
  61. * Scanf-like processor for NMEA sentences. Supports the following formats:
  62. * c - single character (char *)
  63. * d - direction, returned as 1/-1, default 0 (int *)
  64. * f - fractional, returned as value + scale (int *, int *)
  65. * i - decimal, default zero (int *)
  66. * s - string (char *)
  67. * t - talker identifier and type (char *)
  68. * T - date/time stamp (int *, int *, int *)
  69. * Returns true on success. See library source code for details.
  70. */
  71. bool minmea_scan(const char *sentence, const char *format, ...);
  72. /*
  73. * Parse a specific type of frame. Return true on success.
  74. */
  75. bool minmea_parse_gprmc(struct minmea_gprmc *frame, const char *sentence);
  76. bool minmea_parse_gpgga(struct minmea_gpgga *frame, const char *sentence);
  77. /**
  78. * Convert GPS UTC date/time representation to a UNIX timestamp.
  79. */
  80. int minmea_gettimeofday(struct timeval *tv, const struct minmea_date *date, const struct minmea_time *time);
  81. /**
  82. * Rescale signed integer value to a different full-scale value, with rounding.
  83. * The "from" value in the numerator cancels out with the denominator, leaving
  84. * just 1/2 - which makes integer truncation act as rounding. Returns zero for
  85. * invalid values.
  86. */
  87. static inline int minmea_rescale(int value, int from, int to)
  88. {
  89. if (from == 0)
  90. return 0;
  91. if (from == to)
  92. return value;
  93. return (value * to + from / 2) / from;
  94. }
  95. /**
  96. * Convert a fixed-point value to a floating-point value.
  97. * Returns NaN for "unknown" values.
  98. */
  99. static inline float minmea_float(int value, int scale)
  100. {
  101. if (scale == 0)
  102. return NAN;
  103. return (float) value / (float) scale;
  104. }
  105. /**
  106. * Convert a raw coordinate to a floating point DD.DDD... value.
  107. * Returns NaN for "unknown" values.
  108. */
  109. static inline float minmea_coord(int value, int scale)
  110. {
  111. if (scale == 0)
  112. return NAN;
  113. int degrees = value / (scale * 100);
  114. int minutes = value % (scale * 100);
  115. return (float) degrees + (float) minutes / (60 * scale);
  116. }
  117. #ifdef __cplusplus
  118. }
  119. #endif
  120. #endif /* MINMEA_H */
  121. /* vim: set ts=4 sw=4 et: */