当前位置:网站首页>ESP32 LVGL8. 1 - event (event 17)

ESP32 LVGL8. 1 - event (event 17)

2022-04-23 18:23:00 Please call me Xiao Peng

Tips : This blog serves as a learning note , If there are mistakes, I hope to correct them

One 、event brief introduction

1.1 summary Overview

   stay LVGL in , When something happens that users may be interested in , The event will be triggered , for example , An object :
Click on 、 rolling 、 Its value has changed 、 Repaint , wait .

1.2 Add events to objects Add events to the object

   The user can assign a callback function to an object to view its events . The added examples are as follows :

lv_obj_t * btn = lv_btn_create(lv_scr_act());					// establish btn object 
lv_obj_add_event_cb(btn, my_event_cb, LV_EVENT_CLICKED, NULL); 	// Add event callback 
/*callback*/
static void my_event_cb(lv_event_t * event)						// Event callback function 
{
    
	printf("Clicked\n");
}

   In the example ,LV_EVENT_CLICKED Means that only the click event will call my_event_cb.LV_EVENT_ALL Can be used to receive all events .
  lv_obj_add_event_cb The last argument to is a pointer to any custom data available in the event .
   In addition, we can add more events to an object , for example :

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);

   Even the same event callback can be used with different user_data The object of , 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);

   Events will be called in the order in which they were added , More objects can use the same event callback .

1.3 Remove event from object Remove event(s) from an object

   have access to lv_obj_remove_event_cb(obj, event_cb) Function or lv_obj_remove_event_dsc(obj, event_dsc) Delete event from object .Event_dsc By lv_obj_add_event_cb Pointer returned .

1.4 Code event Events code

   Before code event btn The article goes beyond the outline to say a part , Here is a description to deepen the impression . Classification of events : Enter the device event 、 Draw the event 、 Other events 、 Special events 、 Custom events . All objects ( Button like / label / Slider, etc ), Whatever its type , Both receive input devices , Drawings and other events . however , Special events are specific to a particular widget type . Check the documentation of the widgets to see when they are sent , Custom events are added by the user , So these will not be LVGL send out .

 Event codes can be divided into the following categories :
•  Enter the event of the device 
•  Painting Events 
•  Other events 
•  Special events 
•  Custom events 

LV_EVENT_ALL = 0,				/**< Object all events */
/** Enter the device event */
LV_EVENT_PRESSED, 				/**< Object has been pressed */
LV_EVENT_PRESSING, 			/**< Object is being pressed ( Continuously call... When pressed )*/
LV_EVENT_PRESS_LOST, 			/**< The object is still being pressed , But the cursor / Slide your fingers away from the object */
LV_EVENT_SHORT_CLICKED, 		/**< The object is pressed for a short time , Then release . Do not call , If you scroll */
LV_EVENT_LONG_PRESSED, 		/**< The object is at least pressed ' long_press_time '. Do not call , If you scroll */
LV_EVENT_LONG_PRESSED_REPEAT, 	/**< At every `long_press_repeat_time` in `long_press_time` Then call ms. If you scroll, do not call .*/
LV_EVENT_CLICKED, 				/**< Called on release if there is no scrolling ( No matter long press )*/
LV_EVENT_RELEASED, 			/**< Called every time an object is released */
LV_EVENT_SCROLL_BEGIN, 		/**< Scroll start */
LV_EVENT_SCROLL_END, 			/**< Roll over */
LV_EVENT_SCROLL , 				/**<  rolling */
LV_EVENT_GESTURE, 				/**< Gesture detected . Use “lv_indev_get_gesture_dir(lv_indev_get_act())” Get gestures */
LV_EVENT_KEY, 					/**< One key Sent to object . use ' lv_indev_get_key(lv_indev_get_act())); ' Get key */
LV_EVENT_FOCUSED, 				/**< The object is focused */
lv_event_DEFOCUSED,			/**< Object defocus */
LV_EVENT_LEAVE, 				/**< The object is defocused but still selected */
LV_EVENT_HIT_TEST, 			/**< Perform advanced hit test */

* / / * * Draw the event */
LV_EVENT_COVER_CHECK, 			/**< Check whether the object completely covers an area . The event parameter is ' lv_cover_check_info_t '*/
lv_event_REFR_ext_draw_size, 	/**< Get the extra drawing area needed around the object ( For example, shadow ).event Parameter is ' lv_coord_t * ' To store size */
LV_EVENT_DRAW_MAIN_BEGIN, 		/**< Start the main drawing phase */
LV_EVENT_DRAW_MAIN, 			/**< Execute the main drawing */
LV_EVENT_DRAW_MAIN_END, 		/**< Complete the main drawing phase */
LV_EVENT_DRAW_POST_BEGIN, 		/**< Start post Drawing phase ( When all child elements are drawn )*/
LV_EVENT_DRAW_POST, 			/**< perform post Drawing phase ( When all child elements are drawn )*/
LV_EVENT_DRAW_POST_END, 		/**< complete post Drawing phase ( When all child elements are drawn )*/
LV_EVENT_DRAW_PART_BEGIN, 		/**< Start drawing parts . The event parameter is ' lv_obj_draw_dsc_t * '.*/
LV_EVENT_DRAW_PART_END, 		/**< Draw part end . The event parameter is ' lv_obj_draw_dsc_t * '.*/

* / / * * Special events */
LV_EVENT_VALUE_CHANGED, 		/**< The value of the object has changed ( That is, the slider moves )*/
LV_EVENT_INSERT, 				/**< Insert text into the object . The event data is ' char * ' Inserting */
LV_EVENT_REFRESH, 				/**< Notify the object to refresh something on it ( For the user )*/
LV_EVENT_READY, 				/**< The process is complete */
LV_EVENT_CANCEL, 				/**< The process has been canceled */

* / / * * Other events */
LV_EVENT_DELETE, 				/**< Object is being deleted */
LV_EVENT_CHILD_CHANGED, 		/**< Child was removed/added*/
LV_EVENT_SIZE_CHANGED, 		/**< Object coordinates / Size changed */
LV_EVENT_STYLE_CHANGED, 		/**< The style of the object has changed */
LV_EVENT_LAYOUT_CHANGED, 		/**< The position of child nodes changes due to layout recalculation */
LV_EVENT_GET_SELF_SIZE, 		/**< Get the internal size of the widget */
_LV_EVENT_LAST 					/** Default number of events */

1.5 User events Custom events

  MY_EVENT_1 = lv_event_register_id() You can register any custom event code ; And through lv_event_send(obj, MY_EVENT_1, &some_data) Send to any object .

1.6 Send events Sending events

   Manually send events to an object , Use lv_event_send(obj, <EVENT_CODE> &some_data).
for example , It can be used to manually close a message box , By simulating a button press ( Although there are simpler ways ):

uint32_t btn_id = 0;
lv_event_send(mbox, LV_EVENT_VALUE_CHANGED, &btn_id);

1.7 Refresh Events Refresh event

  LV_EVENT_REFRESH It's a special event , Because it is designed for users to notify objects to refresh themselves . Some examples :
1、 Based on one or more variables ( For example, the current time ) The notification label refreshes its text
2、 Refresh tags when language changes
3、 If certain conditions are met ( For example, enter the correct password ), Enable a button
4、 If the limit is exceeded , You can add or remove styles from objects , wait

1.8 Structure field in event callback Fields of lv_event_t

  lv_event_t Is the only parameter passed to the event callback , It contains all the data about the event .

typedef struct _lv_event_t {
    
    struct _lv_obj_t * target;			// Gets the object that the event was originally aimed at . Even if the event is bubbling , It's the same .
    struct _lv_obj_t * current_target;	// Get the current target of the event . It is the object to which the event handler is called .
    lv_event_code_t code;				// Get the event code of the event 
    void * user_data;					// Gets the value passed when registering an event on an object user_data
    void * param;						// When an event is sent , Get parameter passing 
    struct _lv_event_t * prev;			
    uint8_t deleted : 1;				
} lv_event_t;

  Lv_event_t Is the only parameter passed to the event callback , It contains all the data about the event . The following values can be obtained from it :

lv_event_get_code(e)			// Get event code 
lv_event_get_current_target(e)	// Gets the object that sent the event . That is, the object event handler is being called .
lv_event_get_target(e)			// Gets the object that originally triggered the event ( And Lv_event_get_target( If event bubbling is enabled )
lv_event_get_user_data(e)		// Get as lv_obj_add_event_cb The pointer passed by the last parameter of .
lv_event_get_param(e)			// Gets the passed parameter as lv_event_send Last parameter of 

1.9 Bubbling event Event bubbling

   If lv_obj_add_flag(obj, LV_OBJ_FLAG_EVENT_BUBBLE) All events will also be sent to the parent object of the object . If the parent process is also enabled LV_OBJ_FLAG_EVENT_BUBBLE, Then the event will also be sent to its parent process , And so on . The target parameter of the event is always the current target object , Not the original object . To get the original target , Please call middle note in event handler. lv_event_get_original_target(e).

Two 、 event API

   The event API

lv_res_t lv_event_send(lv_obj_t * obj, lv_event_code_t event_code, void * param)	// Send an event to the object 
lv_res_t lv_obj_event_base(const lv_obj_class_t * class_p, lv_event_t * e)			// Used internally by widgets , Call the event handler of the ancestor widget type 
lv_obj_t * lv_event_get_target(lv_event_t * e)										// Gets the object that the event was originally aimed at . Even if the event is bubbling , It's the same .
lv_obj_t * lv_event_get_current_target(lv_event_t * e)								// Get the current target of the event . It is the object to which the event handler is called .
lv_event_code_t lv_event_get_code(lv_event_t * e)									// Get the event code of the event 
void * lv_event_get_param(lv_event_t * e)											// When an event is sent , Get parameter passing 
void * lv_event_get_user_data(lv_event_t * e)										// Gets the value passed when registering an event on an object user_data
uint32_t lv_event_register_id(void)													// Register a new custom event ID.
void _lv_event_mark_deleted(lv_obj_t * obj)											// Nested events can be called , One of them may belong to the object being deleted .
struct _lv_event_dsc_t * lv_obj_add_event_cb(lv_obj_t * obj, lv_event_cb_t event_cb, lv_event_code_t filter, void * user_data)	// Add an event handler to the object .
bool lv_obj_remove_event_dsc(lv_obj_t * obj, struct _lv_event_dsc_t * event_dsc)	// Remove an object's event handler .
lv_indev_t * lv_event_get_indev(lv_event_t * e)										// Get passed as a parameter to indev Input device for related events .
lv_obj_draw_part_dsc_t * lv_event_get_draw_part_dsc(lv_event_t * e)					// Get the part drawing descriptor and pass it to as a parameter ' LV_EVENT_DRAW_PART_BEGIN/END '.
const lv_area_t * lv_event_get_clip_area(lv_event_t * e)							// Get the clip area as a parameter to draw the event .
const lv_area_t * lv_event_get_old_size(lv_event_t * e)								// Get the old area before the object changes size . Can be in ' LV_EVENT_SIZE_CHANGED ' Use in 
uint32_t lv_event_get_key(lv_event_t * e)											// Get the key passed to the event as a parameter . Can be in ' LV_EVENT_KEY ' Use in 
lv_anim_t * lv_event_get_scroll_anim(lv_event_t * e)								// Get the scrolling animation descriptor . Can be in ' LV_EVENT_SCROLL_BEGIN ' Use in 
void lv_event_set_ext_draw_size(lv_event_t * e, lv_coord_t size)					// Set new extra drawing dimensions . Can be in ' lv_event_ref_ext_draw_size ' Use in 
lv_point_t * lv_event_get_self_size_info(lv_event_t * e)							// Get a pointer to ' lv_point_t ' Pointer to variable , Among them, self size should be preserved ( The width is ' point->x ' And height ' point->y ').
lv_hit_test_info_t * lv_event_get_hit_test_info(lv_event_t * e)						// Get a pointer to ' lv_hit_test_info_t ' Pointer to variable , This variable will hold the hit test results . Can be in ' LV_EVENT_HIT_TEST ' Use in 
const lv_area_t * lv_event_get_cover_area(lv_event_t * e)							// Get a pointer to the region , This area should be checked to see if the object completely covers it .
void lv_event_set_cover_res(lv_event_t * e, lv_cover_res_t res)						// Set the result of cover page check . Can be in ' LV_EVENT_COVER_CHECK ' Use in 

3、 ... and 、 Example

3.1 Press the key to realize the event callback display

/************************************************* *  The name of the function  : btn_event_cb *  ginseng   Count  :  nothing  *  The functionality  :  Key event callback display  *************************************************/
static void btn_event_cb(lv_event_t * e)
{
    
   static unsigned int cnt = 1;
   lv_obj_t *btn = lv_event_get_target(e);            // Gets the object that the event was originally aimed at . Even if the event is bubbling , It's the same .
   lv_obj_t * label = lv_obj_get_child(btn,0);        // Get the sub object through the index of the sub object .
   lv_label_set_text_fmt(label,"%u",cnt);             // Set a new formatted text for the label . Memory will be allocated to labels that store text .
   cnt++;
}
/************************************************* *  The name of the function  : event_show_1 *  ginseng   Count  :  nothing  *  The functionality  :  Implement event callback function  *************************************************/
void event_show_1()
{
    
   lv_obj_t * btn = lv_btn_create(lv_scr_act());      // establish btn object  
   lv_obj_set_size(btn,100,50);                       // Set object size 
   lv_obj_center(btn);                                // Center object 
   lv_obj_add_event_cb(btn,btn_event_cb,LV_EVENT_CLICKED,NULL);   // Add object callback function 

   lv_obj_t * label = lv_label_create(btn);           // establish label
   lv_label_set_text(label,"Click me!");              // Set up label Words of 
   lv_obj_center(label);                              // Center object 
}

 Please add a picture description

3.2 Key callback multi event judgment detection

/************************************************* *  The name of the function  : event_show_cb *  ginseng   Count  :  nothing  *  The functionality  :  Key event callback display  *************************************************/
static void event_show2_cb(lv_event_t * e)
{
    
   lv_event_code_t code = lv_event_get_code(e);       // Get the event code of the event 
   lv_obj_t * label = lv_event_get_user_data(e);      // Get the data carried by the user 
   switch(code){
    
      case LV_EVENT_PRESSED:                          // Press the event 
         lv_label_set_text(label,"The last button event:\nLV_EVENT_PRESSED");
         break;
      case LV_EVENT_CLICKED:                          // Click event 
         lv_label_set_text(label,"The last button event:\nLV_EVENT_CLICKED");
         break;
      case LV_EVENT_LONG_PRESSED:                     // Long press event 
         lv_label_set_text(label,"The last button event:\nLV_EVENT_LONG_PRESSED");
         break; 
      case LV_EVENT_LONG_PRESSED_REPEAT:              // Repeated press event 
         lv_label_set_text(label,"The last button event:\nLV_EVENT_LONG_PRESSED_REPEAT");
         break;
      case LV_EVENT_KEY:                              //KEY event 
         lv_label_set_text(label,"The last button event:\nLV_EVENT_KEY");
         break;
      default:
         break;
   }
}

/************************************************* *  The name of the function  : event_show_2 *  ginseng   Count  :  nothing  *  The functionality  :  Key linkage , Realization Label Animation display  *************************************************/
void event_show_2()
{
    
   lv_obj_t * btn = lv_btn_create(lv_scr_act());      // Create a button object 
   lv_obj_set_size(btn,100,50);                       // Set the button size 
   lv_obj_center(btn);                                // Center object 

   lv_obj_t *  btn_label = lv_label_create(btn);      // establish label
   lv_label_set_text(btn_label,"Click Me!");          // Set word content 
   lv_obj_center(btn_label);                          // In the middle 

   lv_obj_t * info_label = lv_label_create(lv_scr_act());         // establish label
   lv_label_set_text(info_label,"The last button event:\nNone");  // Set up label Content 
   lv_obj_add_event_cb(btn,event_show2_cb,LV_EVENT_ALL,info_label);// Set the transposition function 
}

 Please add a picture description

3.3 Implement event add flag bit

/************************************************* *  The name of the function  : event_show_3_cb *  ginseng   Count  :  nothing  *  The functionality  :  Event callback function  *************************************************/
static void event_show_3_cb (lv_event_t * e)
{
    
   lv_obj_t * target = lv_event_get_target(e);           // Gets the object that the event was originally aimed at . Even if the event is bubbling , It's the same . 
   lv_obj_t * cont = lv_event_get_current_target(e);     // Get the current target of the event . It is the object to which the event handler is called .
   if(target == cont) return;                            // Judge whether the initially aimed object is consistent with the currently obtained object 
   lv_obj_set_style_bg_color(target,lv_palette_main(LV_PALETTE_RED),0); // Set object background color 
}
/************************************************* *  The name of the function  : event_show_3 *  ginseng   Count  :  nothing  *  The functionality  :  Event linkage  *************************************************/
void event_show_3()
{
    
   lv_obj_t * cont = lv_obj_create(lv_scr_act());        // Create objects 
   lv_obj_set_size(cont,290,200);                        // Set object size 
   lv_obj_center(cont);                                  // Center object 
   lv_obj_set_flex_flow(cont,LV_FLEX_FLOW_ROW_WRAP);     // Scaling object settings  LV_FLEX_FLOW_ROW_WRAP  Telescopic popular winding 

   unsigned int i;
   for(i=0;i<30;i++){
    
      lv_obj_t * btn = lv_btn_create(cont);              // Create objects 
      lv_obj_set_size(btn,80,50);                        // Set object size 
      lv_obj_add_flag(btn,LV_OBJ_FLAG_EVENT_BUBBLE);     // Add object flag bit   Object flag event bubble 

      lv_obj_t * label = lv_label_create(btn);           // establish Label
      lv_label_set_text_fmt(label,"%u",i);               // Set up Label According to the content 
      lv_obj_center(label);                              // Center object 
   }
   lv_obj_add_event_cb(cont,event_show_3_cb,LV_EVENT_CLICKED,NULL);  // Add event callback function 
}

 Please add a picture description

版权声明
本文为[Please call me Xiao Peng]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210609450447.html