Skip to content

Commit

Permalink
Fixed issue
Browse files Browse the repository at this point in the history
  • Loading branch information
espzav committed Apr 30, 2024
1 parent 53650e0 commit 828d57b
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 30 deletions.
13 changes: 11 additions & 2 deletions components/esp_lvgl_port/include/esp_lvgl_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,20 @@ extern "C" {
#endif

/**
* @brief LVGL Port task events
* @brief LVGL Port task event type
*/
typedef enum {
LVGL_PORT_EVENT_DISPLAY = 1,
LVGL_PORT_EVENT_TOUCH = 2,
LVGL_PORT_EVENT_USER = 99,
} lvgl_port_event_type_t;

/**
* @brief LVGL Port task events
*/
typedef struct {
lvgl_port_event_type_t type;
void *param;
} lvgl_port_event_t;

/**
Expand Down Expand Up @@ -136,12 +144,13 @@ esp_err_t lvgl_port_resume(void);
* @note It is called from LVGL events and touch interrupts
*
* @param event event type
* @param param user param
* @return
* - ESP_OK on success
* - ESP_ERR_NOT_SUPPORTED if it is not implemented
* - ESP_ERR_INVALID_STATE if queue is not initialized (can be returned after LVGL deinit)
*/
esp_err_t lvgl_port_task_wake(lvgl_port_event_t event);
esp_err_t lvgl_port_task_wake(lvgl_port_event_type_t event, void *param);

#ifdef __cplusplus
}
Expand Down
2 changes: 1 addition & 1 deletion components/esp_lvgl_port/src/lvgl8/esp_lvgl_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ void lvgl_port_unlock(void)
xSemaphoreGiveRecursive(lvgl_port_ctx.lvgl_mux);
}

esp_err_t lvgl_port_task_wake(lvgl_port_event_t event)
esp_err_t lvgl_port_task_wake(lvgl_port_event_type_t event, void *param)
{
ESP_LOGE(TAG, "Task wake is not supported, when used LVGL8!");
return ESP_ERR_NOT_SUPPORTED;
Expand Down
44 changes: 31 additions & 13 deletions components/esp_lvgl_port/src/lvgl9/esp_lvgl_port.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ esp_err_t lvgl_port_init(const lvgl_port_cfg_t *cfg)
lvgl_port_ctx.task_init_mux = xSemaphoreCreateMutex();
ESP_GOTO_ON_FALSE(lvgl_port_ctx.task_init_mux, ESP_ERR_NO_MEM, err, TAG, "Create LVGL task sem fail!");
/* Task queue */
lvgl_port_ctx.lvgl_queue = xQueueCreate(10, sizeof(lvgl_port_event_t));
lvgl_port_ctx.lvgl_queue = xQueueCreate(100, sizeof(lvgl_port_event_t));
ESP_GOTO_ON_FALSE(lvgl_port_ctx.lvgl_queue, ESP_ERR_NO_MEM, err, TAG, "Create LVGL queue fail!");

BaseType_t res;
Expand Down Expand Up @@ -160,20 +160,25 @@ void lvgl_port_unlock(void)
xSemaphoreGiveRecursive(lvgl_port_ctx.lvgl_mux);
}

esp_err_t lvgl_port_task_wake(lvgl_port_event_t event)
esp_err_t lvgl_port_task_wake(lvgl_port_event_type_t event, void *param)
{
if (!lvgl_port_ctx.lvgl_queue) {
return ESP_ERR_INVALID_STATE;
}

lvgl_port_event_t ev = {
.type = event,
.param = param,
};

if (xPortInIsrContext() == pdTRUE) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xQueueSendFromISR(lvgl_port_ctx.lvgl_queue, &event, &xHigherPriorityTaskWoken);
xQueueSendFromISR(lvgl_port_ctx.lvgl_queue, &ev, &xHigherPriorityTaskWoken);
if (xHigherPriorityTaskWoken) {
portYIELD_FROM_ISR( );
}
} else {
xQueueSend(lvgl_port_ctx.lvgl_queue, &event, 0);
xQueueSend(lvgl_port_ctx.lvgl_queue, &ev, 0);
}

return ESP_OK;
Expand All @@ -199,8 +204,9 @@ IRAM_ATTR bool lvgl_port_task_notify(uint32_t value)

static void lvgl_port_task(void *arg)
{
lvgl_port_event_t event = 0;
lvgl_port_event_t event;
uint32_t task_delay_ms = 0;
lv_indev_t *indev = NULL;

/* Take the task semaphore */
if (xSemaphoreTake(lvgl_port_ctx.task_init_mux, 0) != pdTRUE) {
Expand All @@ -213,20 +219,32 @@ static void lvgl_port_task(void *arg)
lvgl_port_ctx.running = true;
while (lvgl_port_ctx.running) {
/* Wait for queue or timeout (sleep task) */
xQueueReceive(lvgl_port_ctx.lvgl_queue, &event, pdMS_TO_TICKS(task_delay_ms));
TickType_t wait = (pdMS_TO_TICKS(task_delay_ms) >= 1 ? pdMS_TO_TICKS(task_delay_ms) : 1);
BaseType_t ret = xQueueReceive(lvgl_port_ctx.lvgl_queue, &event, wait);

if (lv_display_get_default() && lvgl_port_lock(0)) {

/* Call read input devices */
if (event.type == LVGL_PORT_EVENT_TOUCH || ret == pdFALSE) {
if (event.param != NULL) {
lv_indev_read(event.param);
} else {
indev = lv_indev_get_next(NULL);
while (indev != NULL) {
lv_indev_read(indev);
indev = lv_indev_get_next(indev);
}
}
}

/* Handle LVGL */
task_delay_ms = lv_timer_handler();
lvgl_port_unlock();
}
if ((task_delay_ms > lvgl_port_ctx.task_max_sleep_ms) || (1 == task_delay_ms)) {
task_delay_ms = lvgl_port_ctx.task_max_sleep_ms;
} else if (task_delay_ms < 1) {
task_delay_ms = 1;
} else {
task_delay_ms = 1; /*Keep trying*/
}

/* Sleep task, when everything done */
if (lv_anim_count_running() == 0 && lv_display_get_inactive_time(NULL) > 2 * LV_DEF_REFR_PERIOD) {
if (task_delay_ms == LV_NO_TIMER_READY) {
task_delay_ms = lvgl_port_ctx.task_max_sleep_ms;
}
}
Expand Down
5 changes: 3 additions & 2 deletions components/esp_lvgl_port/src/lvgl9/esp_lvgl_port_button.c
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ lv_indev_t *lvgl_port_add_navigation_buttons(const lvgl_port_nav_btns_cfg_t *but
/* Register a touchpad input device */
indev = lv_indev_create();
lv_indev_set_type(indev, LV_INDEV_TYPE_ENCODER);
lv_indev_set_mode(indev, LV_INDEV_MODE_EVENT);
lv_indev_set_read_cb(indev, lvgl_port_navigation_buttons_read);
lv_indev_set_disp(indev, buttons_cfg->disp);
lv_indev_set_user_data(indev, buttons_ctx);
Expand Down Expand Up @@ -182,7 +183,7 @@ static void lvgl_port_btn_down_handler(void *arg, void *arg2)
}

/* Wake LVGL task, if needed */
lvgl_port_task_wake(LVGL_PORT_EVENT_TOUCH);
lvgl_port_task_wake(LVGL_PORT_EVENT_TOUCH, ctx->indev);
}

static void lvgl_port_btn_up_handler(void *arg, void *arg2)
Expand All @@ -205,5 +206,5 @@ static void lvgl_port_btn_up_handler(void *arg, void *arg2)
}

/* Wake LVGL task, if needed */
lvgl_port_task_wake(LVGL_PORT_EVENT_TOUCH);
lvgl_port_task_wake(LVGL_PORT_EVENT_TOUCH, ctx->indev);
}
7 changes: 4 additions & 3 deletions components/esp_lvgl_port/src/lvgl9/esp_lvgl_port_disp.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include "esp_log.h"
#include "esp_err.h"
#include "esp_check.h"
#include "freertos/FreeRTOS.h"
#include "esp_heap_caps.h"
#include "esp_idf_version.h"
#include "esp_lcd_panel_io.h"
Expand Down Expand Up @@ -344,7 +345,7 @@ static bool lvgl_port_flush_vsync_ready_callback(esp_lcd_panel_handle_t panel_io
lv_display_t *disp_drv = (lv_display_t *)user_ctx;
assert(disp_drv != NULL);
need_yield = lvgl_port_task_notify(ULONG_MAX);
lvgl_port_task_wake(LVGL_PORT_EVENT_DISPLAY);
lvgl_port_task_wake(LVGL_PORT_EVENT_DISPLAY, disp_drv);

return (need_yield == pdTRUE);
}
Expand Down Expand Up @@ -468,7 +469,7 @@ static void lvgl_port_disp_rotation_update(lvgl_port_display_ctx_t *disp_ctx)
}

/* Wake LVGL task, if needed */
lvgl_port_task_wake(LVGL_PORT_EVENT_DISPLAY);
lvgl_port_task_wake(LVGL_PORT_EVENT_DISPLAY, disp_ctx->disp_drv);
}

static void lvgl_port_disp_size_update_callback(lv_event_t *e)
Expand All @@ -481,5 +482,5 @@ static void lvgl_port_disp_size_update_callback(lv_event_t *e)
static void lvgl_port_display_invalidate_callback(lv_event_t *e)
{
/* Wake LVGL task, if needed */
lvgl_port_task_wake(LVGL_PORT_EVENT_DISPLAY);
lvgl_port_task_wake(LVGL_PORT_EVENT_DISPLAY, NULL);
}
9 changes: 5 additions & 4 deletions components/esp_lvgl_port/src/lvgl9/esp_lvgl_port_knob.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ lv_indev_t *lvgl_port_add_encoder(const lvgl_port_encoder_cfg_t *encoder_cfg)
encoder_ctx->knob_handle = iot_knob_create(encoder_cfg->encoder_a_b);
ESP_GOTO_ON_FALSE(encoder_ctx->knob_handle, ESP_ERR_NO_MEM, err, TAG, "Not enough memory for knob create!");

iot_knob_register_cb
ESP_ERROR_CHECK(iot_knob_register_cb(encoder_ctx->knob_handle, KNOB_LEFT, lvgl_port_encoder_knob_handler, encoder_ctx));
ESP_ERROR_CHECK(iot_knob_register_cb(encoder_ctx->knob_handle, KNOB_RIGHT, lvgl_port_encoder_knob_handler, encoder_ctx));
}
Expand All @@ -74,6 +73,7 @@ lv_indev_t *lvgl_port_add_encoder(const lvgl_port_encoder_cfg_t *encoder_cfg)
/* Register a encoder input device */
indev = lv_indev_create();
lv_indev_set_type(indev, LV_INDEV_TYPE_ENCODER);
lv_indev_set_mode(indev, LV_INDEV_MODE_EVENT);
lv_indev_set_read_cb(indev, lvgl_port_encoder_read);
lv_indev_set_disp(indev, encoder_cfg->disp);
lv_indev_set_user_data(indev, encoder_ctx);
Expand Down Expand Up @@ -159,7 +159,7 @@ static void lvgl_port_encoder_btn_down_handler(void *arg, void *arg2)
}

/* Wake LVGL task, if needed */
lvgl_port_task_wake(LVGL_PORT_EVENT_TOUCH);
lvgl_port_task_wake(LVGL_PORT_EVENT_TOUCH, ctx->indev);
}

static void lvgl_port_encoder_btn_up_handler(void *arg, void *arg2)
Expand All @@ -174,11 +174,12 @@ static void lvgl_port_encoder_btn_up_handler(void *arg, void *arg2)
}

/* Wake LVGL task, if needed */
lvgl_port_task_wake(LVGL_PORT_EVENT_TOUCH);
lvgl_port_task_wake(LVGL_PORT_EVENT_TOUCH, ctx->indev);
}

static void lvgl_port_encoder_knob_handler(void *arg, void *arg2)
{
lvgl_port_encoder_ctx_t *ctx = (lvgl_port_encoder_ctx_t *) arg2;
/* Wake LVGL task, if needed */
lvgl_port_task_wake(LVGL_PORT_EVENT_TOUCH);
lvgl_port_task_wake(LVGL_PORT_EVENT_TOUCH, ctx->indev);
}
9 changes: 6 additions & 3 deletions components/esp_lvgl_port/src/lvgl9/esp_lvgl_port_touch.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,15 @@ lv_indev_t *lvgl_port_add_touch(const lvgl_port_touch_cfg_t *touch_cfg)

if (touch_ctx->handle->config.int_gpio_num != GPIO_NUM_NC) {
/* Register touch interrupt callback */
ret = esp_lcd_touch_register_interrupt_callback(touch_ctx->handle, lvgl_port_touch_interrupt_callback);
ret = esp_lcd_touch_register_interrupt_callback_with_data(touch_ctx->handle, lvgl_port_touch_interrupt_callback, touch_ctx);
ESP_GOTO_ON_ERROR(ret, err, TAG, "Error in register touch interrupt.");
}

lvgl_port_lock(0);
/* Register a touchpad input device */
indev = lv_indev_create();
lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER);
lv_indev_set_mode(indev, LV_INDEV_MODE_EVENT);
lv_indev_set_read_cb(indev, lvgl_port_touchpad_read);
lv_indev_set_disp(indev, touch_cfg->disp);
lv_indev_set_user_data(indev, touch_ctx);
Expand Down Expand Up @@ -126,8 +127,10 @@ static void lvgl_port_touchpad_read(lv_indev_t *indev_drv, lv_indev_data_t *data
}
}

static void lvgl_port_touch_interrupt_callback(esp_lcd_touch_handle_t tp)
static void IRAM_ATTR lvgl_port_touch_interrupt_callback(esp_lcd_touch_handle_t tp)
{
lvgl_port_touch_ctx_t *touch_ctx = (lvgl_port_touch_ctx_t *) tp->config.user_data;

/* Wake LVGL task, if needed */
lvgl_port_task_wake(LVGL_PORT_EVENT_TOUCH);
lvgl_port_task_wake(LVGL_PORT_EVENT_TOUCH, touch_ctx->indev);
}
6 changes: 4 additions & 2 deletions components/esp_lvgl_port/src/lvgl9/esp_lvgl_port_usbhid.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ lv_indev_t *lvgl_port_add_usb_hid_mouse_input(const lvgl_port_hid_mouse_cfg_t *m
/* Register a mouse input device */
indev = lv_indev_create();
lv_indev_set_type(indev, LV_INDEV_TYPE_POINTER);
lv_indev_set_mode(indev, LV_INDEV_MODE_EVENT);
lv_indev_set_read_cb(indev, lvgl_port_usb_hid_read_mouse);
lv_indev_set_disp(indev, mouse_cfg->disp);
lv_indev_set_user_data(indev, hid_ctx);
Expand Down Expand Up @@ -127,6 +128,7 @@ lv_indev_t *lvgl_port_add_usb_hid_keyboard_input(const lvgl_port_hid_keyboard_cf
/* Register a mouse input device */
indev = lv_indev_create();
lv_indev_set_type(indev, LV_INDEV_TYPE_KEYPAD);
lv_indev_set_mode(indev, LV_INDEV_MODE_EVENT);
lv_indev_set_read_cb(indev, lvgl_port_usb_hid_read_kb);
lv_indev_set_disp(indev, keyboard_cfg->disp);
lv_indev_set_user_data(indev, hid_ctx);
Expand Down Expand Up @@ -329,7 +331,7 @@ static void lvgl_port_usb_hid_host_interface_callback(hid_host_device_handle_t h
}

/* Wake LVGL task, if needed */
lvgl_port_task_wake(LVGL_PORT_EVENT_TOUCH);
lvgl_port_task_wake(LVGL_PORT_EVENT_TOUCH, hid_ctx->kb.indev);
} else if (dev.proto == HID_PROTOCOL_MOUSE) {
hid_mouse_input_report_boot_t *mouse = (hid_mouse_input_report_boot_t *)data;
if (data_length < sizeof(hid_mouse_input_report_boot_t)) {
Expand All @@ -340,7 +342,7 @@ static void lvgl_port_usb_hid_host_interface_callback(hid_host_device_handle_t h
hid_ctx->mouse.y += mouse->y_displacement;

/* Wake LVGL task, if needed */
lvgl_port_task_wake(LVGL_PORT_EVENT_TOUCH);
lvgl_port_task_wake(LVGL_PORT_EVENT_TOUCH, hid_ctx->mouse.indev);
}
break;
case HID_HOST_INTERFACE_EVENT_TRANSFER_ERROR:
Expand Down

0 comments on commit 828d57b

Please sign in to comment.