lizhaoming634:
can you test this code?
Yes, I’ll try your code now, but I also got something
class Hdg_rotate {
public:
Hdg_rotate(lv_obj_t* parent, int x, int y, int width, int height);
void start_color_animation();
void set_rotation(int angle); // Method for setting the rotation angle
private:
lv_obj_t* scale_loader; // Scale object
};
// Callback function for drawing
static void draw_event_cb(lv_event_t* e) {
lv_obj_t* obj = static_cast<lv_obj_t*>(lv_event_get_target(e)); // Explicit type casting
lv_draw_task_t* draw_task = lv_event_get_draw_task(e);
lv_draw_dsc_base_t* base_dsc = static_cast<lv_draw_dsc_base_t*>(lv_draw_task_get_draw_dsc(draw_task)); // Explicit type casting
lv_draw_line_dsc_t* line_draw_dsc = lv_draw_task_get_line_dsc(draw_task);
if (base_dsc->part == LV_PART_ITEMS) {
if (line_draw_dsc) {
int32_t value_of_line = lv_map(base_dsc->id1, 0, 100, 0, 360);
int32_t ratio = lv_map(value_of_line, 0, 360, LV_OPA_TRANSP, LV_OPA_COVER);
line_draw_dsc->color = lv_color_mix(lv_color_make(13, 195, 253), lv_color_make(37, 57, 115), ratio);
}
}
}
// Callback function for rotation
static void time_cb(lv_timer_t* t) {
lv_obj_t* scale_loader = static_cast<lv_obj_t*>(lv_timer_get_user_data(t));
static int i = 0;
i = (i + 1) % 36;
lv_scale_set_rotation(scale_loader, 10 * i);
}
// Implementation of methods
Hdg_rotate::Hdg_rotate(lv_obj_t* parent, int x, int y, int width, int height) {
scale_loader = lv_scale_create(parent); // Create scale
lv_scale_set_mode(scale_loader, LV_SCALE_MODE_ROUND_INNER);
lv_scale_set_label_show(scale_loader, true);
lv_obj_set_size(scale_loader, width, height);
lv_obj_set_pos(scale_loader, x, y);
lv_obj_set_style_arc_opa(scale_loader, LV_OPA_TRANSP, LV_PART_MAIN);
lv_obj_set_style_length(scale_loader, 10, LV_PART_ITEMS);
lv_scale_set_total_tick_count(scale_loader, 37);
lv_scale_set_major_tick_every(scale_loader, 3);
lv_scale_set_angle_range(scale_loader, 360);
static const char * hdg_ticks[] = {"0", "3", "6", "9", "12", "15", "18", "21", "24", "27", "30", "33", NULL};
lv_scale_set_text_src(scale_loader, hdg_ticks);
static lv_style_t section_minor_tick_style;
static lv_style_t section_major_line_style;
lv_style_init(§ion_minor_tick_style);
lv_style_init(§ion_major_line_style);
lv_style_set_length(§ion_minor_tick_style, 20); /* tick length */
// lv_style_set_line_width(§ion_major_line_style, 4U); /* Tick width */
lv_style_set_line_width(§ion_major_line_style, 5); /* Tick width */
lv_scale_section_t* section = lv_scale_add_section(scale_loader);
lv_scale_section_set_range(section, 0, 100);
lv_scale_section_set_style(section, LV_PART_ITEMS, §ion_minor_tick_style);
lv_scale_section_set_style(section, LV_PART_INDICATOR, §ion_major_line_style);
lv_obj_add_event_cb(scale_loader, draw_event_cb, LV_EVENT_DRAW_TASK_ADDED, NULL);
lv_obj_add_flag(scale_loader, LV_OBJ_FLAG_SEND_DRAW_TASK_EVENTS);
// Create a timer to rotate the scale
lv_timer_create(time_cb, 100, scale_loader);
}
void Hdg_rotate::set_rotation(int angle) {
lv_scale_set_rotation(scale_loader, angle);
}
Usage
Hdg_rotate hdg_rotate1(lv_screen_active(), 30, 30, 200, 200);
Hdg_rotate hdg_rotate2(lv_screen_active(), 30, 270, 200, 200);
Hdg_rotate hdg_rotate3(lv_screen_active(), 270, 30, 200, 200);
Hdg_rotate hdg_rotate4(lv_screen_active(), 270, 270, 200, 200);
The plans include programming a C++ class to set rotation values to the desired angle not using the built-in LVGL animation, but by the desired angle in degrees.