Skip to content

Commit 3eae020

Browse files
Finomnisnashif
authored andcommitted
samples: modules: lvgl: add screen_transparency sample
Adds a sample that renders the simple `hello world` LVGL demo with a transparent background, to demonstrate ARGB8888 framebuffer capabilities. Signed-off-by: Martin Stumpf <[email protected]>
1 parent ded2e0b commit 3eae020

File tree

6 files changed

+143
-0
lines changed

6 files changed

+143
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
3+
cmake_minimum_required(VERSION 3.20.0)
4+
5+
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
6+
project(screen_transparency)
7+
8+
FILE(GLOB app_sources src/*.c)
9+
target_sources(app PRIVATE ${app_sources})
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
# Copyright (c) 2024 Martin Stumpf <[email protected]>
2+
# SPDX-License-Identifier: Apache-2.0
3+
4+
source "Kconfig.zephyr"
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
.. zephyr:code-sample:: lvgl-screen-transparency
2+
:name: LVGL screen transparency
3+
:relevant-api: display_interface
4+
5+
Rendering to screens with transparency support using LVGL.
6+
7+
Overview
8+
********
9+
10+
A sample application that demonstrates how to use LVGL to render to
11+
screens that support transparency, like OSD overlays.
12+
13+
Requirements
14+
************
15+
16+
* A board with a display that supports ``ARGB8888`` color.
17+
18+
.. _lvgl_screen_transparency_building_and_running:
19+
20+
Building and Running
21+
********************
22+
23+
The demo can be built as follows:
24+
25+
.. zephyr-app-commands::
26+
:zephyr-app: samples/modules/lvgl/screen_transparency
27+
:host-os: unix
28+
:board: native_sim
29+
:goals: run
30+
:compact:
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CONFIG_MAIN_STACK_SIZE=4096
2+
CONFIG_LOG=y
3+
4+
CONFIG_LVGL=y
5+
CONFIG_LV_Z_MEM_POOL_SIZE=16384
6+
CONFIG_LV_COLOR_DEPTH_32=y
7+
CONFIG_LV_COLOR_SCREEN_TRANSP=y
8+
9+
CONFIG_DISPLAY=y
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
sample:
2+
description: Demonstrating Screen Transparency using LVGL
3+
name: LVGL screen transparency
4+
tests:
5+
sample.modules.lvgl.screen_transparency:
6+
filter: dt_chosen_enabled("zephyr,display")
7+
min_flash: 250
8+
min_ram: 32
9+
harness: none
10+
tags:
11+
- samples
12+
- display
13+
- gui
14+
- lvgl
15+
modules:
16+
- lvgl
17+
integration_platforms:
18+
- native_sim/native/64
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
* Copyright (c) 2024 Martin Stumpf <[email protected]>
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include <zephyr/device.h>
8+
#include <zephyr/devicetree.h>
9+
#include <zephyr/drivers/display.h>
10+
11+
#include <lvgl.h>
12+
#include <stdio.h>
13+
#include <string.h>
14+
#include <zephyr/kernel.h>
15+
16+
#include <zephyr/logging/log.h>
17+
LOG_MODULE_REGISTER(app, CONFIG_LOG_DEFAULT_LEVEL);
18+
19+
static void initialize_gui(void)
20+
{
21+
lv_obj_t *label;
22+
23+
/* Configure screen and background for transparency */
24+
lv_obj_set_style_bg_opa(lv_scr_act(), LV_OPA_TRANSP, LV_PART_MAIN);
25+
lv_disp_set_bg_opa(NULL, LV_OPA_TRANSP);
26+
lv_obj_set_style_bg_color(lv_scr_act(), lv_color_hex(0x000000), LV_PART_MAIN);
27+
28+
/* Create a label, set its text and align it to the center */
29+
label = lv_label_create(lv_scr_act());
30+
lv_label_set_text(label, "Hello, world!");
31+
lv_obj_set_style_text_color(lv_scr_act(), lv_color_hex(0xff00ff), LV_PART_MAIN);
32+
lv_obj_align(label, LV_ALIGN_CENTER, 0, 0);
33+
}
34+
35+
int main(void)
36+
{
37+
int err;
38+
const struct device *display_dev;
39+
struct display_capabilities display_caps;
40+
41+
display_dev = DEVICE_DT_GET(DT_CHOSEN(zephyr_display));
42+
if (!device_is_ready(display_dev)) {
43+
LOG_ERR("Device not ready, aborting test");
44+
return -ENODEV;
45+
}
46+
47+
display_get_capabilities(display_dev, &display_caps);
48+
if (!(display_caps.supported_pixel_formats | PIXEL_FORMAT_ARGB_8888)) {
49+
LOG_ERR("Display does not support ARGB8888 mode");
50+
return -ENOTSUP;
51+
}
52+
53+
if (PIXEL_FORMAT_ARGB_8888 != display_caps.current_pixel_format) {
54+
err = display_set_pixel_format(display_dev, PIXEL_FORMAT_ARGB_8888);
55+
if (err != 0) {
56+
LOG_ERR("Failed to set ARGB8888 pixel format");
57+
return err;
58+
}
59+
}
60+
61+
initialize_gui();
62+
63+
lv_task_handler();
64+
display_blanking_off(display_dev);
65+
66+
while (1) {
67+
uint32_t sleep_ms = lv_task_handler();
68+
69+
k_msleep(MIN(sleep_ms, INT32_MAX));
70+
}
71+
72+
return 0;
73+
}

0 commit comments

Comments
 (0)