CPP   70
code
Guest on 1st February 2023 05:39:20 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  4
  9. #define BRIGHTNESS 5
  10.  
  11. const char *ssid = "xxxx";
  12. const char *password = "xxxx";
  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[8] = {
  23.   matrix.Color(128, 0, 0), matrix.Color(0, 128, 0), matrix.Color(0, 0, 128),
  24.   matrix.Color(128, 0, 128), matrix.Color(128, 128, 0), matrix.Color(0, 128, 128)
  25. };
  26.  
  27. struct tm timeinfo;
  28. int pass = 0;
  29. int x = matrix.width();
  30.  
  31. String dash = String(':');
  32. String minus = String('-');
  33. String space = String(' ');
  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.setBrightness(BRIGHTNESS);
  46.   matrix.setTextColor(colors[0]);
  47. }
  48.  
  49. void loop() {
  50.   printLocalTime();
  51. }
  52.  
  53. void printLocalTime() {
  54.   struct tm timeinfo;
  55.   if (!getLocalTime(&timeinfo)) {
  56.     return;
  57.   }
  58.  
  59.   char timeHour[3];
  60.   strftime(timeHour, 3, "%H", &timeinfo);
  61.   char timeMinute[3];
  62.   strftime(timeMinute, 3, "%M", &timeinfo);
  63.   char timeSeconds[3];
  64.   strftime(timeSeconds, 3, "%S", &timeinfo);
  65.   char timeWeekDay[10];
  66.   strftime(timeWeekDay, 10, "%A", &timeinfo);
  67.   char timeMonth[3];
  68.   strftime(timeMonth, 3, "%B", &timeinfo);
  69.   char timeDay[3];
  70.   strftime(timeDay, 3, "%d", &timeinfo);
  71.   char timeYear[5];
  72.   strftime(timeYear, 5, "%Y", &timeinfo);
  73.  
  74.   for (uint8_t b = 0; b < 2; b++) {
  75.     matrix.print(b);
  76.     switch (b) {
  77.       case 0:
  78.         String timeShow = String(timeHour) + String(space) + String(dash) + String(space) + String(timeMinute) + String(space) + String(dash) + String(space) + String(timeSoconds);
  79.         matrix.clear();
  80.         matrix.fillScreen(0);
  81.         matrix.setTextWrap(false);
  82.         matrix.setCursor(0, 0);
  83.         matrix.print(timeShow);
  84.         pass++;
  85.         if (pass >= 6) pass = 0;
  86.         matrix.setTextColor(colors[pass]);
  87.         matrix.show();
  88.         delay(1000);
  89.         break;
  90.       case 1:
  91.         String dateShow = String(timeWeekDay) + String(space) + String(timeDay) + String(minus) + String(timeMonth) + String(minus) + String(timeYear);
  92.         static byte scrollCounter[1];
  93.         unsigned long previousMillis = 0;
  94.         const long interval = 300000;
  95.         unsigned long currentMillis = millis();
  96.         if (currentMillis - previousMillis >= interval) {
  97.           previousMillis = currentMillis;
  98.           matrix.clear();
  99.           matrix.fillScreen(0);
  100.           matrix.setTextWrap(false);
  101.           matrix.setCursor(x, 0);
  102.           matrix.print(dateShow);
  103.           if (--x < -36) {
  104.             if (scrollCounter[0] < 1) {
  105.               if (dateShow = true) {
  106.                 scrollCounter[0]++;
  107.               }
  108.               return;
  109.             }
  110.             x = matrix.width();
  111.             if (++pass >= 6) pass = 0;
  112.             matrix.setTextColor(colors[pass]);
  113.           }
  114.           matrix.show();
  115.           delay(100);
  116.           break;
  117.         }
  118.     }
  119.   }
  120. }

Raw Paste

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