Skip to content

Commit cfae8e2

Browse files
committed
Refactor smartdisplay_init to return display handle and update function signature
1 parent 225bbe8 commit cfae8e2

File tree

6 files changed

+24
-74
lines changed

6 files changed

+24
-74
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,10 +375,11 @@ To include it globally, the define must be (for the include directory):
375375

376376
The library exposes the following functions.
377377

378-
### void smartdisplay_init()
378+
### lv_display_t * smartdisplay_init()
379379

380380
This is the first function that needs to be called.
381381
It initializes the display controller and touch controller and will turn on the display at 50% brightness.
382+
The return value is the display handle.
382383

383384
### void smartdisplay_lcd_set_backlight(float duty)
384385

include/esp32_smartdisplay.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ extern "C"
3131
} touch_calibration_data_t;
3232

3333
// Initialize the display and touch
34-
void smartdisplay_init();
34+
lv_display_t* smartdisplay_init();
3535
#ifdef BOARD_HAS_TOUCH
3636
// Touch calibration
3737
extern touch_calibration_data_t touch_calibration_data;

include/lvgl_panel_common.h

Lines changed: 3 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -108,68 +108,11 @@ static inline void lv_flush_oled(lv_display_t *display, const lv_area_t *area, u
108108
// More information about the monochrome, please refer to https://docs.lvgl.io/9.2/porting/display.html#monochrome-displays
109109
px_map += 8;
110110

111-
// log_v("Converting to tiled format for OLED");
112-
// #define LVGL_OLED_BIT_ORDER_LSB true
113-
// // lv_draw_sw_i1_convert_to_vtiled(px_map, area_buf_size, w, h, oled_buffer, area_buf_size, LVGL_OLED_BIT_ORDER_LSB);
114-
// // - The output buffer (oled_buffer) must be at least as large as the input buffer, and is statically allocated for the full display size.
115-
// // - The function assumes that both width and height are multiples of 8, as required by LVGL for monochrome tiled rendering.
116-
// // - If the area is not aligned to 8x8, or the buffer sizes are insufficient, the function may assert or produce incorrect output.
117-
// log_v("Convert to vtiled: w=%d, h=%d, buf_size=%d", w, h, px_buf_size);
118-
// lv_draw_sw_i1_convert_to_vtiled(px_map, px_buf_size, w, h, &oled_buffer, px_buf_size, LVGL_OLED_BIT_ORDER_LSB);
119-
uint32_t hor_res = lv_display_get_physical_horizontal_resolution(display);
120-
int32_t x1 = area->x1;
121-
int32_t x2 = area->x2;
122-
int32_t y1 = area->y1;
123-
int32_t y2 = area->y2;
124-
125-
for (int y = y1; y <= y2; y++)
126-
{
127-
for (int32_t x = x1; x <= x2; x++)
128-
{
129-
/* The order of bits is MSB first
130-
MSB LSB
131-
bits 7 6 5 4 3 2 1 0
132-
pixels 0 1 2 3 4 5 6 7
133-
Left Right
134-
*/
135-
bool chroma_color = (px_map[(hor_res >> 3) * y + (x >> 3)] & 1 << (7 - x % 8));
136-
/* Write to the buffer as required for the display.
137-
* It writes only 1-bit for monochrome displays mapped vertically.*/
138-
uint8_t *buf = oled_buffer + hor_res * (y >> 3) + (x);
139-
if (chroma_color)
140-
*buf &= ~(1 << (y % 8));
141-
else
142-
*buf |= (1 << (y % 8));
143-
}
144-
}
111+
const size_t px_buf_size = lv_display_get_draw_buf_size(display);
112+
lv_draw_sw_i1_convert_to_vtiled(px_map, px_buf_size, LVGL_HOR_RES, LVGL_VER_RES, oled_buffer, sizeof(oled_buffer), false);
145113

146114
// pass the draw buffer to the driver
147-
esp_lcd_panel_draw_bitmap(panel_handle, area->x1, area->y1, area->x2 + 1, area->y2 + 1, oled_buffer);
148-
149-
150-
151-
// // To use LV_COLOR_FORMAT_I1, we need an extra buffer to hold the converted data
152-
// int32_t w = area->x2 - area->x1 + 1;
153-
// int32_t h = area->y2 - area->y1 + 1;
154-
// size_t area_buf_size = (w * h) / 8;
155-
// uint8_t *oled_buffer = (uint8_t *)heap_caps_malloc(area_buf_size, LVGL_BUFFER_MALLOC_FLAGS);
156-
// assert(oled_buffer != NULL);
157-
158-
// // This is necessary because LVGL reserves 2 x 4 bytes in the buffer, as these are assumed to be used as a palette. Skip the palette here
159-
// // More information about the monochrome, please refer to https://docs.lvgl.io/9.2/porting/display.html#monochrome-displays
160-
// px_map += 8;
161-
162-
// #define LVGL_OLED_BIT_ORDER_LSB true
163-
// // lv_draw_sw_i1_convert_to_vtiled(px_map, area_buf_size, w, h, oled_buffer, area_buf_size, LVGL_OLED_BIT_ORDER_LSB);
164-
// // - The output buffer (oled_buffer) must be at least as large as the input buffer, and is statically allocated for the full display size.
165-
// // - The function assumes that both width and height are multiples of 8, as required by LVGL for monochrome tiled rendering.
166-
// // - If the area is not aligned to 8x8, or the buffer sizes are insufficient, the function may assert or produce incorrect output.
167-
// lv_draw_sw_i1_convert_to_vtiled(px_map, area_buf_size, w, h, oled_buffer, area_buf_size, true);
168-
169-
// // pass the draw buffer to the driver
170-
// esp_lcd_panel_draw_bitmap(panel_handle, area->x1, area->y1, area->x2, area->y2 , oled_buffer);
171-
172-
// free(oled_buffer);
115+
esp_lcd_panel_draw_bitmap(panel_handle, 0,0, LVGL_HOR_RES, LVGL_VER_RES, oled_buffer);
173116
}
174117

175118
static inline void lvgl_setup_panel(esp_lcd_panel_handle_t panel_handle)

src/esp32_smartdisplay.c

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,7 @@
1313
extern lv_display_t *lvgl_lcd_init();
1414
extern lv_indev_t *lvgl_touch_init();
1515

16-
lv_display_t *display;
17-
1816
#ifdef BOARD_HAS_TOUCH
19-
lv_indev_t *indev;
2017
touch_calibration_data_t touch_calibration_data;
2118
void (*driver_touch_read_cb)(lv_indev_t *indev, lv_indev_data_t *data);
2219
#endif
@@ -160,7 +157,7 @@ touch_calibration_data_t smartdisplay_compute_touch_calibration(const lv_point_t
160157
};
161158
#endif
162159

163-
void smartdisplay_init()
160+
lv_display_t *smartdisplay_init()
164161
{
165162
log_d("smartdisplay_init");
166163
#ifdef BOARD_HAS_RGB_LED
@@ -203,30 +200,32 @@ void smartdisplay_init()
203200
#endif
204201
#endif
205202
// Setup TFT display
206-
display = lvgl_lcd_init();
203+
lv_display_t *display = lvgl_lcd_init();
207204

208205
#ifndef LVGL_DISPLAY_SOFTWARE_ROTATION
209206
// Register callback for hardware rotation
210-
lv_display_add_event_cb(display, lvgl_display_resolution_changed_callback, LV_EVENT_RESOLUTION_CHANGED, NULL);
207+
lv_display_add_event_cb(display, lvgl_display_resolution_changed_callback, LV_EVENT_RESOLUTION_CHANGED, display);
211208
#endif
212209

213210
// Clear screen
214211
lv_obj_clean(lv_scr_act());
215-
#if DISPLAY_BCKL
212+
#if DISPLAY_BCKL
216213
// Turn backlight on (50%)
217214
smartdisplay_lcd_set_backlight(0.5f);
218215
#endif
219216

220217
// If there is a touch controller defined
221218
#ifdef BOARD_HAS_TOUCH
222219
// Setup touch
223-
indev = lvgl_touch_init();
224-
indev->disp = display;
220+
lv_indev_t* indev = lvgl_touch_init();
221+
lv_indev_set_display(indev, display);
225222
// Intercept callback
226-
driver_touch_read_cb = indev->read_cb;
227-
indev->read_cb = lvgl_touch_calibration_transform;
223+
driver_touch_read_cb = lv_indev_get_read_cb(indev);
224+
lv_indev_set_read_cb(indev, lvgl_touch_calibration_transform);
228225
lv_indev_enable(indev, true);
229226
#endif
227+
228+
return display;
230229
}
231230

232231
#ifndef LVGL_DISPLAY_SOFTWARE_ROTATION
@@ -236,7 +235,8 @@ void smartdisplay_init()
236235
// So, LV_DISPLAY_ROTATION_90 means you rotate the hardware 90 degrees clockwise, and the display rotates 90 degrees counterclockwise to compensate.
237236
static void lvgl_display_resolution_changed_callback(lv_event_t *event)
238237
{
239-
const esp_lcd_panel_handle_t panel_handle = display->user_data;
238+
lv_display_t *display = lv_event_get_target(event);
239+
const esp_lcd_panel_handle_t panel_handle = lv_display_get_user_data(display);
240240
switch (display->rotation)
241241
{
242242
case LV_DISPLAY_ROTATION_0:

src/lvgl_panel_ssd1306_i2c.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ lv_display_t *lvgl_lcd_init()
5353
esp_lcd_panel_handle_t panel_handle;
5454
ESP_ERROR_CHECK(esp_lcd_new_panel_ssd1306(io_handle, &panel_dev_config, &panel_handle));
5555

56+
if ((res = esp_lcd_panel_io_rx_param(th->io, 0x81, (const uint8_t[]){0x8F}, 1)) != ESP_OK) // SSD1306_SETCONTRAST (0x81), 0x8F is a reasonable default contrast value
57+
{
58+
log_e("Unable to set brightness");
59+
return res;
60+
}
61+
5662
lvgl_setup_panel(panel_handle);
5763
lv_display_set_user_data(display, panel_handle);
5864
lv_display_set_flush_cb(display, lv_flush_oled);

0 commit comments

Comments
 (0)