Lvgl draw a red Canvas

Important: unclear posts may not receive useful answers.


when i want to draw a red canvas on my project,it does not work as my expect.i set align as LV_ALIGN_IN_TOP_LEFT.when i output first 8 bytes of color_p,it’s FF FF FF FF FF FF FF FF.But red color should be 00 1F 00 1F 001F 00 1F

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

What LVGL version are you using?

What do you want to achieve?

What have you tried so far?

Code to reproduce

The code block(s) should be formatted like:

/*You code here*/

#define CANVAS_WIDTH  50
#define CANVAS_HEIGHT  50
void lv_ex_canvas_test(void)

    lv_obj_t* canvas = lv_canvas_create(lv_scr_act(), NULL);
    lv_canvas_set_buffer(canvas, cbuf, CANVAS_WIDTH, CANVAS_HEIGHT, LV_IMG_CF_TRUE_COLOR);
    //lv_canvas_set_palette(canvas, 0, LV_COLOR_TRANSP);
    lv_canvas_set_palette(canvas, 1, LV_COLOR_RED);
    lv_obj_align(canvas, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    lv_canvas_fill_bg(canvas, LV_COLOR_RED, LV_OPA_TRANSP);


static void LcdDisp_callback(lv_disp_drv_t * disp_drv, const lv_area_t * area, lv_color_t * color_p)
    /* IMPORTANT!!!
     * Inform the graphics library that you are ready with the flushing*/
    lv_coord_t hres = disp_drv->rotated == 0 ? disp_drv->hor_res : disp_drv->ver_res;
    lv_coord_t vres = disp_drv->rotated == 0 ? disp_drv->ver_res : disp_drv->hor_res;
    TRACE(DBG_TRACE_LVL,"%s--->area->x2=%d  area->x1=%d  area->y1=%d  area->y2=%d  hres=%d  vres=%d rotated=%d\r\n",__FUNCTION__,area->x2,area->x1,area->y1,area->y2,hres,vres,disp_drv->rotated);

    if(area->x2 < 0 || area->y2 < 0 || area->x1 > hres - 1 || area->y1 > vres - 1)

/*    int32_t x;*/
/*    int32_t y;*/
/*    for(y = area->y1; y <= 1; y++)*/
/*    {*/
/*        for(x = area->x1; x <= area->x2; x++)*/
/*        {*/
/*            TemppBuffer[y *disp_drv->hor_res + x] = lv_color_to16(*color_p);*/
/*            //TRACE(DBG_TRACE_LVL,"%02x",FrameBuffer[(y * disp_drv->hor_res)+x]);*/
/*            color_p++;*/
/*        }*/

/*    }*/
    u32 i=0;
    lark_lcd_write(LCD_MODE_COLOR, area->x1,area->y1,area->x2-area->x1+1,area->y2-area->y1+1,(pu8)color_p);


## Screenshot and/or video
If possible, add screenshots and/or videos about the current state.

try to change this line using LV_IMG_CF_INDEXED_1BIT

lv_canvas_set_buffer(canvas, cbuf, 50, 50, LV_IMG_CF_INDEXED_1BIT);

i tried with LV_IMG_CF_INDEXED_1BIT.when i output fist 8 bytes,it was ff ff ff ff ff ff ff ff

here is my setting of LCD is RGB565

does i need to modify other setting of lvgl?

With LVGL simulator Codeblocks i see a red rectangle, maybe an issue with your TFT driver.

i think it was my LCD driver issue before,so i output the fist 8 bytes of’s not red color.
red color should be 00 1F 001F 001F 001F

here is my lcd driver test code

void lcd_test()
u32 i=0;
lark_lcd_write(LCD_MODE_COLOR, 0,0,50,50,(pu8)DispBuffer);

would you give some guidance?

i found this function used lv_color32_t,however the setting of lvgl is 16(RGB565)

Please check if your code works correctly on the simulator. If it does, the issue is likely in your display driver (flush_cb).

would you share a simulator project (with RGB565)on windows?i pull windows simulator (with RGB888)form gitlib .

when i modify color depth as RGB565.simulator does not works as expect

when i test my code in simulator,it does not display as expect

would you help to check my setting of lvgl?
lvgl setting file

when i modify color depth as 16 on simulator,display as picture

Your lv_ex_canvas_test code, from where did you get this code?

Have you read the lvgl docu pages ( about canvas?

What does the example code show?
It shows this for defining the buffer:

static lv_color_t buffer[LV_CANVAS_BUF_SIZE_TRUE_COLOR(width, height)]. LV_CANVAS_BUF_SIZE_... 

The used type is lv_color_t, but you use uint8_t.

And also for the lv_canvas_set_palette it is correct to use LV_IMG_CF_TRUE_COLOR.

And please, if you want to show code, don’t do screenshots and post the images.
When you post it as ‘code’ it is easy to copy and paste your code for us for doing some investigations.
And it is enough to post it as ‘code’ and not as ‘code’ and screenshot.

So this code is showing a red rectangle on my screen (on real hardware)

#define CANVAS_WIDTH   50
#define CANVAS_HEIGHT  50

void lv_ex_canvas_test (void)

    lv_obj_t* canvas = lv_canvas_create (lv_scr_act (), NULL);

    lv_canvas_set_buffer (canvas, cbuf, CANVAS_WIDTH, CANVAS_HEIGHT, LV_IMG_CF_TRUE_COLOR);

    lv_canvas_set_palette (canvas, 1, LV_COLOR_RED);
    lv_obj_align          (canvas, NULL, LV_ALIGN_IN_TOP_LEFT, 0, 0);
    lv_canvas_fill_bg     (canvas, LV_COLOR_RED, LV_OPA_TRANSP);

i got test code from lvgl docu page(
when i test your code on windows simulator,it display as pitcure.
i modify color depth as 16 and modify simulator size same as my LCD

here is flush_cb() on simulater
int32_t x;

for(y = area->y1; y <= area->y2 && y < disp_drv->ver_res; y++) {
    for(x = area->x1; x <= area->x2; x++) {
        printf("----->>color_p=%x\r\n", *color_p);
        printf("lv_color_to16(*color_p)=%x\r\n", lv_color_to16(*color_p));
        monitor.tft_fb[y *disp_drv->hor_res + x] = lv_color_to16(*color_p);
        //printf("%04x\r\n", monitor.tft_fb[y * disp_drv->hor_res + x]);


i want to got correct color value from simulator,then debug my lcd used correct color value