PowerTGS440

simpleTFT Clock v1.5beta DEMO

Jun 20th, 2021 (edited)
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 39.77 KB | None | 0 0
  1. // ------------------------------------------------------------------------------------------------------------------- //
  2. //                                                                                                                     //
  3. // Simple ESP8266 NodeMCU 1.5 SimpleTFT Clock by TOMASZ MAJCHER (TRANSLINE GROUP POAND)                                //
  4. //                                                                                                                     //
  5. // e-mail  : [email protected]                                                                               //
  6. // licence : DonationWare                                                                                              //
  7. // paypal  : @TomaszMajcher                                                                                            //
  8. // www     : https://esp32dev.blogspot.com/                                                                            //
  9. //                                                                                                                     //
  10. // ------------------------------------------------------------------------------------------------------------------- //
  11. //
  12. // v1.0 Base (13.06.2021) by Tomasz Majcher
  13. //
  14. // ------------------------------------------------------------------------------------------------------------------- //
  15. //
  16. // ILI9341 Driver (Pinout) Hardware SPI !!!
  17. //
  18. // VCC to         :   3.3V
  19. // GND to         :   GND
  20. // CS pin to      :   D1
  21. // Reset pin to   :   Reset
  22. // DC pin to      :   D4
  23. // SDI pin to     :   D7
  24. // SCK pin to     :   D5
  25. //
  26. //
  27. // (20.06.2021) v1.5 beta  -  update : add TFT_ShowProgres (show progres bar for second)
  28. // (20.06.2021) v1.4 beta  -  update : add TFT_ShowBug (show flying bug)
  29. // (20.06.2021) v1.3 beta  -  update : add TFT_ShowDate (show date on TFT display) from Task3 (one per min)
  30. // (20.06.2021) v1.2 beta  -  update : add TFT_ShowTime (show time on TFT display) from Task0 (one per sec)
  31. // (20.06.2021) v1.1 beta  -  update : add TFT (test, setup library -> ILI9341, pinout)
  32. // ------------------------------------------------------------------------------------------------------------------- //
  33. // --- I N C L U D E   L I B R A R Y --------------------------------------------------------------------------------- //
  34. // ------------------------------------------------------------------------------------------------------------------- //
  35.  
  36. extern "C" char* sbrk(int incr);
  37.  
  38. #include <ESP8266WiFi.h>                                         // biblioteka Wi-Fi
  39. #include <NTPClient.h>                                           // NTP klient (Time)
  40. #include <WiFiUdp.h>                                             // UDP dla NTP (Time)
  41. #include <TaskScheduler.h>                                       // scheluder
  42. #include <TimeLib.h>
  43. #include <Arduino.h>
  44.  
  45. // ------------------------------------------------------------------------------------------------------------------- //
  46. // --- L A N G U A G E   S E T U P ----------------------------------------------------------------------------------- //
  47. // ------------------------------------------------------------------------------------------------------------------- //
  48.  
  49. #define LANGUAGE 0                                            // 0 (Polish), 1 (English), 2 (Germany)
  50.                                                              
  51. #if LANGUAGE == 0                                                
  52.     #define POLISH 1
  53.     #define ENGLISH 0
  54.     #define GERMANY 0                                          
  55. #endif
  56.  
  57. #if LANGUAGE == 1                                                      
  58.     #define POLISH 0                                        
  59.     #define ENGLISH 1
  60.     #define GERMANY 0
  61. #endif
  62.  
  63. #if LANGUAGE == 2
  64.     #define POLISH 0
  65.     #define ENGLISH 0
  66.     #define GERMANY 1
  67. #endif      
  68.  
  69. // ------------------------------------------------------------------------------------------------------------------- //
  70. // --- M A C R O   D E F I N E --------------------------------------------------------------------------------------- //
  71. // ------------------------------------------------------------------------------------------------------------------- //
  72.  
  73. #define ARRAY_SIZE(a)   (sizeof(a) / sizeof((a)[0]))
  74.  
  75. // ------------------------------------------------------------------------------------------------------------------- //
  76. // --- N E T W O R K   S E T T I N G S ------------------------------------------------------------------------------- //
  77. // ------------------------------------------------------------------------------------------------------------------- //
  78.  
  79. const char *ssid     = "DIABLO";                                       // network wifi ssid  
  80. const char *password = "XXXXXXXXXX";                                  // network wifi password
  81.  
  82. // ------------------------------------------------------------------------------------------------------------------- //
  83. // --- T F T --------------------------------------------------------------------------------------------------------- //
  84. // ------------------------------------------------------------------------------------------------------------------- //
  85.  
  86. #include "Free_Fonts.h"
  87. #include "SPI.h"
  88. #include "TFT_eSPI.h"
  89.  
  90. TFT_eSPI tft = TFT_eSPI();
  91.  
  92. // define color
  93.  
  94. #define TFT_LIGHTGRAY         tft.color565(163, 159, 158)
  95. #define TFT_SWEETBLUE         tft.color565(0, 128, 255)
  96.  
  97. // define day mode color
  98.  
  99. #define TFT_DAY_TEXT          TFT_BLACK
  100. #define TFT_DAY_BACKGROUND    TFT_LIGHTGRAY
  101.  
  102. // define night mode color
  103.  
  104. #define TFT_NIGHT_TEXT        TFT_LIGHTGRAY
  105. #define TFT_NIGHT_BACKGROUND  TFT_BLACK
  106.  
  107. // define cords
  108.  
  109. #define x_min 0
  110. #define y_min 0
  111. #define x_max 320
  112. #define y_max 240
  113. #define frame 5
  114.  
  115. // ------------------------------------------------------------------------------------------------------------------- //
  116. // --- S E R I A L   S E T T I N G S --------------------------------------------------------------------------------- //
  117. // ------------------------------------------------------------------------------------------------------------------- //
  118.  
  119. #define SERIAL true
  120. #define SerialSpeed 57600
  121.  
  122. // ------------------------------------------------------------------------------------------------------------------- //
  123. // --- P R O T O T Y P E --------------------------------------------------------------------------------------------- //
  124. // ------------------------------------------------------------------------------------------------------------------- //
  125.  
  126. void scheluderInit();                 // run from setup() - init scheluder
  127. void statWiFi();                      // show Wi-Fi Status
  128. void initWiFi();                      // init Wi-Fi connection
  129.  
  130. void TFT_Init();                      // init TFT (from setup)
  131. void TFT_ShowDate();                  // show Date on TFT
  132. void TFT_ShowTime();                  // show Time on TFT
  133. void TFT_ShowBug();                   // show BUG on TFT
  134.  
  135. // ------------------------------------------------------------------------------------------------------------------- //
  136. // --- P R O T O T Y P E --- C O R E T A S K ------------------------------------------------------------------------- //
  137. // ------------------------------------------------------------------------------------------------------------------- //
  138.  
  139. void Task0Callback();                 // function Task0 for ESP8266 Core
  140. void Task1Callback();                 // function Task1 for ESP8266 Core
  141. void Task2Callback();                 // function Task2 for ESP8266 Core
  142. void Task3Callback();                 // function Task3 for ESP8266 Core
  143. void Task4Callback();                 // function Task4 for ESP8266 Core
  144. void Task5Callback();                 // function Task4 for ESP8266 Core
  145. void Task6Callback();                 // function Task4 for ESP8266 Core
  146.  
  147. #define Micros_01         50          // 1/100 sekundy
  148. #define Second_01       1000          // 01 second timeout makro definition
  149. #define Second_05       5000          // 05 second timeout makro definition
  150. #define Second_10       10000         // 10 second timeout makro definition
  151. #define Second_30       30000         // 30 second timeout makro definition
  152. #define Second_60       60000         // 60 second timeout makro definition
  153. #define Minute_01       60000         // 01 minute timeout makro definition
  154. #define Minute_05      300000         // 05 minute timeout makro definition
  155. #define Minute_10      600000         // 10 minute timeout makro definition
  156. #define Minute_30     1800000         // 30 minute timeout makro definiction
  157. #define Minute_60     3600000         // 30 minute timeout makro definiction
  158.  
  159.  
  160. // ------------------------------------------------------------------------------------------------------------------- //
  161. // --- Z A D A N I A   S C H E L U D E R ' A ------------------------------------------------------------------------- //
  162. // ------------------------------------------------------------------------------------------------------------------- //
  163.                                                                                                    
  164. Task task_0( Second_01,  TASK_FOREVER, &Task0Callback);         // task_0 run at interval Second_01            
  165. Task task_1( Second_60,  TASK_FOREVER, &Task1Callback);         // task_1 run at interval Second_60            
  166. Task task_2( Second_01,  TASK_FOREVER, &Task2Callback);         // task_2 run at interval Second_10            
  167. Task task_3( Minute_01,  TASK_FOREVER, &Task3Callback);         // task_3 run at interval Minute_01  
  168. Task task_4( Minute_10,  TASK_FOREVER, &Task4Callback);         // task_4 run at interval Minute_10  
  169. Task task_5( Micros_01,  TASK_FOREVER, &Task5Callback);         // task_4 run at interval 1/10 sec
  170. Task task_6( Minute_60,  TASK_FOREVER, &Task6Callback);         // task_4 run at interval Minute_60  
  171.                                                                                                    
  172. Scheduler runner;    
  173.  
  174. // ------------------------------------------------------------------------------------------------------------------- //
  175. //                                              *** WiFi Reconect ***                                                  //
  176. // ------------------------------------------------------------------------------------------------------------------- //
  177.  
  178. unsigned long previousMillis = 0;
  179. unsigned long interval = 30000;
  180.  
  181. // ------------------------------------------------------------------------------------------------------------------- //
  182. //                                              *** STRUKTURA Time ***                                                 //
  183. // ------------------------------------------------------------------------------------------------------------------- //
  184.  
  185. struct TIME_DATA
  186. {
  187.     int     Hour = 0;
  188.     int     Minute  = 0;
  189.     int     Second = 0;
  190.     String  FormatedData  = "NULL";
  191.     String  ShortData = "NULL";
  192.     String  LongData = "NULL";
  193.     String  TFT_Data = "NULL";
  194.     String  TFT_Time = "NULL";
  195.     int     DayOfTheWeek = 0;
  196.     int     DayOfTheMonth = 0;
  197.     int     Month = 0;
  198.     int     Year = 0;
  199. }
  200. Time;
  201.  
  202.  
  203. // ------------------------------------------------------------------------------------------------------------------- //
  204. //                                              *** STRUKTURA DZIEN ***                                                //
  205. // ------------------------------------------------------------------------------------------------------------------- //
  206.  
  207. #if POLISH
  208.     const char Day_0 [] = "Niedziela";
  209.     const char Day_1 [] = "Poniedzialek";
  210.     const char Day_2 [] = "Wtorek";
  211.     const char Day_3 [] = "Sroda";
  212.     const char Day_4 [] = "Czwartek";
  213.     const char Day_5 [] = "Piatek";
  214.     const char Day_6 [] = "Sobota";
  215. #endif
  216.  
  217. #if ENGLISH
  218.     const char Day_0 [] = "Sunday";
  219.     const char Day_1 [] = "Monday";
  220.     const char Day_2 [] = "Tuesday";
  221.     const char Day_3 [] = "Wednesday";
  222.     const char Day_4 [] = "Thursday";
  223.     const char Day_5 [] = "Friday";
  224.     const char Day_6 [] = "Saturday";
  225. #endif
  226.  
  227. #if GERMANY
  228.     const char Day_0 [] = "Sonntag";
  229.     const char Day_1 [] = "Montag";
  230.     const char Day_2 [] = "Dienstag";
  231.     const char Day_3 [] = "Mittwoch";
  232.     const char Day_4 [] = "Donnerstag";
  233.     const char Day_5 [] = "Freitag";
  234.     const char Day_6 [] = "Samstag";
  235. #endif
  236.  
  237. const char * const DAY[] = { Day_0, Day_1, Day_2, Day_3, Day_4, Day_5, Day_6 }; // dni tygodnia [0..6]
  238.  
  239. // ------------------------------------------------------------------------------------------------------------------- //
  240. //                                              *** STRUKTURA MONTH ***                                                //
  241. // ------------------------------------------------------------------------------------------------------------------- //
  242.  
  243. #if POLISH
  244.     const char MONTH_0  [] = "NULL";
  245.     const char MONTH_1  [] = "Styczen";
  246.     const char MONTH_2  [] = "Luty";
  247.     const char MONTH_3  [] = "Marzec";
  248.     const char MONTH_4  [] = "Kwiecien";
  249.     const char MONTH_5  [] = "Maj";
  250.     const char MONTH_6  [] = "Czerwiec";
  251.     const char MONTH_7  [] = "Lipiec";
  252.     const char MONTH_8  [] = "Sierpien";
  253.     const char MONTH_9  [] = "Wrzesien";
  254.     const char MONTH_10 [] = "Pazdziernik";
  255.     const char MONTH_11 [] = "Listopad";
  256.     const char MONTH_12 [] = "Grudzien";
  257. #endif
  258.  
  259. #if ENGLISH
  260.     const char MONTH_0  [] = "NULL";
  261.     const char MONTH_1  [] = "January";
  262.     const char MONTH_2  [] = "February";
  263.     const char MONTH_3  [] = "March";
  264.     const char MONTH_4  [] = "April";
  265.     const char MONTH_5  [] = "May";
  266.     const char MONTH_6  [] = "June";
  267.     const char MONTH_7  [] = "July";
  268.     const char MONTH_8  [] = "August";
  269.     const char MONTH_9  [] = "September";
  270.     const char MONTH_10 [] = "October";
  271.     const char MONTH_11 [] = "November";
  272.     const char MONTH_12 [] = "December";
  273. #endif
  274.  
  275. #if GERMANY
  276.     const char MONTH_0  [] = "NULL";
  277.     const char MONTH_1  [] = "Januar";
  278.     const char MONTH_2  [] = "Februar";
  279.     const char MONTH_3  [] = "März";
  280.     const char MONTH_4  [] = "April";
  281.     const char MONTH_5  [] = "Mai";
  282.     const char MONTH_6  [] = "Juni";
  283.     const char MONTH_7  [] = "Juli";
  284.     const char MONTH_8  [] = "August";
  285.     const char MONTH_9  [] = "September";
  286.     const char MONTH_10 [] = "Oktober";
  287.     const char MONTH_11 [] = "November";
  288.     const char MONTH_12 [] = "Dezember";
  289. #endif
  290.    
  291.     const char * const MONTH[] = { MONTH_0, MONTH_1, MONTH_2, MONTH_3, MONTH_4, MONTH_5, MONTH_6,
  292.                                    MONTH_7, MONTH_8, MONTH_9, MONTH_10, MONTH_11, MONTH_12 };
  293.  
  294. // ------------------------------------------------------------------------------------------------------------------- //
  295. // --- T O O L S   C L A S S ----------------------------------------------------------------------------------------- //
  296. // ------------------------------------------------------------------------------------------------------------------- //  
  297.  
  298. class TOOLS
  299. {
  300.     private:
  301.  
  302.     public:
  303.       void TypeWriter(const String & message, unsigned int wait, bool lf = true);
  304.       void FreeRam(bool lf = true);
  305.       void CpuInfo(bool lf = true);
  306.       void WiFiInfo(bool lf = true);
  307.       inline void BreakLine();
  308.       inline void PrintLine(byte lenght = 60);
  309. };
  310.  
  311. // ------------------------------------------------------------------------------------------------------------------- //  
  312.  
  313. void TOOLS :: TypeWriter(const String & message, unsigned int wait, bool lf)
  314. {
  315.     if(lf) Serial.printf("\n");   // if lf is true print next line
  316.    
  317.     for(const char c : message)
  318.     {
  319.         Serial.print(c);    
  320.         delay(wait);  
  321.     }  
  322. }
  323.  
  324. // ------------------------------------------------------------------------------------------------------------------- //
  325.  
  326. void TOOLS :: FreeRam(bool lf)
  327. {
  328.     if(lf) Serial.printf("\n");   // if lf is true print next line
  329.  
  330.     Serial.printf("[TOOLS] [Memory Free] \t\t: %u [kB]", ESP.getFreeHeap());      
  331. }
  332.  
  333. // ------------------------------------------------------------------------------------------------------------------- //
  334.  
  335. void TOOLS :: CpuInfo(bool lf)
  336. {
  337.     if(lf) Serial.printf("\n");   // if lf is true print next line
  338.  
  339.     Serial.printf("[TOOLS] [CPU frequency] \t: %u [MHz]",ESP.getCpuFreqMHz());      
  340. }
  341.  
  342. // ------------------------------------------------------------------------------------------------------------------- //
  343.  
  344. void TOOLS :: WiFiInfo(bool lf)
  345. {
  346.     if(lf) Serial.printf("\n");   // if lf is true print next line
  347.    
  348.     Serial.printf("[TOOLS] [IP adress] \t\t: %s", WiFi.localIP().toString().c_str());
  349. }
  350.  
  351. // ------------------------------------------------------------------------------------------------------------------- //
  352.  
  353. inline void TOOLS :: BreakLine()
  354. {
  355.     Serial.printf("\n");   // if lf is true print next line
  356. }
  357.  
  358. // ------------------------------------------------------------------------------------------------------------------- //
  359.  
  360. inline void TOOLS :: PrintLine(byte lenght)
  361. {
  362.     for(byte i = 0; i <= lenght; i++) Serial.print("-");
  363. }
  364.  
  365. // ------------------------------------------------------------------------------------------------------------------- //
  366. // --- O B J E C T   I N I T ----------------------------------------------------------------------------------------- //
  367. // ------------------------------------------------------------------------------------------------------------------- //  
  368.  
  369. WiFiUDP ntpUDP;                                                
  370. NTPClient timeClient(ntpUDP, "pool.ntp.org", 3600);  
  371. TOOLS tools;
  372.  
  373. // ------------------------------------------------------------------------------------------------------------------- //
  374. // --- Task0Callback() ----------------------------------------------------------------------------------------------- //
  375. // ------------------------------------------------------------------------------------------------------------------- //
  376.  
  377. void Task0Callback()
  378. {
  379.     String bufor = "NULL";
  380.  
  381.     static bool STEP = false;
  382.  
  383.     if (Time.Hour < 10)
  384.     {
  385.         bufor =  "0";
  386.         bufor += String(Time.Hour);
  387.     }
  388.     else
  389.     {
  390.         bufor = String(Time.Hour);
  391.     }
  392.  
  393.     if (STEP)                           // step 0 sec show :
  394.     {
  395.         bufor += ":";
  396.         STEP = false;
  397.     }
  398.     else                                // step 1 sec show _
  399.     {
  400.         bufor += " ";
  401.         STEP = true;
  402.     }
  403.  
  404.     if (Time.Minute < 10)
  405.     {
  406.         bufor +=  "0";
  407.         bufor += String(Time.Minute);
  408.     }
  409.     else
  410.     {
  411.         bufor += String(Time.Minute);
  412.     }
  413.  
  414.     Time.TFT_Time = bufor;                        // copy bufor to TFT_Time
  415.  
  416.     TFT_ShowTime();                               // show time on TFT  
  417.  
  418. }
  419.  
  420. // ------------------------------------------------------------------------------------------------------------------- //
  421. // --- Task1Callback() ----------------------------------------------------------------------------------------------- //
  422. // ------------------------------------------------------------------------------------------------------------------- //
  423.  
  424. void Task1Callback()
  425. {
  426.     #if SERIAL
  427.        
  428.         String bufor;
  429.        
  430.         if (Time.Hour < 10)
  431.         {
  432.             bufor =  "0";
  433.             bufor += String(Time.Hour);
  434.         }
  435.         else
  436.         {
  437.             bufor = String(Time.Hour);
  438.         }
  439.  
  440.         bufor += ":";
  441.  
  442.         if (Time.Minute < 10)
  443.         {
  444.             bufor +=  "0";
  445.             bufor += String(Time.Minute);
  446.         }
  447.         else
  448.         {
  449.             bufor += String(Time.Minute);
  450.         }
  451.  
  452.         if (Time.Hour < 12) bufor += " AM";
  453.         else                bufor += " PM";
  454.        
  455.         Serial.printf("\n\n[ESP] [TASK1] [Time NTP] \t: %s", bufor.c_str());
  456.         Serial.printf("\n[ESP] [TASK1] [LongData]  \t: %s ", Time.LongData.c_str());
  457.         Serial.printf("\n[ESP] [TASK1] [ShortData] \t: %s ", Time.ShortData.c_str());
  458.        
  459.     #endif            
  460. }
  461.  
  462. // ------------------------------------------------------------------------------------------------------------------- //
  463. // --- Task2Callback() ----------------------------------------------------------------------------------------------- //
  464. // ------------------------------------------------------------------------------------------------------------------- //
  465.  
  466. void Task2Callback()
  467. {
  468.     timeClient.update();                                 // time update                                                        
  469.                                                                                    
  470.     Time.Hour    =  timeClient.getHours()+1;             // zapisz godzinę z serwera
  471.     Time.Minute  =  timeClient.getMinutes();             // zapisz minutę z serwera
  472.     Time.Second  =  timeClient.getSeconds();             // zapisz sekundę z serwera
  473.    
  474.     TFT_ShowProgres();
  475. }
  476.  
  477. // ------------------------------------------------------------------------------------------------------------------- //
  478. // --- Task3Callback() ----------------------------------------------------------------------------------------------- //
  479. // ------------------------------------------------------------------------------------------------------------------- //
  480.  
  481. void Task3Callback()
  482. {
  483.     tools.BreakLine();                                   // breaking line
  484.     tools.FreeRam();                                     // show Free RAM
  485.     tools.CpuInfo();                                     // Show CPU
  486.     tools.WiFiInfo();                                    // Show WiFi Adress
  487.  
  488.     TFT_ShowDate();                                      // Show Date on TFT
  489. }
  490.  
  491. // ------------------------------------------------------------------------------------------------------------------- //
  492. // --- Task4Callback() ----------------------------------------------------------------------------------------------- //
  493. // ------------------------------------------------------------------------------------------------------------------- //
  494.  
  495. void Task4Callback()
  496. {
  497.    
  498. }
  499.  
  500. // ------------------------------------------------------------------------------------------------------------------- //
  501. // --- Task3Callback() ----------------------------------------------------------------------------------------------- //
  502. // ------------------------------------------------------------------------------------------------------------------- //
  503.  
  504. void Task5Callback()
  505. {
  506.     TFT_ShowBug();          // pokaż robala
  507. }
  508. // ------------------------------------------------------------------------------------------------------------------- //
  509. // --- Task6Callback() ----------------------------------------------------------------------------------------------- //
  510. // ------------------------------------------------------------------------------------------------------------------- //
  511.  
  512. void Task6Callback()
  513. {
  514.     timeClient.update();                                                                              
  515.    
  516.     unsigned long epochTime = timeClient.getEpochTime();                                              
  517.     struct tm *ptm = gmtime ( (time_t *) &epochTime );                                          
  518.                                                                                                
  519.     Time.DayOfTheWeek      =   timeClient.getDay();                                          
  520.     Time.DayOfTheMonth     =   ptm->tm_mday;                                                
  521.     Time.Month             =   ptm->tm_mon+1;                                                
  522.     Time.Year              =   ptm->tm_year+1900;
  523.  
  524.     Time.FormatedData   =   String(DAY[Time.DayOfTheWeek])
  525.                         +   ", "
  526.                         +   String(Time.DayOfTheMonth)
  527.                         +   " "
  528.                         +   String(MONTH[Time.Month])
  529.                         +   " "
  530.                         +   String(Time.Year);    
  531.  
  532.     Time.ShortData      =   String(Time.DayOfTheMonth)
  533.                         +   " "
  534.                         +   String(MONTH[Time.Month])
  535.                         +   " "
  536.                         +   String(Time.Year);  
  537.  
  538.     Time.LongData       =   String(DAY[Time.DayOfTheWeek])
  539.                         +   " "
  540.                         +   String(Time.DayOfTheMonth)
  541.                         +   "/"
  542.                         +   String(Time.Month)
  543.                         +   "/"
  544.                         +   String(Time.Year);  
  545.  
  546.     String Buffor = "NULL";
  547.  
  548.     if(Time.DayOfTheMonth < 10)
  549.     {
  550.         Buffor = "0";
  551.         Buffor += Time.DayOfTheMonth;
  552.     }
  553.     else Buffor = Time.DayOfTheMonth;
  554.  
  555.     Buffor += "/";
  556.  
  557.     if(Time.Month < 10)
  558.     {
  559.         Buffor += "0";
  560.         Buffor += Time.Month;
  561.     }
  562.     else Buffor += Time.Month;
  563.    
  564.     Buffor += "/";
  565.     Buffor += Time.Year;    
  566.  
  567.     Time.TFT_Data = Buffor;
  568. }
  569.  
  570. // ------------------------------------------------------------------------------------------------------------------- //
  571. // --- stat Wi-Fi ---------------------------------------------------------------------------------------------------- //
  572. // ------------------------------------------------------------------------------------------------------------------- //
  573.  
  574. void statWiFi()
  575. {
  576.     switch (WiFi.status())
  577.     {
  578.         case WL_NO_SSID_AVAIL:
  579.                             #if SERIAL
  580.                                 #if ENGLISH
  581.                                     Serial.printf("\nConfigured SSID cannot be reached");
  582.                                 #endif
  583.                                 #if POLISH
  584.                                     Serial.printf("\nNie można osiągnąć skonfigurowanego identyfikatora SSID");
  585.                                 #endif
  586.                                 #if GERMANY
  587.                                     Serial.printf("\nKonfigurierte SSID ist nicht erreichbar");
  588.                                 #endif
  589.                             #endif        
  590.                             break;
  591.                            
  592.         case WL_CONNECTED:
  593.                             #if SERIAL
  594.                                 #if ENGLISH
  595.                                     Serial.printf("\nConnection successfully established");
  596.                                     Serial.printf("\nYour IP : %s", WiFi.localIP().toString().c_str());
  597.                                     Serial.printf("\nReceived Signal Strength Indication : %d", WiFi.RSSI());
  598.                                 #endif
  599.                                 #if POLISH
  600.                                     Serial.printf("\nPomyślnie nawiązano połączenie");
  601.                                     Serial.printf("\nTwój adres IP : %s", WiFi.localIP().toString().c_str());
  602.                                     Serial.printf("\nWskazanie siły odbieranego sygnału : %d", WiFi.RSSI());
  603.                                 #endif
  604.                                 #if GERMANY
  605.                                     Serial.printf("\nVerbindung erfolgreich hergestellt");
  606.                                     Serial.printf("\nDeine IP-Adresse : %s", WiFi.localIP().toString().c_str());
  607.                                     Serial.printf("\nAnzeige der empfangenen Signalstärke : %d", WiFi.RSSI());                                
  608.                                 #endif
  609.                             #endif
  610.                             break;
  611.                            
  612.         case WL_CONNECT_FAILED:
  613.                             #if SERIAL
  614.                                 #if ENGLISH
  615.                                     Serial.printf("\nConnection failed");
  616.                                 #endif
  617.                                 #if POLISH
  618.                                     Serial.printf("\nPołączenie nieudane");
  619.                                 #endif
  620.                                 #if GERMANY
  621.                                     Serial.printf("\nVerbindung fehlgeschlagen");
  622.                                 #endif
  623.                             #endif
  624.                             break;
  625.     }      
  626. }
  627.  
  628. // ------------------------------------------------------------------------------------------------------------------- //
  629. // --- init Wi-Fi ---------------------------------------------------------------------------------------------------- //
  630. // ------------------------------------------------------------------------------------------------------------------- //
  631.  
  632. void initWiFi()
  633. {
  634.     WiFi.mode(WIFI_STA);
  635.     WiFi.begin(ssid, password);
  636.  
  637.     #if SERIAL    
  638.         #if ENGLISH
  639.             Serial.printf("\nConnecting to WiFi ..");
  640.         #endif
  641.         #if POLISH
  642.             Serial.printf("\nŁącze z siecią WiFi ..");
  643.         #endif
  644.         #if GERMANY
  645.             Serial.printf("\nNetzwerkverbindung mit WLAN ..");
  646.         #endif
  647.     #endif
  648.    
  649.     while (WiFi.status() != WL_CONNECTED)
  650.     {
  651.         Serial.print('.');
  652.         delay(200);
  653.     }
  654.  
  655.     statWiFi();
  656.        
  657.     WiFi.setAutoReconnect(true);
  658.     WiFi.persistent(true);
  659. }
  660.  
  661. // ------------------------------------------------------------------------------------------------------------------- //
  662. // --- reconect Wi-Fi ------------------------------------------------------------------------------------------------ //
  663. // ------------------------------------------------------------------------------------------------------------------- //
  664.  
  665. void reconectWifi()
  666. {
  667.     unsigned long currentMillis = millis();
  668.    
  669.     if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis >=interval))
  670.     {
  671.         #if SERIAL
  672.             #if ENGLISH
  673.                 Serial.println("Reconnecting to WiFi...");
  674.             #endif
  675.             #if POLISH
  676.                 Serial.println("Ponowne łączenie z Wi-Fi...");
  677.             #endif
  678.             #if GERMANY
  679.                 Serial.println("Erneut mit WLAN verbinden...");
  680.             #endif
  681.         #endif
  682.        
  683.         WiFi.disconnect();
  684.         WiFi.begin(ssid, password);
  685.        
  686.         statWiFi();
  687.        
  688.         previousMillis = currentMillis;
  689.   }
  690. }
  691.  
  692. // ------------------------------------------------------------------------------------------------------------------- //
  693. // --- schedulerInit ------------------------------------------------------------------------------------------------- //
  694. // ------------------------------------------------------------------------------------------------------------------- //
  695.  
  696. void scheluderInit()
  697. {
  698.     runner.init();                                       // inicjalizuj scheluder
  699.                                                                                  
  700.     runner.addTask(task_0);                              // dodaj zadanie do scheluder
  701.     task_0.enable();                                     // włącz zadanie do scheluder
  702.     runner.addTask(task_1);                              // dodaj zadanie do scheluder
  703.     task_1.enable();                                     // włącz zadanie do scheluder
  704.     runner.addTask(task_2);                              // dodaj zadanie do scheluder
  705.     task_2.enable();                                     // włącz zadanie do scheluder
  706.     runner.addTask(task_3);                              // dodaj zadanie do scheluder
  707.     task_3.enable();                                     // włącz zadanie do scheluder
  708.     runner.addTask(task_4);                              // dodaj zadanie do scheluder
  709.     task_4.enable();                                     // włącz zadanie do scheluder
  710.     runner.addTask(task_5);                              // dodaj zadanie do scheluder
  711.     task_5.enable();                                     // włącz zadanie do scheluder
  712.     runner.addTask(task_6);                              // dodaj zadanie do scheluder
  713.     task_6.enable();                                     // włącz zadanie do scheluder  
  714. }
  715.  
  716. // ------------------------------------------------------------------------------------------------------------------- //
  717. // --- S E T U P --- T F T --- I N I T ------------------------------------------------------------------------------- //
  718. // ------------------------------------------------------------------------------------------------------------------- //
  719.  
  720. void TFT_Init()
  721. {
  722.     tft.begin();
  723.     tft.setRotation(1);
  724.     TFT_DrawPanel();    
  725. }
  726.  
  727. // ------------------------------------------------------------------------------------------------------------------- //
  728. // --- S E T U P ----------------------------------------------------------------------------------------------------- //
  729. // ------------------------------------------------------------------------------------------------------------------- //
  730.  
  731. void setup()
  732. {
  733.     #if SERIAL
  734.         Serial.begin(SerialSpeed);
  735.     #endif
  736.  
  737.     delay(200);
  738.        
  739.     tools.TypeWriter("Project by Tomasz Majcher", 100, true);  
  740.     tools.TypeWriter("Tools version 0.01", 100);
  741.     tools.BreakLine();
  742.    
  743.     initWiFi();
  744.     TFT_Init();
  745.     scheluderInit();
  746.  
  747.     Task6Callback();
  748. }
  749.  
  750. // ------------------------------------------------------------------------------------------------------------------- //
  751. // --- L O O P ------------------------------------------------------------------------------------------------------- //
  752. // ------------------------------------------------------------------------------------------------------------------- //
  753.  
  754. void loop()
  755. {
  756.     runner.execute();     // scheluder init
  757.     reconectWifi();       // check Wi-Fi status
  758. }
  759.  
  760. // ------------------------------------------------------------------------------------------------------------------- //
  761. // --- D R A W   P A N E L   T F T ----------------------------------------------------------------------------------- //
  762. // ------------------------------------------------------------------------------------------------------------------- //
  763.  
  764. void TFT_DrawPanel()
  765. {
  766.     tft.fillScreen(TFT_LIGHTGRAY);                                                      // BACKGROUND COLOR (E-paper)
  767.  
  768.     tft.drawFastVLine(x_min+frame,    y_min+frame,    y_max-2*(frame),  TFT_BLACK);     // LEFT LINE
  769.     tft.drawFastVLine(x_min+frame+1,  y_min+frame+1,  y_max-2*(frame),  TFT_BLACK);     // LEFT LINE
  770.     tft.drawFastVLine(x_max-frame,    y_min+frame,    y_max-2*(frame),  TFT_BLACK);     // RIGHT LINE
  771.     tft.drawFastVLine(x_max-frame-1,  y_min+frame+1,  y_max-2*(frame),  TFT_BLACK);     // RIGHT LINE
  772.  
  773.     tft.drawFastHLine(x_min+frame,    y_min+frame,    x_max-2*(frame),  TFT_BLACK);     // UP LINE
  774.     tft.drawFastHLine(x_min+frame,    y_min+frame+1,  x_max-2*(frame),  TFT_BLACK);     // UP LINE
  775.     tft.drawFastHLine(x_min+frame,    y_max-frame,    x_max-2*(frame),  TFT_BLACK);     // DOWN LINE
  776.     tft.drawFastHLine(x_min+frame+1,  y_max-frame-1,  x_max-2*(frame),  TFT_BLACK);     // DOWN LINE
  777.  
  778.     tft.drawFastHLine(x_min+frame,    y_min+frame+60,    x_max-2*(frame),  TFT_BLACK);   // CLOCK LINE DOWN
  779.     tft.drawFastHLine(x_min+frame,    y_min+frame+61,    x_max-2*(frame),  TFT_BLACK);   // CLOCK LINE DOWN
  780.     tft.drawFastVLine(x_min+157,      y_min+frame+1,     60,  TFT_BLACK);                // CLOCK LINE RIGHT
  781.     tft.drawFastVLine(x_min+156,      y_min+frame+1,     60,  TFT_BLACK);                // CLOCK LINE RIGHT
  782.  
  783.     tft.drawFastHLine(x_min+frame,    y_min+frame+80,    x_max-2*(frame),  TFT_BLACK);   // CLOCK LINE DOWN
  784.     tft.drawFastHLine(x_min+frame,    y_min+frame+81,    x_max-2*(frame),  TFT_BLACK);   // CLOCK LINE DOWN
  785. }
  786.  
  787. // ------------------------------------------------------------------------------------------------------------------- //
  788. // --- T F T   S H O W    D A T E -=---------------------------------------------------------------------------------- //
  789. // ------------------------------------------------------------------------------------------------------------------- //
  790.  
  791. void TFT_ShowDate()
  792. {
  793.     tft.setTextColor(TFT_BLACK, TFT_LIGHTGRAY);    
  794.     //tft.drawCentreString(DAY[Time.DayOfTheWeek], 240, 10, 4);    
  795.     tft.drawCentreString(Time.TFT_Data, 240, 25, 4);  
  796. }
  797.  
  798. // ------------------------------------------------------------------------------------------------------------------- //
  799. // --- T F T   S H O W    T I M E ------------------------------------------------------------------------------------ //
  800. // ------------------------------------------------------------------------------------------------------------------- //
  801.  
  802. void TFT_ShowTime()
  803. {
  804.     tft.setTextColor(TFT_BLACK, TFT_LIGHTGRAY);            
  805.     tft.drawString(Time.TFT_Time, 12, 12, 7);      
  806. }
  807.  
  808. // ------------------------------------------------------------------------------------------------------------------- //
  809. // --- T F T   P R O G R E S   T I M E ------------------------------------------------------------------------------- //
  810. // ------------------------------------------------------------------------------------------------------------------- //
  811.  
  812. void TFT_ShowProgres()
  813. {
  814.     int progres = Time.Second;
  815.     static bool F_Clear = false;
  816.  
  817.     if (progres>50) F_Clear = true;
  818.  
  819.     if ( !progres && F_Clear )
  820.     {
  821.         tft.fillRect(13, 70, 300, 12, TFT_DAY_BACKGROUND);                    // fast clear progres bar    
  822.         F_Clear = false;
  823.     }
  824.     else
  825.     if ( progres < 5 && F_Clear )
  826.     {
  827.         tft.fillRect(13, 70, 300, 12, TFT_DAY_BACKGROUND);                    // fast clear progres bar    
  828.         F_Clear = false;
  829.     }
  830.  
  831.     for(int i = 0; i < progres; i++)
  832.     {
  833.         tft.fillRect(13+(i*5), 70, 3, 12, TFT_DAY_TEXT);                       // fast draw progres bar
  834.     }
  835.  
  836. }
  837.  
  838. // ------------------------------------------------------------------------------------------------------------------- //
  839. // --- T F T   S H O W    B U G -  ----------------------------------------------------------------------------------- //
  840. // ------------------------------------------------------------------------------------------------------------------- //
  841.  
  842. void TFT_ShowBug()
  843. {
  844.     static int position_x = 0;
  845.     static int position_y = 0;
  846.     static bool step_1 = true;
  847.     static bool step_2 = false;
  848.     static bool step_3 = false;
  849.     static bool step_4 = false;
  850.  
  851.     if (step_1)
  852.     {
  853.         if (position_x<140)
  854.         {
  855.             tft.fillRect(165+position_x, 15, 1, 5, TFT_DAY_BACKGROUND);
  856.             position_x ++;
  857.             tft.fillRect(165+position_x, 15, 5, 5, TFT_DAY_TEXT);
  858.         }        
  859.         else
  860.         {
  861.             step_1 = false;   // stop move bug to right
  862.             step_2 = true;    // start move bug to down
  863.         }            
  864.     }
  865.     else
  866.     if (step_2)
  867.     {
  868.         if (position_y<35)
  869.         {
  870.             tft.fillRect(165+position_x, 15+position_y, 5, 1, TFT_DAY_BACKGROUND);
  871.             position_y ++;
  872.             tft.fillRect(165+position_x, 15+position_y, 5, 5, TFT_DAY_TEXT);      
  873.         }
  874.         else
  875.         {
  876.             step_2 = false;   // stop move bug to down
  877.             step_3 = true;    // start move bug to left
  878.         }
  879.     }
  880.     else
  881.     if (step_3)
  882.     {
  883.         if (position_x>0)
  884.         {
  885.             tft.fillRect(165+position_x+5, 15+position_y, 1, 5, TFT_DAY_BACKGROUND);
  886.             position_x --;
  887.             tft.fillRect(165+position_x, 15+position_y, 5, 5, TFT_DAY_TEXT);  
  888.         }
  889.         else
  890.         {
  891.             tft.fillRect(165+position_x+5, 15+position_y, 1, 5, TFT_DAY_BACKGROUND);
  892.             step_3 = false;
  893.             step_4 = true;
  894.         }
  895.     }
  896.     else
  897.     if (step_4)
  898.     {
  899.         if (position_y>0)
  900.         {
  901.             tft.fillRect(165+position_x, 15+position_y+5, 5, 1, TFT_DAY_BACKGROUND);
  902.             position_y --;
  903.             tft.fillRect(165+position_x, 15+position_y, 5, 5, TFT_DAY_TEXT);        
  904.         }
  905.         else
  906.         {
  907.             tft.fillRect(165+position_x, 15+position_y+5, 5, 1, TFT_DAY_BACKGROUND);
  908.             step_4 = false;
  909.             step_1 = true;
  910.         }
  911.     }
  912. }
Add Comment
Please, Sign In to add comment