ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
user_main.c
Go to the documentation of this file.
1 
25 #include <stdint.h>
26 #include <stdarg.h>
27 #include <string.h>
28 #include <math.h>
29 
30 #include "user_config.h"
31 
32 #include "time.h"
33 #include "timer.h"
34 
35 
36 #ifdef ADF4351
37  #include "adf4351.h"
38 #endif
39 
40 #include "matrix.h"
41 #include "esp8266/system.h"
42 #include "lib/stringsup.h"
43 
44 #ifdef DISPLAY
45  #include "display/ili9341.h"
46 
47  #include "network/network.h"
48 
49  #ifdef WIRECUBE
50  #include "cordic/cordic.h"
51  #include "wire.h"
52  #include "cube_data.h"
53  #endif
54 
55  #ifdef EARTH
56  #include "cordic/cordic.h"
57  #include "wire.h"
58  #include "earth_data.h"
59  #endif
60 
61  #ifdef XPT2046
62  #include "xpt2046.h"
63  #include "calibrate.h"
64  // Calibration status
65  extern int tft_is_calibrated;
66  #endif
67 
68  extern mat_t tft_calX,tft_calY;
69 
70  /*
71  * Window layouts optional
72  *
73  * wintop winearth
74  * winmsg wincube
75  * winbottom
76  */
77 
78  /* Master Window, Full size of TFT */
79  window *master;
80 
81  /* Bottom status window */
82  window _winbottom;
83  window *winbottom = &_winbottom;
84 
85  /* wintop and winearth have the same height
86  /* Top Left status window */
87  window _wintop;
88  window *wintop = &_wintop;
89 
90  /* Earth Top Right status window */
91  window _winearth;
92  window *winearth = &_winearth;
93 
94  /* winmsg and winearth have the same height
95  /* Middle Left status window */
96  window _winmsg;
97  window *winmsg = &_winmsg;
98 
99  /* Right Wireframe window */
100  window _wincube;
101  window *wincube = &_wincube;
102 
103  // Rotation angle
104  LOCAL double degree = 0.0;
105  // Rotation increment
106  LOCAL double deg_inc = 4;
107  // Scale increment
108  LOCAL double dscale_inc;
109  // Scale factor
110  LOCAL double dscale;
111  // Scale maximum
112  LOCAL double dscale_max;
113 
114  LOCAL long count = 0;
115  LOCAL int rad;
116  LOCAL point V;
117  LOCAL point S;
118 
119 
120 #endif
121 
122 
123 //extern int printf(const char *fmt, ...);
124 void ets_timer_disarm(ETSTimer *ptimer);
125 void ets_timer_setfn(ETSTimer *ptimer, ETSTimerFunc *pfunction, void *parg);
126 
127 unsigned long ms_time = 0;
128 
129 // ==================================================================
130 // ==================================================================
131 
135 MEMSPACE
136 void ms_clear()
137 {
138  while(ms_time)
139  ms_time = 0;
140 }
141 
145 MEMSPACE
146 unsigned long ms_read()
147 {
148  unsigned long ret = 0;
149  while(ret != ms_time)
150  ret = ms_time;
151  return(ret);
152 }
153 
158 void ms_task(void)
159 {
160  ms_time++;
161 }
162 
165 MEMSPACE
166 void ms_init()
167 {
168  ms_time = 0;
169  if(set_timers(ms_task,1) == -1)
170  printf("Clock task init failed\n");
171 }
172 
173 
174 // FIXME we need to add time zone and timezone processing rules
175 int ntp_init = 0;
176 void ntp_setup(void)
177 {
178  tv_t tv;
179  tz_t tz;
180  time_t sec;
181  struct ip_info getinfo;
182 
183 
184  // Wait until we have an IP address before we set the time
185  if(!network_init)
186  return;
187 
188  if(ntp_init == 0)
189  {
190  ip_addr_t *addr = (ip_addr_t *)safecalloc(sizeof(ip_addr_t),1);
191 
192  // form pool.ntp.org
193  ipaddr_aton("206.108.0.131", addr);
194  sntp_setserver(1,addr);
195  ipaddr_aton("167.114.204.238", addr);
196  sntp_setserver(2,addr);
197 
198 #if 0
199  // Alternate time setting if the local router does NTP
200  if(wifi_get_ip_info(0, &getinfo))
201  {
202  printf("NTP:0 GW: %s\n", ipv4_2str(getinfo.gw.addr));
203  printf("NTP:0 IP: %s\n", ipv4_2str(getinfo.ip.addr));
204  sntp_setserver(1, & getinfo.gw);
205  sntp_setserver(2, & getinfo.ip);
206  }
207  else
208  {
209  printf("NTP:0 failed to get GW address\n");
210  return;
211  }
212 #endif
213 
214  if( sntp_set_timezone(0) )
215  {
216  printf("NTP: set_timeone OK\n");
217  sntp_init();
218  safefree(addr);
219  ntp_init = 1;
220  printf("NTP:1\n");
221  }
222  else
223  {
224  printf("NTP: set_timeone Failed\n");
225  }
226  }
227 
228  if(ntp_init == 1)
229  {
230  // they hard coded it to +8 hours from GMT
231  if( (sec = sntp_get_current_timestamp()) > 10 )
232  {
233  sntp_stop();
234  ntp_init = 2;
235  }
236  }
237  if(ntp_init == 2)
238  {
239  time_t s;
240 
241  tm_t *p;
242 
243  printf("NTP:2\n");
244 
245  // they return GMT + 8
246  // sec = sec - (8UL * 3600UL);
247 
248  tv.tv_sec = sec;
249  printf("ntp_init: %s\n", asctime(gmtime(&sec)));
250  printf("ntp_init: %s\n", ctime_gm(&sec));
251 
252  tv.tv_usec = 0;
253  tz.tz_minuteswest = 300;
254  tz.tz_dsttime = 0;
255 
256  settimeofday(&tv, &tz);
257 
258  printf("SEC:%ld\n",sec);
259  printf("TIME:%s\n", ctime(&sec));
260  printf("Zone: %d\n", (int) sntp_get_timezone());
261  ntp_init = 3;
262 
263  set_dst(tv.tv_sec);
264 
265  print_dst_gmt();
266  print_dst();
267 
268  p = gmtime(&tv.tv_sec);
269  mktime(p);
270  printf("Localtime: %s\n", asctime(p));
271  }
272 }
273 
275 {
276  char buffer[260];
277  int argc;
278  char *argv[10];
279 
280 
281  if(kbhiteol(0))
282  {
283  fgets(buffer,255,stdin);
284  printf("Command:[%s]\n",buffer);
285  argc = split_args(buffer, argv, 10);
286  if(!user_tests(argc,argv))
287  printf("unknown command:[%s]\n", buffer);
288  }
289 }
290 // Signal strength update interval
291 int signal_loop = 0;
292 
294 
302 extern uint8_t ip_msg[];
303 
304 int skip = 0;
305 
306 long last_time10 = 0;
307 long last_time50 = 0;
309 
310 int loop_cnt = 0;
311 
312 // VCC voltage divider
313 #define R1 330000.0
314 // R2 is actually a variable resister
315 #define R2 100000.0
316 
317 // ADC read error scale
318 // Measured error
319 #define VERROR 0.95
320 // This is reduced to a single constant by the compiler
321 #define VSCALE (VERROR*((R1+R2)/R2)/1024.0)
322 
332 float adc_read()
333 {
334  uint16_t system_adc_read(void);
335  // system adc read returns 0 .. 1023
336  // range 0 .. 1.0V
337  return( ((float)system_adc_read()) * VSCALE );
338 }
339 
340 // working on vector fonts
341 
342 // ============================================================
343 
344 
345 // ============================================================
346 
347 // main task loop called by yield code
348 void user_loop(void)
349 {
350  extern int connections;
351  uint32_t time1,time2;
352  long t;
353 #ifdef DISPLAY
354  char time_tmp[32];
355  uint8_t red, blue,green;
356  int touched;
357  uint16_t X,Y;
358 #endif
359 
360  // getinfo.ip.addr, getinfo.gw.addr, getinfo.netmask.addr
361  struct ip_info getinfo;
362  char *ptr;
363 
364  // ========================================================
365  // Run all remaining tasks once every 1mS
366  t = ms_read();
367  if((t - last_time10) < 1U)
368  return;
369  last_time10 = t;
370  // ========================================================
371 // Tasks that must run very fast , once every millisecond should be at the top
372 
373 #ifdef ADF4351
374  ADF4351_task();
375 #endif
376 #ifdef XPT2046
377  XPT2046_task();
378 #endif
379 
380  // ========================================================
381  // Only run every 50mS
382  t = ms_read();
383  if((t - last_time50) < 50U)
384  return;
385  last_time50 = t;
386  // ========================================================
387 
388  user_tasks();
389 
390  // NTP state machine
391  ntp_setup();
392 
393 
394 #ifdef DISPLAY
395  #ifdef XPT2046
396  if(tft_is_calibrated)
397  {
398  touched = tft_touch_key(master,(uint16_t *)&X, (uint16_t *)&Y);
399  #if XPT2046_DEBUG
400  if(touched)
401  tft_printf(winmsg,"X:%d,Y:%d\n",(int)X,(int)Y);
402  #endif
403  }
404  #endif
405 
406  #ifdef NETWORK_TEST
407  servertest_message(winmsg);
408  #endif
409 
410  #ifdef DEBUG_STATS
411  #ifdef VOLTAGE_TEST
412  #ifdef DEBUG_STATS
413  // Do NOT run adc_read() every millisecond as system_adc_read() blocks WIFI
414  tft_set_textpos(wintop, 0,2);
415  tft_printf(wintop,"Volt:%2.2f\n", (float)adc_read());
416  #endif
417  #endif // VOLTAGE_TEST
418 
419  count += 1;
420  tft_set_textpos(wintop, 0,0);
421  tft_set_font(wintop,0);
422  tft_font_fixed(wintop);
423  tft_printf(wintop,"Iter:% 10ld, %+7.2f\n", count, degree);
424  #endif
425 
426  #ifdef CIRCLE
427  rad = dscale; // +/- 90
428  tft_drawCircle(wincube, wincube->w/2, wincube->h/2, rad ,wincube->bg);
429  // RGB
430  #endif
431 
432  // reset cube to background
433  #ifdef WIRECUBE
434  V.x = degree;
435  V.y = degree;
436  V.z = degree;
437  // Cube points were defined with sides of 1.0
438  // We want a scale of +/- w/2
439  wire_draw(wincube, cube_points, cube_edges, &V, wincube->w/2, wincube->h/2, dscale, wincube->bg);
440  #endif
441 
442  degree += deg_inc;
443  dscale += dscale_inc;
444 
445  if(degree <= -360)
446  deg_inc = 4;
447  if(degree >= 360)
448  deg_inc = -4;
449 
450  if(dscale < dscale_max/2)
451  {
452  dscale_inc = -dscale_inc;
453  }
454  if(dscale > dscale_max)
455  {
456  dscale_inc = -dscale_inc;
457  }
458 
459  #ifdef WIRECUBE
460  V.x = degree;
461  V.y = degree;
462  V.z = degree;
463  wire_draw(wincube, cube_points, cube_edges, &V, wincube->w/2, wincube->h/2, dscale, ILI9341_WHITE);
464  #endif
465 
466  #ifdef CIRCLE
467  // Display bounding circle that changes color around the cube
468  if(dscale_inc < 0.0)
469  {
470  red = 255;
471  blue = 0;
472  green = 0;
473  }
474  else
475  {
476  red = 0;
477  blue = 0;
478  green = 255;
479  }
480  rad = dscale; // +/- 90
481  tft_drawCircle(wincube, wincube->w/2, wincube->h/2, rad, tft_RGBto565(red,green,blue));
482  #endif
483 #endif // DISPLAY
484 
485  // ========================================================
486  // Tasks run only once every second go after this
487  time(&sec);
488  if(sec == seconds)
489  return;
490  seconds=sec;
491  // ========================================================
492 
493 #ifdef DISPLAY
494  // ========================================================
495  // TIME
496  tft_set_textpos(winbottom, 0,0);
497  //Tue May 17 18:56:01 2016
498  strncpy(time_tmp,ctime(&sec),31);
499  time_tmp[19] = 0;
500  tft_printf(winbottom," %s", time_tmp);
501  tft_cleareol(winbottom);
502  tft_set_textpos(winbottom, 0,1);
503 
504  // ========================================================
505  // CONNECTION status
506  //tft_printf(winbottom," %s", ip_msg);
507  // IP and disconnected connection state only
508  if(wifi_get_ip_info(0, &getinfo))
509  tft_printf(winbottom," %s", ipv4_2str(getinfo.ip.addr));
510  else
511  tft_printf(winbottom," Disconnected");
512  tft_cleareol(winbottom);
513 
514  #ifdef DEBUG_STATS
515  // ========================================================
516  // HEAP size
517  tft_set_textpos(wintop, 0,1);
518  tft_printf(wintop,"Heap: %d, Conn:%d\n",
519  system_get_free_heap_size(), connections);
520 
521  // ========================================================
522  // WIFI status
523  tft_set_textpos(wintop, 0,3);
524  tft_printf(wintop,"CH:%02d, DB:%+02d\n",
525  wifi_get_channel(),
526  wifi_station_get_rssi());
527  #endif // DEBUG_STATS
528 #endif //DISPLAY
529 
530 }
531 
532 int inloop = 0;
533 void loop()
534 {
535  int ret;
536  if(inloop)
537  {
538  printf("Error: loop() task overrun\n");
539  inloop = 0;
540  return;
541  }
542  inloop = 1;
543 
544  // ========================================================
545  // We should not have any SPI devices enabled at this point
546  ret = spi_chip_select_status();
547  if(ret != 0xff)
548  {
549  printf("Error: loop() entered with spi_cs = %d\n",ret);
550  spi_end(ret);
551  return;
552  }
553 
554 
555  user_loop();
556 
557  // We should not have any SPI devices enabled at this point
558  ret = spi_chip_select_status();
559  if(ret != 0xff)
560  {
561  printf("Error: loop() entered with spi_cs = %d\n",ret);
562  spi_end(ret);
563  return;
564  }
565 
566  inloop = 0;
567 }
568 
569 
570 #ifdef DISPLAY
571  #if ILI9341_DEBUG & 1
572  MEMSPACE
573  void read_tests(window *win)
574  {
575  int x,y;
576  uint16_t color;
577  uint16_t buffer[3*16]; // 16 RGB sets
578  y = 4;
579  tft_readRect(win, x, y, 16, 1, buffer);
580  for(x=0;x<16;++x)
581  {
582  printf("x:%d,y:%d,c:%04x\n", x,y,buffer[x]);
583  }
584  printf("\n");
585  }
586  #endif
587 
588 
589 #endif //DISPLAY
590 
591 
592 void user_help()
593 {
594  #ifdef POSIX_TESTS
595  posix_help();
596  #endif
597  #ifdef FATFS_TESTS
598  fatfs_help(1);
599  #endif
600  #ifdef ADF4351
601  adf4351_help();
602  #endif
603  printf(
604  "help\n"
605  "connection\n"
606  "calibrate N\n"
607  "calibrate_test N\n"
608  "display_clock\n"
609  "draw C[1]\n"
610  "mem\n"
611  "pixel\n"
612  "rotate N\n"
613  "setdate YYYY MM DD HH:MM:SS\n"
614  "time\n"
615  "timetest\n"
616  "\n");
617 }
618 
619 
620 
625 
630 MEMSPACE
631 int user_tests(int argc, char *argv[])
632 {
633 
634  char *ptr;
635  time_t t;
636  double freq;
637  extern int connections;
638  int ind;
639 
640  if(argc < 2)
641  return(0);
642 
643  ind = 1;
644  ptr = argv[ind++];
645 
646 
647  if (MATCHARGS(ptr,"help", (ind+0),argc ))
648  {
649  user_help();
650  return(1);
651  }
652 #ifdef POSIX_TESTS
653  if(posix_tests(argc,argv))
654  return(1);
655 #endif
656 #ifdef FATFS_TESTS
657  if(fatfs_tests(argc,argv))
658  return(1);
659 #endif
660 #ifdef ADF4351
661  if (MATCHARGS(ptr,"adf4351", (ind + 1) ,argc))
662  {
663  adf4351_cmd(argc,argv);
664  return(1);
665  }
666 #endif
667  if (MATCHARGS(ptr,"setdate", (ind + 1) ,argc))
668  {
669  setdate_r(argv[ind++]);
670  return(1);
671  }
672  if (MATCHARGS(ptr,"display_clock", (ind + 0) ,argc))
673  {
674  display_clock();
675  return(1);
676  }
677  if (MATCHARGS(ptr,"time", (ind + 0) ,argc))
678  {
679  t = time(0);
680  printf("TIME:%s\n", ctime(&t));
681  return(1);
682  }
683  if (MATCHARGS(ptr,"connection", (ind + 0) ,argc))
684  {
685  printf("connections:%d\n", connections);
686  return(1);
687  }
688  if (MATCHARGS(ptr,"mem", (ind + 0) ,argc))
689  {
690  PrintRam();
691  return(1);
692  }
693  if (MATCHARGS(ptr,"timetest", (ind + 1) ,argc))
694  {
695  timetests(argv[ind++],0);
696  return(1);
697  }
698 #ifdef DISPLAY
699  if (MATCHARGS(ptr,"calibrate", (ind + 1) ,argc))
700  {
701  int ret = atoi(argv[ind++]);
702  tft_setRotation(ret);
703  tft_touch_calibrate(master);
704  MatWrite("/tft_calX",tft_calX);
705  MatWrite("/tft_calY",tft_calY);
706  setup_windows(ret & 3,0);
707  return(1);
708  }
709  if (MATCHARGS(ptr,"calibrate_test", (ind + 1) ,argc))
710  {
711  int ret = atoi(argv[ind++]);
712  tft_setRotation(ret);
713  tft_touch_calibrate(master);
714  MatWrite("/tft_calX",tft_calX);
715  MatWrite("/tft_calY",tft_calY);
716  tft_map_test(master, 10);
717  setup_windows(ret & 3,0);
718  return(1);
719  }
720  if (MATCHARGS(ptr,"rotate", (ind + 1) ,argc))
721  {
722 // FIXME rotate calibration data ???
723  int ret = atoi(argv[ind++]);
724  tft_setRotation(ret);
725  setup_windows(ret & 3,0);
726  return(1);
727  }
728 #ifdef VFONTS
729  if (MATCHARGS(ptr,"draw", (ind + 1) ,argc))
730  {
731  char *ptr = argv[ind++];
732  int c = *ptr++;
733  int n = *ptr++;
734  if( n == '1')
735 
736  drawSVG(winmsg, 8, 24, c, 0.08, ILI9341_WHITE, 1);
737  else
738  drawSVG(winmsg, 8, 24, c, 0.08, ILI9341_WHITE, 0);
739  return(1);
740  }
741 #endif
742  if (MATCHARGS(ptr,"pixel", (ind + 0) ,argc))
743  {
744  int c;
745  int x,y;
746 
747  tft_drawPixel(winmsg, 8, 24, ILI9341_WHITE);
748  for(y=24;y<26;++y)
749  {
750  for(x=8;x<10;++x)
751  {
752  c = tft_readPixel(winmsg, x, y);
753  printf("pixel(%d,%d): %04x\n", x,y,c);
754  }
755  }
756  return(1);
757  }
758 #endif //DISPLAY
759  return(0);
760 }
761 
762 #ifdef TEST_FLASH
763  ICACHE_RODATA_ATTR uint8_t xxx[] = { 1,2,3,4,5,6,7,8 };
767  MEMSPACE
768  void test_flashio(window *win)
769  {
770  uint16_t xpros,ypos;
771  for(i=0;i<8;++i)
772  {
773  printf("%02x", read_flash8((uint32_t) &xxx+i));
774  }
775  printf("\n");
776  printf("%08x, %08x", read_flash32((uint8_t *)&xxx), read_flash16((uint8_t *)&xxx));
777  }
778 #endif
779 
784 {
785  int i;
786 // Test byte order
787 // extensa has LSB to MSB byte order LITTLE_ENDIAN
788  union UUU
789  {
790  unsigned int wide;
791  unsigned char byte8[sizeof(unsigned int)];
792  };
793  volatile union UUU u;
794 
795  printf("Byte Order of 0x12345678:\n");
796  u.wide = 0x12345678;
797  for (i=0; i < sizeof(unsigned int); i++)
798  printf("byte[%d] = %02x\n", i, u.byte8[i]);
799 
800 // Test basic type sizes
801  printf("sizeof (double) = %d\n", sizeof (double ) );
802  printf("sizeof (float) = %d\n", sizeof (float ) );
803  printf("sizeof (long long) = %d\n", sizeof (long long ) );
804  printf("sizeof (long) = %d\n", sizeof (long ) );
805  printf("sizeof (int) = %d\n", sizeof (int ) );
806  printf("sizeof (char) = %d\n", sizeof (char ) );
807 
808  printf("long: %ld\n", 123456789L);
809  printf("unsigned long: %lu\n", 123456789L);
810  printf("long hex: %lx\n", 123456789L);
811 
812  printf("int : %d\n", 123456789L);
813  printf("unsigned int: %u\n", 123456789L);
814  printf("int hex: %lx\n", 123456789L);
815 
816  printf("int: %d\n", 12345);
817  printf("unsigned int: %u\n", 12345);
818  printf("int hex: %x\n", 12345);
819 
820 }
821 
822 #if DISPLAY
823 setup_windows(int rotation, int debug)
824 {
825  int16_t x,y,w,h;
826  uint32_t ID;
827  extern uint16_t tft_ID;
828  ID = tft_ID;
829 
830  // Set master rotation
831  tft_setRotation(rotation);
833  tft_fillWin(master, master->bg);
834 
835 #if ILI9341_DEBUG & 1
836  if(debug)
837  printf("\nDisplay ID=%08lx\n",ID);
838 #endif
839 
840  // Message window setup
841 #ifdef EARTH
842  w = master->w * 7 / 10;
843 #else
844  w = master->w;
845 #endif
846  // TOP
847 #ifdef DEBUG_STATS
848  tft_window_init(wintop,0,0, w, font_H(0)*4);
850 #else
851  tft_window_init(wintop,0,0, w, font_H(2)*2);
852  tft_setTextColor(wintop, ILI9341_WHITE, tft_RGBto565(0,64,255));
853 #endif
854  tft_set_font(wintop,0);
855  tft_font_var(wintop);
856  tft_fillWin(wintop, wintop->bg);
857  tft_set_textpos(wintop, 0,0);
858 
859 #ifdef EARTH
860  tft_window_init(winearth,w,0, master->w - w + 1, wintop->h);
862  tft_fillWin(winearth, winearth->bg);
863 #endif
864 
865  // BOTOM
866  // TIME,DATE
867  tft_window_init(winbottom, 0, master->h - 1 - font_H(2)*2,
868  master->w, font_H(2)*2);
869  if(master->rotation & 1)
870  tft_set_font(winbottom,2);
871  else
872  tft_set_font(winbottom,1);
873  tft_font_var(winbottom);
874  tft_setTextColor(winbottom, 0, tft_RGBto565(0,255,0));
875  tft_fillWin(winbottom, winbottom->bg);
876  tft_set_textpos(winbottom, 0,0);
877 
878  // Message window setup
879 #ifdef WIRECUBE
880  w = master->w * 7 / 10;
881 #else
882  w = master->w;
883 #endif
884 
885  // MSG
886  tft_window_init(winmsg,0,wintop->h,
887  w, master->h - (wintop->h + winbottom->h));
888 
890  tft_fillWin(winmsg, winmsg->bg);
891  // write some text
892  tft_set_font(winmsg,0);
893  tft_font_var(winmsg);
894  tft_set_textpos(winmsg, 0,0);
895 
896  // CUBE setup
897 #ifdef WIRECUBE
898  /* Setup cube/wireframe demo window */
899  /* This is to the right of the winmsg window and the same height */
900  tft_window_init(wincube, winmsg->w, wintop->h, master->w - winmsg->w, winmsg->h);
902  tft_fillWin(wincube, wincube->bg);
903 #endif
904 
905 #ifdef DEBUG_STATS
906  if(debug)
907  {
908  // Display ID
909  tft_setTextColor(winmsg, ILI9341_RED,winmsg->bg);
910  tft_printf(winmsg, "DISP ID: %04lx\n", ID);
911  tft_setTextColor(winmsg, ILI9341_WHITE,winmsg->bg);
912 #ifdef XPT2046
913  if(!tft_is_calibrated)
914  {
915  tft_set_font(winmsg,0);
916  tft_printf(winmsg,"Please Calibrate Display\n");
917  tft_printf(winmsg,"serial command: calibrate N\n");
918  tft_printf(winmsg,"N is rotation, 0..3\n");
919  tft_printf(winmsg,"%d is current\n",master->rotation);
920  }
921 #endif
922  }
923 #endif
924 
925 
926  // Cube points were defined with sides of 1.0
927  // We want a scale of +/- w/2
928 #ifdef WIRECUBE
929  if(wincube->w < wincube->h)
930  dscale_max = wincube->w/2;
931  else
932  dscale_max = wincube->h/2;
933 
934  dscale = dscale_max;
935  dscale_inc = dscale_max / 100;
936 #endif
937 
938 #if ILI9341_DEBUG & 1
939  if(debug)
940  {
941  printf("Test Display Read\n");
942  read_tests(winmsg);
943  }
944 #endif
945 
946  // Draw Wireframe earth in message area
947 #ifdef EARTH
948  // Earth points were defined with radius of 0.5, diameter of 1.0
949  // We want a scale of +/- w/2
950  double tscale_max;
951  if(winearth->w < winearth->h)
952  tscale_max = winearth->w;
953  else
954  tscale_max = winearth->h;
955  V.x = -90;
956  V.y = -90;
957  V.z = -90;
958  // draw earth
959  // Earth points were defined over with a scale of -0.5/+0.5 scale - so scale must be 1 or less
960  wire_draw(winearth, earth_data, NULL, &V, winearth->w/2, winearth->h/2, tscale_max, winearth->fg);
961 #endif
962 
963 }
964 #endif //DISPLAY
965 
970 MEMSPACE
971 void setup(void)
972 {
973  int i;
974  char time[20];
975  int ret;
976  uint16_t *ptr;
977  double ang;
978  extern web_init();
979  int w,h;
980 
981  ip_msg[0] = 0;
982 
983 // CPU
984 // 160MHZ
985  REG_SET_BIT(0x3ff00014, BIT(0));
986 // 80MHZ
987 // REG_CLR_BIT(0x3ff00014, BIT(0));
988 
989  os_delay_us(200000L); // Power Up dalay - lets power supplies and devices settle
990 
991  // Configure the UART
992  //uart_init(BIT_RATE_115200,BIT_RATE_115200);
994 
995  os_delay_us(200000L); // Power Up dalay - lets power supplies and devices settle
996  os_delay_us(200000L); // Power Up dalay - lets power supplies and devices settle
997  printf("\n\n\n\n");
998  sep();
999  printf("System init...\n");
1000  printf("ESP8266 multidevice project\n");
1001  printf(" (c) 2014-2017 by Mike Gore\n");
1002  printf(" GNU version 3\n");
1003  printf("-> https://github.com/magore/esp8266_ili9341\n");
1004  printf(" GIT last pushed: %s\n", GIT_VERSION);
1005  printf(" Last updated file: %s\n", LOCAL_MOD);
1006 
1007 
1008  sep();
1009  PrintRam();
1010 
1011  sep();
1012  printf("HSPI init...\n");
1013  hspi_init(1,0);
1014 
1015  printf("Timers init...\n");
1016  init_timers();
1017 
1018  // 1000HZ timer
1019  ms_init();
1020 
1021  test_types();
1022 
1023  // Functions manage user defined address pins
1024  chip_addr_init();
1025 
1026  initialize_clock(300);
1027 
1028 
1029 #ifdef ADF4351
1030  #ifdef ADF4351_CS
1031  chip_select_init(ADF4351_CS);
1032  #endif
1033  ADF4351_Init();
1034  printf("ADF4351 init done\n");
1035 #endif
1036 
1037 // Make sure all other GPIO pins are initialized BEFORE SD card
1038 #ifdef FATFS_SUPPORT
1039  sep();
1040  // Functions manage chip selects
1041  #ifdef MMC_CS
1042  chip_select_init(MMC_CS);
1043  #endif
1044  printf("SD Card init...\n");
1045  mmc_init(1);
1046 #endif
1047 
1048 #ifdef DISPLAY
1049  sep();
1050  #ifdef ILI9341_CS
1051  chip_select_init(ILI9341_CS);
1052  #endif
1053 
1054  #ifdef XPT2046_CS
1055  XPT2046_spi_init();
1056  #endif
1057 
1058  // Initialize TFT
1059  master = tft_init();
1060 
1061  tft_calX = MatRead("/tft_calX");
1062  tft_calY = MatRead("/tft_calY");
1063  if(tft_calX.data == NULL || tft_calY.data == NULL)
1064  tft_is_calibrated = 0;
1065  else
1066  tft_is_calibrated = 1;
1067  printf("TFT calibration %s\n", tft_is_calibrated ? "YES" : "NO");
1068 
1069  // rotateion = 1, debug = 1
1070  setup_windows(1,1);
1071 #endif
1072 
1073  wdt_reset();
1074  sep();
1075  printf("Setup Tasks\n");
1076 
1077  sep();
1078  setup_networking();
1079 
1080  #ifdef TELNET_SERIAL
1081  printf("Setup Network Serial Bridge\n");
1082  bridge_task_init(23);
1083  #endif
1084 
1085  if ( espconn_tcp_set_max_con(MAX_CONNECTIONS+1) )
1086  printf("espconn_tcp_set_max_con(%d) != (%d) - failed!\n",
1087  MAX_CONNECTIONS+1, espconn_tcp_get_max_con());
1088  else
1089  printf("espconn_tcp_set_max_con(%d) = (%d) - success!\n",
1090  MAX_CONNECTIONS+1, espconn_tcp_get_max_con());
1091 
1092 #ifdef NETWORK_TEST
1093  printf("Setup Network TFT Display Client\n");
1094  servertest_setup(TCP_PORT);
1095 #endif
1096 
1097 #ifdef WEBSERVER
1098  printf("Setup Network WEB SERVER\n");
1099  web_init(80);
1100 #endif
1101 
1102  sep();
1103  PrintRam();
1104 
1105  system_set_os_print(0);
1106 
1107 } //setup()
int16_t w
Definition: ili9341.h:40
MEMSPACE void tft_setRotation(uint8_t m)
Set Display rotation, applies to the master window only Set hardware display rotation and memory fill...
Definition: ili9341.c:982
uint16_t tft_readPixel(window *win, int16_t x, int16_t y)
Read one pixel and return color in 1bit 565 RGB format We clip the window to the current view Note: R...
Definition: ili9341.c:952
int tz_dsttime
Definition: time.h:81
uint8_t ip_msg[]
test task Runs corrected cube demo from Sem Optionally wireframe Earth viewer
Definition: network.c:51
float ** data
Definition: matrix.h:28
MEMSPACE void tft_set_textpos(window *win, int16_t x, int16_t y)
Set current window text pointer in characters (per current rotation) - overall font bounding box...
Definition: ili9341.c:1254
MEMSPACE void display_clock()
Display system time and optionally RTC time.
Definition: time.c:1227
MEMSPACE_RO wire_p earth_data[]
Definition: earth_data.h:10
void loop()
Definition: user_main.c:533
void ets_timer_disarm(ETSTimer *ptimer)
int16_t h
Definition: ili9341.h:41
MEMSPACE void initialize_clock(int minwest)
initialize system time - if we have an RTC use it
Definition: time.c:1178
unsigned short uint16_t
Definition: send.c:18
MEMSPACE void servertest_message(window *win)
Network receive task.
Definition: server.c:56
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.
uint16_t bg
Definition: ili9341.h:45
MEMSPACE void PrintRam()
Display Free memory and regions.
Definition: system.c:113
int skip
Definition: user_main.c:304
void hspi_init(uint32_t prescale, int hwcs)
HSPI Initiaization - with automatic chip select Pins: MISO GPIO12 MOSI GPIO13 CLK GPIO14 CS GPIO15 - ...
Definition: hspi.c:55
Common Linux/POSIX time functions.
MEMSPACE void tft_setTextColor(window *win, uint16_t fg, uint16_t bg)
====================================
Definition: ili9341.c:1228
Cordic_T X
Main Cordic routine - used for basic trig and vector rotations We use fixed point numbers...
Definition: cordic.c:102
MEMSPACE void set_dst(time_t epoch)
Set DST start and end time for the given epoch year.
Definition: time.c:1113
void drawSVG(window *win, int16_t x, int16_t y, int16_t c, float scale, uint16_t color, int16_t fill)
uint8_t rotation
Definition: ili9341.h:46
uint8_t spi_chip_select_status()
SPI CS pin status return CS GPIO pin number or 0xff.
Definition: hal.c:404
MEMSPACE void posix_help(int full)
Definition: posix_tests.c:46
int16_t y[XYSTACK+2]
Definition: ili9341.c:372
#define ILI9341_WHITE
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
void ETSTimerFunc(void *timer_arg)
Definition: ets_sys.h:29
MEMSPACE void setup_networking()
Code fragments if(!wifi_station_dhcpc_stop()) printf( "ERROR wifi_station_dhcpc_stop() \n ");...
Definition: network.c:165
MEMSPACE int setdate_r(char *buf)
Set date and time from string in this format "YYYY MM DD HH:MM:SS".
Definition: time.c:919
uint8_t read_flash8(uint8_t *p)
Definition: flash.c:46
MEMSPACE void ms_init()
Initialize 1000HZ timer task.
Definition: user_main.c:166
unsigned int uint32_t
Definition: send.c:19
int kbhiteol(int uart_no)
Has an EOL been read on stdin ?
Definition: uart.c:572
int loop_cnt
Definition: user_main.c:310
MEMSPACE void print_dst_gmt()
print start/stop for DST as GMT for this year
Definition: time.c:1166
MEMSPACE WEAK_ATR char * strncpy(char *dest, const char *src, size_t size)
copy a string of at most N characters
Definition: stringsup.c:179
unsigned long ms_time
Definition: user_main.c:127
Cordic_T Y
Definition: cordic.c:102
time_t tv_sec
Definition: time.h:70
void user_loop(void)
Definition: user_main.c:348
MEMSPACE void tft_set_font(window *win, uint16_t index)
Set current font size (per current rotation)
Definition: ili9341.c:1267
long last_time10
Definition: user_main.c:306
window * winmsg
MEMSPACE int set_timers(void(*handler)(void), int timer)
Install a user timer task.
Definition: timer.c:60
float adc_read()
return system_adc_read scaled to a float T_OUT pin is connected to the junction of a voltage divider ...
Definition: user_main.c:332
timer functions
MEMSPACE void XPT2046_spi_init(void)
MEMSPACE time_t time(time_t *t)
Return second from epoch - POSIX function.
Definition: time.c:843
MEMSPACE void setup(void)
main() Initialize user task
Definition: user_main.c:971
long last_time50
Definition: user_main.c:307
MEMSPACE int split_args(char *str, char *argv[], int max)
Split string into arguments stored in argv[] We split source string into arguments Warning: source st...
Definition: stringsup.c:566
int network_init
Definition: network.c:50
MEMSPACE int fatfs_tests(int argc, char *argv[])
FatFs test parser.
Definition: fatfs_tests.c:101
uint16_t tft_ID
Definition: ili9341_hal.c:44
int16_t x[XYSTACK+2]
Definition: ili9341.c:371
MEMSPACE char * ipv4_2str(uint32_t ip)
Definition: network.c:66
void spi_end(uint8_t pin)
SPI chip disable function wait for current tranaction to finish!
Definition: hal.c:388
void ets_timer_setfn(ETSTimer *ptimer, ETSTimerFunc *pfunction, void *parg)
void chip_select_init(uint8_t pin)
CHIP select HAL.
Definition: hal.c:198
uint32_t time_t
type of EPOCH result.
Definition: time.h:35
MEMSPACE void servertest_setup(int port)
Setup Server Task.
Definition: server.c:100
Definition: matrix.h:27
user_tasks()
Definition: user_main.c:274
#define NULL
Definition: cpu.h:55
#define tft_RGBto565(r, g, b)
Pass 8-bit (each) R,G,B, get back 16-bit packed color ILI9341 defaults to MSB/LSB data so we have to ...
Definition: ili9341.h:93
MEMSPACE char * ctime_gm(time_t *tp)
GMT version of POSIX ctime().
Definition: time.c:441
MEMSPACE_RO wire_p cube_points[]
CUBE data +/- 0.5 is a cube with sides of 1.0.
Definition: cube_data.h:36
MEMSPACE unsigned long ms_read()
Read 1000HZ timer We loop in case the update of ms_time is not "atomic" - done in a single instructio...
Definition: user_main.c:146
window * wintop
Cordic Routines Handle angle outside of the first quadrant Added standalone test to verify CORDIC aga...
#define MAX_CONNECTIONS
Definition: web.h:31
MEMSPACE int tft_map_test(window *win, int points)
POSIX timeval.
Definition: time.h:68
MEMSPACE void tft_font_fixed(window *win)
Definition: ili9341.c:1278
matrix functions
MEMSPACE int mmc_init(int verbose)
Initialize MMC and FatFs interface, display diagnostics.
Definition: mmc_hal.c:208
MEMSPACE char * asctime(tm_t *t)
Convert tm_t *t structure into POSIX asctime() ASCII string.
Definition: time.c:394
uint32_t read_flash32(uint8_t *p)
32 bits reads from Flash memory space Uses cpy_flash() to avoid alighnment problems ...
Definition: flash.c:115
void tft_drawPixel(window *win, int16_t x, int16_t y, int16_t color)
Pixel functions
Definition: ili9341.c:717
MEMSPACE int timetests(char *str, int check)
Definition: timetests.c:99
int ind
Definition: ili9341.c:373
MEMSPACE tm_t * gmtime(time_t *tp)
Convert epoch GMT time_t *tp into POSIX static tm_t *t.
Definition: time.c:471
MEMSPACE void ms_clear()
Clear 1000HZ timer We loop in case the update of ms_time is not "atomic" - done in a single instructi...
Definition: user_main.c:136
uint32_t tv_usec
Definition: time.h:71
MEMSPACE void print_dst()
print start/stop for DST as localtime for this year
Definition: time.c:1158
void ntp_setup(void)
Definition: user_main.c:176
XPT2046 calibration code.
MEMSPACE void uart_init(UartBaudRate uart0_br, UartBaudRate uart1_br)
initialize uart0 and uart1 Defaults: 8 = data bits, 1 = stop bits, no parity
Definition: uart.c:773
#define stdin
define stdin, stdout and stderr
Definition: posix.h:269
Various string and character functions.
MEMSPACE void XPT2046_task(void)
MEMSPACE int posix_tests(int argc, char *argv[])
POSIX tests.
Definition: posix_tests.c:84
int ntp_init
Definition: user_main.c:175
POSIX struct tm.
Definition: time.h:41
MEMSPACE mat_t MatRead(char *name)
Read a matrix.
Definition: matrix.c:527
MEMSPACE void tft_font_var(window *win)
Set current font type to variable.
Definition: ili9341.c:1288
MEMSPACE void fatfs_help(int full)
Display FatFs test diagnostics help menu.
Definition: fatfs_tests.c:46
uint16_t fg
Definition: ili9341.h:44
int connections
Definition: web.c:45
Point definition.
Definition: cordic.h:37
#define MEMSPACE
Definition: cpu.h:25
MEMSPACE void wdt_reset(void)
reset watchdog
Definition: system.c:190
#define VSCALE
Definition: user_main.c:321
MEMSPACE int user_tests(int argc, char *argv[])
help functions test parser
Definition: user_main.c:631
void tft_fillWin(window *win, uint16_t color)
Fill functions
Definition: ili9341.c:334
wireframe view code
void tft_readRect(window *win, int16_t x, int16_t y, int16_t w, int16_t h, uint16_t *color)
Definition: ili9341.c:837
#define ILI9341_NAVY
MEMSPACE int printf(const char *format,...)
MEMSPACE void init_timers()
Setup all timers tasksi and enable interrupts.
Definition: timer.c:293
Definition: ili9341.h:34
System memory and reset utilities.
time_t sec
Definition: user_main.c:308
MEMSPACE int MATCHARGS(char *str, char *pat, int min, int argc)
Match two strings and compare argument index Display message if the number of arguments is too few...
Definition: stringsup.c:471
void wire_draw(window *win, const wire_p *wire, const wire_e *edge, point *view, int x, int y, double scale, uint16_t color)
Definition: wire.c:60
uint16_t read_flash16(uint8_t *p)
16 bits reads from Flash memory space Uses cpy_flash() to avoid alighnment problems ...
Definition: flash.c:103
void ms_task(void)
1000HZ timer task
Definition: user_main.c:158
MEMSPACE_RO wire_e cube_edges[]
Definition: cube_data.h:51
MEMSPACE void tft_cleareol(window *win)
Character and String functions
Definition: ili9341.c:1601
MEMSPACE int MatWrite(char *name, mat_t MatW)
Write a matrix.
Definition: matrix.c:605
#define L(x)
Definition: cal_dex.c:54
ili9341 driver inspired by Adafruit ili9341 code All code in this file has been rewritten by Mike Gor...
MEMSPACE int settimeofday(tv_t *tv, tz_t *tz)
Set current time struct timeval *tv and struct timezone *tz - POSIX function. We assume a GMT hardwar...
Definition: time.c:861
MEMSPACE int tft_printf(window *win, const char *fmt,...)
tft_printf function
Definition: tft_printf.c:52
MEMSPACE void safefree(void *p)
Safe free - Only free a pointer if it is in malloc memory range. We want to try to catch frees of sta...
Definition: system.c:165
MEMSPACE char * ctime(time_t *tp)
Convert local time_t *t epoch time into POSIX asctime() string buf[].
Definition: time.c:424
#define ILI9341_BLUE
Wireframe CUBE data used by wireframe viewer The code handles fixed, proportional and bounding box fo...
MEMSPACE int tft_touch_key(window *win, uint16_t *X, uint16_t *Y)
void chip_addr_init()
ADDRESS select HAL.
Definition: hal.c:242
unsigned char uint8_t
Definition: send.c:17
time_t seconds
Definition: user_main.c:293
MEMSPACE void web_init(int port)
Setup WEB server and accept connections.
Definition: web.c:2490
void user_help()
Definition: user_main.c:592
int signal_loop
Definition: user_main.c:291
POSIX timezone.
Definition: time.h:78
MEMSPACE int atoi(const char *str)
Convert ASCII string to number in base 10.
Definition: mathio.c:281
test_types()
Definition: user_main.c:783
MEMSPACE window * tft_init(void)
Initialize TFT.
Definition: ili9341_hal.c:178
int font_H(int font)
Get font height used for line to line spacing.
Definition: font.c:70
MEMSPACE void * safecalloc(size_t nmemb, size_t size)
Safe Calloc - Display Error message if Calloc fails.
Definition: system.c:128
#define ILI9341_RED
MEMSPACE void tft_drawCircle(window *win, int16_t x0, int16_t y0, int16_t r, uint16_t color)
Draw a circle outline.
MEMSPACE int tft_touch_calibrate(window *win)
MEMSPACE void tft_window_init(window *win, int16_t x, int16_t y, int16_t w, int16_t h)
Initialize window structure we default values FIXME check x+w, y+h absolute limits against TFT limuts...
Definition: ili9341.c:1198
int tz_minuteswest
Definition: time.h:80
MEMSPACE void sep()
print seperator
Definition: stringsup.c:32
int inloop
Definition: user_main.c:532