minmea.h 4.2 KB

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