minmea.h 3.6 KB

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