From ef6ccddc0d1bbe61d8c8331ed7eb933f284c8ef7 Mon Sep 17 00:00:00 2001 From: guillempages Date: Thu, 17 Oct 2024 22:23:37 +0200 Subject: [PATCH] [lvgl] Allow esphome::Image in lambda to update image source directly (#7624) --- esphome/components/lvgl/lvgl_esphome.h | 11 +++++++++++ tests/components/lvgl/common.yaml | 5 +++++ tests/components/lvgl/lvgl-package.yaml | 1 + 3 files changed, 17 insertions(+) diff --git a/esphome/components/lvgl/lvgl_esphome.h b/esphome/components/lvgl/lvgl_esphome.h index b8c0f5738e..f357c4950c 100644 --- a/esphome/components/lvgl/lvgl_esphome.h +++ b/esphome/components/lvgl/lvgl_esphome.h @@ -4,6 +4,9 @@ #ifdef USE_BINARY_SENSOR #include "esphome/components/binary_sensor/binary_sensor.h" #endif // USE_BINARY_SENSOR +#ifdef USE_LVGL_IMAGE +#include "esphome/components/image/image.h" +#endif // USE_LVGL_IMAGE #ifdef USE_LVGL_ROTARY_ENCODER #include "esphome/components/rotary_encoder/rotary_encoder.h" #endif // USE_LVGL_ROTARY_ENCODER @@ -47,6 +50,14 @@ static const display::ColorBitness LV_BITNESS = display::ColorBitness::COLOR_BIT static const display::ColorBitness LV_BITNESS = display::ColorBitness::COLOR_BITNESS_332; #endif // LV_COLOR_DEPTH +#ifdef USE_LVGL_IMAGE +// Shortcut / overload, so that the source of an image can easily be updated +// from within a lambda. +inline void lv_img_set_src(lv_obj_t *obj, esphome::image::Image *image) { + lv_img_set_src(obj, image->get_lv_img_dsc()); +} +#endif // USE_LVGL_IMAGE + // Parent class for things that wrap an LVGL object class LvCompound { public: diff --git a/tests/components/lvgl/common.yaml b/tests/components/lvgl/common.yaml index ad935ae563..5dcf30e0c1 100644 --- a/tests/components/lvgl/common.yaml +++ b/tests/components/lvgl/common.yaml @@ -127,6 +127,11 @@ binary_sensor: - platform: lvgl name: LVGL checkbox widget: checkbox_id + on_state: + then: + - lvgl.image.update: + id: lv_image + src: !lambda if (x) return id(cat_image); else return id(dog_image); wifi: ssid: SSID diff --git a/tests/components/lvgl/lvgl-package.yaml b/tests/components/lvgl/lvgl-package.yaml index 8d515280c9..cef76396c2 100644 --- a/tests/components/lvgl/lvgl-package.yaml +++ b/tests/components/lvgl/lvgl-package.yaml @@ -419,6 +419,7 @@ lvgl: spin_time: 2s align: left_mid - image: + id: lv_image src: cat_image align: top_left y: 50