|
本帖最后由 botao258 于 2021-2-12 21:37 编辑
折腾M5Stack做NTP时钟显示时, 菜鸟初学,找了一份代码,可不知道怎么能分别获取到各个值(时间,日期,星期),用于每个值在指定的地方显示,用M5.Lcd.drawNumber(long long_num, int32_t poX, int32_t poY); 和 M5.Lcd.drawChar(int16_t uniCode, int32_t x, uint16_t y, uint8_t font); 显示在屏幕上。 用M5.Lcd.println() 能整体能打印出来, 但怎么能拆开,求指教:
- #include <WiFi.h>
- #include "time.h"
- #include <M5Stack.h>
- const char* ssid = "新年快乐";
- const char* password = "牛年大吉恭喜发财";
- const char* ntpServer = "pool.ntp.org";
- const long gmtOffset_sec = 28800;
- const int daylightOffset_sec = 3600;
- void setup(){
- //Serial.begin(115200);
-
- M5.begin();
- M5.Lcd.fillScreen(TFT_BLACK);
- M5.Lcd.setTextSize(2);
- M5.Lcd.setTextColor(TFT_YELLOW, TFT_BLACK);
-
- M5.Lcd.print("Connecting to: ");
- M5.Lcd.print(ssid);
-
- // Connect to Wi-Fi
- WiFi.begin(ssid, password);
-
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- M5.Lcd.print(".");
- }
- M5.Lcd.print("");
- M5.Lcd.print("WiFi connected.\r\n");
-
- // Init and get the time
- configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
- printLocalTime();
- //disconnect WiFi as it's no longer needed
- WiFi.disconnect(true);
- WiFi.mode(WIFI_OFF);
- }
- void loop(){
- delay(1000);
- printLocalTime();
- }
- void printLocalTime(){
- struct tm timeinfo;
-
- if(!getLocalTime(&timeinfo)){
- M5.Lcd.print("Failed to obtain time");
- return;
- }
-
- M5.Lcd.setTextColor(TFT_PINK);
- M5.Lcd.println(&timeinfo, "%A %B %d %Y %H:%M:%S");
- }
复制代码
|
|