- 本文深度依赖 TTGO T-Watch-2020 编程系列(二) 开发环境的搭建Windows, 没看过的请先看https://blog.csdn.net/coolwaterld/article/details/109067854
- 不知道什么是TTGO T-Watch-2020的请参看TTGO T-Watch-2020 编程系列(一)基于ESP32可编程手表简介,https://blog.csdn.net/coolwaterld/article/details/109065963 决定是否需要看本文
- 本文设计修改代码,如题所示添加日起相关代码到SimpleWatch程序
原始程序效果
修改之后的效果
通过查找time,找到需要修改的位置 在gui.cpp
static lv_obj_t *timeLabel = nullptr;
static lv_obj_t *dateLabel = nullptr;//L55,新增,用于维护date显示内容
void setupGui()
{
//L384
//! Time
static lv_style_t timeStyle;
lv_style_copy(&timeStyle, &mainStyle);
lv_style_set_text_font(&timeStyle, LV_STATE_DEFAULT, &Ubuntu);
timeLabel = lv_label_create(mainBar, NULL);
lv_obj_add_style(timeLabel, LV_OBJ_PART_MAIN, &timeStyle);
//设置字体,Geometr(小字体),Ubuntu(大字体)
//在库文件中只有两种字体分别在
// \TTGO_TWatch_Library-master\src\font\Ubuntu.c
// \TTGO_TWatch_Library-master\src\font\Geometr.c
static lv_style_t dateStyle;
lv_style_copy(&dateStyle, &mainStyle);
lv_style_set_text_font(&dateStyle, LV_STATE_DEFAULT, &Geometr);
dateLabel = lv_label_create(mainBar, NULL);
lv_obj_add_style(dateLabel, LV_OBJ_PART_MAIN, &dateStyle);
......
static void updateTime()
{
time_t now;
struct tm info;
char buf[64];
time(&now);
localtime_r(&now, &info);
strftime(buf, sizeof(buf), "%H:%M", &info);
lv_label_set_text(timeLabel, buf);
lv_obj_align(timeLabel, NULL, LV_ALIGN_IN_TOP_MID, 0, 20);
//更新date的内容以及显示在屏幕上的位置
memset(buf,0,sizeof(buf));
strftime(buf, sizeof(buf), "%x", &info);
lv_label_set_text(dateLabel, buf);
lv_obj_align(dateLabel, NULL, LV_ALIGN_CENTER, 0, 0);
TTGOClass *ttgo = TTGOClass::getWatch();
ttgo->rtc->syncToRtc();
}
4. 重新编译SimpleWatch,并下载即可,如果有问题,可参考上一篇文章
5参考链接
5.1 Strftime
https://www.runoob.com/cprogramming/c-function-strftime.html
C 库函数 size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr) 根据 format 中定义的格式化规则,格式化结构 timeptr 表示的时间,并把它存储在 str 中。
- str -- 这是指向目标数组的指针,用来复制产生的 C 字符串。
- maxsize -- 这是被复制到 str 的最大字符数。
- format -- 这是 C 字符串,包含了普通字符和特殊格式说明符的任何组合。这些格式说明符由函数替换为表示 tm 中所指定时间的相对应值。格式说明符是:
%x | 日期表示法 | 08/19/12 |
5.2 lv_obj_align
https://docs.lvgl.io/v7/en/html/widgets/obj.html
v_obj_align(obj, obj_ref, LV_ALIGN_..., x_ofs, y_ofs)