mirror of
https://github.com/esphome/esphome.git
synced 2025-11-04 17:11:51 +00:00
Compare commits
21 Commits
select_opt
...
action_cha
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
38fe47b554 | ||
|
|
266e4ae91f | ||
|
|
99d1a9cf6e | ||
|
|
99ce989eae | ||
|
|
8aa8bb8f98 | ||
|
|
9c7cb30ae5 | ||
|
|
fb7dbc9910 | ||
|
|
3f12630a6b | ||
|
|
06d0787ee0 | ||
|
|
cb039b42aa | ||
|
|
f05f45af74 | ||
|
|
738cf4cace | ||
|
|
0a01fde503 | ||
|
|
52a5cccc77 | ||
|
|
a3dbaa7a95 | ||
|
|
47cc240368 | ||
|
|
21a343701d | ||
|
|
2f35a94d28 | ||
|
|
035a510aba | ||
|
|
c1023116f2 | ||
|
|
6c2f1c8a28 |
@@ -155,6 +155,7 @@ esphome/components/esp32_ble_tracker/* @bdraco
|
||||
esphome/components/esp32_camera_web_server/* @ayufan
|
||||
esphome/components/esp32_can/* @Sympatron
|
||||
esphome/components/esp32_hosted/* @swoboda1337
|
||||
esphome/components/esp32_hosted/update/* @swoboda1337
|
||||
esphome/components/esp32_improv/* @jesserockz
|
||||
esphome/components/esp32_rmt/* @jesserockz
|
||||
esphome/components/esp32_rmt_led_strip/* @jesserockz
|
||||
|
||||
@@ -877,7 +877,7 @@ uint16_t APIConnection::try_send_select_state(EntityBase *entity, APIConnection
|
||||
bool is_single) {
|
||||
auto *select = static_cast<select::Select *>(entity);
|
||||
SelectStateResponse resp;
|
||||
resp.set_state(StringRef(select->current_option()));
|
||||
resp.set_state(StringRef(select->state));
|
||||
resp.missing_state = !select->has_state();
|
||||
return fill_and_encode_entity_state(select, resp, SelectStateResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
|
||||
@@ -7,19 +7,19 @@ namespace copy {
|
||||
static const char *const TAG = "copy.select";
|
||||
|
||||
void CopySelect::setup() {
|
||||
source_->add_on_state_callback([this](const std::string &value, size_t index) { this->publish_state(index); });
|
||||
source_->add_on_state_callback([this](const std::string &value, size_t index) { this->publish_state(value); });
|
||||
|
||||
traits.set_options(source_->traits.get_options());
|
||||
|
||||
if (source_->has_state())
|
||||
this->publish_state(source_->active_index().value());
|
||||
this->publish_state(source_->state);
|
||||
}
|
||||
|
||||
void CopySelect::dump_config() { LOG_SELECT("", "Copy Select", this); }
|
||||
|
||||
void CopySelect::control(size_t index) {
|
||||
void CopySelect::control(const std::string &value) {
|
||||
auto call = source_->make_call();
|
||||
call.set_index(index);
|
||||
call.set_option(value);
|
||||
call.perform();
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ class CopySelect : public select::Select, public Component {
|
||||
void dump_config() override;
|
||||
|
||||
protected:
|
||||
void control(size_t index) override;
|
||||
void control(const std::string &value) override;
|
||||
|
||||
select::Select *source_;
|
||||
};
|
||||
|
||||
@@ -210,7 +210,7 @@ class Display : public PollingComponent {
|
||||
/// Fill the entire screen with the given color.
|
||||
virtual void fill(Color color);
|
||||
/// Clear the entire screen by filling it with OFF pixels.
|
||||
void clear();
|
||||
virtual void clear();
|
||||
|
||||
/// Get the calculated width of the display in pixels with rotation applied.
|
||||
virtual int get_width() { return this->get_width_internal(); }
|
||||
|
||||
@@ -42,7 +42,7 @@ std::string MenuItemSelect::get_value_text() const {
|
||||
result = this->value_getter_.value()(this);
|
||||
} else {
|
||||
if (this->select_var_ != nullptr) {
|
||||
result = this->select_var_->current_option();
|
||||
result = this->select_var_->state;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,21 +1,35 @@
|
||||
import importlib
|
||||
import pkgutil
|
||||
|
||||
from esphome import core, pins
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import display, spi
|
||||
from esphome.components.mipi import flatten_sequence, map_sequence
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_BUSY_PIN,
|
||||
CONF_CS_PIN,
|
||||
CONF_DATA_RATE,
|
||||
CONF_DC_PIN,
|
||||
CONF_DIMENSIONS,
|
||||
CONF_ENABLE_PIN,
|
||||
CONF_HEIGHT,
|
||||
CONF_ID,
|
||||
CONF_INIT_SEQUENCE,
|
||||
CONF_LAMBDA,
|
||||
CONF_MODEL,
|
||||
CONF_PAGES,
|
||||
CONF_RESET_DURATION,
|
||||
CONF_RESET_PIN,
|
||||
CONF_WIDTH,
|
||||
)
|
||||
|
||||
from . import models
|
||||
|
||||
AUTO_LOAD = ["split_buffer"]
|
||||
DEPENDENCIES = ["spi"]
|
||||
|
||||
CONF_INIT_SEQUENCE_ID = "init_sequence_id"
|
||||
|
||||
epaper_spi_ns = cg.esphome_ns.namespace("epaper_spi")
|
||||
EPaperBase = epaper_spi_ns.class_(
|
||||
"EPaperBase", cg.PollingComponent, spi.SPIDevice, display.DisplayBuffer
|
||||
@@ -24,30 +38,79 @@ EPaperBase = epaper_spi_ns.class_(
|
||||
EPaperSpectraE6 = epaper_spi_ns.class_("EPaperSpectraE6", EPaperBase)
|
||||
EPaper7p3InSpectraE6 = epaper_spi_ns.class_("EPaper7p3InSpectraE6", EPaperSpectraE6)
|
||||
|
||||
MODELS = {
|
||||
"7.3in-spectra-e6": EPaper7p3InSpectraE6,
|
||||
}
|
||||
|
||||
# Import all models dynamically from the models package
|
||||
for module_info in pkgutil.iter_modules(models.__path__):
|
||||
importlib.import_module(f".models.{module_info.name}", package=__package__)
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
display.FULL_DISPLAY_SCHEMA.extend(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(EPaperBase),
|
||||
cv.Required(CONF_DC_PIN): pins.gpio_output_pin_schema,
|
||||
cv.Required(CONF_MODEL): cv.one_of(*MODELS, lower=True, space="-"),
|
||||
cv.Optional(CONF_RESET_PIN): pins.gpio_output_pin_schema,
|
||||
cv.Optional(CONF_BUSY_PIN): pins.gpio_input_pin_schema,
|
||||
cv.Optional(CONF_RESET_DURATION): cv.All(
|
||||
cv.positive_time_period_milliseconds,
|
||||
cv.Range(max=core.TimePeriod(milliseconds=500)),
|
||||
),
|
||||
}
|
||||
)
|
||||
.extend(cv.polling_component_schema("60s"))
|
||||
.extend(spi.spi_device_schema()),
|
||||
cv.has_at_most_one_key(CONF_PAGES, CONF_LAMBDA),
|
||||
MODELS = models.EpaperModel.models
|
||||
|
||||
DIMENSION_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_WIDTH): cv.int_,
|
||||
cv.Required(CONF_HEIGHT): cv.int_,
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def model_schema(config):
|
||||
model = MODELS[config[CONF_MODEL]]
|
||||
class_name = epaper_spi_ns.class_(model.class_name, EPaperBase)
|
||||
cv_dimensions = cv.Optional if model.get_default(CONF_WIDTH) else cv.Required
|
||||
return (
|
||||
display.FULL_DISPLAY_SCHEMA.extend(
|
||||
spi.spi_device_schema(
|
||||
cs_pin_required=False,
|
||||
default_mode="MODE0",
|
||||
default_data_rate=model.get_default(CONF_DATA_RATE, 10_000_000),
|
||||
)
|
||||
)
|
||||
.extend(
|
||||
{
|
||||
model.option(pin): pins.gpio_output_pin_schema
|
||||
for pin in (CONF_RESET_PIN, CONF_CS_PIN, CONF_BUSY_PIN)
|
||||
}
|
||||
)
|
||||
.extend(
|
||||
{
|
||||
cv.Required(CONF_MODEL): cv.one_of(model.name, upper=True),
|
||||
model.option(CONF_DC_PIN, fallback=None): pins.gpio_output_pin_schema,
|
||||
cv.GenerateID(): cv.declare_id(class_name),
|
||||
cv.GenerateID(CONF_INIT_SEQUENCE_ID): cv.declare_id(cg.uint8),
|
||||
cv_dimensions(CONF_DIMENSIONS): DIMENSION_SCHEMA,
|
||||
model.option(CONF_ENABLE_PIN): cv.ensure_list(
|
||||
pins.gpio_output_pin_schema
|
||||
),
|
||||
model.option(CONF_INIT_SEQUENCE, cv.UNDEFINED): cv.ensure_list(
|
||||
map_sequence
|
||||
),
|
||||
model.option(CONF_RESET_DURATION, cv.UNDEFINED): cv.All(
|
||||
cv.positive_time_period_milliseconds,
|
||||
cv.Range(max=core.TimePeriod(milliseconds=500)),
|
||||
),
|
||||
}
|
||||
)
|
||||
)
|
||||
|
||||
|
||||
def customise_schema(config):
|
||||
"""
|
||||
Create a customised config schema for a specific model and validate the configuration.
|
||||
:param config: The configuration dictionary to validate
|
||||
:return: The validated configuration dictionary
|
||||
:raises cv.Invalid: If the configuration is invalid
|
||||
"""
|
||||
config = cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_MODEL): cv.one_of(*MODELS, upper=True),
|
||||
},
|
||||
extra=cv.ALLOW_EXTRA,
|
||||
)(config)
|
||||
return model_schema(config)(config)
|
||||
|
||||
|
||||
CONFIG_SCHEMA = customise_schema
|
||||
|
||||
FINAL_VALIDATE_SCHEMA = spi.final_validate_device_schema(
|
||||
"epaper_spi", require_miso=False, require_mosi=True
|
||||
)
|
||||
@@ -56,8 +119,23 @@ FINAL_VALIDATE_SCHEMA = spi.final_validate_device_schema(
|
||||
async def to_code(config):
|
||||
model = MODELS[config[CONF_MODEL]]
|
||||
|
||||
rhs = model.new()
|
||||
var = cg.Pvariable(config[CONF_ID], rhs, model)
|
||||
init_sequence = config.get(CONF_INIT_SEQUENCE)
|
||||
if init_sequence is None:
|
||||
init_sequence = model.get_init_sequence(config)
|
||||
init_sequence = flatten_sequence(init_sequence)
|
||||
init_sequence_length = len(init_sequence)
|
||||
init_sequence_id = cg.static_const_array(
|
||||
config[CONF_INIT_SEQUENCE_ID], init_sequence
|
||||
)
|
||||
width, height = model.get_dimensions(config)
|
||||
var = cg.new_Pvariable(
|
||||
config[CONF_ID],
|
||||
model.name,
|
||||
width,
|
||||
height,
|
||||
init_sequence_id,
|
||||
init_sequence_length,
|
||||
)
|
||||
|
||||
await display.register_display(var, config)
|
||||
await spi.register_spi_device(var, config)
|
||||
|
||||
@@ -8,33 +8,20 @@ namespace esphome::epaper_spi {
|
||||
|
||||
static const char *const TAG = "epaper_spi";
|
||||
|
||||
static const LogString *epaper_state_to_string(EPaperState state) {
|
||||
switch (state) {
|
||||
case EPaperState::IDLE:
|
||||
return LOG_STR("IDLE");
|
||||
case EPaperState::UPDATE:
|
||||
return LOG_STR("UPDATE");
|
||||
case EPaperState::RESET:
|
||||
return LOG_STR("RESET");
|
||||
case EPaperState::INITIALISE:
|
||||
return LOG_STR("INITIALISE");
|
||||
case EPaperState::TRANSFER_DATA:
|
||||
return LOG_STR("TRANSFER_DATA");
|
||||
case EPaperState::POWER_ON:
|
||||
return LOG_STR("POWER_ON");
|
||||
case EPaperState::REFRESH_SCREEN:
|
||||
return LOG_STR("REFRESH_SCREEN");
|
||||
case EPaperState::POWER_OFF:
|
||||
return LOG_STR("POWER_OFF");
|
||||
case EPaperState::DEEP_SLEEP:
|
||||
return LOG_STR("DEEP_SLEEP");
|
||||
default:
|
||||
return LOG_STR("UNKNOWN");
|
||||
}
|
||||
static constexpr const char *const EPAPER_STATE_STRINGS[] = {
|
||||
"IDLE", "UPDATE", "RESET", "RESET_END",
|
||||
|
||||
"SHOULD_WAIT", "INITIALISE", "TRANSFER_DATA", "POWER_ON", "REFRESH_SCREEN", "POWER_OFF", "DEEP_SLEEP",
|
||||
};
|
||||
|
||||
const char *EPaperBase::epaper_state_to_string_() {
|
||||
if (auto idx = static_cast<unsigned>(this->state_); idx < std::size(EPAPER_STATE_STRINGS))
|
||||
return EPAPER_STATE_STRINGS[idx];
|
||||
return "Unknown";
|
||||
}
|
||||
|
||||
void EPaperBase::setup() {
|
||||
if (!this->init_buffer_(this->get_buffer_length())) {
|
||||
if (!this->init_buffer_(this->buffer_length_)) {
|
||||
this->mark_failed("Failed to initialise buffer");
|
||||
return;
|
||||
}
|
||||
@@ -50,7 +37,7 @@ bool EPaperBase::init_buffer_(size_t buffer_length) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void EPaperBase::setup_pins_() {
|
||||
void EPaperBase::setup_pins_() const {
|
||||
this->dc_pin_->setup(); // OUTPUT
|
||||
this->dc_pin_->digital_write(false);
|
||||
|
||||
@@ -81,11 +68,7 @@ void EPaperBase::data(uint8_t value) {
|
||||
// write a command followed by zero or more bytes of data.
|
||||
// The command is the first byte, length is the length of data only in the second byte, followed by the data.
|
||||
// [COMMAND, LENGTH, DATA...]
|
||||
void EPaperBase::cmd_data(const uint8_t *data) {
|
||||
const uint8_t command = data[0];
|
||||
const uint8_t length = data[1];
|
||||
const uint8_t *ptr = data + 2;
|
||||
|
||||
void EPaperBase::cmd_data(uint8_t command, const uint8_t *ptr, size_t length) {
|
||||
ESP_LOGVV(TAG, "Command: 0x%02X, Length: %d, Data: %s", command, length,
|
||||
format_hex_pretty(ptr, length, '.', false).c_str());
|
||||
|
||||
@@ -99,91 +82,146 @@ void EPaperBase::cmd_data(const uint8_t *data) {
|
||||
this->disable();
|
||||
}
|
||||
|
||||
bool EPaperBase::is_idle_() {
|
||||
bool EPaperBase::is_idle_() const {
|
||||
if (this->busy_pin_ == nullptr) {
|
||||
return true;
|
||||
}
|
||||
return this->busy_pin_->digital_read();
|
||||
return !this->busy_pin_->digital_read();
|
||||
}
|
||||
|
||||
void EPaperBase::reset() {
|
||||
bool EPaperBase::reset_() const {
|
||||
if (this->reset_pin_ != nullptr) {
|
||||
this->reset_pin_->digital_write(false);
|
||||
this->disable_loop();
|
||||
this->set_timeout(this->reset_duration_, [this] {
|
||||
this->reset_pin_->digital_write(true);
|
||||
this->set_timeout(20, [this] { this->enable_loop(); });
|
||||
});
|
||||
if (this->state_ == EPaperState::RESET) {
|
||||
this->reset_pin_->digital_write(false);
|
||||
return false;
|
||||
}
|
||||
this->reset_pin_->digital_write(true);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void EPaperBase::update() {
|
||||
if (!this->state_queue_.empty()) {
|
||||
ESP_LOGE(TAG, "Display update already in progress - %s",
|
||||
LOG_STR_ARG(epaper_state_to_string(this->state_queue_.front())));
|
||||
if (this->state_ != EPaperState::IDLE) {
|
||||
ESP_LOGE(TAG, "Display already in state %s", epaper_state_to_string_());
|
||||
return;
|
||||
}
|
||||
|
||||
this->state_queue_.push(EPaperState::UPDATE);
|
||||
this->state_queue_.push(EPaperState::RESET);
|
||||
this->state_queue_.push(EPaperState::INITIALISE);
|
||||
this->state_queue_.push(EPaperState::TRANSFER_DATA);
|
||||
this->state_queue_.push(EPaperState::POWER_ON);
|
||||
this->state_queue_.push(EPaperState::REFRESH_SCREEN);
|
||||
this->state_queue_.push(EPaperState::POWER_OFF);
|
||||
this->state_queue_.push(EPaperState::DEEP_SLEEP);
|
||||
this->state_queue_.push(EPaperState::IDLE);
|
||||
|
||||
this->set_state_(EPaperState::RESET);
|
||||
this->enable_loop();
|
||||
}
|
||||
|
||||
void EPaperBase::wait_for_idle_(bool should_wait) {
|
||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
|
||||
if (should_wait) {
|
||||
this->waiting_for_idle_start_ = millis();
|
||||
this->waiting_for_idle_last_print_ = this->waiting_for_idle_start_;
|
||||
}
|
||||
#endif
|
||||
this->waiting_for_idle_ = should_wait;
|
||||
}
|
||||
|
||||
/**
|
||||
* Called during the loop task.
|
||||
* First defer for any pending delays, then check if we are waiting for the display to become idle.
|
||||
* If not waiting for idle, process the state machine.
|
||||
*/
|
||||
|
||||
void EPaperBase::loop() {
|
||||
auto now = millis();
|
||||
if (this->delay_until_ != 0) {
|
||||
// using modulus arithmetic to handle wrap-around
|
||||
int diff = now - this->delay_until_;
|
||||
if (diff < 0) {
|
||||
return;
|
||||
}
|
||||
this->delay_until_ = 0;
|
||||
}
|
||||
if (this->waiting_for_idle_) {
|
||||
if (this->is_idle_()) {
|
||||
this->waiting_for_idle_ = false;
|
||||
ESP_LOGV(TAG, "Screen now idle after %u ms", (unsigned) (millis() - this->waiting_for_idle_start_));
|
||||
} else {
|
||||
if (App.get_loop_component_start_time() - this->waiting_for_idle_last_print_ >= 1000) {
|
||||
ESP_LOGV(TAG, "Waiting for idle");
|
||||
this->waiting_for_idle_last_print_ = App.get_loop_component_start_time();
|
||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
|
||||
if (now - this->waiting_for_idle_last_print_ >= 1000) {
|
||||
ESP_LOGV(TAG, "Waiting for idle in state %s", this->epaper_state_to_string_());
|
||||
this->waiting_for_idle_last_print_ = millis();
|
||||
}
|
||||
#endif
|
||||
return;
|
||||
}
|
||||
}
|
||||
this->process_state_();
|
||||
}
|
||||
|
||||
auto state = this->state_queue_.front();
|
||||
|
||||
switch (state) {
|
||||
/**
|
||||
* Process the state machine.
|
||||
* Typical state sequence:
|
||||
* IDLE -> RESET -> RESET_END -> UPDATE -> INITIALISE -> TRANSFER_DATA -> POWER_ON -> REFRESH_SCREEN -> POWER_OFF ->
|
||||
* DEEP_SLEEP -> IDLE
|
||||
*
|
||||
* Should a subclassed class need to override this, the method will need to be made virtual.
|
||||
*/
|
||||
void EPaperBase::process_state_() {
|
||||
ESP_LOGV(TAG, "Process state entered in state %s", epaper_state_to_string_());
|
||||
switch (this->state_) {
|
||||
default:
|
||||
ESP_LOGD(TAG, "Display is in unhandled state %s", epaper_state_to_string_());
|
||||
this->disable_loop();
|
||||
break;
|
||||
case EPaperState::IDLE:
|
||||
this->disable_loop();
|
||||
break;
|
||||
case EPaperState::RESET:
|
||||
case EPaperState::RESET_END:
|
||||
if (this->reset_()) {
|
||||
this->set_state_(EPaperState::UPDATE);
|
||||
} else {
|
||||
this->set_state_(EPaperState::RESET_END);
|
||||
}
|
||||
break;
|
||||
case EPaperState::UPDATE:
|
||||
this->do_update_(); // Calls ESPHome (current page) lambda
|
||||
break;
|
||||
case EPaperState::RESET:
|
||||
this->reset();
|
||||
this->set_state_(EPaperState::INITIALISE);
|
||||
break;
|
||||
case EPaperState::INITIALISE:
|
||||
this->initialise_();
|
||||
this->set_state_(EPaperState::TRANSFER_DATA);
|
||||
break;
|
||||
case EPaperState::TRANSFER_DATA:
|
||||
if (!this->transfer_data()) {
|
||||
return; // Not done yet, come back next loop
|
||||
}
|
||||
this->set_state_(EPaperState::POWER_ON);
|
||||
break;
|
||||
case EPaperState::POWER_ON:
|
||||
this->power_on();
|
||||
this->set_state_(EPaperState::REFRESH_SCREEN);
|
||||
break;
|
||||
case EPaperState::REFRESH_SCREEN:
|
||||
this->refresh_screen();
|
||||
this->set_state_(EPaperState::POWER_OFF);
|
||||
break;
|
||||
case EPaperState::POWER_OFF:
|
||||
this->power_off();
|
||||
this->set_state_(EPaperState::DEEP_SLEEP);
|
||||
break;
|
||||
case EPaperState::DEEP_SLEEP:
|
||||
this->deep_sleep();
|
||||
this->set_state_(EPaperState::IDLE);
|
||||
break;
|
||||
}
|
||||
this->state_queue_.pop();
|
||||
}
|
||||
|
||||
void EPaperBase::set_state_(EPaperState state, uint16_t delay) {
|
||||
ESP_LOGV(TAG, "Exit state %s", this->epaper_state_to_string_());
|
||||
this->state_ = state;
|
||||
this->wait_for_idle_(state > EPaperState::SHOULD_WAIT);
|
||||
if (delay != 0) {
|
||||
this->delay_until_ = millis() + delay;
|
||||
} else {
|
||||
this->delay_until_ = 0;
|
||||
}
|
||||
ESP_LOGV(TAG, "Enter state %s, delay %u, wait_for_idle=%s", this->epaper_state_to_string_(), delay,
|
||||
TRUEFALSE(this->waiting_for_idle_));
|
||||
}
|
||||
|
||||
void EPaperBase::start_command_() {
|
||||
@@ -203,25 +241,39 @@ void EPaperBase::on_safe_shutdown() { this->deep_sleep(); }
|
||||
|
||||
void EPaperBase::initialise_() {
|
||||
size_t index = 0;
|
||||
const auto &sequence = this->init_sequence_;
|
||||
const size_t sequence_size = this->init_sequence_length_;
|
||||
while (index != sequence_size) {
|
||||
if (sequence_size - index < 2) {
|
||||
this->mark_failed("Malformed init sequence");
|
||||
return;
|
||||
}
|
||||
const auto *ptr = sequence + index;
|
||||
const uint8_t length = ptr[1];
|
||||
if (sequence_size - index < length + 2) {
|
||||
this->mark_failed("Malformed init sequence");
|
||||
return;
|
||||
}
|
||||
|
||||
this->cmd_data(ptr);
|
||||
index += length + 2;
|
||||
auto *sequence = this->init_sequence_;
|
||||
auto length = this->init_sequence_length_;
|
||||
while (index != length) {
|
||||
if (length - index < 2) {
|
||||
this->mark_failed("Malformed init sequence");
|
||||
return;
|
||||
}
|
||||
const uint8_t cmd = sequence[index++];
|
||||
if (const uint8_t x = sequence[index++]; x == DELAY_FLAG) {
|
||||
ESP_LOGV(TAG, "Delay %dms", cmd);
|
||||
delay(cmd);
|
||||
} else {
|
||||
const uint8_t num_args = x & 0x7F;
|
||||
if (length - index < num_args) {
|
||||
ESP_LOGE(TAG, "Malformed init sequence, cmd = %X, num_args = %u", cmd, num_args);
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
ESP_LOGV(TAG, "Command %02X, length %d", cmd, num_args);
|
||||
this->cmd_data(cmd, sequence + index, num_args);
|
||||
index += num_args;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
this->power_on();
|
||||
void EPaperBase::dump_config() {
|
||||
LOG_DISPLAY("", "E-Paper SPI", this);
|
||||
ESP_LOGCONFIG(TAG, " Model: %s", this->name_);
|
||||
LOG_PIN(" Reset Pin: ", this->reset_pin_);
|
||||
LOG_PIN(" DC Pin: ", this->dc_pin_);
|
||||
LOG_PIN(" Busy Pin: ", this->busy_pin_);
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
}
|
||||
|
||||
} // namespace esphome::epaper_spi
|
||||
|
||||
@@ -8,36 +8,48 @@
|
||||
#include <queue>
|
||||
|
||||
namespace esphome::epaper_spi {
|
||||
using namespace display;
|
||||
|
||||
enum class EPaperState : uint8_t {
|
||||
IDLE,
|
||||
UPDATE,
|
||||
RESET,
|
||||
INITIALISE,
|
||||
TRANSFER_DATA,
|
||||
POWER_ON,
|
||||
REFRESH_SCREEN,
|
||||
POWER_OFF,
|
||||
DEEP_SLEEP,
|
||||
IDLE, // not doing anything
|
||||
UPDATE, // update the buffer
|
||||
RESET, // drive reset low (active)
|
||||
RESET_END, // drive reset high (inactive)
|
||||
|
||||
SHOULD_WAIT, // states higher than this should wait for the display to be not busy
|
||||
INITIALISE, // send the init sequence
|
||||
TRANSFER_DATA, // transfer data to the display
|
||||
POWER_ON, // power on the display
|
||||
REFRESH_SCREEN, // send refresh command
|
||||
POWER_OFF, // power off the display
|
||||
DEEP_SLEEP, // deep sleep the display
|
||||
};
|
||||
|
||||
static const uint8_t MAX_TRANSFER_TIME = 10; // Transfer in 10ms blocks to allow the loop to run
|
||||
static constexpr uint8_t MAX_TRANSFER_TIME = 10; // Transfer in 10ms blocks to allow the loop to run
|
||||
static constexpr uint8_t DELAY_FLAG = 0xFF;
|
||||
|
||||
class EPaperBase : public display::DisplayBuffer,
|
||||
class EPaperBase : public DisplayBuffer,
|
||||
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
|
||||
spi::DATA_RATE_2MHZ> {
|
||||
public:
|
||||
EPaperBase(const uint8_t *init_sequence, const size_t init_sequence_length)
|
||||
: init_sequence_length_(init_sequence_length), init_sequence_(init_sequence) {}
|
||||
EPaperBase(const char *name, uint16_t width, uint16_t height, const uint8_t *init_sequence,
|
||||
size_t init_sequence_length, DisplayType display_type = DISPLAY_TYPE_BINARY)
|
||||
: name_(name),
|
||||
width_(width),
|
||||
height_(height),
|
||||
init_sequence_(init_sequence),
|
||||
init_sequence_length_(init_sequence_length),
|
||||
display_type_(display_type) {}
|
||||
void set_dc_pin(GPIOPin *dc_pin) { dc_pin_ = dc_pin; }
|
||||
float get_setup_priority() const override;
|
||||
void set_reset_pin(GPIOPin *reset) { this->reset_pin_ = reset; }
|
||||
void set_busy_pin(GPIOPin *busy) { this->busy_pin_ = busy; }
|
||||
void set_reset_duration(uint32_t reset_duration) { this->reset_duration_ = reset_duration; }
|
||||
void dump_config() override;
|
||||
|
||||
void command(uint8_t value);
|
||||
void data(uint8_t value);
|
||||
void cmd_data(const uint8_t *data);
|
||||
void cmd_data(uint8_t command, const uint8_t *ptr, size_t length);
|
||||
|
||||
void update() override;
|
||||
void loop() override;
|
||||
@@ -46,48 +58,84 @@ class EPaperBase : public display::DisplayBuffer,
|
||||
|
||||
void on_safe_shutdown() override;
|
||||
|
||||
DisplayType get_display_type() override { return this->display_type_; };
|
||||
|
||||
protected:
|
||||
bool is_idle_();
|
||||
void setup_pins_();
|
||||
virtual void reset();
|
||||
int get_height_internal() override { return this->height_; };
|
||||
int get_width_internal() override { return this->width_; };
|
||||
void process_state_();
|
||||
|
||||
const char *epaper_state_to_string_();
|
||||
bool is_idle_() const;
|
||||
void setup_pins_() const;
|
||||
bool reset_() const;
|
||||
void initialise_();
|
||||
void wait_for_idle_(bool should_wait);
|
||||
bool init_buffer_(size_t buffer_length);
|
||||
|
||||
virtual int get_width_controller() { return this->get_width_internal(); };
|
||||
virtual void deep_sleep() = 0;
|
||||
|
||||
/**
|
||||
* Methods that must be implemented by concrete classes to control the display
|
||||
*/
|
||||
/**
|
||||
* Send data to the device via SPI
|
||||
* @return true if done, false if should be called next loop
|
||||
* @return true if done, false if it should be called next loop
|
||||
*/
|
||||
virtual bool transfer_data() = 0;
|
||||
/**
|
||||
* Refresh the screen after data transfer
|
||||
*/
|
||||
virtual void refresh_screen() = 0;
|
||||
|
||||
/**
|
||||
* Power the display on
|
||||
*/
|
||||
virtual void power_on() = 0;
|
||||
/**
|
||||
* Power the display off
|
||||
*/
|
||||
virtual void power_off() = 0;
|
||||
virtual uint32_t get_buffer_length() = 0;
|
||||
|
||||
/**
|
||||
* Place the display into deep sleep
|
||||
*/
|
||||
virtual void deep_sleep() = 0;
|
||||
|
||||
void set_state_(EPaperState state, uint16_t delay = 0);
|
||||
|
||||
void start_command_();
|
||||
void end_command_();
|
||||
void start_data_();
|
||||
void end_data_();
|
||||
|
||||
const size_t init_sequence_length_{0};
|
||||
// properties initialised in the constructor
|
||||
const char *name_;
|
||||
uint16_t width_;
|
||||
uint16_t height_;
|
||||
const uint8_t *init_sequence_;
|
||||
size_t init_sequence_length_;
|
||||
DisplayType display_type_;
|
||||
|
||||
size_t current_data_index_{0};
|
||||
size_t buffer_length_{};
|
||||
size_t current_data_index_{0}; // used by data transfer to track progress
|
||||
uint32_t reset_duration_{200};
|
||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
|
||||
uint32_t transfer_start_time_{};
|
||||
uint32_t waiting_for_idle_last_print_{0};
|
||||
uint32_t waiting_for_idle_start_{0};
|
||||
#endif
|
||||
|
||||
GPIOPin *dc_pin_;
|
||||
GPIOPin *busy_pin_{nullptr};
|
||||
GPIOPin *reset_pin_{nullptr};
|
||||
|
||||
const uint8_t *init_sequence_{nullptr};
|
||||
GPIOPin *dc_pin_{};
|
||||
GPIOPin *busy_pin_{};
|
||||
GPIOPin *reset_pin_{};
|
||||
|
||||
bool waiting_for_idle_{false};
|
||||
uint32_t delay_until_{0};
|
||||
|
||||
split_buffer::SplitBuffer buffer_;
|
||||
|
||||
std::queue<EPaperState> state_queue_{{EPaperState::IDLE}};
|
||||
EPaperState state_{EPaperState::IDLE};
|
||||
};
|
||||
|
||||
} // namespace esphome::epaper_spi
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
#include "epaper_spi_model_7p3in_spectra_e6.h"
|
||||
|
||||
namespace esphome::epaper_spi {
|
||||
|
||||
static constexpr const char *const TAG = "epaper_spi.7.3in-spectra-e6";
|
||||
|
||||
void EPaper7p3InSpectraE6::power_on() {
|
||||
ESP_LOGI(TAG, "Power on");
|
||||
this->command(0x04);
|
||||
this->waiting_for_idle_ = true;
|
||||
}
|
||||
|
||||
void EPaper7p3InSpectraE6::power_off() {
|
||||
ESP_LOGI(TAG, "Power off");
|
||||
this->command(0x02);
|
||||
this->data(0x00);
|
||||
this->waiting_for_idle_ = true;
|
||||
}
|
||||
|
||||
void EPaper7p3InSpectraE6::refresh_screen() {
|
||||
ESP_LOGI(TAG, "Refresh");
|
||||
this->command(0x12);
|
||||
this->data(0x00);
|
||||
this->waiting_for_idle_ = true;
|
||||
}
|
||||
|
||||
void EPaper7p3InSpectraE6::deep_sleep() {
|
||||
ESP_LOGI(TAG, "Deep sleep");
|
||||
this->command(0x07);
|
||||
this->data(0xA5);
|
||||
}
|
||||
|
||||
void EPaper7p3InSpectraE6::dump_config() {
|
||||
LOG_DISPLAY("", "E-Paper SPI", this);
|
||||
ESP_LOGCONFIG(TAG, " Model: 7.3in Spectra E6");
|
||||
LOG_PIN(" Reset Pin: ", this->reset_pin_);
|
||||
LOG_PIN(" DC Pin: ", this->dc_pin_);
|
||||
LOG_PIN(" Busy Pin: ", this->busy_pin_);
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
}
|
||||
|
||||
} // namespace esphome::epaper_spi
|
||||
@@ -1,45 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include "epaper_spi_spectra_e6.h"
|
||||
|
||||
namespace esphome::epaper_spi {
|
||||
|
||||
class EPaper7p3InSpectraE6 : public EPaperSpectraE6 {
|
||||
static constexpr const uint16_t WIDTH = 800;
|
||||
static constexpr const uint16_t HEIGHT = 480;
|
||||
// clang-format off
|
||||
|
||||
// Command, data length, data
|
||||
static constexpr uint8_t INIT_SEQUENCE[] = {
|
||||
0xAA, 6, 0x49, 0x55, 0x20, 0x08, 0x09, 0x18,
|
||||
0x01, 1, 0x3F,
|
||||
0x00, 2, 0x5F, 0x69,
|
||||
0x03, 4, 0x00, 0x54, 0x00, 0x44,
|
||||
0x05, 4, 0x40, 0x1F, 0x1F, 0x2C,
|
||||
0x06, 4, 0x6F, 0x1F, 0x17, 0x49,
|
||||
0x08, 4, 0x6F, 0x1F, 0x1F, 0x22,
|
||||
0x30, 1, 0x03,
|
||||
0x50, 1, 0x3F,
|
||||
0x60, 2, 0x02, 0x00,
|
||||
0x61, 4, WIDTH / 256, WIDTH % 256, HEIGHT / 256, HEIGHT % 256,
|
||||
0x84, 1, 0x01,
|
||||
0xE3, 1, 0x2F,
|
||||
};
|
||||
// clang-format on
|
||||
|
||||
public:
|
||||
EPaper7p3InSpectraE6() : EPaperSpectraE6(INIT_SEQUENCE, sizeof(INIT_SEQUENCE)) {}
|
||||
|
||||
void dump_config() override;
|
||||
|
||||
protected:
|
||||
int get_width_internal() override { return WIDTH; };
|
||||
int get_height_internal() override { return HEIGHT; };
|
||||
|
||||
void refresh_screen() override;
|
||||
void power_on() override;
|
||||
void power_off() override;
|
||||
void deep_sleep() override;
|
||||
};
|
||||
|
||||
} // namespace esphome::epaper_spi
|
||||
@@ -1,135 +1,166 @@
|
||||
#include "epaper_spi_spectra_e6.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome::epaper_spi {
|
||||
|
||||
static constexpr const char *const TAG = "epaper_spi.6c";
|
||||
static constexpr size_t MAX_TRANSFER_SIZE = 128;
|
||||
static constexpr unsigned char GRAY_THRESHOLD = 50;
|
||||
|
||||
static inline uint8_t color_to_hex(Color color) {
|
||||
if (color.red > 127) {
|
||||
if (color.green > 170) {
|
||||
if (color.blue > 127) {
|
||||
return 0x1; // White
|
||||
} else {
|
||||
return 0x2; // Yellow
|
||||
}
|
||||
} else {
|
||||
return 0x3; // Red (or Magenta)
|
||||
}
|
||||
} else {
|
||||
if (color.green > 127) {
|
||||
if (color.blue > 127) {
|
||||
return 0x5; // Cyan -> Blue
|
||||
} else {
|
||||
return 0x6; // Green
|
||||
}
|
||||
} else {
|
||||
if (color.blue > 127) {
|
||||
return 0x5; // Blue
|
||||
} else {
|
||||
return 0x0; // Black
|
||||
}
|
||||
enum E6Color {
|
||||
BLACK,
|
||||
WHITE,
|
||||
YELLOW,
|
||||
RED,
|
||||
SKIP_1,
|
||||
BLUE,
|
||||
GREEN,
|
||||
CYAN,
|
||||
SKIP_2,
|
||||
};
|
||||
|
||||
static uint8_t color_to_hex(Color color) {
|
||||
// --- Step 1: Check for Grayscale (Black or White) ---
|
||||
// We define "grayscale" as a color where the min and max components
|
||||
// are close to each other.
|
||||
unsigned char max_rgb = std::max({color.r, color.g, color.b});
|
||||
unsigned char min_rgb = std::min({color.r, color.g, color.b});
|
||||
|
||||
if ((max_rgb - min_rgb) < GRAY_THRESHOLD) {
|
||||
// It's a shade of gray. Map to BLACK or WHITE.
|
||||
// We split the luminance at the halfway point (382 = (255*3)/2)
|
||||
if ((static_cast<int>(color.r) + color.g + color.b) > 382) {
|
||||
return WHITE;
|
||||
}
|
||||
return BLACK;
|
||||
}
|
||||
// --- Step 2: Check for Primary/Secondary Colors ---
|
||||
// If it's not gray, it's a color. We check which components are
|
||||
// "on" (over 128) vs "off". This divides the RGB cube into 8 corners.
|
||||
bool r_on = (color.r > 128);
|
||||
bool g_on = (color.g > 128);
|
||||
bool b_on = (color.b > 128);
|
||||
|
||||
if (r_on && g_on && !b_on) {
|
||||
return YELLOW;
|
||||
}
|
||||
if (r_on && !g_on && !b_on) {
|
||||
return RED;
|
||||
}
|
||||
if (!r_on && g_on && !b_on) {
|
||||
return GREEN;
|
||||
}
|
||||
if (!r_on && !g_on && b_on) {
|
||||
return BLUE;
|
||||
}
|
||||
// Handle "impure" colors (Cyan, Magenta)
|
||||
if (!r_on && g_on && b_on) {
|
||||
// Cyan (G+B) -> Closest is Green or Blue. Pick Green.
|
||||
return GREEN;
|
||||
}
|
||||
if (r_on && !g_on) {
|
||||
// Magenta (R+B) -> Closest is Red or Blue. Pick Red.
|
||||
return RED;
|
||||
}
|
||||
// Handle the remaining corners (White-ish, Black-ish)
|
||||
if (r_on) {
|
||||
// All high (but not gray) -> White
|
||||
return WHITE;
|
||||
}
|
||||
// !r_on && !g_on && !b_on
|
||||
// All low (but not gray) -> Black
|
||||
return BLACK;
|
||||
}
|
||||
|
||||
void EPaperSpectraE6::power_on() {
|
||||
ESP_LOGD(TAG, "Power on");
|
||||
this->command(0x04);
|
||||
}
|
||||
|
||||
void EPaperSpectraE6::power_off() {
|
||||
ESP_LOGD(TAG, "Power off");
|
||||
this->command(0x02);
|
||||
this->data(0x00);
|
||||
}
|
||||
|
||||
void EPaperSpectraE6::refresh_screen() {
|
||||
ESP_LOGD(TAG, "Refresh");
|
||||
this->command(0x12);
|
||||
this->data(0x00);
|
||||
}
|
||||
|
||||
void EPaperSpectraE6::deep_sleep() {
|
||||
ESP_LOGD(TAG, "Deep sleep");
|
||||
this->command(0x07);
|
||||
this->data(0xA5);
|
||||
}
|
||||
|
||||
void EPaperSpectraE6::fill(Color color) {
|
||||
uint8_t pixel_color;
|
||||
if (color.is_on()) {
|
||||
pixel_color = color_to_hex(color);
|
||||
} else {
|
||||
pixel_color = 0x1;
|
||||
}
|
||||
auto pixel_color = color_to_hex(color);
|
||||
|
||||
// We store 8 bitset<3> in 3 bytes
|
||||
// | byte 1 | byte 2 | byte 3 |
|
||||
// |aaabbbaa|abbbaaab|bbaaabbb|
|
||||
uint8_t byte_1 = pixel_color << 5 | pixel_color << 2 | pixel_color >> 1;
|
||||
uint8_t byte_2 = pixel_color << 7 | pixel_color << 4 | pixel_color << 1 | pixel_color >> 2;
|
||||
uint8_t byte_3 = pixel_color << 6 | pixel_color << 3 | pixel_color << 0;
|
||||
|
||||
const size_t buffer_length = this->get_buffer_length();
|
||||
for (size_t i = 0; i < buffer_length; i += 3) {
|
||||
this->buffer_[i + 0] = byte_1;
|
||||
this->buffer_[i + 1] = byte_2;
|
||||
this->buffer_[i + 2] = byte_3;
|
||||
}
|
||||
// We store 2 pixels per byte
|
||||
this->buffer_.fill(pixel_color + (pixel_color << 4));
|
||||
}
|
||||
|
||||
uint32_t EPaperSpectraE6::get_buffer_length() {
|
||||
// 6 colors buffer, 1 pixel = 3 bits, we will store 8 pixels in 24 bits = 3 bytes
|
||||
return this->get_width_controller() * this->get_height_internal() / 8u * 3u;
|
||||
void EPaperSpectraE6::clear() {
|
||||
// clear buffer to white, just like real paper.
|
||||
this->fill(COLOR_ON);
|
||||
}
|
||||
|
||||
void HOT EPaperSpectraE6::draw_absolute_pixel_internal(int x, int y, Color color) {
|
||||
if (x >= this->get_width_internal() || y >= this->get_height_internal() || x < 0 || y < 0)
|
||||
if (x >= this->width_ || y >= this->height_ || x < 0 || y < 0)
|
||||
return;
|
||||
|
||||
uint8_t pixel_bits = color_to_hex(color);
|
||||
auto pixel_bits = color_to_hex(color);
|
||||
uint32_t pixel_position = x + y * this->get_width_controller();
|
||||
uint32_t first_bit_position = pixel_position * 3;
|
||||
uint32_t byte_position = first_bit_position / 8u;
|
||||
uint32_t byte_subposition = first_bit_position % 8u;
|
||||
|
||||
if (byte_subposition <= 5) {
|
||||
this->buffer_[byte_position] = (this->buffer_[byte_position] & (0xFF ^ (0b111 << (5 - byte_subposition)))) |
|
||||
(pixel_bits << (5 - byte_subposition));
|
||||
uint32_t byte_position = pixel_position / 2;
|
||||
auto original = this->buffer_[byte_position];
|
||||
if ((pixel_position & 1) != 0) {
|
||||
this->buffer_[byte_position] = (original & 0xF0) | pixel_bits;
|
||||
} else {
|
||||
this->buffer_[byte_position] = (this->buffer_[byte_position] & (0xFF ^ (0b111 >> (byte_subposition - 5)))) |
|
||||
(pixel_bits >> (byte_subposition - 5));
|
||||
|
||||
this->buffer_[byte_position + 1] =
|
||||
(this->buffer_[byte_position + 1] & (0xFF ^ (0xFF & (0b111 << (13 - byte_subposition))))) |
|
||||
(pixel_bits << (13 - byte_subposition));
|
||||
this->buffer_[byte_position] = (original & 0x0F) | (pixel_bits << 4);
|
||||
}
|
||||
}
|
||||
|
||||
bool HOT EPaperSpectraE6::transfer_data() {
|
||||
const uint32_t start_time = App.get_loop_component_start_time();
|
||||
const size_t buffer_length = this->buffer_length_;
|
||||
if (this->current_data_index_ == 0) {
|
||||
ESP_LOGV(TAG, "Sending data");
|
||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_VERBOSE
|
||||
this->transfer_start_time_ = millis();
|
||||
#endif
|
||||
ESP_LOGV(TAG, "Start sending data at %ums", (unsigned) millis());
|
||||
this->command(0x10);
|
||||
}
|
||||
|
||||
uint8_t bytes_to_send[4]{0};
|
||||
const size_t buffer_length = this->get_buffer_length();
|
||||
for (size_t i = this->current_data_index_; i < buffer_length; i += 3) {
|
||||
const uint32_t triplet = encode_uint24(this->buffer_[i + 0], this->buffer_[i + 1], this->buffer_[i + 2]);
|
||||
// 8 pixels are stored in 3 bytes
|
||||
// |aaabbbaa|abbbaaab|bbaaabbb|
|
||||
// | byte 1 | byte 2 | byte 3 |
|
||||
bytes_to_send[0] = ((triplet >> 17) & 0b01110000) | ((triplet >> 18) & 0b00000111);
|
||||
bytes_to_send[1] = ((triplet >> 11) & 0b01110000) | ((triplet >> 12) & 0b00000111);
|
||||
bytes_to_send[2] = ((triplet >> 5) & 0b01110000) | ((triplet >> 6) & 0b00000111);
|
||||
bytes_to_send[3] = ((triplet << 1) & 0b01110000) | ((triplet << 0) & 0b00000111);
|
||||
size_t buf_idx = 0;
|
||||
uint8_t bytes_to_send[MAX_TRANSFER_SIZE];
|
||||
while (this->current_data_index_ != buffer_length) {
|
||||
bytes_to_send[buf_idx++] = this->buffer_[this->current_data_index_++];
|
||||
|
||||
this->start_data_();
|
||||
this->write_array(bytes_to_send, sizeof(bytes_to_send));
|
||||
this->end_data_();
|
||||
if (buf_idx == sizeof bytes_to_send) {
|
||||
this->start_data_();
|
||||
this->write_array(bytes_to_send, buf_idx);
|
||||
this->end_data_();
|
||||
ESP_LOGV(TAG, "Wrote %d bytes at %ums", buf_idx, (unsigned) millis());
|
||||
buf_idx = 0;
|
||||
|
||||
if (millis() - start_time > MAX_TRANSFER_TIME) {
|
||||
// Let the main loop run and come back next loop
|
||||
this->current_data_index_ = i + 3;
|
||||
return false;
|
||||
if (millis() - start_time > MAX_TRANSFER_TIME) {
|
||||
// Let the main loop run and come back next loop
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
// Finished the entire dataset
|
||||
if (buf_idx != 0) {
|
||||
this->start_data_();
|
||||
this->write_array(bytes_to_send, buf_idx);
|
||||
this->end_data_();
|
||||
}
|
||||
this->current_data_index_ = 0;
|
||||
ESP_LOGV(TAG, "Sent data in %" PRIu32 " ms", millis() - this->transfer_start_time_);
|
||||
return true;
|
||||
}
|
||||
|
||||
void EPaperSpectraE6::reset() {
|
||||
if (this->reset_pin_ != nullptr) {
|
||||
this->disable_loop();
|
||||
this->reset_pin_->digital_write(true);
|
||||
this->set_timeout(20, [this] {
|
||||
this->reset_pin_->digital_write(false);
|
||||
delay(2);
|
||||
this->reset_pin_->digital_write(true);
|
||||
this->set_timeout(20, [this] { this->enable_loop(); });
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace esphome::epaper_spi
|
||||
|
||||
@@ -6,18 +6,23 @@ namespace esphome::epaper_spi {
|
||||
|
||||
class EPaperSpectraE6 : public EPaperBase {
|
||||
public:
|
||||
EPaperSpectraE6(const uint8_t *init_sequence, const size_t init_sequence_length)
|
||||
: EPaperBase(init_sequence, init_sequence_length) {}
|
||||
EPaperSpectraE6(const char *name, uint16_t width, uint16_t height, const uint8_t *init_sequence,
|
||||
size_t init_sequence_length)
|
||||
: EPaperBase(name, width, height, init_sequence, init_sequence_length, DISPLAY_TYPE_COLOR) {
|
||||
this->buffer_length_ = width * height / 2; // 2 pixels per byte
|
||||
}
|
||||
|
||||
display::DisplayType get_display_type() override { return display::DisplayType::DISPLAY_TYPE_COLOR; }
|
||||
void fill(Color color) override;
|
||||
void clear() override;
|
||||
|
||||
protected:
|
||||
void refresh_screen() override;
|
||||
void power_on() override;
|
||||
void power_off() override;
|
||||
void deep_sleep() override;
|
||||
void draw_absolute_pixel_internal(int x, int y, Color color) override;
|
||||
uint32_t get_buffer_length() override;
|
||||
|
||||
bool transfer_data() override;
|
||||
void reset() override;
|
||||
};
|
||||
|
||||
} // namespace esphome::epaper_spi
|
||||
|
||||
65
esphome/components/epaper_spi/models/__init__.py
Normal file
65
esphome/components/epaper_spi/models/__init__.py
Normal file
@@ -0,0 +1,65 @@
|
||||
from typing import Any, Self
|
||||
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_DIMENSIONS, CONF_HEIGHT, CONF_WIDTH
|
||||
|
||||
|
||||
class EpaperModel:
|
||||
models: dict[str, Self] = {}
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
class_name: str,
|
||||
initsequence=None,
|
||||
**defaults,
|
||||
):
|
||||
name = name.upper()
|
||||
self.name = name
|
||||
self.class_name = class_name
|
||||
self.initsequence = initsequence
|
||||
self.defaults = defaults
|
||||
EpaperModel.models[name] = self
|
||||
|
||||
def get_default(self, key, fallback: Any = False) -> Any:
|
||||
return self.defaults.get(key, fallback)
|
||||
|
||||
def get_init_sequence(self, config: dict):
|
||||
return self.initsequence
|
||||
|
||||
def option(self, name, fallback=cv.UNDEFINED) -> cv.Optional | cv.Required:
|
||||
if fallback is None and self.get_default(name, None) is None:
|
||||
return cv.Required(name)
|
||||
return cv.Optional(name, default=self.get_default(name, fallback))
|
||||
|
||||
def get_dimensions(self, config) -> tuple[int, int]:
|
||||
if CONF_DIMENSIONS in config:
|
||||
# Explicit dimensions, just use as is
|
||||
dimensions = config[CONF_DIMENSIONS]
|
||||
if isinstance(dimensions, dict):
|
||||
width = dimensions[CONF_WIDTH]
|
||||
height = dimensions[CONF_HEIGHT]
|
||||
else:
|
||||
(width, height) = dimensions
|
||||
|
||||
else:
|
||||
# Default dimensions, use model defaults
|
||||
width = self.get_default(CONF_WIDTH)
|
||||
height = self.get_default(CONF_HEIGHT)
|
||||
return width, height
|
||||
|
||||
def extend(self, name, **kwargs) -> "EpaperModel":
|
||||
"""
|
||||
Extend the current model with additional parameters or a modified init sequence.
|
||||
Parameters supplied here will override the defaults of the current model.
|
||||
if the initsequence is not provided, the current model's initsequence will be used.
|
||||
If add_init_sequence is provided, it will be appended to the current initsequence.
|
||||
:param name:
|
||||
:param kwargs:
|
||||
:return:
|
||||
"""
|
||||
initsequence = list(kwargs.pop("initsequence", self.initsequence) or ())
|
||||
initsequence.extend(kwargs.pop("add_init_sequence", ()))
|
||||
defaults = self.defaults.copy()
|
||||
defaults.update(kwargs)
|
||||
return self.__class__(name, initsequence=tuple(initsequence), **defaults)
|
||||
51
esphome/components/epaper_spi/models/spectra_e6.py
Normal file
51
esphome/components/epaper_spi/models/spectra_e6.py
Normal file
@@ -0,0 +1,51 @@
|
||||
from typing import Any
|
||||
|
||||
from . import EpaperModel
|
||||
|
||||
|
||||
class SpectraE6(EpaperModel):
|
||||
def __init__(self, name, class_name="EPaperSpectraE6", **kwargs):
|
||||
super().__init__(name, class_name, **kwargs)
|
||||
|
||||
# fmt: off
|
||||
def get_init_sequence(self, config: dict):
|
||||
width, height = self.get_dimensions(config)
|
||||
return (
|
||||
(0xAA, 0x49, 0x55, 0x20, 0x08, 0x09, 0x18,),
|
||||
(0x01, 0x3F,),
|
||||
(0x00, 0x5F, 0x69,),
|
||||
(0x03, 0x00, 0x54, 0x00, 0x44,),
|
||||
(0x05, 0x40, 0x1F, 0x1F, 0x2C,),
|
||||
(0x06, 0x6F, 0x1F, 0x17, 0x49,),
|
||||
(0x08, 0x6F, 0x1F, 0x1F, 0x22,),
|
||||
(0x30, 0x03,),
|
||||
(0x50, 0x3F,),
|
||||
(0x60, 0x02, 0x00,),
|
||||
(0x61, width // 256, width % 256, height // 256, height % 256,),
|
||||
(0x84, 0x01,),
|
||||
(0xE3, 0x2F,),
|
||||
)
|
||||
|
||||
def get_default(self, key, fallback: Any = False) -> Any:
|
||||
return self.defaults.get(key, fallback)
|
||||
|
||||
|
||||
spectra_e6 = SpectraE6("spectra-e6")
|
||||
|
||||
spectra_e6.extend(
|
||||
"Seeed-reTerminal-E1002",
|
||||
width=800,
|
||||
height=480,
|
||||
data_rate="20MHz",
|
||||
cs_pin=10,
|
||||
dc_pin=11,
|
||||
reset_pin=12,
|
||||
busy_pin={
|
||||
"number": 13,
|
||||
"inverted": True,
|
||||
"mode": {
|
||||
"input": True,
|
||||
"pullup": True,
|
||||
},
|
||||
},
|
||||
)
|
||||
@@ -558,6 +558,7 @@ CONF_DISABLE_LIBC_LOCKS_IN_IRAM = "disable_libc_locks_in_iram"
|
||||
CONF_DISABLE_VFS_SUPPORT_TERMIOS = "disable_vfs_support_termios"
|
||||
CONF_DISABLE_VFS_SUPPORT_SELECT = "disable_vfs_support_select"
|
||||
CONF_DISABLE_VFS_SUPPORT_DIR = "disable_vfs_support_dir"
|
||||
CONF_LOOP_TASK_STACK_SIZE = "loop_task_stack_size"
|
||||
|
||||
# VFS requirement tracking
|
||||
# Components that need VFS features can call require_vfs_select() or require_vfs_dir()
|
||||
@@ -654,6 +655,9 @@ FRAMEWORK_SCHEMA = cv.All(
|
||||
): cv.boolean,
|
||||
cv.Optional(CONF_DISABLE_VFS_SUPPORT_DIR, default=True): cv.boolean,
|
||||
cv.Optional(CONF_EXECUTE_FROM_PSRAM): cv.boolean,
|
||||
cv.Optional(CONF_LOOP_TASK_STACK_SIZE, default=8192): cv.int_range(
|
||||
min=8192, max=32768
|
||||
),
|
||||
}
|
||||
),
|
||||
cv.Optional(CONF_COMPONENTS, default=[]): cv.ensure_list(
|
||||
@@ -926,6 +930,10 @@ async def to_code(config):
|
||||
f"VERSION_CODE({framework_ver.major}, {framework_ver.minor}, {framework_ver.patch})"
|
||||
),
|
||||
)
|
||||
add_idf_sdkconfig_option(
|
||||
"CONFIG_ARDUINO_LOOP_STACK_SIZE",
|
||||
conf[CONF_ADVANCED][CONF_LOOP_TASK_STACK_SIZE],
|
||||
)
|
||||
add_idf_sdkconfig_option("CONFIG_AUTOSTART_ARDUINO", True)
|
||||
add_idf_sdkconfig_option("CONFIG_MBEDTLS_PSK_MODES", True)
|
||||
add_idf_sdkconfig_option("CONFIG_MBEDTLS_CERTIFICATE_BUNDLE", True)
|
||||
@@ -1071,6 +1079,10 @@ async def to_code(config):
|
||||
)
|
||||
add_idf_sdkconfig_option("CONFIG_IDF_EXPERIMENTAL_FEATURES", True)
|
||||
|
||||
cg.add_define(
|
||||
"ESPHOME_LOOP_TASK_STACK_SIZE", advanced.get(CONF_LOOP_TASK_STACK_SIZE)
|
||||
)
|
||||
|
||||
cg.add_define(
|
||||
"USE_ESP_IDF_VERSION_CODE",
|
||||
cg.RawExpression(
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#ifdef USE_ESP32
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "preferences.h"
|
||||
@@ -97,9 +98,9 @@ void loop_task(void *pv_params) {
|
||||
extern "C" void app_main() {
|
||||
esp32::setup_preferences();
|
||||
#if CONFIG_FREERTOS_UNICORE
|
||||
xTaskCreate(loop_task, "loopTask", 8192, nullptr, 1, &loop_task_handle);
|
||||
xTaskCreate(loop_task, "loopTask", ESPHOME_LOOP_TASK_STACK_SIZE, nullptr, 1, &loop_task_handle);
|
||||
#else
|
||||
xTaskCreatePinnedToCore(loop_task, "loopTask", 8192, nullptr, 1, &loop_task_handle, 1);
|
||||
xTaskCreatePinnedToCore(loop_task, "loopTask", ESPHOME_LOOP_TASK_STACK_SIZE, nullptr, 1, &loop_task_handle, 1);
|
||||
#endif
|
||||
}
|
||||
#endif // USE_ESP_IDF
|
||||
|
||||
@@ -22,6 +22,7 @@ from esphome.core import CORE, CoroPriority, TimePeriod, coroutine_with_priority
|
||||
import esphome.final_validate as fv
|
||||
|
||||
DEPENDENCIES = ["esp32"]
|
||||
AUTO_LOAD = ["socket"]
|
||||
CODEOWNERS = ["@jesserockz", "@Rapsssito", "@bdraco"]
|
||||
DOMAIN = "esp32_ble"
|
||||
|
||||
@@ -482,13 +483,10 @@ async def to_code(config):
|
||||
cg.add(var.set_name(name))
|
||||
await cg.register_component(var, config)
|
||||
|
||||
# BLE uses 1 UDP socket for event notification to wake up main loop from select()
|
||||
# BLE uses the socket wake_loop_threadsafe() mechanism to wake the main loop from BLE tasks
|
||||
# This enables low-latency (~12μs) BLE event processing instead of waiting for
|
||||
# select() timeout (0-16ms). The socket is created in ble_setup_() and used to
|
||||
# wake lwip_select() when BLE events arrive from the BLE thread.
|
||||
# Note: Called during config generation, socket is created at runtime. In practice,
|
||||
# always used since esp32_ble only runs on ESP32 which always has USE_SOCKET_SELECT_SUPPORT.
|
||||
socket.consume_sockets(1, "esp32_ble")(config)
|
||||
# select() timeout (0-16ms). The wake socket is shared across all components.
|
||||
socket.require_wake_loop_threadsafe()
|
||||
|
||||
# Define max connections for use in C++ code (e.g., ble_server.h)
|
||||
max_connections = config.get(CONF_MAX_CONNECTIONS, DEFAULT_MAX_CONNECTIONS)
|
||||
|
||||
@@ -297,21 +297,10 @@ bool ESP32BLE::ble_setup_() {
|
||||
// BLE takes some time to be fully set up, 200ms should be more than enough
|
||||
delay(200); // NOLINT
|
||||
|
||||
// Set up notification socket to wake main loop for BLE events
|
||||
// This enables low-latency (~12μs) event processing instead of waiting for select() timeout
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
this->setup_event_notification_();
|
||||
#endif
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ESP32BLE::ble_dismantle_() {
|
||||
// Clean up notification socket first before dismantling BLE stack
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
this->cleanup_event_notification_();
|
||||
#endif
|
||||
|
||||
esp_err_t err = esp_bluedroid_disable();
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "esp_bluedroid_disable failed: %d", err);
|
||||
@@ -409,12 +398,6 @@ void ESP32BLE::loop() {
|
||||
break;
|
||||
}
|
||||
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
// Drain any notification socket events first
|
||||
// This clears the socket so it doesn't stay "ready" in subsequent select() calls
|
||||
this->drain_event_notifications_();
|
||||
#endif
|
||||
|
||||
BLEEvent *ble_event = this->ble_events_.pop();
|
||||
while (ble_event != nullptr) {
|
||||
switch (ble_event->type_) {
|
||||
@@ -589,8 +572,8 @@ void ESP32BLE::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_pa
|
||||
GAP_SECURITY_EVENTS:
|
||||
enqueue_ble_event(event, param);
|
||||
// Wake up main loop to process security event immediately
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
global_ble->notify_main_loop_();
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
|
||||
App.wake_loop_threadsafe();
|
||||
#endif
|
||||
return;
|
||||
|
||||
@@ -612,8 +595,8 @@ void ESP32BLE::gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gat
|
||||
esp_ble_gatts_cb_param_t *param) {
|
||||
enqueue_ble_event(event, gatts_if, param);
|
||||
// Wake up main loop to process GATT event immediately
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
global_ble->notify_main_loop_();
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
|
||||
App.wake_loop_threadsafe();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
@@ -623,8 +606,8 @@ void ESP32BLE::gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gat
|
||||
esp_ble_gattc_cb_param_t *param) {
|
||||
enqueue_ble_event(event, gattc_if, param);
|
||||
// Wake up main loop to process GATT event immediately
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
global_ble->notify_main_loop_();
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
|
||||
App.wake_loop_threadsafe();
|
||||
#endif
|
||||
}
|
||||
#endif
|
||||
@@ -665,89 +648,6 @@ void ESP32BLE::dump_config() {
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
void ESP32BLE::setup_event_notification_() {
|
||||
// Create UDP socket for event notifications
|
||||
this->notify_fd_ = lwip_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (this->notify_fd_ < 0) {
|
||||
ESP_LOGW(TAG, "Event socket create failed: %d", errno);
|
||||
return;
|
||||
}
|
||||
|
||||
// Bind to loopback with auto-assigned port
|
||||
struct sockaddr_in addr = {};
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = lwip_htonl(INADDR_LOOPBACK);
|
||||
addr.sin_port = 0; // Auto-assign port
|
||||
|
||||
if (lwip_bind(this->notify_fd_, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
||||
ESP_LOGW(TAG, "Event socket bind failed: %d", errno);
|
||||
lwip_close(this->notify_fd_);
|
||||
this->notify_fd_ = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the assigned address and connect to it
|
||||
// Connecting a UDP socket allows using send() instead of sendto() for better performance
|
||||
struct sockaddr_in notify_addr;
|
||||
socklen_t len = sizeof(notify_addr);
|
||||
if (lwip_getsockname(this->notify_fd_, (struct sockaddr *) ¬ify_addr, &len) < 0) {
|
||||
ESP_LOGW(TAG, "Event socket address failed: %d", errno);
|
||||
lwip_close(this->notify_fd_);
|
||||
this->notify_fd_ = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
// Connect to self (loopback) - allows using send() instead of sendto()
|
||||
// After connect(), no need to store notify_addr - the socket remembers it
|
||||
if (lwip_connect(this->notify_fd_, (struct sockaddr *) ¬ify_addr, sizeof(notify_addr)) < 0) {
|
||||
ESP_LOGW(TAG, "Event socket connect failed: %d", errno);
|
||||
lwip_close(this->notify_fd_);
|
||||
this->notify_fd_ = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
// Set non-blocking mode
|
||||
int flags = lwip_fcntl(this->notify_fd_, F_GETFL, 0);
|
||||
lwip_fcntl(this->notify_fd_, F_SETFL, flags | O_NONBLOCK);
|
||||
|
||||
// Register with application's select() loop
|
||||
if (!App.register_socket_fd(this->notify_fd_)) {
|
||||
ESP_LOGW(TAG, "Event socket register failed");
|
||||
lwip_close(this->notify_fd_);
|
||||
this->notify_fd_ = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG, "Event socket ready");
|
||||
}
|
||||
|
||||
void ESP32BLE::cleanup_event_notification_() {
|
||||
if (this->notify_fd_ >= 0) {
|
||||
App.unregister_socket_fd(this->notify_fd_);
|
||||
lwip_close(this->notify_fd_);
|
||||
this->notify_fd_ = -1;
|
||||
ESP_LOGD(TAG, "Event socket closed");
|
||||
}
|
||||
}
|
||||
|
||||
void ESP32BLE::drain_event_notifications_() {
|
||||
// Called from main loop to drain any pending notifications
|
||||
// Must check is_socket_ready() to avoid blocking on empty socket
|
||||
if (this->notify_fd_ >= 0 && App.is_socket_ready(this->notify_fd_)) {
|
||||
char buffer[BLE_EVENT_NOTIFY_DRAIN_BUFFER_SIZE];
|
||||
// Drain all pending notifications with non-blocking reads
|
||||
// Multiple BLE events may have triggered multiple writes, so drain until EWOULDBLOCK
|
||||
// We control both ends of this loopback socket (always write 1 byte per event),
|
||||
// so no error checking needed - any errors indicate catastrophic system failure
|
||||
while (lwip_recvfrom(this->notify_fd_, buffer, sizeof(buffer), 0, nullptr, nullptr) > 0) {
|
||||
// Just draining, no action needed - actual BLE events are already queued
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#endif // USE_SOCKET_SELECT_SUPPORT
|
||||
|
||||
uint64_t ble_addr_to_uint64(const esp_bd_addr_t address) {
|
||||
uint64_t u = 0;
|
||||
u |= uint64_t(address[0] & 0xFF) << 40;
|
||||
|
||||
@@ -166,12 +166,10 @@ class ESP32BLE : public Component {
|
||||
void advertising_init_();
|
||||
#endif
|
||||
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
void setup_event_notification_(); // Create notification socket
|
||||
void cleanup_event_notification_(); // Close and unregister socket
|
||||
inline void notify_main_loop_(); // Wake up select() from BLE thread (hot path - inlined)
|
||||
void drain_event_notifications_(); // Read pending notifications in main loop
|
||||
#endif
|
||||
// BLE uses the core wake_loop_threadsafe() mechanism to wake the main event loop
|
||||
// from BLE tasks. This enables low-latency (~12μs) event processing instead of
|
||||
// waiting for select() timeout (0-16ms). The wake socket is shared with other
|
||||
// components that need this functionality.
|
||||
|
||||
private:
|
||||
template<typename... Args> friend void enqueue_ble_event(Args... args);
|
||||
@@ -207,13 +205,6 @@ class ESP32BLE : public Component {
|
||||
esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE}; // 4 bytes (enum)
|
||||
uint32_t advertising_cycle_time_{}; // 4 bytes
|
||||
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
// Event notification socket for waking up main loop from BLE thread
|
||||
// Uses connected UDP loopback socket to wake lwip_select() with ~12μs latency vs 0-16ms timeout
|
||||
// Socket is connected during setup, allowing use of send() instead of sendto() for efficiency
|
||||
int notify_fd_{-1}; // 4 bytes (file descriptor)
|
||||
#endif
|
||||
|
||||
// 2-byte aligned members
|
||||
uint16_t appearance_{0}; // 2 bytes
|
||||
|
||||
@@ -225,29 +216,6 @@ class ESP32BLE : public Component {
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
extern ESP32BLE *global_ble;
|
||||
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
// Inline implementations for hot-path functions
|
||||
// These are called from BLE thread (notify) and main loop (drain) on every event
|
||||
|
||||
// Small buffer for draining notification bytes (1 byte sent per BLE event)
|
||||
// Size allows draining multiple notifications per recvfrom() without wasting stack
|
||||
static constexpr size_t BLE_EVENT_NOTIFY_DRAIN_BUFFER_SIZE = 16;
|
||||
|
||||
inline void ESP32BLE::notify_main_loop_() {
|
||||
// Called from BLE thread context when events are queued
|
||||
// Wakes up lwip_select() in main loop by writing to connected loopback socket
|
||||
if (this->notify_fd_ >= 0) {
|
||||
const char dummy = 1;
|
||||
// Non-blocking send - if it fails (unlikely), select() will wake on timeout anyway
|
||||
// No error checking needed: we control both ends of this loopback socket, and the
|
||||
// BLE event is already queued. Notification is best-effort to reduce latency.
|
||||
// This is safe to call from BLE thread - send() is thread-safe in lwip
|
||||
// Socket is already connected to loopback address, so send() is faster than sendto()
|
||||
lwip_send(this->notify_fd_, &dummy, 1, 0);
|
||||
}
|
||||
}
|
||||
#endif // USE_SOCKET_SELECT_SUPPORT
|
||||
|
||||
template<typename... Ts> class BLEEnabledCondition : public Condition<Ts...> {
|
||||
public:
|
||||
bool check(Ts... x) override { return global_ble->is_active(); }
|
||||
|
||||
78
esphome/components/esp32_hosted/update/__init__.py
Normal file
78
esphome/components/esp32_hosted/update/__init__.py
Normal file
@@ -0,0 +1,78 @@
|
||||
import hashlib
|
||||
from typing import Any
|
||||
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import esp32, update
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_PATH, CONF_RAW_DATA_ID
|
||||
from esphome.core import CORE, HexInt
|
||||
|
||||
CODEOWNERS = ["@swoboda1337"]
|
||||
AUTO_LOAD = ["sha256", "watchdog"]
|
||||
DEPENDENCIES = ["esp32_hosted"]
|
||||
|
||||
CONF_SHA256 = "sha256"
|
||||
|
||||
esp32_hosted_ns = cg.esphome_ns.namespace("esp32_hosted")
|
||||
Esp32HostedUpdate = esp32_hosted_ns.class_(
|
||||
"Esp32HostedUpdate", update.UpdateEntity, cg.Component
|
||||
)
|
||||
|
||||
|
||||
def _validate_sha256(value: Any) -> str:
|
||||
value = cv.string_strict(value)
|
||||
if len(value) != 64:
|
||||
raise cv.Invalid("SHA256 must be 64 hexadecimal characters")
|
||||
try:
|
||||
bytes.fromhex(value)
|
||||
except ValueError as e:
|
||||
raise cv.Invalid(f"SHA256 must be valid hexadecimal: {e}") from e
|
||||
return value
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.All(
|
||||
update.update_schema(Esp32HostedUpdate, device_class="firmware").extend(
|
||||
{
|
||||
cv.GenerateID(CONF_RAW_DATA_ID): cv.declare_id(cg.uint8),
|
||||
cv.Required(CONF_PATH): cv.file_,
|
||||
cv.Required(CONF_SHA256): _validate_sha256,
|
||||
}
|
||||
),
|
||||
esp32.only_on_variant(
|
||||
supported=[
|
||||
esp32.const.VARIANT_ESP32H2,
|
||||
esp32.const.VARIANT_ESP32P4,
|
||||
]
|
||||
),
|
||||
)
|
||||
|
||||
|
||||
def _validate_firmware(config: dict[str, Any]) -> None:
|
||||
path = CORE.relative_config_path(config[CONF_PATH])
|
||||
with open(path, "rb") as f:
|
||||
firmware_data = f.read()
|
||||
calculated = hashlib.sha256(firmware_data).hexdigest()
|
||||
expected = config[CONF_SHA256].lower()
|
||||
if calculated != expected:
|
||||
raise cv.Invalid(
|
||||
f"SHA256 mismatch for {config[CONF_PATH]}: expected {expected}, got {calculated}"
|
||||
)
|
||||
|
||||
|
||||
FINAL_VALIDATE_SCHEMA = _validate_firmware
|
||||
|
||||
|
||||
async def to_code(config: dict[str, Any]) -> None:
|
||||
var = await update.new_update(config)
|
||||
|
||||
path = config[CONF_PATH]
|
||||
with open(CORE.relative_config_path(path), "rb") as f:
|
||||
firmware_data = f.read()
|
||||
rhs = [HexInt(x) for x in firmware_data]
|
||||
prog_arr = cg.progmem_array(config[CONF_RAW_DATA_ID], rhs)
|
||||
|
||||
sha256_bytes = bytes.fromhex(config[CONF_SHA256])
|
||||
cg.add(var.set_firmware_sha256([HexInt(b) for b in sha256_bytes]))
|
||||
cg.add(var.set_firmware_data(prog_arr))
|
||||
cg.add(var.set_firmware_size(len(firmware_data)))
|
||||
await cg.register_component(var, config)
|
||||
164
esphome/components/esp32_hosted/update/esp32_hosted_update.cpp
Normal file
164
esphome/components/esp32_hosted/update/esp32_hosted_update.cpp
Normal file
@@ -0,0 +1,164 @@
|
||||
#if defined(USE_ESP32_VARIANT_ESP32H2) || defined(USE_ESP32_VARIANT_ESP32P4)
|
||||
#include "esp32_hosted_update.h"
|
||||
#include "esphome/components/watchdog/watchdog.h"
|
||||
#include "esphome/components/sha256/sha256.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include <esp_image_format.h>
|
||||
#include <esp_app_desc.h>
|
||||
#include <esp_hosted.h>
|
||||
|
||||
extern "C" {
|
||||
#include <esp_hosted_ota.h>
|
||||
}
|
||||
|
||||
namespace esphome::esp32_hosted {
|
||||
|
||||
static const char *const TAG = "esp32_hosted.update";
|
||||
|
||||
// older coprocessor firmware versions have a 1500-byte limit per RPC call
|
||||
constexpr size_t CHUNK_SIZE = 1500;
|
||||
|
||||
void Esp32HostedUpdate::setup() {
|
||||
this->update_info_.title = "ESP32 Hosted Coprocessor";
|
||||
|
||||
// get coprocessor version
|
||||
esp_hosted_coprocessor_fwver_t ver_info;
|
||||
if (esp_hosted_get_coprocessor_fwversion(&ver_info) == ESP_OK) {
|
||||
this->update_info_.current_version = str_sprintf("%d.%d.%d", ver_info.major1, ver_info.minor1, ver_info.patch1);
|
||||
} else {
|
||||
this->update_info_.current_version = "unknown";
|
||||
}
|
||||
ESP_LOGD(TAG, "Coprocessor version: %s", this->update_info_.current_version.c_str());
|
||||
|
||||
// get image version
|
||||
const int app_desc_offset = sizeof(esp_image_header_t) + sizeof(esp_image_segment_header_t);
|
||||
if (this->firmware_size_ >= app_desc_offset + sizeof(esp_app_desc_t)) {
|
||||
esp_app_desc_t *app_desc = (esp_app_desc_t *) (this->firmware_data_ + app_desc_offset);
|
||||
if (app_desc->magic_word == ESP_APP_DESC_MAGIC_WORD) {
|
||||
ESP_LOGD(TAG, "Firmware version: %s", app_desc->version);
|
||||
ESP_LOGD(TAG, "Project name: %s", app_desc->project_name);
|
||||
ESP_LOGD(TAG, "Build date: %s", app_desc->date);
|
||||
ESP_LOGD(TAG, "Build time: %s", app_desc->time);
|
||||
ESP_LOGD(TAG, "IDF version: %s", app_desc->idf_ver);
|
||||
this->update_info_.latest_version = app_desc->version;
|
||||
if (this->update_info_.latest_version != this->update_info_.current_version) {
|
||||
this->state_ = update::UPDATE_STATE_AVAILABLE;
|
||||
} else {
|
||||
this->state_ = update::UPDATE_STATE_NO_UPDATE;
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Invalid app description magic word: 0x%08x (expected 0x%08x)", app_desc->magic_word,
|
||||
ESP_APP_DESC_MAGIC_WORD);
|
||||
this->state_ = update::UPDATE_STATE_NO_UPDATE;
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Firmware too small to contain app description");
|
||||
this->state_ = update::UPDATE_STATE_NO_UPDATE;
|
||||
}
|
||||
|
||||
// publish state
|
||||
this->status_clear_error();
|
||||
this->publish_state();
|
||||
}
|
||||
|
||||
void Esp32HostedUpdate::dump_config() {
|
||||
ESP_LOGCONFIG(TAG,
|
||||
"ESP32 Hosted Update:\n"
|
||||
" Current Version: %s\n"
|
||||
" Latest Version: %s\n"
|
||||
" Latest Size: %zu bytes",
|
||||
this->update_info_.current_version.c_str(), this->update_info_.latest_version.c_str(),
|
||||
this->firmware_size_);
|
||||
}
|
||||
|
||||
void Esp32HostedUpdate::perform(bool force) {
|
||||
if (this->state_ != update::UPDATE_STATE_AVAILABLE && !force) {
|
||||
ESP_LOGW(TAG, "Update not available");
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->firmware_data_ == nullptr || this->firmware_size_ == 0) {
|
||||
ESP_LOGE(TAG, "No firmware data available");
|
||||
return;
|
||||
}
|
||||
|
||||
sha256::SHA256 hasher;
|
||||
hasher.init();
|
||||
hasher.add(this->firmware_data_, this->firmware_size_);
|
||||
hasher.calculate();
|
||||
if (!hasher.equals_bytes(this->firmware_sha256_.data())) {
|
||||
this->status_set_error("SHA256 verification failed");
|
||||
this->publish_state();
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Starting OTA update (%zu bytes)", this->firmware_size_);
|
||||
|
||||
watchdog::WatchdogManager watchdog(20000);
|
||||
update::UpdateState prev_state = this->state_;
|
||||
this->state_ = update::UPDATE_STATE_INSTALLING;
|
||||
this->update_info_.has_progress = false;
|
||||
this->publish_state();
|
||||
|
||||
esp_err_t err = esp_hosted_slave_ota_begin(); // NOLINT
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to begin OTA: %s", esp_err_to_name(err));
|
||||
this->state_ = prev_state;
|
||||
this->status_set_error("Failed to begin OTA");
|
||||
this->publish_state();
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t chunk[CHUNK_SIZE];
|
||||
const uint8_t *data_ptr = this->firmware_data_;
|
||||
size_t remaining = this->firmware_size_;
|
||||
while (remaining > 0) {
|
||||
size_t chunk_size = std::min(remaining, static_cast<size_t>(CHUNK_SIZE));
|
||||
memcpy(chunk, data_ptr, chunk_size);
|
||||
err = esp_hosted_slave_ota_write(chunk, chunk_size); // NOLINT
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to write OTA data: %s", esp_err_to_name(err));
|
||||
esp_hosted_slave_ota_end(); // NOLINT
|
||||
this->state_ = prev_state;
|
||||
this->status_set_error("Failed to write OTA data");
|
||||
this->publish_state();
|
||||
return;
|
||||
}
|
||||
data_ptr += chunk_size;
|
||||
remaining -= chunk_size;
|
||||
App.feed_wdt();
|
||||
}
|
||||
|
||||
err = esp_hosted_slave_ota_end(); // NOLINT
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to end OTA: %s", esp_err_to_name(err));
|
||||
this->state_ = prev_state;
|
||||
this->status_set_error("Failed to end OTA");
|
||||
this->publish_state();
|
||||
return;
|
||||
}
|
||||
|
||||
// activate new firmware
|
||||
err = esp_hosted_slave_ota_activate(); // NOLINT
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to activate OTA: %s", esp_err_to_name(err));
|
||||
this->state_ = prev_state;
|
||||
this->status_set_error("Failed to activate OTA");
|
||||
this->publish_state();
|
||||
return;
|
||||
}
|
||||
|
||||
// update state
|
||||
ESP_LOGI(TAG, "OTA update successful");
|
||||
this->state_ = update::UPDATE_STATE_NO_UPDATE;
|
||||
this->status_clear_error();
|
||||
this->publish_state();
|
||||
|
||||
// schedule a restart to ensure everything is in sync
|
||||
ESP_LOGI(TAG, "Restarting in 1 second");
|
||||
this->set_timeout(1000, []() { App.safe_reboot(); });
|
||||
}
|
||||
|
||||
} // namespace esphome::esp32_hosted
|
||||
#endif
|
||||
32
esphome/components/esp32_hosted/update/esp32_hosted_update.h
Normal file
32
esphome/components/esp32_hosted/update/esp32_hosted_update.h
Normal file
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#if defined(USE_ESP32_VARIANT_ESP32H2) || defined(USE_ESP32_VARIANT_ESP32P4)
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/update/update_entity.h"
|
||||
#include <array>
|
||||
|
||||
namespace esphome::esp32_hosted {
|
||||
|
||||
class Esp32HostedUpdate : public update::UpdateEntity, public Component {
|
||||
public:
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
||||
|
||||
void perform(bool force) override;
|
||||
void check() override {}
|
||||
|
||||
void set_firmware_data(const uint8_t *data) { this->firmware_data_ = data; }
|
||||
void set_firmware_size(size_t size) { this->firmware_size_ = size; }
|
||||
void set_firmware_sha256(const std::array<uint8_t, 32> &sha256) { this->firmware_sha256_ = sha256; }
|
||||
|
||||
protected:
|
||||
const uint8_t *firmware_data_{nullptr};
|
||||
size_t firmware_size_{0};
|
||||
std::array<uint8_t, 32> firmware_sha256_;
|
||||
};
|
||||
|
||||
} // namespace esphome::esp32_hosted
|
||||
|
||||
#endif
|
||||
@@ -2,11 +2,18 @@ import logging
|
||||
|
||||
from esphome import pins
|
||||
import esphome.codegen as cg
|
||||
from esphome.components.zephyr import (
|
||||
zephyr_add_overlay,
|
||||
zephyr_add_prj_conf,
|
||||
zephyr_data,
|
||||
)
|
||||
from esphome.components.zephyr.const import KEY_BOARD
|
||||
from esphome.config_helpers import filter_source_files_from_platform
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_ADDRESS,
|
||||
CONF_FREQUENCY,
|
||||
CONF_I2C,
|
||||
CONF_I2C_ID,
|
||||
CONF_ID,
|
||||
CONF_SCAN,
|
||||
@@ -15,10 +22,12 @@ from esphome.const import (
|
||||
CONF_TIMEOUT,
|
||||
PLATFORM_ESP32,
|
||||
PLATFORM_ESP8266,
|
||||
PLATFORM_NRF52,
|
||||
PLATFORM_RP2040,
|
||||
PlatformFramework,
|
||||
)
|
||||
from esphome.core import CORE, CoroPriority, coroutine_with_priority
|
||||
from esphome.cpp_generator import MockObj
|
||||
import esphome.final_validate as fv
|
||||
|
||||
LOGGER = logging.getLogger(__name__)
|
||||
@@ -28,6 +37,7 @@ I2CBus = i2c_ns.class_("I2CBus")
|
||||
InternalI2CBus = i2c_ns.class_("InternalI2CBus", I2CBus)
|
||||
ArduinoI2CBus = i2c_ns.class_("ArduinoI2CBus", InternalI2CBus, cg.Component)
|
||||
IDFI2CBus = i2c_ns.class_("IDFI2CBus", InternalI2CBus, cg.Component)
|
||||
ZephyrI2CBus = i2c_ns.class_("ZephyrI2CBus", I2CBus, cg.Component)
|
||||
I2CDevice = i2c_ns.class_("I2CDevice")
|
||||
|
||||
|
||||
@@ -41,6 +51,8 @@ def _bus_declare_type(value):
|
||||
return cv.declare_id(ArduinoI2CBus)(value)
|
||||
if CORE.using_esp_idf:
|
||||
return cv.declare_id(IDFI2CBus)(value)
|
||||
if CORE.using_zephyr:
|
||||
return cv.declare_id(ZephyrI2CBus)(value)
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
@@ -62,23 +74,70 @@ CONFIG_SCHEMA = cv.All(
|
||||
cv.SplitDefault(CONF_SCL_PULLUP_ENABLED, esp32_idf=True): cv.All(
|
||||
cv.only_with_esp_idf, cv.boolean
|
||||
),
|
||||
cv.Optional(CONF_FREQUENCY, default="50kHz"): cv.All(
|
||||
cv.frequency, cv.Range(min=0, min_included=False)
|
||||
cv.SplitDefault(
|
||||
CONF_FREQUENCY,
|
||||
esp32="50kHz",
|
||||
esp8266="50kHz",
|
||||
rp2040="50kHz",
|
||||
nrf52="100kHz",
|
||||
): cv.All(
|
||||
cv.frequency,
|
||||
cv.Range(min=0, min_included=False),
|
||||
),
|
||||
cv.Optional(CONF_TIMEOUT): cv.All(
|
||||
cv.only_with_framework(["arduino", "esp-idf"]),
|
||||
cv.positive_time_period,
|
||||
),
|
||||
cv.Optional(CONF_TIMEOUT): cv.positive_time_period,
|
||||
cv.Optional(CONF_SCAN, default=True): cv.boolean,
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA),
|
||||
cv.only_on([PLATFORM_ESP32, PLATFORM_ESP8266, PLATFORM_RP2040]),
|
||||
cv.only_on([PLATFORM_ESP32, PLATFORM_ESP8266, PLATFORM_RP2040, PLATFORM_NRF52]),
|
||||
validate_config,
|
||||
)
|
||||
|
||||
|
||||
def _final_validate(config):
|
||||
full_config = fv.full_config.get()[CONF_I2C]
|
||||
if CORE.using_zephyr and len(full_config) > 1:
|
||||
raise cv.Invalid("Second i2c is not implemented on Zephyr yet")
|
||||
|
||||
|
||||
FINAL_VALIDATE_SCHEMA = _final_validate
|
||||
|
||||
|
||||
@coroutine_with_priority(CoroPriority.BUS)
|
||||
async def to_code(config):
|
||||
cg.add_global(i2c_ns.using)
|
||||
cg.add_define("USE_I2C")
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
if CORE.using_zephyr:
|
||||
zephyr_add_prj_conf("I2C", True)
|
||||
i2c = "i2c0"
|
||||
if zephyr_data()[KEY_BOARD] in ["xiao_ble"]:
|
||||
i2c = "i2c1"
|
||||
zephyr_add_overlay(
|
||||
f"""
|
||||
&pinctrl {{
|
||||
{i2c}_default: {i2c}_default {{
|
||||
group1 {{
|
||||
psels = <NRF_PSEL(TWIM_SDA, {config[CONF_SDA] // 32}, {config[CONF_SDA] % 32})>,
|
||||
<NRF_PSEL(TWIM_SCL, {config[CONF_SCL] // 32}, {config[CONF_SCL] % 32})>;
|
||||
}};
|
||||
}};
|
||||
{i2c}_sleep: {i2c}_sleep {{
|
||||
group1 {{
|
||||
psels = <NRF_PSEL(TWIM_SDA, {config[CONF_SDA] // 32}, {config[CONF_SDA] % 32})>,
|
||||
<NRF_PSEL(TWIM_SCL, {config[CONF_SCL] // 32}, {config[CONF_SCL] % 32})>;
|
||||
low-power-enable;
|
||||
}};
|
||||
}};
|
||||
}};
|
||||
"""
|
||||
)
|
||||
var = cg.new_Pvariable(
|
||||
config[CONF_ID], MockObj(f"DEVICE_DT_GET(DT_NODELABEL({i2c}))")
|
||||
)
|
||||
else:
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
|
||||
cg.add(var.set_sda_pin(config[CONF_SDA]))
|
||||
@@ -197,5 +256,6 @@ FILTER_SOURCE_FILES = filter_source_files_from_platform(
|
||||
PlatformFramework.LN882X_ARDUINO,
|
||||
},
|
||||
"i2c_bus_esp_idf.cpp": {PlatformFramework.ESP32_IDF},
|
||||
"i2c_bus_zephyr.cpp": {PlatformFramework.NRF52_ZEPHYR},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#include "i2c.h"
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include <memory>
|
||||
|
||||
@@ -23,6 +24,8 @@ void I2CBus::i2c_scan_() {
|
||||
} else if (err == ERROR_UNKNOWN) {
|
||||
scan_results_.emplace_back(address, false);
|
||||
}
|
||||
// it takes 16sec to scan on nrf52. It prevents board reset.
|
||||
arch_feed_wdt();
|
||||
}
|
||||
#if defined(USE_ESP32) && defined(USE_LOGGER)
|
||||
esp_log_level_set("*", previous);
|
||||
|
||||
133
esphome/components/i2c/i2c_bus_zephyr.cpp
Normal file
133
esphome/components/i2c/i2c_bus_zephyr.cpp
Normal file
@@ -0,0 +1,133 @@
|
||||
#ifdef USE_ZEPHYR
|
||||
|
||||
#include "i2c_bus_zephyr.h"
|
||||
#include <zephyr/drivers/i2c.h>
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome::i2c {
|
||||
|
||||
static const char *const TAG = "i2c.zephyr";
|
||||
|
||||
void ZephyrI2CBus::setup() {
|
||||
if (!device_is_ready(this->i2c_dev_)) {
|
||||
ESP_LOGE(TAG, "I2C dev is not ready.");
|
||||
mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
int ret = i2c_configure(this->i2c_dev_, this->dev_config_);
|
||||
if (ret < 0) {
|
||||
ESP_LOGE(TAG, "I2C: Failed to configure device");
|
||||
}
|
||||
|
||||
this->recovery_result_ = i2c_recover_bus(this->i2c_dev_);
|
||||
if (this->recovery_result_ != 0) {
|
||||
ESP_LOGE(TAG, "I2C recover bus failed, err %d", this->recovery_result_);
|
||||
}
|
||||
if (this->scan_) {
|
||||
ESP_LOGV(TAG, "Scanning I2C bus for active devices...");
|
||||
this->i2c_scan_();
|
||||
}
|
||||
}
|
||||
|
||||
void ZephyrI2CBus::dump_config() {
|
||||
auto get_speed = [](uint32_t dev_config) {
|
||||
switch (I2C_SPEED_GET(dev_config)) {
|
||||
case I2C_SPEED_STANDARD:
|
||||
return "100 kHz";
|
||||
case I2C_SPEED_FAST:
|
||||
return "400 kHz";
|
||||
case I2C_SPEED_FAST_PLUS:
|
||||
return "1 MHz";
|
||||
case I2C_SPEED_HIGH:
|
||||
return "3.4 MHz";
|
||||
case I2C_SPEED_ULTRA:
|
||||
return "5 MHz";
|
||||
}
|
||||
return "unknown";
|
||||
};
|
||||
ESP_LOGCONFIG(TAG,
|
||||
"I2C Bus:\n"
|
||||
" SDA Pin: GPIO%u\n"
|
||||
" SCL Pin: GPIO%u\n"
|
||||
" Frequency: %s\n"
|
||||
" Name: %s",
|
||||
this->sda_pin_, this->scl_pin_, get_speed(this->dev_config_), this->i2c_dev_->name);
|
||||
|
||||
if (this->recovery_result_ != 0) {
|
||||
ESP_LOGCONFIG(TAG, " Recovery: failed, err %d", this->recovery_result_);
|
||||
} else {
|
||||
ESP_LOGCONFIG(TAG, " Recovery: bus successfully recovered");
|
||||
}
|
||||
if (this->scan_) {
|
||||
ESP_LOGI(TAG, "Results from I2C bus scan:");
|
||||
if (scan_results_.empty()) {
|
||||
ESP_LOGI(TAG, "Found no I2C devices!");
|
||||
} else {
|
||||
for (const auto &s : scan_results_) {
|
||||
if (s.second) {
|
||||
ESP_LOGI(TAG, "Found I2C device at address 0x%02X", s.first);
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Unknown error at address 0x%02X", s.first);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ErrorCode ZephyrI2CBus::write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count,
|
||||
uint8_t *read_buffer, size_t read_count) {
|
||||
if (!device_is_ready(this->i2c_dev_)) {
|
||||
return ERROR_NOT_INITIALIZED;
|
||||
}
|
||||
|
||||
i2c_msg msgs[2]{};
|
||||
size_t cnt = 0;
|
||||
uint8_t dst = 0x00; // dummy data to not use random value
|
||||
|
||||
if (read_count == 0 && write_count == 0) {
|
||||
msgs[cnt].buf = &dst;
|
||||
msgs[cnt].len = 0U;
|
||||
msgs[cnt++].flags = I2C_MSG_WRITE;
|
||||
} else {
|
||||
if (write_count) {
|
||||
// the same struct is used for read/write — const cast is fine; data isn't modified
|
||||
msgs[cnt].buf = const_cast<uint8_t *>(write_buffer);
|
||||
msgs[cnt].len = write_count;
|
||||
msgs[cnt++].flags = I2C_MSG_WRITE;
|
||||
}
|
||||
if (read_count) {
|
||||
msgs[cnt].buf = const_cast<uint8_t *>(read_buffer);
|
||||
msgs[cnt].len = read_count;
|
||||
msgs[cnt++].flags = I2C_MSG_READ | I2C_MSG_RESTART;
|
||||
}
|
||||
}
|
||||
|
||||
msgs[cnt - 1].flags |= I2C_MSG_STOP;
|
||||
|
||||
auto err = i2c_transfer(this->i2c_dev_, msgs, cnt, address);
|
||||
|
||||
if (err == -EIO) {
|
||||
return ERROR_NOT_ACKNOWLEDGED;
|
||||
}
|
||||
|
||||
if (err != 0) {
|
||||
ESP_LOGE(TAG, "i2c transfer error %d", err);
|
||||
return ERROR_UNKNOWN;
|
||||
}
|
||||
|
||||
return ERROR_OK;
|
||||
}
|
||||
|
||||
void ZephyrI2CBus::set_frequency(uint32_t frequency) {
|
||||
this->dev_config_ &= ~I2C_SPEED_MASK;
|
||||
if (frequency >= 400000) {
|
||||
this->dev_config_ |= I2C_SPEED_SET(I2C_SPEED_FAST);
|
||||
} else {
|
||||
this->dev_config_ |= I2C_SPEED_SET(I2C_SPEED_STANDARD);
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace esphome::i2c
|
||||
|
||||
#endif
|
||||
38
esphome/components/i2c/i2c_bus_zephyr.h
Normal file
38
esphome/components/i2c/i2c_bus_zephyr.h
Normal file
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#ifdef USE_ZEPHYR
|
||||
|
||||
#include "i2c_bus.h"
|
||||
#include "esphome/core/component.h"
|
||||
|
||||
struct device;
|
||||
|
||||
namespace esphome::i2c {
|
||||
|
||||
class ZephyrI2CBus : public InternalI2CBus, public Component {
|
||||
public:
|
||||
explicit ZephyrI2CBus(const device *i2c_dev) : i2c_dev_(i2c_dev) {}
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
ErrorCode write_readv(uint8_t address, const uint8_t *write_buffer, size_t write_count, uint8_t *read_buffer,
|
||||
size_t read_count) override;
|
||||
float get_setup_priority() const override { return setup_priority::BUS; }
|
||||
|
||||
void set_scan(bool scan) { scan_ = scan; }
|
||||
void set_sda_pin(uint8_t sda_pin) { this->sda_pin_ = sda_pin; }
|
||||
void set_scl_pin(uint8_t scl_pin) { this->scl_pin_ = scl_pin; }
|
||||
void set_frequency(uint32_t frequency);
|
||||
|
||||
int get_port() const override { return 0; }
|
||||
|
||||
protected:
|
||||
const device *i2c_dev_;
|
||||
int recovery_result_ = 0;
|
||||
uint8_t sda_pin_{};
|
||||
uint8_t scl_pin_{};
|
||||
uint32_t dev_config_{};
|
||||
};
|
||||
|
||||
} // namespace esphome::i2c
|
||||
|
||||
#endif
|
||||
@@ -121,9 +121,9 @@ constexpr Uint8ToString OUT_PIN_LEVELS_BY_UINT[] = {
|
||||
};
|
||||
|
||||
// Helper functions for lookups
|
||||
template<size_t N> uint8_t find_uint8(const StringToUint8 (&arr)[N], const char *str) {
|
||||
template<size_t N> uint8_t find_uint8(const StringToUint8 (&arr)[N], const std::string &str) {
|
||||
for (const auto &entry : arr) {
|
||||
if (strcmp(str, entry.str) == 0)
|
||||
if (str == entry.str)
|
||||
return entry.value;
|
||||
}
|
||||
return 0xFF; // Not found
|
||||
@@ -441,7 +441,7 @@ bool LD2410Component::handle_ack_data_() {
|
||||
ESP_LOGV(TAG, "Baud rate change");
|
||||
#ifdef USE_SELECT
|
||||
if (this->baud_rate_select_ != nullptr) {
|
||||
ESP_LOGE(TAG, "Change baud rate to %s and reinstall", this->baud_rate_select_->current_option());
|
||||
ESP_LOGE(TAG, "Change baud rate to %s and reinstall", this->baud_rate_select_->state.c_str());
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
@@ -626,14 +626,14 @@ void LD2410Component::set_bluetooth(bool enable) {
|
||||
this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
|
||||
}
|
||||
|
||||
void LD2410Component::set_distance_resolution(const char *state) {
|
||||
void LD2410Component::set_distance_resolution(const std::string &state) {
|
||||
this->set_config_mode_(true);
|
||||
const uint8_t cmd_value[2] = {find_uint8(DISTANCE_RESOLUTIONS_BY_STR, state), 0x00};
|
||||
this->send_command_(CMD_SET_DISTANCE_RESOLUTION, cmd_value, sizeof(cmd_value));
|
||||
this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
|
||||
}
|
||||
|
||||
void LD2410Component::set_baud_rate(const char *state) {
|
||||
void LD2410Component::set_baud_rate(const std::string &state) {
|
||||
this->set_config_mode_(true);
|
||||
const uint8_t cmd_value[2] = {find_uint8(BAUD_RATES_BY_STR, state), 0x00};
|
||||
this->send_command_(CMD_SET_BAUD_RATE, cmd_value, sizeof(cmd_value));
|
||||
@@ -759,10 +759,10 @@ void LD2410Component::set_light_out_control() {
|
||||
#endif
|
||||
#ifdef USE_SELECT
|
||||
if (this->light_function_select_ != nullptr && this->light_function_select_->has_state()) {
|
||||
this->light_function_ = find_uint8(LIGHT_FUNCTIONS_BY_STR, this->light_function_select_->current_option());
|
||||
this->light_function_ = find_uint8(LIGHT_FUNCTIONS_BY_STR, this->light_function_select_->state);
|
||||
}
|
||||
if (this->out_pin_level_select_ != nullptr && this->out_pin_level_select_->has_state()) {
|
||||
this->out_pin_level_ = find_uint8(OUT_PIN_LEVELS_BY_STR, this->out_pin_level_select_->current_option());
|
||||
this->out_pin_level_ = find_uint8(OUT_PIN_LEVELS_BY_STR, this->out_pin_level_select_->state);
|
||||
}
|
||||
#endif
|
||||
this->set_config_mode_(true);
|
||||
|
||||
@@ -98,8 +98,8 @@ class LD2410Component : public Component, public uart::UARTDevice {
|
||||
void read_all_info();
|
||||
void restart_and_read_all_info();
|
||||
void set_bluetooth(bool enable);
|
||||
void set_distance_resolution(const char *state);
|
||||
void set_baud_rate(const char *state);
|
||||
void set_distance_resolution(const std::string &state);
|
||||
void set_baud_rate(const std::string &state);
|
||||
void factory_reset();
|
||||
|
||||
protected:
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
namespace esphome {
|
||||
namespace ld2410 {
|
||||
|
||||
void BaudRateSelect::control(size_t index) {
|
||||
this->publish_state(index);
|
||||
this->parent_->set_baud_rate(this->option_at(index));
|
||||
void BaudRateSelect::control(const std::string &value) {
|
||||
this->publish_state(value);
|
||||
this->parent_->set_baud_rate(state);
|
||||
}
|
||||
|
||||
} // namespace ld2410
|
||||
|
||||
@@ -11,7 +11,7 @@ class BaudRateSelect : public select::Select, public Parented<LD2410Component> {
|
||||
BaudRateSelect() = default;
|
||||
|
||||
protected:
|
||||
void control(size_t index) override;
|
||||
void control(const std::string &value) override;
|
||||
};
|
||||
|
||||
} // namespace ld2410
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
namespace esphome {
|
||||
namespace ld2410 {
|
||||
|
||||
void DistanceResolutionSelect::control(size_t index) {
|
||||
this->publish_state(index);
|
||||
this->parent_->set_distance_resolution(this->option_at(index));
|
||||
void DistanceResolutionSelect::control(const std::string &value) {
|
||||
this->publish_state(value);
|
||||
this->parent_->set_distance_resolution(state);
|
||||
}
|
||||
|
||||
} // namespace ld2410
|
||||
|
||||
@@ -11,7 +11,7 @@ class DistanceResolutionSelect : public select::Select, public Parented<LD2410Co
|
||||
DistanceResolutionSelect() = default;
|
||||
|
||||
protected:
|
||||
void control(size_t index) override;
|
||||
void control(const std::string &value) override;
|
||||
};
|
||||
|
||||
} // namespace ld2410
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
namespace esphome {
|
||||
namespace ld2410 {
|
||||
|
||||
void LightOutControlSelect::control(size_t index) {
|
||||
this->publish_state(index);
|
||||
void LightOutControlSelect::control(const std::string &value) {
|
||||
this->publish_state(value);
|
||||
this->parent_->set_light_out_control();
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ class LightOutControlSelect : public select::Select, public Parented<LD2410Compo
|
||||
LightOutControlSelect() = default;
|
||||
|
||||
protected:
|
||||
void control(size_t index) override;
|
||||
void control(const std::string &value) override;
|
||||
};
|
||||
|
||||
} // namespace ld2410
|
||||
|
||||
@@ -132,9 +132,9 @@ constexpr Uint8ToString OUT_PIN_LEVELS_BY_UINT[] = {
|
||||
};
|
||||
|
||||
// Helper functions for lookups
|
||||
template<size_t N> uint8_t find_uint8(const StringToUint8 (&arr)[N], const char *str) {
|
||||
template<size_t N> uint8_t find_uint8(const StringToUint8 (&arr)[N], const std::string &str) {
|
||||
for (const auto &entry : arr) {
|
||||
if (strcmp(str, entry.str) == 0) {
|
||||
if (str == entry.str) {
|
||||
return entry.value;
|
||||
}
|
||||
}
|
||||
@@ -485,7 +485,7 @@ bool LD2412Component::handle_ack_data_() {
|
||||
ESP_LOGV(TAG, "Baud rate change");
|
||||
#ifdef USE_SELECT
|
||||
if (this->baud_rate_select_ != nullptr) {
|
||||
ESP_LOGW(TAG, "Change baud rate to %s and reinstall", this->baud_rate_select_->current_option());
|
||||
ESP_LOGW(TAG, "Change baud rate to %s and reinstall", this->baud_rate_select_->state.c_str());
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
@@ -699,14 +699,14 @@ void LD2412Component::set_bluetooth(bool enable) {
|
||||
this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
|
||||
}
|
||||
|
||||
void LD2412Component::set_distance_resolution(const char *state) {
|
||||
void LD2412Component::set_distance_resolution(const std::string &state) {
|
||||
this->set_config_mode_(true);
|
||||
const uint8_t cmd_value[6] = {find_uint8(DISTANCE_RESOLUTIONS_BY_STR, state), 0x00, 0x00, 0x00, 0x00, 0x00};
|
||||
this->send_command_(CMD_SET_DISTANCE_RESOLUTION, cmd_value, sizeof(cmd_value));
|
||||
this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
|
||||
}
|
||||
|
||||
void LD2412Component::set_baud_rate(const char *state) {
|
||||
void LD2412Component::set_baud_rate(const std::string &state) {
|
||||
this->set_config_mode_(true);
|
||||
const uint8_t cmd_value[2] = {find_uint8(BAUD_RATES_BY_STR, state), 0x00};
|
||||
this->send_command_(CMD_SET_BAUD_RATE, cmd_value, sizeof(cmd_value));
|
||||
@@ -783,7 +783,7 @@ void LD2412Component::set_basic_config() {
|
||||
1, TOTAL_GATES, DEFAULT_PRESENCE_TIMEOUT, 0,
|
||||
#endif
|
||||
#ifdef USE_SELECT
|
||||
find_uint8(OUT_PIN_LEVELS_BY_STR, this->out_pin_level_select_->current_option()),
|
||||
find_uint8(OUT_PIN_LEVELS_BY_STR, this->out_pin_level_select_->state),
|
||||
#else
|
||||
0x01, // Default value if not using select
|
||||
#endif
|
||||
@@ -837,7 +837,7 @@ void LD2412Component::set_light_out_control() {
|
||||
#endif
|
||||
#ifdef USE_SELECT
|
||||
if (this->light_function_select_ != nullptr && this->light_function_select_->has_state()) {
|
||||
this->light_function_ = find_uint8(LIGHT_FUNCTIONS_BY_STR, this->light_function_select_->current_option());
|
||||
this->light_function_ = find_uint8(LIGHT_FUNCTIONS_BY_STR, this->light_function_select_->state);
|
||||
}
|
||||
#endif
|
||||
uint8_t value[2] = {this->light_function_, this->light_threshold_};
|
||||
|
||||
@@ -99,8 +99,8 @@ class LD2412Component : public Component, public uart::UARTDevice {
|
||||
void read_all_info();
|
||||
void restart_and_read_all_info();
|
||||
void set_bluetooth(bool enable);
|
||||
void set_distance_resolution(const char *state);
|
||||
void set_baud_rate(const char *state);
|
||||
void set_distance_resolution(const std::string &state);
|
||||
void set_baud_rate(const std::string &state);
|
||||
void factory_reset();
|
||||
void start_dynamic_background_correction();
|
||||
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
namespace esphome {
|
||||
namespace ld2412 {
|
||||
|
||||
void BaudRateSelect::control(size_t index) {
|
||||
this->publish_state(index);
|
||||
this->parent_->set_baud_rate(this->option_at(index));
|
||||
void BaudRateSelect::control(const std::string &value) {
|
||||
this->publish_state(value);
|
||||
this->parent_->set_baud_rate(state);
|
||||
}
|
||||
|
||||
} // namespace ld2412
|
||||
|
||||
@@ -11,7 +11,7 @@ class BaudRateSelect : public select::Select, public Parented<LD2412Component> {
|
||||
BaudRateSelect() = default;
|
||||
|
||||
protected:
|
||||
void control(size_t index) override;
|
||||
void control(const std::string &value) override;
|
||||
};
|
||||
|
||||
} // namespace ld2412
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
namespace esphome {
|
||||
namespace ld2412 {
|
||||
|
||||
void DistanceResolutionSelect::control(size_t index) {
|
||||
this->publish_state(index);
|
||||
this->parent_->set_distance_resolution(this->option_at(index));
|
||||
void DistanceResolutionSelect::control(const std::string &value) {
|
||||
this->publish_state(value);
|
||||
this->parent_->set_distance_resolution(state);
|
||||
}
|
||||
|
||||
} // namespace ld2412
|
||||
|
||||
@@ -11,7 +11,7 @@ class DistanceResolutionSelect : public select::Select, public Parented<LD2412Co
|
||||
DistanceResolutionSelect() = default;
|
||||
|
||||
protected:
|
||||
void control(size_t index) override;
|
||||
void control(const std::string &value) override;
|
||||
};
|
||||
|
||||
} // namespace ld2412
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
namespace esphome {
|
||||
namespace ld2412 {
|
||||
|
||||
void LightOutControlSelect::control(size_t index) {
|
||||
this->publish_state(index);
|
||||
void LightOutControlSelect::control(const std::string &value) {
|
||||
this->publish_state(value);
|
||||
this->parent_->set_light_out_control();
|
||||
}
|
||||
|
||||
|
||||
@@ -11,7 +11,7 @@ class LightOutControlSelect : public select::Select, public Parented<LD2412Compo
|
||||
LightOutControlSelect() = default;
|
||||
|
||||
protected:
|
||||
void control(size_t index) override;
|
||||
void control(const std::string &value) override;
|
||||
};
|
||||
|
||||
} // namespace ld2412
|
||||
|
||||
@@ -131,8 +131,8 @@ static const uint8_t CMD_FRAME_STATUS = 7;
|
||||
static const uint8_t CMD_ERROR_WORD = 8;
|
||||
static const uint8_t ENERGY_SENSOR_START = 9;
|
||||
static const uint8_t CALIBRATE_REPORT_INTERVAL = 4;
|
||||
static const char *const OP_NORMAL_MODE_STRING = "Normal";
|
||||
static const char *const OP_SIMPLE_MODE_STRING = "Simple";
|
||||
static const std::string OP_NORMAL_MODE_STRING = "Normal";
|
||||
static const std::string OP_SIMPLE_MODE_STRING = "Simple";
|
||||
|
||||
// Memory-efficient lookup tables
|
||||
struct StringToUint8 {
|
||||
@@ -379,7 +379,7 @@ void LD2420Component::report_gate_data() {
|
||||
ESP_LOGI(TAG, "Total samples: %d", this->total_sample_number_counter);
|
||||
}
|
||||
|
||||
void LD2420Component::set_operating_mode(const char *state) {
|
||||
void LD2420Component::set_operating_mode(const std::string &state) {
|
||||
// If unsupported firmware ignore mode select
|
||||
if (ld2420::get_firmware_int(firmware_ver_) >= CALIBRATE_VERSION_MIN) {
|
||||
this->current_operating_mode = find_uint8(OP_MODE_BY_STR, state);
|
||||
|
||||
@@ -107,7 +107,7 @@ class LD2420Component : public Component, public uart::UARTDevice {
|
||||
int send_cmd_from_array(CmdFrameT cmd_frame);
|
||||
void report_gate_data();
|
||||
void handle_cmd_error(uint8_t error);
|
||||
void set_operating_mode(const char *state);
|
||||
void set_operating_mode(const std::string &state);
|
||||
void auto_calibrate_sensitivity();
|
||||
void update_radar_data(uint16_t const *gate_energy, uint8_t sample_number);
|
||||
uint8_t set_config_mode(bool enable);
|
||||
|
||||
@@ -7,9 +7,9 @@ namespace ld2420 {
|
||||
|
||||
static const char *const TAG = "ld2420.select";
|
||||
|
||||
void LD2420Select::control(size_t index) {
|
||||
this->publish_state(index);
|
||||
this->parent_->set_operating_mode(this->option_at(index));
|
||||
void LD2420Select::control(const std::string &value) {
|
||||
this->publish_state(value);
|
||||
this->parent_->set_operating_mode(value);
|
||||
}
|
||||
|
||||
} // namespace ld2420
|
||||
|
||||
@@ -11,7 +11,7 @@ class LD2420Select : public Component, public select::Select, public Parented<LD
|
||||
LD2420Select() = default;
|
||||
|
||||
protected:
|
||||
void control(size_t index) override;
|
||||
void control(const std::string &value) override;
|
||||
};
|
||||
|
||||
} // namespace ld2420
|
||||
|
||||
@@ -380,7 +380,7 @@ void LD2450Component::read_all_info() {
|
||||
this->set_config_mode_(false);
|
||||
#ifdef USE_SELECT
|
||||
const auto baud_rate = std::to_string(this->parent_->get_baud_rate());
|
||||
if (this->baud_rate_select_ != nullptr && strcmp(this->baud_rate_select_->current_option(), baud_rate.c_str()) != 0) {
|
||||
if (this->baud_rate_select_ != nullptr && this->baud_rate_select_->state != baud_rate) {
|
||||
this->baud_rate_select_->publish_state(baud_rate);
|
||||
}
|
||||
this->publish_zone_type();
|
||||
@@ -635,7 +635,7 @@ bool LD2450Component::handle_ack_data_() {
|
||||
ESP_LOGV(TAG, "Baud rate change");
|
||||
#ifdef USE_SELECT
|
||||
if (this->baud_rate_select_ != nullptr) {
|
||||
ESP_LOGE(TAG, "Change baud rate to %s and reinstall", this->baud_rate_select_->current_option());
|
||||
ESP_LOGE(TAG, "Change baud rate to %s and reinstall", this->baud_rate_select_->state.c_str());
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
@@ -716,7 +716,7 @@ bool LD2450Component::handle_ack_data_() {
|
||||
this->publish_zone_type();
|
||||
#ifdef USE_SELECT
|
||||
if (this->zone_type_select_ != nullptr) {
|
||||
ESP_LOGV(TAG, "Change zone type to: %s", this->zone_type_select_->current_option());
|
||||
ESP_LOGV(TAG, "Change zone type to: %s", this->zone_type_select_->state.c_str());
|
||||
}
|
||||
#endif
|
||||
if (this->buffer_data_[10] == 0x00) {
|
||||
@@ -790,7 +790,7 @@ void LD2450Component::set_bluetooth(bool enable) {
|
||||
}
|
||||
|
||||
// Set Baud rate
|
||||
void LD2450Component::set_baud_rate(const char *state) {
|
||||
void LD2450Component::set_baud_rate(const std::string &state) {
|
||||
this->set_config_mode_(true);
|
||||
const uint8_t cmd_value[2] = {find_uint8(BAUD_RATES_BY_STR, state), 0x00};
|
||||
this->send_command_(CMD_SET_BAUD_RATE, cmd_value, sizeof(cmd_value));
|
||||
@@ -798,8 +798,8 @@ void LD2450Component::set_baud_rate(const char *state) {
|
||||
}
|
||||
|
||||
// Set Zone Type - one of: Disabled, Detection, Filter
|
||||
void LD2450Component::set_zone_type(const char *state) {
|
||||
ESP_LOGV(TAG, "Set zone type: %s", state);
|
||||
void LD2450Component::set_zone_type(const std::string &state) {
|
||||
ESP_LOGV(TAG, "Set zone type: %s", state.c_str());
|
||||
uint8_t zone_type = find_uint8(ZONE_TYPE_BY_STR, state);
|
||||
this->zone_type_ = zone_type;
|
||||
this->send_set_zone_command_();
|
||||
|
||||
@@ -115,8 +115,8 @@ class LD2450Component : public Component, public uart::UARTDevice {
|
||||
void restart_and_read_all_info();
|
||||
void set_bluetooth(bool enable);
|
||||
void set_multi_target(bool enable);
|
||||
void set_baud_rate(const char *state);
|
||||
void set_zone_type(const char *state);
|
||||
void set_baud_rate(const std::string &state);
|
||||
void set_zone_type(const std::string &state);
|
||||
void publish_zone_type();
|
||||
void factory_reset();
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
namespace esphome {
|
||||
namespace ld2450 {
|
||||
|
||||
void BaudRateSelect::control(size_t index) {
|
||||
this->publish_state(index);
|
||||
this->parent_->set_baud_rate(this->option_at(index));
|
||||
void BaudRateSelect::control(const std::string &value) {
|
||||
this->publish_state(value);
|
||||
this->parent_->set_baud_rate(state);
|
||||
}
|
||||
|
||||
} // namespace ld2450
|
||||
|
||||
@@ -11,7 +11,7 @@ class BaudRateSelect : public select::Select, public Parented<LD2450Component> {
|
||||
BaudRateSelect() = default;
|
||||
|
||||
protected:
|
||||
void control(size_t index) override;
|
||||
void control(const std::string &value) override;
|
||||
};
|
||||
|
||||
} // namespace ld2450
|
||||
|
||||
@@ -3,9 +3,9 @@
|
||||
namespace esphome {
|
||||
namespace ld2450 {
|
||||
|
||||
void ZoneTypeSelect::control(size_t index) {
|
||||
this->publish_state(index);
|
||||
this->parent_->set_zone_type(this->option_at(index));
|
||||
void ZoneTypeSelect::control(const std::string &value) {
|
||||
this->publish_state(value);
|
||||
this->parent_->set_zone_type(state);
|
||||
}
|
||||
|
||||
} // namespace ld2450
|
||||
|
||||
@@ -11,7 +11,7 @@ class ZoneTypeSelect : public select::Select, public Parented<LD2450Component> {
|
||||
ZoneTypeSelect() = default;
|
||||
|
||||
protected:
|
||||
void control(size_t index) override;
|
||||
void control(const std::string &value) override;
|
||||
};
|
||||
|
||||
} // namespace ld2450
|
||||
|
||||
@@ -3,10 +3,10 @@
|
||||
namespace esphome::logger {
|
||||
|
||||
void LoggerLevelSelect::publish_state(int level) {
|
||||
auto index = level_to_index(level);
|
||||
if (!this->has_index(index))
|
||||
const auto &option = this->at(level_to_index(level));
|
||||
if (!option)
|
||||
return;
|
||||
Select::publish_state(index);
|
||||
Select::publish_state(option.value());
|
||||
}
|
||||
|
||||
void LoggerLevelSelect::setup() {
|
||||
@@ -14,6 +14,11 @@ void LoggerLevelSelect::setup() {
|
||||
this->publish_state(this->parent_->get_log_level());
|
||||
}
|
||||
|
||||
void LoggerLevelSelect::control(size_t index) { this->parent_->set_log_level(index_to_level(index)); }
|
||||
void LoggerLevelSelect::control(const std::string &value) {
|
||||
const auto index = this->index_of(value);
|
||||
if (!index)
|
||||
return;
|
||||
this->parent_->set_log_level(index_to_level(index.value()));
|
||||
}
|
||||
|
||||
} // namespace esphome::logger
|
||||
|
||||
@@ -9,7 +9,7 @@ class LoggerLevelSelect : public Component, public select::Select, public Parent
|
||||
public:
|
||||
void publish_state(int level);
|
||||
void setup() override;
|
||||
void control(size_t index) override;
|
||||
void control(const std::string &value) override;
|
||||
|
||||
protected:
|
||||
// Convert log level to option index (skip CONFIG at level 4)
|
||||
|
||||
@@ -7,7 +7,7 @@ from urllib.parse import urljoin
|
||||
from esphome import automation, external_files, git
|
||||
from esphome.automation import register_action, register_condition
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import esp32, microphone
|
||||
from esphome.components import esp32, microphone, socket
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_FILE,
|
||||
@@ -32,6 +32,7 @@ _LOGGER = logging.getLogger(__name__)
|
||||
|
||||
CODEOWNERS = ["@kahrendt", "@jesserockz"]
|
||||
DEPENDENCIES = ["microphone"]
|
||||
AUTO_LOAD = ["socket"]
|
||||
DOMAIN = "micro_wake_word"
|
||||
|
||||
|
||||
@@ -443,6 +444,10 @@ async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
|
||||
# Enable wake_loop_threadsafe() for low-latency wake word detection
|
||||
# The inference task queues detection events that need immediate processing
|
||||
socket.require_wake_loop_threadsafe()
|
||||
|
||||
mic_source = await microphone.microphone_source_to_code(config[CONF_MICROPHONE])
|
||||
cg.add(var.set_microphone_source(mic_source))
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#ifdef USE_ESP_IDF
|
||||
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
@@ -426,6 +427,12 @@ void MicroWakeWord::process_probabilities_() {
|
||||
if (vad_state.detected) {
|
||||
#endif
|
||||
xQueueSend(this->detection_queue_, &wake_word_state, portMAX_DELAY);
|
||||
|
||||
// Wake main loop immediately to process wake word detection
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
|
||||
App.wake_loop_threadsafe();
|
||||
#endif
|
||||
|
||||
model->reset_probabilities();
|
||||
#ifdef USE_MICRO_WAKE_WORD_VAD
|
||||
} else {
|
||||
|
||||
@@ -218,6 +218,21 @@ def map_sequence(value):
|
||||
return tuple(value)
|
||||
|
||||
|
||||
def flatten_sequence(sequence: tuple | list):
|
||||
"""
|
||||
Flatten an init sequence into a single list of bytes.
|
||||
:param sequence: The list of tuples
|
||||
:return: a list of bytes
|
||||
"""
|
||||
return sum(
|
||||
tuple(
|
||||
(x[1], 0xFF) if x[0] == DELAY_FLAG else (x[0], len(x) - 1) + x[1:]
|
||||
for x in sequence
|
||||
),
|
||||
(),
|
||||
)
|
||||
|
||||
|
||||
def delay(ms):
|
||||
return DELAY_FLAG, ms
|
||||
|
||||
@@ -456,13 +471,7 @@ class DriverChip:
|
||||
|
||||
# Flatten the sequence into a list of bytes, with the length of each command
|
||||
# or the delay flag inserted where needed
|
||||
return sum(
|
||||
tuple(
|
||||
(x[1], 0xFF) if x[0] == DELAY_FLAG else (x[0], len(x) - 1) + x[1:]
|
||||
for x in sequence
|
||||
),
|
||||
(),
|
||||
), madctl
|
||||
return flatten_sequence(sequence), madctl
|
||||
|
||||
|
||||
def requires_buffer(config) -> bool:
|
||||
|
||||
@@ -72,7 +72,7 @@ bool MopekaStdCheck::parse_device(const esp32_ble_tracker::ESPBTDevice &device)
|
||||
|
||||
const u_int8_t hardware_id = mopeka_data->data_1 & 0xCF;
|
||||
if (static_cast<SensorType>(hardware_id) != STANDARD && static_cast<SensorType>(hardware_id) != XL &&
|
||||
static_cast<SensorType>(hardware_id) != ETRAILER) {
|
||||
static_cast<SensorType>(hardware_id) != ETRAILER && static_cast<SensorType>(hardware_id) != STANDARD_ALT) {
|
||||
ESP_LOGE(TAG, "[%s] Unsupported Sensor Type (0x%X)", device.address_str().c_str(), hardware_id);
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -15,6 +15,7 @@ namespace mopeka_std_check {
|
||||
enum SensorType {
|
||||
STANDARD = 0x02,
|
||||
XL = 0x03,
|
||||
STANDARD_ALT = 0x44,
|
||||
ETRAILER = 0x46,
|
||||
};
|
||||
|
||||
|
||||
@@ -21,8 +21,7 @@ void MQTTSelectComponent::setup() {
|
||||
call.set_option(state);
|
||||
call.perform();
|
||||
});
|
||||
this->select_->add_on_state_callback(
|
||||
[this](const std::string &state, size_t index) { this->publish_state(this->select_->option_at(index)); });
|
||||
this->select_->add_on_state_callback([this](const std::string &state, size_t index) { this->publish_state(state); });
|
||||
}
|
||||
|
||||
void MQTTSelectComponent::dump_config() {
|
||||
@@ -45,7 +44,7 @@ void MQTTSelectComponent::send_discovery(JsonObject root, mqtt::SendDiscoveryCon
|
||||
}
|
||||
bool MQTTSelectComponent::send_initial_state() {
|
||||
if (this->select_->has_state()) {
|
||||
return this->publish_state(this->select_->current_option());
|
||||
return this->publish_state(this->select_->state);
|
||||
} else {
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -435,12 +435,12 @@ void MR24HPC1Component::r24_frame_parse_open_underlying_information_(uint8_t *da
|
||||
} else if ((this->existence_boundary_select_ != nullptr) &&
|
||||
((data[FRAME_COMMAND_WORD_INDEX] == 0x0a) || (data[FRAME_COMMAND_WORD_INDEX] == 0x8a))) {
|
||||
if (this->existence_boundary_select_->has_index(data[FRAME_DATA_INDEX] - 1)) {
|
||||
this->existence_boundary_select_->publish_state(data[FRAME_DATA_INDEX] - 1);
|
||||
this->existence_boundary_select_->publish_state(S_BOUNDARY_STR[data[FRAME_DATA_INDEX] - 1]);
|
||||
}
|
||||
} else if ((this->motion_boundary_select_ != nullptr) &&
|
||||
((data[FRAME_COMMAND_WORD_INDEX] == 0x0b) || (data[FRAME_COMMAND_WORD_INDEX] == 0x8b))) {
|
||||
if (this->motion_boundary_select_->has_index(data[FRAME_DATA_INDEX] - 1)) {
|
||||
this->motion_boundary_select_->publish_state(data[FRAME_DATA_INDEX] - 1);
|
||||
this->motion_boundary_select_->publish_state(S_BOUNDARY_STR[data[FRAME_DATA_INDEX] - 1]);
|
||||
}
|
||||
} else if ((this->motion_trigger_number_ != nullptr) &&
|
||||
((data[FRAME_COMMAND_WORD_INDEX] == 0x0c) || (data[FRAME_COMMAND_WORD_INDEX] == 0x8c))) {
|
||||
@@ -515,7 +515,7 @@ void MR24HPC1Component::r24_frame_parse_work_status_(uint8_t *data) {
|
||||
ESP_LOGD(TAG, "Reply: get radar init status 0x%02X", data[FRAME_DATA_INDEX]);
|
||||
} else if (data[FRAME_COMMAND_WORD_INDEX] == 0x07) {
|
||||
if ((this->scene_mode_select_ != nullptr) && (this->scene_mode_select_->has_index(data[FRAME_DATA_INDEX]))) {
|
||||
this->scene_mode_select_->publish_state(data[FRAME_DATA_INDEX]);
|
||||
this->scene_mode_select_->publish_state(S_SCENE_STR[data[FRAME_DATA_INDEX]]);
|
||||
} else {
|
||||
ESP_LOGD(TAG, "Select has index offset %d Error", data[FRAME_DATA_INDEX]);
|
||||
}
|
||||
@@ -538,7 +538,7 @@ void MR24HPC1Component::r24_frame_parse_work_status_(uint8_t *data) {
|
||||
ESP_LOGD(TAG, "Reply: get radar init status 0x%02X", data[FRAME_DATA_INDEX]);
|
||||
} else if (data[FRAME_COMMAND_WORD_INDEX] == 0x87) {
|
||||
if ((this->scene_mode_select_ != nullptr) && (this->scene_mode_select_->has_index(data[FRAME_DATA_INDEX]))) {
|
||||
this->scene_mode_select_->publish_state(data[FRAME_DATA_INDEX]);
|
||||
this->scene_mode_select_->publish_state(S_SCENE_STR[data[FRAME_DATA_INDEX]]);
|
||||
} else {
|
||||
ESP_LOGD(TAG, "Select has index offset %d Error", data[FRAME_DATA_INDEX]);
|
||||
}
|
||||
@@ -581,7 +581,7 @@ void MR24HPC1Component::r24_frame_parse_human_information_(uint8_t *data) {
|
||||
((data[FRAME_COMMAND_WORD_INDEX] == 0x0A) || (data[FRAME_COMMAND_WORD_INDEX] == 0x8A))) {
|
||||
// none:0x00 1s:0x01 30s:0x02 1min:0x03 2min:0x04 5min:0x05 10min:0x06 30min:0x07 1hour:0x08
|
||||
if (data[FRAME_DATA_INDEX] < 9) {
|
||||
this->unman_time_select_->publish_state(data[FRAME_DATA_INDEX]);
|
||||
this->unman_time_select_->publish_state(S_UNMANNED_TIME_STR[data[FRAME_DATA_INDEX]]);
|
||||
}
|
||||
} else if ((this->keep_away_text_sensor_ != nullptr) &&
|
||||
((data[FRAME_COMMAND_WORD_INDEX] == 0x0B) || (data[FRAME_COMMAND_WORD_INDEX] == 0x8B))) {
|
||||
|
||||
@@ -292,7 +292,7 @@ void MR60FDA2Component::process_frame_() {
|
||||
|
||||
install_height_float = bit_cast<float>(current_install_height_int);
|
||||
uint32_t select_index = find_nearest_index(install_height_float, INSTALL_HEIGHT, 7);
|
||||
this->install_height_select_->publish_state(select_index);
|
||||
this->install_height_select_->publish_state(this->install_height_select_->at(select_index).value());
|
||||
}
|
||||
|
||||
if (this->height_threshold_select_ != nullptr) {
|
||||
@@ -301,7 +301,7 @@ void MR60FDA2Component::process_frame_() {
|
||||
|
||||
height_threshold_float = bit_cast<float>(current_height_threshold_int);
|
||||
size_t select_index = find_nearest_index(height_threshold_float, HEIGHT_THRESHOLD, 7);
|
||||
this->height_threshold_select_->publish_state(select_index);
|
||||
this->height_threshold_select_->publish_state(this->height_threshold_select_->at(select_index).value());
|
||||
}
|
||||
|
||||
if (this->sensitivity_select_ != nullptr) {
|
||||
@@ -309,7 +309,7 @@ void MR60FDA2Component::process_frame_() {
|
||||
encode_uint32(current_data_buf_[11], current_data_buf_[10], current_data_buf_[9], current_data_buf_[8]);
|
||||
|
||||
uint32_t select_index = find_nearest_index(current_sensitivity, SENSITIVITY, 3);
|
||||
this->sensitivity_select_->publish_state(select_index);
|
||||
this->sensitivity_select_->publish_state(this->sensitivity_select_->at(select_index).value());
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG, "Mounting height: %.2f, Height threshold: %.2f, Sensitivity: %" PRIu32, install_height_float,
|
||||
|
||||
@@ -7,43 +7,24 @@ namespace select {
|
||||
|
||||
static const char *const TAG = "select";
|
||||
|
||||
void Select::publish_state(const std::string &state) { this->publish_state(state.c_str()); }
|
||||
|
||||
void Select::publish_state(const char *state) {
|
||||
void Select::publish_state(const std::string &state) {
|
||||
auto index = this->index_of(state);
|
||||
const auto *name = this->get_name().c_str();
|
||||
if (index.has_value()) {
|
||||
this->publish_state(index.value());
|
||||
this->set_has_state(true);
|
||||
this->state = state;
|
||||
ESP_LOGD(TAG, "'%s': Sending state %s (index %zu)", name, state.c_str(), index.value());
|
||||
this->state_callback_.call(state, index.value());
|
||||
} else {
|
||||
ESP_LOGE(TAG, "'%s': Invalid option %s", this->get_name().c_str(), state);
|
||||
ESP_LOGE(TAG, "'%s': invalid state for publish_state(): %s", name, state.c_str());
|
||||
}
|
||||
}
|
||||
|
||||
void Select::publish_state(size_t index) {
|
||||
if (!this->has_index(index)) {
|
||||
ESP_LOGE(TAG, "'%s': Invalid index %zu", this->get_name().c_str(), index);
|
||||
return;
|
||||
}
|
||||
const char *option = this->option_at(index);
|
||||
this->set_has_state(true);
|
||||
this->active_index_ = index;
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
this->state = option; // Update deprecated member for backward compatibility
|
||||
#pragma GCC diagnostic pop
|
||||
ESP_LOGD(TAG, "'%s': Sending state %s (index %zu)", this->get_name().c_str(), option, index);
|
||||
// Callback signature requires std::string, create temporary for compatibility
|
||||
this->state_callback_.call(std::string(option), index);
|
||||
}
|
||||
|
||||
const char *Select::current_option() const { return this->has_state() ? this->option_at(this->active_index_) : ""; }
|
||||
|
||||
void Select::add_on_state_callback(std::function<void(std::string, size_t)> &&callback) {
|
||||
this->state_callback_.add(std::move(callback));
|
||||
}
|
||||
|
||||
bool Select::has_option(const std::string &option) const { return this->index_of(option.c_str()).has_value(); }
|
||||
|
||||
bool Select::has_option(const char *option) const { return this->index_of(option).has_value(); }
|
||||
bool Select::has_option(const std::string &option) const { return this->index_of(option).has_value(); }
|
||||
|
||||
bool Select::has_index(size_t index) const { return index < this->size(); }
|
||||
|
||||
@@ -52,12 +33,10 @@ size_t Select::size() const {
|
||||
return options.size();
|
||||
}
|
||||
|
||||
optional<size_t> Select::index_of(const std::string &option) const { return this->index_of(option.c_str()); }
|
||||
|
||||
optional<size_t> Select::index_of(const char *option) const {
|
||||
optional<size_t> Select::index_of(const std::string &option) const {
|
||||
const auto &options = traits.get_options();
|
||||
for (size_t i = 0; i < options.size(); i++) {
|
||||
if (strcmp(options[i], option) == 0) {
|
||||
if (strcmp(options[i], option.c_str()) == 0) {
|
||||
return i;
|
||||
}
|
||||
}
|
||||
@@ -66,17 +45,19 @@ optional<size_t> Select::index_of(const char *option) const {
|
||||
|
||||
optional<size_t> Select::active_index() const {
|
||||
if (this->has_state()) {
|
||||
return this->active_index_;
|
||||
return this->index_of(this->state);
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
optional<std::string> Select::at(size_t index) const {
|
||||
if (this->has_index(index)) {
|
||||
const auto &options = traits.get_options();
|
||||
return std::string(options.at(index));
|
||||
} else {
|
||||
return {};
|
||||
}
|
||||
return {};
|
||||
}
|
||||
|
||||
const char *Select::option_at(size_t index) const { return traits.get_options().at(index); }
|
||||
|
||||
@@ -30,31 +30,16 @@ namespace select {
|
||||
*/
|
||||
class Select : public EntityBase {
|
||||
public:
|
||||
std::string state;
|
||||
SelectTraits traits;
|
||||
|
||||
#pragma GCC diagnostic push
|
||||
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
|
||||
/// @deprecated Use current_option() instead. This member will be removed in ESPHome 2026.5.0.
|
||||
__attribute__((deprecated("Use current_option() instead of .state. Will be removed in 2026.5.0")))
|
||||
std::string state{};
|
||||
|
||||
Select() = default;
|
||||
~Select() = default;
|
||||
#pragma GCC diagnostic pop
|
||||
|
||||
void publish_state(const std::string &state);
|
||||
void publish_state(const char *state);
|
||||
void publish_state(size_t index);
|
||||
|
||||
/// Return the currently selected option (as const char* from flash).
|
||||
const char *current_option() const;
|
||||
|
||||
/// Instantiate a SelectCall object to modify this select component's state.
|
||||
SelectCall make_call() { return SelectCall(this); }
|
||||
|
||||
/// Return whether this select component contains the provided option.
|
||||
bool has_option(const std::string &option) const;
|
||||
bool has_option(const char *option) const;
|
||||
|
||||
/// Return whether this select component contains the provided index offset.
|
||||
bool has_index(size_t index) const;
|
||||
@@ -64,7 +49,6 @@ class Select : public EntityBase {
|
||||
|
||||
/// Find the (optional) index offset of the provided option value.
|
||||
optional<size_t> index_of(const std::string &option) const;
|
||||
optional<size_t> index_of(const char *option) const;
|
||||
|
||||
/// Return the (optional) index offset of the currently active option.
|
||||
optional<size_t> active_index() const;
|
||||
@@ -80,36 +64,13 @@ class Select : public EntityBase {
|
||||
protected:
|
||||
friend class SelectCall;
|
||||
|
||||
size_t active_index_{0};
|
||||
|
||||
/** Set the value of the select by index, this is an optional virtual method.
|
||||
/** Set the value of the select, this is a virtual method that each select integration must implement.
|
||||
*
|
||||
* IMPORTANT: At least ONE of the two control() methods must be overridden by derived classes.
|
||||
* Overriding this index-based version is PREFERRED as it avoids string conversions.
|
||||
* This method is called by the SelectCall.
|
||||
*
|
||||
* This method is called by the SelectCall when the index is already known.
|
||||
* Default implementation converts to string and calls control(const std::string&).
|
||||
*
|
||||
* @param index The index as validated by the SelectCall.
|
||||
* @param value The value as validated by the SelectCall.
|
||||
*/
|
||||
virtual void control(size_t index) { this->control(this->option_at(index)); }
|
||||
|
||||
/** Set the value of the select, this is a virtual method that each select integration can implement.
|
||||
*
|
||||
* IMPORTANT: At least ONE of the two control() methods must be overridden by derived classes.
|
||||
* Overriding control(size_t) is PREFERRED as it avoids string conversions.
|
||||
*
|
||||
* This method is called by control(size_t) when not overridden, or directly by external code.
|
||||
* Default implementation converts to index and calls control(size_t).
|
||||
*
|
||||
* @param value The value as validated by the caller.
|
||||
*/
|
||||
virtual void control(const std::string &value) {
|
||||
auto index = this->index_of(value);
|
||||
if (index.has_value()) {
|
||||
this->control(index.value());
|
||||
}
|
||||
}
|
||||
virtual void control(const std::string &value) = 0;
|
||||
|
||||
CallbackManager<void(std::string, size_t)> state_callback_;
|
||||
};
|
||||
|
||||
@@ -7,21 +7,19 @@ namespace select {
|
||||
|
||||
static const char *const TAG = "select";
|
||||
|
||||
SelectCall &SelectCall::set_option(const std::string &option) { return this->with_option(option); }
|
||||
|
||||
SelectCall &SelectCall::set_option(const char *option) { return this->with_option(option); }
|
||||
|
||||
SelectCall &SelectCall::set_index(size_t index) { return this->with_index(index); }
|
||||
|
||||
SelectCall &SelectCall::select_next(bool cycle) { return this->with_operation(SELECT_OP_NEXT).with_cycle(cycle); }
|
||||
|
||||
SelectCall &SelectCall::select_previous(bool cycle) {
|
||||
return this->with_operation(SELECT_OP_PREVIOUS).with_cycle(cycle);
|
||||
SelectCall &SelectCall::set_option(const std::string &option) {
|
||||
return with_operation(SELECT_OP_SET).with_option(option);
|
||||
}
|
||||
|
||||
SelectCall &SelectCall::select_first() { return this->with_operation(SELECT_OP_FIRST); }
|
||||
SelectCall &SelectCall::set_index(size_t index) { return with_operation(SELECT_OP_SET_INDEX).with_index(index); }
|
||||
|
||||
SelectCall &SelectCall::select_last() { return this->with_operation(SELECT_OP_LAST); }
|
||||
SelectCall &SelectCall::select_next(bool cycle) { return with_operation(SELECT_OP_NEXT).with_cycle(cycle); }
|
||||
|
||||
SelectCall &SelectCall::select_previous(bool cycle) { return with_operation(SELECT_OP_PREVIOUS).with_cycle(cycle); }
|
||||
|
||||
SelectCall &SelectCall::select_first() { return with_operation(SELECT_OP_FIRST); }
|
||||
|
||||
SelectCall &SelectCall::select_last() { return with_operation(SELECT_OP_LAST); }
|
||||
|
||||
SelectCall &SelectCall::with_operation(SelectOperation operation) {
|
||||
this->operation_ = operation;
|
||||
@@ -33,96 +31,89 @@ SelectCall &SelectCall::with_cycle(bool cycle) {
|
||||
return *this;
|
||||
}
|
||||
|
||||
SelectCall &SelectCall::with_option(const std::string &option) { return this->with_option(option.c_str()); }
|
||||
|
||||
SelectCall &SelectCall::with_option(const char *option) {
|
||||
this->operation_ = SELECT_OP_SET;
|
||||
// Find the option index - this validates the option exists
|
||||
this->index_ = this->parent_->index_of(option);
|
||||
SelectCall &SelectCall::with_option(const std::string &option) {
|
||||
this->option_ = option;
|
||||
return *this;
|
||||
}
|
||||
|
||||
SelectCall &SelectCall::with_index(size_t index) {
|
||||
this->operation_ = SELECT_OP_SET;
|
||||
if (index >= this->parent_->size()) {
|
||||
ESP_LOGW(TAG, "'%s' - Index value %zu out of bounds", this->parent_->get_name().c_str(), index);
|
||||
this->index_ = {}; // Store nullopt for invalid index
|
||||
} else {
|
||||
this->index_ = index;
|
||||
}
|
||||
this->index_ = index;
|
||||
return *this;
|
||||
}
|
||||
|
||||
optional<size_t> SelectCall::calculate_target_index_(const char *name) {
|
||||
const auto &options = this->parent_->traits.get_options();
|
||||
if (options.empty()) {
|
||||
ESP_LOGW(TAG, "'%s' - Select has no options", name);
|
||||
return {};
|
||||
}
|
||||
|
||||
if (this->operation_ == SELECT_OP_FIRST) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (this->operation_ == SELECT_OP_LAST) {
|
||||
return options.size() - 1;
|
||||
}
|
||||
|
||||
if (this->operation_ == SELECT_OP_SET) {
|
||||
ESP_LOGD(TAG, "'%s' - Setting", name);
|
||||
if (!this->index_.has_value()) {
|
||||
ESP_LOGW(TAG, "'%s' - No option set", name);
|
||||
return {};
|
||||
}
|
||||
return this->index_.value();
|
||||
}
|
||||
|
||||
// SELECT_OP_NEXT or SELECT_OP_PREVIOUS
|
||||
ESP_LOGD(TAG, "'%s' - Selecting %s, with%s cycling", name,
|
||||
this->operation_ == SELECT_OP_NEXT ? LOG_STR_LITERAL("next") : LOG_STR_LITERAL("previous"),
|
||||
this->cycle_ ? LOG_STR_LITERAL("") : LOG_STR_LITERAL("out"));
|
||||
|
||||
const auto size = options.size();
|
||||
if (!this->parent_->has_state()) {
|
||||
return this->operation_ == SELECT_OP_NEXT ? 0 : size - 1;
|
||||
}
|
||||
|
||||
// Use cached active_index_ instead of index_of() lookup
|
||||
const auto active_index = this->parent_->active_index_;
|
||||
if (this->cycle_) {
|
||||
return (size + active_index + (this->operation_ == SELECT_OP_NEXT ? +1 : -1)) % size;
|
||||
}
|
||||
|
||||
if (this->operation_ == SELECT_OP_PREVIOUS && active_index > 0) {
|
||||
return active_index - 1;
|
||||
}
|
||||
|
||||
if (this->operation_ == SELECT_OP_NEXT && active_index < size - 1) {
|
||||
return active_index + 1;
|
||||
}
|
||||
|
||||
return {}; // Can't navigate further without cycling
|
||||
}
|
||||
|
||||
void SelectCall::perform() {
|
||||
auto *parent = this->parent_;
|
||||
const auto *name = parent->get_name().c_str();
|
||||
const auto &traits = parent->traits;
|
||||
const auto &options = traits.get_options();
|
||||
|
||||
if (this->operation_ == SELECT_OP_NONE) {
|
||||
ESP_LOGW(TAG, "'%s' - SelectCall performed without selecting an operation", name);
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate target index (with_index() and with_option() already validate bounds/existence)
|
||||
auto target_index = this->calculate_target_index_(name);
|
||||
if (!target_index.has_value()) {
|
||||
if (options.empty()) {
|
||||
ESP_LOGW(TAG, "'%s' - Cannot perform SelectCall, select has no options", name);
|
||||
return;
|
||||
}
|
||||
|
||||
auto idx = target_index.value();
|
||||
// All operations use indices, call control() by index to avoid string conversion
|
||||
ESP_LOGD(TAG, "'%s' - Set selected option to: %s", name, parent->option_at(idx));
|
||||
parent->control(idx);
|
||||
std::string target_value;
|
||||
|
||||
if (this->operation_ == SELECT_OP_SET) {
|
||||
ESP_LOGD(TAG, "'%s' - Setting", name);
|
||||
if (!this->option_.has_value()) {
|
||||
ESP_LOGW(TAG, "'%s' - No option value set for SelectCall", name);
|
||||
return;
|
||||
}
|
||||
target_value = this->option_.value();
|
||||
} else if (this->operation_ == SELECT_OP_SET_INDEX) {
|
||||
if (!this->index_.has_value()) {
|
||||
ESP_LOGW(TAG, "'%s' - No index value set for SelectCall", name);
|
||||
return;
|
||||
}
|
||||
if (this->index_.value() >= options.size()) {
|
||||
ESP_LOGW(TAG, "'%s' - Index value %zu out of bounds", name, this->index_.value());
|
||||
return;
|
||||
}
|
||||
target_value = options[this->index_.value()];
|
||||
} else if (this->operation_ == SELECT_OP_FIRST) {
|
||||
target_value = options.front();
|
||||
} else if (this->operation_ == SELECT_OP_LAST) {
|
||||
target_value = options.back();
|
||||
} else if (this->operation_ == SELECT_OP_NEXT || this->operation_ == SELECT_OP_PREVIOUS) {
|
||||
auto cycle = this->cycle_;
|
||||
ESP_LOGD(TAG, "'%s' - Selecting %s, with%s cycling", name, this->operation_ == SELECT_OP_NEXT ? "next" : "previous",
|
||||
cycle ? "" : "out");
|
||||
if (!parent->has_state()) {
|
||||
target_value = this->operation_ == SELECT_OP_NEXT ? options.front() : options.back();
|
||||
} else {
|
||||
auto index = parent->index_of(parent->state);
|
||||
if (index.has_value()) {
|
||||
auto size = options.size();
|
||||
if (cycle) {
|
||||
auto use_index = (size + index.value() + (this->operation_ == SELECT_OP_NEXT ? +1 : -1)) % size;
|
||||
target_value = options[use_index];
|
||||
} else {
|
||||
if (this->operation_ == SELECT_OP_PREVIOUS && index.value() > 0) {
|
||||
target_value = options[index.value() - 1];
|
||||
} else if (this->operation_ == SELECT_OP_NEXT && index.value() < options.size() - 1) {
|
||||
target_value = options[index.value() + 1];
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
target_value = this->operation_ == SELECT_OP_NEXT ? options.front() : options.back();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!parent->has_option(target_value)) {
|
||||
ESP_LOGW(TAG, "'%s' - Option %s is not a valid option", name, target_value.c_str());
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGD(TAG, "'%s' - Set selected option to: %s", name, target_value.c_str());
|
||||
parent->control(target_value);
|
||||
}
|
||||
|
||||
} // namespace select
|
||||
|
||||
@@ -10,6 +10,7 @@ class Select;
|
||||
enum SelectOperation {
|
||||
SELECT_OP_NONE,
|
||||
SELECT_OP_SET,
|
||||
SELECT_OP_SET_INDEX,
|
||||
SELECT_OP_NEXT,
|
||||
SELECT_OP_PREVIOUS,
|
||||
SELECT_OP_FIRST,
|
||||
@@ -22,7 +23,6 @@ class SelectCall {
|
||||
void perform();
|
||||
|
||||
SelectCall &set_option(const std::string &option);
|
||||
SelectCall &set_option(const char *option);
|
||||
SelectCall &set_index(size_t index);
|
||||
|
||||
SelectCall &select_next(bool cycle);
|
||||
@@ -33,13 +33,11 @@ class SelectCall {
|
||||
SelectCall &with_operation(SelectOperation operation);
|
||||
SelectCall &with_cycle(bool cycle);
|
||||
SelectCall &with_option(const std::string &option);
|
||||
SelectCall &with_option(const char *option);
|
||||
SelectCall &with_index(size_t index);
|
||||
|
||||
protected:
|
||||
__attribute__((always_inline)) inline optional<size_t> calculate_target_index_(const char *name);
|
||||
|
||||
Select *const parent_;
|
||||
optional<std::string> option_;
|
||||
optional<size_t> index_;
|
||||
SelectOperation operation_{SELECT_OP_NONE};
|
||||
bool cycle_;
|
||||
|
||||
@@ -15,6 +15,9 @@ IMPLEMENTATION_BSD_SOCKETS = "bsd_sockets"
|
||||
# Components register their socket needs and platforms read this to configure appropriately
|
||||
KEY_SOCKET_CONSUMERS = "socket_consumers"
|
||||
|
||||
# Wake loop threadsafe support tracking
|
||||
KEY_WAKE_LOOP_THREADSAFE_REQUIRED = "wake_loop_threadsafe_required"
|
||||
|
||||
|
||||
def consume_sockets(
|
||||
value: int, consumer: str
|
||||
@@ -37,6 +40,30 @@ def consume_sockets(
|
||||
return _consume_sockets
|
||||
|
||||
|
||||
def require_wake_loop_threadsafe() -> None:
|
||||
"""Mark that wake_loop_threadsafe support is required by a component.
|
||||
|
||||
Call this from components that need to wake the main event loop from background threads.
|
||||
This enables the shared UDP loopback socket mechanism (~208 bytes RAM).
|
||||
The socket is shared across all components that use this feature.
|
||||
|
||||
IMPORTANT: This is for background thread context only, NOT ISR context.
|
||||
Socket operations are not safe to call from ISR handlers.
|
||||
|
||||
Example:
|
||||
from esphome.components import socket
|
||||
|
||||
async def to_code(config):
|
||||
socket.require_wake_loop_threadsafe()
|
||||
"""
|
||||
# Only set up once (idempotent - multiple components can call this)
|
||||
if not CORE.data.get(KEY_WAKE_LOOP_THREADSAFE_REQUIRED, False):
|
||||
CORE.data[KEY_WAKE_LOOP_THREADSAFE_REQUIRED] = True
|
||||
cg.add_define("USE_WAKE_LOOP_THREADSAFE")
|
||||
# Consume 1 socket for the shared wake notification socket
|
||||
consume_sockets(1, "socket.wake_loop_threadsafe")({})
|
||||
|
||||
|
||||
CONFIG_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.SplitDefault(
|
||||
|
||||
@@ -4,7 +4,6 @@
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome::split_buffer {
|
||||
|
||||
static constexpr const char *const TAG = "split_buffer";
|
||||
|
||||
SplitBuffer::~SplitBuffer() { this->free(); }
|
||||
@@ -102,32 +101,44 @@ void SplitBuffer::free() {
|
||||
this->total_length_ = 0;
|
||||
}
|
||||
|
||||
uint8_t &SplitBuffer::operator[](size_t index) {
|
||||
const uint8_t &SplitBuffer::operator[](size_t index) const {
|
||||
if (index >= this->total_length_) {
|
||||
ESP_LOGE(TAG, "Out of bounds - %zu >= %zu", index, this->total_length_);
|
||||
// Return reference to a static dummy byte to avoid crash
|
||||
// Return reference to a static dummy byte since we can't throw exceptions.
|
||||
// the byte is non-const since it will also be used by the non-const [] overload.
|
||||
static uint8_t dummy = 0;
|
||||
return dummy;
|
||||
}
|
||||
|
||||
size_t buffer_index = index / this->buffer_size_;
|
||||
size_t offset_in_buffer = index - this->buffer_size_ * buffer_index;
|
||||
const auto buffer_index = index / this->buffer_size_;
|
||||
const auto offset_in_buffer = index % this->buffer_size_;
|
||||
|
||||
return this->buffers_[buffer_index][offset_in_buffer];
|
||||
}
|
||||
|
||||
const uint8_t &SplitBuffer::operator[](size_t index) const {
|
||||
if (index >= this->total_length_) {
|
||||
ESP_LOGE(TAG, "Out of bounds - %zu >= %zu", index, this->total_length_);
|
||||
// Return reference to a static dummy byte to avoid crash
|
||||
static const uint8_t DUMMY = 0;
|
||||
return DUMMY;
|
||||
// non-const version of operator[] for write access
|
||||
uint8_t &SplitBuffer::operator[](size_t index) {
|
||||
// avoid code duplication. These casts are safe since we know the object is not const.
|
||||
return const_cast<uint8_t &>(static_cast<const SplitBuffer *>(this)->operator[](index));
|
||||
}
|
||||
|
||||
/**
|
||||
* Fill the entire buffer with a single byte value
|
||||
* @param value Fill value
|
||||
*/
|
||||
void SplitBuffer::fill(uint8_t value) const {
|
||||
if (this->buffer_count_ == 0)
|
||||
return;
|
||||
// clear all the full sized buffers
|
||||
size_t i = 0;
|
||||
for (; i != this->buffer_count_ - 1; i++) {
|
||||
memset(this->buffers_[i], value, this->buffer_size_);
|
||||
}
|
||||
|
||||
size_t buffer_index = index / this->buffer_size_;
|
||||
size_t offset_in_buffer = index - this->buffer_size_ * buffer_index;
|
||||
|
||||
return this->buffers_[buffer_index][offset_in_buffer];
|
||||
// clear the last, potentially short, buffer.
|
||||
// `i` is guaranteed to equal the last index since the loop terminates at that value.
|
||||
// where all buffers are the same size, the modulus must return the size, not 0.
|
||||
auto size_last = ((this->total_length_ - 1) % this->buffer_size_) + 1;
|
||||
memset(this->buffers_[i], value, size_last);
|
||||
}
|
||||
|
||||
} // namespace esphome::split_buffer
|
||||
|
||||
@@ -4,7 +4,13 @@
|
||||
#include <cstdlib>
|
||||
|
||||
namespace esphome::split_buffer {
|
||||
|
||||
/**
|
||||
* A SplitBuffer allocates a large memory buffer potentially as multiple smaller buffers
|
||||
* to facilitate allocation of large buffers on devices with fragmented memory spaces.
|
||||
* Each sub-buffer is the same size, except for the last one which may be smaller.
|
||||
* Standard array indexing using `[]` is possible on the buffer, but, since the buffer may not be contiguous in memory,
|
||||
* there is no easy way to access the buffer as a single array, i.e. no `.data()` access like a vector.
|
||||
*/
|
||||
class SplitBuffer {
|
||||
public:
|
||||
SplitBuffer() = default;
|
||||
@@ -19,13 +25,13 @@ class SplitBuffer {
|
||||
// Access operators
|
||||
uint8_t &operator[](size_t index);
|
||||
const uint8_t &operator[](size_t index) const;
|
||||
void fill(uint8_t value) const;
|
||||
|
||||
// Get the total length
|
||||
size_t size() const { return this->total_length_; }
|
||||
|
||||
// Get buffer information
|
||||
size_t get_buffer_count() const { return this->buffer_count_; }
|
||||
size_t get_buffer_size() const { return this->buffer_size_; }
|
||||
|
||||
// Check if successfully initialized
|
||||
bool is_valid() const { return this->buffers_ != nullptr && this->buffer_count_ > 0; }
|
||||
|
||||
@@ -24,7 +24,7 @@ void TemplateSelect::setup() {
|
||||
ESP_LOGD(TAG, "State from initial: %s", this->option_at(index));
|
||||
}
|
||||
|
||||
this->publish_state(index);
|
||||
this->publish_state(this->at(index).value());
|
||||
}
|
||||
|
||||
void TemplateSelect::update() {
|
||||
@@ -41,14 +41,16 @@ void TemplateSelect::update() {
|
||||
}
|
||||
}
|
||||
|
||||
void TemplateSelect::control(size_t index) {
|
||||
this->set_trigger_->trigger(std::string(this->option_at(index)));
|
||||
void TemplateSelect::control(const std::string &value) {
|
||||
this->set_trigger_->trigger(value);
|
||||
|
||||
if (this->optimistic_)
|
||||
this->publish_state(index);
|
||||
this->publish_state(value);
|
||||
|
||||
if (this->restore_value_)
|
||||
this->pref_.save(&index);
|
||||
if (this->restore_value_) {
|
||||
auto index = this->index_of(value);
|
||||
this->pref_.save(&index.value());
|
||||
}
|
||||
}
|
||||
|
||||
void TemplateSelect::dump_config() {
|
||||
|
||||
@@ -24,7 +24,7 @@ class TemplateSelect : public select::Select, public PollingComponent {
|
||||
void set_restore_value(bool restore_value) { this->restore_value_ = restore_value; }
|
||||
|
||||
protected:
|
||||
void control(size_t index) override;
|
||||
void control(const std::string &value) override;
|
||||
bool optimistic_ = false;
|
||||
size_t initial_option_index_{0};
|
||||
bool restore_value_ = false;
|
||||
|
||||
@@ -17,7 +17,8 @@ void TuyaSelect::setup() {
|
||||
return;
|
||||
}
|
||||
size_t mapping_idx = std::distance(mappings.cbegin(), it);
|
||||
this->publish_state(mapping_idx);
|
||||
auto value = this->at(mapping_idx);
|
||||
this->publish_state(value.value());
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import socket
|
||||
from esphome.components.esp32 import (
|
||||
VARIANT_ESP32P4,
|
||||
VARIANT_ESP32S2,
|
||||
@@ -11,7 +12,7 @@ from esphome.const import CONF_DEVICES, CONF_ID
|
||||
from esphome.cpp_types import Component
|
||||
from esphome.types import ConfigType
|
||||
|
||||
AUTO_LOAD = ["bytebuffer"]
|
||||
AUTO_LOAD = ["bytebuffer", "socket"]
|
||||
CODEOWNERS = ["@clydebarrow"]
|
||||
DEPENDENCIES = ["esp32"]
|
||||
usb_host_ns = cg.esphome_ns.namespace("usb_host")
|
||||
@@ -71,6 +72,11 @@ async def to_code(config: ConfigType) -> None:
|
||||
max_requests = config[CONF_MAX_TRANSFER_REQUESTS]
|
||||
cg.add_define("USB_HOST_MAX_REQUESTS", max_requests)
|
||||
|
||||
# USB uses the socket wake_loop_threadsafe() mechanism to wake the main loop from USB task
|
||||
# This enables low-latency (~12μs) USB event processing instead of waiting for
|
||||
# select() timeout (0-16ms). The wake socket is shared across all components.
|
||||
socket.require_wake_loop_threadsafe()
|
||||
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
for device in config.get(CONF_DEVICES) or ():
|
||||
|
||||
@@ -55,7 +55,7 @@ static const uint8_t USB_DIR_IN = 1 << 7;
|
||||
static const uint8_t USB_DIR_OUT = 0;
|
||||
static const size_t SETUP_PACKET_SIZE = 8;
|
||||
|
||||
static const size_t MAX_REQUESTS = USB_HOST_MAX_REQUESTS; // maximum number of outstanding requests possible.
|
||||
static constexpr size_t MAX_REQUESTS = USB_HOST_MAX_REQUESTS; // maximum number of outstanding requests possible.
|
||||
static_assert(MAX_REQUESTS >= 1 && MAX_REQUESTS <= 32, "MAX_REQUESTS must be between 1 and 32");
|
||||
|
||||
// Select appropriate bitmask type for tracking allocation of TransferRequest slots.
|
||||
@@ -65,6 +65,7 @@ static_assert(MAX_REQUESTS >= 1 && MAX_REQUESTS <= 32, "MAX_REQUESTS must be bet
|
||||
// This is tied to the static_assert above, which enforces MAX_REQUESTS is between 1 and 32.
|
||||
// If MAX_REQUESTS is increased above 32, this logic and the static_assert must be updated.
|
||||
using trq_bitmask_t = std::conditional<(MAX_REQUESTS <= 16), uint16_t, uint32_t>::type;
|
||||
static constexpr trq_bitmask_t ALL_REQUESTS_IN_USE = MAX_REQUESTS == 32 ? ~0 : (1 << MAX_REQUESTS) - 1;
|
||||
|
||||
static constexpr size_t USB_EVENT_QUEUE_SIZE = 32; // Size of event queue between USB task and main loop
|
||||
static constexpr size_t USB_TASK_STACK_SIZE = 4096; // Stack size for USB task (same as ESP-IDF USB examples)
|
||||
@@ -133,11 +134,11 @@ class USBClient : public Component {
|
||||
float get_setup_priority() const override { return setup_priority::IO; }
|
||||
void on_opened(uint8_t addr);
|
||||
void on_removed(usb_device_handle_t handle);
|
||||
void control_transfer_callback(const usb_transfer_t *xfer) const;
|
||||
void transfer_in(uint8_t ep_address, const transfer_cb_t &callback, uint16_t length);
|
||||
void transfer_out(uint8_t ep_address, const transfer_cb_t &callback, const uint8_t *data, uint16_t length);
|
||||
bool transfer_in(uint8_t ep_address, const transfer_cb_t &callback, uint16_t length);
|
||||
bool transfer_out(uint8_t ep_address, const transfer_cb_t &callback, const uint8_t *data, uint16_t length);
|
||||
void dump_config() override;
|
||||
void release_trq(TransferRequest *trq);
|
||||
trq_bitmask_t get_trq_in_use() const { return trq_in_use_; }
|
||||
bool control_transfer(uint8_t type, uint8_t request, uint16_t value, uint16_t index, const transfer_cb_t &callback,
|
||||
const std::vector<uint8_t> &data = {});
|
||||
|
||||
@@ -147,7 +148,6 @@ class USBClient : public Component {
|
||||
EventPool<UsbEvent, USB_EVENT_QUEUE_SIZE> event_pool;
|
||||
|
||||
protected:
|
||||
bool register_();
|
||||
TransferRequest *get_trq_(); // Lock-free allocation using atomic bitmask (multi-consumer safe)
|
||||
virtual void disconnect();
|
||||
virtual void on_connected() {}
|
||||
@@ -158,7 +158,7 @@ class USBClient : public Component {
|
||||
|
||||
// USB task management
|
||||
static void usb_task_fn(void *arg);
|
||||
void usb_task_loop();
|
||||
[[noreturn]] void usb_task_loop() const;
|
||||
|
||||
TaskHandle_t usb_task_handle_{nullptr};
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "usb_host.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "esphome/components/bytebuffer/bytebuffer.h"
|
||||
|
||||
#include <cinttypes>
|
||||
@@ -174,6 +175,11 @@ static void client_event_cb(const usb_host_client_event_msg_t *event_msg, void *
|
||||
|
||||
// Push to lock-free queue (always succeeds since pool size == queue size)
|
||||
client->event_queue.push(event);
|
||||
|
||||
// Wake main loop immediately to process USB event instead of waiting for select() timeout
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
|
||||
App.wake_loop_threadsafe();
|
||||
#endif
|
||||
}
|
||||
void USBClient::setup() {
|
||||
usb_host_client_config_t config{.is_synchronous = false,
|
||||
@@ -188,9 +194,9 @@ void USBClient::setup() {
|
||||
}
|
||||
// Pre-allocate USB transfer buffers for all slots at startup
|
||||
// This avoids any dynamic allocation during runtime
|
||||
for (size_t i = 0; i < MAX_REQUESTS; i++) {
|
||||
usb_host_transfer_alloc(64, 0, &this->requests_[i].transfer);
|
||||
this->requests_[i].client = this; // Set once, never changes
|
||||
for (auto &request : this->requests_) {
|
||||
usb_host_transfer_alloc(64, 0, &request.transfer);
|
||||
request.client = this; // Set once, never changes
|
||||
}
|
||||
|
||||
// Create and start USB task
|
||||
@@ -210,8 +216,7 @@ void USBClient::usb_task_fn(void *arg) {
|
||||
auto *client = static_cast<USBClient *>(arg);
|
||||
client->usb_task_loop();
|
||||
}
|
||||
|
||||
void USBClient::usb_task_loop() {
|
||||
void USBClient::usb_task_loop() const {
|
||||
while (true) {
|
||||
usb_host_client_handle_events(this->handle_, portMAX_DELAY);
|
||||
}
|
||||
@@ -334,22 +339,23 @@ static void control_callback(const usb_transfer_t *xfer) {
|
||||
// This multi-threaded access is intentional for performance - USB task can
|
||||
// immediately restart transfers without waiting for main loop scheduling.
|
||||
TransferRequest *USBClient::get_trq_() {
|
||||
trq_bitmask_t mask = this->trq_in_use_.load(std::memory_order_relaxed);
|
||||
trq_bitmask_t mask = this->trq_in_use_.load(std::memory_order_acquire);
|
||||
|
||||
// Find first available slot (bit = 0) and try to claim it atomically
|
||||
// We use a while loop to allow retrying the same slot after CAS failure
|
||||
size_t i = 0;
|
||||
while (i != MAX_REQUESTS) {
|
||||
if (mask & (static_cast<trq_bitmask_t>(1) << i)) {
|
||||
// Slot is in use, move to next slot
|
||||
i++;
|
||||
continue;
|
||||
for (;;) {
|
||||
if (mask == ALL_REQUESTS_IN_USE) {
|
||||
ESP_LOGE(TAG, "All %zu transfer slots in use", MAX_REQUESTS);
|
||||
return nullptr;
|
||||
}
|
||||
// find the least significant zero bit
|
||||
trq_bitmask_t lsb = ~mask & (mask + 1);
|
||||
|
||||
// Slot i appears available, try to claim it atomically
|
||||
trq_bitmask_t desired = mask | (static_cast<trq_bitmask_t>(1) << i); // Set bit i to mark as in-use
|
||||
trq_bitmask_t desired = mask | lsb;
|
||||
|
||||
if (this->trq_in_use_.compare_exchange_weak(mask, desired, std::memory_order_acquire, std::memory_order_relaxed)) {
|
||||
if (this->trq_in_use_.compare_exchange_weak(mask, desired, std::memory_order::acquire)) {
|
||||
auto i = __builtin_ctz(lsb); // count trailing zeroes
|
||||
// Successfully claimed slot i - prepare the TransferRequest
|
||||
auto *trq = &this->requests_[i];
|
||||
trq->transfer->context = trq;
|
||||
@@ -358,13 +364,9 @@ TransferRequest *USBClient::get_trq_() {
|
||||
}
|
||||
// CAS failed - another thread modified the bitmask
|
||||
// mask was already updated by compare_exchange_weak with the current value
|
||||
// No need to reload - the CAS already did that for us
|
||||
i = 0;
|
||||
}
|
||||
|
||||
ESP_LOGE(TAG, "All %zu transfer slots in use", MAX_REQUESTS);
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void USBClient::disconnect() {
|
||||
this->on_disconnected();
|
||||
auto err = usb_host_device_close(this->handle_, this->device_handle_);
|
||||
@@ -446,11 +448,11 @@ static void transfer_callback(usb_transfer_t *xfer) {
|
||||
*
|
||||
* @throws None.
|
||||
*/
|
||||
void USBClient::transfer_in(uint8_t ep_address, const transfer_cb_t &callback, uint16_t length) {
|
||||
bool USBClient::transfer_in(uint8_t ep_address, const transfer_cb_t &callback, uint16_t length) {
|
||||
auto *trq = this->get_trq_();
|
||||
if (trq == nullptr) {
|
||||
ESP_LOGE(TAG, "Too many requests queued");
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
trq->callback = callback;
|
||||
trq->transfer->callback = transfer_callback;
|
||||
@@ -460,7 +462,9 @@ void USBClient::transfer_in(uint8_t ep_address, const transfer_cb_t &callback, u
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to submit transfer, address=%x, length=%d, err=%x", ep_address, length, err);
|
||||
this->release_trq(trq);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -476,11 +480,11 @@ void USBClient::transfer_in(uint8_t ep_address, const transfer_cb_t &callback, u
|
||||
*
|
||||
* @throws None.
|
||||
*/
|
||||
void USBClient::transfer_out(uint8_t ep_address, const transfer_cb_t &callback, const uint8_t *data, uint16_t length) {
|
||||
bool USBClient::transfer_out(uint8_t ep_address, const transfer_cb_t &callback, const uint8_t *data, uint16_t length) {
|
||||
auto *trq = this->get_trq_();
|
||||
if (trq == nullptr) {
|
||||
ESP_LOGE(TAG, "Too many requests queued");
|
||||
return;
|
||||
return false;
|
||||
}
|
||||
trq->callback = callback;
|
||||
trq->transfer->callback = transfer_callback;
|
||||
@@ -491,7 +495,9 @@ void USBClient::transfer_out(uint8_t ep_address, const transfer_cb_t &callback,
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to submit transfer, address=%x, length=%d, err=%x", ep_address, length, err);
|
||||
this->release_trq(trq);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
void USBClient::dump_config() {
|
||||
ESP_LOGCONFIG(TAG,
|
||||
@@ -505,7 +511,7 @@ void USBClient::dump_config() {
|
||||
// - Main loop: When transfer submission fails
|
||||
//
|
||||
// THREAD SAFETY: Lock-free using atomic AND to clear bit
|
||||
// Thread-safe atomic operation allows multi-threaded deallocation
|
||||
// Thread-safe atomic operation allows multithreaded deallocation
|
||||
void USBClient::release_trq(TransferRequest *trq) {
|
||||
if (trq == nullptr)
|
||||
return;
|
||||
@@ -517,10 +523,10 @@ void USBClient::release_trq(TransferRequest *trq) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Atomically clear bit i to mark slot as available
|
||||
// Atomically clear the bit to mark slot as available
|
||||
// fetch_and with inverted bitmask clears the bit atomically
|
||||
trq_bitmask_t bit = static_cast<trq_bitmask_t>(1) << index;
|
||||
this->trq_in_use_.fetch_and(static_cast<trq_bitmask_t>(~bit), std::memory_order_release);
|
||||
trq_bitmask_t mask = ~(static_cast<trq_bitmask_t>(1) << index);
|
||||
this->trq_in_use_.fetch_and(mask, std::memory_order_release);
|
||||
}
|
||||
|
||||
} // namespace usb_host
|
||||
|
||||
@@ -214,7 +214,7 @@ void USBUartComponent::dump_config() {
|
||||
}
|
||||
}
|
||||
void USBUartComponent::start_input(USBUartChannel *channel) {
|
||||
if (!channel->initialised_.load() || channel->input_started_.load())
|
||||
if (!channel->initialised_.load())
|
||||
return;
|
||||
// THREAD CONTEXT: Called from both USB task and main loop threads
|
||||
// - USB task: Immediate restart after successful transfer for continuous data flow
|
||||
@@ -226,12 +226,18 @@ void USBUartComponent::start_input(USBUartChannel *channel) {
|
||||
//
|
||||
// The underlying transfer_in() uses lock-free atomic allocation from the
|
||||
// TransferRequest pool, making this multi-threaded access safe
|
||||
|
||||
// if already started, don't restart. A spurious failure in compare_exchange_weak
|
||||
// is not a problem, as it will be retried on the next read_array()
|
||||
auto started = false;
|
||||
if (!channel->input_started_.compare_exchange_weak(started, true))
|
||||
return;
|
||||
const auto *ep = channel->cdc_dev_.in_ep;
|
||||
// CALLBACK CONTEXT: This lambda is executed in USB task via transfer_callback
|
||||
auto callback = [this, channel](const usb_host::TransferStatus &status) {
|
||||
ESP_LOGV(TAG, "Transfer result: length: %u; status %X", status.data_len, status.error_code);
|
||||
if (!status.success) {
|
||||
ESP_LOGE(TAG, "Control transfer failed, status=%s", esp_err_to_name(status.error_code));
|
||||
ESP_LOGE(TAG, "Input transfer failed, status=%s", esp_err_to_name(status.error_code));
|
||||
// On failure, don't restart - let next read_array() trigger it
|
||||
channel->input_started_.store(false);
|
||||
return;
|
||||
@@ -263,8 +269,9 @@ void USBUartComponent::start_input(USBUartChannel *channel) {
|
||||
channel->input_started_.store(false);
|
||||
this->start_input(channel);
|
||||
};
|
||||
channel->input_started_.store(true);
|
||||
this->transfer_in(ep->bEndpointAddress, callback, ep->wMaxPacketSize);
|
||||
if (!this->transfer_in(ep->bEndpointAddress, callback, ep->wMaxPacketSize)) {
|
||||
channel->input_started_.store(false);
|
||||
}
|
||||
}
|
||||
|
||||
void USBUartComponent::start_output(USBUartChannel *channel) {
|
||||
@@ -357,11 +364,12 @@ void USBUartTypeCdcAcm::on_disconnected() {
|
||||
usb_host_endpoint_flush(this->device_handle_, channel->cdc_dev_.notify_ep->bEndpointAddress);
|
||||
}
|
||||
usb_host_interface_release(this->handle_, this->device_handle_, channel->cdc_dev_.bulk_interface_number);
|
||||
channel->initialised_.store(false);
|
||||
channel->input_started_.store(false);
|
||||
channel->output_started_.store(false);
|
||||
// Reset the input and output started flags to their initial state to avoid the possibility of spurious restarts
|
||||
channel->input_started_.store(true);
|
||||
channel->output_started_.store(true);
|
||||
channel->input_buffer_.clear();
|
||||
channel->output_buffer_.clear();
|
||||
channel->initialised_.store(false);
|
||||
}
|
||||
USBClient::on_disconnected();
|
||||
}
|
||||
|
||||
@@ -1188,7 +1188,7 @@ void WebServer::handle_select_request(AsyncWebServerRequest *request, const UrlM
|
||||
|
||||
if (request->method() == HTTP_GET && match.method_empty()) {
|
||||
auto detail = get_request_detail(request);
|
||||
std::string data = this->select_json(obj, obj->has_state() ? obj->current_option() : "", detail);
|
||||
std::string data = this->select_json(obj, obj->state, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
return;
|
||||
}
|
||||
@@ -1208,14 +1208,12 @@ void WebServer::handle_select_request(AsyncWebServerRequest *request, const UrlM
|
||||
request->send(404);
|
||||
}
|
||||
std::string WebServer::select_state_json_generator(WebServer *web_server, void *source) {
|
||||
auto *obj = (select::Select *) (source);
|
||||
return web_server->select_json(obj, obj->has_state() ? obj->current_option() : "", DETAIL_STATE);
|
||||
return web_server->select_json((select::Select *) (source), ((select::Select *) (source))->state, DETAIL_STATE);
|
||||
}
|
||||
std::string WebServer::select_all_json_generator(WebServer *web_server, void *source) {
|
||||
auto *obj = (select::Select *) (source);
|
||||
return web_server->select_json(obj, obj->has_state() ? obj->current_option() : "", DETAIL_ALL);
|
||||
return web_server->select_json((select::Select *) (source), ((select::Select *) (source))->state, DETAIL_ALL);
|
||||
}
|
||||
std::string WebServer::select_json(select::Select *obj, const char *value, JsonDetail start_config) {
|
||||
std::string WebServer::select_json(select::Select *obj, const std::string &value, JsonDetail start_config) {
|
||||
json::JsonBuilder builder;
|
||||
JsonObject root = builder.root();
|
||||
|
||||
|
||||
@@ -410,7 +410,7 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
|
||||
static std::string select_state_json_generator(WebServer *web_server, void *source);
|
||||
static std::string select_all_json_generator(WebServer *web_server, void *source);
|
||||
/// Dump the select state with its value as a JSON string.
|
||||
std::string select_json(select::Select *obj, const char *value, JsonDetail start_config);
|
||||
std::string select_json(select::Select *obj, const std::string &value, JsonDetail start_config);
|
||||
#endif
|
||||
|
||||
#ifdef USE_CLIMATE
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from pathlib import Path
|
||||
import textwrap
|
||||
from typing import TypedDict
|
||||
|
||||
import esphome.codegen as cg
|
||||
@@ -90,7 +91,7 @@ def zephyr_add_prj_conf(
|
||||
|
||||
|
||||
def zephyr_add_overlay(content):
|
||||
zephyr_data()[KEY_OVERLAY] += content
|
||||
zephyr_data()[KEY_OVERLAY] += textwrap.dedent(content)
|
||||
|
||||
|
||||
def add_extra_build_file(filename: str, path: Path) -> bool:
|
||||
|
||||
@@ -122,6 +122,11 @@ void Application::setup() {
|
||||
// Clear setup priority overrides to free memory
|
||||
clear_setup_priority_overrides();
|
||||
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
|
||||
// Set up wake socket for waking main loop from tasks
|
||||
this->setup_wake_loop_threadsafe_();
|
||||
#endif
|
||||
|
||||
this->schedule_dump_config();
|
||||
}
|
||||
void Application::loop() {
|
||||
@@ -472,6 +477,11 @@ void Application::enable_pending_loops_() {
|
||||
}
|
||||
|
||||
void Application::before_loop_tasks_(uint32_t loop_start_time) {
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
|
||||
// Drain wake notifications first to clear socket for next wake
|
||||
this->drain_wake_notifications_();
|
||||
#endif
|
||||
|
||||
// Process scheduled tasks
|
||||
this->scheduler.call(loop_start_time);
|
||||
|
||||
@@ -625,4 +635,73 @@ void Application::yield_with_select_(uint32_t delay_ms) {
|
||||
|
||||
Application App; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
|
||||
void Application::setup_wake_loop_threadsafe_() {
|
||||
// Create UDP socket for wake notifications
|
||||
this->wake_socket_fd_ = lwip_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
||||
if (this->wake_socket_fd_ < 0) {
|
||||
ESP_LOGW(TAG, "Wake socket create failed: %d", errno);
|
||||
return;
|
||||
}
|
||||
|
||||
// Bind to loopback with auto-assigned port
|
||||
struct sockaddr_in addr = {};
|
||||
addr.sin_family = AF_INET;
|
||||
addr.sin_addr.s_addr = lwip_htonl(INADDR_LOOPBACK);
|
||||
addr.sin_port = 0; // Auto-assign port
|
||||
|
||||
if (lwip_bind(this->wake_socket_fd_, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
|
||||
ESP_LOGW(TAG, "Wake socket bind failed: %d", errno);
|
||||
lwip_close(this->wake_socket_fd_);
|
||||
this->wake_socket_fd_ = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the assigned address and connect to it
|
||||
// Connecting a UDP socket allows using send() instead of sendto() for better performance
|
||||
struct sockaddr_in wake_addr;
|
||||
socklen_t len = sizeof(wake_addr);
|
||||
if (lwip_getsockname(this->wake_socket_fd_, (struct sockaddr *) &wake_addr, &len) < 0) {
|
||||
ESP_LOGW(TAG, "Wake socket address failed: %d", errno);
|
||||
lwip_close(this->wake_socket_fd_);
|
||||
this->wake_socket_fd_ = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
// Connect to self (loopback) - allows using send() instead of sendto()
|
||||
// After connect(), no need to store wake_addr - the socket remembers it
|
||||
if (lwip_connect(this->wake_socket_fd_, (struct sockaddr *) &wake_addr, sizeof(wake_addr)) < 0) {
|
||||
ESP_LOGW(TAG, "Wake socket connect failed: %d", errno);
|
||||
lwip_close(this->wake_socket_fd_);
|
||||
this->wake_socket_fd_ = -1;
|
||||
return;
|
||||
}
|
||||
|
||||
// Set non-blocking mode
|
||||
int flags = lwip_fcntl(this->wake_socket_fd_, F_GETFL, 0);
|
||||
lwip_fcntl(this->wake_socket_fd_, F_SETFL, flags | O_NONBLOCK);
|
||||
|
||||
// Register with application's select() loop
|
||||
if (!this->register_socket_fd(this->wake_socket_fd_)) {
|
||||
ESP_LOGW(TAG, "Wake socket register failed");
|
||||
lwip_close(this->wake_socket_fd_);
|
||||
this->wake_socket_fd_ = -1;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
void Application::wake_loop_threadsafe() {
|
||||
// Called from FreeRTOS task context when events need immediate processing
|
||||
// Wakes up lwip_select() in main loop by writing to connected loopback socket
|
||||
if (this->wake_socket_fd_ >= 0) {
|
||||
const char dummy = 1;
|
||||
// Non-blocking send - if it fails (unlikely), select() will wake on timeout anyway
|
||||
// No error checking needed: we control both ends of this loopback socket.
|
||||
// This is safe to call from FreeRTOS tasks - send() is thread-safe in lwip
|
||||
// Socket is already connected to loopback address, so send() is faster than sendto()
|
||||
lwip_send(this->wake_socket_fd_, &dummy, 1, 0);
|
||||
}
|
||||
}
|
||||
#endif // defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
|
||||
|
||||
} // namespace esphome
|
||||
|
||||
@@ -21,7 +21,10 @@
|
||||
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
#include <sys/select.h>
|
||||
#ifdef USE_WAKE_LOOP_THREADSAFE
|
||||
#include <lwip/sockets.h>
|
||||
#endif
|
||||
#endif // USE_SOCKET_SELECT_SUPPORT
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
@@ -429,6 +432,13 @@ class Application {
|
||||
/// Check if there's data available on a socket without blocking
|
||||
/// This function is thread-safe for reading, but should be called after select() has run
|
||||
bool is_socket_ready(int fd) const;
|
||||
|
||||
#ifdef USE_WAKE_LOOP_THREADSAFE
|
||||
/// Wake the main event loop from a FreeRTOS task
|
||||
/// Thread-safe, can be called from task context to immediately wake select()
|
||||
/// IMPORTANT: NOT safe to call from ISR context (socket operations not ISR-safe)
|
||||
void wake_loop_threadsafe();
|
||||
#endif
|
||||
#endif
|
||||
|
||||
protected:
|
||||
@@ -454,6 +464,11 @@ class Application {
|
||||
/// Perform a delay while also monitoring socket file descriptors for readiness
|
||||
void yield_with_select_(uint32_t delay_ms);
|
||||
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
|
||||
void setup_wake_loop_threadsafe_(); // Create wake notification socket
|
||||
inline void drain_wake_notifications_(); // Read pending wake notifications in main loop (hot path - inlined)
|
||||
#endif
|
||||
|
||||
// === Member variables ordered by size to minimize padding ===
|
||||
|
||||
// Pointer-sized members first
|
||||
@@ -481,6 +496,9 @@ class Application {
|
||||
FixedVector<Component *> looping_components_{};
|
||||
#ifdef USE_SOCKET_SELECT_SUPPORT
|
||||
std::vector<int> socket_fds_; // Vector of all monitored socket file descriptors
|
||||
#ifdef USE_WAKE_LOOP_THREADSAFE
|
||||
int wake_socket_fd_{-1}; // Shared wake notification socket for waking main loop from tasks
|
||||
#endif
|
||||
#endif
|
||||
|
||||
// std::string members (typically 24-32 bytes each)
|
||||
@@ -597,4 +615,28 @@ class Application {
|
||||
/// Global storage of Application pointer - only one Application can exist.
|
||||
extern Application App; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
#if defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
|
||||
// Inline implementations for hot-path functions
|
||||
// drain_wake_notifications_() is called on every loop iteration
|
||||
|
||||
// Small buffer for draining wake notification bytes (1 byte sent per wake)
|
||||
// Size allows draining multiple notifications per recvfrom() without wasting stack
|
||||
static constexpr size_t WAKE_NOTIFY_DRAIN_BUFFER_SIZE = 16;
|
||||
|
||||
inline void Application::drain_wake_notifications_() {
|
||||
// Called from main loop to drain any pending wake notifications
|
||||
// Must check is_socket_ready() to avoid blocking on empty socket
|
||||
if (this->wake_socket_fd_ >= 0 && this->is_socket_ready(this->wake_socket_fd_)) {
|
||||
char buffer[WAKE_NOTIFY_DRAIN_BUFFER_SIZE];
|
||||
// Drain all pending notifications with non-blocking reads
|
||||
// Multiple wake events may have triggered multiple writes, so drain until EWOULDBLOCK
|
||||
// We control both ends of this loopback socket (always write 1 byte per wake),
|
||||
// so no error checking needed - any errors indicate catastrophic system failure
|
||||
while (lwip_recvfrom(this->wake_socket_fd_, buffer, sizeof(buffer), 0, nullptr, nullptr) > 0) {
|
||||
// Just draining, no action needed - wake has already occurred
|
||||
}
|
||||
}
|
||||
}
|
||||
#endif // defined(USE_SOCKET_SELECT_SUPPORT) && defined(USE_WAKE_LOOP_THREADSAFE)
|
||||
|
||||
} // namespace esphome
|
||||
|
||||
@@ -220,6 +220,7 @@ template<typename... Ts> class Action {
|
||||
|
||||
protected:
|
||||
friend ActionList<Ts...>;
|
||||
template<typename... Us> friend class ContinuationAction;
|
||||
|
||||
virtual void play(Ts... x) = 0;
|
||||
void play_next_(Ts... x) {
|
||||
|
||||
@@ -216,18 +216,46 @@ template<typename... Ts> class StatelessLambdaAction : public Action<Ts...> {
|
||||
void (*f_)(Ts...);
|
||||
};
|
||||
|
||||
/// Simple continuation action that calls play_next_ on a parent action.
|
||||
/// Used internally by IfAction, WhileAction, RepeatAction, etc. to chain actions.
|
||||
/// Memory: 4-8 bytes (parent pointer) vs 40 bytes (LambdaAction with std::function).
|
||||
template<typename... Ts> class ContinuationAction : public Action<Ts...> {
|
||||
public:
|
||||
explicit ContinuationAction(Action<Ts...> *parent) : parent_(parent) {}
|
||||
|
||||
void play(Ts... x) override { this->parent_->play_next_(x...); }
|
||||
|
||||
protected:
|
||||
Action<Ts...> *parent_;
|
||||
};
|
||||
|
||||
// Forward declaration for WhileLoopContinuation
|
||||
template<typename... Ts> class WhileAction;
|
||||
|
||||
/// Loop continuation for WhileAction that checks condition and repeats or continues.
|
||||
/// Memory: 4-8 bytes (parent pointer) vs 40 bytes (LambdaAction with std::function).
|
||||
template<typename... Ts> class WhileLoopContinuation : public Action<Ts...> {
|
||||
public:
|
||||
explicit WhileLoopContinuation(WhileAction<Ts...> *parent) : parent_(parent) {}
|
||||
|
||||
void play(Ts... x) override;
|
||||
|
||||
protected:
|
||||
WhileAction<Ts...> *parent_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class IfAction : public Action<Ts...> {
|
||||
public:
|
||||
explicit IfAction(Condition<Ts...> *condition) : condition_(condition) {}
|
||||
|
||||
void add_then(const std::initializer_list<Action<Ts...> *> &actions) {
|
||||
this->then_.add_actions(actions);
|
||||
this->then_.add_action(new LambdaAction<Ts...>([this](Ts... x) { this->play_next_(x...); }));
|
||||
this->then_.add_action(new ContinuationAction<Ts...>(this));
|
||||
}
|
||||
|
||||
void add_else(const std::initializer_list<Action<Ts...> *> &actions) {
|
||||
this->else_.add_actions(actions);
|
||||
this->else_.add_action(new LambdaAction<Ts...>([this](Ts... x) { this->play_next_(x...); }));
|
||||
this->else_.add_action(new ContinuationAction<Ts...>(this));
|
||||
}
|
||||
|
||||
void play_complex(Ts... x) override {
|
||||
@@ -268,17 +296,11 @@ template<typename... Ts> class WhileAction : public Action<Ts...> {
|
||||
|
||||
void add_then(const std::initializer_list<Action<Ts...> *> &actions) {
|
||||
this->then_.add_actions(actions);
|
||||
this->then_.add_action(new LambdaAction<Ts...>([this](Ts... x) {
|
||||
if (this->num_running_ > 0 && this->condition_->check(x...)) {
|
||||
// play again
|
||||
this->then_.play(x...);
|
||||
} else {
|
||||
// condition false, play next
|
||||
this->play_next_(x...);
|
||||
}
|
||||
}));
|
||||
this->then_.add_action(new WhileLoopContinuation<Ts...>(this));
|
||||
}
|
||||
|
||||
friend class WhileLoopContinuation<Ts...>;
|
||||
|
||||
void play_complex(Ts... x) override {
|
||||
this->num_running_++;
|
||||
// Initial condition check
|
||||
@@ -304,22 +326,43 @@ template<typename... Ts> class WhileAction : public Action<Ts...> {
|
||||
ActionList<Ts...> then_;
|
||||
};
|
||||
|
||||
// Implementation of WhileLoopContinuation::play
|
||||
template<typename... Ts> void WhileLoopContinuation<Ts...>::play(Ts... x) {
|
||||
if (this->parent_->num_running_ > 0 && this->parent_->condition_->check(x...)) {
|
||||
// play again
|
||||
this->parent_->then_.play(x...);
|
||||
} else {
|
||||
// condition false, play next
|
||||
this->parent_->play_next_(x...);
|
||||
}
|
||||
}
|
||||
|
||||
// Forward declaration for RepeatLoopContinuation
|
||||
template<typename... Ts> class RepeatAction;
|
||||
|
||||
/// Loop continuation for RepeatAction that increments iteration and repeats or continues.
|
||||
/// Memory: 4-8 bytes (parent pointer) vs 40 bytes (LambdaAction with std::function).
|
||||
template<typename... Ts> class RepeatLoopContinuation : public Action<uint32_t, Ts...> {
|
||||
public:
|
||||
explicit RepeatLoopContinuation(RepeatAction<Ts...> *parent) : parent_(parent) {}
|
||||
|
||||
void play(uint32_t iteration, Ts... x) override;
|
||||
|
||||
protected:
|
||||
RepeatAction<Ts...> *parent_;
|
||||
};
|
||||
|
||||
template<typename... Ts> class RepeatAction : public Action<Ts...> {
|
||||
public:
|
||||
TEMPLATABLE_VALUE(uint32_t, count)
|
||||
|
||||
void add_then(const std::initializer_list<Action<uint32_t, Ts...> *> &actions) {
|
||||
this->then_.add_actions(actions);
|
||||
this->then_.add_action(new LambdaAction<uint32_t, Ts...>([this](uint32_t iteration, Ts... x) {
|
||||
iteration++;
|
||||
if (iteration >= this->count_.value(x...)) {
|
||||
this->play_next_(x...);
|
||||
} else {
|
||||
this->then_.play(iteration, x...);
|
||||
}
|
||||
}));
|
||||
this->then_.add_action(new RepeatLoopContinuation<Ts...>(this));
|
||||
}
|
||||
|
||||
friend class RepeatLoopContinuation<Ts...>;
|
||||
|
||||
void play_complex(Ts... x) override {
|
||||
this->num_running_++;
|
||||
if (this->count_.value(x...) > 0) {
|
||||
@@ -338,6 +381,16 @@ template<typename... Ts> class RepeatAction : public Action<Ts...> {
|
||||
ActionList<uint32_t, Ts...> then_;
|
||||
};
|
||||
|
||||
// Implementation of RepeatLoopContinuation::play
|
||||
template<typename... Ts> void RepeatLoopContinuation<Ts...>::play(uint32_t iteration, Ts... x) {
|
||||
iteration++;
|
||||
if (iteration >= this->parent_->count_.value(x...)) {
|
||||
this->parent_->play_next_(x...);
|
||||
} else {
|
||||
this->parent_->then_.play(iteration, x...);
|
||||
}
|
||||
}
|
||||
|
||||
/** Wait until a condition is true to continue execution.
|
||||
*
|
||||
* Uses queue-based storage to safely handle concurrent executions.
|
||||
|
||||
@@ -155,6 +155,7 @@
|
||||
// IDF-specific feature flags
|
||||
#ifdef USE_ESP_IDF
|
||||
#define USE_MQTT_IDF_ENQUEUE
|
||||
#define ESPHOME_LOOP_TASK_STACK_SIZE 8192
|
||||
#endif
|
||||
|
||||
// ESP32-specific feature flags
|
||||
@@ -195,6 +196,7 @@
|
||||
#define USE_PSRAM
|
||||
#define USE_SOCKET_IMPL_BSD_SOCKETS
|
||||
#define USE_SOCKET_SELECT_SUPPORT
|
||||
#define USE_WAKE_LOOP_THREADSAFE
|
||||
#define USE_SPEAKER
|
||||
#define USE_SPI
|
||||
#define USE_VOICE_ASSISTANT
|
||||
|
||||
@@ -643,6 +643,12 @@ std::string get_mac_address_pretty() {
|
||||
return format_mac_address_pretty(mac);
|
||||
}
|
||||
|
||||
void get_mac_address_into_buffer(std::span<char, 13> buf) {
|
||||
uint8_t mac[6];
|
||||
get_mac_address_raw(mac);
|
||||
format_mac_addr_lower_no_sep(mac, buf.data());
|
||||
}
|
||||
|
||||
#ifndef USE_ESP32
|
||||
bool has_custom_mac_address() { return false; }
|
||||
#endif
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include <iterator>
|
||||
#include <limits>
|
||||
#include <memory>
|
||||
#include <span>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
@@ -1027,6 +1028,10 @@ std::string get_mac_address();
|
||||
/// Get the device MAC address as a string, in colon-separated uppercase hex notation.
|
||||
std::string get_mac_address_pretty();
|
||||
|
||||
/// Get the device MAC address into the given buffer, in lowercase hex notation.
|
||||
/// Assumes buffer length is 13 (12 digits for hexadecimal representation followed by null terminator).
|
||||
void get_mac_address_into_buffer(std::span<char, 13> buf);
|
||||
|
||||
#ifdef USE_ESP32
|
||||
/// Set the MAC address to use from the provided byte array (6 bytes).
|
||||
void set_mac_address(uint8_t *mac);
|
||||
|
||||
@@ -6,15 +6,15 @@ dependencies:
|
||||
espressif/mdns:
|
||||
version: 1.8.2
|
||||
espressif/esp_wifi_remote:
|
||||
version: 0.10.2
|
||||
version: 1.1.5
|
||||
rules:
|
||||
- if: "target in [esp32h2, esp32p4]"
|
||||
espressif/eppp_link:
|
||||
version: 0.2.0
|
||||
version: 1.1.3
|
||||
rules:
|
||||
- if: "target in [esp32h2, esp32p4]"
|
||||
espressif/esp_hosted:
|
||||
version: 2.0.11
|
||||
version: 2.6.1
|
||||
rules:
|
||||
- if: "target in [esp32h2, esp32p4]"
|
||||
zorxx/multipart-parser:
|
||||
|
||||
@@ -71,6 +71,7 @@ ignore_types = (
|
||||
".apng",
|
||||
".gif",
|
||||
".webp",
|
||||
".bin",
|
||||
)
|
||||
|
||||
LINT_FILE_CHECKS = []
|
||||
|
||||
4
tests/components/ads1115/test.nrf52-adafruit.yaml
Normal file
4
tests/components/ads1115/test.nrf52-adafruit.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
packages:
|
||||
i2c: !include ../../test_build_components/common/i2c/nrf52.yaml
|
||||
|
||||
<<: !include common.yaml
|
||||
4
tests/components/ads1115/test.nrf52-mcumgr.yaml
Normal file
4
tests/components/ads1115/test.nrf52-mcumgr.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
packages:
|
||||
i2c: !include ../../test_build_components/common/i2c/nrf52.yaml
|
||||
|
||||
<<: !include common.yaml
|
||||
4
tests/components/aht10/test.nrf52-adafruit.yaml
Normal file
4
tests/components/aht10/test.nrf52-adafruit.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
packages:
|
||||
i2c: !include ../../test_build_components/common/i2c/nrf52.yaml
|
||||
|
||||
<<: !include common.yaml
|
||||
@@ -4,7 +4,10 @@ packages:
|
||||
display:
|
||||
- platform: epaper_spi
|
||||
spi_id: spi_bus
|
||||
model: 7.3in-spectra-e6
|
||||
model: spectra-e6
|
||||
dimensions:
|
||||
width: 800
|
||||
height: 480
|
||||
cs_pin: GPIO5
|
||||
dc_pin: GPIO17
|
||||
reset_pin: GPIO16
|
||||
@@ -13,3 +16,6 @@ display:
|
||||
update_interval: 60s
|
||||
lambda: |-
|
||||
it.circle(64, 64, 50, Color::BLACK);
|
||||
|
||||
- platform: epaper_spi
|
||||
model: seeed-reterminal-e1002
|
||||
|
||||
6
tests/components/esp32/test-stack_size.esp32-idf.yaml
Normal file
6
tests/components/esp32/test-stack_size.esp32-idf.yaml
Normal file
@@ -0,0 +1,6 @@
|
||||
esp32:
|
||||
board: esp32dev
|
||||
framework:
|
||||
type: esp-idf
|
||||
advanced:
|
||||
loop_task_stack_size: 16384
|
||||
1
tests/components/esp32_hosted/.gitattributes
vendored
Normal file
1
tests/components/esp32_hosted/.gitattributes
vendored
Normal file
@@ -0,0 +1 @@
|
||||
*.bin -text
|
||||
@@ -1 +1,7 @@
|
||||
<<: !include common.yaml
|
||||
|
||||
update:
|
||||
- platform: esp32_hosted
|
||||
name: "Coprocessor Firmware Update"
|
||||
path: $component_dir/test_firmware.bin
|
||||
sha256: de2f256064a0af797747c2b97505dc0b9f3df0de4f489eac731c23ae9ca9cc31
|
||||
|
||||
BIN
tests/components/esp32_hosted/test_firmware.bin
Normal file
BIN
tests/components/esp32_hosted/test_firmware.bin
Normal file
Binary file not shown.
4
tests/components/i2c/test.nrf52-adafruit.yaml
Normal file
4
tests/components/i2c/test.nrf52-adafruit.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
packages:
|
||||
i2c: !include ../../test_build_components/common/i2c/nrf52.yaml
|
||||
|
||||
<<: !include common.yaml
|
||||
4
tests/components/i2c/test.nrf52-mcumgr.yaml
Normal file
4
tests/components/i2c/test.nrf52-mcumgr.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
packages:
|
||||
i2c: !include ../../test_build_components/common/i2c/nrf52.yaml
|
||||
|
||||
<<: !include common.yaml
|
||||
4
tests/components/i2c/test.nrf52-xiao-ble.yaml
Normal file
4
tests/components/i2c/test.nrf52-xiao-ble.yaml
Normal file
@@ -0,0 +1,4 @@
|
||||
packages:
|
||||
i2c: !include ../../test_build_components/common/i2c/nrf52.yaml
|
||||
|
||||
<<: !include common.yaml
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user