common.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  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. #define REJECT_REASON_QUALITY 4 /* low satellite count, poor HDOP, or non-3D fix */
  43. /* System.conf.flags bit indices, read/set via get_flag(index)/set_flag(index,
  44. * val) - both sides index the same way (a bit position within a flags byte
  45. * array), not a pre-shifted bitmask, so these constants and the get_flag()
  46. * call sites are interchangeable between builds. */
  47. #define CONFFLAG_DISABLE_FILTERS 0
  48. #define CONFFLAG_ENABLE_SBAS 1
  49. #define CONFFLAG_LOGGING_AFTER_BOOT 2
  50. #define CONFFLAG_AUTO_PAUSE 3
  51. #define CONFFLAG_RESET_ON_NEW_FILE 4
  52. #define CONFFLAG_VERBOSE_LOG 5
  53. #define CONFFLAG_LAST 5
  54. struct config_s {
  55. union {
  56. unsigned char conf_u8[16];
  57. struct {
  58. unsigned char gnss_mode; // 0
  59. unsigned char skip_points; // 1
  60. unsigned char auto_pause_time; // 2
  61. unsigned char auto_pause_dist; // 3
  62. unsigned char min_sats; // 4
  63. unsigned char auto_pause_speed; // 5
  64. unsigned char uart_log_mode; // 6
  65. };
  66. };
  67. unsigned char flags[4];
  68. };
  69. struct location_s {
  70. float lon;
  71. float lat;
  72. float alt;
  73. time_t time;
  74. };
  75. #ifndef PC_BUILD
  76. /* Real hardware-timer-backed countdowns (set_timer()/timer_expired() in
  77. * soft/main.h), driven by an ISR via atomic_set_uint()/atomic_get_uint().
  78. * Embedded-only: the PC build's set_timer()/timer_expired() are plain no-op
  79. * functions that never reference System.timers, so struct system_s doesn't
  80. * carry it there - dead weight, and a stray cli()/sei() dependency, for a
  81. * build with no such ISR. */
  82. struct timers {
  83. unsigned int owire;
  84. unsigned int beep;
  85. unsigned int recv_timeout;
  86. unsigned int status_log;
  87. unsigned int lcd;
  88. unsigned int backlight;
  89. };
  90. #endif
  91. struct system_s {
  92. #ifndef PC_BUILD
  93. struct timers timers;
  94. #endif
  95. struct config_s conf;
  96. unsigned long int distance; // cm
  97. unsigned long int elevation_gain; // dm (decimeters, 0.1m resolution); filtered (dead-band+smoothed)
  98. unsigned long int elevation_loss; // dm; filtered (dead-band+smoothed)
  99. unsigned long int elevation_gain_raw; // dm; unfiltered, for log comparison only
  100. unsigned long int elevation_loss_raw; // dm; unfiltered, for log comparison only
  101. unsigned long int alt_max; // dm; highest smoothed altitude seen this session
  102. unsigned long int speed_accum_x100; // sum of instantaneous NMEA speed samples (km/h * 100) while not paused
  103. unsigned long int speed_sample_count; // number of samples in speed_accum_x100
  104. unsigned long int points_written; // count of accepted/written track points this session
  105. 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)
  106. unsigned long int points_paused; // count of candidate points seen while tracking_is_paused(), this session
  107. unsigned long int points_skipped; // count of candidate points dropped by the skip_points warm-up (after boot or a fix gap), this session
  108. 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".
  109. // 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.
  110. unsigned int hdop_x100; // HDOP * 100, from GSA
  111. unsigned char fix_type; // FIX_TYPE_* from GSA
  112. unsigned int speed_x10; // instantaneous NMEA speed, km/h * 10 - see nmea.c's speed_sample_process()
  113. // for why whole km/h is too coarse for the auto-pause speed threshold
  114. time_t time_start;
  115. time_t current_pause_start;
  116. time_t pause_time;
  117. float bat_volt;
  118. float bat_volt_min; // lowest battery voltage seen this session
  119. float temperature;
  120. unsigned temperature_ok:1;
  121. unsigned satellites_used:5;
  122. unsigned location_valid:2;
  123. unsigned dgps:1; /* GGA reports a DGPS-quality fix - the receiver's own doing, not CONFFLAG_ENABLE_SBAS (our SBAS-search request) */
  124. unsigned gps_initialized:2;
  125. unsigned gps_only:1;
  126. unsigned tracking_paused:1;
  127. unsigned tracking_auto_paused:1;
  128. unsigned open_new_file:1;
  129. unsigned sat_count_low:1;
  130. unsigned int global_error;
  131. unsigned char status;
  132. unsigned char display_state;
  133. unsigned char working_mode;
  134. unsigned char keypress; // bitmask of K_* (main.h); needs full width, not a bitfield bit
  135. };
  136. /* Auto-pause state (soft/autopause.c, compiled unmodified into both builds -
  137. * see soft/autopause.h). */
  138. struct auto_pause_s {
  139. unsigned char point_counter;
  140. unsigned long int prev_distance;
  141. unsigned char speed_counter;
  142. struct location_s pause_anchor; /* running average position while paused */
  143. unsigned int anchor_sample_count;
  144. unsigned char resume_high_history; /* bit history: was the anchor-distance/speed resume condition met */
  145. struct location_s prev_location; /* last sample seen while paused, for step-to-step bearing */
  146. unsigned char prev_location_valid:1;
  147. unsigned char prev_bearing_valid:1;
  148. float prev_bearing;
  149. unsigned char heading_consistent_history; /* bit history: did this step's bearing agree with the last */
  150. };