PowerTGS440

ESP8266 Morphing Clock

Mar 22nd, 2022 (edited)
656
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 54.03 KB | None | 0 0
  1. /*
  2.  * Morphing Clock by Tomasz Majcher (Transline Group)
  3.  * version      : 1.1 beta (StartUp)
  4.  * date         : 22.03.2022
  5.  * e-mail       : [email protected]
  6.  * mobile       : +48 667082121
  7.  */
  8.  
  9. extern "C" char* sbrk(int incr);
  10.  
  11. /*
  12.  * History
  13.  *
  14.  * version 0.9      : build -> add DHT11 sensor and tested : build buffor for humidity & devPoint
  15.  * version 0.8      : build -> add DHT11 sensor and tested : show temperature on PxMatrix
  16.  * version 0.7      : test
  17.  * version 0.6      : build
  18.  * version 0.5      : build
  19.  * version 0.4      : build
  20.  * version 0.3      : build
  21.  */
  22.  
  23. #define ARRAY_SIZE(a)   (sizeof(a) / sizeof((a)[0]))
  24.  
  25. #define FPSTR(pstr_pointer) (reinterpret_cast<const __FlashStringHelper *>(pstr_pointer))
  26. #define F(string_literal) (FPSTR(PSTR(string_literal)))
  27.  
  28. /***************************************************************************************************************/
  29. /*!
  30.     @define   macro
  31.     @create   macro-time
  32. */
  33. /***************************************************************************************************************/
  34.  
  35. #define TIME_01S      1000                                     // 1 sekunda
  36. #define TIME_10S     10000                                     // 10 sekund
  37. #define TIME_01M     60000                                     // 1 minuta
  38. #define TIME_05M    360000                                     // 5 minut
  39. #define TIME_10M    600000                                     // 10 minut
  40. #define TINE_01H   3600000                                     // 1 godzina
  41.  
  42. /***************************************************************************************************************/
  43. /*!
  44.     @library  PxMatrix.h
  45.     @include  library, font
  46.     @define   pin, width, height
  47.     @create   object PxMatrix
  48. */
  49. /***************************************************************************************************************/
  50.  
  51. #include <PxMatrix.h>                                       // załączenie biblioteki
  52. #include "FreeMonoBold12pt7b.h"                             // załączenie czcionek
  53. #include "kongtext4pt7b.h"                                  // załączenie czcionek
  54.  
  55. #define P_LAT 16                                            // definicja pin : P_LAT
  56. #define P_A 5                                               // definicja pin : P_A
  57. #define P_B 4                                               // definicja pin : P_B
  58. #define P_C 15                                              // definicja pin : P_C
  59. #define P_D 12                                              // definicja pin : P_D
  60. #define P_E 0                                               // definicja pin : P_E
  61. #define P_OE 2                                              // definicja pin : P_OE
  62.  
  63. #define matrix_width 64                                     // definicja  : szerokość wyświetlacza
  64. #define matrix_height 32                                    // definicja  : wysokość wyświetlacza
  65.  
  66. uint8_t display_draw_time=10;                               // zmienna : display_draw_time
  67.  
  68. PxMATRIX display(64,32,P_LAT, P_OE,P_A,P_B,P_C,P_D);        // utwórz obiekt display klasy PxMatrix
  69.  
  70. /***************************************************************************************************************/
  71. /*!
  72.     @library  Ticker.h
  73.     @include  library
  74.     @define   function
  75. */
  76. /***************************************************************************************************************/
  77.  
  78. #include <Ticker.h>
  79.  
  80. Ticker display_ticker;
  81.  
  82. void display_updater();                                     // definicja funkcji
  83. void display_update_enable(bool is_enable);                 // definicja funkcji
  84.  
  85. /***************************************************************************************************************/
  86. /*!
  87.     @value    Digit    
  88.     @class    Digit
  89. */
  90. /***************************************************************************************************************/
  91.  
  92. const byte sA = 0;
  93. const byte sB = 1;
  94. const byte sC = 2;
  95. const byte sD = 3;
  96. const byte sE = 4;
  97. const byte sF = 5;
  98. const byte sG = 6;
  99. const int segHeight = 6;
  100. const int segWidth = segHeight;
  101. const uint16_t height = 31;
  102. const uint16_t width = 63;
  103.  
  104. byte digitBits[] =
  105. {
  106.     B11111100, // 0 ABCDEF--
  107.     B01100000, // 1 -BC-----
  108.     B11011010, // 2 AB-DE-G-
  109.     B11110010, // 3 ABCD--G-
  110.     B01100110, // 4 -BC--FG-
  111.     B10110110, // 5 A-CD-FG-
  112.     B10111110, // 6 A-CDEFG-
  113.     B11100000, // 7 ABC-----
  114.     B11111110, // 8 ABCDEFG-
  115.     B11110110, // 9 ABCD_FG-
  116. };
  117.  
  118. uint16_t black;
  119.  
  120. /***************************************************************************************************************/
  121. /*!
  122.     @class    Digit    
  123. */
  124. /***************************************************************************************************************/
  125.  
  126. class Digit
  127. {
  128.     public:
  129.         Digit(PxMATRIX* d, byte value, uint16_t xo, uint16_t yo, uint16_t color);
  130.         void Draw(byte value);
  131.         void Morph(byte newValue);
  132.         byte Value();
  133.         void DrawColon(uint16_t c);
  134.    
  135.     private:
  136.         PxMATRIX* _display;
  137.         byte _value;
  138.         uint16_t _color;
  139.         uint16_t xOffset;
  140.         uint16_t yOffset;
  141.         int animSpeed = 30;
  142.  
  143.         void drawPixel(uint16_t x, uint16_t y, uint16_t c);
  144.         void drawFillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t c);
  145.         void drawLine(uint16_t x, uint16_t y, uint16_t x2, uint16_t y2, uint16_t c);
  146.         void drawSeg(byte seg);
  147.         void Morph2();
  148.         void Morph3();
  149.         void Morph4();
  150.         void Morph5();
  151.         void Morph6();
  152.         void Morph7();
  153.         void Morph8();
  154.         void Morph9();
  155.         void Morph0();
  156.         void Morph1();
  157. };
  158.  
  159. /***************************************************************************************************************/
  160. /*!
  161.     @library  Digit.h
  162.     @include  library
  163.     @define  
  164.     @create   object
  165. */
  166. /***************************************************************************************************************/
  167.  
  168. Digit digit0(&display, 0, 63 - 1 - 9*1, 8, display.color565(0, 0, 255));
  169. Digit digit1(&display, 0, 63 - 1 - 9*2, 8, display.color565(0, 0, 255));
  170. Digit digit2(&display, 0, 63 - 4 - 9*3, 8, display.color565(0, 0, 255));
  171. Digit digit3(&display, 0, 63 - 4 - 9*4, 8, display.color565(0, 0, 255));
  172. Digit digit4(&display, 0, 63 - 7 - 9*5, 8, display.color565(0, 0, 255));
  173. Digit digit5(&display, 0, 63 - 7 - 9*6, 8, display.color565(0, 0, 255));
  174.  
  175. /***************************************************************************************************************/
  176. /*!
  177.     @library  ESP8266WiFi.h
  178.     @include  library
  179.     @define   ssid, password
  180.     @create   object
  181. */
  182. /***************************************************************************************************************/
  183.  
  184. #include <ESP8266WiFi.h>                                          // biblioteka do obsługi wi-fi
  185.  
  186. const char *ssid     = "HOME_SERV";                                  // network wifi ssid  
  187. const char *password = "xxxxxxxxxxxxxx";                             // network wifi password
  188.  
  189. unsigned long previousMillis = 0;                                 // re-conect Wi-FI
  190. unsigned long interval = 30000;                                   // re-conect Wi-FI
  191.  
  192. /***************************************************************************************************************/
  193. /*!
  194.     @library      TaskScheduler.h
  195.     @include      library
  196.     @declaration  function
  197.     @create       object
  198. */
  199. /***************************************************************************************************************/
  200.  
  201. #include <TaskScheduler.h>                                                // scheluder
  202.  
  203. void Task0Callback();                                                     // funkcja dla task_0    
  204. void Task1Callback();                                                     // funkcja dla task_1    
  205. void Task2Callback();                                                     // funkcja dla task_2    
  206. void Task3Callback();                                                     // funkcja dla task_3    
  207. void Task4Callback();                                                     // funkcja dla task_4    
  208.  
  209. Task task_0(  TIME_01S,  TASK_FOREVER, &Task0Callback);                   // task_0 - co 1 sekundę
  210. Task task_1(  TIME_01M,  TASK_FOREVER, &Task1Callback);                   // task_1 - co 1 sekundę  
  211. Task task_2(  TIME_01S,  TASK_FOREVER, &Task2Callback);                   // task_2 - co 1 sekundę  
  212. Task task_3(  TIME_10M,  TASK_FOREVER, &Task3Callback);                   // task_3 - co 1 sekundę
  213. Task task_4(  TIME_05M,  TASK_FOREVER, &Task4Callback);                   // task_4 - co 5 minut                                                                                                      
  214.  
  215. Scheduler runner;      
  216.  
  217. /***************************************************************************************************************/
  218. /*!
  219.     @library      TimeLib.h, WifiUdp.h, NTPClient_Generic.h
  220.     @include      library
  221.     @declaration  timefunction
  222.     @create       object
  223. */
  224. /***************************************************************************************************************/
  225.  
  226. #include <NTPClient_Generic.h>                                    // NTP klient (Time)
  227. #include <WiFiUdp.h>                                              // UDP dla NTP (Time)
  228. #include <TimeLib.h>                                              // TimeLib
  229.  
  230. WiFiUDP ntpUDP;  
  231.  
  232. char timeServer[] = "0.pl.pool.ntp.org";
  233.    
  234. #define TIME_ZONE_OFFSET_HRS            1                        // Europe, Poland (2)
  235. #define NTP_UPDATE_INTERVAL_MS          60000L                   // update interval
  236.  
  237. NTPClient timeClient(ntpUDP, timeServer, (3600 * TIME_ZONE_OFFSET_HRS), NTP_UPDATE_INTERVAL_MS);                                    
  238.  
  239. bool F_TimeUpdate = false;                                       // flag to update (second)
  240. unsigned long prevEpoch;
  241. byte prevhh;
  242. byte prevmm;
  243. byte prevss;
  244.  
  245. /***************************************************************************************************************/
  246. /*!
  247.     @library      dht11.h
  248.     @include      library
  249.     @declaration  temperature, humidity
  250.     @create       object
  251. */
  252. /***************************************************************************************************************/
  253.  
  254. #include <dht11.h>
  255. #define DHT_PIN 0
  256.  
  257. dht11 DHT11;
  258.  
  259.  
  260. /***************************************************************************************************************/
  261. /*
  262.     @declaration   function
  263. */
  264. /***************************************************************************************************************/
  265.  
  266. void statWiFi();
  267. void initWiFi();
  268. void reconectWifi();
  269. void SerialTypeWriter(const String & message, unsigned int wait);
  270. void ShowInfo();                                                        // pokaż INTRO
  271. void PM_ShowTemperature();
  272.  
  273.  
  274. /***************************************************************************************************************/
  275. /*!
  276.     @colour   standard    
  277. */
  278. /***************************************************************************************************************/
  279.  
  280. uint16_t myRED = display.color565(255, 0, 0);
  281. uint16_t myGREEN = display.color565(0, 255, 0);
  282. uint16_t myBLUE = display.color565(0, 0, 255);
  283. uint16_t myWHITE = display.color565(255, 255, 255);
  284. uint16_t myYELLOW = display.color565(255, 255, 0);
  285. uint16_t myCYAN = display.color565(0, 255, 255);
  286. uint16_t myMAGENTA = display.color565(255, 0, 255);
  287. uint16_t myBLACK = display.color565(0, 0, 0);
  288. uint16_t mySha = display.color565(54,47,36);
  289. uint16_t myROSE = display.color565(255,0,127);
  290.  
  291. uint16_t myCOLORS[8]  = {myRED,myGREEN,myBLUE,myWHITE,myYELLOW,myCYAN,myMAGENTA,myBLACK};
  292. uint16_t myCOLORS2[9] = {myRED, myGREEN, myWHITE, myMAGENTA, myBLUE, myYELLOW, myCYAN, mySha, myROSE};
  293.  
  294. /***************************************************************************************************************/
  295. /*!
  296.     @colour   Blue    
  297. */
  298. /***************************************************************************************************************/
  299.  
  300. uint16_t myBlue0 = display.color565(0,   0, 255);
  301. uint16_t myBlue1 = display.color565(0,  30, 255);
  302. uint16_t myBlue2 = display.color565(0,  60, 255);
  303. uint16_t myBlue3 = display.color565(0,  90, 255);
  304. uint16_t myBlue4 = display.color565(0, 120, 255);
  305. uint16_t myBlue5 = display.color565(0, 150, 255);
  306. uint16_t myBlue6 = display.color565(0, 180, 255);
  307. uint16_t myBlue7 = display.color565(0, 210, 255);
  308. uint16_t myBlue8 = display.color565(0, 250, 255);
  309.  
  310. uint16_t myBlue[9] = {myBlue0, myBlue1, myBlue2, myBlue3, myBlue4, myBlue5, myBlue6, myBlue7, myBlue8};
  311.  
  312. /***************************************************************************************************************/
  313. /*!
  314.     @colour   Dark
  315. */    
  316. /***************************************************************************************************************/
  317. uint16_t myDark0 = display.color565(20, 20, 20);
  318. uint16_t myDark1 = display.color565(25, 25, 25);
  319. uint16_t myDark2 = display.color565(30, 30, 30);
  320. uint16_t myDark3 = display.color565(35, 35, 35);
  321. uint16_t myDark4 = display.color565(40, 40, 40);
  322. uint16_t myDark5 = display.color565(45, 45, 45);
  323. uint16_t myDark6 = display.color565(50, 50, 50);
  324. uint16_t myDark7 = display.color565(55, 55, 55);
  325. uint16_t myDark8 = display.color565(60, 60, 60);
  326.  
  327. uint16_t myDark[9] = {myDark0, myDark1, myDark2, myDark3, myDark4, myDark5, myDark6, myDark7, myDark8};
  328.  
  329. /***************************************************************************************************************/
  330. /*!
  331.     @colour   DarkRed
  332. */
  333. /***************************************************************************************************************/
  334.  
  335. uint16_t myDarkRed0 = display.color565(20, 1,   8);
  336. uint16_t myDarkRed1 = display.color565(31, 1,  12);
  337. uint16_t myDarkRed2 = display.color565(36, 1,  14);
  338. uint16_t myDarkRed3 = display.color565(41, 1,  16);
  339. uint16_t myDarkRed4 = display.color565(46, 1,  18);
  340. uint16_t myDarkRed5 = display.color565(50, 1,  20);
  341. uint16_t myDarkRed6 = display.color565(54, 1,  24);
  342. uint16_t myDarkRed7 = display.color565(58, 1,  28);
  343. uint16_t myDarkRed8 = display.color565(64, 1,  32);
  344.  
  345. uint16_t myDarkRed[9] = {myDarkRed0, myDarkRed1, myDarkRed2, myDarkRed3, myDarkRed4, myDarkRed5, myDarkRed6, myDarkRed7, myDarkRed8};
  346.  
  347. /***************************************************************************************************************/
  348. /*!
  349.     @colour   DarkSea  
  350. */
  351. /***************************************************************************************************************/
  352. uint16_t myDarkSea0 = display.color565(1, 10,  32);
  353. uint16_t myDarkSea1 = display.color565(1, 14,  36);
  354. uint16_t myDarkSea2 = display.color565(1, 16,  38);
  355. uint16_t myDarkSea3 = display.color565(1, 20,  40);
  356. uint16_t myDarkSea4 = display.color565(1, 24,  42);
  357. uint16_t myDarkSea5 = display.color565(1, 28,  44);
  358. uint16_t myDarkSea6 = display.color565(1, 32,  46);
  359. uint16_t myDarkSea7 = display.color565(1, 36,  48);
  360. uint16_t myDarkSea8 = display.color565(1, 40,  50);
  361.  
  362. uint16_t myDarkSea[9] = {myDarkSea0, myDarkSea1, myDarkSea2, myDarkSea3, myDarkSea4, myDarkSea5, myDarkSea6, myDarkSea7, myDarkSea8};
  363.  
  364. /***************************************************************************************************************/
  365. /*!
  366.     @definition   Weekday, Month
  367. */
  368. /***************************************************************************************************************/
  369.  
  370. const char* Weekday[7] = {"Sunday", "Monday", "Tuesday", "Wednesday","Thursday", "Friday", "Saturday"};
  371. const char* Months []= {"Jan", "Feb", "Mar", "Apr", "May", "Jun","Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
  372.  
  373. /***************************************************************************************************************/
  374. /*!
  375.     @define   Serial Setup & Language Setup
  376. */
  377. /***************************************************************************************************************/
  378.  
  379. #define LANGUAGE 0                                            // 0 (Polish), 1 (English), 2 (Germany)
  380.                                                                
  381. #if LANGUAGE == 0                                                
  382.     #define POLISH 1
  383.     #define ENGLISH 0
  384.     #define GERMANY 0                                          
  385. #endif
  386.    
  387. #if LANGUAGE == 1                                                      
  388.     #define POLISH 0                                        
  389.     #define ENGLISH 1
  390.     #define GERMANY 0
  391. #endif
  392.  
  393. #if LANGUAGE == 2
  394.     #define POLISH 0
  395.     #define ENGLISH 0
  396.     #define GERMANY 1
  397. #endif      
  398.  
  399. #define SERIAL true
  400. #define SerialSpeed 57600
  401.  
  402. #define DEBUG true
  403.  
  404. /***************************************************************************************************************/
  405. /*    
  406.     @declaration    function
  407.     @function       display_updater()
  408.     @function       display_update_enable(bool is_enable)
  409. */
  410. /***************************************************************************************************************/
  411.  
  412. void display_updater()
  413. {
  414.     display.display(display_draw_time);
  415. }
  416.  
  417. void display_update_enable(bool is_enable)
  418. {
  419.     if (is_enable)
  420.     {
  421.         display_ticker.attach(0.004, display_updater);
  422.     }
  423.     else
  424.     {
  425.         display_ticker.detach();
  426.     }
  427. }
  428.  
  429. /***************************************************************************************************************/
  430. /*    
  431.     @struct         TIME_DATA    
  432. */
  433. /***************************************************************************************************************/
  434.  
  435. struct TIME_DATA
  436. {
  437.       int Hour = 0;
  438.       int Minute  = 0;
  439.       int Second = 0;    
  440.       int DayOfTheWeek = 0;
  441.       int DayOfTheMonth = 0;
  442.       int Day = 0;
  443.       int Month = 0;
  444.       int Year = 0;
  445.       String formated_date = "";
  446. }
  447. MyClock;
  448.  
  449. /***************************************************************************************************************/
  450. /*    
  451.     @struct         HomeClimat  
  452. */
  453. /***************************************************************************************************************/
  454.  
  455. struct  HOME_CLIMAT
  456. {
  457.     float  inside_temperature  = 0;
  458.     int    inside_humidity     = 0;
  459.     float  inside_devpoint     = 0;
  460.     char   temp_char_buffer[20];                        // bufor
  461.     String buf_temperature = "no data";                 // bufor
  462.     String buf_humidity    = "no data";                 // bufor
  463.     String buf_devpoint    = "no data";                 // bufor
  464. }
  465. mydht;
  466.  
  467. /***************************************************************************************************************/
  468. /*    
  469.     @declaration    DAY PL, EN, DE    
  470. */
  471. /***************************************************************************************************************/
  472.  
  473. #if POLISH
  474.       const char Day_0 [] = "Niedziela";
  475.       const char Day_1 [] = "Poniedzialek";
  476.       const char Day_2 [] = "Wtorek";
  477.       const char Day_3 [] = "Sroda";
  478.       const char Day_4 [] = "Czwartek";
  479.       const char Day_5 [] = "Piatek";
  480.       const char Day_6 [] = "Sobota";
  481. #endif
  482.  
  483. #if ENGLISH
  484.       const char Day_0 [] = "Sunday";
  485.       const char Day_1 [] = "Monday";
  486.       const char Day_2 [] = "Tuesday";
  487.       const char Day_3 [] = "Wednesday";
  488.       const char Day_4 [] = "Thursday";
  489.       const char Day_5 [] = "Friday";
  490.       const char Day_6 [] = "Saturday";
  491. #endif
  492.  
  493. #if GERMANY
  494.       const char Day_0 [] = "Sonntag";
  495.       const char Day_1 [] = "Montag";
  496.       const char Day_2 [] = "Dienstag";
  497.       const char Day_3 [] = "Mittwoch";
  498.       const char Day_4 [] = "Donnerstag";
  499.       const char Day_5 [] = "Freitag";
  500.       const char Day_6 [] = "Samstag";
  501. #endif
  502.  
  503. const char * const DAY[] = { Day_0, Day_1, Day_2, Day_3, Day_4, Day_5, Day_6 }; // dni tygodnia [0..6]
  504.  
  505. /***************************************************************************************************************/
  506. /*    
  507.     @declaration    MONTH PL, EN, DE    
  508. */
  509. /***************************************************************************************************************/
  510.  
  511. #if POLISH
  512.       const char MONTH_0  [] = "NULL";
  513.       const char MONTH_1  [] = "Styczen";
  514.       const char MONTH_2  [] = "Luty";
  515.       const char MONTH_3  [] = "Marzec";
  516.       const char MONTH_4  [] = "Kwiecien";
  517.       const char MONTH_5  [] = "Maj";
  518.       const char MONTH_6  [] = "Czerwiec";
  519.       const char MONTH_7  [] = "Lipiec";
  520.       const char MONTH_8  [] = "Sierpien";
  521.       const char MONTH_9  [] = "Wrzesien";
  522.       const char MONTH_10 [] = "Pazdziernik";
  523.       const char MONTH_11 [] = "Listopad";
  524.       const char MONTH_12 [] = "Grudzien";
  525. #endif
  526.  
  527. #if ENGLISH
  528.       const char MONTH_0  [] = "NULL";
  529.       const char MONTH_1  [] = "January";
  530.       const char MONTH_2  [] = "February";
  531.       const char MONTH_3  [] = "March";
  532.       const char MONTH_4  [] = "April";
  533.       const char MONTH_5  [] = "May";
  534.       const char MONTH_6  [] = "June";
  535.       const char MONTH_7  [] = "July";
  536.       const char MONTH_8  [] = "August";
  537.       const char MONTH_9  [] = "September";
  538.       const char MONTH_10 [] = "October";
  539.       const char MONTH_11 [] = "November";
  540.       const char MONTH_12 [] = "December";
  541. #endif
  542.  
  543. #if GERMANY
  544.       const char MONTH_0  [] = "NULL";
  545.       const char MONTH_1  [] = "Januar";
  546.       const char MONTH_2  [] = "Februar";
  547.       const char MONTH_3  [] = "März";
  548.       const char MONTH_4  [] = "April";
  549.       const char MONTH_5  [] = "Mai";
  550.       const char MONTH_6  [] = "Juni";
  551.       const char MONTH_7  [] = "Juli";
  552.       const char MONTH_8  [] = "August";
  553.       const char MONTH_9  [] = "September";
  554.       const char MONTH_10 [] = "Oktober";
  555.       const char MONTH_11 [] = "November";
  556.       const char MONTH_12 [] = "Dezember";
  557. #endif
  558.      
  559. const char * const MONTH[] = { MONTH_0, MONTH_1, MONTH_2, MONTH_3, MONTH_4, MONTH_5, MONTH_6,
  560.                                MONTH_7, MONTH_8, MONTH_9, MONTH_10, MONTH_11, MONTH_12 };
  561.  
  562.  
  563. /***************************************************************************************************************/
  564. /*    
  565.     @definition     Task0Callback
  566.     @interval       1 sekunda
  567.     @task           morphing clock
  568. */
  569. /***************************************************************************************************************/
  570.  
  571. void Task0Callback()
  572. {
  573.    
  574. }
  575.  
  576. /***************************************************************************************************************/
  577. /*    
  578.     @definition    Task1Callback
  579. */
  580. /***************************************************************************************************************/
  581.  
  582. void Task1Callback()
  583. {
  584.     #if SERIAL
  585.         #if ENGLISH
  586.             Serial.printf_P( PSTR ("\n\n[ESP] [TASK1] Hello from Task1Callback :-)") );
  587.         #endif
  588.         #if POLISH
  589.             Serial.printf_P( PSTR ("\n\n[ESP] [TASK1] Uruchomiono Task1Callback :-)") );
  590.         #endif
  591.         #if GERMANY
  592.             Serial.printf_P( PSTR ("\n\n[ESP] [TASK1] Sie sind in Task1Callback :-)") );
  593.         #endif
  594.     #endif
  595.    
  596.     #if SERIAL
  597.         Serial.printf_P( PSTR( "\n\n[ESP] [TASK1] [Memory Free]     : %u [kB]" ), ESP.getFreeHeap());      
  598.         Serial.printf_P( PSTR( "\n[ESP] [TASK1] [CPU frequency]   : %u [MHz]" ),ESP.getCpuFreqMHz());  
  599.         Serial.printf_P( PSTR( "\n[ESP] [TASK1] [WiFi IP]         : " )); Serial.print(WiFi.localIP() );
  600.        
  601.         #if POLISH
  602.            Serial.printf("\n[ESP] [TASK1] [Local Time]      : ");
  603.            Serial.print(String() + timeClient.getStrHours() + ":"
  604.                                    + timeClient.getStrMinutes() + ":"
  605.                                    + timeClient.getStrSeconds() + " "
  606.                                    + DAY[timeClient.WeekDay()-1] + " "                                  
  607.                                    + timeClient.getDay() + "/"
  608.                                    + timeClient.getMonth() + "/"
  609.                                    + timeClient.getYear());          
  610.         #endif        
  611.         #if ENGLISH
  612.            Serial.printf("\n[ESP] [TASK1] [Local Time]      : ");
  613.            Serial.print(String() + timeClient.getStrHours() + ":"
  614.                                    + timeClient.getStrMinutes() + ":"
  615.                                    + timeClient.getStrSeconds() + " "
  616.                                    + DAY[timeClient.WeekDay()-1] + " "
  617.                                    + timeClient.getDay() + "/"
  618.                                    + timeClient.getMonth() + "/"
  619.                                    + timeClient.getYear());          
  620.        #endif        
  621.        #if GERMANY
  622.            Serial.printf("\n[ESP] [TASK1] [Local Time]      : ");
  623.            Serial.print(String() + timeClient.getStrHours() + ":"
  624.                                    + timeClient.getStrMinutes() + ":"
  625.                                    + timeClient.getStrSeconds() + " "
  626.                                    + DAY[timeClient.getWeek()-1] + " "
  627.                                    + timeClient.getDay() + "/"
  628.                                    + timeClient.getMonth() + "/"
  629.                                    + timeClient.getYear());          
  630.        #endif                              
  631.     #endif        
  632. }
  633.  
  634. /***************************************************************************************************************/
  635. /*    
  636.     @definition     Task2Callback
  637.     @interval       1 seconds
  638.     @function       update time only
  639. */
  640. /***************************************************************************************************************/
  641.  
  642. void Task2Callback()
  643. {
  644.     timeClient.update();                                        // update time
  645.      
  646.     if (timeClient.updated())
  647.     {
  648.         F_TimeUpdate = true;
  649.     }
  650.     else
  651.     {
  652.         #if ENGLISH
  653.             Serial.printf_P( PSTR ("\n[ESP] [TASK2] [Time Update]     : Wait"));
  654.         #endif
  655.         #if POLISH
  656.             Serial.printf_P( PSTR ("\n[ESP] [TASK2] [Time Update]     : Czekaj"));
  657.         #endif
  658.         #if GERMANY
  659.             Serial.printf_P( PSTR ("\n[ESP] [TASK2] [Time Update]     : Warten"));
  660.         #endif
  661.         F_TimeUpdate = false;                            
  662.     }
  663.      
  664. }
  665.  
  666. /***************************************************************************************************************/
  667. /*    
  668.     @definition     Task3Callback
  669.     interval       10 minuts
  670.    
  671. */
  672. /***************************************************************************************************************/
  673.  
  674. void Task3Callback()
  675. {
  676.     #if SERIAL
  677.         #if ENGLISH
  678.             Serial.printf_P( PSTR ("\n\n[ESP] [TASK3] Hello from Task3Callback :-)") );
  679.         #endif
  680.         #if POLISH
  681.             Serial.printf_P( PSTR ("\n\n[ESP] [TASK3] Uruchomiono Task3Callback :-)") );
  682.         #endif
  683.         #if GERMANY
  684.             Serial.printf_P( PSTR ("\n\n[ESP] [TASK3] Sie sind in Task3Callback :-)") );
  685.         #endif
  686.     #endif
  687.  
  688.     if (timeClient.updated())
  689.     {
  690.  
  691.         /*          
  692.         *  tworzy sformatowaną datę (format String) w postaci dd/mm/yyyy
  693.         */
  694.  
  695.         if( timeClient.getDay() < 10 ) MyClock.formated_date = "0" + String(timeClient.getDay()) + "/";
  696.         else                           MyClock.formated_date = String(timeClient.getDay()) + "/";
  697.  
  698.         if( timeClient.getMonth() < 10 )  MyClock.formated_date += "0" + String(timeClient.getMonth()) + "/";
  699.         else                              MyClock.formated_date += String(timeClient.getMonth()) + "/";
  700.            
  701.         MyClock.formated_date += String(timeClient.getYear());          
  702.  
  703.         #if SERIAL
  704.             #if ENGLISH
  705.                 Serial.printf_P( PSTR ("\n[ESP] [TASK4] Create Formated Date : %s "), MyClock.formated_date.c_str());
  706.             #endif
  707.             #if POLISH
  708.                 Serial.printf_P( PSTR ("\n[ESP] [TASK4] Utworzono Sformatowaną Datę :%s "), MyClock.formated_date.c_str());
  709.             #endif
  710.         #endif  
  711.          
  712.      }
  713. }
  714.  
  715. /***************************************************************************************************************/
  716. /*    
  717.     @definition    Task4Callback
  718.     @function      get & show temperature & humidity
  719. */
  720. /***************************************************************************************************************/
  721.  
  722. void Task4Callback()
  723. {
  724.    #if SERIAL
  725.         #if ENGLISH
  726.             Serial.printf_P( PSTR ("\n\n[ESP] [TASK4] Hello from Task4Callback :-)") );
  727.         #endif
  728.         #if POLISH
  729.             Serial.printf_P( PSTR ("\n\n[ESP] [TASK4] Uruchomiono Task4Callback :-)") );
  730.         #endif
  731.         #if GERMANY
  732.             Serial.printf_P( PSTR ("\n\n[ESP] [TASK4] Sie sind in Task4Callback :-)") );
  733.         #endif
  734.     #endif
  735.  
  736.     int chk = DHT11.read();
  737.  
  738.     #if SERIAL
  739.         #if ENGLISH
  740.             Serial.printf_P( PSTR ("\nRead DHT11 sensor : ") );    
  741.         #endif
  742.         #if POLISH
  743.             Serial.printf_P( PSTR ("\nOdczyt z sensora DHT11 : ") );
  744.         #endif
  745.         #if GERMANY
  746.             Serial.printf_P( PSTR ("\nSensorwert DHT11 : ") );
  747.         #endif
  748.     #endif
  749.  
  750.     #if SERIAL
  751.         switch (chk)
  752.         {
  753.             case  0:
  754.                     Serial.printf_P( PSTR ("OK") );                    
  755.                     break;
  756.            
  757.             case -1:
  758.                 #if ENGLISH
  759.                     Serial.printf_P( PSTR ("Checksum error") ); break;
  760.                 #endif                
  761.                 #if POLISH
  762.                     Serial.printf_P( PSTR ("Błąd sumy kontrolnej") ); break;
  763.                 #endif
  764.                 #if GERMANY
  765.                     Serial.printf_P( PSTR ("Prüfsummenfehler") ); break;
  766.                 #endif
  767.            
  768.             case -2:
  769.                 #if ENGLISH
  770.                     Serial.printf_P( PSTR ("Time out error") ); break;
  771.                 #endif                
  772.                 #if POLISH
  773.                     Serial.printf_P( PSTR ("Błąd przekroczenia limitu czasu") ); break;
  774.                 #endif
  775.                 #if GERMANY
  776.                     Serial.printf_P( PSTR ("Zeitüberschreitungsfehler") ); break;
  777.                 #endif            
  778.            
  779.             default:
  780.                 #if ENGLISH
  781.                     Serial.printf_P( PSTR ("Unknown error") ); break;
  782.                 #endif                
  783.                 #if POLISH
  784.                     Serial.printf_P( PSTR ("Nieznany błąd") ); break;
  785.                 #endif
  786.                 #if GERMANY
  787.                     Serial.printf_P( PSTR ("Unbekannter Fehler") ); break;
  788.                 #endif      
  789.         }   // end switch
  790.     #endif
  791.  
  792.     /*
  793.      *  copy DHT11 temperature, humidity, devPoint to data in structure mydht
  794.      *  kopiowanie DHT 11 temperatura, wilgotność, punkt zerowy do danych w strukturze mydht
  795.      */
  796.      
  797.     mydht.inside_temperature = (float)DHT11.temperature;
  798.     mydht.inside_humidity = DHT11.humidity;
  799.     mydht.inside_devpoint = (float)DHT11.dewPoint();    
  800.  
  801.     /*
  802.      *  jeśli włączony SERIAL wydrukuj odczyty na terminalu SERIAL
  803.      *  if SERIAL is TRUE print sensor-read on terminal SERIAL
  804.      */
  805.  
  806.     #if SERIAL
  807.         #if ENGLISH
  808.             Serial.printf_P( PSTR ("\nINSIDE Temperature : %0.1f °C"), mydht.inside_temperature );
  809.             Serial.printf_P( PSTR ("\nINSIDE Humidity    : %d %%"), mydht.inside_humidity );
  810.             Serial.printf_P( PSTR ("\nINSIDE DevPoint    : %0.1f °C"), mydht.inside_devpoint );
  811.         #endif
  812.         #if POLISH
  813.             Serial.printf_P( PSTR ("\nWEWNĄTRZ Temperatura : %0.1f °C"), mydht.inside_temperature );
  814.             Serial.printf_P( PSTR ("\nWEWNĄTRZ Wilgotność  : %d %%"), mydht.inside_humidity );
  815.             Serial.printf_P( PSTR ("\nWEWNĄTRZ Punkt ZERO  : %0.1f °C"), mydht.inside_devpoint );
  816.         #endif
  817.         #if GERMANY
  818.             Serial.printf_P( PSTR ("\nInnen temperatur                   : %0.1f °C"), mydht.inside_temperature );
  819.             Serial.printf_P( PSTR ("\ninnere Feuchtigkeit                : %d %%"), mydht.inside_humidity );
  820.             Serial.printf_P( PSTR ("\ninnerhalb des Entwicklungspunktes  : %0.1f °C"), mydht.inside_devpoint );
  821.         #endif      
  822.     #endif
  823.  
  824.     char buffer[20];
  825.     String buff;
  826.  
  827.     /*
  828.      *  konwersja float (temperature) do char[20] buffer
  829.      *  rzutowanie typów char[20] buffer do String -> mydht.buf_temperature
  830.      */
  831.      
  832.     snprintf(mydht.temp_char_buffer, sizeof mydht.temp_char_buffer, "%0.1fC", mydht.inside_temperature);
  833.     mydht.buf_temperature = "Temp:" + String(mydht.temp_char_buffer);
  834.  
  835.     #if DEBUG
  836.         Serial.printf_P( PSTR ("\n[DEBUG] Zawartość zmiennej ---> mydht.temp_char_buffer : %s"), mydht.temp_char_buffer);
  837.         Serial.printf_P( PSTR ("\n[DEBUG] Zawartość zmiennej ---> mydht.buf_temperature  : %s"), mydht.buf_temperature);
  838.     #endif
  839.  
  840.     /*
  841.      *  konwersja float (humidity) do char[20] buffer
  842.      *  rzutowanie typów char[20] buffer do String -> mydht.buf_temperature
  843.      */
  844.      
  845.     snprintf(mydht.temp_char_buffer, sizeof mydht.temp_char_buffer, "%d%%", mydht.inside_humidity);
  846.     mydht.buf_humidity = "Humi:" + String(mydht.temp_char_buffer);
  847.  
  848.     #if DEBUG
  849.         Serial.printf_P( PSTR ("\n[DEBUG] Zawartość zmiennej ---> mydht.temp_char_buffer : %s"), mydht.temp_char_buffer);
  850.         Serial.printf_P( PSTR ("\n[DEBUG] Zawartość zmiennej ---> mydht.buf_humidity     : %s"), mydht.buf_humidity);
  851.     #endif
  852.  
  853.     /*
  854.      *  konwersja float (humidity) do char[20] buffer
  855.      *  rzutowanie typów char[20] buffer do String -> mydht.buf_temperature
  856.      */
  857.      
  858.     snprintf(mydht.temp_char_buffer, sizeof mydht.temp_char_buffer, "%0.1fC", mydht.inside_devpoint);
  859.     mydht.buf_devpoint = "DevP:" + String(mydht.temp_char_buffer);
  860.  
  861.     #if DEBUG
  862.         Serial.printf_P( PSTR ("\n[DEBUG] Zawartość zmiennej ---> mydht.temp_char_buffer : %s"), mydht.temp_char_buffer);
  863.         Serial.printf_P( PSTR ("\n[DEBUG] Zawartość zmiennej ---> buf_devpoint           : %s"), mydht.buf_devpoint);
  864.     #endif
  865. }
  866.  
  867. /***************************************************************************************************************/
  868. /*    
  869.     @definition    statWiFi()
  870. */
  871. /***************************************************************************************************************/
  872.  
  873. void statWiFi()
  874. {
  875.     switch (WiFi.status())
  876.     {
  877.         case WL_NO_SSID_AVAIL:
  878.                               #if SERIAL
  879.                                   #if ENGLISH
  880.                                       Serial.printf_P ( PSTR ("\nConfigured SSID cannot be reached"));
  881.                                   #endif
  882.                                   #if POLISH
  883.                                       Serial.printf_P ( PSTR ("\nNie można osiągnąć skonfigurowanego identyfikatora SSID"));
  884.                                   #endif
  885.                                   #if GERMANY
  886.                                       Serial.printf_P ( PSTR ("\nKonfigurierte SSID ist nicht erreichbar"));
  887.                                   #endif
  888.                               #endif        
  889.                               break;
  890.                              
  891.         case WL_CONNECTED:
  892.                               #if SERIAL
  893.                                   #if ENGLISH
  894.                                       Serial.printf_P ( PSTR ("\nConnection successfully established"));
  895.                                       Serial.printf_P ( PSTR ("\nYour IP : %s"), WiFi.localIP().toString().c_str());
  896.                                       Serial.printf_P ( PSTR ("\nReceived Signal Strength Indication : %d"), WiFi.RSSI());
  897.                                   #endif
  898.                                   #if POLISH
  899.                                       Serial.printf_P ( PSTR ("\nPomyślnie nawiązano połączenie"));
  900.                                       Serial.printf_P ( PSTR ("\nTwój adres IP : %s"), WiFi.localIP().toString().c_str());
  901.                                       Serial.printf_P ( PSTR ("\nWskazanie siły odbieranego sygnału : %d"), WiFi.RSSI());
  902.                                   #endif
  903.                                   #if GERMANY
  904.                                       Serial.printf_P( PSTR ("\nVerbindung erfolgreich hergestellt"));
  905.                                       Serial.printf_P( PSTR ("\nDeine IP-Adresse : %s"), WiFi.localIP().toString().c_str());
  906.                                       Serial.printf_P( PSTR ("\nAnzeige der empfangenen Signalstärke : %d"), WiFi.RSSI());                                
  907.                                   #endif
  908.                               #endif
  909.                               break;
  910.                              
  911.         case WL_CONNECT_FAILED:
  912.                               #if SERIAL
  913.                                   #if ENGLISH
  914.                                       Serial.printf_P( PSTR ("\nConnection failed"));
  915.                                   #endif
  916.                                   #if POLISH
  917.                                       Serial.printf_P( PSTR ("\nPołączenie nieudane"));
  918.                                   #endif
  919.                                   #if GERMANY
  920.                                       Serial.printf_P( PSTR ("\nVerbindung fehlgeschlagen"));
  921.                                   #endif
  922.                               #endif
  923.                               break;
  924.       }      
  925. }
  926.  
  927. /***************************************************************************************************************/
  928. /*    
  929.     @definition    initWiFi()
  930. */
  931. /***************************************************************************************************************/
  932.  
  933. void initWiFi()
  934. {
  935.     WiFi.mode(WIFI_STA);
  936.     WiFi.begin(ssid, password);
  937.  
  938.     #if SERIAL    
  939.         #if ENGLISH
  940.             Serial.printf_P( PSTR ("\nConnecting to WiFi .."));
  941.         #endif
  942.         #if POLISH
  943.             Serial.printf_P( PSTR ( "\nŁącze z siecią WiFi .."));
  944.         #endif
  945.         #if GERMANY
  946.             Serial.printf_P( PSTR ("\nNetzwerkverbindung mit WLAN .."));
  947.         #endif
  948.     #endif
  949.      
  950.     while (WiFi.status() != WL_CONNECTED)
  951.     {
  952.         Serial.printf_P( PSTR( "." ));
  953.         delay(200);
  954.     }
  955.  
  956.     statWiFi();
  957.          
  958.     WiFi.setAutoReconnect(true);
  959.     WiFi.persistent(true);
  960. }
  961.  
  962. /***************************************************************************************************************/
  963. /*    
  964.     @definition    reconectWifi()
  965. */
  966. /***************************************************************************************************************/
  967.  
  968. void reconectWifi()
  969. {
  970.     unsigned long currentMillis = millis();
  971.      
  972.     if ((WiFi.status() != WL_CONNECTED) && (currentMillis - previousMillis >=interval))
  973.     {
  974.         #if SERIAL
  975.             #if ENGLISH
  976.                 Serial.printf_P( PSTR ( "\nReconnecting to WiFi..."));
  977.             #endif
  978.             #if POLISH
  979.                 Serial.printf_P( PSTR ( "\nPonowne łączenie z Wi-Fi..."));
  980.             #endif
  981.             #if GERMANY
  982.                 Serial.printf_P( PSTR ( "\nErneut mit WLAN verbinden..."));
  983.             #endif
  984.         #endif
  985.          
  986.         WiFi.disconnect();
  987.         WiFi.begin(ssid, password);
  988.          
  989.         statWiFi();
  990.          
  991.         previousMillis = currentMillis;
  992.     }
  993. }
  994.  
  995. /***************************************************************************************************************/
  996. /*    
  997.     @definition    SerialTypeWriter
  998. */
  999. /***************************************************************************************************************/
  1000.  
  1001. void SerialTypeWriter(const String & message, unsigned int wait)
  1002. {
  1003.     for(const char c : message)
  1004.     {
  1005.         Serial.print(c);    
  1006.         delay(wait);  
  1007.     }
  1008. }
  1009.  
  1010. /***************************************************************************************************************/
  1011. /*    
  1012.     @definition    MorphingClock
  1013. */
  1014. /***************************************************************************************************************/
  1015.  
  1016. void MorphingClock()
  1017. {
  1018.     unsigned long epoch = timeClient.getEpochTime();  
  1019.  
  1020.     static int color_int = 1;
  1021.  
  1022.     if (epoch != prevEpoch)
  1023.     {
  1024.         int hh = timeClient.getHours();
  1025.         int mm = timeClient.getMinutes();
  1026.         int ss = timeClient.getSeconds();      
  1027.    
  1028.         if (prevEpoch == 0)
  1029.         {
  1030.             digit0.Draw(ss % 10);
  1031.             digit1.Draw(ss / 10);
  1032.             digit2.Draw(mm % 10);
  1033.             digit3.Draw(mm / 10);
  1034.             digit4.Draw(hh % 10);
  1035.             digit5.Draw(hh / 10);
  1036.            
  1037.             display.setTextColor(myCYAN);
  1038.             display.setCursor(2,0);
  1039.             display.print(MyClock.formated_date.c_str());
  1040.             display.setCursor(2,25);
  1041.             display.print(mydht.buf_temperature.c_str());
  1042.          }
  1043.          else
  1044.          {      
  1045.             if (ss!=prevss)
  1046.             {
  1047.                 int s0 = ss % 10;
  1048.                 int s1 = ss / 10;
  1049.                 if (s0!=digit0.Value()) digit0.Morph(s0);
  1050.                 if (s1!=digit1.Value()) digit1.Morph(s1);        
  1051.                 prevss = ss;
  1052.  
  1053.                 uint16_t myB;
  1054.                
  1055.                 if    (color_int)
  1056.                 {
  1057.                     color_int=0;
  1058.                     myB = myBlue0;
  1059.                 }
  1060.                 else              
  1061.                 {
  1062.                     color_int=1;
  1063.                     myB = myBLACK;
  1064.                 }
  1065.  
  1066.                 display.drawPixel(20,13,myB);
  1067.                 display.drawPixel(21,13,myB);
  1068.                 display.drawPixel(20,14,myB);
  1069.                 display.drawPixel(21,14,myB);
  1070.                 display.drawPixel(20,18,myB);
  1071.                 display.drawPixel(21,18,myB);
  1072.                 display.drawPixel(20,19,myB);
  1073.                 display.drawPixel(21,19,myB);
  1074.  
  1075.                 display.drawPixel(41,13,myB);
  1076.                 display.drawPixel(42,13,myB);
  1077.                 display.drawPixel(41,14,myB);
  1078.                 display.drawPixel(42,14,myB);
  1079.                 display.drawPixel(41,18,myB);
  1080.                 display.drawPixel(42,18,myB);
  1081.                 display.drawPixel(41,19,myB);
  1082.                 display.drawPixel(42,19,myB);
  1083.             }
  1084.             if (mm!=prevmm)
  1085.             {
  1086.                 int m0 = mm % 10;
  1087.                 int m1 = mm / 10;
  1088.                     if (m0!=digit2.Value()) digit2.Morph(m0);
  1089.                     if (m1!=digit3.Value()) digit3.Morph(m1);
  1090.                     prevmm = mm;
  1091.                 }
  1092.      
  1093.                 if (hh!=prevhh)
  1094.                 {
  1095.                     int h0 = hh % 10;
  1096.                     int h1 = hh / 10;
  1097.                     if (h0!=digit4.Value()) digit4.Morph(h0);
  1098.                     if (h1!=digit5.Value()) digit5.Morph(h1);
  1099.                     prevhh = hh;
  1100.                 }
  1101.             }
  1102.             prevEpoch = epoch;
  1103.         }  
  1104. }
  1105.  
  1106. /***************************************************************************************************************/
  1107. /*    
  1108.     @definition    ShowInfo
  1109. */
  1110. /***************************************************************************************************************/
  1111.  
  1112. void ShowInfo()
  1113. {
  1114.     Serial.printf_P( PSTR ("\n+-------------------------------------------------------------+"));
  1115.     Serial.printf_P( PSTR ("\n|        Morphing Clock v1.1 beta by Tomasz Majcher           |" ));    
  1116.     Serial.printf_P( PSTR ("\n|        support : ESP8266 + PxMatrix + DHT11                 |" ));      
  1117.     Serial.printf_P( PSTR ("\n|                                                             |" ));      
  1118.     Serial.printf_P( PSTR ("\n|        e-mail: [email protected]                  |" ));      
  1119.     Serial.printf_P( PSTR ("\n+-------------------------------------------------------------+\n"));
  1120. }
  1121.  
  1122. /***************************************************************************************************************/
  1123. /*    
  1124.     @definition    setup
  1125. */
  1126. /***************************************************************************************************************/
  1127.  
  1128. void setup()
  1129. {
  1130.     delay(300);
  1131.     #if SERIAL      
  1132.         Serial.begin(SerialSpeed);
  1133.         ShowInfo();
  1134.     #endif  
  1135.  
  1136.     DHT11.attach(DHT_PIN);                                // DHT11 sensor start
  1137.    
  1138.     initWiFi();                                           // start Wi-Fi  
  1139.  
  1140.     timeClient.begin();    
  1141.     runner.init();                                        // inicjalizuj scheluder                                                                                    
  1142.     runner.addTask(task_0);                               // dodaj zadanie do scheluder
  1143.     task_0.enable();                                      // włącz zadanie do scheluder
  1144.     runner.addTask(task_1);                               // dodaj zadanie do scheluder
  1145.     task_1.enable();                                      // włącz zadanie do scheluder
  1146.     runner.addTask(task_2);                               // dodaj zadanie do scheluder
  1147.     task_2.enable();                                      //  włącz zadanie do scheluder
  1148.     runner.addTask(task_3);                               // dodaj zadanie do scheluder
  1149.     task_3.enable();                                      // włącz zadanie do scheluder
  1150.     runner.addTask(task_4);                               // dodaj zadanie do scheluder
  1151.     task_4.enable();                                      // włącz zadanie do scheluder
  1152.      
  1153.      // --- WYWOŁAJ TERAZ --------------------- //
  1154.    
  1155.     Task1Callback();  
  1156.     Task2Callback();  
  1157.     Task3Callback();    
  1158.     Task4Callback();    
  1159.     Task4Callback();    
  1160.     Task4Callback();    
  1161.  
  1162.     display.begin(16);
  1163.     display_update_enable(true);
  1164.    
  1165.     display.fillScreen(display.color565(0, 0, 0));
  1166. }
  1167.  
  1168. /***************************************************************************************************************/
  1169. /*    
  1170.     @main    loop program
  1171. */
  1172. /***************************************************************************************************************/
  1173.  
  1174.  
  1175. void loop()
  1176. {
  1177.     runner.execute();     // scheluder init
  1178.     reconectWifi();       // check Wi-Fi status
  1179.     MorphingClock();
  1180.  
  1181. }
  1182.  
  1183. /***************************************************************************************************************/
  1184. /*    
  1185.     @definition    Digit Class
  1186. */
  1187. /***************************************************************************************************************/
  1188.  
  1189.  
  1190. Digit::Digit(PxMATRIX* d, byte value, uint16_t xo, uint16_t yo, uint16_t color)
  1191. {
  1192.     _display = d;
  1193.     _value = value;
  1194.     xOffset = xo;
  1195.     yOffset = yo;
  1196.     _color = color;
  1197. }
  1198.  
  1199. byte Digit::Value()
  1200. {
  1201.     return _value;
  1202. }
  1203.  
  1204. void Digit::drawPixel(uint16_t x, uint16_t y, uint16_t c)
  1205. {
  1206.     _display->drawPixel(xOffset + x, height - (y + yOffset), c);
  1207. }
  1208.  
  1209. void Digit::drawLine(uint16_t x, uint16_t y, uint16_t x2, uint16_t y2, uint16_t c)
  1210. {
  1211.     _display->drawLine(xOffset + x, height - (y + yOffset), xOffset + x2, height - (y2 + yOffset), c);
  1212. }
  1213.  
  1214. void Digit::drawFillRect(uint16_t x, uint16_t y, uint16_t w, uint16_t h, uint16_t c)
  1215. {
  1216.     _display->fillRect(xOffset + x, height - (y + yOffset), w,h, c);
  1217. }
  1218.  
  1219. void Digit::DrawColon(uint16_t c)
  1220. {  
  1221.     drawFillRect(-3, segHeight-1, 2,2, c);
  1222.     drawFillRect(-3, segHeight+1+3, 2,2, c);
  1223. }
  1224.  
  1225. void Digit::drawSeg(byte seg)
  1226. {
  1227.     switch (seg)
  1228.     {
  1229.         case sA: drawLine(1, segHeight * 2 + 2, segWidth, segHeight * 2 + 2, _color); break;
  1230.         case sB: drawLine(segWidth + 1, segHeight * 2 + 1, segWidth + 1, segHeight + 2, _color); break;
  1231.         case sC: drawLine(segWidth + 1, 1, segWidth + 1, segHeight, _color); break;
  1232.         case sD: drawLine(1, 0, segWidth, 0, _color); break;
  1233.         case sE: drawLine(0, 1, 0, segHeight, _color); break;
  1234.         case sF: drawLine(0, segHeight * 2 + 1, 0, segHeight + 2, _color); break;
  1235.         case sG: drawLine(1, segHeight + 1, segWidth, segHeight + 1, _color); break;
  1236.     }
  1237. }
  1238.  
  1239. void Digit::Draw(byte value)
  1240. {
  1241.     byte pattern = digitBits[value];
  1242.     if (bitRead(pattern, 7)) drawSeg(sA);
  1243.     if (bitRead(pattern, 6)) drawSeg(sB);
  1244.     if (bitRead(pattern, 5)) drawSeg(sC);
  1245.     if (bitRead(pattern, 4)) drawSeg(sD);
  1246.     if (bitRead(pattern, 3)) drawSeg(sE);
  1247.     if (bitRead(pattern, 2)) drawSeg(sF);
  1248.     if (bitRead(pattern, 1)) drawSeg(sG);
  1249.     _value = value;
  1250. }
  1251.  
  1252. void Digit::Morph2()
  1253. {
  1254.     for (int i = 0; i <= segWidth; i++)
  1255.     {
  1256.         if (i < segWidth)
  1257.         {
  1258.             drawPixel(segWidth - i, segHeight * 2 + 2, _color);
  1259.             drawPixel(segWidth - i, segHeight + 1, _color);
  1260.             drawPixel(segWidth - i, 0, _color);
  1261.         }
  1262.  
  1263.         drawLine(segWidth + 1 - i, 1, segWidth + 1 - i, segHeight, black);
  1264.         drawLine(segWidth - i, 1, segWidth - i, segHeight, _color);
  1265.         delay(animSpeed);
  1266.     }
  1267. }
  1268.  
  1269. void Digit::Morph3()
  1270. {
  1271.     for (int i = 0; i <= segWidth; i++)
  1272.     {
  1273.         drawLine(0 + i, 1, 0 + i, segHeight, black);
  1274.         drawLine(1 + i, 1, 1 + i, segHeight, _color);
  1275.         delay(animSpeed);
  1276.     }
  1277. }
  1278.  
  1279. void Digit::Morph4()
  1280. {
  1281.     for (int i = 0; i < segWidth; i++)
  1282.     {
  1283.         drawPixel(segWidth - i, segHeight * 2 + 2, black); // Erase A
  1284.         drawPixel(0, segHeight * 2 + 1 - i, _color); // Draw as F
  1285.         drawPixel(1 + i, 0, black); // Erase D
  1286.         delay(animSpeed);
  1287.     }
  1288. }
  1289.  
  1290. void Digit::Morph5()
  1291. {
  1292.     for (int i = 0; i < segWidth; i++)
  1293.     {
  1294.         drawPixel(segWidth + 1, segHeight + 2 + i, black); // Erase B
  1295.         drawPixel(segWidth - i, segHeight * 2 + 2, _color); // Draw as A
  1296.         drawPixel(segWidth - i, 0, _color); // Draw D
  1297.         delay(animSpeed);
  1298.     }
  1299. }
  1300.  
  1301. void Digit::Morph6()
  1302. {
  1303.     for (int i = 0; i <= segWidth; i++)
  1304.     {
  1305.         drawLine(segWidth - i, 1, segWidth - i, segHeight, _color);
  1306.         if (i > 0) drawLine(segWidth - i + 1, 1, segWidth - i + 1, segHeight, black);
  1307.         delay(animSpeed);
  1308.     }
  1309. }
  1310.  
  1311. void Digit::Morph7()
  1312. {
  1313.     for (int i = 0; i <= (segWidth + 1); i++)
  1314.     {
  1315.         drawLine(0 + i - 1, 1, 0 + i - 1, segHeight, black);
  1316.         drawLine(0 + i, 1, 0 + i, segHeight, _color);
  1317.         drawLine(0 + i - 1, segHeight * 2 + 1, 0 + i - 1, segHeight + 2, black);
  1318.         drawLine(0 + i, segHeight * 2 + 1, 0 + i, segHeight + 2, _color);
  1319.         drawPixel(1 + i, 0, black); // D
  1320.         drawPixel(1 + i, segHeight + 1, black); // G
  1321.         delay(animSpeed);
  1322.     }
  1323. }
  1324.  
  1325. void Digit::Morph8()
  1326. {
  1327.     for (int i = 0; i <= segWidth; i++)
  1328.     {
  1329.         drawLine(segWidth - i, segHeight * 2 + 1, segWidth - i, segHeight + 2, _color);
  1330.         if (i > 0) drawLine(segWidth - i + 1, segHeight * 2 + 1, segWidth - i + 1, segHeight + 2, black);
  1331.  
  1332.         drawLine(segWidth - i, 1, segWidth - i, segHeight, _color);
  1333.         if (i > 0) drawLine(segWidth - i + 1, 1, segWidth - i + 1, segHeight, black);
  1334.  
  1335.         if (i < segWidth)
  1336.         {
  1337.             drawPixel(segWidth - i, 0, _color); // D
  1338.             drawPixel(segWidth - i, segHeight + 1, _color); // G
  1339.         }
  1340.         delay(animSpeed);
  1341.     }
  1342. }
  1343.  
  1344. void Digit::Morph9()
  1345. {
  1346.     for (int i = 0; i <= (segWidth + 1); i++)
  1347.     {
  1348.         drawLine(0 + i - 1, 1, 0 + i - 1, segHeight, black);
  1349.         drawLine(0 + i, 1, 0 + i, segHeight, _color);
  1350.         delay(animSpeed);
  1351.     }
  1352. }
  1353.  
  1354. void Digit::Morph0()
  1355. {
  1356.     for (int i = 0; i <= segWidth; i++)
  1357.     {
  1358.         if (_value==1)
  1359.         {
  1360.             drawLine(segWidth - i, segHeight * 2+1 , segWidth - i, segHeight + 2, _color);
  1361.             if (i > 0) drawLine(segWidth - i + 1, segHeight * 2+1, segWidth - i + 1, segHeight + 2, black);
  1362.             drawLine(segWidth - i, 1, segWidth - i, segHeight, _color);
  1363.             if (i > 0) drawLine(segWidth - i + 1, 1, segWidth - i + 1, segHeight, black);
  1364.             if (i<segWidth) drawPixel(segWidth - i, segHeight * 2 + 2 , _color); // Draw A
  1365.             if (i<segWidth) drawPixel(segWidth - i, 0, _color); // Draw D
  1366.         }
  1367.    
  1368.         if (_value==2)
  1369.         {
  1370.             drawLine(segWidth - i, segHeight * 2+1 , segWidth - i, segHeight + 2, _color);
  1371.             if (i > 0) drawLine(segWidth - i + 1, segHeight * 2+1, segWidth - i + 1, segHeight + 2, black);
  1372.             drawPixel(1+i, segHeight + 1, black); // Erase G left to right
  1373.             if (i<segWidth) drawPixel(segWidth + 1, segHeight + 1- i, _color);// Draw C
  1374.         }
  1375.  
  1376.         if (_value==3)
  1377.         {
  1378.             drawLine(segWidth - i, segHeight * 2+1 , segWidth - i, segHeight + 2, _color);
  1379.             if (i > 0) drawLine(segWidth - i + 1, segHeight * 2+1, segWidth - i + 1, segHeight + 2, black);
  1380.             drawLine(segWidth - i, 1, segWidth - i, segHeight, _color);
  1381.             if (i > 0) drawLine(segWidth - i + 1, 1, segWidth - i + 1, segHeight, black);
  1382.             drawPixel(segWidth - i, segHeight + 1, black); // G
  1383.          }
  1384.    
  1385.         if (_value==5)
  1386.         {
  1387.             if (i<segWidth)
  1388.             {
  1389.                 if (i>0) drawLine(1 + i, segHeight * 2 + 1, 1 + i, segHeight + 2, black);
  1390.                 drawLine(2 + i, segHeight * 2 + 1, 2 + i, segHeight + 2, _color);
  1391.             }
  1392.         }
  1393.    
  1394.         if (_value==5 || _value==9)
  1395.         {
  1396.               if (i<segWidth) drawPixel(segWidth - i, segHeight + 1, black);
  1397.               if (i<segWidth) drawPixel(0, segHeight - i, _color);
  1398.         }
  1399.         delay(animSpeed);
  1400.     }
  1401. }
  1402.  
  1403. void Digit::Morph1()
  1404. {
  1405.     for (int i = 0; i <= (segWidth + 1); i++)
  1406.     {
  1407.         drawLine(0 + i - 1, 1, 0 + i - 1, segHeight, black);
  1408.         drawLine(0 + i, 1, 0 + i, segHeight, _color);
  1409.         drawLine(0 + i - 1, segHeight * 2 + 1, 0 + i - 1, segHeight + 2, black);
  1410.         drawLine(0 + i, segHeight * 2 + 1, 0 + i, segHeight + 2, _color);
  1411.         drawPixel(1 + i, segHeight * 2 + 2, black); // A
  1412.         drawPixel(1 + i, 0, black); // D
  1413.         drawPixel(1 + i, segHeight + 1, black); // G
  1414.         delay(animSpeed);
  1415.     }
  1416. }
  1417.  
  1418. void Digit::Morph(byte newValue)
  1419. {
  1420.     switch (newValue)
  1421.     {
  1422.         case 2: Morph2(); break;
  1423.         case 3: Morph3(); break;
  1424.         case 4: Morph4(); break;
  1425.         case 5: Morph5(); break;
  1426.         case 6: Morph6(); break;
  1427.         case 7: Morph7(); break;
  1428.         case 8: Morph8(); break;
  1429.         case 9: Morph9(); break;
  1430.         case 0: Morph0(); break;
  1431.         case 1: Morph1(); break;
  1432.     }
  1433.     _value = newValue;
  1434. }
Add Comment
Please, Sign In to add comment