minmea.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. GPGSA_MODE_AUTO,
  61. GPGSA_MODE_FORCED
  62. };
  63. enum minmea_gpgsa_fix_type {
  64. GPGSA_FIX_NONE,
  65. GPGSA_FIX_2D,
  66. GPGSA_FIX_3D
  67. };
  68. struct minmea_gpgsa {
  69. int mode;
  70. int fix_type;
  71. int sat1;
  72. int sat2;
  73. int sat3;
  74. int sat4;
  75. int sat5;
  76. int sat6;
  77. int sat7;
  78. int sat8;
  79. int sat9;
  80. int sat10;
  81. int sat11;
  82. int sat12;
  83. int pdop, pdop_scale;
  84. int hdop, hdop_scale;
  85. int vdop, vdop_scale;
  86. };
  87. /**
  88. * Check sequence validity and checksum. Returns true for valid sequences.
  89. */
  90. bool minmea_check(const char *sentence);
  91. /**
  92. * Determine sequence type.
  93. */
  94. enum minmea_type minmea_type(const char *sequence);
  95. /**
  96. * Scanf-like processor for NMEA sentences. Supports the following formats:
  97. * c - single character (char *)
  98. * d - direction, returned as 1/-1, default 0 (int *)
  99. * f - fractional, returned as value + scale (int *, int *)
  100. * i - decimal, default zero (int *)
  101. * s - string (char *)
  102. * t - talker identifier and type (char *)
  103. * T - date/time stamp (int *, int *, int *)
  104. * Returns true on success. See library source code for details.
  105. */
  106. bool minmea_scan(const char *sentence, const char *format, ...);
  107. /*
  108. * Parse a specific type of frame. Return true on success.
  109. */
  110. bool minmea_parse_gprmc(struct minmea_gprmc *frame, const char *sentence);
  111. bool minmea_parse_gpgga(struct minmea_gpgga *frame, const char *sentence);
  112. bool minmea_parse_gpgsa(struct minmea_gpgsa *frame, const char *sentence);
  113. /**
  114. * Convert GPS UTC date/time representation to a UNIX timestamp.
  115. */
  116. int minmea_gettimeofday(struct timeval *tv, const struct minmea_date *date, const struct minmea_time *time);
  117. /**
  118. * Rescale signed integer value to a different full-scale value, with rounding.
  119. * The "from" value in the numerator cancels out with the denominator, leaving
  120. * just 1/2 - which makes integer truncation act as rounding. Returns zero for
  121. * invalid values.
  122. */
  123. static inline int minmea_rescale(int value, int from, int to)
  124. {
  125. if (from == 0)
  126. return 0;
  127. if (from == to)
  128. return value;
  129. return (value * to + from / 2) / from;
  130. }
  131. /**
  132. * Convert a fixed-point value to a floating-point value.
  133. * Returns NaN for "unknown" values.
  134. */
  135. static inline float minmea_float(int value, int scale)
  136. {
  137. if (scale == 0)
  138. return NAN;
  139. return (float) value / (float) scale;
  140. }
  141. /**
  142. * Convert a raw coordinate to a floating point DD.DDD... value.
  143. * Returns NaN for "unknown" values.
  144. */
  145. static inline float minmea_coord(int value, int scale)
  146. {
  147. if (scale == 0)
  148. return NAN;
  149. int degrees = value / (scale * 100);
  150. int minutes = value % (scale * 100);
  151. return (float) degrees + (float) minutes / (60 * scale);
  152. }
  153. #ifdef __cplusplus
  154. }
  155. #endif
  156. #endif /* MINMEA_H */
  157. /* vim: set ts=4 sw=4 et: */