ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
timetests.c
Go to the documentation of this file.
1 
27 #ifdef ESP8266
28 #include "user_config.h"
29 #include "fatfs.h"
30 #include "printf/mathio.h"
31 #include "lib/time.h"
32 #include "lib/timer.h"
33 #ifdef RTC_SUPPORT
34 #include "lib/rtc.h"
35 #endif
36 
37 #else
38 #include <stdlib.h>
39 #include <string.h>
40 #include <time.h>
41 #endif
42 
43 static unsigned int __seed;
44 
46 void mysrand(int seed)
47 {
48  __seed = seed;
49 }
50 
52 int myrand(int start, int end)
53 {
54  long k,s;
55 
56 
57  int val;
58  double fval,begin,span;
59 
60  if(end > start)
61  {
62  begin = (double) start;
63  span = (double) (end - start);
64  }
65  else
66  {
67  span = (double) (start - end);
68  begin = (double) end;
69  }
70 
71  s = (long)__seed;
72 
73  if (s == 0)
74  s = 0x31415926L;
75 
76  k = s / 127773;
77  s = 16807 * (s - k * 127773) - 2836 * k;
78  if (s < 0)
79  s += 2147483647UL;
80 
81  __seed = (unsigned int)s;
82 
83  // force s into 0 .. 0x7fffffffUL positive
84  s &= 0x7fffffffUL;
85 
86  fval = (double) s / (double) 0x7fffffffL;
87 
88  fval *= (double) span;
89  fval += (double) begin;
90 //printf("fval: %f, span:%f, begin:%f\n", (double)fval, (double)span, (double)begin);
91 
92  val = fval;
93 
94  return(val);
95 }
96 
97 
99 int timetests(char *str, int check)
100 {
101  FILE *fp;
102  time_t epoch;
103  tm_t tm;
104  tv_t tv;
105  tz_t tz;
106  int i;
107  int val;
108  char buf[64];
109 
110  str = skipspaces(str);
111  if(!*str)
112  fp = stdout;
113  else if(check)
114  fp = fopen(str,"rb");
115  else
116  fp = fopen(str,"wb");
117 
118  if(fp == NULL)
119  perror("timetest file open failed");
120  gettimeofday(&tv, &tz);
121 
122  mysrand(tv.tv_sec);
123 
124 // struct tm {
125 // int tm_sec; /* Seconds (0-60) */
126 // int tm_min; /* Minutes (0-59) */
127 // int tm_hour; /* Hours (0-23) */
128 // int tm_mday; /* Day of the month (1-31) */
129 // int tm_mon; /* Month (0-11) */
130 // int tm_year; /* Year - 1900 */
131 // int tm_wday; /* Day of the week (0-6, Sunday = 0) */
132 // int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */
133 // int tm_isdst; /* Daylight saving time */
134 // };
135 
136  for(i=0;i<20;++i)
137  {
138 #ifdef ESP8266
139  optimistic_yield(1000);
140 #endif
141 
142  if(!check)
143  {
144  tm.tm_year = myrand(EPOCH_YEAR+2,2099) - 1900;
145  tm.tm_mon = myrand(-18,18);
146  tm.tm_mday = myrand(-45,45);
147  tm.tm_hour = myrand(-36,36);
148  tm.tm_min = myrand(-90,90);
149  tm.tm_sec = myrand(-90,90);
150 
151  fprintf(fp,"test: %d\n", i);
152  fprintf(fp,"%4d,%2d,%2d, %02d:%02d:%2d\n",
153  (int)tm.tm_year+1900, (int)tm.tm_mon, (int)tm.tm_mday,
154  (int)tm.tm_hour, (int)tm.tm_min, (int)tm.tm_sec);
155 
156  // express val as local time
157  epoch = mktime(&tm);
158 
159  fprintf(fp,"%10lu, %s\n", (long) epoch, asctime_r(&tm, buf));
160  fprintf(fp,"%4d,%2d,%2d, %02d:%02d:%2d\n",
161  (int)tm.tm_year+1900, (int)tm.tm_mon, (int)tm.tm_mday,
162  (int)tm.tm_hour, (int)tm.tm_min, (int)tm.tm_sec);
163 
164  localtime_r(&epoch, &tm);
165 
166  fprintf(fp,"%10lu, %s\n", (long) epoch, asctime_r(&tm, buf));
167  fprintf(fp,"%4d,%2d,%2d, %02d:%02d:%2d\n",
168  (int)tm.tm_year+1900, (int)tm.tm_mon, (int)tm.tm_mday,
169  (int)tm.tm_hour, (int)tm.tm_min, (int)tm.tm_sec);
170  fprintf(fp,"\n");
171 
172  }
173  }
174  printf("-1 = %d\n", -1);
175 
176  if (fp != stdout )
177  if( fclose(fp) )
178  perror("timetest fclose failed");
179  return(1);
180 }
int tm_min
Definition: time.h:44
MEMSPACE char * asctime_r(tm_t *t, char *buf)
Convert tm_t *t structure into POSIX asctime() ASCII string *buf.
Definition: time.c:367
MEMSPACE time_t mktime(tm_t *t)
convert tm_t structure to time_t local time epoch
Definition: time.c:534
Master include file for project Includes all project includes and defines here.
Common Linux/POSIX time functions.
int tm_mday
Definition: time.h:46
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
MEMSPACE void mysrand(int seed)
Definition: timetests.c:46
int tm_year
Definition: time.h:48
MEMSPACE void perror(const char *s)
POSIX perror() - convert POSIX errno to text with user message.
Definition: posix.c:1814
FILE type structure.
Definition: posix.h:156
time_t tv_sec
Definition: time.h:70
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
timer functions
void optimistic_yield(uint32_t interval_us)
Definition: user_task.c:102
uint32_t time_t
type of EPOCH result.
Definition: time.h:35
int tm_mon
Definition: time.h:47
#define NULL
Definition: cpu.h:55
static unsigned int __seed
Definition: timetests.c:43
POSIX timeval.
Definition: time.h:68
MEMSPACE int myrand(int start, int end)
Definition: timetests.c:52
Math IO functions, and verious conversion code with floating point support.
MEMSPACE int timetests(char *str, int check)
Definition: timetests.c:99
MEMSPACE int fclose(FILE *stream)
POSIX close a file stream.
Definition: posix.c:1239
#define EPOCH_YEAR
Definition: time.h:29
POSIX struct tm.
Definition: time.h:41
#define stdout
Definition: posix.h:270
#define MEMSPACE
Definition: cpu.h:25
MEMSPACE int gettimeofday(tv_t *tv, tz_t *tz)
Get current time struct timeval *tv and struct timezone *tz - POSIX function. We assume a GMT hardwar...
Definition: time.c:822
MEMSPACE int printf(const char *format,...)
MEMSPACE tm_t * localtime_r(time_t *t, tm_t *result)
Convert POSIX epoch time_t *tp into POSIX tm_t *result expressed as local time using timezone and DST...
Definition: time.c:499
#define L(x)
Definition: cal_dex.c:54
int tm_sec
Definition: time.h:43
int tm_hour
Definition: time.h:45
POSIX timezone.
Definition: time.h:78
MEMSPACE char * skipspaces(char *ptr)
Skip white space in a string - tabs and spaces.
Definition: stringsup.c:289