diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md
index 3bf9c4e1f6..5703d39be1 100644
--- a/.github/PULL_REQUEST_TEMPLATE.md
+++ b/.github/PULL_REQUEST_TEMPLATE.md
@@ -7,11 +7,16 @@
- [ ] Bugfix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
+- [ ] Code quality improvements to existing code or addition of tests
- [ ] Other
-**Related issue or feature (if applicable):** fixes
+**Related issue or feature (if applicable):**
-**Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):** esphome/esphome-docs#
+- fixes
+
+**Pull request in [esphome-docs](https://github.com/esphome/esphome-docs) with documentation (if applicable):**
+
+- esphome/esphome-docs#
## Test Environment
@@ -23,12 +28,6 @@
- [ ] RTL87xx
## Example entry for `config.yaml`:
-
```yaml
# Example config.yaml
diff --git a/.github/actions/restore-python/action.yml b/.github/actions/restore-python/action.yml
index 1f5812691e..c6978f68c5 100644
--- a/.github/actions/restore-python/action.yml
+++ b/.github/actions/restore-python/action.yml
@@ -22,7 +22,7 @@ runs:
python-version: ${{ inputs.python-version }}
- name: Restore Python virtual environment
id: cache-venv
- uses: actions/cache/restore@v4.1.1
+ uses: actions/cache/restore@v4.1.2
with:
path: venv
# yamllint disable-line rule:line-length
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 178b914a1c..0d2f1c877d 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -46,7 +46,7 @@ jobs:
python-version: ${{ env.DEFAULT_PYTHON }}
- name: Restore Python virtual environment
id: cache-venv
- uses: actions/cache@v4.1.1
+ uses: actions/cache@v4.1.2
with:
path: venv
# yamllint disable-line rule:line-length
@@ -302,14 +302,14 @@ jobs:
- name: Cache platformio
if: github.ref == 'refs/heads/dev'
- uses: actions/cache@v4.1.1
+ uses: actions/cache@v4.1.2
with:
path: ~/.platformio
key: platformio-${{ matrix.pio_cache_key }}
- name: Cache platformio
if: github.ref != 'refs/heads/dev'
- uses: actions/cache/restore@v4.1.1
+ uses: actions/cache/restore@v4.1.2
with:
path: ~/.platformio
key: platformio-${{ matrix.pio_cache_key }}
diff --git a/CODEOWNERS b/CODEOWNERS
index d6104c9345..f96e43d5b7 100644
--- a/CODEOWNERS
+++ b/CODEOWNERS
@@ -48,7 +48,9 @@ esphome/components/at581x/* @X-Ryl669
esphome/components/atc_mithermometer/* @ahpohl
esphome/components/atm90e26/* @danieltwagner
esphome/components/atm90e32/* @circuitsetup @descipher
+esphome/components/audio/* @kahrendt
esphome/components/audio_dac/* @kbx81
+esphome/components/axs15231/* @clydebarrow
esphome/components/b_parasite/* @rbaron
esphome/components/ballu/* @bazuchan
esphome/components/bang_bang/* @OttoWinter
@@ -196,10 +198,11 @@ esphome/components/htu31d/* @betterengineering
esphome/components/hydreon_rgxx/* @functionpointer
esphome/components/hyt271/* @Philippe12
esphome/components/i2c/* @esphome/core
+esphome/components/i2c_device/* @gabest11
esphome/components/i2s_audio/* @jesserockz
esphome/components/i2s_audio/media_player/* @jesserockz
esphome/components/i2s_audio/microphone/* @jesserockz
-esphome/components/i2s_audio/speaker/* @jesserockz
+esphome/components/i2s_audio/speaker/* @jesserockz @kahrendt
esphome/components/iaqcore/* @yozik04
esphome/components/ili9xxx/* @clydebarrow @nielsnl68
esphome/components/improv_base/* @esphome/core
@@ -374,7 +377,7 @@ esphome/components/smt100/* @piechade
esphome/components/sn74hc165/* @jesserockz
esphome/components/socket/* @esphome/core
esphome/components/sonoff_d1/* @anatoly-savchenkov
-esphome/components/speaker/* @jesserockz
+esphome/components/speaker/* @jesserockz @kahrendt
esphome/components/spi/* @clydebarrow @esphome/core
esphome/components/spi_device/* @clydebarrow
esphome/components/spi_led_strip/* @clydebarrow
diff --git a/esphome/components/audio/__init__.py b/esphome/components/audio/__init__.py
new file mode 100644
index 0000000000..4ffdc401dc
--- /dev/null
+++ b/esphome/components/audio/__init__.py
@@ -0,0 +1,9 @@
+import esphome.codegen as cg
+import esphome.config_validation as cv
+
+CODEOWNERS = ["@kahrendt"]
+audio_ns = cg.esphome_ns.namespace("audio")
+
+CONFIG_SCHEMA = cv.All(
+ cv.Schema({}),
+)
diff --git a/esphome/components/audio/audio.h b/esphome/components/audio/audio.h
new file mode 100644
index 0000000000..b0968dc8da
--- /dev/null
+++ b/esphome/components/audio/audio.h
@@ -0,0 +1,21 @@
+#pragma once
+
+#include
+#include
+
+namespace esphome {
+namespace audio {
+
+struct AudioStreamInfo {
+ bool operator==(const AudioStreamInfo &rhs) const {
+ return (channels == rhs.channels) && (bits_per_sample == rhs.bits_per_sample) && (sample_rate == rhs.sample_rate);
+ }
+ bool operator!=(const AudioStreamInfo &rhs) const { return !operator==(rhs); }
+ size_t get_bytes_per_sample() const { return bits_per_sample / 8; }
+ uint8_t channels = 1;
+ uint8_t bits_per_sample = 16;
+ uint32_t sample_rate = 16000;
+};
+
+} // namespace audio
+} // namespace esphome
diff --git a/esphome/components/axs15231/__init__.py b/esphome/components/axs15231/__init__.py
new file mode 100644
index 0000000000..3246dbed24
--- /dev/null
+++ b/esphome/components/axs15231/__init__.py
@@ -0,0 +1,6 @@
+import esphome.codegen as cg
+
+CODEOWNERS = ["@clydebarrow"]
+DEPENDENCIES = ["i2c"]
+
+axs15231_ns = cg.esphome_ns.namespace("axs15231")
diff --git a/esphome/components/axs15231/touchscreen/__init__.py b/esphome/components/axs15231/touchscreen/__init__.py
new file mode 100644
index 0000000000..8c18d8ca75
--- /dev/null
+++ b/esphome/components/axs15231/touchscreen/__init__.py
@@ -0,0 +1,36 @@
+from esphome import pins
+import esphome.codegen as cg
+from esphome.components import i2c, touchscreen
+import esphome.config_validation as cv
+from esphome.const import CONF_ID, CONF_INTERRUPT_PIN, CONF_RESET_PIN
+
+from .. import axs15231_ns
+
+AXS15231Touchscreen = axs15231_ns.class_(
+ "AXS15231Touchscreen",
+ touchscreen.Touchscreen,
+ i2c.I2CDevice,
+)
+
+CONFIG_SCHEMA = (
+ touchscreen.touchscreen_schema("50ms")
+ .extend(
+ {
+ cv.GenerateID(): cv.declare_id(AXS15231Touchscreen),
+ cv.Optional(CONF_INTERRUPT_PIN): pins.internal_gpio_input_pin_schema,
+ cv.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema,
+ }
+ )
+ .extend(i2c.i2c_device_schema(0x3B))
+)
+
+
+async def to_code(config):
+ var = cg.new_Pvariable(config[CONF_ID])
+ await touchscreen.register_touchscreen(var, config)
+ await i2c.register_i2c_device(var, config)
+
+ if interrupt_pin := config.get(CONF_INTERRUPT_PIN):
+ cg.add(var.set_interrupt_pin(await cg.gpio_pin_expression(interrupt_pin)))
+ if reset_pin := config.get(CONF_RESET_PIN):
+ cg.add(var.set_reset_pin(await cg.gpio_pin_expression(reset_pin)))
diff --git a/esphome/components/axs15231/touchscreen/axs15231_touchscreen.cpp b/esphome/components/axs15231/touchscreen/axs15231_touchscreen.cpp
new file mode 100644
index 0000000000..54b39a6bb9
--- /dev/null
+++ b/esphome/components/axs15231/touchscreen/axs15231_touchscreen.cpp
@@ -0,0 +1,64 @@
+#include "axs15231_touchscreen.h"
+
+#include "esphome/core/helpers.h"
+#include "esphome/core/log.h"
+
+namespace esphome {
+namespace axs15231 {
+
+static const char *const TAG = "ax15231.touchscreen";
+
+constexpr static const uint8_t AXS_READ_TOUCHPAD[11] = {0xb5, 0xab, 0xa5, 0x5a, 0x0, 0x0, 0x0, 0x8};
+
+#define ERROR_CHECK(err) \
+ if ((err) != i2c::ERROR_OK) { \
+ this->status_set_warning("Failed to communicate"); \
+ return; \
+ }
+
+void AXS15231Touchscreen::setup() {
+ ESP_LOGCONFIG(TAG, "Setting up AXS15231 Touchscreen...");
+ if (this->reset_pin_ != nullptr) {
+ this->reset_pin_->setup();
+ this->reset_pin_->digital_write(false);
+ delay(5);
+ this->reset_pin_->digital_write(true);
+ delay(10);
+ }
+ if (this->interrupt_pin_ != nullptr) {
+ this->interrupt_pin_->pin_mode(gpio::FLAG_INPUT);
+ this->interrupt_pin_->setup();
+ this->attach_interrupt_(this->interrupt_pin_, gpio::INTERRUPT_FALLING_EDGE);
+ }
+ this->x_raw_max_ = this->display_->get_native_width();
+ this->y_raw_max_ = this->display_->get_native_height();
+ ESP_LOGCONFIG(TAG, "AXS15231 Touchscreen setup complete");
+}
+
+void AXS15231Touchscreen::update_touches() {
+ i2c::ErrorCode err;
+ uint8_t data[8]{};
+
+ err = this->write(AXS_READ_TOUCHPAD, sizeof(AXS_READ_TOUCHPAD), false);
+ ERROR_CHECK(err);
+ err = this->read(data, sizeof(data));
+ ERROR_CHECK(err);
+ this->status_clear_warning();
+ if (data[0] != 0) // no touches
+ return;
+ uint16_t x = encode_uint16(data[2] & 0xF, data[3]);
+ uint16_t y = encode_uint16(data[4] & 0xF, data[5]);
+ this->add_raw_touch_position_(0, x, y);
+}
+
+void AXS15231Touchscreen::dump_config() {
+ ESP_LOGCONFIG(TAG, "AXS15231 Touchscreen:");
+ LOG_I2C_DEVICE(this);
+ LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
+ LOG_PIN(" Reset Pin: ", this->reset_pin_);
+ ESP_LOGCONFIG(TAG, " Width: %d", this->x_raw_max_);
+ ESP_LOGCONFIG(TAG, " Height: %d", this->y_raw_max_);
+}
+
+} // namespace axs15231
+} // namespace esphome
diff --git a/esphome/components/axs15231/touchscreen/axs15231_touchscreen.h b/esphome/components/axs15231/touchscreen/axs15231_touchscreen.h
new file mode 100644
index 0000000000..a55c5c0d32
--- /dev/null
+++ b/esphome/components/axs15231/touchscreen/axs15231_touchscreen.h
@@ -0,0 +1,27 @@
+#pragma once
+
+#include "esphome/components/i2c/i2c.h"
+#include "esphome/components/touchscreen/touchscreen.h"
+#include "esphome/core/component.h"
+#include "esphome/core/hal.h"
+
+namespace esphome {
+namespace axs15231 {
+
+class AXS15231Touchscreen : public touchscreen::Touchscreen, public i2c::I2CDevice {
+ public:
+ void setup() override;
+ void dump_config() override;
+
+ void set_interrupt_pin(InternalGPIOPin *pin) { this->interrupt_pin_ = pin; }
+ void set_reset_pin(GPIOPin *pin) { this->reset_pin_ = pin; }
+
+ protected:
+ void update_touches() override;
+
+ InternalGPIOPin *interrupt_pin_{};
+ GPIOPin *reset_pin_{};
+};
+
+} // namespace axs15231
+} // namespace esphome
diff --git a/esphome/components/ble_rssi/sensor.py b/esphome/components/ble_rssi/sensor.py
index e3ba1abfd7..c4e767aa21 100644
--- a/esphome/components/ble_rssi/sensor.py
+++ b/esphome/components/ble_rssi/sensor.py
@@ -45,7 +45,7 @@ CONFIG_SCHEMA = cv.All(
cv.Optional(CONF_SERVICE_UUID): esp32_ble_tracker.bt_uuid,
cv.Optional(CONF_IBEACON_MAJOR): cv.uint16_t,
cv.Optional(CONF_IBEACON_MINOR): cv.uint16_t,
- cv.Optional(CONF_IBEACON_UUID): cv.uuid,
+ cv.Optional(CONF_IBEACON_UUID): esp32_ble_tracker.bt_uuid,
}
)
.extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
@@ -79,7 +79,7 @@ async def to_code(config):
cg.add(var.set_service_uuid128(uuid128))
if ibeacon_uuid := config.get(CONF_IBEACON_UUID):
- ibeacon_uuid = esp32_ble_tracker.as_hex_array(str(ibeacon_uuid))
+ ibeacon_uuid = esp32_ble_tracker.as_reversed_hex_array(ibeacon_uuid)
cg.add(var.set_ibeacon_uuid(ibeacon_uuid))
if (ibeacon_major := config.get(CONF_IBEACON_MAJOR)) is not None:
diff --git a/esphome/components/bme68x_bsec2/__init__.py b/esphome/components/bme68x_bsec2/__init__.py
index 1930c7c9e3..d6dbb52f18 100644
--- a/esphome/components/bme68x_bsec2/__init__.py
+++ b/esphome/components/bme68x_bsec2/__init__.py
@@ -16,7 +16,7 @@ CODEOWNERS = ["@neffs", "@kbx81"]
DOMAIN = "bme68x_bsec2"
-BSEC2_LIBRARY_VERSION = "v1.7.2502"
+BSEC2_LIBRARY_VERSION = "v1.8.2610"
CONF_ALGORITHM_OUTPUT = "algorithm_output"
CONF_BME68X_BSEC2_ID = "bme68x_bsec2_id"
diff --git a/esphome/components/esp32/__init__.py b/esphome/components/esp32/__init__.py
index 8a73f2020d..61fbb53e3a 100644
--- a/esphome/components/esp32/__init__.py
+++ b/esphome/components/esp32/__init__.py
@@ -395,6 +395,13 @@ ARDUINO_FRAMEWORK_SCHEMA = cv.All(
cv.Optional(CONF_VERSION, default="recommended"): cv.string_strict,
cv.Optional(CONF_SOURCE): cv.string_strict,
cv.Optional(CONF_PLATFORM_VERSION): _parse_platform_version,
+ cv.Optional(CONF_ADVANCED, default={}): cv.Schema(
+ {
+ cv.Optional(
+ CONF_IGNORE_EFUSE_CUSTOM_MAC, default=False
+ ): cv.boolean,
+ }
+ ),
}
),
_arduino_check_versions,
@@ -494,6 +501,9 @@ async def to_code(config):
conf = config[CONF_FRAMEWORK]
cg.add_platformio_option("platform", conf[CONF_PLATFORM_VERSION])
+ if CONF_ADVANCED in conf and conf[CONF_ADVANCED][CONF_IGNORE_EFUSE_CUSTOM_MAC]:
+ cg.add_define("USE_ESP32_IGNORE_EFUSE_CUSTOM_MAC")
+
add_extra_script(
"post",
"post_build.py",
@@ -540,8 +550,6 @@ async def to_code(config):
for name, value in conf[CONF_SDKCONFIG_OPTIONS].items():
add_idf_sdkconfig_option(name, RawSdkconfigValue(value))
- if conf[CONF_ADVANCED][CONF_IGNORE_EFUSE_CUSTOM_MAC]:
- cg.add_define("USE_ESP32_IGNORE_EFUSE_CUSTOM_MAC")
if conf[CONF_ADVANCED].get(CONF_IGNORE_EFUSE_MAC_CRC):
add_idf_sdkconfig_option("CONFIG_ESP_MAC_IGNORE_MAC_CRC_ERROR", True)
if (framework_ver.major, framework_ver.minor) >= (4, 4):
diff --git a/esphome/components/esp32/boards.py b/esphome/components/esp32/boards.py
index 60abcd447c..02744ecb6f 100644
--- a/esphome/components/esp32/boards.py
+++ b/esphome/components/esp32/boards.py
@@ -103,6 +103,173 @@ ESP32_BOARD_PINS = {
"LED": 13,
"LED_BUILTIN": 13,
},
+ "adafruit_feather_esp32s3": {
+ "BUTTON": 0,
+ "A0": 18,
+ "A1": 17,
+ "A2": 16,
+ "A3": 15,
+ "A4": 14,
+ "A5": 8,
+ "SCK": 36,
+ "MOSI": 35,
+ "MISO": 37,
+ "RX": 38,
+ "TX": 39,
+ "SCL": 4,
+ "SDA": 3,
+ "NEOPIXEL": 33,
+ "PIN_NEOPIXEL": 33,
+ "NEOPIXEL_POWER": 21,
+ "I2C_POWER": 7,
+ "LED": 13,
+ "LED_BUILTIN": 13,
+ },
+ "adafruit_feather_esp32s3_nopsram": {
+ "BUTTON": 0,
+ "A0": 18,
+ "A1": 17,
+ "A2": 16,
+ "A3": 15,
+ "A4": 14,
+ "A5": 8,
+ "SCK": 36,
+ "MOSI": 35,
+ "MISO": 37,
+ "RX": 38,
+ "TX": 39,
+ "SCL": 4,
+ "SDA": 3,
+ "NEOPIXEL": 33,
+ "PIN_NEOPIXEL": 33,
+ "NEOPIXEL_POWER": 21,
+ "I2C_POWER": 7,
+ "LED": 13,
+ "LED_BUILTIN": 13,
+ },
+ "adafruit_feather_esp32s3_tft": {
+ "BUTTON": 0,
+ "A0": 18,
+ "A1": 17,
+ "A2": 16,
+ "A3": 15,
+ "A4": 14,
+ "A5": 8,
+ "SCK": 36,
+ "MOSI": 35,
+ "MISO": 37,
+ "RX": 2,
+ "TX": 1,
+ "SCL": 41,
+ "SDA": 42,
+ "NEOPIXEL": 33,
+ "PIN_NEOPIXEL": 33,
+ "NEOPIXEL_POWER": 34,
+ "TFT_I2C_POWER": 21,
+ "TFT_CS": 7,
+ "TFT_DC": 39,
+ "TFT_RESET": 40,
+ "TFT_BACKLIGHT": 45,
+ "LED": 13,
+ "LED_BUILTIN": 13,
+ },
+ "adafruit_funhouse_esp32s2": {
+ "BUTTON_UP": 5,
+ "BUTTON_DOWN": 3,
+ "BUTTON_SELECT": 4,
+ "DOTSTAR_DATA": 14,
+ "DOTSTAR_CLOCK": 15,
+ "PIR_SENSE": 16,
+ "A0": 17,
+ "A1": 2,
+ "A2": 1,
+ "CAP6": 6,
+ "CAP7": 7,
+ "CAP8": 8,
+ "CAP9": 9,
+ "CAP10": 10,
+ "CAP11": 11,
+ "CAP12": 12,
+ "CAP13": 13,
+ "SPEAKER": 42,
+ "LED": 37,
+ "LIGHT": 18,
+ "TFT_MOSI": 35,
+ "TFT_SCK": 36,
+ "TFT_CS": 40,
+ "TFT_DC": 39,
+ "TFT_RESET": 41,
+ "TFT_BACKLIGHT": 21,
+ "RED_LED": 31,
+ "BUTTON": 0,
+ },
+ "adafruit_itsybitsy_esp32": {
+ "A0": 25,
+ "A1": 26,
+ "A2": 4,
+ "A3": 38,
+ "A4": 37,
+ "A5": 36,
+ "SCK": 19,
+ "MOSI": 21,
+ "MISO": 22,
+ "SCL": 27,
+ "SDA": 15,
+ "TX": 20,
+ "RX": 8,
+ "NEOPIXEL": 0,
+ "PIN_NEOPIXEL": 0,
+ "NEOPIXEL_POWER": 2,
+ "BUTTON": 35,
+ },
+ "adafruit_magtag29_esp32s2": {
+ "A1": 18,
+ "BUTTON_A": 15,
+ "BUTTON_B": 14,
+ "BUTTON_C": 12,
+ "BUTTON_D": 11,
+ "SDA": 33,
+ "SCL": 34,
+ "SPEAKER": 17,
+ "SPEAKER_ENABLE": 16,
+ "VOLTAGE_MONITOR": 4,
+ "ACCELEROMETER_INT": 9,
+ "ACCELEROMETER_INTERRUPT": 9,
+ "LIGHT": 3,
+ "NEOPIXEL": 1,
+ "PIN_NEOPIXEL": 1,
+ "NEOPIXEL_POWER": 21,
+ "EPD_BUSY": 5,
+ "EPD_RESET": 6,
+ "EPD_DC": 7,
+ "EPD_CS": 8,
+ "EPD_MOSI": 35,
+ "EPD_SCK": 36,
+ "EPD_MISO": 37,
+ "BUTTON": 0,
+ "LED": 13,
+ "LED_BUILTIN": 13,
+ },
+ "adafruit_metro_esp32s2": {
+ "A0": 17,
+ "A1": 18,
+ "A2": 1,
+ "A3": 2,
+ "A4": 3,
+ "A5": 4,
+ "RX": 38,
+ "TX": 37,
+ "SCL": 34,
+ "SDA": 33,
+ "MISO": 37,
+ "SCK": 36,
+ "MOSI": 35,
+ "NEOPIXEL": 45,
+ "PIN_NEOPIXEL": 45,
+ "LED": 42,
+ "LED_BUILTIN": 42,
+ "BUTTON": 0,
+ },
"adafruit_qtpy_esp32c3": {
"A0": 4,
"A1": 3,
@@ -141,6 +308,26 @@ ESP32_BOARD_PINS = {
"BUTTON": 0,
"SWITCH": 0,
},
+ "adafruit_qtpy_esp32s3_nopsram": {
+ "A0": 18,
+ "A1": 17,
+ "A2": 9,
+ "A3": 8,
+ "SDA": 7,
+ "SCL": 6,
+ "MOSI": 35,
+ "MISO": 37,
+ "SCK": 36,
+ "RX": 16,
+ "TX": 5,
+ "SDA1": 41,
+ "SCL1": 40,
+ "NEOPIXEL": 39,
+ "PIN_NEOPIXEL": 39,
+ "NEOPIXEL_POWER": 38,
+ "BUTTON": 0,
+ "SWITCH": 0,
+ },
"adafruit_qtpy_esp32": {
"A0": 26,
"A1": 25,
@@ -1068,7 +1255,18 @@ ESP32_BOARD_PINS = {
"_VBAT": 35,
},
"wemosbat": {"LED": 16},
- "wesp32": {"MISO": 32, "SCL": 4, "SDA": 15},
+ "wesp32": {
+ "MISO": 32,
+ "MOSI": 23,
+ "SCK": 18,
+ "SCL": 4,
+ "SDA": 15,
+ "MISO1": 12,
+ "MOSI1": 13,
+ "SCK1": 14,
+ "SCL1": 5,
+ "SDA1": 33,
+ },
"widora-air": {
"A1": 39,
"A2": 35,
diff --git a/esphome/components/esp32_ble/const_esp32c6.h b/esphome/components/esp32_ble/const_esp32c6.h
index 69f9adcf6b..89179d8dd9 100644
--- a/esphome/components/esp32_ble/const_esp32c6.h
+++ b/esphome/components/esp32_ble/const_esp32c6.h
@@ -40,6 +40,9 @@ static const esp_bt_controller_config_t BT_CONTROLLER_CONFIG = {
.controller_run_cpu = 0,
.enable_qa_test = RUN_QA_TEST,
.enable_bqb_test = RUN_BQB_TEST,
+#if ESP_IDF_VERSION < ESP_IDF_VERSION_VAL(5, 3, 1)
+ // The following fields have been removed since ESP IDF version 5.3.1, see commit:
+ // https://github.com/espressif/esp-idf/commit/e761c1de8f9c0777829d597b4d5a33bb070a30a8
.enable_uart_hci = HCI_UART_EN,
.ble_hci_uart_port = DEFAULT_BT_LE_HCI_UART_PORT,
.ble_hci_uart_baud = DEFAULT_BT_LE_HCI_UART_BAUD,
@@ -47,6 +50,7 @@ static const esp_bt_controller_config_t BT_CONTROLLER_CONFIG = {
.ble_hci_uart_stop_bits = DEFAULT_BT_LE_HCI_UART_STOP_BITS,
.ble_hci_uart_flow_ctrl = DEFAULT_BT_LE_HCI_UART_FLOW_CTRL,
.ble_hci_uart_uart_parity = DEFAULT_BT_LE_HCI_UART_PARITY,
+#endif
.enable_tx_cca = DEFAULT_BT_LE_TX_CCA_ENABLED,
.cca_rssi_thresh = 256 - DEFAULT_BT_LE_CCA_RSSI_THRESH,
.sleep_en = NIMBLE_SLEEP_ENABLE,
@@ -58,6 +62,9 @@ static const esp_bt_controller_config_t BT_CONTROLLER_CONFIG = {
.cpu_freq_mhz = CONFIG_ESP_DEFAULT_CPU_FREQ_MHZ,
.ignore_wl_for_direct_adv = 0,
.enable_pcl = DEFAULT_BT_LE_POWER_CONTROL_ENABLED,
+#if ESP_IDF_VERSION >= ESP_IDF_VERSION_VAL(5, 1, 3)
+ .csa2_select = DEFAULT_BT_LE_50_FEATURE_SUPPORT,
+#endif
.config_magic = CONFIG_MAGIC,
};
diff --git a/esphome/components/host/__init__.py b/esphome/components/host/__init__.py
index e83bf2dba8..eb8cfbd984 100644
--- a/esphome/components/host/__init__.py
+++ b/esphome/components/host/__init__.py
@@ -16,7 +16,7 @@ from .const import KEY_HOST
from .gpio import host_pin_to_code # noqa
CODEOWNERS = ["@esphome/core", "@clydebarrow"]
-AUTO_LOAD = ["network"]
+AUTO_LOAD = ["network", "preferences"]
def set_core_data(config):
diff --git a/esphome/components/i2c_device/__init__.py b/esphome/components/i2c_device/__init__.py
new file mode 100644
index 0000000000..e145ba56f8
--- /dev/null
+++ b/esphome/components/i2c_device/__init__.py
@@ -0,0 +1,26 @@
+import esphome.codegen as cg
+import esphome.config_validation as cv
+from esphome.components import i2c
+from esphome.const import CONF_ID
+
+DEPENDENCIES = ["i2c"]
+CODEOWNERS = ["@gabest11"]
+MULTI_CONF = True
+
+i2c_device_ns = cg.esphome_ns.namespace("i2c_device")
+
+I2CDeviceComponent = i2c_device_ns.class_(
+ "I2CDeviceComponent", cg.Component, i2c.I2CDevice
+)
+
+CONFIG_SCHEMA = cv.Schema(
+ {
+ cv.GenerateID(CONF_ID): cv.declare_id(I2CDeviceComponent),
+ }
+).extend(i2c.i2c_device_schema(None))
+
+
+async def to_code(config):
+ var = cg.new_Pvariable(config[CONF_ID])
+ await cg.register_component(var, config)
+ await i2c.register_i2c_device(var, config)
diff --git a/esphome/components/i2c_device/i2c_device.cpp b/esphome/components/i2c_device/i2c_device.cpp
new file mode 100644
index 0000000000..455c68fbed
--- /dev/null
+++ b/esphome/components/i2c_device/i2c_device.cpp
@@ -0,0 +1,17 @@
+#include "i2c_device.h"
+#include "esphome/core/log.h"
+#include "esphome/core/hal.h"
+#include
+
+namespace esphome {
+namespace i2c_device {
+
+static const char *const TAG = "i2c_device";
+
+void I2CDeviceComponent::dump_config() {
+ ESP_LOGCONFIG(TAG, "I2CDevice");
+ LOG_I2C_DEVICE(this);
+}
+
+} // namespace i2c_device
+} // namespace esphome
diff --git a/esphome/components/i2c_device/i2c_device.h b/esphome/components/i2c_device/i2c_device.h
new file mode 100644
index 0000000000..ab118e3e89
--- /dev/null
+++ b/esphome/components/i2c_device/i2c_device.h
@@ -0,0 +1,18 @@
+#pragma once
+
+#include "esphome/core/component.h"
+#include "esphome/components/i2c/i2c.h"
+
+namespace esphome {
+namespace i2c_device {
+
+class I2CDeviceComponent : public Component, public i2c::I2CDevice {
+ public:
+ void dump_config() override;
+ float get_setup_priority() const override { return setup_priority::DATA; }
+
+ protected:
+};
+
+} // namespace i2c_device
+} // namespace esphome
diff --git a/esphome/components/i2s_audio/speaker/__init__.py b/esphome/components/i2s_audio/speaker/__init__.py
index bba886b39b..dd43d6cb39 100644
--- a/esphome/components/i2s_audio/speaker/__init__.py
+++ b/esphome/components/i2s_audio/speaker/__init__.py
@@ -16,7 +16,8 @@ from .. import (
register_i2s_audio_component,
)
-CODEOWNERS = ["@jesserockz"]
+AUTO_LOAD = ["audio"]
+CODEOWNERS = ["@jesserockz", "@kahrendt"]
DEPENDENCIES = ["i2s_audio"]
I2SAudioSpeaker = i2s_audio_ns.class_(
@@ -72,7 +73,7 @@ BASE_SCHEMA = (
.extend(
{
cv.Optional(
- CONF_TIMEOUT, default="100ms"
+ CONF_TIMEOUT, default="500ms"
): cv.positive_time_period_milliseconds,
}
)
diff --git a/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp b/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp
index 97c1d86c36..cf6c3bbbba 100644
--- a/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp
+++ b/esphome/components/i2s_audio/speaker/i2s_audio_speaker.cpp
@@ -4,6 +4,8 @@
#include
+#include "esphome/components/audio/audio.h"
+
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
@@ -11,186 +13,342 @@
namespace esphome {
namespace i2s_audio {
-static const size_t BUFFER_COUNT = 20;
+static const size_t DMA_BUFFER_SIZE = 512;
+static const size_t DMA_BUFFERS_COUNT = 4;
+static const size_t FRAMES_IN_ALL_DMA_BUFFERS = DMA_BUFFER_SIZE * DMA_BUFFERS_COUNT;
+static const size_t RING_BUFFER_SAMPLES = 8192;
+static const size_t TASK_DELAY_MS = 10;
+static const size_t TASK_STACK_SIZE = 4096;
+static const ssize_t TASK_PRIORITY = 23;
static const char *const TAG = "i2s_audio.speaker";
+enum SpeakerEventGroupBits : uint32_t {
+ COMMAND_START = (1 << 0), // Starts the main task purpose
+ COMMAND_STOP = (1 << 1), // stops the main task
+ COMMAND_STOP_GRACEFULLY = (1 << 2), // Stops the task once all data has been written
+ MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE = (1 << 5), // Locks the ring buffer when not set
+ STATE_STARTING = (1 << 10),
+ STATE_RUNNING = (1 << 11),
+ STATE_STOPPING = (1 << 12),
+ STATE_STOPPED = (1 << 13),
+ ERR_INVALID_FORMAT = (1 << 14),
+ ERR_TASK_FAILED_TO_START = (1 << 15),
+ ERR_ESP_INVALID_STATE = (1 << 16),
+ ERR_ESP_INVALID_ARG = (1 << 17),
+ ERR_ESP_INVALID_SIZE = (1 << 18),
+ ERR_ESP_NO_MEM = (1 << 19),
+ ERR_ESP_FAIL = (1 << 20),
+ ALL_ERR_ESP_BITS = ERR_ESP_INVALID_STATE | ERR_ESP_INVALID_ARG | ERR_ESP_INVALID_SIZE | ERR_ESP_NO_MEM | ERR_ESP_FAIL,
+ ALL_BITS = 0x00FFFFFF, // All valid FreeRTOS event group bits
+};
+
+// Translates a SpeakerEventGroupBits ERR_ESP bit to the coressponding esp_err_t
+static esp_err_t err_bit_to_esp_err(uint32_t bit) {
+ switch (bit) {
+ case SpeakerEventGroupBits::ERR_ESP_INVALID_STATE:
+ return ESP_ERR_INVALID_STATE;
+ case SpeakerEventGroupBits::ERR_ESP_INVALID_ARG:
+ return ESP_ERR_INVALID_ARG;
+ case SpeakerEventGroupBits::ERR_ESP_INVALID_SIZE:
+ return ESP_ERR_INVALID_SIZE;
+ case SpeakerEventGroupBits::ERR_ESP_NO_MEM:
+ return ESP_ERR_NO_MEM;
+ default:
+ return ESP_FAIL;
+ }
+}
+
+/// @brief Multiplies the input array of Q15 numbers by a Q15 constant factor
+///
+/// Based on `dsps_mulc_s16_ansi` from the esp-dsp library:
+/// https://github.com/espressif/esp-dsp/blob/master/modules/math/mulc/fixed/dsps_mulc_s16_ansi.c
+/// (accessed on 2024-09-30).
+/// @param input Array of Q15 numbers
+/// @param output Array of Q15 numbers
+/// @param len Length of array
+/// @param c Q15 constant factor
+static void q15_multiplication(const int16_t *input, int16_t *output, size_t len, int16_t c) {
+ for (int i = 0; i < len; i++) {
+ int32_t acc = (int32_t) input[i] * (int32_t) c;
+ output[i] = (int16_t) (acc >> 15);
+ }
+}
+
+// Lists the Q15 fixed point scaling factor for volume reduction.
+// Has 100 values representing silence and a reduction [49, 48.5, ... 0.5, 0] dB.
+// dB to PCM scaling factor formula: floating_point_scale_factor = 2^(-db/6.014)
+// float to Q15 fixed point formula: q15_scale_factor = floating_point_scale_factor * 2^(15)
+static const std::vector Q15_VOLUME_SCALING_FACTORS = {
+ 0, 116, 122, 130, 137, 146, 154, 163, 173, 183, 194, 206, 218, 231, 244,
+ 259, 274, 291, 308, 326, 345, 366, 388, 411, 435, 461, 488, 517, 548, 580,
+ 615, 651, 690, 731, 774, 820, 868, 920, 974, 1032, 1094, 1158, 1227, 1300, 1377,
+ 1459, 1545, 1637, 1734, 1837, 1946, 2061, 2184, 2313, 2450, 2596, 2750, 2913, 3085, 3269,
+ 3462, 3668, 3885, 4116, 4360, 4619, 4893, 5183, 5490, 5816, 6161, 6527, 6914, 7324, 7758,
+ 8218, 8706, 9222, 9770, 10349, 10963, 11613, 12302, 13032, 13805, 14624, 15491, 16410, 17384, 18415,
+ 19508, 20665, 21891, 23189, 24565, 26022, 27566, 29201, 30933, 32767};
+
void I2SAudioSpeaker::setup() {
ESP_LOGCONFIG(TAG, "Setting up I2S Audio Speaker...");
- this->buffer_queue_ = xQueueCreate(BUFFER_COUNT, sizeof(DataEvent));
- if (this->buffer_queue_ == nullptr) {
- ESP_LOGE(TAG, "Failed to create buffer queue");
- this->mark_failed();
- return;
+ if (this->event_group_ == nullptr) {
+ this->event_group_ = xEventGroupCreate();
}
- this->event_queue_ = xQueueCreate(BUFFER_COUNT, sizeof(TaskEvent));
- if (this->event_queue_ == nullptr) {
- ESP_LOGE(TAG, "Failed to create event queue");
+ if (this->event_group_ == nullptr) {
+ ESP_LOGE(TAG, "Failed to create event group");
this->mark_failed();
return;
}
}
+void I2SAudioSpeaker::loop() {
+ uint32_t event_group_bits = xEventGroupGetBits(this->event_group_);
+
+ if (event_group_bits & SpeakerEventGroupBits::STATE_STARTING) {
+ ESP_LOGD(TAG, "Starting Speaker");
+ this->state_ = speaker::STATE_STARTING;
+ xEventGroupClearBits(this->event_group_, SpeakerEventGroupBits::STATE_STARTING);
+ }
+ if (event_group_bits & SpeakerEventGroupBits::STATE_RUNNING) {
+ ESP_LOGD(TAG, "Started Speaker");
+ this->state_ = speaker::STATE_RUNNING;
+ xEventGroupClearBits(this->event_group_, SpeakerEventGroupBits::STATE_RUNNING);
+ this->status_clear_warning();
+ this->status_clear_error();
+ }
+ if (event_group_bits & SpeakerEventGroupBits::STATE_STOPPING) {
+ ESP_LOGD(TAG, "Stopping Speaker");
+ this->state_ = speaker::STATE_STOPPING;
+ xEventGroupClearBits(this->event_group_, SpeakerEventGroupBits::STATE_STOPPING);
+ }
+ if (event_group_bits & SpeakerEventGroupBits::STATE_STOPPED) {
+ if (!this->task_created_) {
+ ESP_LOGD(TAG, "Stopped Speaker");
+ this->state_ = speaker::STATE_STOPPED;
+ xEventGroupClearBits(this->event_group_, SpeakerEventGroupBits::ALL_BITS);
+ this->speaker_task_handle_ = nullptr;
+ }
+ }
+
+ if (event_group_bits & SpeakerEventGroupBits::ERR_TASK_FAILED_TO_START) {
+ this->status_set_error("Failed to start speaker task");
+ xEventGroupClearBits(this->event_group_, SpeakerEventGroupBits::ERR_TASK_FAILED_TO_START);
+ }
+
+ if (event_group_bits & SpeakerEventGroupBits::ERR_INVALID_FORMAT) {
+ this->status_set_error("Failed to adjust I2S bus to match the incoming audio");
+ ESP_LOGE(TAG,
+ "Incompatible audio format: sample rate = %" PRIu32 ", channels = %" PRIu8 ", bits per sample = %" PRIu8,
+ this->audio_stream_info_.sample_rate, this->audio_stream_info_.channels,
+ this->audio_stream_info_.bits_per_sample);
+ }
+
+ if (event_group_bits & SpeakerEventGroupBits::ALL_ERR_ESP_BITS) {
+ uint32_t error_bits = event_group_bits & SpeakerEventGroupBits::ALL_ERR_ESP_BITS;
+ ESP_LOGW(TAG, "Error writing to I2S: %s", esp_err_to_name(err_bit_to_esp_err(error_bits)));
+ this->status_set_warning();
+ }
+}
+
+void I2SAudioSpeaker::set_volume(float volume) {
+ this->volume_ = volume;
+#ifdef USE_AUDIO_DAC
+ if (this->audio_dac_ != nullptr) {
+ if (volume > 0.0) {
+ this->audio_dac_->set_mute_off();
+ }
+ this->audio_dac_->set_volume(volume);
+ } else
+#endif
+ {
+ // Fallback to software volume control by using a Q15 fixed point scaling factor
+ ssize_t decibel_index = remap(volume, 0.0f, 1.0f, 0, Q15_VOLUME_SCALING_FACTORS.size() - 1);
+ this->q15_volume_factor_ = Q15_VOLUME_SCALING_FACTORS[decibel_index];
+ }
+}
+
+void I2SAudioSpeaker::set_mute_state(bool mute_state) {
+ this->mute_state_ = mute_state;
+#ifdef USE_AUDIO_DAC
+ if (this->audio_dac_) {
+ if (mute_state) {
+ this->audio_dac_->set_mute_on();
+ } else {
+ this->audio_dac_->set_mute_off();
+ }
+ } else
+#endif
+ {
+ if (mute_state) {
+ // Fallback to software volume control and scale by 0
+ this->q15_volume_factor_ = 0;
+ } else {
+ // Revert to previous volume when unmuting
+ this->set_volume(this->volume_);
+ }
+ }
+}
+
+size_t I2SAudioSpeaker::play(const uint8_t *data, size_t length, TickType_t ticks_to_wait) {
+ if (this->is_failed()) {
+ ESP_LOGE(TAG, "Cannot play audio, speaker failed to setup");
+ return 0;
+ }
+ if (this->state_ != speaker::STATE_RUNNING && this->state_ != speaker::STATE_STARTING) {
+ this->start();
+ }
+
+ // Wait for the ring buffer to be available
+ uint32_t event_bits =
+ xEventGroupWaitBits(this->event_group_, SpeakerEventGroupBits::MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE, pdFALSE,
+ pdFALSE, pdMS_TO_TICKS(TASK_DELAY_MS));
+
+ if (event_bits & SpeakerEventGroupBits::MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE) {
+ // Ring buffer is available to write
+
+ // Lock the ring buffer, write to it, then unlock it
+ xEventGroupClearBits(this->event_group_, SpeakerEventGroupBits::MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE);
+ size_t bytes_written = this->audio_ring_buffer_->write_without_replacement((void *) data, length, ticks_to_wait);
+ xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE);
+
+ return bytes_written;
+ }
+
+ return 0;
+}
+
+bool I2SAudioSpeaker::has_buffered_data() const {
+ if (this->audio_ring_buffer_ != nullptr) {
+ return this->audio_ring_buffer_->available() > 0;
+ }
+ return false;
+}
+
+void I2SAudioSpeaker::speaker_task(void *params) {
+ I2SAudioSpeaker *this_speaker = (I2SAudioSpeaker *) params;
+ uint32_t event_group_bits =
+ xEventGroupWaitBits(this_speaker->event_group_,
+ SpeakerEventGroupBits::COMMAND_START | SpeakerEventGroupBits::COMMAND_STOP |
+ SpeakerEventGroupBits::COMMAND_STOP_GRACEFULLY, // Bit message to read
+ pdTRUE, // Clear the bits on exit
+ pdFALSE, // Don't wait for all the bits,
+ portMAX_DELAY); // Block indefinitely until a bit is set
+
+ if (event_group_bits & (SpeakerEventGroupBits::COMMAND_STOP | SpeakerEventGroupBits::COMMAND_STOP_GRACEFULLY)) {
+ // Received a stop signal before the task was requested to start
+ this_speaker->delete_task_(0);
+ }
+
+ xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::STATE_STARTING);
+
+ audio::AudioStreamInfo audio_stream_info = this_speaker->audio_stream_info_;
+ const ssize_t bytes_per_sample = audio_stream_info.get_bytes_per_sample();
+ const uint8_t number_of_channels = audio_stream_info.channels;
+
+ const size_t dma_buffers_size = FRAMES_IN_ALL_DMA_BUFFERS * bytes_per_sample * number_of_channels;
+
+ if (this_speaker->send_esp_err_to_event_group_(
+ this_speaker->allocate_buffers_(dma_buffers_size, RING_BUFFER_SAMPLES * bytes_per_sample))) {
+ // Failed to allocate buffers
+ xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::ERR_ESP_NO_MEM);
+ this_speaker->delete_task_(dma_buffers_size);
+ }
+
+ if (this_speaker->send_esp_err_to_event_group_(this_speaker->start_i2s_driver_())) {
+ // Failed to start I2S driver
+ this_speaker->delete_task_(dma_buffers_size);
+ } else {
+ // Ring buffer is allocated, so indicate its can be written to
+ xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE);
+ }
+
+ if (!this_speaker->send_esp_err_to_event_group_(this_speaker->reconfigure_i2s_stream_info_(audio_stream_info))) {
+ // Successfully set the I2S stream info, ready to write audio data to the I2S port
+
+ xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::STATE_RUNNING);
+
+ bool stop_gracefully = false;
+ uint32_t last_data_received_time = millis();
+
+ while ((millis() - last_data_received_time) <= this_speaker->timeout_) {
+ event_group_bits = xEventGroupGetBits(this_speaker->event_group_);
+
+ if (event_group_bits & SpeakerEventGroupBits::COMMAND_STOP) {
+ break;
+ }
+ if (event_group_bits & SpeakerEventGroupBits::COMMAND_STOP_GRACEFULLY) {
+ stop_gracefully = true;
+ }
+
+ size_t bytes_to_read = dma_buffers_size;
+ size_t bytes_read = this_speaker->audio_ring_buffer_->read((void *) this_speaker->data_buffer_, bytes_to_read,
+ pdMS_TO_TICKS(TASK_DELAY_MS));
+
+ if (bytes_read > 0) {
+ last_data_received_time = millis();
+ size_t bytes_written = 0;
+
+ if ((audio_stream_info.bits_per_sample == 16) && (this_speaker->q15_volume_factor_ < INT16_MAX)) {
+ // Scale samples by the volume factor in place
+ q15_multiplication((int16_t *) this_speaker->data_buffer_, (int16_t *) this_speaker->data_buffer_,
+ bytes_read / sizeof(int16_t), this_speaker->q15_volume_factor_);
+ }
+
+ if (audio_stream_info.bits_per_sample == (uint8_t) this_speaker->bits_per_sample_) {
+ i2s_write(this_speaker->parent_->get_port(), this_speaker->data_buffer_, bytes_read, &bytes_written,
+ portMAX_DELAY);
+ } else if (audio_stream_info.bits_per_sample < (uint8_t) this_speaker->bits_per_sample_) {
+ i2s_write_expand(this_speaker->parent_->get_port(), this_speaker->data_buffer_, bytes_read,
+ audio_stream_info.bits_per_sample, this_speaker->bits_per_sample_, &bytes_written,
+ portMAX_DELAY);
+ }
+
+ if (bytes_written != bytes_read) {
+ xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::ERR_ESP_INVALID_SIZE);
+ }
+
+ } else {
+ // No data received
+
+ if (stop_gracefully) {
+ break;
+ }
+
+ i2s_zero_dma_buffer(this_speaker->parent_->get_port());
+ }
+ }
+ } else {
+ // Couldn't configure the I2S port to be compatible with the incoming audio
+ xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::ERR_INVALID_FORMAT);
+ }
+ i2s_zero_dma_buffer(this_speaker->parent_->get_port());
+
+ xEventGroupSetBits(this_speaker->event_group_, SpeakerEventGroupBits::STATE_STOPPING);
+
+ i2s_stop(this_speaker->parent_->get_port());
+ i2s_driver_uninstall(this_speaker->parent_->get_port());
+
+ this_speaker->parent_->unlock();
+ this_speaker->delete_task_(dma_buffers_size);
+}
+
void I2SAudioSpeaker::start() {
- if (this->is_failed()) {
- ESP_LOGE(TAG, "Cannot start audio, speaker failed to setup");
+ if (this->is_failed() || this->status_has_error())
return;
- }
- if (this->task_created_) {
- ESP_LOGW(TAG, "Called start while task has been already created.");
+ if ((this->state_ == speaker::STATE_STARTING) || (this->state_ == speaker::STATE_RUNNING))
return;
- }
- this->state_ = speaker::STATE_STARTING;
-}
-void I2SAudioSpeaker::start_() {
- if (this->task_created_) {
- return;
- }
- if (!this->parent_->try_lock()) {
- return; // Waiting for another i2s component to return lock
+
+ if (this->speaker_task_handle_ == nullptr) {
+ xTaskCreate(I2SAudioSpeaker::speaker_task, "speaker_task", TASK_STACK_SIZE, (void *) this, TASK_PRIORITY,
+ &this->speaker_task_handle_);
}
- xTaskCreate(I2SAudioSpeaker::player_task, "speaker_task", 8192, (void *) this, 1, &this->player_task_handle_);
- this->task_created_ = true;
-}
-
-template const uint8_t *convert_data_format(const a *from, b *to, size_t &bytes, bool repeat) {
- if (sizeof(a) == sizeof(b) && !repeat) {
- return reinterpret_cast(from);
- }
- const b *result = to;
- for (size_t i = 0; i < bytes; i += sizeof(a)) {
- b value = static_cast(*from++) << (sizeof(b) - sizeof(a)) * 8;
- *to++ = value;
- if (repeat)
- *to++ = value;
- }
- bytes *= (sizeof(b) / sizeof(a)) * (repeat ? 2 : 1); // NOLINT
- return reinterpret_cast(result);
-}
-
-void I2SAudioSpeaker::player_task(void *params) {
- I2SAudioSpeaker *this_speaker = (I2SAudioSpeaker *) params;
-
- TaskEvent event;
- event.type = TaskEventType::STARTING;
- xQueueSend(this_speaker->event_queue_, &event, portMAX_DELAY);
-
- i2s_driver_config_t config = {
- .mode = (i2s_mode_t) (this_speaker->i2s_mode_ | I2S_MODE_TX),
- .sample_rate = this_speaker->sample_rate_,
- .bits_per_sample = this_speaker->bits_per_sample_,
- .channel_format = this_speaker->channel_,
- .communication_format = this_speaker->i2s_comm_fmt_,
- .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
- .dma_buf_count = 8,
- .dma_buf_len = 256,
- .use_apll = this_speaker->use_apll_,
- .tx_desc_auto_clear = true,
- .fixed_mclk = 0,
- .mclk_multiple = I2S_MCLK_MULTIPLE_256,
- .bits_per_chan = this_speaker->bits_per_channel_,
- };
-#if SOC_I2S_SUPPORTS_DAC
- if (this_speaker->internal_dac_mode_ != I2S_DAC_CHANNEL_DISABLE) {
- config.mode = (i2s_mode_t) (config.mode | I2S_MODE_DAC_BUILT_IN);
- }
-#endif
-
- esp_err_t err = i2s_driver_install(this_speaker->parent_->get_port(), &config, 0, nullptr);
- if (err != ESP_OK) {
- event.type = TaskEventType::WARNING;
- event.err = err;
- xQueueSend(this_speaker->event_queue_, &event, 0);
- event.type = TaskEventType::STOPPED;
- xQueueSend(this_speaker->event_queue_, &event, 0);
- while (true) {
- delay(10);
- }
- }
-
-#if SOC_I2S_SUPPORTS_DAC
- if (this_speaker->internal_dac_mode_ == I2S_DAC_CHANNEL_DISABLE) {
-#endif
- i2s_pin_config_t pin_config = this_speaker->parent_->get_pin_config();
- pin_config.data_out_num = this_speaker->dout_pin_;
-
- i2s_set_pin(this_speaker->parent_->get_port(), &pin_config);
-#if SOC_I2S_SUPPORTS_DAC
+ if (this->speaker_task_handle_ != nullptr) {
+ xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::COMMAND_START);
+ this->task_created_ = true;
} else {
- i2s_set_dac_mode(this_speaker->internal_dac_mode_);
- }
-#endif
-
- DataEvent data_event;
-
- event.type = TaskEventType::STARTED;
- xQueueSend(this_speaker->event_queue_, &event, portMAX_DELAY);
-
- int32_t buffer[BUFFER_SIZE];
-
- while (true) {
- if (xQueueReceive(this_speaker->buffer_queue_, &data_event, this_speaker->timeout_ / portTICK_PERIOD_MS) !=
- pdTRUE) {
- break; // End of audio from main thread
- }
- if (data_event.stop) {
- // Stop signal from main thread
- xQueueReset(this_speaker->buffer_queue_); // Flush queue
- break;
- }
-
- const uint8_t *data = data_event.data;
- size_t remaining = data_event.len;
- switch (this_speaker->bits_per_sample_) {
- case I2S_BITS_PER_SAMPLE_8BIT:
- case I2S_BITS_PER_SAMPLE_16BIT: {
- data = convert_data_format(reinterpret_cast(data), reinterpret_cast(buffer),
- remaining, this_speaker->channel_ == I2S_CHANNEL_FMT_ALL_LEFT);
- break;
- }
- case I2S_BITS_PER_SAMPLE_24BIT:
- case I2S_BITS_PER_SAMPLE_32BIT: {
- data = convert_data_format(reinterpret_cast(data), reinterpret_cast(buffer),
- remaining, this_speaker->channel_ == I2S_CHANNEL_FMT_ALL_LEFT);
- break;
- }
- }
-
- while (remaining != 0) {
- size_t bytes_written;
- esp_err_t err =
- i2s_write(this_speaker->parent_->get_port(), data, remaining, &bytes_written, (32 / portTICK_PERIOD_MS));
- if (err != ESP_OK) {
- event = {.type = TaskEventType::WARNING, .err = err};
- if (xQueueSend(this_speaker->event_queue_, &event, 10 / portTICK_PERIOD_MS) != pdTRUE) {
- ESP_LOGW(TAG, "Failed to send WARNING event");
- }
- continue;
- }
- data += bytes_written;
- remaining -= bytes_written;
- }
- }
-
- event.type = TaskEventType::STOPPING;
- if (xQueueSend(this_speaker->event_queue_, &event, 10 / portTICK_PERIOD_MS) != pdTRUE) {
- ESP_LOGW(TAG, "Failed to send STOPPING event");
- }
-
- i2s_zero_dma_buffer(this_speaker->parent_->get_port());
-
- i2s_driver_uninstall(this_speaker->parent_->get_port());
-
- event.type = TaskEventType::STOPPED;
- if (xQueueSend(this_speaker->event_queue_, &event, 10 / portTICK_PERIOD_MS) != pdTRUE) {
- ESP_LOGW(TAG, "Failed to send STOPPED event");
- }
-
- while (true) {
- delay(10);
+ xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::ERR_TASK_FAILED_TO_START);
}
}
@@ -203,92 +361,169 @@ void I2SAudioSpeaker::stop_(bool wait_on_empty) {
return;
if (this->state_ == speaker::STATE_STOPPED)
return;
- if (this->state_ == speaker::STATE_STARTING) {
- this->state_ = speaker::STATE_STOPPED;
- return;
- }
- this->state_ = speaker::STATE_STOPPING;
- DataEvent data;
- data.stop = true;
+
if (wait_on_empty) {
- xQueueSend(this->buffer_queue_, &data, portMAX_DELAY);
+ xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::COMMAND_STOP_GRACEFULLY);
} else {
- xQueueSendToFront(this->buffer_queue_, &data, portMAX_DELAY);
+ xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::COMMAND_STOP);
}
}
-void I2SAudioSpeaker::watch_() {
- TaskEvent event;
- if (xQueueReceive(this->event_queue_, &event, 0) == pdTRUE) {
- switch (event.type) {
- case TaskEventType::STARTING:
- ESP_LOGD(TAG, "Starting I2S Audio Speaker");
- break;
- case TaskEventType::STARTED:
- ESP_LOGD(TAG, "Started I2S Audio Speaker");
- this->state_ = speaker::STATE_RUNNING;
- this->status_clear_warning();
- break;
- case TaskEventType::STOPPING:
- ESP_LOGD(TAG, "Stopping I2S Audio Speaker");
- break;
- case TaskEventType::STOPPED:
- this->state_ = speaker::STATE_STOPPED;
- vTaskDelete(this->player_task_handle_);
- this->task_created_ = false;
- this->player_task_handle_ = nullptr;
- this->parent_->unlock();
- xQueueReset(this->buffer_queue_);
- ESP_LOGD(TAG, "Stopped I2S Audio Speaker");
- break;
- case TaskEventType::WARNING:
- ESP_LOGW(TAG, "Error writing to I2S: %s", esp_err_to_name(event.err));
- this->status_set_warning();
- break;
- }
+bool I2SAudioSpeaker::send_esp_err_to_event_group_(esp_err_t err) {
+ switch (err) {
+ case ESP_OK:
+ return false;
+ case ESP_ERR_INVALID_STATE:
+ xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::ERR_ESP_INVALID_STATE);
+ return true;
+ case ESP_ERR_INVALID_ARG:
+ xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::ERR_ESP_INVALID_ARG);
+ return true;
+ case ESP_ERR_INVALID_SIZE:
+ xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::ERR_ESP_INVALID_SIZE);
+ return true;
+ case ESP_ERR_NO_MEM:
+ xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::ERR_ESP_NO_MEM);
+ return true;
+ default:
+ xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::ERR_ESP_FAIL);
+ return true;
}
}
-void I2SAudioSpeaker::loop() {
- switch (this->state_) {
- case speaker::STATE_STARTING:
- this->start_();
- [[fallthrough]];
- case speaker::STATE_RUNNING:
- case speaker::STATE_STOPPING:
- this->watch_();
- break;
- case speaker::STATE_STOPPED:
- break;
+esp_err_t I2SAudioSpeaker::allocate_buffers_(size_t data_buffer_size, size_t ring_buffer_size) {
+ if (this->data_buffer_ == nullptr) {
+ // Allocate data buffer for temporarily storing audio from the ring buffer before writing to the I2S bus
+ ExternalRAMAllocator allocator(ExternalRAMAllocator::ALLOW_FAILURE);
+ this->data_buffer_ = allocator.allocate(data_buffer_size);
}
+
+ if (this->data_buffer_ == nullptr) {
+ return ESP_ERR_NO_MEM;
+ }
+
+ if (this->audio_ring_buffer_ == nullptr) {
+ // Allocate ring buffer
+ this->audio_ring_buffer_ = RingBuffer::create(ring_buffer_size);
+ }
+
+ if (this->audio_ring_buffer_ == nullptr) {
+ return ESP_ERR_NO_MEM;
+ }
+
+ return ESP_OK;
}
-size_t I2SAudioSpeaker::play(const uint8_t *data, size_t length) {
- if (this->is_failed()) {
- ESP_LOGE(TAG, "Cannot play audio, speaker failed to setup");
- return 0;
+esp_err_t I2SAudioSpeaker::start_i2s_driver_() {
+ if (!this->parent_->try_lock()) {
+ return ESP_ERR_INVALID_STATE;
}
- if (this->state_ != speaker::STATE_RUNNING && this->state_ != speaker::STATE_STARTING) {
- this->start();
+
+ i2s_driver_config_t config = {
+ .mode = (i2s_mode_t) (this->i2s_mode_ | I2S_MODE_TX),
+ .sample_rate = this->sample_rate_,
+ .bits_per_sample = this->bits_per_sample_,
+ .channel_format = this->channel_,
+ .communication_format = this->i2s_comm_fmt_,
+ .intr_alloc_flags = ESP_INTR_FLAG_LEVEL1,
+ .dma_buf_count = DMA_BUFFERS_COUNT,
+ .dma_buf_len = DMA_BUFFER_SIZE,
+ .use_apll = this->use_apll_,
+ .tx_desc_auto_clear = true,
+ .fixed_mclk = I2S_PIN_NO_CHANGE,
+ .mclk_multiple = I2S_MCLK_MULTIPLE_256,
+ .bits_per_chan = this->bits_per_channel_,
+#if SOC_I2S_SUPPORTS_TDM
+ .chan_mask = (i2s_channel_t) (I2S_TDM_ACTIVE_CH0 | I2S_TDM_ACTIVE_CH1),
+ .total_chan = 2,
+ .left_align = false,
+ .big_edin = false,
+ .bit_order_msb = false,
+ .skip_msk = false,
+#endif
+ };
+#if SOC_I2S_SUPPORTS_DAC
+ if (this->internal_dac_mode_ != I2S_DAC_CHANNEL_DISABLE) {
+ config.mode = (i2s_mode_t) (config.mode | I2S_MODE_DAC_BUILT_IN);
}
- size_t remaining = length;
- size_t index = 0;
- while (remaining > 0) {
- DataEvent event;
- event.stop = false;
- size_t to_send_length = std::min(remaining, BUFFER_SIZE);
- event.len = to_send_length;
- memcpy(event.data, data + index, to_send_length);
- if (xQueueSend(this->buffer_queue_, &event, 0) != pdTRUE) {
- return index;
- }
- remaining -= to_send_length;
- index += to_send_length;
+#endif
+
+ esp_err_t err = i2s_driver_install(this->parent_->get_port(), &config, 0, nullptr);
+ if (err != ESP_OK) {
+ // Failed to install the driver, so unlock the I2S port
+ this->parent_->unlock();
+ return err;
}
- return index;
+
+#if SOC_I2S_SUPPORTS_DAC
+ if (this->internal_dac_mode_ == I2S_DAC_CHANNEL_DISABLE) {
+#endif
+ i2s_pin_config_t pin_config = this->parent_->get_pin_config();
+ pin_config.data_out_num = this->dout_pin_;
+
+ err = i2s_set_pin(this->parent_->get_port(), &pin_config);
+#if SOC_I2S_SUPPORTS_DAC
+ } else {
+ i2s_set_dac_mode(this->internal_dac_mode_);
+ }
+#endif
+
+ if (err != ESP_OK) {
+ // Failed to set the data out pin, so uninstall the driver and unlock the I2S port
+ i2s_driver_uninstall(this->parent_->get_port());
+ this->parent_->unlock();
+ }
+
+ return err;
}
-bool I2SAudioSpeaker::has_buffered_data() const { return uxQueueMessagesWaiting(this->buffer_queue_) > 0; }
+esp_err_t I2SAudioSpeaker::reconfigure_i2s_stream_info_(audio::AudioStreamInfo &audio_stream_info) {
+ if (this->i2s_mode_ & I2S_MODE_MASTER) {
+ // ESP controls for the the I2S bus, so adjust the sample rate and bits per sample to match the incoming audio
+ this->sample_rate_ = audio_stream_info.sample_rate;
+ this->bits_per_sample_ = (i2s_bits_per_sample_t) audio_stream_info.bits_per_sample;
+ } else if (this->sample_rate_ != audio_stream_info.sample_rate) {
+ // Can't reconfigure I2S bus, so the sample rate must match the configured value
+ return ESP_ERR_INVALID_ARG;
+ }
+
+ if ((i2s_bits_per_sample_t) audio_stream_info.bits_per_sample > this->bits_per_sample_) {
+ // Currently can't handle the case when the incoming audio has more bits per sample than the configured value
+ return ESP_ERR_INVALID_ARG;
+ }
+
+ if (audio_stream_info.channels == 1) {
+ return i2s_set_clk(this->parent_->get_port(), this->sample_rate_, this->bits_per_sample_, I2S_CHANNEL_MONO);
+ } else if (audio_stream_info.channels == 2) {
+ return i2s_set_clk(this->parent_->get_port(), this->sample_rate_, this->bits_per_sample_, I2S_CHANNEL_STEREO);
+ }
+
+ return ESP_ERR_INVALID_ARG;
+}
+
+void I2SAudioSpeaker::delete_task_(size_t buffer_size) {
+ if (this->audio_ring_buffer_ != nullptr) {
+ xEventGroupWaitBits(this->event_group_,
+ MESSAGE_RING_BUFFER_AVAILABLE_TO_WRITE, // Bit message to read
+ pdFALSE, // Don't clear the bits on exit
+ pdTRUE, // Don't wait for all the bits,
+ portMAX_DELAY); // Block indefinitely until a command bit is set
+
+ this->audio_ring_buffer_.reset(); // Deallocates the ring buffer stored in the unique_ptr
+ this->audio_ring_buffer_ = nullptr;
+ }
+
+ if (this->data_buffer_ != nullptr) {
+ ExternalRAMAllocator allocator(ExternalRAMAllocator::ALLOW_FAILURE);
+ allocator.deallocate(this->data_buffer_, buffer_size);
+ this->data_buffer_ = nullptr;
+ }
+
+ xEventGroupSetBits(this->event_group_, SpeakerEventGroupBits::STATE_STOPPED);
+
+ this->task_created_ = false;
+ vTaskDelete(nullptr);
+}
} // namespace i2s_audio
} // namespace esphome
diff --git a/esphome/components/i2s_audio/speaker/i2s_audio_speaker.h b/esphome/components/i2s_audio/speaker/i2s_audio_speaker.h
index 9d1817c86f..3c512d4d4d 100644
--- a/esphome/components/i2s_audio/speaker/i2s_audio_speaker.h
+++ b/esphome/components/i2s_audio/speaker/i2s_audio_speaker.h
@@ -5,38 +5,21 @@
#include "../i2s_audio.h"
#include
-#include
-#include
+#include
+#include
+
+#include "esphome/components/audio/audio.h"
#include "esphome/components/speaker/speaker.h"
+
#include "esphome/core/component.h"
#include "esphome/core/gpio.h"
#include "esphome/core/helpers.h"
+#include "esphome/core/ring_buffer.h"
namespace esphome {
namespace i2s_audio {
-static const size_t BUFFER_SIZE = 1024;
-
-enum class TaskEventType : uint8_t {
- STARTING = 0,
- STARTED,
- STOPPING,
- STOPPED,
- WARNING = 255,
-};
-
-struct TaskEvent {
- TaskEventType type;
- esp_err_t err;
-};
-
-struct DataEvent {
- bool stop;
- size_t len;
- uint8_t data[BUFFER_SIZE];
-};
-
class I2SAudioSpeaker : public I2SAudioOut, public speaker::Speaker, public Component {
public:
float get_setup_priority() const override { return esphome::setup_priority::LATE; }
@@ -55,25 +38,95 @@ class I2SAudioSpeaker : public I2SAudioOut, public speaker::Speaker, public Comp
void stop() override;
void finish() override;
- size_t play(const uint8_t *data, size_t length) override;
+ /// @brief Plays the provided audio data.
+ /// Starts the speaker task, if necessary. Writes the audio data to the ring buffer.
+ /// @param data Audio data in the format set by the parent speaker classes ``set_audio_stream_info`` method.
+ /// @param length The length of the audio data in bytes.
+ /// @param ticks_to_wait The FreeRTOS ticks to wait before writing as much data as possible to the ring buffer.
+ /// @return The number of bytes that were actually written to the ring buffer.
+ size_t play(const uint8_t *data, size_t length, TickType_t ticks_to_wait) override;
+ size_t play(const uint8_t *data, size_t length) override { return play(data, length, 0); }
bool has_buffered_data() const override;
+ /// @brief Sets the volume of the speaker. Uses the speaker's configured audio dac component. If unavailble, it is
+ /// implemented as a software volume control. Overrides the default setter to convert the floating point volume to a
+ /// Q15 fixed-point factor.
+ /// @param volume between 0.0 and 1.0
+ void set_volume(float volume) override;
+
+ /// @brief Mutes or unmute the speaker. Uses the speaker's configured audio dac component. If unavailble, it is
+ /// implemented as a software volume control. Overrides the default setter to convert the floating point volume to a
+ /// Q15 fixed-point factor.
+ /// @param mute_state true for muting, false for unmuting
+ void set_mute_state(bool mute_state) override;
+
protected:
- void start_();
+ /// @brief Function for the FreeRTOS task handling audio output.
+ /// After receiving the COMMAND_START signal, allocates space for the buffers, starts the I2S driver, and reads
+ /// audio from the ring buffer and writes audio to the I2S port. Stops immmiately after receiving the COMMAND_STOP
+ /// signal and stops only after the ring buffer is empty after receiving the COMMAND_STOP_GRACEFULLY signal. Stops if
+ /// the ring buffer hasn't read data for more than timeout_ milliseconds. When stopping, it deallocates the buffers,
+ /// stops the I2S driver, unlocks the I2S port, and deletes the task. It communicates the state and any errors via
+ /// event_group_.
+ /// @param params I2SAudioSpeaker component
+ static void speaker_task(void *params);
+
+ /// @brief Sends a stop command to the speaker task via event_group_.
+ /// @param wait_on_empty If false, sends the COMMAND_STOP signal. If true, sends the COMMAND_STOP_GRACEFULLY signal.
void stop_(bool wait_on_empty);
- void watch_();
- static void player_task(void *params);
+ /// @brief Sets the corresponding ERR_ESP event group bits.
+ /// @param err esp_err_t error code.
+ /// @return True if an ERR_ESP bit is set and false if err == ESP_OK
+ bool send_esp_err_to_event_group_(esp_err_t err);
- TaskHandle_t player_task_handle_{nullptr};
- QueueHandle_t buffer_queue_;
- QueueHandle_t event_queue_;
+ /// @brief Allocates the data buffer and ring buffer
+ /// @param data_buffer_size Number of bytes to allocate for the data buffer.
+ /// @param ring_buffer_size Number of bytes to allocate for the ring buffer.
+ /// @return ESP_ERR_NO_MEM if either buffer fails to allocate
+ /// ESP_OK if successful
+ esp_err_t allocate_buffers_(size_t data_buffer_size, size_t ring_buffer_size);
+
+ /// @brief Starts the ESP32 I2S driver.
+ /// Attempts to lock the I2S port, starts the I2S driver, and sets the data out pin. If it fails, it will unlock
+ /// the I2S port and uninstall the driver, if necessary.
+ /// @return ESP_ERR_INVALID_STATE if the I2S port is already locked.
+ /// ESP_ERR_INVALID_ARG if installing the driver or setting the data out pin fails due to a parameter error.
+ /// ESP_ERR_NO_MEM if the driver fails to install due to a memory allocation error.
+ /// ESP_FAIL if setting the data out pin fails due to an IO error
+ /// ESP_OK if successful
+ esp_err_t start_i2s_driver_();
+
+ /// @brief Adjusts the I2S driver configuration to match the incoming audio stream.
+ /// Modifies I2S driver's sample rate, bits per sample, and number of channel settings. If the I2S is in secondary
+ /// mode, it only modifies the number of channels.
+ /// @param audio_stream_info Describes the incoming audio stream
+ /// @return ESP_ERR_INVALID_ARG if there is a parameter error, if there is more than 2 channels in the stream, or if
+ /// the audio settings are incompatible with the configuration.
+ /// ESP_ERR_NO_MEM if the driver fails to reconfigure due to a memory allocation error.
+ /// ESP_OK if successful.
+ esp_err_t reconfigure_i2s_stream_info_(audio::AudioStreamInfo &audio_stream_info);
+
+ /// @brief Deletes the speaker's task.
+ /// Deallocates the data_buffer_ and audio_ring_buffer_, if necessary, and deletes the task. Should only be called by
+ /// the speaker_task itself.
+ /// @param buffer_size The allocated size of the data_buffer_.
+ void delete_task_(size_t buffer_size);
+
+ TaskHandle_t speaker_task_handle_{nullptr};
+ EventGroupHandle_t event_group_{nullptr};
+
+ uint8_t *data_buffer_;
+ std::unique_ptr audio_ring_buffer_;
+
+ uint32_t timeout_;
+ uint8_t dout_pin_;
- uint32_t timeout_{0};
- uint8_t dout_pin_{0};
bool task_created_{false};
+ int16_t q15_volume_factor_{INT16_MAX};
+
#if SOC_I2S_SUPPORTS_DAC
i2s_dac_mode_t internal_dac_mode_{I2S_DAC_CHANNEL_DISABLE};
#endif
diff --git a/esphome/components/image/image.h b/esphome/components/image/image.h
index ae5a7a814d..a8a8aab2c2 100644
--- a/esphome/components/image/image.h
+++ b/esphome/components/image/image.h
@@ -3,12 +3,7 @@
#include "esphome/components/display/display.h"
#ifdef USE_LVGL
-// required for clang-tidy
-#ifndef LV_CONF_H
-#define LV_CONF_SKIP 1 // NOLINT
-#endif // LV_CONF_H
-
-#include
+#include "esphome/components/lvgl/lvgl_proxy.h"
#endif // USE_LVGL
namespace esphome {
diff --git a/esphome/components/libretiny/__init__.py b/esphome/components/libretiny/__init__.py
index cc7fae7e70..b29d2e309c 100644
--- a/esphome/components/libretiny/__init__.py
+++ b/esphome/components/libretiny/__init__.py
@@ -46,7 +46,7 @@ from .const import (
_LOGGER = logging.getLogger(__name__)
CODEOWNERS = ["@kuba2k2"]
-AUTO_LOAD = []
+AUTO_LOAD = ["preferences"]
def _detect_variant(value):
diff --git a/esphome/components/lvgl/__init__.py b/esphome/components/lvgl/__init__.py
index dea3b11a94..215fdecdb5 100644
--- a/esphome/components/lvgl/__init__.py
+++ b/esphome/components/lvgl/__init__.py
@@ -22,7 +22,7 @@ from esphome.helpers import write_file_if_changed
from . import defines as df, helpers, lv_validation as lvalid
from .automation import disp_update, focused_widgets, update_to_code
-from .defines import CONF_WIDGETS, add_define
+from .defines import add_define
from .encoders import ENCODERS_CONFIG, encoders_to_code, initial_focus_to_code
from .gradient import GRADIENT_SCHEMA, gradients_to_code
from .hello_world import get_hello_world
@@ -33,7 +33,7 @@ from .schemas import (
FLEX_OBJ_SCHEMA,
GRID_CELL_SCHEMA,
LAYOUT_SCHEMAS,
- STATE_SCHEMA,
+ STYLE_SCHEMA,
WIDGET_TYPES,
any_widget_schema,
container_schema,
@@ -54,7 +54,7 @@ from .types import (
lv_style_t,
lvgl_ns,
)
-from .widgets import Widget, add_widgets, lv_scr_act, set_obj_properties, styles_used
+from .widgets import Widget, add_widgets, get_scr_act, set_obj_properties, styles_used
from .widgets.animimg import animimg_spec
from .widgets.arc import arc_spec
from .widgets.button import button_spec
@@ -186,7 +186,7 @@ def final_validation(config):
async def to_code(config):
cg.add_library("lvgl/lvgl", "8.4.0")
- CORE.add_define("USE_LVGL")
+ cg.add_define("USE_LVGL")
# suppress default enabling of extra widgets
add_define("_LV_KCONFIG_PRESENT")
# Always enable - lots of things use it.
@@ -200,7 +200,13 @@ async def to_code(config):
add_define("LV_MEM_CUSTOM_REALLOC", "lv_custom_mem_realloc")
add_define("LV_MEM_CUSTOM_INCLUDE", '"esphome/components/lvgl/lvgl_hal.h"')
- add_define("LV_LOG_LEVEL", f"LV_LOG_LEVEL_{config[df.CONF_LOG_LEVEL]}")
+ add_define(
+ "LV_LOG_LEVEL", f"LV_LOG_LEVEL_{df.LV_LOG_LEVELS[config[df.CONF_LOG_LEVEL]]}"
+ )
+ cg.add_define(
+ "LVGL_LOG_LEVEL",
+ cg.RawExpression(f"ESPHOME_LOG_LEVEL_{config[df.CONF_LOG_LEVEL]}"),
+ )
add_define("LV_COLOR_DEPTH", config[df.CONF_COLOR_DEPTH])
for font in helpers.lv_fonts_used:
add_define(f"LV_FONT_{font.upper()}")
@@ -214,15 +220,9 @@ async def to_code(config):
"LV_COLOR_CHROMA_KEY",
await lvalid.lv_color.process(config[df.CONF_TRANSPARENCY_KEY]),
)
- CORE.add_build_flag("-Isrc")
+ cg.add_build_flag("-Isrc")
cg.add_global(lvgl_ns.using)
- lv_component = cg.new_Pvariable(config[CONF_ID])
- await cg.register_component(lv_component, config)
- Widget.create(config[CONF_ID], lv_component, obj_spec, config)
- for display in config[df.CONF_DISPLAYS]:
- cg.add(lv_component.add_display(await cg.get_variable(display)))
-
frac = config[CONF_BUFFER_SIZE]
if frac >= 0.75:
frac = 1
@@ -232,10 +232,17 @@ async def to_code(config):
frac = 4
else:
frac = 8
- cg.add(lv_component.set_buffer_frac(int(frac)))
- cg.add(lv_component.set_full_refresh(config[df.CONF_FULL_REFRESH]))
- cg.add(lv_component.set_draw_rounding(config[df.CONF_DRAW_ROUNDING]))
- cg.add(lv_component.set_resume_on_input(config[df.CONF_RESUME_ON_INPUT]))
+ displays = [await cg.get_variable(display) for display in config[df.CONF_DISPLAYS]]
+ lv_component = cg.new_Pvariable(
+ config[CONF_ID],
+ displays,
+ frac,
+ config[df.CONF_FULL_REFRESH],
+ config[df.CONF_DRAW_ROUNDING],
+ config[df.CONF_RESUME_ON_INPUT],
+ )
+ await cg.register_component(lv_component, config)
+ Widget.create(config[CONF_ID], lv_component, obj_spec, config)
for font in helpers.esphome_fonts_used:
await cg.get_variable(font)
@@ -257,6 +264,7 @@ async def to_code(config):
else:
add_define("LV_FONT_DEFAULT", await lvalid.lv_font.process(default_font))
+ lv_scr_act = get_scr_act(lv_component)
async with LvContext(lv_component):
await touchscreens_to_code(lv_component, config)
await encoders_to_code(lv_component, config)
@@ -266,12 +274,11 @@ async def to_code(config):
await set_obj_properties(lv_scr_act, config)
await add_widgets(lv_scr_act, config)
await add_pages(lv_component, config)
- await add_top_layer(config)
- await msgboxes_to_code(config)
- await disp_update(f"{lv_component}->get_disp()", config)
- # At this point only the setup code should be generated
- assert LvContext.added_lambda_count == 1
- Widget.set_completed()
+ await add_top_layer(lv_component, config)
+ await msgboxes_to_code(lv_component, config)
+ await disp_update(lv_component.get_disp(), config)
+ # Set this directly since we are limited in how many methods can be added to the Widget class.
+ Widget.widgets_completed = True
async with LvContext(lv_component):
await generate_triggers(lv_component)
await generate_page_triggers(lv_component, config)
@@ -290,15 +297,15 @@ async def to_code(config):
await build_automation(resume_trigger, [], conf)
for comp in helpers.lvgl_components_required:
- CORE.add_define(f"USE_LVGL_{comp.upper()}")
+ cg.add_define(f"USE_LVGL_{comp.upper()}")
if "transform_angle" in styles_used:
add_define("LV_COLOR_SCREEN_TRANSP", "1")
for use in helpers.lv_uses:
add_define(f"LV_USE_{use.upper()}")
lv_conf_h_file = CORE.relative_src_path(LV_CONF_FILENAME)
write_file_if_changed(lv_conf_h_file, generate_lv_conf_h())
- CORE.add_build_flag("-DLV_CONF_H=1")
- CORE.add_build_flag(f'-DLV_CONF_PATH="{LV_CONF_FILENAME}"')
+ cg.add_build_flag("-DLV_CONF_H=1")
+ cg.add_build_flag(f'-DLV_CONF_PATH="{LV_CONF_FILENAME}"')
def display_schema(config):
@@ -307,9 +314,9 @@ def display_schema(config):
def add_hello_world(config):
- if CONF_WIDGETS not in config and CONF_PAGES not in config:
+ if df.CONF_WIDGETS not in config and CONF_PAGES not in config:
LOGGER.info("No pages or widgets configured, creating default hello_world page")
- config[CONF_WIDGETS] = cv.ensure_list(WIDGET_SCHEMA)(get_hello_world())
+ config[df.CONF_WIDGETS] = cv.ensure_list(WIDGET_SCHEMA)(get_hello_world())
return config
@@ -328,14 +335,14 @@ CONFIG_SCHEMA = (
cv.Optional(df.CONF_DRAW_ROUNDING, default=2): cv.positive_int,
cv.Optional(CONF_BUFFER_SIZE, default="100%"): cv.percentage,
cv.Optional(df.CONF_LOG_LEVEL, default="WARN"): cv.one_of(
- *df.LOG_LEVELS, upper=True
+ *df.LV_LOG_LEVELS, upper=True
),
cv.Optional(df.CONF_BYTE_ORDER, default="big_endian"): cv.one_of(
"big_endian", "little_endian"
),
cv.Optional(df.CONF_STYLE_DEFINITIONS): cv.ensure_list(
cv.Schema({cv.Required(CONF_ID): cv.declare_id(lv_style_t)})
- .extend(STATE_SCHEMA)
+ .extend(STYLE_SCHEMA)
.extend(
{
cv.Optional(df.CONF_GRID_CELL_X_ALIGN): grid_alignments,
diff --git a/esphome/components/lvgl/automation.py b/esphome/components/lvgl/automation.py
index cdc7553e81..48472354f8 100644
--- a/esphome/components/lvgl/automation.py
+++ b/esphome/components/lvgl/automation.py
@@ -5,7 +5,7 @@ from esphome import automation
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import CONF_ACTION, CONF_GROUP, CONF_ID, CONF_TIMEOUT
-from esphome.cpp_generator import RawExpression, get_variable
+from esphome.cpp_generator import get_variable
from esphome.cpp_types import nullptr
from .defines import (
@@ -23,7 +23,6 @@ from .lvcode import (
UPDATE_EVENT,
LambdaContext,
LocalVariable,
- LvConditional,
LvglComponent,
ReturnStatement,
add_line_marks,
@@ -47,8 +46,8 @@ from .types import (
)
from .widgets import (
Widget,
+ get_scr_act,
get_widgets,
- lv_scr_act,
set_obj_properties,
wait_for_widgets,
)
@@ -66,8 +65,6 @@ async def action_to_code(
):
await wait_for_widgets()
async with LambdaContext(parameters=args, where=action_id) as context:
- with LvConditional(lv_expr.is_pre_initialise()):
- context.add(RawExpression("return"))
for widget in widgets:
await action(widget)
var = cg.new_Pvariable(action_id, template_arg, await context.get_lambda())
@@ -126,7 +123,7 @@ async def lvgl_is_idle(config, condition_id, template_arg, args):
async def disp_update(disp, config: dict):
if CONF_DISP_BG_COLOR not in config and CONF_DISP_BG_IMAGE not in config:
return
- with LocalVariable("lv_disp_tmp", lv_disp_t, literal(disp)) as disp_temp:
+ with LocalVariable("lv_disp_tmp", lv_disp_t, disp) as disp_temp:
if (bg_color := config.get(CONF_DISP_BG_COLOR)) is not None:
lv.disp_set_bg_color(disp_temp, await lv_color.process(bg_color))
if bg_image := config.get(CONF_DISP_BG_IMAGE):
@@ -136,15 +133,24 @@ async def disp_update(disp, config: dict):
@automation.register_action(
"lvgl.widget.redraw",
ObjUpdateAction,
- cv.Schema(
- {
- cv.Optional(CONF_ID): cv.use_id(lv_obj_t),
- cv.GenerateID(CONF_LVGL_ID): cv.use_id(LvglComponent),
- }
+ cv.Any(
+ cv.maybe_simple_value(
+ {
+ cv.Required(CONF_ID): cv.use_id(lv_obj_t),
+ cv.GenerateID(CONF_LVGL_ID): cv.use_id(LvglComponent),
+ },
+ key=CONF_ID,
+ ),
+ cv.Schema(
+ {
+ cv.GenerateID(CONF_LVGL_ID): cv.use_id(LvglComponent),
+ }
+ ),
),
)
async def obj_invalidate_to_code(config, action_id, template_arg, args):
- widgets = await get_widgets(config) or [lv_scr_act]
+ lv_comp = await cg.get_variable(config[CONF_LVGL_ID])
+ widgets = await get_widgets(config) or [get_scr_act(lv_comp)]
async def do_invalidate(widget: Widget):
lv_obj.invalidate(widget.obj)
@@ -164,7 +170,7 @@ async def obj_invalidate_to_code(config, action_id, template_arg, args):
async def lvgl_update_to_code(config, action_id, template_arg, args):
widgets = await get_widgets(config)
w = widgets[0]
- disp = f"{w.obj}->get_disp()"
+ disp = literal(f"{w.obj}->get_disp()")
async with LambdaContext(LVGL_COMP_ARG, where=action_id) as context:
await disp_update(disp, config)
var = cg.new_Pvariable(action_id, template_arg, await context.get_lambda())
diff --git a/esphome/components/lvgl/defines.py b/esphome/components/lvgl/defines.py
index 7c42ed2f22..4d48028611 100644
--- a/esphome/components/lvgl/defines.py
+++ b/esphome/components/lvgl/defines.py
@@ -189,14 +189,14 @@ LV_ANIM = LvConstant(
LV_GRAD_DIR = LvConstant("LV_GRAD_DIR_", "NONE", "HOR", "VER")
LV_DITHER = LvConstant("LV_DITHER_", "NONE", "ORDERED", "ERR_DIFF")
-LOG_LEVELS = (
- "TRACE",
- "INFO",
- "WARN",
- "ERROR",
- "USER",
- "NONE",
-)
+LV_LOG_LEVELS = {
+ "VERBOSE": "TRACE",
+ "DEBUG": "TRACE",
+ "INFO": "INFO",
+ "WARN": "WARN",
+ "ERROR": "ERROR",
+ "NONE": "NONE",
+}
LV_LONG_MODES = LvConstant(
"LV_LABEL_LONG_",
@@ -477,6 +477,7 @@ CONF_ROWS = "rows"
CONF_SCALE_LINES = "scale_lines"
CONF_SCROLLBAR_MODE = "scrollbar_mode"
CONF_SELECTED_INDEX = "selected_index"
+CONF_SELECTED_TEXT = "selected_text"
CONF_SHOW_SNOW = "show_snow"
CONF_SPIN_TIME = "spin_time"
CONF_SRC = "src"
diff --git a/esphome/components/lvgl/light/__init__.py b/esphome/components/lvgl/light/__init__.py
index a0eeded349..8031ae8221 100644
--- a/esphome/components/lvgl/light/__init__.py
+++ b/esphome/components/lvgl/light/__init__.py
@@ -2,9 +2,9 @@ import esphome.codegen as cg
from esphome.components import light
from esphome.components.light import LightOutput
import esphome.config_validation as cv
-from esphome.const import CONF_GAMMA_CORRECT, CONF_LED, CONF_OUTPUT_ID
+from esphome.const import CONF_GAMMA_CORRECT, CONF_OUTPUT_ID
-from ..defines import CONF_LVGL_ID
+from ..defines import CONF_LVGL_ID, CONF_WIDGET
from ..lvcode import LvContext
from ..schemas import LVGL_SCHEMA
from ..types import LvType, lvgl_ns
@@ -15,7 +15,7 @@ LVLight = lvgl_ns.class_("LVLight", LightOutput)
CONFIG_SCHEMA = light.RGB_LIGHT_SCHEMA.extend(
{
cv.Optional(CONF_GAMMA_CORRECT, default=0.0): cv.positive_float,
- cv.Required(CONF_LED): cv.use_id(lv_led_t),
+ cv.Required(CONF_WIDGET): cv.use_id(lv_led_t),
cv.GenerateID(CONF_OUTPUT_ID): cv.declare_id(LVLight),
}
).extend(LVGL_SCHEMA)
@@ -26,7 +26,7 @@ async def to_code(config):
await light.register_light(var, config)
paren = await cg.get_variable(config[CONF_LVGL_ID])
- widget = await get_widgets(config, CONF_LED)
+ widget = await get_widgets(config, CONF_WIDGET)
widget = widget[0]
await wait_for_widgets()
async with LvContext(paren) as ctx:
diff --git a/esphome/components/lvgl/lv_validation.py b/esphome/components/lvgl/lv_validation.py
index fd840cc417..b91b0905df 100644
--- a/esphome/components/lvgl/lv_validation.py
+++ b/esphome/components/lvgl/lv_validation.py
@@ -267,6 +267,9 @@ def angle(value):
return int(cv.float_range(0.0, 360.0)(cv.angle(value)) * 10)
+lv_angle = LValidator(angle, uint32)
+
+
@schema_extractor("one_of")
def size_validator(value):
"""A size in one axis - one of "size_content", a number (pixels) or a percentage"""
@@ -274,10 +277,8 @@ def size_validator(value):
return ["SIZE_CONTENT", "number of pixels", "percentage"]
if isinstance(value, str) and value.lower().endswith("px"):
value = cv.int_(value[:-2])
- if isinstance(value, str) and not value.endswith("%"):
- if value.upper() == "SIZE_CONTENT":
- return "LV_SIZE_CONTENT"
- raise cv.Invalid("must be 'size_content', a percentage or an integer (pixels)")
+ if isinstance(value, str) and value.upper() == "SIZE_CONTENT":
+ return "LV_SIZE_CONTENT"
return pixels_or_percent_validator(value)
@@ -403,6 +404,7 @@ class TextValidator(LValidator):
lv_text = TextValidator()
lv_float = LValidator(cv.float_, cg.float_)
lv_int = LValidator(cv.int_, cg.int_)
+lv_positive_int = LValidator(cv.positive_int, cg.int_)
lv_brightness = LValidator(cv.percentage, cg.float_, retmapper=lambda x: int(x * 255))
diff --git a/esphome/components/lvgl/lvcode.py b/esphome/components/lvgl/lvcode.py
index 3a080d63e9..37d6670b84 100644
--- a/esphome/components/lvgl/lvcode.py
+++ b/esphome/components/lvgl/lvcode.py
@@ -183,17 +183,11 @@ class LvContext(LambdaContext):
super().__init__(parameters=self.args)
self.lv_component = lv_component
- async def add_init_lambda(self):
- if self.code_list:
- cg.add(self.lv_component.add_init_lambda(await self.get_lambda()))
- LvContext.added_lambda_count += 1
-
async def __aexit__(self, exc_type, exc_val, exc_tb):
await super().__aexit__(exc_type, exc_val, exc_tb)
- await self.add_init_lambda()
def add(self, expression: Union[Expression, Statement]):
- self.code_list.append(self.indented_statement(expression))
+ cg.add(expression)
return expression
def __call__(self, *args):
diff --git a/esphome/components/lvgl/lvgl_esphome.cpp b/esphome/components/lvgl/lvgl_esphome.cpp
index ddf41ae377..70cfb859de 100644
--- a/esphome/components/lvgl/lvgl_esphome.cpp
+++ b/esphome/components/lvgl/lvgl_esphome.cpp
@@ -5,16 +5,12 @@
#include "lvgl_hal.h"
#include "lvgl_esphome.h"
+#include
+
namespace esphome {
namespace lvgl {
static const char *const TAG = "lvgl";
-#if LV_USE_LOG
-static void log_cb(const char *buf) {
- esp_log_printf_(ESPHOME_LOG_LEVEL_INFO, TAG, 0, "%.*s", (int) strlen(buf) - 1, buf);
-}
-#endif // LV_USE_LOG
-
static const char *const EVENT_NAMES[] = {
"NONE",
"PRESSED",
@@ -88,6 +84,7 @@ lv_event_code_t lv_api_event; // NOLINT
lv_event_code_t lv_update_event; // NOLINT
void LvglComponent::dump_config() {
ESP_LOGCONFIG(TAG, "LVGL:");
+ ESP_LOGCONFIG(TAG, " Display width/height: %d x %d", this->disp_drv_.hor_res, this->disp_drv_.ver_res);
ESP_LOGCONFIG(TAG, " Rotation: %d", this->rotation);
ESP_LOGCONFIG(TAG, " Draw rounding: %d", (int) this->draw_rounding);
}
@@ -149,7 +146,7 @@ void LvglComponent::draw_buffer_(const lv_area_t *area, lv_color_t *ptr) {
lv_color_t *dst = this->rotate_buf_;
switch (this->rotation) {
case display::DISPLAY_ROTATION_90_DEGREES:
- for (lv_coord_t x = height - 1; x-- != 0;) {
+ for (lv_coord_t x = height; x-- != 0;) {
for (lv_coord_t y = 0; y != width; y++) {
dst[y * height + x] = *ptr++;
}
@@ -263,6 +260,39 @@ LVEncoderListener::LVEncoderListener(lv_indev_type_t type, uint16_t lpt, uint16_
}
#endif // USE_LVGL_KEY_LISTENER
+#if defined(USE_LVGL_DROPDOWN) || defined(LV_USE_ROLLER)
+std::string LvSelectable::get_selected_text() {
+ auto selected = this->get_selected_index();
+ if (selected >= this->options_.size())
+ return "";
+ return this->options_[selected];
+}
+
+static std::string join_string(std::vector options) {
+ return std::accumulate(
+ options.begin(), options.end(), std::string(),
+ [](const std::string &a, const std::string &b) -> std::string { return a + (a.length() > 0 ? "\n" : "") + b; });
+}
+
+void LvSelectable::set_selected_text(const std::string &text, lv_anim_enable_t anim) {
+ auto index = std::find(this->options_.begin(), this->options_.end(), text);
+ if (index != this->options_.end()) {
+ this->set_selected_index(index - this->options_.begin(), anim);
+ lv_event_send(this->obj, lv_api_event, nullptr);
+ }
+}
+
+void LvSelectable::set_options(std::vector options) {
+ auto index = this->get_selected_index();
+ if (index >= options.size())
+ index = options.size() - 1;
+ this->options_ = std::move(options);
+ this->set_option_string(join_string(this->options_).c_str());
+ lv_event_send(this->obj, LV_EVENT_REFRESH, nullptr);
+ this->set_selected_index(index, LV_ANIM_OFF);
+}
+#endif // USE_LVGL_DROPDOWN || LV_USE_ROLLER
+
#ifdef USE_LVGL_BUTTONMATRIX
void LvButtonMatrixType::set_obj(lv_obj_t *lv_obj) {
LvCompound::set_obj(lv_obj);
@@ -348,26 +378,48 @@ void LvglComponent::write_random_() {
}
}
-void LvglComponent::setup() {
- ESP_LOGCONFIG(TAG, "LVGL Setup starts");
-#if LV_USE_LOG
- lv_log_register_print_cb(log_cb);
-#endif
+/**
+ * @class LvglComponent
+ * @brief Component for rendering LVGL.
+ *
+ * This component renders LVGL widgets on a display. Some initialisation must be done in the constructor
+ * since LVGL needs to be initialised before any widgets can be created.
+ *
+ * @param displays a list of displays to render onto. All displays must have the same
+ * resolution.
+ * @param buffer_frac the fraction of the display resolution to use for the LVGL
+ * draw buffer. A higher value will make animations smoother but
+ * also increase memory usage.
+ * @param full_refresh if true, the display will be fully refreshed on every frame.
+ * If false, only changed areas will be updated.
+ * @param draw_rounding the rounding to use when drawing. A value of 1 will draw
+ * without any rounding, a value of 2 will round to the nearest
+ * multiple of 2, and so on.
+ * @param resume_on_input if true, this component will resume rendering when the user
+ * presses a key or clicks on the screen.
+ */
+LvglComponent::LvglComponent(std::vector displays, float buffer_frac, bool full_refresh,
+ int draw_rounding, bool resume_on_input)
+ : draw_rounding(draw_rounding),
+ displays_(std::move(displays)),
+ buffer_frac_(buffer_frac),
+ full_refresh_(full_refresh),
+ resume_on_input_(resume_on_input) {
lv_init();
lv_update_event = static_cast(lv_event_register_id());
lv_api_event = static_cast(lv_event_register_id());
auto *display = this->displays_[0];
size_t buffer_pixels = display->get_width() * display->get_height() / this->buffer_frac_;
auto buf_bytes = buffer_pixels * LV_COLOR_DEPTH / 8;
- auto *buf = lv_custom_mem_alloc(buf_bytes); // NOLINT
- if (buf == nullptr) {
-#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_ERROR
- ESP_LOGE(TAG, "Malloc failed to allocate %zu bytes", buf_bytes);
-#endif
- this->mark_failed();
- this->status_set_error("Memory allocation failure");
- return;
+ this->rotation = display->get_rotation();
+ if (this->rotation != display::DISPLAY_ROTATION_0_DEGREES) {
+ this->rotate_buf_ = static_cast(lv_custom_mem_alloc(buf_bytes)); // NOLINT
+ if (this->rotate_buf_ == nullptr)
+ return;
}
+ auto *buf = lv_custom_mem_alloc(buf_bytes); // NOLINT
+ if (buf == nullptr)
+ return;
lv_disp_draw_buf_init(&this->draw_buf_, buf, nullptr, buffer_pixels);
lv_disp_drv_init(&this->disp_drv_);
this->disp_drv_.draw_buf = &this->draw_buf_;
@@ -375,37 +427,36 @@ void LvglComponent::setup() {
this->disp_drv_.full_refresh = this->full_refresh_;
this->disp_drv_.flush_cb = static_flush_cb;
this->disp_drv_.rounder_cb = rounder_cb;
- this->rotation = display->get_rotation();
- if (this->rotation != display::DISPLAY_ROTATION_0_DEGREES) {
- this->rotate_buf_ = static_cast(lv_custom_mem_alloc(buf_bytes)); // NOLINT
- if (this->rotate_buf_ == nullptr) {
-#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_ERROR
- ESP_LOGE(TAG, "Malloc failed to allocate %zu bytes", buf_bytes);
-#endif
- this->mark_failed();
- this->status_set_error("Memory allocation failure");
- return;
- }
- }
- display->set_rotation(display::DISPLAY_ROTATION_0_DEGREES);
- switch (this->rotation) {
- default:
- this->disp_drv_.hor_res = (lv_coord_t) display->get_width();
- this->disp_drv_.ver_res = (lv_coord_t) display->get_height();
- break;
- case display::DISPLAY_ROTATION_90_DEGREES:
- case display::DISPLAY_ROTATION_270_DEGREES:
- this->disp_drv_.ver_res = (lv_coord_t) display->get_width();
- this->disp_drv_.hor_res = (lv_coord_t) display->get_height();
- break;
- }
+ this->disp_drv_.hor_res = (lv_coord_t) display->get_width();
+ this->disp_drv_.ver_res = (lv_coord_t) display->get_height();
this->disp_ = lv_disp_drv_register(&this->disp_drv_);
- for (const auto &v : this->init_lambdas_)
- v(this);
+}
+
+void LvglComponent::setup() {
+ if (this->draw_buf_.buf1 == nullptr) {
+ this->mark_failed();
+ this->status_set_error("Memory allocation failure");
+ return;
+ }
+ ESP_LOGCONFIG(TAG, "LVGL Setup starts");
+#if LV_USE_LOG
+ lv_log_register_print_cb([](const char *buf) {
+ auto next = strchr(buf, ')');
+ if (next != nullptr)
+ buf = next + 1;
+ while (isspace(*buf))
+ buf++;
+ esp_log_printf_(LVGL_LOG_LEVEL, TAG, 0, "%.*s", (int) strlen(buf) - 1, buf);
+ });
+#endif
+ // Rotation will be handled by our drawing function, so reset the display rotation.
+ for (auto *display : this->displays_)
+ display->set_rotation(display::DISPLAY_ROTATION_0_DEGREES);
this->show_page(0, LV_SCR_LOAD_ANIM_NONE, 0);
lv_disp_trig_activity(this->disp_);
ESP_LOGCONFIG(TAG, "LVGL Setup complete");
}
+
void LvglComponent::update() {
// update indicators
if (this->paused_) {
@@ -420,13 +471,6 @@ void LvglComponent::loop() {
}
lv_timer_handler_run_in_period(5);
}
-bool lv_is_pre_initialise() {
- if (!lv_is_initialized()) {
- ESP_LOGE(TAG, "LVGL call before component is initialised");
- return true;
- }
- return false;
-}
#ifdef USE_LVGL_ANIMIMG
void lv_animimg_stop(lv_obj_t *obj) {
diff --git a/esphome/components/lvgl/lvgl_esphome.h b/esphome/components/lvgl/lvgl_esphome.h
index b28a9bcbe1..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
@@ -18,11 +21,9 @@
#include "esphome/core/component.h"
#include "esphome/core/log.h"
#include
-#include
#include