@@ -41,60 +41,35 @@ int y_position = 50;
4141
4242static void btn_event_cb (lv_event_t *e)
4343{
44+ // Get the event code
4445 lv_event_code_t code = lv_event_get_code (e);
4546
47+ // If click event detected, update the position
4648 if (code == LV_EVENT_CLICKED)
4749 {
4850 if (rect)
4951 {
52+ // Update coordinates
5053 x_position += 100 ;
5154 y_position += 100 ;
52-
55+
5356 if (y_position < 660 )
5457 {
58+ // Set new position of an object
5559 lv_obj_set_pos (rect, x_position, y_position);
60+ // Mark the object to redraw it in next refresh
5661 lv_obj_invalidate (rect);
5762 isRectangleClicked = true ;
5863 }
5964 else
6065 {
6166 x_position = 50 ;
6267 y_position = 50 ;
63-
64- inkplate.clearDisplay ();
65- lv_obj_clean (lv_scr_act ());
66- lv_draw_initials ();
67- lv_obj_invalidate (lv_scr_act ());
68- for (int i = 0 ; i < 5 ; i++)
69- {
70- lv_timer_handler ();
71- delay (10 );
72- }
73- inkplate.display ();
7468 }
7569 }
7670 }
7771}
7872
79- static void lv_draw_initials ()
80- {
81- /* Create a black label, set its text and font and align it to the center */
82- lv_obj_t *label = lv_label_create (lv_screen_active ());
83- lv_label_set_text (label, " Touch the rectangles on screen!" );
84- lv_obj_set_style_text_color (lv_screen_active (), lv_color_hex (0x000000 ), LV_PART_MAIN);
85- lv_obj_set_style_text_font (label, &lv_font_montserrat_48, 0 );
86- lv_obj_align (label, LV_ALIGN_TOP_MID, 0 , 0 );
87-
88- /* Create a black rectangle */
89- rect = lv_obj_create (lv_scr_act ());
90- lv_obj_set_style_bg_color (rect, lv_color_hex (0x000000 ), 0 );
91- lv_obj_set_size (rect, 100 , 50 );
92- lv_obj_set_pos (rect, x_position, y_position);
93-
94- // Add event callback function
95- lv_obj_add_event_cb (rect, btn_event_cb, LV_EVENT_ALL, NULL );
96- }
97-
9873void setup ()
9974{
10075 Serial.begin (115200 );
@@ -112,6 +87,25 @@ void setup()
11287 while (true );
11388 }
11489
90+ /* Create a black label, set its text and font and align it to the center */
91+ lv_obj_t *label = lv_label_create (lv_screen_active ());
92+ lv_label_set_text (label, " Touch the rectangles on screen!" );
93+ lv_obj_set_style_text_color (lv_screen_active (), lv_color_hex (0x000000 ), LV_PART_MAIN);
94+ lv_obj_set_style_text_font (label, &lv_font_montserrat_48, 0 );
95+ lv_obj_center (label);
96+
97+ // Render text on screen
98+ lv_timer_handler ();
99+ inkplate.display ();
100+
101+ // Wait for 2 seconds
102+ delay (2000 );
103+
104+ // Delete the label object and clear the display
105+ lv_obj_del (label);
106+ label = NULL ;
107+ inkplate.clearDisplay ();
108+
115109 // Create LVGL task on core 1 to run independently from the rest of the sketch
116110 xTaskCreatePinnedToCore (
117111 lvgl_task, // Function which will be pinned
@@ -123,14 +117,22 @@ void setup()
123117 1 // core used
124118 );
125119
126- lv_draw_initials ();
120+ /* Create a black rectangle and add a callback event function to it */
121+ rect = lv_obj_create (lv_scr_act ());
122+ lv_obj_set_style_bg_color (rect, lv_color_hex (0x000000 ), 0 );
123+ lv_obj_set_size (rect, 100 , 50 );
124+ lv_obj_set_pos (rect, x_position, y_position);
125+
126+ // Add event callback function
127+ lv_obj_add_event_cb (rect, btn_event_cb, LV_EVENT_ALL, NULL );
127128
128129 // Force initial render
129130 for (int i = 0 ; i < 5 ; i++)
130131 {
131132 lv_timer_handler ();
132133 delay (10 );
133134 }
135+
134136 // Display content from buffer
135137 inkplate.display ();
136138}
@@ -143,4 +145,3 @@ void loop()
143145 isRectangleClicked = false ;
144146 }
145147}
146-
0 commit comments