当前位置:   article > 正文

LVGL8制作简易时钟_lv_timer_t

lv_timer_t

通过这两天对LVGL8的部分控件和样式的学习,自己制作了一个简易时钟,可显示时间,日期,星期,用到的主要有样式,布局等对象,还是通过codeblock来模拟代码的运行,代码如下:

  1. typedef struct _lv_clock
  2. {
  3. lv_obj_t *time_label; // 时间标签
  4. lv_obj_t *date_label; // 日期标签
  5. lv_obj_t *weekday_label; // 星期标签
  6. }lv_clock_t;
  7. static void clock_date_task_callback(lv_timer_t *timer)
  8. {
  9. static const char *week_day[7] = { "Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday" };
  10. static time_t unix_time;
  11. static struct tm *time_info;
  12. unix_time = time(NULL);
  13. time_info = localtime(&unix_time);
  14. int year = time_info->tm_year+1900;
  15. int month = time_info->tm_mon + 1;
  16. int day = time_info->tm_mday;
  17. int weekday = time_info->tm_wday;
  18. int hour = time_info->tm_hour;
  19. int minutes = time_info->tm_min;
  20. int second = time_info->tm_sec;
  21. if (timer != NULL && timer->user_data != NULL)
  22. {
  23. lv_clock_t * clock = (lv_clock_t *)(timer->user_data);
  24. if (clock->time_label != NULL)
  25. {
  26. lv_label_set_text_fmt(clock->time_label, "%02d:%02d:%02d", hour, minutes, second);
  27. lv_obj_align_to(clock->time_label, lv_obj_get_parent(clock->time_label), LV_ALIGN_CENTER, 0, 0);
  28. }
  29. if (clock->date_label != NULL)
  30. {
  31. lv_label_set_text_fmt(clock->date_label, "%d-%02d-%02d", year, month, day);
  32. lv_obj_align_to(clock->date_label, lv_obj_get_parent(clock->date_label), LV_ALIGN_TOP_MID, 2, 0);
  33. }
  34. if (clock->weekday_label != NULL)
  35. {
  36. lv_label_set_text_fmt(clock->weekday_label, "%s", week_day[weekday]);
  37. lv_obj_align_to(clock->weekday_label, lv_obj_get_parent(clock->weekday_label), LV_ALIGN_BOTTOM_MID, -2, 0);
  38. }
  39. }
  40. }
  41. void lvgl_clock_start()
  42. {
  43. static lv_style_t date_time_clock_style; // 最外层对象的样式
  44. lv_style_reset(&date_time_clock_style); // 重置样式
  45. lv_style_init(&date_time_clock_style); // 初始化样式
  46. lv_style_set_radius(&date_time_clock_style, 5); // 设置样式圆角
  47. lv_style_set_bg_opa(&date_time_clock_style, LV_OPA_20); // 设置样式背景透明度
  48. lv_style_set_border_width(&date_time_clock_style, 0); // 设置样式边框宽度
  49. lv_style_set_bg_color(&date_time_clock_style, lv_color_white()); // 设置样式背景颜色,白色
  50. lv_style_set_pad_left(&date_time_clock_style, 1); // 设置样式左边padding填充宽度
  51. lv_style_set_pad_right(&date_time_clock_style, 1); // 设置样式右边padding填充宽度
  52. lv_style_set_pad_top(&date_time_clock_style, 0); // 设置样式顶部padding填充宽度
  53. lv_style_set_pad_bottom(&date_time_clock_style, 0); // 设置样式底部padding填充宽度
  54. static lv_style_t time_style; // 时间对象样式
  55. lv_style_reset(&time_style);
  56. lv_style_init(&time_style);
  57. lv_style_set_bg_opa(&time_style, LV_OPA_COVER);
  58. lv_style_set_border_width(&time_style, 0);
  59. lv_style_set_radius(&time_style, 5);
  60. lv_style_set_bg_color(&time_style, lv_palette_main(LV_PALETTE_BLUE));
  61. lv_style_set_pad_left(&time_style, 0);
  62. lv_style_set_pad_right(&time_style, 0);
  63. lv_style_set_pad_top(&time_style, 0);
  64. lv_style_set_pad_bottom(&time_style, 0);
  65. static lv_style_t date_style; // 日期对象样式
  66. lv_style_reset(&date_style);
  67. lv_style_init(&date_style);
  68. lv_style_set_bg_opa(&date_style, LV_OPA_COVER);
  69. lv_style_set_border_width(&date_style, 0);
  70. lv_style_set_radius(&date_style, 5);
  71. lv_style_set_bg_color(&date_style, lv_palette_main(LV_PALETTE_BLUE));
  72. lv_style_set_pad_left(&date_style, 0);
  73. lv_style_set_pad_right(&date_style, 0);
  74. /* Time font */
  75. static lv_style_t time_label_style; // 时间标签样式
  76. lv_style_reset(&time_label_style); // 重置样式
  77. lv_style_init(&time_label_style); // 初始化样式
  78. lv_style_set_text_color(&time_label_style , lv_color_white()); // 设置标签样式文本颜色
  79. lv_style_set_text_font(&time_label_style, &lv_font_montserrat_32); // 设置字体风格
  80. lv_style_set_text_opa(&time_label_style, LV_OPA_COVER); // 设置字体透明度
  81. lv_style_set_bg_opa(&time_label_style, LV_OPA_0); // 设置样式背景透明度
  82. /* Date font */
  83. static lv_style_t date_label_style; // 日期标签样式
  84. lv_style_reset(&date_label_style);
  85. lv_style_init(&date_label_style);
  86. lv_style_set_text_opa(&date_label_style, LV_OPA_COVER);
  87. lv_style_set_bg_opa(&date_label_style, LV_OPA_0);
  88. lv_style_set_text_color(&date_label_style , lv_color_white());
  89. lv_style_set_text_font(&date_label_style, &lv_font_montserrat_16);
  90. /* Week font */
  91. static lv_style_t week_lable_style; // 日期标签样式
  92. lv_style_reset(&week_lable_style);
  93. lv_style_init(&week_lable_style);
  94. lv_style_set_text_opa(&week_lable_style, LV_OPA_COVER);
  95. lv_style_set_bg_opa(&week_lable_style, LV_OPA_0);
  96. lv_style_set_text_color(&week_lable_style, lv_color_white());
  97. lv_style_set_text_font(&week_lable_style, &lv_font_montserrat_16);
  98. /* Time & Date */
  99. lv_obj_t *time_date_obj = lv_obj_create(lv_scr_act()); // 基于屏幕创建时间日期对象
  100. if (time_date_obj == NULL)
  101. {
  102. printf("[%s:%d] time_date_obj create failed\n", __FUNCTION__, __LINE__);
  103. return;
  104. }
  105. lv_obj_set_size(time_date_obj, 320, 100); // 设置对象大小
  106. lv_obj_center(time_date_obj); // 对象居屏幕中间显示
  107. lv_obj_add_style(time_date_obj, &date_time_clock_style, LV_STATE_DEFAULT); //给time_date_obj对象添加样式
  108. /*Time display*/
  109. lv_obj_t *time_obj = lv_obj_create(time_date_obj); // 基于time_date_obj对象创建时间对象
  110. if (time_obj == NULL)
  111. {
  112. printf("[%s:%d] time_obj create failed\n", __FUNCTION__, __LINE__);
  113. return;
  114. }
  115. lv_obj_set_size(time_obj, 158, 100); // 设置对象大小
  116. lv_obj_align_to(time_obj, time_date_obj, LV_ALIGN_LEFT_MID, 0, 0); // 设置time_obj对象基于time_date_obj对象左边中间对齐
  117. lv_obj_add_style(time_obj, &time_style, LV_STATE_DEFAULT); // 给time_obj对象添加样式
  118. static lv_clock_t lv_clock = { 0 };
  119. lv_clock.time_label = lv_label_create(time_obj); // 基于time_obj对象创建时间显示标签对象 lv_clock.time_label
  120. if (lv_clock.time_label == NULL)
  121. {
  122. printf("[%s:%d] time_label create failed\n", __FUNCTION__, __LINE__);
  123. return ;
  124. }
  125. lv_obj_add_style(lv_clock.time_label, &time_label_style, LV_STATE_DEFAULT); // 给对象 lv_clock.time_label添加样式
  126. /*Date display*/
  127. lv_obj_t *date_obj = lv_obj_create(time_date_obj); // 基于time_date_obj对象创建date_obj对象
  128. if (date_obj == NULL)
  129. {
  130. printf("[%s:%d] date_obj create failed\n", __FUNCTION__, __LINE__);
  131. return ;
  132. }
  133. lv_obj_set_size(date_obj, 158, 100); // 设置对象大小
  134. lv_obj_align_to(date_obj, time_date_obj, LV_ALIGN_RIGHT_MID, 0, 0); //设置date_obj对象基于time_date_obj对象右边中部对齐
  135. lv_obj_add_style(date_obj, &date_style, LV_STATE_DEFAULT); // 给date_obj对象添加样式
  136. lv_clock.date_label = lv_label_create(date_obj); // 基于date_obj对象创建lv_clock.date_label日期显示对象
  137. if (lv_clock.date_label == NULL)
  138. {
  139. printf("[%s:%d] date_label create failed\n", __FUNCTION__, __LINE__);
  140. return ;
  141. }
  142. lv_obj_add_style(lv_clock.date_label, &date_label_style, LV_STATE_DEFAULT); // 给lv_clock.date_label对象添加样式
  143. /*Week display*/
  144. lv_clock.weekday_label = lv_label_create(date_obj); // 基于date_obj对象创建星期显示lv_clock.weekday_label对象
  145. if (lv_clock.weekday_label == NULL)
  146. {
  147. printf("[%s:%d] weekday_label create failed\n", __FUNCTION__, __LINE__);
  148. return;
  149. }
  150. lv_obj_add_style(lv_clock.weekday_label, &week_lable_style, LV_STATE_DEFAULT); // 给对象lv_clock.weekday_label添加样式
  151. // 设置时间标签lv_clock.time_label对象基于父对象居中对齐
  152. lv_obj_align_to(lv_clock.time_label, lv_obj_get_parent(lv_clock.time_label), LV_ALIGN_CENTER, 0, 0);
  153. // 设置时间标签lv_clock.date_label对象基于父对象顶部中间对齐
  154. lv_obj_align_to(lv_clock.date_label, lv_obj_get_parent(lv_clock.date_label), LV_ALIGN_TOP_MID, 2, 0);
  155. // 设置时间标签lv_clock.weekday_label对象基于父对象底部中间对齐
  156. lv_obj_align_to(lv_clock.weekday_label, lv_obj_get_parent(lv_clock.weekday_label), LV_ALIGN_BOTTOM_MID, -2, 0);
  157. lv_timer_t* task_timer = lv_timer_create(clock_date_task_callback, 200, (void *)&lv_clock); // 创建定时任务,200ms刷新一次
  158. if (task_timer == NULL)
  159. {
  160. printf("[%s:%d] lv_timer_create failed\n", __FUNCTION__, __LINE__);
  161. }
  162. }

运行效果:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/很楠不爱3/article/detail/471951
推荐阅读
相关标签
  

闽ICP备14008679号