How to Add Custom Labelling to a Gauge

What MCU/Processor/Board and compiler are you using?

Compiler: Eclipse running on Xilinx SDK
Board: Xilinx Development board

What LVGL version are you using?

8.1

What do you want to achieve?

I want to add custom labelling to a gauge. In lvgl version 7, I was able to do this with the use of the lv_gauge_set_formatter_cb function. This is how I did it:

    lv_gauge_set_formatter_cb(vacuumPressureGauge, Vacuum_GaugeFormat);
static void Vacuum_GaugeFormat(lv_obj_t* gauge, char* buf, int bufSz, int value)
{
	snprintf(buf, bufSz, "E-%d", value/100 - 8);
}

However, this function no longer exists in LVGL version 8 and I don’t know how to add custom labelling to the gauge anymore

What have you tried so far?

The only thing that seemed to change the labels on the gauge for me was using the lv_meter_set_scale_range function. However, this function only creates a linear gauge and does not allow me to create a logarithmic gauge with custom labelling values.

Screenshot and/or video

Here’s how the gauge looked in my version 7 build (which I want to emulate in my version 8 build):

@embeddedt or @kisvegabor if you could help me out with this that would be greatly appreciated!

I’m also looking for an alternative to lv_gauge_set_formatter_cb in LVGL 8. A few weeks ago I tried to revive an old thread about this topic, but sadly haven’t gotten any replies yet.

I use in v8 event LV_EVENT_DRAW_PART_BEGIN to customize the gauge labeling

lv_obj_add_event_cb(meter, smeter_event_cb, LV_EVENT_DRAW_PART_BEGIN, NULL);

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);
	
	switch (code)
	{
	case LV_EVENT_DRAW_PART_BEGIN:
		dsc->value = dsc->value / 10;
		if (dsc->value == 1)
		{
			strcpy(dsc->text, "S");			
		}
		
		if (dsc->value > 9)
		{
			if (dsc->value == 10)
				dsc->value = 20;
			if (dsc->value == 11)
				dsc->value = 40;
			if (dsc->value == 12)
				dsc->value = 60;				
		}
		lv_snprintf(dsc->text, sizeof(dsc->text), "%d", dsc->value);	
		break;
	}
}
3 Likes

Thanks! Works like a charm!

Kinda stupid of me that I’ve always overlooked the part in the documentation where it literally explains how to do this with events… (Meter (lv_meter) — LVGL documentation)