十六、在屏幕上显示时间

上一章中已经实现了在屏幕上显示字符,要在屏幕上显示时间只需要修改主函数即可,主函数修改如下:

 1 #include <stdio.h>
 2 #include <time.h>
 3 #include "draw.h"
 4 
 5 int main(int argc, const char *argv[])
 6 {
 7     int i = 0;
 8     time_t total_sec;
 9     struct tm *p_tm = NULL;
10     int last_sec = 60;                /* 赋一个不可能达到的秒数值,确保第一秒能显示 */
11     char show_buf[32] = { 0 };
12     font_size_t font_size = FONT_SIZE_48;
13     int x, y;
14 
15     if (framebuffer_init()) {
16         return 0;
17     }
18 
19     full_screen(LCD_YELLOW);
20     set_pen_color(LCD_RED);
21     set_bk_color(LCD_WHITE);
22 
23     set_font_size(font_size);
24 
25     y = (272 - font_size) / 2;
26     while (1) {
27         time(&total_sec);
28         p_tm = localtime(&total_sec);
29         if (p_tm == NULL) {
30             continue;
31         }
32         
33         if (last_sec != p_tm->tm_sec) {
34             last_sec = p_tm->tm_sec;
35 
36             sprintf(show_buf,
37                     "%4d-%02d-%02d %02d:%02d:%02d",
38                     p_tm->tm_year + 1900,
39                     p_tm->tm_mon  + 1,
40                     p_tm->tm_mday,
41                     p_tm->tm_hour,
42                     p_tm->tm_min,
43                     p_tm->tm_sec);
44         
45             x = (480 - strlen(show_buf) * font_size / 2) / 2;
46             if (x < 0) {
47                 x = 0;
48             }
49 
50             draw_str(x, y, show_buf);
51         }
52 
53         usleep(200 * 1000);
54     }
55 
56     return 0;
57 }

附上显示效果图:

 

转载于:https://www.cnblogs.com/Suzkfly/p/10383771.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值