ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
cordic2c.c
Go to the documentation of this file.
1 
52 #include <stdio.h>
53 #include <stdlib.h>
54 #include <string.h>
55 #include <math.h>
56 #include <time.h>
57 #include <unistd.h>
58 #include <libgen.h>
59 
60 
96 // =================================================================
97 typedef int Cordic_T;
98 typedef unsigned int UCordic_T;
99 typedef double FCordic_T;
100 
101 // Number of bits in Cordic_T
102 #define Cordic_T_Bits (int)(sizeof(Cordic_T) << 3)
103 #define FCordic_T_Bits (int)(sizeof(FCordic_T) << 3)
104 // 3 integer bits 1 sign bin
105 #define Cordic_T_FractionBits (int)(Cordic_T_Bits - 3 - 1)
106 
107 #define Cordic_T_STR "typedef int Cordic_T;"
108 #define UCordic_T_STR "typedef unsigned int UCordic_T;"
109 #define FCordic_T_STR "typedef double FCordic_T;"
110 // =================================================================
111 
113 static Cordic_T X;
114 static Cordic_T Y;
115 static Cordic_T Z;
116 
117 #define Cordic_One (1UL << Cordic_T_FractionBits)
118 #define Cordic_K (Cordic_One * 0.6072529350088812561694)
119 #define Cordic_INVK (Cordic_One / 0.6072529350088812561694)
120 #define Cordic_KP (Cordic_One * 1.20749706776307212887772)
121 #define Cordic_INVKP (Cordic_One * 1/1.20749706776307212887772)
122 #define Cordic_HalfPI ((UCordic_T) (Cordic_One * M_PI_2) )
123 #define Cordic2FP(a) ( (double) (a) / (double) (Cordic_One))
124 #define FP2Cordic(a) ((Cordic_T) (Cordic_One * (a)))
125 
128 char *get_date()
129 {
130  int len;
131  char *ptr;
132  time_t timev;
133  timev = time(0);
134  ptr = asctime(localtime(&timev));
135  len = strlen(ptr);
136  ptr[len-1] = 0;
137  return(ptr);
138 }
139 
140 
150 void dump_tables(FILE *FO)
154 {
155  int i;
156  Cordic_T xx;
157 
158 // Dump types
159  fprintf(FO,"%s /* %u */\n", Cordic_T_STR, Cordic_T_Bits);
160  fprintf(FO,"%s /* %u */\n", UCordic_T_STR, Cordic_T_Bits);
161  fprintf(FO,"%s /* %u */\n", FCordic_T_STR, FCordic_T_Bits);
162  fprintf(FO,"#define Cordic_T_Bits %d\n", (int) Cordic_T_Bits);
163  fprintf(FO,"#define Cordic_T_FractionBits %d\n", (int) Cordic_T_FractionBits);
164 
165 
166 // Dump defines
167  fprintf(FO,"#define Cordic_One 0x%lx /* %.15le */\n",
168  (long) Cordic_One, Cordic2FP(Cordic_One));
169  fprintf(FO,"#define Cordic_K 0x%lx /* %.15le */\n",
170  (long) Cordic_K ,Cordic2FP(Cordic_K));
171  fprintf(FO,"#define Cordic_INVK 0x%lx /* %.15le */\n",
172  (long) Cordic_INVK ,Cordic2FP(Cordic_INVK));
173  fprintf(FO,"#define Cordic_KP 0x%lx /* %.15le */\n",
174  (long) Cordic_KP ,Cordic2FP(Cordic_KP));
175  fprintf(FO,"#define Cordic_INVKP 0x%lx /* %.15le */\n",
176  (long) Cordic_INVKP ,Cordic2FP(Cordic_INVKP));
177  fprintf(FO,"#define Cordic_HalfPI 0x%lx /* %.15le */\n",
178  (long) Cordic_HalfPI ,Cordic2FP(Cordic_HalfPI));
179  fprintf(FO,"#define Cordic2FP(a) ( (double) (a) / (double) (Cordic_One)) \n");
180  fprintf(FO,"#define FP2Cordic(a) ((Cordic_T) (Cordic_One * (a)))\n");
181 
182  fprintf(FO,"#ifdef CORDIC_TABLE\n");
183 // Dump normalize atan table
184  xx = Cordic_One;
185  for(i=0;i<Cordic_T_Bits;++i) {
186  v_atan[i] = Cordic_One * atan( Cordic2FP(xx)) * (2.0/M_PI);
187  xx >>= 1;
188  }
189  fprintf(FO,"static const Cordic_T v_atangrad[] = {\n");
190  for(i=0;i<=Cordic_T_FractionBits;++i) {
191  fprintf(FO,"\t0x%lx, /* %.8le */\n",
192  (long) v_atan[i], Cordic2FP(v_atan[i]));
193  }
194  fprintf(FO,"\t0\n};\n");
195 }
196 
200 void PrintXYZ (char *str)
201 {
202  printf("/* %s\n",str);
203  printf("X: %.8lf\n", Cordic2FP(X));
204  printf("Y: %.8lf\n", Cordic2FP(Y));
205  printf("Z: %.8lf\n", Cordic2FP(Z));
206  printf("*/\n");
207 }
208 
209 
217 {
218  int i;
219 
220  X = x;
221  Y = y;
222  Z = z;
223 
224  for (i = 0; i <= Cordic_T_FractionBits; ++i)
225  {
226  x = X >> i;
227  y = Y >> i;
228 
229  if(i < 14)
230  z = v_atan[i];
231  else
232  z >>= 1;
233 
234  if(Z >= 0) {
235  X -= y;
236  Y += x;
237  Z -= z;
238  }
239  else {
240  X += y;
241  Y -= x;
242  Z += z;
243  }
244  }
245 }
246 
260 {
261 
262  int i;
263 
264  X = x;
265  Y = y;
266  Z = z;
267 
268  for (i = 0; i < Cordic_T_FractionBits; ++i) {
269  x = X >> i;
270  y = Y >> i;
271  if(i < 14)
272  z = v_atan[i];
273  else
274  z >>= 1;
275  if (vecmode >= 0 && Y < vecmode || vecmode < 0 && Z >= 0) {
276  X -= y;
277  Y += x;
278  Z -= z;
279  }
280  else {
281  X += y;
282  Y -= x;
283  Z += z;
284  }
285  }
286 }
287 
293 {
294  Cordic_T x, y, z;
295 
296 
297  x = Cordic_K;
298  y = 0;
299  z = 0;
300 
301  int neg = 1;
302  if (a < 0) {
303  a = -a;
304  neg = 0;
305  }
306 
307 
308  cordit1(x, y, z, a);
309 
310  if (neg) Z = -Z;
311  return Z;
312 }
313 
314 
317 int main (int argc, char *argv[])
318 {
319  Cordic_T x1;
320  char str[256];
321  char *oname;
322  char *p;
323  double d;
324  int a;
325  int i;
326  FILE *FO;
327 
328  for(i=1; i<argc;++i)
329  {
330  p = argv[i];
331  if(*p != '-')
332  continue;
333  ++p;
334  if(*p == 'o')
335  {
336  oname = argv[++i];
337  }
338  }
339  if(!oname)
340  {
341  fprintf(stderr,"Usage: %s -o filename [-p]\n",argv[0]);
342  fprintf(stderr,"-o filename is CORDIC C table output file\n");
343  exit(1);
344  }
345 
346  FO = fopen(oname,"w");
347  if(FO == NULL)
348  {
349  fprintf(stderr,"Can not open: [%s]\n", oname);
350  exit (1);
351  }
352 
353 
354  fprintf(FO,"#ifndef _CORDIC_INC_H\n");
355  fprintf(FO,"#define _CORDIC_INC_H\n");
356 
357  fprintf(FO,"/**\n");
358  fprintf(FO," @file %s\n", basename(oname));
359  fprintf(FO," Generated by:[%s]\n", basename(argv[0]));
360  fprintf(FO," On: %s\n", get_date());
361  fprintf(FO," By Mike Gore 2015, Cordic C Table\n");
362  fprintf(FO,"*/\n");
363 
364 
365  dump_tables( FO);
366 
367  fprintf(FO,"#else // CORDIC_TABLE\n");
368  fprintf(FO,"extern const Cordic_T v_atangrad[];\n");
369  fprintf(FO,"#endif // CORDIC_TABLE\n");
370  fprintf(FO,"#endif // _CORDIC_INC_H\n");
371 
372  printf("// Verify CORDIC table \n");
373  for(d=0;d<=1;d+=.1)
374  {
375  x1 = FP2Cordic(d);
376  Circular (Cordic_K, 0L, x1);
377  a = (int) 100 * d + 0.000005; // rounding - 0.1 is not exact
378  sprintf(str,"%d Gradians", a);
379  PrintXYZ (str);
380  }
381  printf("// End of CORDIC verify\n");
382 
383  return (0);
384 }
#define Cordic_T_Bits
Definition: cordic2c.c:102
Cordic_T asinCordic(Cordic_T a)
Compute ArcSine (a) Only works for |a| < 0.98.
Definition: cordic2c.c:292
#define FCordic_T_Bits
Definition: cordic2c.c:103
#define UCordic_T_STR
Definition: cordic2c.c:108
#define Cordic_One
Definition: cordic2c.c:117
void cordit1(Cordic_T x, Cordic_T y, Cordic_T z, Cordic_T vecmode)
This is the circular method. One slight change from the other methods is the y < vecmode test...
Definition: cordic2c.c:259
MEMSPACE size_t WEAK_ATR strlen(const char *str)
String Length.
Definition: stringsup.c:146
int main(int argc, char *argv[])
Create C Cordic Tables and test the results.
Definition: cordic2c.c:317
void dump_tables(FILE *FO)
Create Cordic tables Normalize base number system to 1.0 == Cordic_One == PI/2 (90 degrees) Example ...
Definition: cordic2c.c:153
Common Linux/POSIX time functions.
#define Cordic_T_FractionBits
Definition: cordic2c.c:105
int16_t y[XYSTACK+2]
Definition: ili9341.c:372
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 Cordic_T_STR
Definition: cordic2c.c:107
FILE type structure.
Definition: posix.h:156
static Cordic_T X
Definition: cordic2c.c:113
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
#define Cordic_K
Definition: cordic2c.c:118
double FCordic_T
Definition: cordic2c.c:99
MEMSPACE time_t time(time_t *t)
Return second from epoch - POSIX function.
Definition: time.c:843
#define Cordic_KP
Definition: cordic2c.c:120
#define FP2Cordic(a)
Definition: cordic2c.c:124
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
int Cordic_T
Definition: cordic2c_inc.h:9
void Circular(Cordic_T x, Cordic_T y, Cordic_T z)
Main Cordic routine - used for basic trig and vector rotations.
Definition: cordic2c.c:216
#define Cordic2FP(a)
Definition: cordic2c.c:123
MEMSPACE char * asctime(tm_t *t)
Convert tm_t *t structure into POSIX asctime() ASCII string.
Definition: time.c:394
#define FCordic_T_STR
Definition: cordic2c.c:109
int Cordic_T
cordicC.c – J. Pitts Jarvis, III cordicC.c computes CORDIC constants and exercises the basic algorit...
Definition: cordic2c.c:97
static Cordic_T Z
Definition: cordic2c.c:115
#define Cordic_HalfPI
Definition: cordic2c.c:122
#define Cordic_INVK
Definition: cordic2c.c:119
static Cordic_T Y
Definition: cordic2c.c:114
MEMSPACE int printf(const char *format,...)
char * get_date()
Get the current date in a string.
Definition: cordic2c.c:128
#define stderr
Definition: posix.h:271
MEMSPACE char * basename(char *str)
POSIX Basename of filename.
Definition: posix.c:1446
#define L(x)
Definition: cal_dex.c:54
#define Cordic_INVKP
Definition: cordic2c.c:121
static Cordic_T v_atan[Cordic_T_Bits+1]
Definition: cordic2c.c:112
#define sprintf(s, format, args...)
Definition: cpu.h:85
unsigned int UCordic_T
Definition: cordic2c.c:98
void PrintXYZ(char *str)
Display X,Y,Z as floating point.
Definition: cordic2c.c:200