example.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. #include <stdio.h>
  9. #include <string.h>
  10. #include "minmea.h"
  11. #define INDENT_SPACES " "
  12. int main(void)
  13. {
  14. char line[MINMEA_MAX_SENTENCE_LENGTH];
  15. while (fgets(line, sizeof(line), stdin) != NULL) {
  16. printf("%s", line);
  17. switch (minmea_sentence_id(line, false)) {
  18. case MINMEA_SENTENCE_RMC: {
  19. struct minmea_sentence_rmc frame;
  20. if (minmea_parse_rmc(&frame, line)) {
  21. printf(INDENT_SPACES "$xxRMC: raw coordinates and speed: (%d/%d,%d/%d) %d/%d\n",
  22. frame.latitude.value, frame.latitude.scale,
  23. frame.longitude.value, frame.longitude.scale,
  24. frame.speed.value, frame.speed.scale);
  25. printf(INDENT_SPACES "$xxRMC fixed-point coordinates and speed scaled to three decimal places: (%d,%d) %d\n",
  26. minmea_rescale(&frame.latitude, 1000),
  27. minmea_rescale(&frame.longitude, 1000),
  28. minmea_rescale(&frame.speed, 1000));
  29. printf(INDENT_SPACES "$xxRMC floating point degree coordinates and speed: (%f,%f) %f\n",
  30. minmea_tocoord(&frame.latitude),
  31. minmea_tocoord(&frame.longitude),
  32. minmea_tofloat(&frame.speed));
  33. }
  34. else {
  35. printf(INDENT_SPACES "$xxRMC sentence is not parsed\n");
  36. }
  37. } break;
  38. case MINMEA_SENTENCE_GGA: {
  39. struct minmea_sentence_gga frame;
  40. if (minmea_parse_gga(&frame, line)) {
  41. printf(INDENT_SPACES "$xxGGA: fix quality: %d\n", frame.fix_quality);
  42. }
  43. else {
  44. printf(INDENT_SPACES "$xxGGA sentence is not parsed\n");
  45. }
  46. } break;
  47. case MINMEA_SENTENCE_GST: {
  48. struct minmea_sentence_gst frame;
  49. if (minmea_parse_gst(&frame, line)) {
  50. printf(INDENT_SPACES "$xxGST: raw latitude,longitude and altitude error deviation: (%d/%d,%d/%d,%d/%d)\n",
  51. frame.latitude_error_deviation.value, frame.latitude_error_deviation.scale,
  52. frame.longitude_error_deviation.value, frame.longitude_error_deviation.scale,
  53. frame.altitude_error_deviation.value, frame.altitude_error_deviation.scale);
  54. printf(INDENT_SPACES "$xxGST fixed point latitude,longitude and altitude error deviation"
  55. " scaled to one decimal place: (%d,%d,%d)\n",
  56. minmea_rescale(&frame.latitude_error_deviation, 10),
  57. minmea_rescale(&frame.longitude_error_deviation, 10),
  58. minmea_rescale(&frame.altitude_error_deviation, 10));
  59. printf(INDENT_SPACES "$xxGST floating point degree latitude, longitude and altitude error deviation: (%f,%f,%f)",
  60. minmea_tofloat(&frame.latitude_error_deviation),
  61. minmea_tofloat(&frame.longitude_error_deviation),
  62. minmea_tofloat(&frame.altitude_error_deviation));
  63. }
  64. else {
  65. printf(INDENT_SPACES "$xxGST sentence is not parsed\n");
  66. }
  67. } break;
  68. case MINMEA_SENTENCE_GSV: {
  69. struct minmea_sentence_gsv frame;
  70. if (minmea_parse_gsv(&frame, line)) {
  71. printf(INDENT_SPACES "$xxGSV: message %d of %d\n", frame.msg_nr, frame.total_msgs);
  72. printf(INDENT_SPACES "$xxGSV: satellites in view: %d\n", frame.total_sats);
  73. for (int i = 0; i < 4; i++)
  74. printf(INDENT_SPACES "$xxGSV: sat nr %d, elevation: %d, azimuth: %d, snr: %d dbm\n",
  75. frame.sats[i].nr,
  76. frame.sats[i].elevation,
  77. frame.sats[i].azimuth,
  78. frame.sats[i].snr);
  79. }
  80. else {
  81. printf(INDENT_SPACES "$xxGSV sentence is not parsed\n");
  82. }
  83. } break;
  84. case MINMEA_SENTENCE_VTG: {
  85. struct minmea_sentence_vtg frame;
  86. if (minmea_parse_vtg(&frame, line)) {
  87. printf(INDENT_SPACES "$xxVTG: true track degrees = %f\n",
  88. minmea_tofloat(&frame.true_track_degrees));
  89. printf(INDENT_SPACES " magnetic track degrees = %f\n",
  90. minmea_tofloat(&frame.magnetic_track_degrees));
  91. printf(INDENT_SPACES " speed knots = %f\n",
  92. minmea_tofloat(&frame.speed_knots));
  93. printf(INDENT_SPACES " speed kph = %f\n",
  94. minmea_tofloat(&frame.speed_kph));
  95. }
  96. else {
  97. printf(INDENT_SPACES "$xxVTG sentence is not parsed\n");
  98. }
  99. } break;
  100. case MINMEA_SENTENCE_ZDA: {
  101. struct minmea_sentence_zda frame;
  102. if (minmea_parse_zda(&frame, line)) {
  103. printf(INDENT_SPACES "$xxZDA: %d:%d:%d %02d.%02d.%d UTC%+03d:%02d\n",
  104. frame.time.hours,
  105. frame.time.minutes,
  106. frame.time.seconds,
  107. frame.date.day,
  108. frame.date.month,
  109. frame.date.year,
  110. frame.hour_offset,
  111. frame.minute_offset);
  112. }
  113. else {
  114. printf(INDENT_SPACES "$xxZDA sentence is not parsed\n");
  115. }
  116. } break;
  117. case MINMEA_INVALID: {
  118. printf(INDENT_SPACES "$xxxxx sentence is not valid\n");
  119. } break;
  120. default: {
  121. printf(INDENT_SPACES "$xxxxx sentence is not parsed\n");
  122. } break;
  123. }
  124. }
  125. return 0;
  126. }
  127. /* vim: set ts=4 sw=4 et: */