main.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. /*
  2. * main.h - PC Compatible Version
  3. * Central header for GPS Test Tool - handles PC/embedded switches
  4. */
  5. #pragma once
  6. #ifdef PC_BUILD
  7. /* ========== PC BUILD CONFIGURATION ========== */
  8. /* Standard C headers - all includes centralized here */
  9. #include <stdio.h>
  10. #include <stdint.h>
  11. #include <stdlib.h>
  12. #include <stddef.h>
  13. #include <string.h>
  14. #include <time.h>
  15. #include <math.h>
  16. #include <stdarg.h>
  17. #include <ctype.h>
  18. /* Prevent AVR headers from being included */
  19. #define _AVR_PGMSPACE_H_ 1
  20. #define _AVR_IO_H_ 1
  21. #define _AVR_WDT_H_ 1
  22. #define _AVR_SLEEP_H_ 1
  23. #define _AVR_INTERRUPT_H_ 1
  24. #define _AVR_EEPROM_H_ 1
  25. /* PROGMEM/Flash support - no-op on PC */
  26. #define PROGMEM
  27. #define __flash
  28. #define PSTR(s) (s)
  29. #define strcpy_P strcpy
  30. #define strcat_P strcat
  31. #define strcmp_P strcmp
  32. #define pgm_read_byte(addr) (*(addr))
  33. /* Stub LCD type to bypass error */
  34. #define LCD_ALNUM
  35. /* Interrupt stubs */
  36. #define cli()
  37. #define sei()
  38. /* FatFS types */
  39. typedef struct {
  40. FILE *fp;
  41. char filename[256];
  42. } FIL;
  43. typedef uint32_t DWORD;
  44. typedef unsigned int UINT;
  45. typedef unsigned char BYTE;
  46. typedef char TCHAR;
  47. typedef unsigned char FRESULT;
  48. /* FatFS functions */
  49. #define FA_WRITE 0x02
  50. #define FA_OPEN_ALWAYS 0x10
  51. static inline FRESULT f_open(FIL *file, const TCHAR *path, BYTE mode) {
  52. (void)mode;
  53. file->fp = fopen(path, "w");
  54. return file->fp ? 0 : 1;
  55. }
  56. static inline FRESULT f_write(FIL *file, const void *buf, UINT len, UINT *written) {
  57. if (!file->fp) return 1;
  58. *written = fwrite(buf, 1, len, file->fp);
  59. return (*written == len) ? 0 : 1;
  60. }
  61. static inline FRESULT f_close(FIL *file) {
  62. if (file->fp) {
  63. fclose(file->fp);
  64. file->fp = NULL;
  65. return 0;
  66. }
  67. return 1;
  68. }
  69. /* xprintf functions */
  70. extern FILE *debug_output;
  71. static inline void xputs(const char* str) {
  72. FILE *out = debug_output ? debug_output : stdout;
  73. fputs(str, out);
  74. }
  75. static inline void xputs_P(const char* str) {
  76. FILE *out = debug_output ? debug_output : stdout;
  77. fputs(str, out);
  78. }
  79. static inline void xprintf(const char* fmt, ...) {
  80. FILE *out = debug_output ? debug_output : stdout;
  81. va_list ap;
  82. va_start(ap, fmt);
  83. vfprintf(out, fmt, ap);
  84. va_end(ap);
  85. }
  86. static inline void xsprintf(char* buff, const char* fmt, ...) {
  87. va_list ap;
  88. va_start(ap, fmt);
  89. vsprintf(buff, fmt, ap);
  90. va_end(ap);
  91. }
  92. static inline void xfprintf(void (*put)(int), const char* fmt, ...) {
  93. /* Stub for PC - just ignore output to UART */
  94. (void)put;
  95. (void)fmt;
  96. }
  97. /* xatof - Parse float from string (used by NMEA parser) */
  98. static inline void xatof(const char **str, double *result) {
  99. char *end;
  100. *result = strtod(*str, &end);
  101. *str = end;
  102. }
  103. /* Math constants */
  104. #ifndef M_PI
  105. #define M_PI 3.14159265358979323846
  106. #endif
  107. #else
  108. /* ========== EMBEDDED BUILD CONFIGURATION ========== */
  109. #define __PROG_TYPES_COMPAT__
  110. #include <inttypes.h>
  111. #include <avr/pgmspace.h>
  112. #include <avr/interrupt.h>
  113. #include "stime.h"
  114. #include "expander.h"
  115. #include "settings.h"
  116. #if defined(LCD_GRAPHIC)
  117. #include "UC1601S-I2C.h"
  118. #elif defined(LCD_ALNUM)
  119. #include "HD44780-I2C.h"
  120. #else
  121. #error No LCD type defined
  122. #endif
  123. #endif /* PC_BUILD */
  124. /* ========== COMMON DEFINITIONS (both PC and embedded) ========== */
  125. #define IVT_SYNC 180
  126. #define POWER_SW_TIME 300
  127. #define BACKLIGHT_TIME 10000
  128. #define VI_LVL 3.1
  129. #define VI_LVH 3.4
  130. #define VI_MULT (3.3 / 6.6 / 2.495 * 1024)
  131. /* Configuration flags */
  132. #define CONFFLAG_DISABLE_FILTERS 0x01
  133. #define CONFFLAG_ENABLE_SBAS 0x02
  134. #define CONFFLAG_AUTO_PAUSE 0x04
  135. /* GNSS modes */
  136. #define GNSS_MODE_GPS_GLONASS_GALILEO 0
  137. #define GNSS_MODE_GPS 1
  138. #define GNSS_MODE_GPS_GALILEO 2
  139. #define GNSS_MODE_GALILEO 3
  140. #define GNSS_MODE_GPS_BEIDOU 4
  141. #define GNSS_MODE_BEIDOU 5
  142. /* GPS initialization states */
  143. #define GPS_INIT_NOT_INITIALIZED 0
  144. #define GPS_INIT_QUERY_SENT 1
  145. /* Location validity states */
  146. #define LOC_INVALID 0
  147. #define LOC_VALID_NEW 1
  148. #define LOC_VALID 2
  149. /* Configuration structure */
  150. struct config_s {
  151. unsigned char skip_points;
  152. unsigned char auto_pause_dist;
  153. unsigned char gnss_mode;
  154. unsigned char min_sat;
  155. unsigned char min_sats; /* Alias for compatibility */
  156. };
  157. /* System structure */
  158. struct system_s {
  159. struct config_s conf;
  160. unsigned long int distance; /* cm */
  161. unsigned long int elevation_gain; /* dm (decimeters, 0.1m resolution) */
  162. unsigned long int elevation_loss; /* dm (decimeters, 0.1m resolution) */
  163. unsigned char speed; /* km/h */
  164. time_t time_start;
  165. time_t current_pause_start;
  166. time_t pause_time;
  167. unsigned tracking_paused:1;
  168. unsigned tracking_auto_paused:1;
  169. unsigned gps_initialized:2;
  170. unsigned gps_only:1;
  171. unsigned keypress:1;
  172. unsigned location_valid:2;
  173. unsigned sat_count_low:1;
  174. unsigned sbas:1;
  175. unsigned satellites_used:5;
  176. };
  177. /* Location structure */
  178. struct location_s {
  179. float lon;
  180. float lat;
  181. float alt;
  182. time_t time;
  183. };
  184. /* Global variables */
  185. extern volatile struct system_s System;
  186. extern struct location_s location;
  187. extern time_t utc;
  188. /* Configuration flags */
  189. extern unsigned char config_flags;
  190. static inline unsigned char get_flag(unsigned char flag) {
  191. return (config_flags & flag) ? 1 : 0;
  192. }
  193. /* Time functions */
  194. char *get_iso_time(time_t time, unsigned char local);
  195. void iso_time_to_filename(char *time);
  196. /* GPS and distance functions */
  197. void gps_initialize(void);
  198. void add_distance(float dist);
  199. void add_elevation(float ele_change);
  200. /* Stub functions for PC build */
  201. #ifdef PC_BUILD
  202. static inline void set_timer(int timer, int val) { (void)timer; (void)val; }
  203. static inline int timer_expired(int timer) { (void)timer; return 0; }
  204. static inline void wdt_reset(void) {}
  205. static inline void sleep(void) {}
  206. static inline int uart0_test(void) { return 0; }
  207. static inline unsigned char uart0_get(void) { return 0; }
  208. static inline void uart0_put(char c) { (void)c; }
  209. static inline void uart1_put(char c) { (void)c; }
  210. static unsigned char FLAGS = 0;
  211. #define F_LVD 0x01
  212. #define F_POWEROFF 0x02
  213. #define F_GPSOK 0x04
  214. #define recv_timeout 0
  215. #endif /* PC_BUILD stubs */