common.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. #pragma once
  2. /* Needs time_t already defined (embedded: stime.h's typedef; PC: <time.h>) -
  3. * include this only after whichever of those this build already includes. */
  4. /*
  5. * Definitions shared byte-for-byte between the embedded build (soft/) and
  6. * the PC test tool (gps-test-tool/): struct layouts that cross the boundary
  7. * between translation units compiled for each side (System/location/
  8. * auto_pause, all real logic - gpx.c/nmea.c/syslog.c/autopause.c compile
  9. * unmodified into both), and the value constants those structs' fields hold.
  10. *
  11. * Kept deliberately minimal: this is not "the" shared header, just the part
  12. * that must never drift, because a mismatch here is a silent ABI mismatch
  13. * between translation units rather than a compile error. Includes, pin/
  14. * hardware definitions, and anything embedded-only or PC-only stay in each
  15. * side's own main.h.
  16. */
  17. /* System.location_valid values */
  18. #define LOC_INVALID 0
  19. #define LOC_VALID 1
  20. #define LOC_VALID_NEW 2
  21. /* System.fix_type values (from GSA) */
  22. #define FIX_TYPE_UNKNOWN 0
  23. #define FIX_TYPE_2D 2
  24. #define FIX_TYPE_3D 3
  25. /* GNSS modes (System.conf.gnss_mode) */
  26. #define GNSS_MODE_GPS_GLONASS_GALILEO 0
  27. #define GNSS_MODE_GPS 1
  28. #define GNSS_MODE_GPS_GALILEO 2
  29. #define GNSS_MODE_GALILEO 3
  30. #define GNSS_MODE_GPS_BEIDOU 4
  31. #define GNSS_MODE_BEIDOU 5
  32. /* UART log mode values (System.conf.uart_log_mode) */
  33. #define UART_LOG_MODE_MIXED 0 /* NMEA and system log interleaved (default) */
  34. #define UART_LOG_MODE_NMEA 1 /* NMEA only */
  35. #define UART_LOG_MODE_SYSTEM 2 /* system log only */
  36. /* Reasons a candidate point can be rejected before being written (see
  37. * soft/syslog.h's reject_reason_names[], indexed by these) */
  38. #define REJECT_REASON_KALMAN 0 /* Kalman-filtered position error too large */
  39. #define REJECT_REASON_DISTDIFF 1 /* distance jump inconsistent with recent history */
  40. #define REJECT_REASON_MINDIST 2 /* too small a position change since last saved point */
  41. #define REJECT_REASON_ALTDIFF 3 /* altitude jump inconsistent with recent history */
  42. /* System.conf.flags bit indices, read/set via get_flag(index)/set_flag(index,
  43. * val) - both sides index the same way (a bit position within a flags byte
  44. * array), not a pre-shifted bitmask, so these constants and the get_flag()
  45. * call sites are interchangeable between builds. */
  46. #define CONFFLAG_DISABLE_FILTERS 0
  47. #define CONFFLAG_ENABLE_SBAS 1
  48. #define CONFFLAG_LOGGING_AFTER_BOOT 2
  49. #define CONFFLAG_AUTO_PAUSE 3
  50. #define CONFFLAG_RESET_ON_NEW_FILE 4
  51. #define CONFFLAG_VERBOSE_LOG 5
  52. #define CONFFLAG_LAST 5
  53. struct config_s {
  54. union {
  55. unsigned char conf_u8[16];
  56. struct {
  57. unsigned char gnss_mode; // 0
  58. unsigned char skip_points; // 1
  59. unsigned char auto_pause_time; // 2
  60. unsigned char auto_pause_dist; // 3
  61. unsigned char min_sats; // 4
  62. unsigned char auto_pause_speed; // 5
  63. unsigned char uart_log_mode; // 6
  64. };
  65. };
  66. unsigned char flags[4];
  67. };
  68. struct location_s {
  69. float lon;
  70. float lat;
  71. float alt;
  72. time_t time;
  73. };
  74. #ifndef PC_BUILD
  75. /* Real hardware-timer-backed countdowns (set_timer()/timer_expired() in
  76. * soft/main.h), driven by an ISR via atomic_set_uint()/atomic_get_uint().
  77. * Embedded-only: the PC build's set_timer()/timer_expired() are plain no-op
  78. * functions that never reference System.timers, so struct system_s doesn't
  79. * carry it there - dead weight, and a stray cli()/sei() dependency, for a
  80. * build with no such ISR. */
  81. struct timers {
  82. unsigned int owire;
  83. unsigned int beep;
  84. unsigned int recv_timeout;
  85. unsigned int status_log;
  86. unsigned int lcd;
  87. unsigned int backlight;
  88. };
  89. #endif
  90. struct system_s {
  91. #ifndef PC_BUILD
  92. struct timers timers;
  93. #endif
  94. struct config_s conf;
  95. unsigned long int distance; // cm
  96. unsigned long int elevation_gain; // dm (decimeters, 0.1m resolution); filtered (dead-band+smoothed)
  97. unsigned long int elevation_loss; // dm; filtered (dead-band+smoothed)
  98. unsigned long int elevation_gain_raw; // dm; unfiltered, for log comparison only
  99. unsigned long int elevation_loss_raw; // dm; unfiltered, for log comparison only
  100. unsigned long int alt_max; // dm; highest smoothed altitude seen this session
  101. unsigned long int speed_accum_x100; // sum of instantaneous NMEA speed samples (km/h * 100) while not paused
  102. unsigned long int speed_sample_count; // number of samples in speed_accum_x100
  103. unsigned long int points_written; // count of accepted/written track points this session
  104. unsigned long int rejected_count; // count of rejected candidate points this session (unsigned long: a poor-fix session can reject far more than 65535 times)
  105. unsigned long int points_paused; // count of candidate points seen while tracking_is_paused(), this session
  106. unsigned long int points_skipped; // count of candidate points dropped by the skip_points warm-up (after boot or a fix gap), this session
  107. unsigned long int points_accepted; // count of candidate points that passed every reject check, this session; with filters enabled, points_accepted - points_written*AVG_COUNT is "averaged out, not yet flushed".
  108. // points_paused + points_skipped + rejected_count + points_accepted may fall a couple short of the true candidate-point total: the sliding window used for the distance/altitude spike checks needs a couple of points to refill after each reset (boot, or a fix gap) before it can even decide reject-vs-accept, and those few points aren't tallied anywhere.
  109. unsigned int hdop_x100; // HDOP * 100, from GSA
  110. unsigned char fix_type; // FIX_TYPE_* from GSA
  111. unsigned char speed; // km/h
  112. time_t time_start;
  113. time_t current_pause_start;
  114. time_t pause_time;
  115. float bat_volt;
  116. float bat_volt_min; // lowest battery voltage seen this session
  117. float temperature;
  118. unsigned temperature_ok:1;
  119. unsigned satellites_used:5;
  120. unsigned location_valid:2;
  121. unsigned dgps:1; /* GGA reports a DGPS-quality fix - the receiver's own doing, not CONFFLAG_ENABLE_SBAS (our SBAS-search request) */
  122. unsigned gps_initialized:2;
  123. unsigned gps_only:1;
  124. unsigned tracking_paused:1;
  125. unsigned tracking_auto_paused:1;
  126. unsigned open_new_file:1;
  127. unsigned sat_count_low:1;
  128. unsigned int global_error;
  129. unsigned char status;
  130. unsigned char display_state;
  131. unsigned char working_mode;
  132. unsigned char keypress; // bitmask of K_* (main.h); needs full width, not a bitfield bit
  133. };
  134. /* Auto-pause state (soft/autopause.c, compiled unmodified into both builds -
  135. * see soft/autopause.h). */
  136. struct auto_pause_s {
  137. unsigned char point_counter;
  138. unsigned long int prev_distance;
  139. unsigned char speed_counter;
  140. struct location_s pause_anchor; /* running average position while paused */
  141. unsigned int anchor_sample_count;
  142. unsigned char resume_high_history; /* bit history: was the anchor-distance/speed resume condition met */
  143. };