What is the best way to modify lv_gauge.c

I want to create an engine rpm gauge using lv_arduino.

I’ve manage to create it by using gauge object and set range by:

lv_gauge_set_range(gauge1,0,8000)

This is working with a range of 0 to 8000 rpm and I can make the needle move ok.

The trouble is the text in the scale label is show 0, 1000, 2000, 3000 and so on. The text is really small and I don’t really need all the 000s. I just want it to show 0,1,2,3,4…

I’ve tried setting to range to 0 to 8 and scale the input but then the needle doesn’t move smoothly because there is not enough resolution between the steps.

How can I make the text in the label different than the standard text ? That is from 0, 1000, 2000… to 0, 1, 2

I’ve tried to modify the lv_gauge.c code by making a copy of it first then adding a prefix so to separate between the two files but it won’t compile.

This feature will be available in version 7.0. Use lv_gauge_set_formatter_cb.

When do you think version 7.0 will be release ?

(The release is planned for 18 May.)

I’ve upgraded to v7 and my code looks like this:

/*Describe the color for the needles*/
static lv_color_t needle_colors[1];
needle_colors[0] = LV_COLOR_BLUE;
  
/*Create a gauge*/
lv_obj_t * gauge1 = lv_gauge_create(lv_scr_act(), NULL);
lv_gauge_set_range(gauge1,0,8000);
lv_gauge_set_needle_count(gauge1, 1, needle_colors);
lv_gauge_set_scale(gauge1,260,17,9);

lv_gauge_set_critical_value(gauge1,7000);

lv_obj_set_size(gauge1, 300, 300);
lv_obj_align(gauge1, NULL, LV_ALIGN_CENTER, 0, 0);
 
/*Set the values*/
lv_gauge_set_value(gauge1, 0, 1200);

Now I want to change the label text from 1000 to 1, 2000 to 2 and so on.
I’m looking at lv_gauge_set_formatter_cb but can’t figure out the syntax.

Please help with changing the label text.

typedef void (*lv_gauge_format_cb_t)(lv_obj_t *gauge, char *buf, int bufsize, int32_t value)

You can use snprintf(buf, bufsize, "%d", value / 1000); inside that callback.

1 Like

It is almost what I wanted.

The only problem is the scale colour. At 0 it is white but by 6 it is grey.
What parameter I need to change to keep it white ?
Also how do I change the text size ?

The code:

void gauge_formatter_cb(lv_obj_t *gauge, char *buf, int bufsize, int32_t value)
{
  //Serial.println("******lv_gauge_set_formatter_cb*****\n");
  snprintf(buf, bufsize, "%d", value / 1000);

}

void lv_ex_gauge_1(void)
{
    /*Describe the color for the needles*/
    static lv_color_t needle_colors[1];
    needle_colors[0] = LV_COLOR_RED;

    static lv_style_t style;
    lv_style_init(&style);

    lv_style_set_bg_color(&style, LV_STATE_DEFAULT, LV_COLOR_BLACK);

    lv_style_set_line_color(&style, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_style_set_scale_end_color(&style, LV_STATE_DEFAULT, LV_COLOR_RED);
 
    lv_style_set_text_color(&style, LV_STATE_DEFAULT, LV_COLOR_WHITE);
 
    /*Create a gauge*/
     gauge1 = lv_gauge_create(lv_scr_act(), NULL);
      
    lv_obj_add_style(gauge1, LV_GAUGE_PART_MAJOR, &style);
    lv_obj_add_style(gauge1, LV_GAUGE_PART_MAIN, &style);
    
    lv_gauge_set_range(gauge1,0,8000);
    lv_gauge_set_needle_count(gauge1, 1, needle_colors);
    lv_gauge_set_scale(gauge1,260,17,9);
    lv_gauge_set_critical_value(gauge1,7000);
    
    lv_gauge_ext_t gauge_format;
    gauge_format.format_cb = gauge_formatter_cb;
    lv_gauge_set_formatter_cb(gauge1,gauge_formatter_cb);
    
    lv_obj_set_size(gauge1, 300, 300);
    lv_obj_align(gauge1, NULL, LV_ALIGN_CENTER, 0, 0);
 
    /*Set the values*/
     lv_gauge_set_value(gauge1, 0, 1100);
}

You’ll need to use a different font.

I believe you need to change bg_grad_color and/or bg_color on LV_GAUGE_PART_MAJOR.

Scale colour is correct now, thanks.

What is the correct way to set a new font ?
I’ve tried:
lv_style_set_text_font(&style, LV_STATE_DEFAULT, LV_FONT_MONTSERRAT_28);

but it crashed at start up.

According to
In https://docs.lvgl.io/v7/en/html/overview/font.html
my_style.text.font = &lv_font_roboto_28; /*Set a larger font*/

So I’ve tried:
style.text.font = &LV_FONT_MONTSERRAT_28;
but that won’t compile.

How do I set a new font for use with Gauge ?

Whoops… that font page needs to be updated with the new styles.

Setting the font the way you described should work, as far as I know. I’ll try it later today.

I’ve just updated this section.

It should be done like this:

lv_style_set_text_font(&style, LV_STATE_DEFAULT, &lv_font_montserrat_28);

Whoops… I forgot that LV_FONT_MONTSERRAT_28 does not expand to &lv_font_montserrat_28, but rather to 1!

@skpang must have compiler warnings off, because usually the compiler will complain about this conversion of an integer to a pointer.

Sorry for digging up this old topic, but it suits my question perfectly. How would you format the scale in v8 and with lv_meter instead of lv_gauge? I need every number divided by 100.

I’m trying to replicate an RPM counter for our old caravan. Didn’t test it on an ESP32 with circular GC9A01 display yet, but LVGL looks very promising (and I’m almost finished):
image

LOL. I’m digging it up also. How did you modify lv_gauge? I’m trying to change the scale and all that fun stuff.