CPP   78
cod
Guest on 5th February 2023 01:22:41 PM


  1. #include <Adafruit_GFX.h>
  2. #include "Font4x7Fixed.h"
  3. #include <Adafruit_NeoMatrix.h>
  4. #include <Adafruit_NeoPixel.h>
  5. #include <ESP8266WiFi.h>
  6. #include <time.h>
  7.  
  8. #define PIN  3
  9. #define BRIGHTNESS 10
  10.  
  11. const char *ssid = "***";
  12. const char *password = "****";
  13. const char* ntpServer = "ro.pool.ntp.org";
  14. const long  gmtOffset_sec = 1 * 60 * 60;
  15. const int   daylightOffset_sec = 3600;
  16.  
  17. Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(32, 8, PIN,
  18.                             NEO_MATRIX_TOP     + NEO_MATRIX_LEFT +
  19.                             NEO_MATRIX_COLUMNS + NEO_MATRIX_ZIGZAG,
  20.                             NEO_GRB            + NEO_KHZ800);
  21.  
  22. const uint16_t colors[] = {
  23.   matrix.Color(128, 0, 0), matrix.Color(0, 128, 0), matrix.Color(0, 0, 128),
  24.   matrix.Color(128, 0, 128), matrix.Color(0, 128, 128), matrix.Color(128, 128, 0)
  25. };
  26.  
  27. int pass = 0;
  28. int x = matrix.width();
  29.  
  30. String dash = String(':');
  31. String minus = String('-');
  32. String space = String(' ');
  33.  
  34.  
  35. void setup() {
  36.   WiFi.begin(ssid, password);
  37.   while (WiFi.status() != WL_CONNECTED) {
  38.     delay(500);
  39.   }
  40.   configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
  41.   printLocalTime();
  42.   matrix.begin();
  43.   matrix.setFont(&Font4x7Fixed);
  44.   matrix.setTextSize(1);
  45.   matrix.setTextWrap(false);
  46.   matrix.setBrightness(BRIGHTNESS);
  47.   matrix.setTextColor(colors[0]);
  48. }
  49.  
  50. void loop() {
  51.   printLocalTime();
  52. }
  53.  
  54. void printLocalTime() {
  55.   struct tm timeinfo;
  56.   if (!getLocalTime(&timeinfo)) {
  57.     return;
  58.   }
  59.  
  60.   char timeHour[3];
  61.   strftime(timeHour, 3, "%H", &timeinfo);
  62.  
  63.   char timeMinute[3];
  64.   strftime(timeMinute, 3, "%M", &timeinfo);
  65.  
  66.   char timeSeconds[3];
  67.   strftime(timeSeconds, 3, "%S", &timeinfo);
  68.  
  69.   char timeWeekDay[10];
  70.   strftime(timeWeekDay, 10, "%A", &timeinfo);
  71.  
  72.   char timeMonth[4];
  73.   strftime(timeMonth, 4, "%B", &timeinfo);
  74.  
  75.   char timeDay[3];
  76.   strftime(timeDay, 3, "%d", &timeinfo);
  77.  
  78.   char timeYear[5];
  79.   strftime(timeYear, 5, "%Y", &timeinfo);
  80.  
  81.   String timeShow = String(timeHour) + String(space) + String(dash) + String(space) + String(timeMinute) + String(space) + String(dash) + String(space) + String(timeSeconds);
  82.   String dateShow = String(timeWeekDay) + String(space) + String(space) + String(timeDay) + String(space) + String(minus) + String(space) + String(timeMonth) + String(space) + String(minus) + String(space) + String(timeYear);
  83.  
  84.  
  85.   unsigned long previousMillis = 0;
  86.   const long interval = 30000;
  87.   unsigned long currentMillis = millis();
  88.   if (currentMillis - previousMillis >= interval) {
  89.     previousMillis = currentMillis;
  90.     matrix.fillScreen(0);
  91.     matrix.setCursor(x, 0);
  92.     matrix.print(dateShow);
  93.     if (--x < -40) {
  94.       matrix.setTextColor(matrix.Color(128, 0, 0));
  95.     }
  96.     matrix.show();
  97.     delay(150);
  98.   } else {
  99.     matrix.clear();
  100.     matrix.setCursor(0, 0);
  101.     matrix.print(timeShow);
  102.     pass++;
  103.     if (pass >= 6) pass = 0;
  104.     matrix.setTextColor(colors[pass]);
  105.     matrix.show();
  106.     delay(1000);
  107.   }
  108. }

Raw Paste

Login or Register to edit or fork this paste. It's free.