Custom meter labels in v8.4

Hi -

I am trying to add custom tick labels to a meter in v8.4 for a tide clock. The meter scale range goes from 0-600. There are 60 minor ticks, 12 major ticks and 12 labels. So it’s a regular clock.

Based on some other posts I have seen, I am using the following code:

static void smeter_event_cb(lv_event_t * e)
{
	lv_event_code_t	code = lv_event_get_code(e);
	lv_obj_draw_part_dsc_t * dsc  = (lv_obj_draw_part_dsc_t *)lv_event_get_param(e);

    static char * custom_labels[] = {"High", "5", "4", "3", "2", "1", "Low", "5", "4", "3", "2", "1", "High", NULL};

    switch (code)
	{
	case LV_EVENT_DRAW_PART_BEGIN:
        int value = dsc->value;
        if (value % 50 == 0) {
            value = value / 50;
            if (value < sizeof(custom_labels) / sizeof (char *) - 1) {
                ESP_LOGI("LVGL", "code: %d, label: %s", value, custom_labels[value]);
                lv_snprintf(dsc->text, sizeof(dsc->text), "%s", custom_labels[value]);	
            }
        }
		break;
    default:
        break;
	}
}

From the documentation and the other code samples I’ve seen, copying the new label to dsc->text should change the displayed label, but I am still seeing 1-12, not my desired labels.

Can anyone help me out?

Thanks.

Do you add the event handler to the correct object? To the scale object?

Thanks for the suggestion. I tried that, but it won’t compile as the pointer to the scale is not to the correct type. If I cast it to lv_obj_t to make it compile, it dumps the stack.

I have been able to get it to work, but only when when I attach it to the meter. Doing that, I get a lot of events, including for the minor scale and other elements of the meter, but I was able to get it to work as I want if I filter for just the major tick events. The code above changes the labels to the minor ticks, which aren’t actually drawn at all.

I also check if the obj is the correct one.

I cannot compare exactly to your code because I have customized the meter widget quite a bit.

scale is here the scale object

if (code == LV_EVENT_DRAW_PART_BEGIN && (lv_meter_scale_c *)dsc->sub_part_ptr == scale)
{

}

Good tip! That’s a nice, clean solution. I have it doing what I want now. Thanks!