/* * main.h - PC Compatible Version * Central header for GPS Test Tool - handles PC/embedded switches */ #pragma once #ifdef PC_BUILD /* ========== PC BUILD CONFIGURATION ========== */ /* Standard C headers - all includes centralized here */ #include #include #include #include #include #include #include #include #include /* Prevent AVR headers from being included */ #define _AVR_PGMSPACE_H_ 1 #define _AVR_IO_H_ 1 #define _AVR_WDT_H_ 1 #define _AVR_SLEEP_H_ 1 #define _AVR_INTERRUPT_H_ 1 #define _AVR_EEPROM_H_ 1 /* PROGMEM/Flash support - no-op on PC */ #define PROGMEM #define __flash #define PSTR(s) (s) #define strcpy_P strcpy #define strcat_P strcat #define strcmp_P strcmp #define pgm_read_byte(addr) (*(addr)) /* Stub LCD type to bypass error */ #define LCD_ALNUM /* Interrupt stubs */ #define cli() #define sei() /* FatFS types */ typedef struct { FILE *fp; char filename[256]; } FIL; typedef uint32_t DWORD; typedef unsigned int UINT; typedef unsigned char BYTE; typedef char TCHAR; typedef unsigned char FRESULT; /* FatFS functions */ #define FA_WRITE 0x02 #define FA_OPEN_ALWAYS 0x10 static inline FRESULT f_open(FIL *file, const TCHAR *path, BYTE mode) { (void)mode; file->fp = fopen(path, "w"); return file->fp ? 0 : 1; } static inline FRESULT f_write(FIL *file, const void *buf, UINT len, UINT *written) { if (!file->fp) return 1; *written = fwrite(buf, 1, len, file->fp); return (*written == len) ? 0 : 1; } static inline FRESULT f_close(FIL *file) { if (file->fp) { fclose(file->fp); file->fp = NULL; return 0; } return 1; } /* xprintf functions */ extern FILE *debug_output; static inline void xputs(const char* str) { FILE *out = debug_output ? debug_output : stdout; fputs(str, out); } static inline void xputs_P(const char* str) { FILE *out = debug_output ? debug_output : stdout; fputs(str, out); } static inline void xprintf(const char* fmt, ...) { FILE *out = debug_output ? debug_output : stdout; va_list ap; va_start(ap, fmt); vfprintf(out, fmt, ap); va_end(ap); } static inline void xsprintf(char* buff, const char* fmt, ...) { va_list ap; va_start(ap, fmt); vsprintf(buff, fmt, ap); va_end(ap); } static inline void xfprintf(void (*put)(int), const char* fmt, ...) { /* Stub for PC - just ignore output to UART */ (void)put; (void)fmt; } /* xatof - Parse float from string (used by NMEA parser) */ static inline void xatof(const char **str, double *result) { char *end; *result = strtod(*str, &end); *str = end; } /* Math constants */ #ifndef M_PI #define M_PI 3.14159265358979323846 #endif #else /* ========== EMBEDDED BUILD CONFIGURATION ========== */ #define __PROG_TYPES_COMPAT__ #include #include #include #include "stime.h" #include "expander.h" #include "settings.h" #if defined(LCD_GRAPHIC) #include "UC1601S-I2C.h" #elif defined(LCD_ALNUM) #include "HD44780-I2C.h" #else #error No LCD type defined #endif #endif /* PC_BUILD */ /* ========== COMMON DEFINITIONS (both PC and embedded) ========== */ #define IVT_SYNC 180 #define POWER_SW_TIME 300 #define BACKLIGHT_TIME 10000 #define VI_LVL 3.1 #define VI_LVH 3.4 #define VI_MULT (3.3 / 6.6 / 2.495 * 1024) /* Configuration flags */ #define CONFFLAG_DISABLE_FILTERS 0x01 #define CONFFLAG_ENABLE_SBAS 0x02 #define CONFFLAG_AUTO_PAUSE 0x04 #define CONFFLAG_VERBOSE_LOG 0x08 #define CONFFLAG_RESET_ON_NEW_FILE 0x10 /* Reject reasons (see soft/syslog.h) */ #define REJECT_REASON_KALMAN 0 #define REJECT_REASON_DISTDIFF 1 #define REJECT_REASON_MINDIST 2 /* UART log mode (see soft/settings.h) */ #define UART_LOG_MODE_MIXED 0 #define UART_LOG_MODE_NMEA 1 #define UART_LOG_MODE_SYSTEM 2 /* Fix type (see soft/main.h) */ #define FIX_TYPE_UNKNOWN 0 #define FIX_TYPE_2D 2 #define FIX_TYPE_3D 3 /* GNSS modes */ #define GNSS_MODE_GPS_GLONASS_GALILEO 0 #define GNSS_MODE_GPS 1 #define GNSS_MODE_GPS_GALILEO 2 #define GNSS_MODE_GALILEO 3 #define GNSS_MODE_GPS_BEIDOU 4 #define GNSS_MODE_BEIDOU 5 /* GPS initialization states */ #define GPS_INIT_NOT_INITIALIZED 0 #define GPS_INIT_QUERY_SENT 1 /* Location validity states */ #define LOC_INVALID 0 #define LOC_VALID_NEW 1 #define LOC_VALID 2 /* Configuration structure */ struct config_s { unsigned char skip_points; unsigned char auto_pause_dist; unsigned char auto_pause_time; unsigned char auto_pause_speed; unsigned char gnss_mode; unsigned char min_sat; unsigned char min_sats; /* Alias for compatibility */ unsigned char uart_log_mode; }; /* System structure */ struct system_s { struct config_s conf; unsigned long int distance; /* cm */ unsigned long int elevation_gain; /* dm (decimeters, 0.1m resolution); filtered */ unsigned long int elevation_loss; /* dm; filtered */ unsigned long int elevation_gain_raw; /* dm; unfiltered, for comparison only */ unsigned long int elevation_loss_raw; /* dm; unfiltered, for comparison only */ unsigned long int alt_max; /* dm; highest smoothed altitude seen */ unsigned long int points_written; unsigned long int rejected_count; unsigned long int speed_accum_x100; unsigned long int speed_sample_count; unsigned int hdop_x100; unsigned char fix_type; unsigned char speed; /* km/h */ float bat_volt; float bat_volt_min; float temperature; unsigned char temperature_ok; time_t time_start; time_t current_pause_start; time_t pause_time; unsigned tracking_paused:1; unsigned tracking_auto_paused:1; unsigned gps_initialized:2; unsigned gps_only:1; unsigned keypress:1; unsigned location_valid:2; unsigned sat_count_low:1; unsigned sbas:1; unsigned satellites_used:5; }; /* Location structure */ struct location_s { float lon; float lat; float alt; time_t time; }; /* Must match soft/autopause.h exactly: soft/autopause.c is compiled into * this tool too (see autopause_wrapper.c). */ struct auto_pause_s { unsigned char point_counter; unsigned long int prev_distance; unsigned char speed_counter; struct location_s pause_anchor; unsigned int anchor_sample_count; unsigned char resume_high_history; }; extern struct auto_pause_s auto_pause; /* Global variables */ extern volatile struct system_s System; extern struct location_s location; extern time_t utc; /* Configuration flags */ extern unsigned char config_flags; static inline unsigned char get_flag(unsigned char flag) { return (config_flags & flag) ? 1 : 0; } /* soft/main.c's helpers, reimplemented here: soft/main.c itself isn't part * of this PC build (this test tool has its own main()), but soft/syslog.c * calls into them. */ unsigned char is_paused(void); /* soft/gpx.c, via gpx_wrapper.c */ static inline time_t get_pause_time(void) { time_t res = System.pause_time; if (System.current_pause_start < System.time_start) System.current_pause_start = System.time_start; if (is_paused() && System.current_pause_start) res += utc - System.current_pause_start; return res; } static inline unsigned int get_logging_time(void) { if (!utc || !System.time_start) return 0; return utc - System.time_start - get_pause_time(); } static inline unsigned int get_avg_speed_x100(void) { if (!System.speed_sample_count) return 0; return System.speed_accum_x100 / System.speed_sample_count; } static inline unsigned int get_dist_avg_speed_x100(void) { unsigned int moving = get_logging_time(); if (!moving) return 0; return (unsigned int)((unsigned long int)System.distance * 36UL / (10UL * moving)); } /* Time functions */ char *get_iso_time(time_t time, unsigned char local); void iso_time_to_filename(char *time); /* GPS and distance functions */ void gps_initialize(void); void add_distance(float dist); void add_elevation(float ele_change); float distance(struct location_s *pos1, struct location_s *pos2); /* The structured system log (soft/syslog.c) is compiled into this test tool * too (see syslog_wrapper.c), so its real log_reject()/log_jump()/log_prefix() * etc. are used here, not stubs - see gnss_names/uart_log_mode_names/ * reset_cause below for what it needs that isn't otherwise part of this * PC build. */ void log_prefix(void); void log_reject(unsigned char reason); void log_reject_flush(void); void log_jump(unsigned char is_alt, float meters); void log_boot_record(void); void log_status_line(void); void log_fix_event(void); void log_fix_lost(void); void log_sbas_transition(void); void log_session_summary(unsigned char final); void log_pause_event(unsigned char started); void gps_powered_on(void); /* Auto-pause is real logic (soft/autopause.c), not stubbed - it directly * gates System.distance/elevation_gain/elevation_loss, which is a major part * of what this tool's output looks like. It calls beep(), which is * meaningless on PC and stubbed out here. */ void auto_pause_process(void); unsigned char gps_fix_trustworthy(void); static inline void beep(unsigned int len, unsigned char cnt) { (void)len; (void)cnt; } extern volatile unsigned char reset_cause; extern __flash const char *gnss_names[]; extern __flash const char *uart_log_mode_names[]; /* MCUSR reset-cause bits and _BV(): meaningless on PC (reset_cause is always * 0 here, so log_boot_record() just reports "UNKNOWN"), but syslog.c * references them unconditionally. */ #define _BV(x) (1 << (x)) #define WDRF 3 #define BORF 2 #define EXTRF 1 #define PORF 0 /* soft/main.c's SD I/O timing and stack-headroom instrumentation isn't * meaningful for the PC build's stdio-based file stubs; stub it out. */ static inline unsigned long int get_uptime_ms(void) { return 0; } static inline void io_mark(unsigned long int start_ms) { (void)start_ms; } static inline unsigned int io_get_and_reset_max(void) { return 0; } static inline unsigned int get_stack_free_bytes(void) { return 0; } /* Stub functions for PC build */ #ifdef PC_BUILD static inline void set_timer(int timer, int val) { (void)timer; (void)val; } static inline int timer_expired(int timer) { (void)timer; return 0; } static inline void wdt_reset(void) {} static inline void sleep(void) {} static inline int uart0_test(void) { return 0; } static inline unsigned char uart0_get(void) { return 0; } static inline void uart0_put(char c) { (void)c; } static inline void uart1_put(char c) { (void)c; } /* Must have external linkage (defined once, in main.c/test_malformed.c): * nmea.c (compiled via nmea_actual_wrapper.c, a separate translation unit) * sets F_GPSOK here, and the driver loop needs to see that same flag. A * `static` FLAGS in this shared header would give every .c file its own * private copy, silently discarding those updates. */ extern unsigned char FLAGS; #define F_LVD 0x01 #define F_POWEROFF 0x02 #define F_GPSOK 0x04 #define recv_timeout 0 #endif /* PC_BUILD stubs */