lv_port_disp.c 3.5 KB
Newer Older
R
Rbb666 已提交
1
/*
R
Rbb666 已提交
2
 * Copyright (c) 2006-2023, RT-Thread Development Team
R
Rbb666 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-11-24     Rbb666       The first version
 */
#include <lvgl.h>
#include "hal_data.h"

#if DLG_LVGL_USE_GPU_RA6M3
    #include "lv_port_gpu.h"
#endif

R
Rbb666 已提交
17 18 19 20 21 22
#ifdef BSP_USING_SPI_LCD
    #include "lcd_ili9341.h"
#else
    #include "lcd_port.h"
#endif

R
Rbb666 已提交
23 24 25 26 27 28 29
#define COLOR_BUFFER  (LV_HOR_RES_MAX * LV_VER_RES_MAX / 4)

/*A static or global variable to store the buffers*/
static lv_disp_draw_buf_t disp_buf;

/*Descriptor of a display driver*/
static lv_disp_drv_t disp_drv;
R
Rbb666 已提交
30
static struct rt_device_graphic_info info;
R
Rbb666 已提交
31 32 33 34 35

/*Static or global buffer(s). The second buffer is optional*/
// 0x1FFE0000    0x20040000
__attribute__((section(".ARM.__at_0x1FFE0000"))) lv_color_t buf_1[COLOR_BUFFER];

R
Rbb666 已提交
36
#if !DLG_LVGL_USE_GPU_RA6M3
R
Rbb666 已提交
37 38 39 40 41 42 43 44 45 46
static void color_to16_maybe(lv_color16_t *dst, lv_color_t *src)
{
#if (LV_COLOR_DEPTH == 16)
    dst->full = src->full;
#else
    dst->ch.blue = src->ch.blue;
    dst->ch.green = src->ch.green;
    dst->ch.red = src->ch.red;
#endif
}
R
Rbb666 已提交
47
#endif
R
Rbb666 已提交
48 49 50

static void disp_flush(lv_disp_drv_t *disp_drv, const lv_area_t *area, lv_color_t *color_p)
{
R
Rbb666 已提交
51 52 53 54 55
#ifdef BSP_USING_SPI_LCD
    lcd_fill_array_spi(area->x1, area->y1, area->x2, area->y2, color_p);
#elif DLG_LVGL_USE_GPU_RA6M3
    lv_port_gpu_flush();
#else
R
Rbb666 已提交
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    int x1, x2, y1, y2;

    x1 = area->x1;
    x2 = area->x2;
    y1 = area->y1;
    y2 = area->y2;

    /*Return if the area is out the screen*/
    if (x2 < 0)
        return;
    if (y2 < 0)
        return;
    if (x1 > info.width - 1)
        return;
    if (y1 > info.height - 1)
        return;

    /*Truncate the area to the screen*/
    int32_t act_x1 = x1 < 0 ? 0 : x1;
    int32_t act_y1 = y1 < 0 ? 0 : y1;
    int32_t act_x2 = x2 > info.width - 1 ? info.width - 1 : x2;
    int32_t act_y2 = y2 > info.height - 1 ? info.height - 1 : y2;

    uint32_t x;
    uint32_t y;
    long int location = 0;

    /* color_p is a buffer pointer; the buffer is provided by LVGL */
    lv_color16_t *fbp16 = (lv_color16_t *)info.framebuffer;

    for (y = act_y1; y <= act_y2; y++)
    {
        for (x = act_x1; x <= act_x2; x++)
        {
            location = (x) + (y) * info.width;
            color_to16_maybe(&fbp16[location], color_p);
            color_p++;
        }

        color_p += x2 - act_x2;
    }
R
Rbb666 已提交
97 98
#endif
    lv_disp_flush_ready(disp_drv);
R
Rbb666 已提交
99 100 101 102
}

void lv_port_disp_init(void)
{
R
Rbb666 已提交
103
#ifdef BSP_USING_SPI_LCD
104
    spi_lcd_init(20);
R
Rbb666 已提交
105 106
#else
    static rt_device_t device;
R
Rbb666 已提交
107 108 109 110 111 112 113 114 115 116 117
    /* LCD Device Init */
    device = rt_device_find("lcd");
    RT_ASSERT(device != RT_NULL);

    if (rt_device_open(device, RT_DEVICE_OFLAG_RDWR) == RT_EOK)
    {
        rt_device_control(device, RTGRAPHIC_CTRL_GET_INFO, &info);
    }

    RT_ASSERT(info.bits_per_pixel == 8 || info.bits_per_pixel == 16 ||
              info.bits_per_pixel == 24 || info.bits_per_pixel == 32);
R
Rbb666 已提交
118
#endif
R
Rbb666 已提交
119 120 121 122 123 124
    /*Initialize `disp_buf` with the buffer(s). With only one buffer use NULL instead buf_2 */
    lv_disp_draw_buf_init(&disp_buf, buf_1, NULL, COLOR_BUFFER);

    lv_disp_drv_init(&disp_drv); /*Basic initialization*/

    /*Set the resolution of the display*/
R
Rbb666 已提交
125 126
    disp_drv.hor_res = LV_HOR_RES_MAX;
    disp_drv.ver_res = LV_VER_RES_MAX;
R
Rbb666 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141

    /*Set a display buffer*/
    disp_drv.draw_buf = &disp_buf;

    /*Used to copy the buffer's content to the display*/
    disp_drv.flush_cb = disp_flush;

#if DLG_LVGL_USE_GPU_RA6M3
    /* Initialize GPU module */
    lv_port_gpu_init();
#endif /* LV_PORT_DISP_GPU_EN */

    /*Finally register the driver*/
    lv_disp_drv_register(&disp_drv);
}