当前位置:网站首页>[lvgl (4)] event and event bubble of the object
[lvgl (4)] event and event bubble of the object
2022-07-24 06:46:00 【Like warm know cold】
LVGL Official documents :Welcome to the documentation of LVGL! — LVGL documentation
https://docs.lvgl.io/master/index.html
event (events)
Click on 、 rolling 、 Array changes 、 Repaint ……
Event type
- Enter the device event (Input device events)
- Graphic Events (Drawing events)
- Other events (Special events)
- Special events (Other events)
- Custom events (Custom events)
Look at the documents ~ Events — LVGL documentation
Add event
lv_obj_add_event_cb(obj, event_cb, event_code, user_data);
Parameters : object , Handle (cb:callbackfunction, Callback function ), Event type , User data
Send events
lv_event_send(obj, event_cb, event_code, user_data);
Delete event
lv_obj_remove_event_cb(obj, event_cb);
lv_obj_remove_event_dsc(obj, event_dsc); //event_dsc yes lv_obj_add_event_cb Pointer returned
The simplest event :
static void function()
{
printf("test\n");
}
void mode(void)
{
lv_obj_t * obj = lv_obj_create(lv_scr_act());
lv_obj_add_event_cb(obj,function,LV_EVENT_CLICKED,NULL); // Press the key to click , Call after release
}The called function can pass in parameters
function(lv_event_t * e)typedef struct _lv_event_t {
struct _lv_obj_t * target; // Object that triggered the event
struct _lv_obj_t * current_target; // The object whose parent triggers the event
lv_event_code_t code; // Event code
void * user_data; // User data
void * param;
struct _lv_event_t * prev;
uint8_t deleted : 1;
uint8_t stop_processing : 1;
uint8_t stop_bubbling : 1;
} lv_event_t;as follows , Write a lighting interface
int count = 0;
static void function1(lv_event_t * e)
{
lv_obj_t * obj = lv_event_get_target(e); // Get the part that triggers the event ( object )
lv_event_code_t code = lv_event_get_code(e); // Get the current part ( object ) Triggered event code
lv_obj_t * label = lv_event_get_user_data(e); // Get the user data passed when adding events
if(count == 0 && code == LV_EVENT_PRESSED)
{
lv_label_set_text(label, "OPEN");
lv_obj_set_style_bg_color(obj, lv_color_hex(0xc43e1c), 0); // Through local style ( Private style ) Set background color
printf("OPEN\n");
count ++;
}
else if(count == 1 && code == LV_EVENT_PRESSED)
{
lv_label_set_text(label, "CLOSE");
lv_obj_set_style_bg_color(obj, lv_color_hex(0xffffff), 0); // Through local style ( Private style ) Set background color
printf("CLOSE\n");
count ++;
}
if(count==2)
count = 0;
}
void lv_100ask_demo_course_2_2_6(void)
{
/* Create basic components ( object ) */
lv_obj_t * obj = lv_obj_create(lv_scr_act());
lv_obj_center(obj); // Put it in the center
/* establish label parts ( object ) */
lv_obj_t * label = lv_label_create(lv_scr_act());
lv_label_set_text(label, "CLOSE"); // Set up label Displayed text
lv_obj_center(label); // Align the object with the center of its parent , The parent object here is the screen :lv_scr_act()
// by obj1 Add event callback function , All event types can trigger the callback function
lv_obj_add_event_cb(obj, function1, LV_EVENT_ALL, label);// What's coming in is label, Can be set up label
}The phenomenon :
Click to turn on the light :
Click to turn off the light :
![]()
Event Bubbling
If the object is enabled lv_obj_add_flag(obj, LV_OBJ_FLAG_EVENT_BUBBLE), All events of this object will be sent to the parent of this object . If The parent also enables LV_OBJ_FLAG_EVENT_BUBBLE, Then the event continues to be sent to his parent , And so on .
- lv_event_get_target(e); Get the current object that triggers the event .
- lv_event_get_current_target(e); Get the parent object of the event bubble .
static void my_event_cb(lv_event_t * e)
{
lv_obj_t * obj = lv_event_get_target(e); // Get the object that triggers the event
lv_obj_t * parent = lv_event_get_current_target(e); // Get the parent object of the trigger event object ( Only when events bubble )
lv_event_code_t code = lv_event_get_code(e); // Get the event code triggered by the current part
lv_obj_t * label = lv_event_get_user_data(e); // Get the user data passed when adding events
switch(code){
case LV_EVENT_PRESSED: // Keep pressing
lv_label_set_text(label, "LV_EVENT_PRESSED");
/* Parent */
lv_obj_set_style_bg_color(parent, lv_color_hex(0xc43e1c), 0); // Through local style ( Private style ) Set background color
/* Trigger event itself object */
lv_obj_set_style_bg_color(obj, lv_color_hex(0xc43e1c), 0); // Through local style ( Private style ) Set background color
printf("LV_EVENT_PRESSED\n");
break;
case LV_EVENT_CLICKED: // Click when releasing
lv_label_set_text(label, "LV_EVENT_CLICKED");
lv_obj_remove_local_style_prop(parent, LV_STYLE_BG_COLOR, 0); // Delete through local style ( Private style ) Set the background color
lv_obj_remove_local_style_prop(obj, LV_STYLE_BG_COLOR, 0); // Delete through local style ( Private style ) Set the background color
printf("LV_EVENT_CLICKED\n");
break;
default:
//printf("NONE\n");
break;
}
}
void lv_100ask_demo_course_2_2_6(void)
{
/* Create a base object obj1 */
lv_obj_t * obj1 = lv_obj_create(lv_scr_act());
lv_obj_set_size(obj1, 450, 250);
lv_obj_center(obj1); // Align the object with the center of its parent , The parent object here is the screen :lv_scr_act()
/* With obj1 Create a base object obj2 */
lv_obj_t * obj2 = lv_obj_create(obj1);
lv_obj_set_size(obj2, 400, 200);
lv_obj_center(obj2); // Align the object with the center of its parent , The parent object here is the screen :obj1
lv_obj_add_flag(obj2, LV_OBJ_FLAG_EVENT_BUBBLE); // Enable event bubbling , Propagate all received events to the parent
/* With obj2 Create a base object obj3 */
lv_obj_t * obj3 = lv_obj_create(obj2);
lv_obj_set_size(obj3, 350, 150);
lv_obj_center(obj3); // Align the object with the center of its parent , The parent object here is the screen :obj2
lv_obj_add_flag(obj3, LV_OBJ_FLAG_EVENT_BUBBLE); // Enable event bubbling , Propagate all received events to the parent
/* With obj3 Create a base object obj4 */
lv_obj_t * obj4 = lv_obj_create(obj3);
lv_obj_set_size(obj4, 300, 100);
lv_obj_center(obj4); // Align the object with the center of its parent , The parent object here is the screen :obj3
lv_obj_add_flag(obj4, LV_OBJ_FLAG_EVENT_BUBBLE); // Enable event bubbling , Propagate all received events to the parent
/* Take screen as parent , Create a label parts ( object ) */
lv_obj_t * label = lv_label_create(lv_scr_act());
lv_label_set_text(label, "test"); // Set up label Displayed text
lv_obj_align_to(label, obj1, LV_ALIGN_OUT_TOP_MID, 0, 0); // take label be relative to obj1 alignment
// Will give obj1 Add event callback function , All event types can trigger the callback function
lv_obj_add_event_cb(obj1, my_event_cb, LV_EVENT_ALL, label);
}Ha ha ha ha ~ It's really interesting ~
The phenomenon :
Click the middle object :

Click on the third layer object :

Click on the second level object :

Outermost object :

Event description
An event callback function can be used for multiple objects
After we create an event handler function, it can be used by different objects .
An object can use multiple event callback functions
The object we create can be bound to multiple events , For example, an event deals with click type events , An event handles press type events, and so on .
other
If the incoming user data is different , An object can be bound to the same event callback function multiple times , Events will be called in the order they are added . for example :
- lv_obj_add_event_cb(obj, my_clicked_event_cb, LV_EVENT_CLICKED, &num1);
- lv_obj_add_event_cb(obj, my_clicked_event_cb, LV_EVENT_CLICKED, &num2);
OK, In this way, we can realize the simple function call function , At least until now, it can be used as a light switch on the development board .
边栏推荐
- 【LVGL(重要)】样式属性API函数及其参数
- RAID的配置实验
- It's not too much to fight a landlord in idea!
- 【LVGL(2)】LVGL入门,在CodeBlock上进行模拟以及移植STM32
- Experiment: creation, expansion, and deletion of LVM logical volumes
- SSH Remote Access and control
- I have seven schemes to realize web real-time message push, seven!
- 磁盘管理和文件系统
- 【LVGL布局】柔性布局
- Common commands and package management of go language
猜你喜欢

Today, let's talk about the underlying architecture design of MySQL database. How much do you know?

Several common problems of SQL server synchronization database without public IP across network segments

Take you to understand the inventory deduction principle of MySQL database

SSH Remote Access and control

Jenkins CI CD

Transition effect

实验:LVM逻辑卷的建立、扩容、与删除

【ESP8266点焊机】基于 ESP8266 for Arduino

I have seven schemes to realize web real-time message push, seven!

进程和计划任务管理
随机推荐
Solution: exit status 1 and exit status 145 appear when the console uses NVM to control the node version
LM393 电压比较器及其典型电路介绍
FTP服务与实验
Restful API introduction
Sed command
Account and authority management
NFS共享服务及实验
Browser local storage
深入了解MySQL 两把锁啥时候用(表锁,行锁)
Summary browser object
DHCP原理与配置
Multiple types of functions
nodejs开启多进程并实现进程间通信
openssl版本升级
Speed pointer in JS linked list
Disk management and file system
Take you to understand the inventory deduction principle of MySQL database
Rsync (I): basic commands and usage
【媒体控制器】开源项目学习笔记(基于Arduino Micro开发板)
【音频解码芯片】VS1503音频解码芯片的应用
