Add time zone setting

This commit is contained in:
icyqwq 2020-11-20 09:56:38 +08:00
parent ef8d1ff944
commit 2c8e32c1f2
4 changed files with 116 additions and 1 deletions

View File

@ -5,12 +5,14 @@
#define KEY_W 92 #define KEY_W 92
#define KEY_H 92 #define KEY_H 92
const uint16_t kTimeZoneY = 520;
void key_shutdown_cb(epdgui_args_vector_t &args) void key_shutdown_cb(epdgui_args_vector_t &args)
{ {
M5.EPD.WriteFullGram4bpp(GetWallpaper()); M5.EPD.WriteFullGram4bpp(GetWallpaper());
M5.EPD.UpdateFull(UPDATE_MODE_GC16); M5.EPD.UpdateFull(UPDATE_MODE_GC16);
M5.EPD.UpdateFull(UPDATE_MODE_GC16); M5.EPD.UpdateFull(UPDATE_MODE_GC16);
SaveSetting();
delay(600); delay(600);
M5.disableEPDPower(); M5.disableEPDPower();
M5.disableEXTPower(); M5.disableEXTPower();
@ -21,6 +23,9 @@ void key_shutdown_cb(epdgui_args_vector_t &args)
void key_restart_cb(epdgui_args_vector_t &args) void key_restart_cb(epdgui_args_vector_t &args)
{ {
M5.EPD.WriteFullGram4bpp(GetWallpaper());
M5.EPD.UpdateFull(UPDATE_MODE_GC16);
SaveSetting();
esp_restart(); esp_restart();
} }
@ -50,8 +55,10 @@ void key_wallpaper_cb(epdgui_args_vector_t &args)
void key_synctime_cb(epdgui_args_vector_t &args) void key_synctime_cb(epdgui_args_vector_t &args)
{ {
SaveSetting();
M5EPD_Canvas info(&M5.EPD); M5EPD_Canvas info(&M5.EPD);
M5EPD_Canvas *title = (M5EPD_Canvas*)(args[0]); M5EPD_Canvas *title = (M5EPD_Canvas*)(args[0]);
M5EPD_Canvas *tzone = (M5EPD_Canvas*)(args[1]);
info.createCanvas(300, 100); info.createCanvas(300, 100);
info.fillCanvas(15); info.fillCanvas(15);
info.setTextSize(26); info.setTextSize(26);
@ -75,6 +82,7 @@ void key_synctime_cb(epdgui_args_vector_t &args)
info.pushCanvas(120, 430, UPDATE_MODE_GL16); info.pushCanvas(120, 430, UPDATE_MODE_GL16);
M5.EPD.WriteFullGram4bpp(GetWallpaper()); M5.EPD.WriteFullGram4bpp(GetWallpaper());
title->pushCanvas(0, 8, UPDATE_MODE_NONE); title->pushCanvas(0, 8, UPDATE_MODE_NONE);
tzone->pushCanvas(4, kTimeZoneY, UPDATE_MODE_NONE);
EPDGUI_Draw(UPDATE_MODE_NONE); EPDGUI_Draw(UPDATE_MODE_NONE);
while(!M5.TP.avaliable()); while(!M5.TP.avaliable());
M5.EPD.UpdateFull(UPDATE_MODE_GL16); M5.EPD.UpdateFull(UPDATE_MODE_GL16);
@ -118,21 +126,96 @@ void key_synctime_cb(epdgui_args_vector_t &args)
} }
M5.EPD.WriteFullGram4bpp(GetWallpaper()); M5.EPD.WriteFullGram4bpp(GetWallpaper());
title->pushCanvas(0, 8, UPDATE_MODE_NONE); title->pushCanvas(0, 8, UPDATE_MODE_NONE);
tzone->pushCanvas(4, kTimeZoneY, UPDATE_MODE_NONE);
EPDGUI_Draw(UPDATE_MODE_NONE); EPDGUI_Draw(UPDATE_MODE_NONE);
while(!M5.TP.avaliable()); while(!M5.TP.avaliable());
M5.EPD.UpdateFull(UPDATE_MODE_GL16); M5.EPD.UpdateFull(UPDATE_MODE_GL16);
} }
void key_timezone_plus_cb(epdgui_args_vector_t &args)
{
int *tz = (int*)(args[0]);
(*tz)++;
if((*tz) > 12)
{
(*tz) = 12;
}
String str = String(*tz);
if((*tz) > 0)
{
str = "+" + str;
}
((EPDGUI_Button*)(args[1]))->setLabel(str);
((EPDGUI_Button*)(args[1]))->Draw(UPDATE_MODE_GL16);
SetTimeZone(*tz);
}
void key_timezone_minus_cb(epdgui_args_vector_t &args)
{
int *tz = (int*)(args[0]);
(*tz)--;
if((*tz) < -11)
{
(*tz) = -11;
}
String str = String(*tz);
if((*tz) > 0)
{
str = "+" + str;
}
((EPDGUI_Button*)(args[1]))->setLabel(str);
((EPDGUI_Button*)(args[1]))->Draw(UPDATE_MODE_GL16);
SetTimeZone(*tz);
}
void key_timezone_reset_cb(epdgui_args_vector_t &args)
{
int *tz = (int*)(args[0]);
(*tz) = 0;
((EPDGUI_Button*)(args[1]))->setLabel(String(*tz));
((EPDGUI_Button*)(args[1]))->Draw(UPDATE_MODE_GL16);
SetTimeZone(*tz);
}
Frame_Setting::Frame_Setting(void) Frame_Setting::Frame_Setting(void)
{ {
_frame_name = "Frame_Setting"; _frame_name = "Frame_Setting";
_timezone_canvas = new M5EPD_Canvas(&M5.EPD);
_timezone_canvas->createCanvas(540, 60);
_timezone_canvas->fillCanvas(0);
_timezone_canvas->setTextSize(26);
_timezone_canvas->setTextColor(15);
_timezone_canvas->setTextDatum(CL_DATUM);
uint8_t language = GetLanguage(); uint8_t language = GetLanguage();
_key_wallpaper = new EPDGUI_Button(4, 100, 532, 61); _key_wallpaper = new EPDGUI_Button(4, 100, 532, 61);
_key_language = new EPDGUI_Button(4, 160, 532, 61); _key_language = new EPDGUI_Button(4, 160, 532, 61);
_key_syncntp = new EPDGUI_Button(4, 220, 532, 61); _key_syncntp = new EPDGUI_Button(4, 220, 532, 61);
_key_restart = new EPDGUI_Button(4, 340, 532, 61); _key_restart = new EPDGUI_Button(4, 340, 532, 61);
_key_shutdown = new EPDGUI_Button(4, 400, 532, 61); _key_shutdown = new EPDGUI_Button(4, 400, 532, 61);
key_timezone_plus = new EPDGUI_Button("+", 448, kTimeZoneY, 88, 52);
String str = String(GetTimeZone());
if(GetTimeZone() > 0)
{
str = "+" + str;
}
key_timezone_reset = new EPDGUI_Button(str, 360, kTimeZoneY, 88, 52);
key_timezone_minus = new EPDGUI_Button("-", 272, kTimeZoneY, 88, 52);
key_timezone_plus->AddArgs(EPDGUI_Button::EVENT_RELEASED, 0, &_timezone);
key_timezone_plus->AddArgs(EPDGUI_Button::EVENT_RELEASED, 1, key_timezone_reset);
key_timezone_plus->Bind(EPDGUI_Button::EVENT_RELEASED, key_timezone_plus_cb);
key_timezone_reset->AddArgs(EPDGUI_Button::EVENT_RELEASED, 0, &_timezone);
key_timezone_reset->AddArgs(EPDGUI_Button::EVENT_RELEASED, 1, key_timezone_reset);
key_timezone_reset->Bind(EPDGUI_Button::EVENT_RELEASED, key_timezone_reset_cb);
key_timezone_minus->AddArgs(EPDGUI_Button::EVENT_RELEASED, 0, &_timezone);
key_timezone_minus->AddArgs(EPDGUI_Button::EVENT_RELEASED, 1, key_timezone_reset);
key_timezone_minus->Bind(EPDGUI_Button::EVENT_RELEASED, key_timezone_minus_cb);
if(language == LANGUAGE_JA) if(language == LANGUAGE_JA)
{ {
_key_wallpaper->setBMPButton(" 壁紙", "\u25B6", ImageResource_item_icon_wallpaper_32x32); _key_wallpaper->setBMPButton(" 壁紙", "\u25B6", ImageResource_item_icon_wallpaper_32x32);
@ -140,6 +223,7 @@ Frame_Setting::Frame_Setting(void)
_key_syncntp->setBMPButton(" 時間シンクロ", "", ImageResource_item_icon_ntptime_32x32); _key_syncntp->setBMPButton(" 時間シンクロ", "", ImageResource_item_icon_ntptime_32x32);
_key_restart->setBMPButton(" 再起動", "", ImageResource_item_icon_restart_32x32); _key_restart->setBMPButton(" 再起動", "", ImageResource_item_icon_restart_32x32);
_key_shutdown->setBMPButton(" 電源オフ", "", ImageResource_item_icon_shutdown_32x32); _key_shutdown->setBMPButton(" 電源オフ", "", ImageResource_item_icon_shutdown_32x32);
_timezone_canvas->drawString("時間帯 (UTC)", 15, 35);
exitbtn("ホーム"); exitbtn("ホーム");
_canvas_title->drawString("設定", 270, 34); _canvas_title->drawString("設定", 270, 34);
} }
@ -150,6 +234,7 @@ Frame_Setting::Frame_Setting(void)
_key_syncntp->setBMPButton(" 同步时间", "", ImageResource_item_icon_ntptime_32x32); _key_syncntp->setBMPButton(" 同步时间", "", ImageResource_item_icon_ntptime_32x32);
_key_restart->setBMPButton(" 重启", "", ImageResource_item_icon_restart_32x32); _key_restart->setBMPButton(" 重启", "", ImageResource_item_icon_restart_32x32);
_key_shutdown->setBMPButton(" 关机", "", ImageResource_item_icon_shutdown_32x32); _key_shutdown->setBMPButton(" 关机", "", ImageResource_item_icon_shutdown_32x32);
_timezone_canvas->drawString("时区 (UTC)", 15, 35);
exitbtn("主页"); exitbtn("主页");
_canvas_title->drawString("设置", 270, 34); _canvas_title->drawString("设置", 270, 34);
} }
@ -160,6 +245,7 @@ Frame_Setting::Frame_Setting(void)
_key_syncntp->setBMPButton(" Sync Time", "", ImageResource_item_icon_ntptime_32x32); _key_syncntp->setBMPButton(" Sync Time", "", ImageResource_item_icon_ntptime_32x32);
_key_restart->setBMPButton(" Restart", "", ImageResource_item_icon_restart_32x32); _key_restart->setBMPButton(" Restart", "", ImageResource_item_icon_restart_32x32);
_key_shutdown->setBMPButton(" Shutdown", "", ImageResource_item_icon_shutdown_32x32); _key_shutdown->setBMPButton(" Shutdown", "", ImageResource_item_icon_shutdown_32x32);
_timezone_canvas->drawString("Time zone (UTC)", 15, 35);
exitbtn("Home"); exitbtn("Home");
_canvas_title->drawString("Setting", 270, 34); _canvas_title->drawString("Setting", 270, 34);
} }
@ -176,7 +262,10 @@ Frame_Setting::Frame_Setting(void)
_key_shutdown->Bind(EPDGUI_Button::EVENT_RELEASED, &key_shutdown_cb); _key_shutdown->Bind(EPDGUI_Button::EVENT_RELEASED, &key_shutdown_cb);
_key_restart->Bind(EPDGUI_Button::EVENT_RELEASED, &key_restart_cb); _key_restart->Bind(EPDGUI_Button::EVENT_RELEASED, &key_restart_cb);
_key_syncntp->AddArgs(EPDGUI_Button::EVENT_RELEASED, 0, _canvas_title); _key_syncntp->AddArgs(EPDGUI_Button::EVENT_RELEASED, 0, _canvas_title);
_key_syncntp->AddArgs(EPDGUI_Button::EVENT_RELEASED, 1, _timezone_canvas);
_key_syncntp->Bind(EPDGUI_Button::EVENT_RELEASED, &key_synctime_cb); _key_syncntp->Bind(EPDGUI_Button::EVENT_RELEASED, &key_synctime_cb);
_timezone = GetTimeZone();
} }
Frame_Setting::~Frame_Setting(void) Frame_Setting::~Frame_Setting(void)
@ -193,11 +282,15 @@ int Frame_Setting::init(epdgui_args_vector_t &args)
_is_run = 1; _is_run = 1;
M5.EPD.WriteFullGram4bpp(GetWallpaper()); M5.EPD.WriteFullGram4bpp(GetWallpaper());
_canvas_title->pushCanvas(0, 8, UPDATE_MODE_NONE); _canvas_title->pushCanvas(0, 8, UPDATE_MODE_NONE);
_timezone_canvas->pushCanvas(0, kTimeZoneY, UPDATE_MODE_NONE);
EPDGUI_AddObject(_key_wallpaper); EPDGUI_AddObject(_key_wallpaper);
EPDGUI_AddObject(_key_language); EPDGUI_AddObject(_key_language);
EPDGUI_AddObject(_key_shutdown); EPDGUI_AddObject(_key_shutdown);
EPDGUI_AddObject(_key_restart); EPDGUI_AddObject(_key_restart);
EPDGUI_AddObject(_key_syncntp); EPDGUI_AddObject(_key_syncntp);
EPDGUI_AddObject(_key_exit); EPDGUI_AddObject(_key_exit);
EPDGUI_AddObject(key_timezone_plus);
EPDGUI_AddObject(key_timezone_reset);
EPDGUI_AddObject(key_timezone_minus);
return 3; return 3;
} }

View File

@ -18,6 +18,12 @@ private:
EPDGUI_Button *_key_shutdown; EPDGUI_Button *_key_shutdown;
EPDGUI_Button *_key_restart; EPDGUI_Button *_key_restart;
EPDGUI_Button *_key_syncntp; EPDGUI_Button *_key_syncntp;
EPDGUI_Button *key_timezone_plus;
EPDGUI_Button *key_timezone_reset;
EPDGUI_Button *key_timezone_minus;
int _timezone;
M5EPD_Canvas *_timezone_canvas;
}; };
#endif //_FRAME_SETTING_H_ #endif //_FRAME_SETTING_H_

View File

@ -64,6 +64,18 @@ uint16_t global_reader_textsize = 32;
uint8_t global_time_synced = false; uint8_t global_time_synced = false;
uint8_t global_ttf_file_loaded = false; uint8_t global_ttf_file_loaded = false;
uint8_t global_init_status = 0xFF; uint8_t global_init_status = 0xFF;
int8_t global_timezone = 8;
int8_t GetTimeZone(void)
{
return global_timezone;
}
void SetTimeZone(int8_t time_zone)
{
global_timezone = time_zone;
}
void SetInitStatus(uint8_t idx, uint8_t val) void SetInitStatus(uint8_t idx, uint8_t val)
{ {
@ -147,6 +159,7 @@ esp_err_t LoadSetting(void)
NVS_CHECK(nvs_get_u16(nvs_arg, "Wallpaper", &global_wallpaper)); NVS_CHECK(nvs_get_u16(nvs_arg, "Wallpaper", &global_wallpaper));
NVS_CHECK(nvs_get_u8(nvs_arg, "Language", &global_language)); NVS_CHECK(nvs_get_u8(nvs_arg, "Language", &global_language));
NVS_CHECK(nvs_get_u8(nvs_arg, "Timesync", &global_time_synced)); NVS_CHECK(nvs_get_u8(nvs_arg, "Timesync", &global_time_synced));
nvs_get_i8(nvs_arg, "timezone", &global_timezone);
if(global_wallpaper >= WALLPAPER_NUM) if(global_wallpaper >= WALLPAPER_NUM)
{ {
@ -172,6 +185,7 @@ esp_err_t SaveSetting(void)
NVS_CHECK(nvs_set_u16(nvs_arg, "Wallpaper", global_wallpaper)); NVS_CHECK(nvs_set_u16(nvs_arg, "Wallpaper", global_wallpaper));
NVS_CHECK(nvs_set_u8(nvs_arg, "Language", global_language)); NVS_CHECK(nvs_set_u8(nvs_arg, "Language", global_language));
NVS_CHECK(nvs_set_u8(nvs_arg, "Timesync", global_time_synced)); NVS_CHECK(nvs_set_u8(nvs_arg, "Timesync", global_time_synced));
NVS_CHECK(nvs_set_i8(nvs_arg, "timezone", global_timezone));
NVS_CHECK(nvs_set_str(nvs_arg, "ssid", global_wifi_ssid.c_str())); NVS_CHECK(nvs_set_str(nvs_arg, "ssid", global_wifi_ssid.c_str()));
NVS_CHECK(nvs_set_str(nvs_arg, "pswd", global_wifi_password.c_str())); NVS_CHECK(nvs_set_str(nvs_arg, "pswd", global_wifi_password.c_str()));
NVS_CHECK(nvs_commit(nvs_arg)); NVS_CHECK(nvs_commit(nvs_arg));
@ -204,7 +218,7 @@ String GetWifiPassword(void)
bool SyncNTPTime(void) bool SyncNTPTime(void)
{ {
const char *ntpServer = "time.cloudflare.com"; const char *ntpServer = "time.cloudflare.com";
configTime(8 * 3600, 0, ntpServer); configTime(global_timezone * 3600, 0, ntpServer);
struct tm timeInfo; struct tm timeInfo;
if (getLocalTime(&timeInfo)) if (getLocalTime(&timeInfo))

View File

@ -28,6 +28,8 @@ String GetWifiSSID(void);
String GetWifiPassword(void); String GetWifiPassword(void);
uint8_t isWiFiConfiged(void); uint8_t isWiFiConfiged(void);
bool SyncNTPTime(void); bool SyncNTPTime(void);
int8_t GetTimeZone(void);
void SetTimeZone(int8_t time_zone);
uint16_t GetTextSize(); uint16_t GetTextSize();
void SetTextSize(uint16_t size); void SetTextSize(uint16_t size);