上一章中已经实现了在屏幕上显示字符,要在屏幕上显示时间只需要修改主函数即可,主函数修改如下:
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 }
附上显示效果图: