minmea.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright © 2014 Kosma Moczek <kosma@cloudyourcar.com>
  3. * This work is free. You can redistribute it and/or modify it under the
  4. * terms of the Do What The Fuck You Want To Public License, Version 2,
  5. * as published by Sam Hocevar. See the COPYING file for more details.
  6. */
  7. #ifndef MINMEA_H
  8. #define MINMEA_H
  9. #ifdef __cplusplus
  10. extern "C" {
  11. #endif
  12. #include <stdio.h>
  13. #include <stdint.h>
  14. #include <stdbool.h>
  15. #include <errno.h>
  16. #include <time.h>
  17. #include <math.h>
  18. #include <sys/time.h>
  19. #define MINMEA_MAX_LENGTH 80
  20. enum minmea_type {
  21. MINMEA_INVALID = -1,
  22. MINMEA_UNKNOWN = 0,
  23. MINMEA_GPRMC,
  24. MINMEA_GPGGA,
  25. MINMEA_GPGSA,
  26. };
  27. struct minmea_date {
  28. int day;
  29. int month;
  30. int year;
  31. };
  32. struct minmea_time {
  33. int hours;
  34. int minutes;
  35. int seconds;
  36. int microseconds;
  37. };
  38. struct minmea_gprmc {
  39. struct minmea_time time;
  40. bool valid;
  41. int latitude, latitude_scale;
  42. int longitude, longitude_scale;
  43. int speed, speed_scale;
  44. int course, course_scale;
  45. struct minmea_date date;
  46. int variation, variation_scale;
  47. };
  48. struct minmea_gpgga {
  49. struct minmea_time time;
  50. int latitude, latitude_scale;
  51. int longitude, longitude_scale;
  52. int fix_quality;
  53. int satellites_tracked;
  54. int hdop, hdop_scale;
  55. int altitude, altitude_scale; char altitude_units;
  56. int height, height_scale; char height_units;
  57. int dgps_age;
  58. };
  59. enum minmea_gpgsa_mode {
  60. MINMEA_GPGSA_MODE_AUTO = 'A',
  61. MINMEA_GPGSA_MODE_FORCED = 'M',
  62. };
  63. enum minmea_gpgsa_fix_type {
  64. MINMEA_GPGSA_FIX_NONE = 1,
  65. MINMEA_GPGSA_FIX_2D = 2,
  66. MINMEA_GPGSA_FIX_3D = 3,
  67. };
  68. struct minmea_gpgsa {
  69. char mode;
  70. int fix_type;
  71. int sats[12];
  72. int pdop, pdop_scale;
  73. int hdop, hdop_scale;
  74. int vdop, vdop_scale;
  75. };
  76. /**
  77. * Check sequence validity and checksum. Returns true for valid sequences.
  78. */
  79. bool minmea_check(const char *sentence);
  80. /**
  81. * Determine sequence type.
  82. */
  83. enum minmea_type minmea_type(const char *sequence);
  84. /**
  85. * Scanf-like processor for NMEA sentences. Supports the following formats:
  86. * c - single character (char *)
  87. * d - direction, returned as 1/-1, default 0 (int *)
  88. * f - fractional, returned as value + scale (int *, int *)
  89. * i - decimal, default zero (int *)
  90. * s - string (char *)
  91. * t - talker identifier and type (char *)
  92. * T - date/time stamp (int *, int *, int *)
  93. * Returns true on success. See library source code for details.
  94. */
  95. bool minmea_scan(const char *sentence, const char *format, ...);
  96. /*
  97. * Parse a specific type of frame. Return true on success.
  98. */
  99. bool minmea_parse_gprmc(struct minmea_gprmc *frame, const char *sentence);
  100. bool minmea_parse_gpgga(struct minmea_gpgga *frame, const char *sentence);
  101. bool minmea_parse_gpgsa(struct minmea_gpgsa *frame, const char *sentence);
  102. /**
  103. * Convert GPS UTC date/time representation to a UNIX timestamp.
  104. */
  105. int minmea_gettimeofday(struct timeval *tv, const struct minmea_date *date, const struct minmea_time *time);
  106. /**
  107. * Rescale signed integer value to a different full-scale value, with rounding.
  108. * The "from" value in the numerator cancels out with the denominator, leaving
  109. * just 1/2 - which makes integer truncation act as rounding. Returns zero for
  110. * invalid values.
  111. */
  112. static inline int minmea_rescale(int value, int from, int to)
  113. {
  114. if (from == 0)
  115. return 0;
  116. if (from == to)
  117. return value;
  118. return (value * to + from / 2) / from;
  119. }
  120. /**
  121. * Convert a fixed-point value to a floating-point value.
  122. * Returns NaN for "unknown" values.
  123. */
  124. static inline float minmea_float(int value, int scale)
  125. {
  126. if (scale == 0)
  127. return NAN;
  128. return (float) value / (float) scale;
  129. }
  130. /**
  131. * Convert a raw coordinate to a floating point DD.DDD... value.
  132. * Returns NaN for "unknown" values.
  133. */
  134. static inline float minmea_coord(int value, int scale)
  135. {
  136. if (scale == 0)
  137. return NAN;
  138. int degrees = value / (scale * 100);
  139. int minutes = value % (scale * 100);
  140. return (float) degrees + (float) minutes / (60 * scale);
  141. }
  142. #ifdef __cplusplus
  143. }
  144. #endif
  145. #endif /* MINMEA_H */
  146. /* vim: set ts=4 sw=4 et: */