Last active
September 20, 2024 08:12
-
-
Save Drunkar/e92989ce3563b74a81946a9e08b2ea03 to your computer and use it in GitHub Desktop.
4 cities world clock on m5 stack core2. CUF_24px.h is here: https://github.com/m5stack/M5Core2/blob/master/examples/Advanced/Display/Display_Unicode/CUF_24px.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <M5Core2.h> | |
#include <WiFi.h> | |
#include "time.h" | |
#include <Timezone.h> | |
#include "CUF_24px.h" | |
const char* ssid = ""; | |
const char* password = ""; | |
const char* ntpServer = "pool.ntp.org"; | |
// 各都市のタイムゾーン定義 | |
TimeChangeRule JST = {"JST", Last, Sun, Mar, 1, 540}; // 日本標準時 UTC+9 | |
TimeChangeRule BST = {"BST", Last, Sun, Mar, 1, 60}; // 英国夏時間 UTC+1 | |
TimeChangeRule GMT = {"GMT", Last, Sun, Oct, 2, 0}; // グリニッジ標準時 UTC+0 | |
TimeChangeRule GST = {"GST", Last, Sun, Mar, 1, 240}; // ドバイ標準時 UTC+4 | |
TimeChangeRule SGT = {"SGT", Last, Sun, Mar, 1, 480}; // シンガポール標準時 UTC+8 | |
Timezone tzTokyo(JST, JST); | |
Timezone tzLondon(BST, GMT); | |
Timezone tzDubai(GST, GST); | |
Timezone tzSingapore(SGT, SGT); | |
struct City { | |
const char* name; | |
Timezone* tz; // タイムゾーンオブジェクト | |
int x; | |
int y; | |
}; | |
City cities[] = { | |
{"Tokyo", &tzTokyo, 10, 30}, | |
{"Singapore", &tzSingapore, 10, 90}, | |
{"Dubai", &tzDubai, 10, 150}, | |
{"London", &tzLondon, 10, 210} | |
}; | |
void printLocalTime(const char* name, Timezone* tz, int x, int y, time_t utc) { | |
TimeChangeRule* tcr; // ポインタを受け取るための変数 | |
time_t local = tz->toLocal(utc, &tcr); | |
struct tm *timeinfo = localtime(&local); | |
char dateString[20]; | |
strftime(dateString, sizeof(dateString), "%Y/%m/%d", timeinfo); | |
// 日本語の曜日を取得 | |
const char* weekDays[7] = {"日", "月", "火", "水", "木", "金", "土"}; | |
const char* dayString = weekDays[timeinfo->tm_wday]; | |
char timeString[6]; | |
strftime(timeString, sizeof(timeString), "%H:%M", timeinfo); | |
M5.Lcd.setCursor(x, y); | |
M5.Lcd.printf("%s:", name); | |
M5.Lcd.setCursor(x + 150, y); | |
M5.Lcd.print(timeString); | |
// 曜日を時刻の右側に表示 | |
M5.Lcd.setCursor(x + 240, y-5); | |
M5.Lcd.print(dayString); | |
M5.Lcd.setCursor(x + 150, y + 20); | |
M5.Lcd.print(dateString); | |
} | |
void setup(){ | |
M5.begin(true, true, true, true); // M5Core2の場合、引数をすべてtrueにします | |
M5.Lcd.clear(BLACK); | |
M5.Lcd.setFreeFont(&unicode_24px); | |
M5.Lcd.setTextDatum(TC_DATUM); | |
M5.Lcd.setTextSize(0.3); // 文字サイズを調整 | |
M5.Lcd.setTextColor(WHITE); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
M5.Lcd.print("."); | |
} | |
configTime(0, 0, ntpServer); | |
} | |
void loop(){ | |
M5.Lcd.fillScreen(BLACK); | |
time_t now; | |
time(&now); // UTC時刻を取得 | |
for(int i = 0; i < 4; i++) { | |
int x = cities[i].x; | |
int y = cities[i].y; | |
printLocalTime(cities[i].name, cities[i].tz, x, y, now); | |
} | |
delay(10000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment