10
10
#define BRIGHTNESS_DARK_ZONE 250
11
11
12
12
// Functions to be defined in the tft/touch driver
13
- extern void lvgl_lcd_init (lv_disp_drv_t * disp_drv );
14
- extern void lvgl_touch_init (lv_indev_drv_t * disp_drv );
13
+ extern lv_display_t * lvgl_lcd_init ();
14
+ extern lv_indev_t * lvgl_touch_init ();
15
15
16
- lv_disp_drv_t disp_drv ;
17
- lv_timer_t * update_brightness_timer ;
16
+ lv_display_t * display ;
18
17
19
18
#ifdef BOARD_HAS_TOUCH
20
- lv_indev_drv_t indev_drv ;
19
+ lv_indev_t * indev ;
21
20
touch_calibration_data_t touch_calibration_data ;
22
- void (* driver_touch_read_cb )(struct _lv_indev_drv_t * indev_drv , lv_indev_data_t * data );
21
+ void (* driver_touch_read_cb )(lv_indev_t * indev , lv_indev_data_t * data );
23
22
#endif
24
23
24
+ void lvgl_display_resolution_changed_callback (lv_event_t * drv );
25
+
26
+ lv_timer_t * update_brightness_timer ;
27
+
25
28
#ifdef LV_USE_LOG
26
- void lvgl_log (const char * buf )
29
+ void lvgl_log (lv_log_level_t level , const char * buf )
27
30
{
28
- log_printf ("%s" , buf );
31
+ switch (level )
32
+ {
33
+ case LV_LOG_LEVEL_TRACE :
34
+ log_printf ("%s" , buf );
35
+ break ;
36
+ case LV_LOG_LEVEL_INFO :
37
+ log_i ("%s" , buf );
38
+ break ;
39
+ case LV_LOG_LEVEL_WARN :
40
+ log_w ("%s" , buf );
41
+ break ;
42
+ case LV_LOG_LEVEL_ERROR :
43
+ log_e ("%s" , buf );
44
+ break ;
45
+ }
29
46
}
30
47
#endif
31
48
@@ -34,9 +51,9 @@ void smartdisplay_lcd_set_backlight(float duty)
34
51
{
35
52
log_v ("duty:%2f" , duty );
36
53
37
- if (duty > 1.0 )
54
+ if (duty > 1.0f )
38
55
duty = 1.0f ;
39
- if (duty < 0.0 )
56
+ if (duty < 0.0f )
40
57
duty = 0.0f ;
41
58
#if ESP_ARDUINO_VERSION_MAJOR >= 3
42
59
ledcWrite (GPIO_BCKL , duty * PWM_MAX_BCKL );
@@ -74,7 +91,7 @@ void adaptive_brightness(lv_timer_t *timer)
74
91
75
92
void smartdisplay_lcd_set_brightness_cb (smartdisplay_lcd_adaptive_brightness_cb_t cb , uint interval )
76
93
{
77
- log_v ("adaptive_brightness_cb:0x%08x, interval:%d " , cb , interval );
94
+ log_v ("adaptive_brightness_cb:0x%08x, interval:%u " , cb , interval );
78
95
79
96
// Delete current timer if any
80
97
if (update_brightness_timer )
@@ -100,12 +117,12 @@ void smartdisplay_led_set_rgb(bool r, bool g, bool b)
100
117
101
118
#ifdef BOARD_HAS_TOUCH
102
119
// See: https://www.maximintegrated.com/en/design/technical-documents/app-notes/5/5296.html
103
- void lvgl_touch_calibration_transform (lv_indev_drv_t * disp_drv , lv_indev_data_t * data )
120
+ void lvgl_touch_calibration_transform (lv_indev_t * indev , lv_indev_data_t * data )
104
121
{
105
- log_v ("disp_drv :0x%08x, data:0x%08x" , disp_drv , data );
122
+ log_v ("indev :0x%08x, data:0x%08x" , indev , data );
106
123
107
124
// Call low level read from the driver
108
- driver_touch_read_cb (disp_drv , data );
125
+ driver_touch_read_cb (indev , data );
109
126
// Check if transformation is required
110
127
if (touch_calibration_data .valid && data -> state == LV_INDEV_STATE_PRESSED )
111
128
{
@@ -137,37 +154,6 @@ touch_calibration_data_t smartdisplay_compute_touch_calibration(const lv_point_t
137
154
};
138
155
#endif
139
156
140
- // Called when driver parameters are updated (rotation)
141
- // Top of the display is top left when connector is at the bottom
142
- // The rotation values are relative to how you would rotate the physical display in the clockwise direction.
143
- // Thus, LV_DISP_ROT_90 means you rotate the hardware 90 degrees clockwise, and the display rotates 90 degrees counterclockwise to compensate.
144
- void lvgl_update_callback (lv_disp_drv_t * drv )
145
- {
146
- if (drv -> sw_rotate == false)
147
- {
148
- const esp_lcd_panel_handle_t panel_handle = disp_drv .user_data ;
149
- switch (drv -> rotated )
150
- {
151
- case LV_DISP_ROT_NONE :
152
- ESP_ERROR_CHECK (esp_lcd_panel_swap_xy (panel_handle , DISPLAY_SWAP_XY ));
153
- ESP_ERROR_CHECK (esp_lcd_panel_mirror (panel_handle , DISPLAY_MIRROR_X , DISPLAY_MIRROR_Y ));
154
- break ;
155
- case LV_DISP_ROT_90 :
156
- ESP_ERROR_CHECK (esp_lcd_panel_swap_xy (panel_handle , !DISPLAY_SWAP_XY ));
157
- ESP_ERROR_CHECK (esp_lcd_panel_mirror (panel_handle , DISPLAY_MIRROR_X , !DISPLAY_MIRROR_Y ));
158
- break ;
159
- case LV_DISP_ROT_180 :
160
- ESP_ERROR_CHECK (esp_lcd_panel_swap_xy (panel_handle , DISPLAY_SWAP_XY ));
161
- ESP_ERROR_CHECK (esp_lcd_panel_mirror (panel_handle , !DISPLAY_MIRROR_X , !DISPLAY_MIRROR_Y ));
162
- break ;
163
- case LV_DISP_ROT_270 :
164
- ESP_ERROR_CHECK (esp_lcd_panel_swap_xy (panel_handle , !DISPLAY_SWAP_XY ));
165
- ESP_ERROR_CHECK (esp_lcd_panel_mirror (panel_handle , !DISPLAY_MIRROR_X , DISPLAY_MIRROR_Y ));
166
- break ;
167
- }
168
- }
169
- }
170
-
171
157
void smartdisplay_init ()
172
158
{
173
159
log_d ("smartdisplay_init" );
@@ -206,31 +192,52 @@ void smartdisplay_init()
206
192
ledcAttachPin (GPIO_BCKL , PWM_CHANNEL_BCKL );
207
193
#endif
208
194
// Setup TFT display
209
- lv_disp_drv_init (& disp_drv );
210
- disp_drv .hor_res = DISPLAY_WIDTH ;
211
- disp_drv .ver_res = DISPLAY_HEIGHT ;
212
- // Create drawBuffer
213
- disp_drv .draw_buf = (lv_disp_draw_buf_t * )malloc (sizeof (lv_disp_draw_buf_t ));
214
- void * drawBuffer = heap_caps_malloc (sizeof (lv_color_t ) * LVGL_BUFFER_PIXELS , LVGL_BUFFER_MALLOC_FLAGS );
215
- lv_disp_draw_buf_init (disp_drv .draw_buf , drawBuffer , NULL , LVGL_BUFFER_PIXELS );
216
- // Register callback for changes to the driver parameters (rotation!)
217
- disp_drv .drv_update_cb = lvgl_update_callback ;
218
- // Initialize specific driver
219
- lvgl_lcd_init (& disp_drv );
220
- __attribute__((unused )) lv_disp_t * display = lv_disp_drv_register (& disp_drv );
221
- // Clear screen
195
+ display = lvgl_lcd_init ();
196
+ // Register callback for hardware rotation
197
+ if (!display -> sw_rotate )
198
+ lv_display_add_event_cb (display , lvgl_display_resolution_changed_callback , LV_EVENT_RESOLUTION_CHANGED , NULL );
199
+
200
+ // Clear screen
222
201
lv_obj_clean (lv_scr_act ());
223
202
// Turn backlight on (50%)
224
203
smartdisplay_lcd_set_backlight (0.5f );
225
204
226
205
// If there is a touch controller defined
227
206
#ifdef BOARD_HAS_TOUCH
228
207
// Setup touch
229
- lv_indev_drv_init ( & indev_drv );
230
- indev_drv . disp = display ;
231
- lvgl_touch_init ( & indev_drv );
232
- driver_touch_read_cb = indev_drv . read_cb ;
233
- indev_drv . read_cb = lvgl_touch_calibration_transform ;
234
- lv_indev_drv_register ( & indev_drv );
208
+ indev = lvgl_touch_init ( );
209
+ indev -> disp = display ;
210
+ // Intercept callback
211
+ driver_touch_read_cb = indev -> read_cb ;
212
+ indev -> read_cb = lvgl_touch_calibration_transform ;
213
+ lv_indev_enable ( indev , true );
235
214
#endif
215
+ }
216
+
217
+ // Called when driver resolution is updated (including rotation)
218
+ // Top of the display is top left when connector is at the bottom
219
+ // The rotation values are relative to how you would rotate the physical display in the clockwise direction.
220
+ // Thus, LV_DISPLAY_ROTATION_90 means you rotate the hardware 90 degrees clockwise, and the display rotates 90 degrees counterclockwise to compensate.
221
+ void lvgl_display_resolution_changed_callback (lv_event_t * event )
222
+ {
223
+ const esp_lcd_panel_handle_t panel_handle = display -> user_data ;
224
+ switch (display -> rotation )
225
+ {
226
+ case LV_DISPLAY_ROTATION_0 :
227
+ ESP_ERROR_CHECK (esp_lcd_panel_swap_xy (panel_handle , DISPLAY_SWAP_XY ));
228
+ ESP_ERROR_CHECK (esp_lcd_panel_mirror (panel_handle , DISPLAY_MIRROR_X , DISPLAY_MIRROR_Y ));
229
+ break ;
230
+ case LV_DISPLAY_ROTATION_90 :
231
+ ESP_ERROR_CHECK (esp_lcd_panel_swap_xy (panel_handle , !DISPLAY_SWAP_XY ));
232
+ ESP_ERROR_CHECK (esp_lcd_panel_mirror (panel_handle , DISPLAY_MIRROR_X , !DISPLAY_MIRROR_Y ));
233
+ break ;
234
+ case LV_DISPLAY_ROTATION_180 :
235
+ ESP_ERROR_CHECK (esp_lcd_panel_swap_xy (panel_handle , DISPLAY_SWAP_XY ));
236
+ ESP_ERROR_CHECK (esp_lcd_panel_mirror (panel_handle , !DISPLAY_MIRROR_X , !DISPLAY_MIRROR_Y ));
237
+ break ;
238
+ case LV_DISPLAY_ROTATION_270 :
239
+ ESP_ERROR_CHECK (esp_lcd_panel_swap_xy (panel_handle , !DISPLAY_SWAP_XY ));
240
+ ESP_ERROR_CHECK (esp_lcd_panel_mirror (panel_handle , !DISPLAY_MIRROR_X , DISPLAY_MIRROR_Y ));
241
+ break ;
242
+ }
236
243
}
0 commit comments