ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
earth2wireframe.c
Go to the documentation of this file.
1 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <math.h>
33 #include <time.h>
34 #include <unistd.h>
35 #include <libgen.h>
36 
37 
38 #define TEST
39 typedef unsigned char uint8_t;
40 typedef signed char int8_t;
41 typedef unsigned short uint16_t;
42 typedef unsigned int uint32_t;
43 typedef int int32_t;
44 
45 #define MEMSPACE
46 #include "../wire/wire_types.h"
47 #include "../cordic/cordic2c_inc.h"
48 #include "../cordic/cordic.h"
49 
53 double deg2rad(double deg)
54 {
55  return (deg * 0.0174532925);
56 }
57 
60 char *get_date()
61 {
62  int len;
63  char *ptr;
64  time_t timev;
65  timev = time(0);
66  ptr = asctime(localtime(&timev));
67  len = strlen(ptr);
68  ptr[len-1] = 0;
69  return(ptr);
70 }
71 
72 
77 int main(int argc, char *argv[])
78 {
79  double xx,yy,zz;
80  double scale;
81  double lat,lon;
82  double lat_rad,lon_rad;
83  short int x,y,z;
84  int i;
85  int len;
86  int prototype = 1;
87  char *p, *name, *oname;
88  FILE *FI,*FO;
89  char buff[256];
90  char datestr[256];
91 
92  char cwd[512];
93 
94  name = NULL;
95 
96  getcwd(cwd, 510);
97 
98  for(i=1; i<argc;++i)
99  {
100  p = argv[i];
101  if(*p != '-')
102  continue;
103  ++p;
104  if(*p == 'f')
105  {
106  name = argv[++i];
107  }
108  if(*p == 'o')
109  {
110  oname = argv[++i];
111  }
112  }
113  if(!name || !oname)
114  {
115  fprintf(stderr,"Usage: %s -f filename [-p]\n",argv[0]);
116  fprintf(stderr,"-f filename is the lot,lat dataset pairs\n");
117  exit(1);
118  }
119  if(strcmp(name,oname) == 0)
120  {
121  fprintf(stderr,"sorce and destination file can not be the same\n");
122  exit(1);
123  }
124  FI = fopen(name,"r");
125  if(FI == NULL)
126  {
127  fprintf(stderr,"Can not open: [%s]\n", name);
128  exit (1);
129  }
130 
131  FO = fopen(oname,"w");
132  if(FO == NULL)
133  {
134  fprintf(stderr,"Can not open: [%s]\n", oname);
135  exit (1);
136  }
137  fprintf(FO,"#ifndef _EARTH_DATA_H_\n");
138  fprintf(FO,"#define _EARTH_DATA_H_\n");
139  fprintf(FO,"/**\n");
140  fprintf(FO," @file %s\n", basename(oname));
141  fprintf(FO," Generated by:[%s/%s]\n", cwd, basename(argv[0]));
142  fprintf(FO," On: %s\n", get_date());
143  fprintf(FO," By Mike Gore 2015, Earth Wireframe C Table\n");
144  fprintf(FO," Source: %s\n", name);
145  fprintf(FO,"*/\n");
146 
147  fprintf(FO,"MEMSPACE_RO wire_p earth_data[] = {\n");
148 
149  FI = fopen(name,"r");
150  if(FI == NULL)
151  {
152  fprintf(stderr,"Can not open: [%s]\n", name);
153  exit (1);
154  }
155 
156  while( fgets(buff, sizeof(buff)-2, FI))
157  {
158  len = strlen(buff);
159  // remove any white space characters at the end of the line
160  while(len && buff[len-1] < ' ')
161  buff[--len] = 0;
162 
163  if(!len)
164  {
165  if(lon == WIRE_SEP)
166  continue;
167  }
168  if(*buff == '#')
169  {
170  if(lon == WIRE_SEP)
171  continue;
172  fprintf(FO,"/* %s */\n", buff);
173  }
174 
175  lon = WIRE_SEP;
176  lat = WIRE_SEP;
177 
178  // degrees
179  if(len)
180  sscanf(buff, "%lf %lf", &lon, &lat);
181 
182  // radians
183  lon_rad = deg2rad(lon);
184  lat_rad = deg2rad(lat);
185 
186  //printf("%f, %f\n", lon,lat);
187  //printf("%f, %f\n", lon_rad,lat_rad);
188  //fflush(stdout);
189 
190  if(!len || lon == WIRE_SEP || lat == WIRE_SEP)
191  {
192  // this point is the end of a vector - not plotted
193  x = WIRE_SEP;
194  y = WIRE_SEP;
195  z = WIRE_SEP;
196  fprintf(FO,"\t{%d,%d,%d}, /* sep */\n",
197  (int)x ,(int)y ,(int)z
198  );
199  //printf("\t{%f,%f,%f},\n", x,y,z);
200  }
201  else
202  {
203  // WIRE_HALF gives scale factor of 1 (+/-0.5)
204  x = (cos(lat_rad) * cos(lon_rad)) * WIRE_HALF;
205  y = (cos(lat_rad) * sin(lon_rad)) * WIRE_HALF;
206  z = sin(lat_rad) * WIRE_HALF;
207  // reverse is
208  // lat = asin(z / R)
209  // lon = atan2(y, x)
210  fprintf(FO,"\t{%d,%d,%d}, /* %f, %f */\n",
211  (int)x ,(int)y ,(int)z ,
212  lon, lat);
213  //printf("\t{%f,%f,%f},\n", x,y,z);
214  }
215  }
216  fclose(FI);
217  // this point is the end of a vector - not plotted
218  x = WIRE_SEP;
219  y = WIRE_SEP;
220  z = WIRE_SEP;
221  fprintf(FO,"\t{%d,%d,%d}, /* sep */\n",
222  (int)x ,(int)y ,(int)z
223  );
224  // this point is the end of the data set
225  x = WIRE_END;
226  y = WIRE_END;
227  z = WIRE_END;
228  fprintf(FO,"\t{%d,%d,%d}, /* END */\n",
229  (int)x ,(int)y ,(int)z
230  );
231  fprintf(FO,"};\n");
232  fprintf(FO,"#else /* _EARTH_DATA_H */\n");
233  fprintf(FO," extern MEMSPACE_RO wire_p earth_data[];\n");
234  fprintf(FO,"#endif /* _EARTH_DATA_H_ */\n");
235  fclose(FO);
236  return(0);
237 }
238 
int int32_t
MEMSPACE int WEAK_ATR strcmp(const char *str, const char *pat)
Compare two strings.
Definition: stringsup.c:362
MEMSPACE size_t WEAK_ATR strlen(const char *str)
String Length.
Definition: stringsup.c:146
unsigned int uint32_t
Common Linux/POSIX time functions.
#define WIRE_HALF
Definition: wire_types.h:41
int16_t y[XYSTACK+2]
Definition: ili9341.c:372
MEMSPACE char * fgets(char *str, int size, FILE *stream)
get a string from stdin See fdevopen() sets stream->put get for TTY devices
Definition: posix.c:420
MEMSPACE FILE * fopen(const char *path, const char *mode)
POSIX Open a file with path name and ascii file mode string.
Definition: posix.c:782
#define WIRE_SEP
Definition: wire_types.h:42
FILE type structure.
Definition: posix.h:156
MEMSPACE int fprintf(FILE *fp, const char *format,...)
fprintf function Example user defined printf function using fputc for I/O This method allows I/O to d...
Definition: posix.c:2484
MEMSPACE time_t time(time_t *t)
Return second from epoch - POSIX function.
Definition: time.c:843
int16_t x[XYSTACK+2]
Definition: ili9341.c:371
uint32_t time_t
type of EPOCH result.
Definition: time.h:35
#define NULL
Definition: cpu.h:55
MEMSPACE tm_t * localtime(time_t *tp)
Convert POSIX epoch time_t *tp into POSIX tm_t *result.
Definition: time.c:522
unsigned char uint8_t
signed char int8_t
MEMSPACE char * asctime(tm_t *t)
Convert tm_t *t structure into POSIX asctime() ASCII string.
Definition: time.c:394
MEMSPACE int fclose(FILE *stream)
POSIX close a file stream.
Definition: posix.c:1239
double deg2rad(double deg)
Convert of Radians.
int sscanf(const char *strp, const char *fmt,...)
char * get_date()
Get the current date in a string.
unsigned short uint16_t
#define stderr
Definition: posix.h:271
int main(int argc, char *argv[])
Convert earth wireframe data to C structure Usage: s -f source -o result ",argv[0]); -f source is the...
MEMSPACE char * basename(char *str)
POSIX Basename of filename.
Definition: posix.c:1446
MEMSPACE char * getcwd(char *pathname, int len)
POSIX get current working directory.
Definition: posix.c:1596
#define WIRE_END
Definition: wire_types.h:43