diff options
| author | GuEe-GUI <[email protected]> | 2025-12-12 15:58:12 +0800 |
|---|---|---|
| committer | R b b666 <[email protected]> | 2025-12-15 16:54:23 +0800 |
| commit | 5abecc1fd0c3ee9d71ce6b3ff3ee95eda4825065 (patch) | |
| tree | 752168c49b2d3e043e5c264cc0e23fa5376516e5 /examples/test | |
| parent | 35bae6824b61910600cb47a3f1c345671609748d (diff) | |
[dm][graphic] support dm mode
1. Add backlight framework for graphic.
2. Add framebuffer and plane, power, EDID for graphic framework
3. Add boot logo render for graphic
4. Update lcd.h
Signed-off-by: GuEe-GUI <[email protected]>
Diffstat (limited to 'examples/test')
| -rw-r--r-- | examples/test/dm_graphic_test.c | 310 | ||||
| -rw-r--r-- | examples/test/dm_hmi_test.c | 421 |
2 files changed, 731 insertions, 0 deletions
diff --git a/examples/test/dm_graphic_test.c b/examples/test/dm_graphic_test.c new file mode 100644 index 0000000000..a6894ddc6e --- /dev/null +++ b/examples/test/dm_graphic_test.c @@ -0,0 +1,310 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-02-25 GuEe-GUI the first version + */ + +#include <rtthread.h> +#include <rtdevice.h> + +#include <drivers/misc.h> + +#ifdef RT_USING_GRAPHIC +typedef rt_int32_t fixed; /* Q16.16 */ + +#define FIX_ONE 65536 +#define FIX_06 19661 /* 0.3 */ +#define FIX_35 22938 /* 0.35 */ +#define FIX_14 917504 /* 14.0 */ +#define FIX_286 187432 /* 2.86 */ +#define FIX_75 49152 /* 0.75 */ +#define FIX_1_3 21845 +#define FIX_2_3 43690 +#define FIX_6 393216 +#define FIX_3 196608 +#define FIX_255 16711680 /* 255 */ + +#define FMUL(a,b) ((fixed)(((rt_int64_t)(a) * (b)) >> 16)) +#define FDIV(a,b) ((fixed)(((rt_int64_t)(a) << 16) / (b))) + +typedef struct { fixed x, y, z; } vec3f; + +rt_inline fixed fix_floor(fixed x) +{ + return x & 0xffff0000; +} + +rt_inline fixed fix_fract(fixed x) +{ + return x & 0x0000ffff; +} + +rt_inline fixed fix_abs(fixed x) +{ + return rt_abs(x); +} + +rt_inline fixed fix_clamp(fixed x, fixed a, fixed b) +{ + return rt_clamp(x, a, b); +} + +static vec3f clamp3(vec3f v, fixed a, fixed b) +{ + vec3f r = + { + .x = fix_clamp(v.x, a, b), + .y = fix_clamp(v.y, a, b), + .z = fix_clamp(v.z, a, b), + }; + + return r; +} + +static vec3f mix3(vec3f A, vec3f B, fixed t) +{ + fixed omt = FIX_ONE - t; + vec3f r = + { + .x = FMUL(A.x, omt) + FMUL(B.x, t), + .y = FMUL(A.y, omt) + FMUL(B.y, t), + .z = FMUL(A.z, omt) + FMUL(B.z, t), + }; + + return r; +} + +static vec3f hsv2rgb(vec3f c) +{ + vec3f t, r, base, mixed; + fixed pX = FMUL(fix_fract(c.x + FIX_ONE), FIX_6); + fixed pY = FMUL(fix_fract(c.x + FIX_2_3), FIX_6); + fixed pZ = FMUL(fix_fract(c.x + FIX_1_3), FIX_6); + + pX = fix_abs(pX - FIX_3); + pY = fix_abs(pY - FIX_3); + pZ = fix_abs(pZ - FIX_3); + + t.x = pX - FIX_ONE; + t.y = pY - FIX_ONE; + t.z = pZ - FIX_ONE; + t = clamp3(t, 0, FIX_ONE); + + base.x = FIX_ONE; + base.y = FIX_ONE; + base.z = FIX_ONE; + + mixed = mix3(base, t, c.y); + + r.x = FMUL(c.z, mixed.x); + r.y = FMUL(c.z, mixed.y); + r.z = FMUL(c.z, mixed.z); + + return r; +} + +static void shader_frame(int px, int py, int width, int height, int frame, + rt_uint8_t *r, rt_uint8_t *g, rt_uint8_t *b) +{ + vec3f col; + fixed uv_x, uv_y, shift, size_computed, st, x; + + uv_x = FDIV((fixed)(px << 16), (fixed)(width << 16)); + uv_y = FDIV((fixed)(py << 16), (fixed)(width << 16)); + + shift = fix_fract(FDIV(FMUL((frame << 16), FIX_06), FIX_286)); + shift = FMUL(shift, FIX_286); + + size_computed = FMUL(uv_x + shift - uv_y, FIX_35); + + st = FMUL(size_computed, FIX_14); + x = FDIV(fix_floor(st), FIX_14); + + col = hsv2rgb((vec3f){ x, FIX_75, FIX_ONE }); + + *r = (rt_uint8_t)((FMUL(col.x, FIX_255) >> 16) & 255); + *g = (rt_uint8_t)((FMUL(col.y, FIX_255) >> 16) & 255); + *b = (rt_uint8_t)((FMUL(col.z, FIX_255) >> 16) & 255); +} + +static void conv_gray4(rt_uint8_t r, rt_uint8_t g, rt_uint8_t b, void *pixel) +{ + rt_uint8_t gray = (r * 30 + g * 59 + b * 11) / 100; + *(rt_uint8_t *)pixel = gray >> 4; +} + +static void conv_gray16(rt_uint8_t r, rt_uint8_t g, rt_uint8_t b, void *pixel) +{ + rt_uint16_t gray = (r * 30 + g * 59 + b * 11) / 100; + rt_uint16_t out = (gray << 8) | gray; + *(rt_uint16_t *)pixel = out; +} + +static void conv_rgb332(rt_uint8_t r, rt_uint8_t g, rt_uint8_t b, void *pixel) +{ + *(rt_uint8_t*)pixel = ((r >> 5) << 5) | ((g >> 5) << 2) | (b >> 6); +} + +static void conv_rgb444(rt_uint8_t r, rt_uint8_t g, rt_uint8_t b, void *pixel) +{ + *(rt_uint16_t *)pixel = ((r >> 4) << 8) | ((g >> 4) << 4) | (b >> 4); +} + +static void conv_rgb565(rt_uint8_t r, rt_uint8_t g, rt_uint8_t b, void *pixel) +{ + *(rt_uint16_t *)pixel = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3); +} + +static void conv_rgb565p(rt_uint8_t r, rt_uint8_t g, rt_uint8_t b, void *pixel) +{ + *(rt_uint16_t *)pixel = ((g >> 2) << 10) | ((r >> 3) << 5) | (b >> 3); +} + +static void conv_bgr565(rt_uint8_t r, rt_uint8_t g, rt_uint8_t b, void *pixel) +{ + *(rt_uint16_t *)pixel = ((b >> 3) << 11) | ((g >> 2) << 5) | (r >> 3); +} + +static void conv_rgb666(rt_uint8_t r, rt_uint8_t g, rt_uint8_t b, void *pixel) +{ + rt_uint8_t *p = (rt_uint8_t *)pixel; + + p[0] = r & 0xfc; + p[1] = g & 0xfc; + p[2] = b & 0xfc; +} + +static void conv_rgb888(rt_uint8_t r, rt_uint8_t g, rt_uint8_t b, void *pixel) +{ + rt_uint8_t *p = (rt_uint8_t *)pixel; + + p[0] = r; + p[1] = g; + p[2] = b; +} + +static void conv_bgr888(rt_uint8_t r, rt_uint8_t g, rt_uint8_t b, void *pixel) +{ + rt_uint8_t *p = (rt_uint8_t*)pixel; + p[0] = b; + p[1] = g; + p[2] = r; +} + +static void conv_argb888(rt_uint8_t r, rt_uint8_t g, rt_uint8_t b, void *pixel) +{ + *(rt_uint32_t *)pixel = (0xffU << 24) | (r << 16) | (g << 8) | b; +} + +static void conv_abgr888(rt_uint8_t r, rt_uint8_t g, rt_uint8_t b, void *pixel) +{ + *(rt_uint32_t *)pixel = (0xffU << 24) | (b << 16) | (g << 8) | r; +} + +static void (*conv_funcs[])(rt_uint8_t r, rt_uint8_t g, rt_uint8_t b, void *pixel) = +{ + [RTGRAPHIC_PIXEL_FORMAT_GRAY4] = conv_gray4, + [RTGRAPHIC_PIXEL_FORMAT_GRAY16] = conv_gray16, + [RTGRAPHIC_PIXEL_FORMAT_RGB332] = conv_rgb332, + [RTGRAPHIC_PIXEL_FORMAT_RGB444] = conv_rgb444, + [RTGRAPHIC_PIXEL_FORMAT_RGB565] = conv_rgb565, + [RTGRAPHIC_PIXEL_FORMAT_RGB565P] = conv_rgb565p, + [RTGRAPHIC_PIXEL_FORMAT_BGR565] = conv_bgr565, + [RTGRAPHIC_PIXEL_FORMAT_RGB666] = conv_rgb666, + [RTGRAPHIC_PIXEL_FORMAT_RGB888] = conv_rgb888, + [RTGRAPHIC_PIXEL_FORMAT_BGR888] = conv_bgr888, + [RTGRAPHIC_PIXEL_FORMAT_ARGB888] = conv_argb888, + [RTGRAPHIC_PIXEL_FORMAT_ABGR888] = conv_abgr888, +}; + +rt_err_t graphic_start(const char *gdev, int count) +{ + rt_err_t err; + rt_uint8_t *vfb, *fb, *pixel, bpp; + struct rt_device_graphic_info info; + struct rt_device *dev = rt_device_find(gdev); + void (*conv_func)(rt_uint8_t r, rt_uint8_t g, rt_uint8_t b, void *pixel); + + if (!dev) + { + return -RT_EINVAL; + } + + if ((err = rt_device_open(dev, 0))) + { + return err; + } + + if ((err = rt_device_control(dev, RTGRAPHIC_CTRL_GET_INFO, &info))) + { + goto _end; + } + + if (!(vfb = rt_malloc(info.smem_len))) + { + err = -RT_ENOMEM; + goto _end; + } + + bpp = info.bits_per_pixel / 8; + conv_func = conv_funcs[info.pixel_format]; + + for (int frame = 0; frame < count; ++frame) + { + fb = vfb; + + for (int y = 0; y < info.height; ++y) + { + pixel = fb; + + for (int x = 0; x < info.width; ++x) + { + rt_uint8_t r, g, b; + + shader_frame(x, y, info.width, info.height, frame, &r, &g, &b); + + conv_func(r, g, b, pixel); + + pixel += bpp; + } + + fb += info.pitch; + } + + rt_memcpy(info.framebuffer, vfb, info.smem_len); + } + + rt_free(vfb); + +_end: + rt_device_close(dev); + + return err; +} + +#ifdef RT_USING_FINSH +#include <stdlib.h> + +static int _graphic_start(int argc, char**argv) +{ + int count = 10; + const char *gdev = "fb0"; + + if (argc > 1) + { + gdev = argv[1]; + } + if (argc > 2) + { + count = atoi(argv[2]); + } + + return (int)graphic_start(gdev, count); +} +MSH_CMD_EXPORT_ALIAS(_graphic_start, graphic_start, fixed resolution only e.g: graphic_start("fb0", 10)); +#endif /* RT_USING_FINSH */ +#endif /* RT_USING_GRAPHIC */ diff --git a/examples/test/dm_hmi_test.c b/examples/test/dm_hmi_test.c new file mode 100644 index 0000000000..b8fea78ad6 --- /dev/null +++ b/examples/test/dm_hmi_test.c @@ -0,0 +1,421 @@ +/* + * Copyright (c) 2006-2021, RT-Thread Development Team + * + * SPDX-License-Identifier: Apache-2.0 + * + * Change Logs: + * Date Author Notes + * 2023-02-25 GuEe-GUI the first version + */ + +#include <rtthread.h> +#include <rtdevice.h> + +#include <cpuport.h> +#include <drivers/misc.h> + +#if defined(RT_USING_GRAPHIC) && defined(RT_USING_INPUT) + +#define CURSOR_WIDTH 64 +#define CURSOR_HEIGHT 64 + +struct hmi_info +{ + struct rt_device *gdev; + struct rt_device *idev; + + struct rt_device_graphic_info info; + struct rt_device_notify event_notify; + struct rt_input_handler handler; + + rt_bool_t event; + rt_bool_t vsync; + rt_bool_t keydown; + + rt_uint32_t x, y; + rt_uint32_t dx, dy; + rt_uint32_t bytes_per_pixel; + rt_ubase_t line[2]; + rt_ubase_t colors[4]; + + void *backend_framebuffer; +}; + +static rt_bool_t hmi_working; + +static struct rt_input_device *to_input_device(struct rt_device *idev) +{ + return rt_container_of(idev, struct rt_input_device, parent); +} + +static rt_ubase_t to_color(rt_uint8_t color255, rt_ubase_t color_max) +{ + return (rt_ubase_t)color255 * color_max / 255; +} + +static void hmi_reset(struct hmi_info *hmi) +{ + void *cursor; + rt_ubase_t none_alpha; + rt_ubase_t red_off, green_off, blue_off, alpha_off; + rt_ubase_t red_mask, green_mask, blue_mask, alpha_mask; + struct fb_var_screeninfo var; + + if (hmi->backend_framebuffer) + { + rt_free(hmi->backend_framebuffer); + } + + rt_device_control(hmi->gdev, FBIOGET_VSCREENINFO, &var); + rt_device_control(hmi->gdev, RTGRAPHIC_CTRL_GET_INFO, &hmi->info); + + hmi->backend_framebuffer = rt_malloc(hmi->info.smem_len); + + hmi->bytes_per_pixel = hmi->info.bits_per_pixel / 8; + red_off = var.red.offset; + red_mask = RT_GENMASK(var.red.length - 1, 0); + green_off = var.green.offset; + green_mask = RT_GENMASK(var.green.length - 1, 0); + blue_off = var.blue.offset; + blue_mask = RT_GENMASK(var.blue.length - 1, 0); + + if (var.transp.length) + { + alpha_off = var.transp.offset; + alpha_mask = RT_GENMASK(var.transp.length - 1, 0); + } + else + { + alpha_off = 0; + alpha_mask = 0; + } + + if ((cursor = rt_malloc(CURSOR_WIDTH * CURSOR_HEIGHT * hmi->bytes_per_pixel))) + { + rt_uint8_t *stream = cursor; + rt_ubase_t color = ((to_color(0x82, red_mask)) << red_off) | + (to_color(0x50, green_mask) << green_off) | + (to_color(0xdf, blue_mask) << blue_off) | + (to_color(0xcc, alpha_mask) << alpha_off); + + for (int y = 0; y < CURSOR_HEIGHT; ++y) + { + for (int x = 0; x < CURSOR_WIDTH; ++x) + { + rt_memcpy(stream, &color, hmi->bytes_per_pixel); + stream += hmi->bytes_per_pixel; + } + } + + rt_device_control(hmi->gdev, RT_DEVICE_CTRL_CURSOR_SET_TYPE, cursor); + rt_free(cursor); + } + + none_alpha = alpha_mask << alpha_off; + + hmi->line[0] = ~0UL; + hmi->line[1] = none_alpha; + + hmi->colors[0] = ((to_color(0xff, red_mask)) << red_off) | + (to_color(0x4b, green_mask) << green_off) | + (to_color(0x00, blue_mask) << blue_off) | none_alpha; + hmi->colors[1] = ((to_color(0x7f, red_mask)) << red_off) | + (to_color(0xdb, green_mask) << green_off) | + (to_color(0x3b, blue_mask) << blue_off) | none_alpha; + hmi->colors[2] = ((to_color(0x00, red_mask)) << red_off) | + (to_color(0xa4, green_mask) << green_off) | + (to_color(0xef, blue_mask) << blue_off) | none_alpha; + hmi->colors[3] = ((to_color(0xff, red_mask)) << red_off) | + (to_color(0xb8, green_mask) << green_off) | + (to_color(0x1c, blue_mask) << blue_off) | none_alpha; + + hmi->event = RT_FALSE; +} + +static void hmi_graphic_notify(rt_device_t dev) +{ + struct hmi_info *hmi = (void *)dev; + + hmi->event = RT_TRUE; +} + +static rt_bool_t hmi_input_callback(struct rt_input_handler *handler, + struct rt_input_event *ev) +{ + struct hmi_info *hmi = handler->priv; + + if (ev->type == EV_ABS) + { + if (ev->code == 0) + { + hmi->dx = (ev->value * hmi->info.width) / + (handler->idev->absinfo->maximum - handler->idev->absinfo->minimum); + } + else if (ev->code == 1) + { + hmi->dy = (ev->value * hmi->info.height) / + (handler->idev->absinfo->maximum - handler->idev->absinfo->minimum); + } + } + else if (ev->type == EV_KEY) + { + if (ev->code == BTN_LEFT) + { + if (hmi->keydown && ev->value == 0) + { + /* Swap lines color */ + hmi->line[0] ^= hmi->line[1]; + hmi->line[1] ^= hmi->line[0]; + hmi->line[0] ^= hmi->line[1]; + + hmi->keydown = RT_FALSE; + hmi->vsync = RT_FALSE; + } + else + { + hmi->keydown = RT_TRUE; + } + } + } + else if (ev->type == EV_SYN) + { + hmi->vsync = RT_FALSE; + } + + return RT_TRUE; +} + +static void hmi_loop(void *param) +{ + struct hmi_info *hmi = param; + struct rt_device_rect_info rect; + struct rt_device_graphic_ops *gops; + + /* Graphic device event */ + hmi->event_notify.notify = &hmi_graphic_notify; + hmi->event_notify.dev = (void *)hmi; + rt_device_control(hmi->gdev, RT_DEVICE_CTRL_NOTIFY_SET, &hmi->event_notify); + + /* Input device event */ + hmi->handler.idev = to_input_device(hmi->idev); + hmi->handler.identify = RT_NULL; + hmi->handler.callback = &hmi_input_callback; + hmi->handler.priv = hmi; + rt_input_add_handler(&hmi->handler); + + hmi->backend_framebuffer = RT_NULL; + hmi_reset(hmi); + + hmi->dx = hmi->info.width >> 1; + hmi->dy = hmi->info.height >> 1; + + rect.x = 0; + rect.y = 0; + + gops = rt_graphix_ops(hmi->gdev); + rt_device_control(hmi->gdev, RTGRAPHIC_CTRL_POWERON, RT_NULL); + + while (hmi_working) + { + rt_ubase_t pos; + + /* Wait graphic change */ + if (hmi->event) + { + hmi_reset(hmi); + } + + hmi->x = hmi->dx; + hmi->y = hmi->dy; + + rect.width = hmi->info.width; + rect.height = hmi->info.height; + pos = RTGRAPHIC_PIXEL_POSITION(hmi->x, hmi->y); + + for (int i = 0; i < RT_ARRAY_SIZE(hmi->colors); ++i) + { + rt_uint32_t x1, y1, x2, y2; + void *fb = hmi->backend_framebuffer ? : hmi->info.framebuffer; + + switch (i) + { + case 0: + x1 = 0; + y1 = 0; + x2 = hmi->x; + y2 = hmi->y; + break; + + case 1: + x1 = hmi->x; + y1 = 0; + x2 = hmi->info.width; + y2 = hmi->y; + break; + + case 2: + x1 = 0; + y1 = hmi->y; + x2 = hmi->x; + y2 = hmi->info.height; + break; + + case 3: + x1 = hmi->x; + y1 = hmi->y; + x2 = hmi->info.width; + y2 = hmi->info.height; + break; + } + + fb += x1 * hmi->bytes_per_pixel + y1 * hmi->info.pitch; + + for (int y = y1; y < y2; ++y) + { + void *fb_entry = fb; + + for (int x = x1; x < x2; ++x) + { + rt_memcpy(fb, &hmi->colors[i], hmi->bytes_per_pixel); + fb += hmi->bytes_per_pixel; + } + + fb = fb_entry + hmi->info.pitch; + } + } + + if (hmi->backend_framebuffer) + { + rt_memcpy(hmi->info.framebuffer, hmi->backend_framebuffer, hmi->info.smem_len); + } + + gops->draw_hline((void *)&hmi->line[0], 0, rect.width, hmi->y); + gops->draw_vline((void *)&hmi->line[1], hmi->x, 0, rect.height); + + rt_device_control(hmi->gdev, RTGRAPHIC_CTRL_RECT_UPDATE, &rect); + rt_device_control(hmi->gdev, RT_DEVICE_CTRL_CURSOR_SET_POSITION, (void *)pos); + + /* Next position */ + hmi->vsync = RT_TRUE; + rt_hw_wmb(); + + while (hmi_working && hmi->vsync) + { + rt_thread_mdelay(1); + } + } + + rt_device_control(hmi->gdev, RTGRAPHIC_CTRL_POWEROFF, RT_NULL); + + rt_memset(&hmi->event_notify, 0, sizeof(hmi->event_notify)); + rt_device_control(hmi->gdev, RT_DEVICE_CTRL_NOTIFY_SET, &hmi->event_notify); + + rt_input_del_handler(&hmi->handler); + + rt_device_close(hmi->gdev); + rt_device_close(hmi->idev); + + if (hmi->backend_framebuffer) + { + rt_free(hmi->backend_framebuffer); + } + rt_free(hmi); + + rt_thread_delete(rt_thread_self()); +} + +rt_err_t hmi_start(const char *gdev, const char *idev) +{ + rt_err_t err; + struct hmi_info *hmi; + struct rt_thread *loop; + + if (hmi_working) + { + rt_kprintf("HMI is running\n"); + return -RT_EBUSY; + } + + hmi = rt_malloc(sizeof(*hmi)); + + if (!hmi) + { + return -RT_ENOMEM; + } + + hmi->gdev = rt_device_find(gdev); + hmi->idev = rt_device_find(idev); + + if (!hmi->gdev || !hmi->idev) + { + rt_free(hmi); + return -RT_EINVAL; + } + + if (!rt_bitmap_test_bit(to_input_device(hmi->idev)->cap, EV_ABS)) + { + rt_kprintf("%s is not a ABS input\n", idev); + rt_free(hmi); + return -RT_EINVAL; + } + + if ((err = rt_device_open(hmi->gdev, 0))) + { + rt_free(hmi); + return err; + } + + if ((err = rt_device_open(hmi->idev, 0))) + { + rt_device_close(hmi->gdev); + rt_free(hmi); + return err; + } + + loop = rt_thread_create("HMI", hmi_loop, hmi, + DM_THREAD_STACK_SIZE, + RT_THREAD_PRIORITY_MAX / 3, + rt_tick_from_millisecond(RT_GRAPHIC_UPDATE_MS)); + + if (!loop) + { + rt_device_close(hmi->gdev); + rt_device_close(hmi->idev); + rt_free(hmi); + return -RT_ENOMEM; + } + + hmi_working = RT_TRUE; + rt_thread_startup(loop); + + return RT_EOK; +} + +rt_err_t hmi_stop(void) +{ + hmi_working = RT_FALSE; + return RT_EOK; +} + +#ifdef RT_USING_FINSH +static int _hmi_start(int argc, char**argv) +{ + const char *gdev = "fb0", *idev = "input0"; + + if (argc == 3) + { + gdev = argv[1]; + idev = argv[2]; + } + + return (int)hmi_start(gdev, idev); +} +MSH_CMD_EXPORT_ALIAS(_hmi_start, hmi_start, e.g: hmi_start("fb0", "input0")); + +static int _hmi_stop(void) +{ + return (int)hmi_stop(); +} +MSH_CMD_EXPORT_ALIAS(_hmi_stop, hmi_stop, e.g: hmi_exit()); +#endif /* RT_USING_FINSH */ +#endif /* RT_USING_GRAPHIC && RT_USING_INPUT */ |
