lvgl_demo_ui.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. /*
  2. * SPDX-FileCopyrightText: 2021-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: CC0-1.0
  5. */
  6. #include <math.h>
  7. #include "sdkconfig.h"
  8. #include "lvgl.h"
  9. #ifndef PI
  10. #define PI (3.14159f)
  11. #endif
  12. #if CONFIG_EXAMPLE_LCD_IMAGE_FROM_EMBEDDED_BINARY
  13. // LVGL image declare
  14. LV_IMG_DECLARE(esp_logo)
  15. LV_IMG_DECLARE(esp_text)
  16. #endif // CONFIG_EXAMPLE_LCD_IMAGE_FROM_EMBEDDED_BINARY
  17. typedef struct {
  18. lv_obj_t *scr;
  19. int count_val;
  20. } my_timer_context_t;
  21. static my_timer_context_t my_tim_ctx;
  22. static lv_obj_t *btn;
  23. static lv_obj_t *arc[3];
  24. static lv_obj_t *img_logo = NULL;
  25. static lv_obj_t *img_text = NULL;
  26. static lv_color_t arc_color[] = {
  27. LV_COLOR_MAKE(232, 87, 116),
  28. LV_COLOR_MAKE(126, 87, 162),
  29. LV_COLOR_MAKE(90, 202, 228),
  30. };
  31. static void anim_timer_cb(lv_timer_t *timer)
  32. {
  33. my_timer_context_t *timer_ctx = (my_timer_context_t *) timer->user_data;
  34. int count = timer_ctx->count_val;
  35. lv_obj_t *scr = timer_ctx->scr;
  36. // Play arc animation
  37. if (count < 90) {
  38. lv_coord_t arc_start = count > 0 ? (1 - cosf(count / 180.0f * PI)) * 270 : 0;
  39. lv_coord_t arc_len = (sinf(count / 180.0f * PI) + 1) * 135;
  40. for (size_t i = 0; i < sizeof(arc) / sizeof(arc[0]); i++) {
  41. lv_arc_set_bg_angles(arc[i], arc_start, arc_len);
  42. lv_arc_set_rotation(arc[i], (count + 120 * (i + 1)) % 360);
  43. }
  44. }
  45. // Delete arcs when animation finished
  46. if (count == 90) {
  47. for (size_t i = 0; i < sizeof(arc) / sizeof(arc[0]); i++) {
  48. lv_obj_del(arc[i]);
  49. }
  50. // Create new image and make it transparent
  51. img_text = lv_img_create(scr);
  52. #if CONFIG_EXAMPLE_LCD_IMAGE_FROM_FILE_SYSTEM
  53. lv_img_set_src(img_text, "S:/spiffs/esp_text.png");
  54. #elif CONFIG_EXAMPLE_LCD_IMAGE_FROM_EMBEDDED_BINARY
  55. lv_img_set_src(img_text, &esp_text);
  56. #endif
  57. lv_obj_set_style_img_opa(img_text, 0, 0);
  58. }
  59. // Move images when arc animation finished
  60. if ((count >= 100) && (count <= 180)) {
  61. lv_coord_t offset = (sinf((count - 140) * 2.25f / 90.0f) + 1) * 20.0f;
  62. lv_obj_align(img_logo, LV_ALIGN_CENTER, 0, -offset);
  63. lv_obj_align(img_text, LV_ALIGN_CENTER, 0, 2 * offset);
  64. lv_obj_set_style_img_opa(img_text, offset / 40.0f * 255, 0);
  65. }
  66. // Delete timer when all animation finished
  67. if ((count += 5) == 220) {
  68. lv_timer_del(timer);
  69. // Enable button
  70. lv_obj_clear_state(btn, LV_STATE_DISABLED);
  71. } else {
  72. timer_ctx->count_val = count;
  73. }
  74. }
  75. static void start_animation(lv_obj_t *scr)
  76. {
  77. // Align image
  78. lv_obj_center(img_logo);
  79. // Create arcs
  80. for (size_t i = 0; i < sizeof(arc) / sizeof(arc[0]); i++) {
  81. arc[i] = lv_arc_create(scr);
  82. // Set arc caption
  83. lv_obj_set_size(arc[i], 220 - 30 * i, 220 - 30 * i);
  84. lv_arc_set_bg_angles(arc[i], 120 * i, 10 + 120 * i);
  85. lv_arc_set_value(arc[i], 0);
  86. // Set arc style
  87. lv_obj_remove_style(arc[i], NULL, LV_PART_KNOB);
  88. lv_obj_set_style_arc_width(arc[i], 10, 0);
  89. lv_obj_set_style_arc_color(arc[i], arc_color[i], 0);
  90. // Make arc center
  91. lv_obj_center(arc[i]);
  92. }
  93. if (img_text) {
  94. lv_obj_del(img_text);
  95. img_text = NULL;
  96. }
  97. // Create timer for animation
  98. my_tim_ctx.count_val = -90;
  99. my_tim_ctx.scr = scr;
  100. lv_timer_create(anim_timer_cb, 20, &my_tim_ctx);
  101. // Disable button
  102. lv_obj_add_state(btn, LV_STATE_DISABLED);
  103. }
  104. static void btn_cb(lv_event_t *e)
  105. {
  106. lv_obj_t *scr = lv_event_get_user_data(e);
  107. start_animation(scr);
  108. }
  109. void example_lvgl_demo_ui(lv_disp_t *disp)
  110. {
  111. lv_obj_t *scr = lv_disp_get_scr_act(disp);
  112. // Create image
  113. img_logo = lv_img_create(scr);
  114. #if CONFIG_EXAMPLE_LCD_IMAGE_FROM_FILE_SYSTEM
  115. lv_img_set_src(img_logo, "S:/spiffs/esp_logo.png");
  116. #elif CONFIG_EXAMPLE_LCD_IMAGE_FROM_EMBEDDED_BINARY
  117. lv_img_set_src(img_logo, &esp_logo);
  118. #endif
  119. btn = lv_btn_create(scr);
  120. lv_obj_t *lbl = lv_label_create(btn);
  121. lv_label_set_text_static(lbl, LV_SYMBOL_REFRESH" SHOW AGAIN");
  122. lv_obj_set_style_text_font(lbl, &lv_font_montserrat_20, 0);
  123. lv_obj_align(btn, LV_ALIGN_BOTTOM_LEFT, 30, -30);
  124. // Button event
  125. lv_obj_add_event_cb(btn, btn_cb, LV_EVENT_CLICKED, scr);
  126. start_animation(scr);
  127. }