阶段1: 将 dzbj 项目的 LVGL 8.3.11 LCD 显示集成到 AI小智 主项目, 开机显示 ScreenHome 界面,同时优化配网模式下的内存使用, 确保 WiFi+BLE+LVGL 三者共存运行。 ## 新增功能 ### dzbj 显示模块集成 - 新增 main/dzbj/ 目录,移植 LCD 驱动(ST77916 QSPI)、触摸驱动(CST816S)、 LVGL 初始化和 SquareLine Studio UI 界面 - I2C 总线共享:dzbj 触摸控制器复用主项目的 I2C_NUM_1 总线 - GPIO 冲突解决:LED(GPIO21)、Touch1(GPIO1)、Touch4(GPIO7) 改为 NC, 电池 ADC 从 GPIO6 改为 GPIO3 - 添加 LVGL、esp_lcd_st77916、esp_lcd_touch_cst816s 等组件依赖 - managed_components 纳入版本管理 ### 配网模式轻量化启动 - BoxAudioCodec: 新增 output_only 模式,仅创建 I2S TX 通道(省 ~13KB DMA) 跳过 ES7210 ADC 初始化(省 ~2-4KB) - AudioCodec: 新增 StartOutputOnly() 方法,仅启用扬声器输出 - Application: 配网模式跳过 Opus 编码器、输入重采样器、协议初始化、 天气位置检测等网络业务 - 板级构造函数: 配网模式跳过电池检测、IMU传感器、PowerSaveTimer ### WifiBoard 配网流程修复 - NeedsProvisioning() 静态方法: 读取 NVS force_ap 和 SSID 列表, 用于提前判断配网模式 - force_ap 竞态修复: 构造函数不再清零 force_ap,改在 StartNetwork() 清零, 确保 NeedsProvisioning() 能正确读到 force_ap=1 - Application 缓存 provisioning_mode_ 成员变量,避免重复读 NVS ### BLE 配网重启修复 - 配网成功后用 esp_timer 延迟重启替代 xTaskCreate, 避免内存紧张时任务创建失败导致设备不重启 - 注释掉 WiFi 连接成功后的 MAC 地址发送步骤 ### sdkconfig 内存优化 - BT_ALLOCATION_FROM_SPIRAM_FIRST=y (BLE 动态分配优先 PSRAM) - SPIRAM_MALLOC_RESERVE_INTERNAL=32768 - NVS_ALLOCATE_CACHE_IN_SPIRAM=y - WiFi 静态缓冲区数量优化 (RX=10, TX=8) Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
8.6 KiB
Events
Events are triggered in LVGL when something happens which might be interesting to the user, e.g. when an object
- is clicked
- is scrolled
- has its value changed
- is redrawn, etc.
Add events to the object
The user can assign callback functions to an object to see its events. In practice, it looks like this:
lv_obj_t * btn = lv_btn_create(lv_scr_act());
lv_obj_add_event_cb(btn, my_event_cb, LV_EVENT_CLICKED, NULL); /*Assign an event callback*/
...
static void my_event_cb(lv_event_t * event)
{
printf("Clicked\n");
}
In the example LV_EVENT_CLICKED means that only the click event will call my_event_cb. See the list of event codes for all the options.
LV_EVENT_ALL can be used to receive all events.
The last parameter of lv_obj_add_event_cb is a pointer to any custom data that will be available in the event. It will be described later in more detail.
More events can be added to an object, like this:
lv_obj_add_event_cb(obj, my_event_cb_1, LV_EVENT_CLICKED, NULL);
lv_obj_add_event_cb(obj, my_event_cb_2, LV_EVENT_PRESSED, NULL);
lv_obj_add_event_cb(obj, my_event_cb_3, LV_EVENT_ALL, NULL); /*No filtering, receive all events*/
Even the same event callback can be used on an object with different user_data. For example:
lv_obj_add_event_cb(obj, increment_on_click, LV_EVENT_CLICKED, &num1);
lv_obj_add_event_cb(obj, increment_on_click, LV_EVENT_CLICKED, &num2);
The events will be called in the order as they were added.
Other objects can use the same event callback.
Remove event(s) from an object
Events can be removed from an object with the lv_obj_remove_event_cb(obj, event_cb) function or lv_obj_remove_event_dsc(obj, event_dsc). event_dsc is a pointer returned by lv_obj_add_event_cb.
Event codes
The event codes can be grouped into these categories:
- Input device events
- Drawing events
- Other events
- Special events
- Custom events
All objects (such as Buttons/Labels/Sliders etc.) regardless their type receive the Input device, Drawing and Other events.
However, the Special events are specific to a particular widget type. See the widgets' documentation to learn when they are sent,
Custom events are added by the user and are never sent by LVGL.
The following event codes exist:
Input device events
LV_EVENT_PRESSEDAn object has been pressedLV_EVENT_PRESSINGAn object is being pressed (called continuously while pressing)LV_EVENT_PRESS_LOSTAn object is still being pressed but slid cursor/finger off of the objectLV_EVENT_SHORT_CLICKEDAn object was pressed for a short period of time, then released. Not called if scrolled.LV_EVENT_LONG_PRESSEDAn object has been pressed for at least thelong_press_timespecified in the input device driver. Not called if scrolled.LV_EVENT_LONG_PRESSED_REPEATCalled afterlong_press_timein everylong_press_repeat_timems. Not called if scrolled.LV_EVENT_CLICKEDCalled on release if an object did not scroll (regardless of long press)LV_EVENT_RELEASEDCalled in every case when an object has been releasedLV_EVENT_SCROLL_BEGINScrolling begins. The event parameter isNULLor anlv_anim_t *with a scroll animation descriptor that can be modified if required.LV_EVENT_SCROLL_ENDScrolling ends.LV_EVENT_SCROLLAn object was scrolledLV_EVENT_GESTUREA gesture is detected. Get the gesture withlv_indev_get_gesture_dir(lv_indev_get_act());LV_EVENT_KEYA key is sent to an object. Get the key withlv_indev_get_key(lv_indev_get_act());LV_EVENT_FOCUSEDAn object is focusedLV_EVENT_DEFOCUSEDAn object is unfocusedLV_EVENT_LEAVEAn object is unfocused but still selectedLV_EVENT_HIT_TESTPerform advanced hit-testing. Uselv_hit_test_info_t * a = lv_event_get_hit_test_info(e)and check ifa->pointcan click the object or not. If not seta->res = false
Drawing events
LV_EVENT_COVER_CHECKCheck if an object fully covers an area. The event parameter islv_cover_check_info_t *.LV_EVENT_REFR_EXT_DRAW_SIZEGet the required extra draw area around an object (e.g. for a shadow). The event parameter islv_coord_t *to store the size. Only overwrite it with a larger value.LV_EVENT_DRAW_MAIN_BEGINStarting the main drawing phase.LV_EVENT_DRAW_MAINPerform the main drawingLV_EVENT_DRAW_MAIN_ENDFinishing the main drawing phaseLV_EVENT_DRAW_POST_BEGINStarting the post draw phase (when all children are drawn)LV_EVENT_DRAW_POSTPerform the post draw phase (when all children are drawn)LV_EVENT_DRAW_POST_ENDFinishing the post draw phase (when all children are drawn)LV_EVENT_DRAW_PART_BEGINStarting to draw a part. The event parameter islv_obj_draw_dsc_t *. Learn more here.LV_EVENT_DRAW_PART_ENDFinishing to draw a part. The event parameter islv_obj_draw_dsc_t *. Learn more here.
In LV_EVENT_DRAW_... events it's not allowed to adjust the widgets' properties. E.g. you can not call lv_obj_set_width().
In other words only get functions can be called.
Other events
LV_EVENT_DELETEObject is being deletedLV_EVENT_CHILD_CHANGEDChild was removed/addedLV_EVENT_CHILD_CREATEDChild was created, always bubbles up to all parentsLV_EVENT_CHILD_DELETEDChild was deleted, always bubbles up to all parentsLV_EVENT_SIZE_CHANGEDObject coordinates/size have changedLV_EVENT_STYLE_CHANGEDObject's style has changedLV_EVENT_BASE_DIR_CHANGEDThe base dir has changedLV_EVENT_GET_SELF_SIZEGet the internal size of a widgetLV_EVENT_SCREEN_UNLOAD_STARTA screen unload started, fired immediately when lv_scr_load/lv_scr_load_anim is calledLV_EVENT_SCREEN_LOAD_STARTA screen load started, fired when the screen change delay is expiredLV_EVENT_SCREEN_LOADEDA screen was loaded, called when all animations are finishedLV_EVENT_SCREEN_UNLOADEDA screen was unloaded, called when all animations are finished
Special events
LV_EVENT_VALUE_CHANGEDThe object's value has changed (i.e. slider moved)LV_EVENT_INSERTText is being inserted into the object. The event data ischar *being inserted.LV_EVENT_REFRESHNotify the object to refresh something on it (for the user)LV_EVENT_READYA process has finishedLV_EVENT_CANCELA process has been canceled
Custom events
Any custom event codes can be registered by uint32_t MY_EVENT_1 = lv_event_register_id();
They can be sent to any object with lv_event_send(obj, MY_EVENT_1, &some_data)
Sending events
To manually send events to an object, use lv_event_send(obj, <EVENT_CODE> &some_data).
For example, this can be used to manually close a message box by simulating a button press (although there are simpler ways to do this):
/*Simulate the press of the first button (indexes start from zero)*/
uint32_t btn_id = 0;
lv_event_send(mbox, LV_EVENT_VALUE_CHANGED, &btn_id);
Refresh event
LV_EVENT_REFRESH is a special event because it's designed to let the user notify an object to refresh itself. Some examples:
- notify a label to refresh its text according to one or more variables (e.g. current time)
- refresh a label when the language changes
- enable a button if some conditions are met (e.g. the correct PIN is entered)
- add/remove styles to/from an object if a limit is exceeded, etc
Fields of lv_event_t
lv_event_t is the only parameter passed to the event callback and it contains all data about the event. The following values can be gotten from it:
lv_event_get_code(e)get the event codelv_event_get_current_target(e)get the object to which an event was sent. I.e. the object whose event handler is being called.lv_event_get_target(e)get the object that originally triggered the event (different fromlv_event_get_targetif event bubbling is enabled)lv_event_get_user_data(e)get the pointer passed as the last parameter oflv_obj_add_event_cb.lv_event_get_param(e)get the parameter passed as the last parameter oflv_event_send
Event bubbling
If lv_obj_add_flag(obj, LV_OBJ_FLAG_EVENT_BUBBLE) is enabled all events will be sent to an object's parent too. If the parent also has LV_OBJ_FLAG_EVENT_BUBBLE enabled the event will be sent to its parent and so on.
The target parameter of the event is always the current target object, not the original object. To get the original target call lv_event_get_original_target(e) in the event handler.
Examples
.. include:: ../../examples/event/index.rst