mirror of
https://github.com/esphome/esphome.git
synced 2025-08-03 08:57:47 +00:00
Merge remote-tracking branch 'upstream/dev' into memory
This commit is contained in:
commit
62c7f14d9a
@ -4,7 +4,7 @@
|
||||
repos:
|
||||
- repo: https://github.com/astral-sh/ruff-pre-commit
|
||||
# Ruff version.
|
||||
rev: v0.12.1
|
||||
rev: v0.12.2
|
||||
hooks:
|
||||
# Run the linter.
|
||||
- id: ruff
|
||||
|
@ -87,6 +87,7 @@ esphome/components/bp1658cj/* @Cossid
|
||||
esphome/components/bp5758d/* @Cossid
|
||||
esphome/components/button/* @esphome/core
|
||||
esphome/components/bytebuffer/* @clydebarrow
|
||||
esphome/components/camera/* @DT-art1 @bdraco
|
||||
esphome/components/canbus/* @danielschramm @mvturnho
|
||||
esphome/components/cap1188/* @mreditor97
|
||||
esphome/components/captive_portal/* @OttoWinter
|
||||
@ -124,6 +125,7 @@ esphome/components/dht/* @OttoWinter
|
||||
esphome/components/display_menu_base/* @numo68
|
||||
esphome/components/dps310/* @kbx81
|
||||
esphome/components/ds1307/* @badbadc0ffee
|
||||
esphome/components/ds2484/* @mrk-its
|
||||
esphome/components/dsmr/* @glmnet @zuidwijk
|
||||
esphome/components/duty_time/* @dudanov
|
||||
esphome/components/ee895/* @Stock-M
|
||||
@ -440,6 +442,7 @@ esphome/components/sun/* @OttoWinter
|
||||
esphome/components/sun_gtil2/* @Mat931
|
||||
esphome/components/switch/* @esphome/core
|
||||
esphome/components/switch/binary_sensor/* @ssieb
|
||||
esphome/components/sx127x/* @swoboda1337
|
||||
esphome/components/syslog/* @clydebarrow
|
||||
esphome/components/t6615/* @tylermenezes
|
||||
esphome/components/tc74/* @sethgirvan
|
||||
|
@ -132,7 +132,9 @@ async def to_code(config):
|
||||
await cg.register_component(var, config)
|
||||
|
||||
cg.add(var.set_port(config[CONF_PORT]))
|
||||
cg.add(var.set_password(config[CONF_PASSWORD]))
|
||||
if config[CONF_PASSWORD]:
|
||||
cg.add_define("USE_API_PASSWORD")
|
||||
cg.add(var.set_password(config[CONF_PASSWORD]))
|
||||
cg.add(var.set_reboot_timeout(config[CONF_REBOOT_TIMEOUT]))
|
||||
cg.add(var.set_batch_delay(config[CONF_BATCH_DELAY]))
|
||||
|
||||
|
@ -311,6 +311,7 @@ message BinarySensorStateResponse {
|
||||
// If the binary sensor does not have a valid state yet.
|
||||
// Equivalent to `!obj->has_state()` - inverse logic to make state packets smaller
|
||||
bool missing_state = 3;
|
||||
uint32 device_id = 4;
|
||||
}
|
||||
|
||||
// ==================== COVER ====================
|
||||
@ -360,6 +361,7 @@ message CoverStateResponse {
|
||||
float position = 3;
|
||||
float tilt = 4;
|
||||
CoverOperation current_operation = 5;
|
||||
uint32 device_id = 6;
|
||||
}
|
||||
|
||||
enum LegacyCoverCommand {
|
||||
@ -432,6 +434,7 @@ message FanStateResponse {
|
||||
FanDirection direction = 5;
|
||||
int32 speed_level = 6;
|
||||
string preset_mode = 7;
|
||||
uint32 device_id = 8;
|
||||
}
|
||||
message FanCommandRequest {
|
||||
option (id) = 31;
|
||||
@ -513,6 +516,7 @@ message LightStateResponse {
|
||||
float cold_white = 12;
|
||||
float warm_white = 13;
|
||||
string effect = 9;
|
||||
uint32 device_id = 14;
|
||||
}
|
||||
message LightCommandRequest {
|
||||
option (id) = 32;
|
||||
@ -598,6 +602,7 @@ message SensorStateResponse {
|
||||
// If the sensor does not have a valid state yet.
|
||||
// Equivalent to `!obj->has_state()` - inverse logic to make state packets smaller
|
||||
bool missing_state = 3;
|
||||
uint32 device_id = 4;
|
||||
}
|
||||
|
||||
// ==================== SWITCH ====================
|
||||
@ -628,6 +633,7 @@ message SwitchStateResponse {
|
||||
|
||||
fixed32 key = 1;
|
||||
bool state = 2;
|
||||
uint32 device_id = 3;
|
||||
}
|
||||
message SwitchCommandRequest {
|
||||
option (id) = 33;
|
||||
@ -669,6 +675,7 @@ message TextSensorStateResponse {
|
||||
// If the text sensor does not have a valid state yet.
|
||||
// Equivalent to `!obj->has_state()` - inverse logic to make state packets smaller
|
||||
bool missing_state = 3;
|
||||
uint32 device_id = 4;
|
||||
}
|
||||
|
||||
// ==================== SUBSCRIBE LOGS ====================
|
||||
@ -829,7 +836,7 @@ message ListEntitiesCameraResponse {
|
||||
option (id) = 43;
|
||||
option (base_class) = "InfoResponseProtoMessage";
|
||||
option (source) = SOURCE_SERVER;
|
||||
option (ifdef) = "USE_ESP32_CAMERA";
|
||||
option (ifdef) = "USE_CAMERA";
|
||||
|
||||
string object_id = 1;
|
||||
fixed32 key = 2;
|
||||
@ -844,7 +851,7 @@ message ListEntitiesCameraResponse {
|
||||
message CameraImageResponse {
|
||||
option (id) = 44;
|
||||
option (source) = SOURCE_SERVER;
|
||||
option (ifdef) = "USE_ESP32_CAMERA";
|
||||
option (ifdef) = "USE_CAMERA";
|
||||
|
||||
fixed32 key = 1;
|
||||
bytes data = 2;
|
||||
@ -853,7 +860,7 @@ message CameraImageResponse {
|
||||
message CameraImageRequest {
|
||||
option (id) = 45;
|
||||
option (source) = SOURCE_CLIENT;
|
||||
option (ifdef) = "USE_ESP32_CAMERA";
|
||||
option (ifdef) = "USE_CAMERA";
|
||||
option (no_delay) = true;
|
||||
|
||||
bool single = 1;
|
||||
@ -966,6 +973,7 @@ message ClimateStateResponse {
|
||||
string custom_preset = 13;
|
||||
float current_humidity = 14;
|
||||
float target_humidity = 15;
|
||||
uint32 device_id = 16;
|
||||
}
|
||||
message ClimateCommandRequest {
|
||||
option (id) = 48;
|
||||
@ -1039,6 +1047,7 @@ message NumberStateResponse {
|
||||
// If the number does not have a valid state yet.
|
||||
// Equivalent to `!obj->has_state()` - inverse logic to make state packets smaller
|
||||
bool missing_state = 3;
|
||||
uint32 device_id = 4;
|
||||
}
|
||||
message NumberCommandRequest {
|
||||
option (id) = 51;
|
||||
@ -1080,6 +1089,7 @@ message SelectStateResponse {
|
||||
// If the select does not have a valid state yet.
|
||||
// Equivalent to `!obj->has_state()` - inverse logic to make state packets smaller
|
||||
bool missing_state = 3;
|
||||
uint32 device_id = 4;
|
||||
}
|
||||
message SelectCommandRequest {
|
||||
option (id) = 54;
|
||||
@ -1120,6 +1130,7 @@ message SirenStateResponse {
|
||||
|
||||
fixed32 key = 1;
|
||||
bool state = 2;
|
||||
uint32 device_id = 3;
|
||||
}
|
||||
message SirenCommandRequest {
|
||||
option (id) = 57;
|
||||
@ -1183,6 +1194,7 @@ message LockStateResponse {
|
||||
option (no_delay) = true;
|
||||
fixed32 key = 1;
|
||||
LockState state = 2;
|
||||
uint32 device_id = 3;
|
||||
}
|
||||
message LockCommandRequest {
|
||||
option (id) = 60;
|
||||
@ -1282,6 +1294,7 @@ message MediaPlayerStateResponse {
|
||||
MediaPlayerState state = 2;
|
||||
float volume = 3;
|
||||
bool muted = 4;
|
||||
uint32 device_id = 5;
|
||||
}
|
||||
message MediaPlayerCommandRequest {
|
||||
option (id) = 65;
|
||||
@ -1822,6 +1835,7 @@ message AlarmControlPanelStateResponse {
|
||||
option (no_delay) = true;
|
||||
fixed32 key = 1;
|
||||
AlarmControlPanelState state = 2;
|
||||
uint32 device_id = 3;
|
||||
}
|
||||
|
||||
message AlarmControlPanelCommandRequest {
|
||||
@ -1871,6 +1885,7 @@ message TextStateResponse {
|
||||
// If the Text does not have a valid state yet.
|
||||
// Equivalent to `!obj->has_state()` - inverse logic to make state packets smaller
|
||||
bool missing_state = 3;
|
||||
uint32 device_id = 4;
|
||||
}
|
||||
message TextCommandRequest {
|
||||
option (id) = 99;
|
||||
@ -1914,6 +1929,7 @@ message DateStateResponse {
|
||||
uint32 year = 3;
|
||||
uint32 month = 4;
|
||||
uint32 day = 5;
|
||||
uint32 device_id = 6;
|
||||
}
|
||||
message DateCommandRequest {
|
||||
option (id) = 102;
|
||||
@ -1958,6 +1974,7 @@ message TimeStateResponse {
|
||||
uint32 hour = 3;
|
||||
uint32 minute = 4;
|
||||
uint32 second = 5;
|
||||
uint32 device_id = 6;
|
||||
}
|
||||
message TimeCommandRequest {
|
||||
option (id) = 105;
|
||||
@ -1999,6 +2016,7 @@ message EventResponse {
|
||||
|
||||
fixed32 key = 1;
|
||||
string event_type = 2;
|
||||
uint32 device_id = 3;
|
||||
}
|
||||
|
||||
// ==================== VALVE ====================
|
||||
@ -2039,6 +2057,7 @@ message ValveStateResponse {
|
||||
fixed32 key = 1;
|
||||
float position = 2;
|
||||
ValveOperation current_operation = 3;
|
||||
uint32 device_id = 4;
|
||||
}
|
||||
|
||||
message ValveCommandRequest {
|
||||
@ -2082,6 +2101,7 @@ message DateTimeStateResponse {
|
||||
// Equivalent to `!obj->has_state()` - inverse logic to make state packets smaller
|
||||
bool missing_state = 2;
|
||||
fixed32 epoch_seconds = 3;
|
||||
uint32 device_id = 4;
|
||||
}
|
||||
message DateTimeCommandRequest {
|
||||
option (id) = 114;
|
||||
@ -2128,6 +2148,7 @@ message UpdateStateResponse {
|
||||
string title = 8;
|
||||
string release_summary = 9;
|
||||
string release_url = 10;
|
||||
uint32 device_id = 11;
|
||||
}
|
||||
enum UpdateCommand {
|
||||
UPDATE_COMMAND_NONE = 0;
|
||||
|
@ -38,8 +38,8 @@ static constexpr uint16_t PING_RETRY_INTERVAL = 1000;
|
||||
static constexpr uint32_t KEEPALIVE_DISCONNECT_TIMEOUT = (KEEPALIVE_TIMEOUT_MS * 5) / 2;
|
||||
|
||||
static const char *const TAG = "api.connection";
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
static const int ESP32_CAMERA_STOP_STREAM = 5000;
|
||||
#ifdef USE_CAMERA
|
||||
static const int CAMERA_STOP_STREAM = 5000;
|
||||
#endif
|
||||
|
||||
APIConnection::APIConnection(std::unique_ptr<socket::Socket> sock, APIServer *parent)
|
||||
@ -58,6 +58,11 @@ APIConnection::APIConnection(std::unique_ptr<socket::Socket> sock, APIServer *pa
|
||||
#else
|
||||
#error "No frame helper defined"
|
||||
#endif
|
||||
#ifdef USE_CAMERA
|
||||
if (camera::Camera::instance() != nullptr) {
|
||||
this->image_reader_ = std::unique_ptr<camera::CameraImageReader>{camera::Camera::instance()->create_image_reader()};
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
uint32_t APIConnection::get_batch_delay_ms_() const { return this->parent_->get_batch_delay(); }
|
||||
@ -90,19 +95,6 @@ APIConnection::~APIConnection() {
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void APIConnection::log_batch_item_(const DeferredBatch::BatchItem &item) {
|
||||
// Set log-only mode
|
||||
this->flags_.log_only_mode = true;
|
||||
|
||||
// Call the creator - it will create the message and log it via encode_message_to_buffer
|
||||
item.creator(item.entity, this, std::numeric_limits<uint16_t>::max(), true, item.message_type);
|
||||
|
||||
// Clear log-only mode
|
||||
this->flags_.log_only_mode = false;
|
||||
}
|
||||
#endif
|
||||
|
||||
void APIConnection::loop() {
|
||||
if (this->flags_.next_close) {
|
||||
// requested a disconnect
|
||||
@ -154,15 +146,25 @@ void APIConnection::loop() {
|
||||
}
|
||||
}
|
||||
|
||||
// Process deferred batch if scheduled
|
||||
// Process deferred batch if scheduled and timer has expired
|
||||
if (this->flags_.batch_scheduled && now - this->deferred_batch_.batch_start_time >= this->get_batch_delay_ms_()) {
|
||||
this->process_batch_();
|
||||
}
|
||||
|
||||
if (!this->list_entities_iterator_.completed()) {
|
||||
this->list_entities_iterator_.advance();
|
||||
this->process_iterator_batch_(this->list_entities_iterator_);
|
||||
} else if (!this->initial_state_iterator_.completed()) {
|
||||
this->initial_state_iterator_.advance();
|
||||
this->process_iterator_batch_(this->initial_state_iterator_);
|
||||
|
||||
// If we've completed initial states, process any remaining and clear the flag
|
||||
if (this->initial_state_iterator_.completed()) {
|
||||
// Process any remaining batched messages immediately
|
||||
if (!this->deferred_batch_.empty()) {
|
||||
this->process_batch_();
|
||||
}
|
||||
// Now that everything is sent, enable immediate sending for future state changes
|
||||
this->flags_.should_try_send_immediately = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (this->flags_.sent_ping) {
|
||||
@ -183,10 +185,10 @@ void APIConnection::loop() {
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
if (this->image_reader_.available() && this->helper_->can_write_without_blocking()) {
|
||||
uint32_t to_send = std::min((size_t) MAX_PACKET_SIZE, this->image_reader_.available());
|
||||
bool done = this->image_reader_.available() == to_send;
|
||||
#ifdef USE_CAMERA
|
||||
if (this->image_reader_ && this->image_reader_->available() && this->helper_->can_write_without_blocking()) {
|
||||
uint32_t to_send = std::min((size_t) MAX_PACKET_SIZE, this->image_reader_->available());
|
||||
bool done = this->image_reader_->available() == to_send;
|
||||
uint32_t msg_size = 0;
|
||||
ProtoSize::add_fixed_field<4>(msg_size, 1, true);
|
||||
// partial message size calculated manually since its a special case
|
||||
@ -196,18 +198,18 @@ void APIConnection::loop() {
|
||||
|
||||
auto buffer = this->create_buffer(msg_size);
|
||||
// fixed32 key = 1;
|
||||
buffer.encode_fixed32(1, esp32_camera::global_esp32_camera->get_object_id_hash());
|
||||
buffer.encode_fixed32(1, camera::Camera::instance()->get_object_id_hash());
|
||||
// bytes data = 2;
|
||||
buffer.encode_bytes(2, this->image_reader_.peek_data_buffer(), to_send);
|
||||
buffer.encode_bytes(2, this->image_reader_->peek_data_buffer(), to_send);
|
||||
// bool done = 3;
|
||||
buffer.encode_bool(3, done);
|
||||
|
||||
bool success = this->send_buffer(buffer, CameraImageResponse::MESSAGE_TYPE);
|
||||
|
||||
if (success) {
|
||||
this->image_reader_.consume_data(to_send);
|
||||
this->image_reader_->consume_data(to_send);
|
||||
if (done) {
|
||||
this->image_reader_.return_image();
|
||||
this->image_reader_->return_image();
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -300,8 +302,8 @@ uint16_t APIConnection::encode_message_to_buffer(ProtoMessage &msg, uint16_t mes
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
bool APIConnection::send_binary_sensor_state(binary_sensor::BinarySensor *binary_sensor) {
|
||||
return this->schedule_message_(binary_sensor, &APIConnection::try_send_binary_sensor_state,
|
||||
BinarySensorStateResponse::MESSAGE_TYPE);
|
||||
return this->send_message_smart_(binary_sensor, &APIConnection::try_send_binary_sensor_state,
|
||||
BinarySensorStateResponse::MESSAGE_TYPE);
|
||||
}
|
||||
|
||||
uint16_t APIConnection::try_send_binary_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
@ -328,7 +330,7 @@ uint16_t APIConnection::try_send_binary_sensor_info(EntityBase *entity, APIConne
|
||||
|
||||
#ifdef USE_COVER
|
||||
bool APIConnection::send_cover_state(cover::Cover *cover) {
|
||||
return this->schedule_message_(cover, &APIConnection::try_send_cover_state, CoverStateResponse::MESSAGE_TYPE);
|
||||
return this->send_message_smart_(cover, &APIConnection::try_send_cover_state, CoverStateResponse::MESSAGE_TYPE);
|
||||
}
|
||||
uint16_t APIConnection::try_send_cover_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
bool is_single) {
|
||||
@ -389,7 +391,7 @@ void APIConnection::cover_command(const CoverCommandRequest &msg) {
|
||||
|
||||
#ifdef USE_FAN
|
||||
bool APIConnection::send_fan_state(fan::Fan *fan) {
|
||||
return this->schedule_message_(fan, &APIConnection::try_send_fan_state, FanStateResponse::MESSAGE_TYPE);
|
||||
return this->send_message_smart_(fan, &APIConnection::try_send_fan_state, FanStateResponse::MESSAGE_TYPE);
|
||||
}
|
||||
uint16_t APIConnection::try_send_fan_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
bool is_single) {
|
||||
@ -448,7 +450,7 @@ void APIConnection::fan_command(const FanCommandRequest &msg) {
|
||||
|
||||
#ifdef USE_LIGHT
|
||||
bool APIConnection::send_light_state(light::LightState *light) {
|
||||
return this->schedule_message_(light, &APIConnection::try_send_light_state, LightStateResponse::MESSAGE_TYPE);
|
||||
return this->send_message_smart_(light, &APIConnection::try_send_light_state, LightStateResponse::MESSAGE_TYPE);
|
||||
}
|
||||
uint16_t APIConnection::try_send_light_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
bool is_single) {
|
||||
@ -540,7 +542,7 @@ void APIConnection::light_command(const LightCommandRequest &msg) {
|
||||
|
||||
#ifdef USE_SENSOR
|
||||
bool APIConnection::send_sensor_state(sensor::Sensor *sensor) {
|
||||
return this->schedule_message_(sensor, &APIConnection::try_send_sensor_state, SensorStateResponse::MESSAGE_TYPE);
|
||||
return this->send_message_smart_(sensor, &APIConnection::try_send_sensor_state, SensorStateResponse::MESSAGE_TYPE);
|
||||
}
|
||||
|
||||
uint16_t APIConnection::try_send_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
@ -572,7 +574,7 @@ uint16_t APIConnection::try_send_sensor_info(EntityBase *entity, APIConnection *
|
||||
|
||||
#ifdef USE_SWITCH
|
||||
bool APIConnection::send_switch_state(switch_::Switch *a_switch) {
|
||||
return this->schedule_message_(a_switch, &APIConnection::try_send_switch_state, SwitchStateResponse::MESSAGE_TYPE);
|
||||
return this->send_message_smart_(a_switch, &APIConnection::try_send_switch_state, SwitchStateResponse::MESSAGE_TYPE);
|
||||
}
|
||||
|
||||
uint16_t APIConnection::try_send_switch_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
@ -609,8 +611,8 @@ void APIConnection::switch_command(const SwitchCommandRequest &msg) {
|
||||
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
bool APIConnection::send_text_sensor_state(text_sensor::TextSensor *text_sensor) {
|
||||
return this->schedule_message_(text_sensor, &APIConnection::try_send_text_sensor_state,
|
||||
TextSensorStateResponse::MESSAGE_TYPE);
|
||||
return this->send_message_smart_(text_sensor, &APIConnection::try_send_text_sensor_state,
|
||||
TextSensorStateResponse::MESSAGE_TYPE);
|
||||
}
|
||||
|
||||
uint16_t APIConnection::try_send_text_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
@ -637,7 +639,7 @@ uint16_t APIConnection::try_send_text_sensor_info(EntityBase *entity, APIConnect
|
||||
|
||||
#ifdef USE_CLIMATE
|
||||
bool APIConnection::send_climate_state(climate::Climate *climate) {
|
||||
return this->schedule_message_(climate, &APIConnection::try_send_climate_state, ClimateStateResponse::MESSAGE_TYPE);
|
||||
return this->send_message_smart_(climate, &APIConnection::try_send_climate_state, ClimateStateResponse::MESSAGE_TYPE);
|
||||
}
|
||||
uint16_t APIConnection::try_send_climate_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
bool is_single) {
|
||||
@ -737,7 +739,7 @@ void APIConnection::climate_command(const ClimateCommandRequest &msg) {
|
||||
|
||||
#ifdef USE_NUMBER
|
||||
bool APIConnection::send_number_state(number::Number *number) {
|
||||
return this->schedule_message_(number, &APIConnection::try_send_number_state, NumberStateResponse::MESSAGE_TYPE);
|
||||
return this->send_message_smart_(number, &APIConnection::try_send_number_state, NumberStateResponse::MESSAGE_TYPE);
|
||||
}
|
||||
|
||||
uint16_t APIConnection::try_send_number_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
@ -777,7 +779,7 @@ void APIConnection::number_command(const NumberCommandRequest &msg) {
|
||||
|
||||
#ifdef USE_DATETIME_DATE
|
||||
bool APIConnection::send_date_state(datetime::DateEntity *date) {
|
||||
return this->schedule_message_(date, &APIConnection::try_send_date_state, DateStateResponse::MESSAGE_TYPE);
|
||||
return this->send_message_smart_(date, &APIConnection::try_send_date_state, DateStateResponse::MESSAGE_TYPE);
|
||||
}
|
||||
uint16_t APIConnection::try_send_date_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
bool is_single) {
|
||||
@ -811,7 +813,7 @@ void APIConnection::date_command(const DateCommandRequest &msg) {
|
||||
|
||||
#ifdef USE_DATETIME_TIME
|
||||
bool APIConnection::send_time_state(datetime::TimeEntity *time) {
|
||||
return this->schedule_message_(time, &APIConnection::try_send_time_state, TimeStateResponse::MESSAGE_TYPE);
|
||||
return this->send_message_smart_(time, &APIConnection::try_send_time_state, TimeStateResponse::MESSAGE_TYPE);
|
||||
}
|
||||
uint16_t APIConnection::try_send_time_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
bool is_single) {
|
||||
@ -845,8 +847,8 @@ void APIConnection::time_command(const TimeCommandRequest &msg) {
|
||||
|
||||
#ifdef USE_DATETIME_DATETIME
|
||||
bool APIConnection::send_datetime_state(datetime::DateTimeEntity *datetime) {
|
||||
return this->schedule_message_(datetime, &APIConnection::try_send_datetime_state,
|
||||
DateTimeStateResponse::MESSAGE_TYPE);
|
||||
return this->send_message_smart_(datetime, &APIConnection::try_send_datetime_state,
|
||||
DateTimeStateResponse::MESSAGE_TYPE);
|
||||
}
|
||||
uint16_t APIConnection::try_send_datetime_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
bool is_single) {
|
||||
@ -881,7 +883,7 @@ void APIConnection::datetime_command(const DateTimeCommandRequest &msg) {
|
||||
|
||||
#ifdef USE_TEXT
|
||||
bool APIConnection::send_text_state(text::Text *text) {
|
||||
return this->schedule_message_(text, &APIConnection::try_send_text_state, TextStateResponse::MESSAGE_TYPE);
|
||||
return this->send_message_smart_(text, &APIConnection::try_send_text_state, TextStateResponse::MESSAGE_TYPE);
|
||||
}
|
||||
|
||||
uint16_t APIConnection::try_send_text_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
@ -919,7 +921,7 @@ void APIConnection::text_command(const TextCommandRequest &msg) {
|
||||
|
||||
#ifdef USE_SELECT
|
||||
bool APIConnection::send_select_state(select::Select *select) {
|
||||
return this->schedule_message_(select, &APIConnection::try_send_select_state, SelectStateResponse::MESSAGE_TYPE);
|
||||
return this->send_message_smart_(select, &APIConnection::try_send_select_state, SelectStateResponse::MESSAGE_TYPE);
|
||||
}
|
||||
|
||||
uint16_t APIConnection::try_send_select_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
@ -974,7 +976,7 @@ void esphome::api::APIConnection::button_command(const ButtonCommandRequest &msg
|
||||
|
||||
#ifdef USE_LOCK
|
||||
bool APIConnection::send_lock_state(lock::Lock *a_lock) {
|
||||
return this->schedule_message_(a_lock, &APIConnection::try_send_lock_state, LockStateResponse::MESSAGE_TYPE);
|
||||
return this->send_message_smart_(a_lock, &APIConnection::try_send_lock_state, LockStateResponse::MESSAGE_TYPE);
|
||||
}
|
||||
|
||||
uint16_t APIConnection::try_send_lock_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
@ -1018,7 +1020,7 @@ void APIConnection::lock_command(const LockCommandRequest &msg) {
|
||||
|
||||
#ifdef USE_VALVE
|
||||
bool APIConnection::send_valve_state(valve::Valve *valve) {
|
||||
return this->schedule_message_(valve, &APIConnection::try_send_valve_state, ValveStateResponse::MESSAGE_TYPE);
|
||||
return this->send_message_smart_(valve, &APIConnection::try_send_valve_state, ValveStateResponse::MESSAGE_TYPE);
|
||||
}
|
||||
uint16_t APIConnection::try_send_valve_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
bool is_single) {
|
||||
@ -1058,8 +1060,8 @@ void APIConnection::valve_command(const ValveCommandRequest &msg) {
|
||||
|
||||
#ifdef USE_MEDIA_PLAYER
|
||||
bool APIConnection::send_media_player_state(media_player::MediaPlayer *media_player) {
|
||||
return this->schedule_message_(media_player, &APIConnection::try_send_media_player_state,
|
||||
MediaPlayerStateResponse::MESSAGE_TYPE);
|
||||
return this->send_message_smart_(media_player, &APIConnection::try_send_media_player_state,
|
||||
MediaPlayerStateResponse::MESSAGE_TYPE);
|
||||
}
|
||||
uint16_t APIConnection::try_send_media_player_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
bool is_single) {
|
||||
@ -1115,36 +1117,36 @@ void APIConnection::media_player_command(const MediaPlayerCommandRequest &msg) {
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
void APIConnection::set_camera_state(std::shared_ptr<esp32_camera::CameraImage> image) {
|
||||
#ifdef USE_CAMERA
|
||||
void APIConnection::set_camera_state(std::shared_ptr<camera::CameraImage> image) {
|
||||
if (!this->flags_.state_subscription)
|
||||
return;
|
||||
if (this->image_reader_.available())
|
||||
if (!this->image_reader_)
|
||||
return;
|
||||
if (image->was_requested_by(esphome::esp32_camera::API_REQUESTER) ||
|
||||
image->was_requested_by(esphome::esp32_camera::IDLE))
|
||||
this->image_reader_.set_image(std::move(image));
|
||||
if (this->image_reader_->available())
|
||||
return;
|
||||
if (image->was_requested_by(esphome::camera::API_REQUESTER) || image->was_requested_by(esphome::camera::IDLE))
|
||||
this->image_reader_->set_image(std::move(image));
|
||||
}
|
||||
uint16_t APIConnection::try_send_camera_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
bool is_single) {
|
||||
auto *camera = static_cast<esp32_camera::ESP32Camera *>(entity);
|
||||
auto *camera = static_cast<camera::Camera *>(entity);
|
||||
ListEntitiesCameraResponse msg;
|
||||
msg.unique_id = get_default_unique_id("camera", camera);
|
||||
fill_entity_info_base(camera, msg);
|
||||
return encode_message_to_buffer(msg, ListEntitiesCameraResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
void APIConnection::camera_image(const CameraImageRequest &msg) {
|
||||
if (esp32_camera::global_esp32_camera == nullptr)
|
||||
if (camera::Camera::instance() == nullptr)
|
||||
return;
|
||||
|
||||
if (msg.single)
|
||||
esp32_camera::global_esp32_camera->request_image(esphome::esp32_camera::API_REQUESTER);
|
||||
camera::Camera::instance()->request_image(esphome::camera::API_REQUESTER);
|
||||
if (msg.stream) {
|
||||
esp32_camera::global_esp32_camera->start_stream(esphome::esp32_camera::API_REQUESTER);
|
||||
camera::Camera::instance()->start_stream(esphome::camera::API_REQUESTER);
|
||||
|
||||
App.scheduler.set_timeout(this->parent_, "api_esp32_camera_stop_stream", ESP32_CAMERA_STOP_STREAM, []() {
|
||||
esp32_camera::global_esp32_camera->stop_stream(esphome::esp32_camera::API_REQUESTER);
|
||||
});
|
||||
App.scheduler.set_timeout(this->parent_, "api_camera_stop_stream", CAMERA_STOP_STREAM,
|
||||
[]() { camera::Camera::instance()->stop_stream(esphome::camera::API_REQUESTER); });
|
||||
}
|
||||
}
|
||||
#endif
|
||||
@ -1320,8 +1322,8 @@ void APIConnection::voice_assistant_set_configuration(const VoiceAssistantSetCon
|
||||
|
||||
#ifdef USE_ALARM_CONTROL_PANEL
|
||||
bool APIConnection::send_alarm_control_panel_state(alarm_control_panel::AlarmControlPanel *a_alarm_control_panel) {
|
||||
return this->schedule_message_(a_alarm_control_panel, &APIConnection::try_send_alarm_control_panel_state,
|
||||
AlarmControlPanelStateResponse::MESSAGE_TYPE);
|
||||
return this->send_message_smart_(a_alarm_control_panel, &APIConnection::try_send_alarm_control_panel_state,
|
||||
AlarmControlPanelStateResponse::MESSAGE_TYPE);
|
||||
}
|
||||
uint16_t APIConnection::try_send_alarm_control_panel_state(EntityBase *entity, APIConnection *conn,
|
||||
uint32_t remaining_size, bool is_single) {
|
||||
@ -1404,7 +1406,7 @@ uint16_t APIConnection::try_send_event_info(EntityBase *entity, APIConnection *c
|
||||
|
||||
#ifdef USE_UPDATE
|
||||
bool APIConnection::send_update_state(update::UpdateEntity *update) {
|
||||
return this->schedule_message_(update, &APIConnection::try_send_update_state, UpdateStateResponse::MESSAGE_TYPE);
|
||||
return this->send_message_smart_(update, &APIConnection::try_send_update_state, UpdateStateResponse::MESSAGE_TYPE);
|
||||
}
|
||||
uint16_t APIConnection::try_send_update_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
bool is_single) {
|
||||
@ -1503,7 +1505,10 @@ HelloResponse APIConnection::hello(const HelloRequest &msg) {
|
||||
return resp;
|
||||
}
|
||||
ConnectResponse APIConnection::connect(const ConnectRequest &msg) {
|
||||
bool correct = this->parent_->check_password(msg.password);
|
||||
bool correct = true;
|
||||
#ifdef USE_API_PASSWORD
|
||||
correct = this->parent_->check_password(msg.password);
|
||||
#endif
|
||||
|
||||
ConnectResponse resp;
|
||||
// bool invalid_password = 1;
|
||||
@ -1524,7 +1529,11 @@ ConnectResponse APIConnection::connect(const ConnectRequest &msg) {
|
||||
}
|
||||
DeviceInfoResponse APIConnection::device_info(const DeviceInfoRequest &msg) {
|
||||
DeviceInfoResponse resp{};
|
||||
#ifdef USE_API_PASSWORD
|
||||
resp.uses_password = this->parent_->uses_password();
|
||||
#else
|
||||
resp.uses_password = false;
|
||||
#endif
|
||||
resp.name = App.get_name();
|
||||
resp.friendly_name = App.get_friendly_name();
|
||||
resp.suggested_area = App.get_area();
|
||||
@ -1744,11 +1753,16 @@ void APIConnection::process_batch_() {
|
||||
|
||||
if (payload_size > 0 &&
|
||||
this->send_buffer(ProtoWriteBuffer{&this->parent_->get_shared_buffer_ref()}, item.message_type)) {
|
||||
this->deferred_batch_.clear();
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
// Log messages after send attempt for VV debugging
|
||||
// It's safe to use the buffer for logging at this point regardless of send result
|
||||
this->log_batch_item_(item);
|
||||
#endif
|
||||
this->clear_batch_();
|
||||
} else if (payload_size == 0) {
|
||||
// Message too large
|
||||
ESP_LOGW(TAG, "Message too large to send: type=%u", item.message_type);
|
||||
this->deferred_batch_.clear();
|
||||
this->clear_batch_();
|
||||
}
|
||||
return;
|
||||
}
|
||||
@ -1857,7 +1871,7 @@ void APIConnection::process_batch_() {
|
||||
this->schedule_batch_();
|
||||
} else {
|
||||
// All items processed
|
||||
this->deferred_batch_.clear();
|
||||
this->clear_batch_();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -18,6 +18,8 @@ namespace api {
|
||||
|
||||
// Keepalive timeout in milliseconds
|
||||
static constexpr uint32_t KEEPALIVE_TIMEOUT_MS = 60000;
|
||||
// Maximum number of entities to process in a single batch during initial state/info sending
|
||||
static constexpr size_t MAX_INITIAL_PER_BATCH = 20;
|
||||
|
||||
class APIConnection : public APIServerConnection {
|
||||
public:
|
||||
@ -58,8 +60,8 @@ class APIConnection : public APIServerConnection {
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
bool send_text_sensor_state(text_sensor::TextSensor *text_sensor);
|
||||
#endif
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
void set_camera_state(std::shared_ptr<esp32_camera::CameraImage> image);
|
||||
#ifdef USE_CAMERA
|
||||
void set_camera_state(std::shared_ptr<camera::CameraImage> image);
|
||||
void camera_image(const CameraImageRequest &msg) override;
|
||||
#endif
|
||||
#ifdef USE_CLIMATE
|
||||
@ -290,12 +292,29 @@ class APIConnection : public APIServerConnection {
|
||||
// Helper function to fill common entity state fields
|
||||
static void fill_entity_state_base(esphome::EntityBase *entity, StateResponseProtoMessage &response) {
|
||||
response.key = entity->get_object_id_hash();
|
||||
#ifdef USE_DEVICES
|
||||
response.device_id = entity->get_device_id();
|
||||
#endif
|
||||
}
|
||||
|
||||
// Non-template helper to encode any ProtoMessage
|
||||
static uint16_t encode_message_to_buffer(ProtoMessage &msg, uint16_t message_type, APIConnection *conn,
|
||||
uint32_t remaining_size, bool is_single);
|
||||
|
||||
// Helper method to process multiple entities from an iterator in a batch
|
||||
template<typename Iterator> void process_iterator_batch_(Iterator &iterator) {
|
||||
size_t initial_size = this->deferred_batch_.size();
|
||||
while (!iterator.completed() && (this->deferred_batch_.size() - initial_size) < MAX_INITIAL_PER_BATCH) {
|
||||
iterator.advance();
|
||||
}
|
||||
|
||||
// If the batch is full, process it immediately
|
||||
// Note: iterator.advance() already calls schedule_batch_() via schedule_message_()
|
||||
if (this->deferred_batch_.size() >= MAX_INITIAL_PER_BATCH) {
|
||||
this->process_batch_();
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
static uint16_t try_send_binary_sensor_state(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
bool is_single);
|
||||
@ -406,7 +425,7 @@ class APIConnection : public APIServerConnection {
|
||||
static uint16_t try_send_update_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
bool is_single);
|
||||
#endif
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
#ifdef USE_CAMERA
|
||||
static uint16_t try_send_camera_info(EntityBase *entity, APIConnection *conn, uint32_t remaining_size,
|
||||
bool is_single);
|
||||
#endif
|
||||
@ -436,8 +455,8 @@ class APIConnection : public APIServerConnection {
|
||||
// These contain vectors/pointers internally, so putting them early ensures good alignment
|
||||
InitialStateIterator initial_state_iterator_;
|
||||
ListEntitiesIterator list_entities_iterator_;
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
esp32_camera::CameraImageReader image_reader_;
|
||||
#ifdef USE_CAMERA
|
||||
std::unique_ptr<camera::CameraImageReader> image_reader_;
|
||||
#endif
|
||||
|
||||
// Group 3: Strings (12 bytes each on 32-bit, 4-byte aligned)
|
||||
@ -582,7 +601,8 @@ class APIConnection : public APIServerConnection {
|
||||
uint8_t service_call_subscription : 1;
|
||||
uint8_t next_close : 1;
|
||||
uint8_t batch_scheduled : 1;
|
||||
uint8_t batch_first_message : 1; // For batch buffer allocation
|
||||
uint8_t batch_first_message : 1; // For batch buffer allocation
|
||||
uint8_t should_try_send_immediately : 1; // True after initial states are sent
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
uint8_t log_only_mode : 1;
|
||||
#endif
|
||||
@ -609,11 +629,50 @@ class APIConnection : public APIServerConnection {
|
||||
|
||||
bool schedule_batch_();
|
||||
void process_batch_();
|
||||
void clear_batch_() {
|
||||
this->deferred_batch_.clear();
|
||||
this->flags_.batch_scheduled = false;
|
||||
}
|
||||
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
void log_batch_item_(const DeferredBatch::BatchItem &item);
|
||||
// Helper to log a proto message from a MessageCreator object
|
||||
void log_proto_message_(EntityBase *entity, const MessageCreator &creator, uint16_t message_type) {
|
||||
this->flags_.log_only_mode = true;
|
||||
creator(entity, this, MAX_PACKET_SIZE, true, message_type);
|
||||
this->flags_.log_only_mode = false;
|
||||
}
|
||||
|
||||
void log_batch_item_(const DeferredBatch::BatchItem &item) {
|
||||
// Use the helper to log the message
|
||||
this->log_proto_message_(item.entity, item.creator, item.message_type);
|
||||
}
|
||||
#endif
|
||||
|
||||
// Helper method to send a message either immediately or via batching
|
||||
bool send_message_smart_(EntityBase *entity, MessageCreatorPtr creator, uint16_t message_type) {
|
||||
// Try to send immediately if:
|
||||
// 1. We should try to send immediately (should_try_send_immediately = true)
|
||||
// 2. Batch delay is 0 (user has opted in to immediate sending)
|
||||
// 3. Buffer has space available
|
||||
if (this->flags_.should_try_send_immediately && this->get_batch_delay_ms_() == 0 &&
|
||||
this->helper_->can_write_without_blocking()) {
|
||||
// Now actually encode and send
|
||||
if (creator(entity, this, MAX_PACKET_SIZE, true) &&
|
||||
this->send_buffer(ProtoWriteBuffer{&this->parent_->get_shared_buffer_ref()}, message_type)) {
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
// Log the message in verbose mode
|
||||
this->log_proto_message_(entity, MessageCreator(creator), message_type);
|
||||
#endif
|
||||
return true;
|
||||
}
|
||||
|
||||
// If immediate send failed, fall through to batching
|
||||
}
|
||||
|
||||
// Fall back to scheduled batching
|
||||
return this->schedule_message_(entity, creator, message_type);
|
||||
}
|
||||
|
||||
// Helper function to schedule a deferred message with known message type
|
||||
bool schedule_message_(EntityBase *entity, MessageCreator creator, uint16_t message_type) {
|
||||
this->deferred_batch_.add_item(entity, std::move(creator), message_type);
|
||||
|
@ -614,20 +614,14 @@ APIError APINoiseFrameHelper::read_packet(ReadPacketBuffer *buffer) {
|
||||
return APIError::OK;
|
||||
}
|
||||
APIError APINoiseFrameHelper::write_protobuf_packet(uint16_t type, ProtoWriteBuffer buffer) {
|
||||
std::vector<uint8_t> *raw_buffer = buffer.get_buffer();
|
||||
uint16_t payload_len = static_cast<uint16_t>(raw_buffer->size() - frame_header_padding_);
|
||||
|
||||
// Resize to include MAC space (required for Noise encryption)
|
||||
raw_buffer->resize(raw_buffer->size() + frame_footer_size_);
|
||||
|
||||
// Use write_protobuf_packets with a single packet
|
||||
std::vector<PacketInfo> packets;
|
||||
packets.emplace_back(type, 0, payload_len);
|
||||
|
||||
return write_protobuf_packets(buffer, packets);
|
||||
buffer.get_buffer()->resize(buffer.get_buffer()->size() + frame_footer_size_);
|
||||
PacketInfo packet{type, 0,
|
||||
static_cast<uint16_t>(buffer.get_buffer()->size() - frame_header_padding_ - frame_footer_size_)};
|
||||
return write_protobuf_packets(buffer, std::span<const PacketInfo>(&packet, 1));
|
||||
}
|
||||
|
||||
APIError APINoiseFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer, const std::vector<PacketInfo> &packets) {
|
||||
APIError APINoiseFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer, std::span<const PacketInfo> packets) {
|
||||
APIError aerr = state_action_();
|
||||
if (aerr != APIError::OK) {
|
||||
return aerr;
|
||||
@ -642,18 +636,15 @@ APIError APINoiseFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer, co
|
||||
}
|
||||
|
||||
std::vector<uint8_t> *raw_buffer = buffer.get_buffer();
|
||||
uint8_t *buffer_data = raw_buffer->data(); // Cache buffer pointer
|
||||
|
||||
this->reusable_iovs_.clear();
|
||||
this->reusable_iovs_.reserve(packets.size());
|
||||
|
||||
// We need to encrypt each packet in place
|
||||
for (const auto &packet : packets) {
|
||||
uint16_t type = packet.message_type;
|
||||
uint16_t offset = packet.offset;
|
||||
uint16_t payload_len = packet.payload_size;
|
||||
uint16_t msg_len = 4 + payload_len; // type(2) + data_len(2) + payload
|
||||
|
||||
// The buffer already has padding at offset
|
||||
uint8_t *buf_start = raw_buffer->data() + offset;
|
||||
uint8_t *buf_start = buffer_data + packet.offset;
|
||||
|
||||
// Write noise header
|
||||
buf_start[0] = 0x01; // indicator
|
||||
@ -661,10 +652,10 @@ APIError APINoiseFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer, co
|
||||
|
||||
// Write message header (to be encrypted)
|
||||
const uint8_t msg_offset = 3;
|
||||
buf_start[msg_offset + 0] = (uint8_t) (type >> 8); // type high byte
|
||||
buf_start[msg_offset + 1] = (uint8_t) type; // type low byte
|
||||
buf_start[msg_offset + 2] = (uint8_t) (payload_len >> 8); // data_len high byte
|
||||
buf_start[msg_offset + 3] = (uint8_t) payload_len; // data_len low byte
|
||||
buf_start[msg_offset] = static_cast<uint8_t>(packet.message_type >> 8); // type high byte
|
||||
buf_start[msg_offset + 1] = static_cast<uint8_t>(packet.message_type); // type low byte
|
||||
buf_start[msg_offset + 2] = static_cast<uint8_t>(packet.payload_size >> 8); // data_len high byte
|
||||
buf_start[msg_offset + 3] = static_cast<uint8_t>(packet.payload_size); // data_len low byte
|
||||
// payload data is already in the buffer starting at offset + 7
|
||||
|
||||
// Make sure we have space for MAC
|
||||
@ -673,7 +664,8 @@ APIError APINoiseFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer, co
|
||||
// Encrypt the message in place
|
||||
NoiseBuffer mbuf;
|
||||
noise_buffer_init(mbuf);
|
||||
noise_buffer_set_inout(mbuf, buf_start + msg_offset, msg_len, msg_len + frame_footer_size_);
|
||||
noise_buffer_set_inout(mbuf, buf_start + msg_offset, 4 + packet.payload_size,
|
||||
4 + packet.payload_size + frame_footer_size_);
|
||||
|
||||
int err = noise_cipherstate_encrypt(send_cipher_, &mbuf);
|
||||
if (err != 0) {
|
||||
@ -683,14 +675,12 @@ APIError APINoiseFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer, co
|
||||
}
|
||||
|
||||
// Fill in the encrypted size
|
||||
buf_start[1] = (uint8_t) (mbuf.size >> 8);
|
||||
buf_start[2] = (uint8_t) mbuf.size;
|
||||
buf_start[1] = static_cast<uint8_t>(mbuf.size >> 8);
|
||||
buf_start[2] = static_cast<uint8_t>(mbuf.size);
|
||||
|
||||
// Add iovec for this encrypted packet
|
||||
struct iovec iov;
|
||||
iov.iov_base = buf_start;
|
||||
iov.iov_len = 3 + mbuf.size; // indicator + size + encrypted data
|
||||
this->reusable_iovs_.push_back(iov);
|
||||
this->reusable_iovs_.push_back(
|
||||
{buf_start, static_cast<size_t>(3 + mbuf.size)}); // indicator + size + encrypted data
|
||||
}
|
||||
|
||||
// Send all encrypted packets in one writev call
|
||||
@ -1029,18 +1019,11 @@ APIError APIPlaintextFrameHelper::read_packet(ReadPacketBuffer *buffer) {
|
||||
return APIError::OK;
|
||||
}
|
||||
APIError APIPlaintextFrameHelper::write_protobuf_packet(uint16_t type, ProtoWriteBuffer buffer) {
|
||||
std::vector<uint8_t> *raw_buffer = buffer.get_buffer();
|
||||
uint16_t payload_len = static_cast<uint16_t>(raw_buffer->size() - frame_header_padding_);
|
||||
|
||||
// Use write_protobuf_packets with a single packet
|
||||
std::vector<PacketInfo> packets;
|
||||
packets.emplace_back(type, 0, payload_len);
|
||||
|
||||
return write_protobuf_packets(buffer, packets);
|
||||
PacketInfo packet{type, 0, static_cast<uint16_t>(buffer.get_buffer()->size() - frame_header_padding_)};
|
||||
return write_protobuf_packets(buffer, std::span<const PacketInfo>(&packet, 1));
|
||||
}
|
||||
|
||||
APIError APIPlaintextFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer,
|
||||
const std::vector<PacketInfo> &packets) {
|
||||
APIError APIPlaintextFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer, std::span<const PacketInfo> packets) {
|
||||
if (state_ != State::DATA) {
|
||||
return APIError::BAD_STATE;
|
||||
}
|
||||
@ -1050,17 +1033,15 @@ APIError APIPlaintextFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer
|
||||
}
|
||||
|
||||
std::vector<uint8_t> *raw_buffer = buffer.get_buffer();
|
||||
uint8_t *buffer_data = raw_buffer->data(); // Cache buffer pointer
|
||||
|
||||
this->reusable_iovs_.clear();
|
||||
this->reusable_iovs_.reserve(packets.size());
|
||||
|
||||
for (const auto &packet : packets) {
|
||||
uint16_t type = packet.message_type;
|
||||
uint16_t offset = packet.offset;
|
||||
uint16_t payload_len = packet.payload_size;
|
||||
|
||||
// Calculate varint sizes for header layout
|
||||
uint8_t size_varint_len = api::ProtoSize::varint(static_cast<uint32_t>(payload_len));
|
||||
uint8_t type_varint_len = api::ProtoSize::varint(static_cast<uint32_t>(type));
|
||||
uint8_t size_varint_len = api::ProtoSize::varint(static_cast<uint32_t>(packet.payload_size));
|
||||
uint8_t type_varint_len = api::ProtoSize::varint(static_cast<uint32_t>(packet.message_type));
|
||||
uint8_t total_header_len = 1 + size_varint_len + type_varint_len;
|
||||
|
||||
// Calculate where to start writing the header
|
||||
@ -1088,23 +1069,20 @@ APIError APIPlaintextFrameHelper::write_protobuf_packets(ProtoWriteBuffer buffer
|
||||
//
|
||||
// The message starts at offset + frame_header_padding_
|
||||
// So we write the header starting at offset + frame_header_padding_ - total_header_len
|
||||
uint8_t *buf_start = raw_buffer->data() + offset;
|
||||
uint8_t *buf_start = buffer_data + packet.offset;
|
||||
uint32_t header_offset = frame_header_padding_ - total_header_len;
|
||||
|
||||
// Write the plaintext header
|
||||
buf_start[header_offset] = 0x00; // indicator
|
||||
|
||||
// Encode size varint directly into buffer
|
||||
ProtoVarInt(payload_len).encode_to_buffer_unchecked(buf_start + header_offset + 1, size_varint_len);
|
||||
|
||||
// Encode type varint directly into buffer
|
||||
ProtoVarInt(type).encode_to_buffer_unchecked(buf_start + header_offset + 1 + size_varint_len, type_varint_len);
|
||||
// Encode varints directly into buffer
|
||||
ProtoVarInt(packet.payload_size).encode_to_buffer_unchecked(buf_start + header_offset + 1, size_varint_len);
|
||||
ProtoVarInt(packet.message_type)
|
||||
.encode_to_buffer_unchecked(buf_start + header_offset + 1 + size_varint_len, type_varint_len);
|
||||
|
||||
// Add iovec for this packet (header + payload)
|
||||
struct iovec iov;
|
||||
iov.iov_base = buf_start + header_offset;
|
||||
iov.iov_len = total_header_len + payload_len;
|
||||
this->reusable_iovs_.push_back(iov);
|
||||
this->reusable_iovs_.push_back(
|
||||
{buf_start + header_offset, static_cast<size_t>(total_header_len + packet.payload_size)});
|
||||
}
|
||||
|
||||
// Send all packets in one writev call
|
||||
|
@ -2,6 +2,7 @@
|
||||
#include <cstdint>
|
||||
#include <deque>
|
||||
#include <limits>
|
||||
#include <span>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
@ -101,7 +102,7 @@ class APIFrameHelper {
|
||||
// Write multiple protobuf packets in a single operation
|
||||
// packets contains (message_type, offset, length) for each message in the buffer
|
||||
// The buffer contains all messages with appropriate padding before each
|
||||
virtual APIError write_protobuf_packets(ProtoWriteBuffer buffer, const std::vector<PacketInfo> &packets) = 0;
|
||||
virtual APIError write_protobuf_packets(ProtoWriteBuffer buffer, std::span<const PacketInfo> packets) = 0;
|
||||
// Get the frame header padding required by this protocol
|
||||
virtual uint8_t frame_header_padding() = 0;
|
||||
// Get the frame footer size required by this protocol
|
||||
@ -194,7 +195,7 @@ class APINoiseFrameHelper : public APIFrameHelper {
|
||||
APIError loop() override;
|
||||
APIError read_packet(ReadPacketBuffer *buffer) override;
|
||||
APIError write_protobuf_packet(uint16_t type, ProtoWriteBuffer buffer) override;
|
||||
APIError write_protobuf_packets(ProtoWriteBuffer buffer, const std::vector<PacketInfo> &packets) override;
|
||||
APIError write_protobuf_packets(ProtoWriteBuffer buffer, std::span<const PacketInfo> packets) override;
|
||||
// Get the frame header padding required by this protocol
|
||||
uint8_t frame_header_padding() override { return frame_header_padding_; }
|
||||
// Get the frame footer size required by this protocol
|
||||
@ -248,7 +249,7 @@ class APIPlaintextFrameHelper : public APIFrameHelper {
|
||||
APIError loop() override;
|
||||
APIError read_packet(ReadPacketBuffer *buffer) override;
|
||||
APIError write_protobuf_packet(uint16_t type, ProtoWriteBuffer buffer) override;
|
||||
APIError write_protobuf_packets(ProtoWriteBuffer buffer, const std::vector<PacketInfo> &packets) override;
|
||||
APIError write_protobuf_packets(ProtoWriteBuffer buffer, std::span<const PacketInfo> packets) override;
|
||||
uint8_t frame_header_padding() override { return frame_header_padding_; }
|
||||
// Get the frame footer size required by this protocol
|
||||
uint8_t frame_footer_size() override { return frame_footer_size_; }
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -2,6 +2,8 @@
|
||||
// See script/api_protobuf/api_protobuf.py
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
#include "proto.h"
|
||||
#include "api_pb2_size.h"
|
||||
|
||||
@ -15,6 +17,7 @@ enum EntityCategory : uint32_t {
|
||||
ENTITY_CATEGORY_CONFIG = 1,
|
||||
ENTITY_CATEGORY_DIAGNOSTIC = 2,
|
||||
};
|
||||
#ifdef USE_COVER
|
||||
enum LegacyCoverState : uint32_t {
|
||||
LEGACY_COVER_STATE_OPEN = 0,
|
||||
LEGACY_COVER_STATE_CLOSED = 1,
|
||||
@ -29,6 +32,8 @@ enum LegacyCoverCommand : uint32_t {
|
||||
LEGACY_COVER_COMMAND_CLOSE = 1,
|
||||
LEGACY_COVER_COMMAND_STOP = 2,
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_FAN
|
||||
enum FanSpeed : uint32_t {
|
||||
FAN_SPEED_LOW = 0,
|
||||
FAN_SPEED_MEDIUM = 1,
|
||||
@ -38,6 +43,8 @@ enum FanDirection : uint32_t {
|
||||
FAN_DIRECTION_FORWARD = 0,
|
||||
FAN_DIRECTION_REVERSE = 1,
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_LIGHT
|
||||
enum ColorMode : uint32_t {
|
||||
COLOR_MODE_UNKNOWN = 0,
|
||||
COLOR_MODE_ON_OFF = 1,
|
||||
@ -51,6 +58,8 @@ enum ColorMode : uint32_t {
|
||||
COLOR_MODE_RGB_COLOR_TEMPERATURE = 47,
|
||||
COLOR_MODE_RGB_COLD_WARM_WHITE = 51,
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_SENSOR
|
||||
enum SensorStateClass : uint32_t {
|
||||
STATE_CLASS_NONE = 0,
|
||||
STATE_CLASS_MEASUREMENT = 1,
|
||||
@ -62,6 +71,7 @@ enum SensorLastResetType : uint32_t {
|
||||
LAST_RESET_NEVER = 1,
|
||||
LAST_RESET_AUTO = 2,
|
||||
};
|
||||
#endif
|
||||
enum LogLevel : uint32_t {
|
||||
LOG_LEVEL_NONE = 0,
|
||||
LOG_LEVEL_ERROR = 1,
|
||||
@ -82,6 +92,7 @@ enum ServiceArgType : uint32_t {
|
||||
SERVICE_ARG_TYPE_FLOAT_ARRAY = 6,
|
||||
SERVICE_ARG_TYPE_STRING_ARRAY = 7,
|
||||
};
|
||||
#ifdef USE_CLIMATE
|
||||
enum ClimateMode : uint32_t {
|
||||
CLIMATE_MODE_OFF = 0,
|
||||
CLIMATE_MODE_HEAT_COOL = 1,
|
||||
@ -127,11 +138,15 @@ enum ClimatePreset : uint32_t {
|
||||
CLIMATE_PRESET_SLEEP = 6,
|
||||
CLIMATE_PRESET_ACTIVITY = 7,
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_NUMBER
|
||||
enum NumberMode : uint32_t {
|
||||
NUMBER_MODE_AUTO = 0,
|
||||
NUMBER_MODE_BOX = 1,
|
||||
NUMBER_MODE_SLIDER = 2,
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_LOCK
|
||||
enum LockState : uint32_t {
|
||||
LOCK_STATE_NONE = 0,
|
||||
LOCK_STATE_LOCKED = 1,
|
||||
@ -145,6 +160,8 @@ enum LockCommand : uint32_t {
|
||||
LOCK_LOCK = 1,
|
||||
LOCK_OPEN = 2,
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_MEDIA_PLAYER
|
||||
enum MediaPlayerState : uint32_t {
|
||||
MEDIA_PLAYER_STATE_NONE = 0,
|
||||
MEDIA_PLAYER_STATE_IDLE = 1,
|
||||
@ -162,6 +179,8 @@ enum MediaPlayerFormatPurpose : uint32_t {
|
||||
MEDIA_PLAYER_FORMAT_PURPOSE_DEFAULT = 0,
|
||||
MEDIA_PLAYER_FORMAT_PURPOSE_ANNOUNCEMENT = 1,
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_BLUETOOTH_PROXY
|
||||
enum BluetoothDeviceRequestType : uint32_t {
|
||||
BLUETOOTH_DEVICE_REQUEST_TYPE_CONNECT = 0,
|
||||
BLUETOOTH_DEVICE_REQUEST_TYPE_DISCONNECT = 1,
|
||||
@ -183,6 +202,7 @@ enum BluetoothScannerMode : uint32_t {
|
||||
BLUETOOTH_SCANNER_MODE_PASSIVE = 0,
|
||||
BLUETOOTH_SCANNER_MODE_ACTIVE = 1,
|
||||
};
|
||||
#endif
|
||||
enum VoiceAssistantSubscribeFlag : uint32_t {
|
||||
VOICE_ASSISTANT_SUBSCRIBE_NONE = 0,
|
||||
VOICE_ASSISTANT_SUBSCRIBE_API_AUDIO = 1,
|
||||
@ -192,6 +212,7 @@ enum VoiceAssistantRequestFlag : uint32_t {
|
||||
VOICE_ASSISTANT_REQUEST_USE_VAD = 1,
|
||||
VOICE_ASSISTANT_REQUEST_USE_WAKE_WORD = 2,
|
||||
};
|
||||
#ifdef USE_VOICE_ASSISTANT
|
||||
enum VoiceAssistantEvent : uint32_t {
|
||||
VOICE_ASSISTANT_ERROR = 0,
|
||||
VOICE_ASSISTANT_RUN_START = 1,
|
||||
@ -216,6 +237,8 @@ enum VoiceAssistantTimerEvent : uint32_t {
|
||||
VOICE_ASSISTANT_TIMER_CANCELLED = 2,
|
||||
VOICE_ASSISTANT_TIMER_FINISHED = 3,
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_ALARM_CONTROL_PANEL
|
||||
enum AlarmControlPanelState : uint32_t {
|
||||
ALARM_STATE_DISARMED = 0,
|
||||
ALARM_STATE_ARMED_HOME = 1,
|
||||
@ -237,20 +260,27 @@ enum AlarmControlPanelStateCommand : uint32_t {
|
||||
ALARM_CONTROL_PANEL_ARM_CUSTOM_BYPASS = 5,
|
||||
ALARM_CONTROL_PANEL_TRIGGER = 6,
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_TEXT
|
||||
enum TextMode : uint32_t {
|
||||
TEXT_MODE_TEXT = 0,
|
||||
TEXT_MODE_PASSWORD = 1,
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_VALVE
|
||||
enum ValveOperation : uint32_t {
|
||||
VALVE_OPERATION_IDLE = 0,
|
||||
VALVE_OPERATION_IS_OPENING = 1,
|
||||
VALVE_OPERATION_IS_CLOSING = 2,
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_UPDATE
|
||||
enum UpdateCommand : uint32_t {
|
||||
UPDATE_COMMAND_NONE = 0,
|
||||
UPDATE_COMMAND_UPDATE = 1,
|
||||
UPDATE_COMMAND_CHECK = 2,
|
||||
};
|
||||
#endif
|
||||
|
||||
} // namespace enums
|
||||
|
||||
@ -273,6 +303,7 @@ class StateResponseProtoMessage : public ProtoMessage {
|
||||
public:
|
||||
~StateResponseProtoMessage() override = default;
|
||||
uint32_t key{0};
|
||||
uint32_t device_id{0};
|
||||
|
||||
protected:
|
||||
};
|
||||
@ -523,6 +554,7 @@ class SubscribeStatesRequest : public ProtoMessage {
|
||||
|
||||
protected:
|
||||
};
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
class ListEntitiesBinarySensorResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 12;
|
||||
@ -546,7 +578,7 @@ class ListEntitiesBinarySensorResponse : public InfoResponseProtoMessage {
|
||||
class BinarySensorStateResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 21;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 9;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 13;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "binary_sensor_state_response"; }
|
||||
#endif
|
||||
@ -562,6 +594,8 @@ class BinarySensorStateResponse : public StateResponseProtoMessage {
|
||||
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_COVER
|
||||
class ListEntitiesCoverResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 13;
|
||||
@ -588,7 +622,7 @@ class ListEntitiesCoverResponse : public InfoResponseProtoMessage {
|
||||
class CoverStateResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 22;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 19;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 23;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "cover_state_response"; }
|
||||
#endif
|
||||
@ -631,6 +665,8 @@ class CoverCommandRequest : public ProtoMessage {
|
||||
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_FAN
|
||||
class ListEntitiesFanResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 14;
|
||||
@ -657,7 +693,7 @@ class ListEntitiesFanResponse : public InfoResponseProtoMessage {
|
||||
class FanStateResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 23;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 26;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 30;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "fan_state_response"; }
|
||||
#endif
|
||||
@ -709,6 +745,8 @@ class FanCommandRequest : public ProtoMessage {
|
||||
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_LIGHT
|
||||
class ListEntitiesLightResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 15;
|
||||
@ -738,7 +776,7 @@ class ListEntitiesLightResponse : public InfoResponseProtoMessage {
|
||||
class LightStateResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 24;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 63;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 67;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "light_state_response"; }
|
||||
#endif
|
||||
@ -810,6 +848,8 @@ class LightCommandRequest : public ProtoMessage {
|
||||
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_SENSOR
|
||||
class ListEntitiesSensorResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 16;
|
||||
@ -837,7 +877,7 @@ class ListEntitiesSensorResponse : public InfoResponseProtoMessage {
|
||||
class SensorStateResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 25;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 12;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 16;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "sensor_state_response"; }
|
||||
#endif
|
||||
@ -853,6 +893,8 @@ class SensorStateResponse : public StateResponseProtoMessage {
|
||||
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_SWITCH
|
||||
class ListEntitiesSwitchResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 17;
|
||||
@ -876,7 +918,7 @@ class ListEntitiesSwitchResponse : public InfoResponseProtoMessage {
|
||||
class SwitchStateResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 26;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 7;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 11;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "switch_state_response"; }
|
||||
#endif
|
||||
@ -910,6 +952,8 @@ class SwitchCommandRequest : public ProtoMessage {
|
||||
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
class ListEntitiesTextSensorResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 18;
|
||||
@ -932,7 +976,7 @@ class ListEntitiesTextSensorResponse : public InfoResponseProtoMessage {
|
||||
class TextSensorStateResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 27;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 16;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 20;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "text_sensor_state_response"; }
|
||||
#endif
|
||||
@ -949,6 +993,7 @@ class TextSensorStateResponse : public StateResponseProtoMessage {
|
||||
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#endif
|
||||
class SubscribeLogsRequest : public ProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 28;
|
||||
@ -987,6 +1032,7 @@ class SubscribeLogsResponse : public ProtoMessage {
|
||||
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#ifdef USE_API_NOISE
|
||||
class NoiseEncryptionSetKeyRequest : public ProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 124;
|
||||
@ -1021,6 +1067,7 @@ class NoiseEncryptionSetKeyResponse : public ProtoMessage {
|
||||
protected:
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#endif
|
||||
class SubscribeHomeassistantServicesRequest : public ProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 34;
|
||||
@ -1226,6 +1273,7 @@ class ExecuteServiceRequest : public ProtoMessage {
|
||||
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
|
||||
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
|
||||
};
|
||||
#ifdef USE_CAMERA
|
||||
class ListEntitiesCameraResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 43;
|
||||
@ -1283,6 +1331,8 @@ class CameraImageRequest : public ProtoMessage {
|
||||
protected:
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_CLIMATE
|
||||
class ListEntitiesClimateResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 46;
|
||||
@ -1322,7 +1372,7 @@ class ListEntitiesClimateResponse : public InfoResponseProtoMessage {
|
||||
class ClimateStateResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 47;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 65;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 70;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "climate_state_response"; }
|
||||
#endif
|
||||
@ -1392,6 +1442,8 @@ class ClimateCommandRequest : public ProtoMessage {
|
||||
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_NUMBER
|
||||
class ListEntitiesNumberResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 49;
|
||||
@ -1419,7 +1471,7 @@ class ListEntitiesNumberResponse : public InfoResponseProtoMessage {
|
||||
class NumberStateResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 50;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 12;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 16;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "number_state_response"; }
|
||||
#endif
|
||||
@ -1453,6 +1505,8 @@ class NumberCommandRequest : public ProtoMessage {
|
||||
protected:
|
||||
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_SELECT
|
||||
class ListEntitiesSelectResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 52;
|
||||
@ -1475,7 +1529,7 @@ class ListEntitiesSelectResponse : public InfoResponseProtoMessage {
|
||||
class SelectStateResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 53;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 16;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 20;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "select_state_response"; }
|
||||
#endif
|
||||
@ -1511,6 +1565,8 @@ class SelectCommandRequest : public ProtoMessage {
|
||||
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
|
||||
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_SIREN
|
||||
class ListEntitiesSirenResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 55;
|
||||
@ -1535,7 +1591,7 @@ class ListEntitiesSirenResponse : public InfoResponseProtoMessage {
|
||||
class SirenStateResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 56;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 7;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 11;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "siren_state_response"; }
|
||||
#endif
|
||||
@ -1577,6 +1633,8 @@ class SirenCommandRequest : public ProtoMessage {
|
||||
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_LOCK
|
||||
class ListEntitiesLockResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 58;
|
||||
@ -1602,7 +1660,7 @@ class ListEntitiesLockResponse : public InfoResponseProtoMessage {
|
||||
class LockStateResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 59;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 7;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 11;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "lock_state_response"; }
|
||||
#endif
|
||||
@ -1639,6 +1697,8 @@ class LockCommandRequest : public ProtoMessage {
|
||||
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
class ListEntitiesButtonResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 61;
|
||||
@ -1675,6 +1735,8 @@ class ButtonCommandRequest : public ProtoMessage {
|
||||
protected:
|
||||
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_MEDIA_PLAYER
|
||||
class MediaPlayerSupportedFormat : public ProtoMessage {
|
||||
public:
|
||||
std::string format{};
|
||||
@ -1715,7 +1777,7 @@ class ListEntitiesMediaPlayerResponse : public InfoResponseProtoMessage {
|
||||
class MediaPlayerStateResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 64;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 14;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 18;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "media_player_state_response"; }
|
||||
#endif
|
||||
@ -1759,6 +1821,8 @@ class MediaPlayerCommandRequest : public ProtoMessage {
|
||||
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_BLUETOOTH_PROXY
|
||||
class SubscribeBluetoothLEAdvertisementsRequest : public ProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 66;
|
||||
@ -2313,6 +2377,8 @@ class BluetoothScannerSetModeRequest : public ProtoMessage {
|
||||
protected:
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_VOICE_ASSISTANT
|
||||
class SubscribeVoiceAssistantRequest : public ProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 89;
|
||||
@ -2562,6 +2628,8 @@ class VoiceAssistantSetConfiguration : public ProtoMessage {
|
||||
protected:
|
||||
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_ALARM_CONTROL_PANEL
|
||||
class ListEntitiesAlarmControlPanelResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 94;
|
||||
@ -2586,7 +2654,7 @@ class ListEntitiesAlarmControlPanelResponse : public InfoResponseProtoMessage {
|
||||
class AlarmControlPanelStateResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 95;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 7;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 11;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "alarm_control_panel_state_response"; }
|
||||
#endif
|
||||
@ -2622,6 +2690,8 @@ class AlarmControlPanelCommandRequest : public ProtoMessage {
|
||||
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_TEXT
|
||||
class ListEntitiesTextResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 97;
|
||||
@ -2647,7 +2717,7 @@ class ListEntitiesTextResponse : public InfoResponseProtoMessage {
|
||||
class TextStateResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 98;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 16;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 20;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "text_state_response"; }
|
||||
#endif
|
||||
@ -2683,6 +2753,8 @@ class TextCommandRequest : public ProtoMessage {
|
||||
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
|
||||
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_DATETIME_DATE
|
||||
class ListEntitiesDateResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 100;
|
||||
@ -2704,7 +2776,7 @@ class ListEntitiesDateResponse : public InfoResponseProtoMessage {
|
||||
class DateStateResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 101;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 19;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 23;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "date_state_response"; }
|
||||
#endif
|
||||
@ -2743,6 +2815,8 @@ class DateCommandRequest : public ProtoMessage {
|
||||
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_DATETIME_TIME
|
||||
class ListEntitiesTimeResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 103;
|
||||
@ -2764,7 +2838,7 @@ class ListEntitiesTimeResponse : public InfoResponseProtoMessage {
|
||||
class TimeStateResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 104;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 19;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 23;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "time_state_response"; }
|
||||
#endif
|
||||
@ -2803,6 +2877,8 @@ class TimeCommandRequest : public ProtoMessage {
|
||||
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_EVENT
|
||||
class ListEntitiesEventResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 107;
|
||||
@ -2826,7 +2902,7 @@ class ListEntitiesEventResponse : public InfoResponseProtoMessage {
|
||||
class EventResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 108;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 14;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 18;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "event_response"; }
|
||||
#endif
|
||||
@ -2840,7 +2916,10 @@ class EventResponse : public StateResponseProtoMessage {
|
||||
protected:
|
||||
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
|
||||
bool decode_length(uint32_t field_id, ProtoLengthDelimited value) override;
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_VALVE
|
||||
class ListEntitiesValveResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 109;
|
||||
@ -2866,7 +2945,7 @@ class ListEntitiesValveResponse : public InfoResponseProtoMessage {
|
||||
class ValveStateResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 110;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 12;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 16;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "valve_state_response"; }
|
||||
#endif
|
||||
@ -2903,6 +2982,8 @@ class ValveCommandRequest : public ProtoMessage {
|
||||
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_DATETIME_DATETIME
|
||||
class ListEntitiesDateTimeResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 112;
|
||||
@ -2924,7 +3005,7 @@ class ListEntitiesDateTimeResponse : public InfoResponseProtoMessage {
|
||||
class DateTimeStateResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 113;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 12;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 16;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "date_time_state_response"; }
|
||||
#endif
|
||||
@ -2958,6 +3039,8 @@ class DateTimeCommandRequest : public ProtoMessage {
|
||||
protected:
|
||||
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
|
||||
};
|
||||
#endif
|
||||
#ifdef USE_UPDATE
|
||||
class ListEntitiesUpdateResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 116;
|
||||
@ -2980,7 +3063,7 @@ class ListEntitiesUpdateResponse : public InfoResponseProtoMessage {
|
||||
class UpdateStateResponse : public StateResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 117;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 61;
|
||||
static constexpr uint16_t ESTIMATED_SIZE = 65;
|
||||
#ifdef HAS_PROTO_MESSAGE_DUMP
|
||||
const char *message_name() const override { return "update_state_response"; }
|
||||
#endif
|
||||
@ -3023,6 +3106,7 @@ class UpdateCommandRequest : public ProtoMessage {
|
||||
bool decode_32bit(uint32_t field_id, Proto32Bit value) override;
|
||||
bool decode_varint(uint32_t field_id, ProtoVarInt value) override;
|
||||
};
|
||||
#endif
|
||||
|
||||
} // namespace api
|
||||
} // namespace esphome
|
||||
|
4333
esphome/components/api/api_pb2_dump.cpp
Normal file
4333
esphome/components/api/api_pb2_dump.cpp
Normal file
File diff suppressed because it is too large
Load Diff
@ -204,7 +204,7 @@ void APIServerConnectionBase::read_message(uint32_t msg_size, uint32_t msg_type,
|
||||
this->on_execute_service_request(msg);
|
||||
break;
|
||||
}
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
#ifdef USE_CAMERA
|
||||
case 45: {
|
||||
CameraImageRequest msg;
|
||||
msg.decode(msg_data, msg_size);
|
||||
@ -682,7 +682,7 @@ void APIServerConnection::on_button_command_request(const ButtonCommandRequest &
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
#ifdef USE_CAMERA
|
||||
void APIServerConnection::on_camera_image_request(const CameraImageRequest &msg) {
|
||||
if (this->check_authenticated_()) {
|
||||
this->camera_image(msg);
|
||||
|
@ -2,9 +2,10 @@
|
||||
// See script/api_protobuf/api_protobuf.py
|
||||
#pragma once
|
||||
|
||||
#include "api_pb2.h"
|
||||
#include "esphome/core/defines.h"
|
||||
|
||||
#include "api_pb2.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace api {
|
||||
|
||||
@ -70,7 +71,7 @@ class APIServerConnectionBase : public ProtoService {
|
||||
|
||||
virtual void on_execute_service_request(const ExecuteServiceRequest &value){};
|
||||
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
#ifdef USE_CAMERA
|
||||
virtual void on_camera_image_request(const CameraImageRequest &value){};
|
||||
#endif
|
||||
|
||||
@ -222,7 +223,7 @@ class APIServerConnection : public APIServerConnectionBase {
|
||||
#ifdef USE_BUTTON
|
||||
virtual void button_command(const ButtonCommandRequest &msg) = 0;
|
||||
#endif
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
#ifdef USE_CAMERA
|
||||
virtual void camera_image(const CameraImageRequest &msg) = 0;
|
||||
#endif
|
||||
#ifdef USE_CLIMATE
|
||||
@ -339,7 +340,7 @@ class APIServerConnection : public APIServerConnectionBase {
|
||||
#ifdef USE_BUTTON
|
||||
void on_button_command_request(const ButtonCommandRequest &msg) override;
|
||||
#endif
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
#ifdef USE_CAMERA
|
||||
void on_camera_image_request(const CameraImageRequest &msg) override;
|
||||
#endif
|
||||
#ifdef USE_CLIMATE
|
||||
|
@ -24,6 +24,14 @@ static const char *const TAG = "api";
|
||||
// APIServer
|
||||
APIServer *global_api_server = nullptr; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
#ifndef USE_API_YAML_SERVICES
|
||||
// Global empty vector to avoid guard variables (saves 8 bytes)
|
||||
// This is initialized at program startup before any threads
|
||||
static const std::vector<UserServiceDescriptor *> empty_user_services{};
|
||||
|
||||
const std::vector<UserServiceDescriptor *> &get_empty_user_services_instance() { return empty_user_services; }
|
||||
#endif
|
||||
|
||||
APIServer::APIServer() {
|
||||
global_api_server = this;
|
||||
// Pre-allocate shared write buffer
|
||||
@ -111,15 +119,14 @@ void APIServer::setup() {
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
if (esp32_camera::global_esp32_camera != nullptr && !esp32_camera::global_esp32_camera->is_internal()) {
|
||||
esp32_camera::global_esp32_camera->add_image_callback(
|
||||
[this](const std::shared_ptr<esp32_camera::CameraImage> &image) {
|
||||
for (auto &c : this->clients_) {
|
||||
if (!c->flags_.remove)
|
||||
c->set_camera_state(image);
|
||||
}
|
||||
});
|
||||
#ifdef USE_CAMERA
|
||||
if (camera::Camera::instance() != nullptr && !camera::Camera::instance()->is_internal()) {
|
||||
camera::Camera::instance()->add_image_callback([this](const std::shared_ptr<camera::CameraImage> &image) {
|
||||
for (auto &c : this->clients_) {
|
||||
if (!c->flags_.remove)
|
||||
c->set_camera_state(image);
|
||||
}
|
||||
});
|
||||
}
|
||||
#endif
|
||||
}
|
||||
@ -218,6 +225,7 @@ void APIServer::dump_config() {
|
||||
#endif
|
||||
}
|
||||
|
||||
#ifdef USE_API_PASSWORD
|
||||
bool APIServer::uses_password() const { return !this->password_.empty(); }
|
||||
|
||||
bool APIServer::check_password(const std::string &password) const {
|
||||
@ -248,6 +256,7 @@ bool APIServer::check_password(const std::string &password) const {
|
||||
|
||||
return result == 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
void APIServer::handle_disconnect(APIConnection *conn) {}
|
||||
|
||||
@ -431,7 +440,9 @@ float APIServer::get_setup_priority() const { return setup_priority::AFTER_WIFI;
|
||||
|
||||
void APIServer::set_port(uint16_t port) { this->port_ = port; }
|
||||
|
||||
#ifdef USE_API_PASSWORD
|
||||
void APIServer::set_password(const std::string &password) { this->password_ = password; }
|
||||
#endif
|
||||
|
||||
void APIServer::set_batch_delay(uint16_t batch_delay) { this->batch_delay_ = batch_delay; }
|
||||
|
||||
|
@ -25,6 +25,11 @@ struct SavedNoisePsk {
|
||||
} PACKED; // NOLINT
|
||||
#endif
|
||||
|
||||
#ifndef USE_API_YAML_SERVICES
|
||||
// Forward declaration of helper function
|
||||
const std::vector<UserServiceDescriptor *> &get_empty_user_services_instance();
|
||||
#endif
|
||||
|
||||
class APIServer : public Component, public Controller {
|
||||
public:
|
||||
APIServer();
|
||||
@ -35,10 +40,12 @@ class APIServer : public Component, public Controller {
|
||||
void dump_config() override;
|
||||
void on_shutdown() override;
|
||||
bool teardown() override;
|
||||
#ifdef USE_API_PASSWORD
|
||||
bool check_password(const std::string &password) const;
|
||||
bool uses_password() const;
|
||||
void set_port(uint16_t port);
|
||||
void set_password(const std::string &password);
|
||||
#endif
|
||||
void set_port(uint16_t port);
|
||||
void set_reboot_timeout(uint32_t reboot_timeout);
|
||||
void set_batch_delay(uint16_t batch_delay);
|
||||
uint16_t get_batch_delay() const { return batch_delay_; }
|
||||
@ -149,8 +156,11 @@ class APIServer : public Component, public Controller {
|
||||
#ifdef USE_API_YAML_SERVICES
|
||||
return this->user_services_;
|
||||
#else
|
||||
static const std::vector<UserServiceDescriptor *> EMPTY;
|
||||
return this->user_services_ ? *this->user_services_ : EMPTY;
|
||||
if (this->user_services_) {
|
||||
return *this->user_services_;
|
||||
}
|
||||
// Return reference to global empty instance (no guard needed)
|
||||
return get_empty_user_services_instance();
|
||||
#endif
|
||||
}
|
||||
|
||||
@ -179,7 +189,9 @@ class APIServer : public Component, public Controller {
|
||||
|
||||
// Vectors and strings (12 bytes each on 32-bit)
|
||||
std::vector<std::unique_ptr<APIConnection>> clients_;
|
||||
#ifdef USE_API_PASSWORD
|
||||
std::string password_;
|
||||
#endif
|
||||
std::vector<uint8_t> shared_write_buffer_; // Shared proto write buffer for all connections
|
||||
std::vector<HomeAssistantStateSubscription> state_subs_;
|
||||
#ifdef USE_API_YAML_SERVICES
|
||||
|
@ -40,8 +40,8 @@ LIST_ENTITIES_HANDLER(lock, lock::Lock, ListEntitiesLockResponse)
|
||||
#ifdef USE_VALVE
|
||||
LIST_ENTITIES_HANDLER(valve, valve::Valve, ListEntitiesValveResponse)
|
||||
#endif
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
LIST_ENTITIES_HANDLER(camera, esp32_camera::ESP32Camera, ListEntitiesCameraResponse)
|
||||
#ifdef USE_CAMERA
|
||||
LIST_ENTITIES_HANDLER(camera, camera::Camera, ListEntitiesCameraResponse)
|
||||
#endif
|
||||
#ifdef USE_CLIMATE
|
||||
LIST_ENTITIES_HANDLER(climate, climate::Climate, ListEntitiesClimateResponse)
|
||||
|
@ -45,8 +45,8 @@ class ListEntitiesIterator : public ComponentIterator {
|
||||
bool on_text_sensor(text_sensor::TextSensor *entity) override;
|
||||
#endif
|
||||
bool on_service(UserServiceDescriptor *service) override;
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
bool on_camera(esp32_camera::ESP32Camera *entity) override;
|
||||
#ifdef USE_CAMERA
|
||||
bool on_camera(camera::Camera *entity) override;
|
||||
#endif
|
||||
#ifdef USE_CLIMATE
|
||||
bool on_climate(climate::Climate *entity) override;
|
||||
|
@ -170,7 +170,7 @@ int BluetoothProxy::get_bluetooth_connections_free() {
|
||||
void BluetoothProxy::loop() {
|
||||
if (!api::global_api_server->is_connected() || this->api_connection_ == nullptr) {
|
||||
for (auto *connection : this->connections_) {
|
||||
if (connection->get_address() != 0) {
|
||||
if (connection->get_address() != 0 && !connection->disconnect_pending()) {
|
||||
connection->disconnect();
|
||||
}
|
||||
}
|
||||
|
1
esphome/components/camera/__init__.py
Normal file
1
esphome/components/camera/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
CODEOWNERS = ["@DT-art1", "@bdraco"]
|
22
esphome/components/camera/camera.cpp
Normal file
22
esphome/components/camera/camera.cpp
Normal file
@ -0,0 +1,22 @@
|
||||
#include "camera.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace camera {
|
||||
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
Camera *Camera::global_camera = nullptr;
|
||||
|
||||
Camera::Camera() {
|
||||
if (global_camera != nullptr) {
|
||||
this->status_set_error("Multiple cameras are configured, but only one is supported.");
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
global_camera = this;
|
||||
}
|
||||
|
||||
Camera *Camera::instance() { return global_camera; }
|
||||
|
||||
} // namespace camera
|
||||
} // namespace esphome
|
80
esphome/components/camera/camera.h
Normal file
80
esphome/components/camera/camera.h
Normal file
@ -0,0 +1,80 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/entity_base.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace camera {
|
||||
|
||||
/** Different sources for filtering.
|
||||
* IDLE: Camera requests to send an image to the API.
|
||||
* API_REQUESTER: API requests a new image.
|
||||
* WEB_REQUESTER: ESP32 web server request an image. Ignored by API.
|
||||
*/
|
||||
enum CameraRequester : uint8_t { IDLE, API_REQUESTER, WEB_REQUESTER };
|
||||
|
||||
/** Abstract camera image base class.
|
||||
* Encapsulates the JPEG encoded data and it is shared among
|
||||
* all connected clients.
|
||||
*/
|
||||
class CameraImage {
|
||||
public:
|
||||
virtual uint8_t *get_data_buffer() = 0;
|
||||
virtual size_t get_data_length() = 0;
|
||||
virtual bool was_requested_by(CameraRequester requester) const = 0;
|
||||
virtual ~CameraImage() {}
|
||||
};
|
||||
|
||||
/** Abstract image reader base class.
|
||||
* Keeps track of the data offset of the camera image and
|
||||
* how many bytes are remaining to read. When the image
|
||||
* is returned, the shared_ptr is reset and the camera can
|
||||
* reuse the memory of the camera image.
|
||||
*/
|
||||
class CameraImageReader {
|
||||
public:
|
||||
virtual void set_image(std::shared_ptr<CameraImage> image) = 0;
|
||||
virtual size_t available() const = 0;
|
||||
virtual uint8_t *peek_data_buffer() = 0;
|
||||
virtual void consume_data(size_t consumed) = 0;
|
||||
virtual void return_image() = 0;
|
||||
virtual ~CameraImageReader() {}
|
||||
};
|
||||
|
||||
/** Abstract camera base class. Collaborates with API.
|
||||
* 1) API server starts and installs callback (add_image_callback)
|
||||
* which is called by the camera when a new image is available.
|
||||
* 2) New API client connects and creates a new image reader (create_image_reader).
|
||||
* 3) API connection receives protobuf CameraImageRequest and calls request_image.
|
||||
* 3.a) API connection receives protobuf CameraImageRequest and calls start_stream.
|
||||
* 4) Camera implementation provides JPEG data in the CameraImage and calls callback.
|
||||
* 5) API connection sets the image in the image reader.
|
||||
* 6) API connection consumes data from the image reader and returns the image when finished.
|
||||
* 7.a) Camera captures a new image and continues with 4) until start_stream is called.
|
||||
*/
|
||||
class Camera : public EntityBase, public Component {
|
||||
public:
|
||||
Camera();
|
||||
// Camera implementation invokes callback to publish a new image.
|
||||
virtual void add_image_callback(std::function<void(std::shared_ptr<CameraImage>)> &&callback) = 0;
|
||||
/// Returns a new camera image reader that keeps track of the JPEG data in the camera image.
|
||||
virtual CameraImageReader *create_image_reader() = 0;
|
||||
// Connection, camera or web server requests one new JPEG image.
|
||||
virtual void request_image(CameraRequester requester) = 0;
|
||||
// Connection, camera or web server requests a stream of images.
|
||||
virtual void start_stream(CameraRequester requester) = 0;
|
||||
// Connection or web server stops the previously started stream.
|
||||
virtual void stop_stream(CameraRequester requester) = 0;
|
||||
virtual ~Camera() {}
|
||||
/// The singleton instance of the camera implementation.
|
||||
static Camera *instance();
|
||||
|
||||
protected:
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
static Camera *global_camera;
|
||||
};
|
||||
|
||||
} // namespace camera
|
||||
} // namespace esphome
|
1
esphome/components/ds2484/__init__.py
Normal file
1
esphome/components/ds2484/__init__.py
Normal file
@ -0,0 +1 @@
|
||||
CODEOWNERS = ["@mrk-its"]
|
209
esphome/components/ds2484/ds2484.cpp
Normal file
209
esphome/components/ds2484/ds2484.cpp
Normal file
@ -0,0 +1,209 @@
|
||||
#include "ds2484.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ds2484 {
|
||||
static const char *const TAG = "ds2484.onewire";
|
||||
|
||||
void DS2484OneWireBus::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Running setup");
|
||||
this->reset_device();
|
||||
this->search();
|
||||
}
|
||||
|
||||
void DS2484OneWireBus::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "1-wire bus:");
|
||||
this->dump_devices_(TAG);
|
||||
}
|
||||
|
||||
bool DS2484OneWireBus::read_status_(uint8_t *status) {
|
||||
for (uint8_t retry_nr = 0; retry_nr < 10; retry_nr++) {
|
||||
if (this->read(status, 1) != i2c::ERROR_OK) {
|
||||
ESP_LOGE(TAG, "read status error");
|
||||
return false;
|
||||
}
|
||||
ESP_LOGVV(TAG, "status: %02x", *status);
|
||||
if (!(*status & 1)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
ESP_LOGE(TAG, "read status error: too many retries");
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DS2484OneWireBus::wait_for_completion_() {
|
||||
uint8_t status;
|
||||
return this->read_status_(&status);
|
||||
}
|
||||
|
||||
bool DS2484OneWireBus::reset_device() {
|
||||
ESP_LOGVV(TAG, "reset_device");
|
||||
uint8_t device_reset_cmd = 0xf0;
|
||||
uint8_t response;
|
||||
if (this->write(&device_reset_cmd, 1) != i2c::ERROR_OK) {
|
||||
return false;
|
||||
}
|
||||
if (!this->wait_for_completion_()) {
|
||||
ESP_LOGE(TAG, "reset_device: can't complete");
|
||||
return false;
|
||||
}
|
||||
uint8_t config = (this->active_pullup_ ? 1 : 0) | (this->strong_pullup_ ? 4 : 0);
|
||||
uint8_t write_config[2] = {0xd2, (uint8_t) (config | (~config << 4))};
|
||||
if (this->write(write_config, 2) != i2c::ERROR_OK) {
|
||||
ESP_LOGE(TAG, "reset_device: can't write config");
|
||||
return false;
|
||||
}
|
||||
if (this->read(&response, 1) != i2c::ERROR_OK) {
|
||||
ESP_LOGE(TAG, "can't read read8 response");
|
||||
return false;
|
||||
}
|
||||
if (response != (write_config[1] & 0xf)) {
|
||||
ESP_LOGE(TAG, "configuration didn't update");
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
};
|
||||
|
||||
int DS2484OneWireBus::reset_int() {
|
||||
ESP_LOGVV(TAG, "reset");
|
||||
uint8_t reset_cmd = 0xb4;
|
||||
if (this->write(&reset_cmd, 1) != i2c::ERROR_OK) {
|
||||
return -1;
|
||||
}
|
||||
return this->wait_for_completion_() ? 1 : 0;
|
||||
};
|
||||
|
||||
void DS2484OneWireBus::write8_(uint8_t value) {
|
||||
uint8_t buffer[2] = {0xa5, value};
|
||||
this->write(buffer, 2);
|
||||
this->wait_for_completion_();
|
||||
};
|
||||
|
||||
void DS2484OneWireBus::write8(uint8_t value) {
|
||||
ESP_LOGVV(TAG, "write8: %02x", value);
|
||||
this->write8_(value);
|
||||
};
|
||||
|
||||
void DS2484OneWireBus::write64(uint64_t value) {
|
||||
ESP_LOGVV(TAG, "write64: %llx", value);
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
this->write8_((value >> (i * 8)) & 0xff);
|
||||
}
|
||||
}
|
||||
|
||||
uint8_t DS2484OneWireBus::read8() {
|
||||
uint8_t read8_cmd = 0x96;
|
||||
uint8_t set_read_reg_cmd[2] = {0xe1, 0xe1};
|
||||
uint8_t response = 0;
|
||||
if (this->write(&read8_cmd, 1) != i2c::ERROR_OK) {
|
||||
ESP_LOGE(TAG, "can't write read8 cmd");
|
||||
return 0;
|
||||
}
|
||||
this->wait_for_completion_();
|
||||
if (this->write(set_read_reg_cmd, 2) != i2c::ERROR_OK) {
|
||||
ESP_LOGE(TAG, "can't set read data reg");
|
||||
return 0;
|
||||
}
|
||||
if (this->read(&response, 1) != i2c::ERROR_OK) {
|
||||
ESP_LOGE(TAG, "can't read read8 response");
|
||||
return 0;
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
uint64_t DS2484OneWireBus::read64() {
|
||||
uint8_t response = 0;
|
||||
for (uint8_t i = 0; i < 8; i++) {
|
||||
response |= (this->read8() << (i * 8));
|
||||
}
|
||||
return response;
|
||||
}
|
||||
|
||||
void DS2484OneWireBus::reset_search() {
|
||||
this->last_discrepancy_ = 0;
|
||||
this->last_device_flag_ = false;
|
||||
this->address_ = 0;
|
||||
}
|
||||
|
||||
bool DS2484OneWireBus::one_wire_triple_(bool *branch, bool *id_bit, bool *cmp_id_bit) {
|
||||
uint8_t buffer[2] = {(uint8_t) 0x78, (uint8_t) (*branch ? 0x80u : 0)};
|
||||
uint8_t status;
|
||||
if (!this->read_status_(&status)) {
|
||||
ESP_LOGE(TAG, "one_wire_triple start: read status error");
|
||||
return false;
|
||||
}
|
||||
if (this->write(buffer, 2) != i2c::ERROR_OK) {
|
||||
ESP_LOGV(TAG, "one_wire_triple: can't write cmd");
|
||||
return false;
|
||||
}
|
||||
if (!this->read_status_(&status)) {
|
||||
ESP_LOGE(TAG, "one_wire_triple: read status error");
|
||||
return false;
|
||||
}
|
||||
*id_bit = bool(status & 0x20);
|
||||
*cmp_id_bit = bool(status & 0x40);
|
||||
*branch = bool(status & 0x80);
|
||||
return true;
|
||||
}
|
||||
|
||||
uint64_t IRAM_ATTR DS2484OneWireBus::search_int() {
|
||||
ESP_LOGVV(TAG, "search_int");
|
||||
if (this->last_device_flag_) {
|
||||
ESP_LOGVV(TAG, "last device flag set, quitting");
|
||||
return 0u;
|
||||
}
|
||||
|
||||
uint8_t last_zero = 0;
|
||||
uint64_t bit_mask = 1;
|
||||
uint64_t address = this->address_;
|
||||
|
||||
// Initiate search
|
||||
for (uint8_t bit_number = 1; bit_number <= 64; bit_number++, bit_mask <<= 1) {
|
||||
bool branch;
|
||||
|
||||
// compute branch value for the case when there is a discrepancy
|
||||
// (there are devices with both 0s and 1s at this bit)
|
||||
if (bit_number < this->last_discrepancy_) {
|
||||
branch = (address & bit_mask) > 0;
|
||||
} else {
|
||||
branch = bit_number == this->last_discrepancy_;
|
||||
}
|
||||
|
||||
bool id_bit, cmp_id_bit;
|
||||
bool branch_before = branch;
|
||||
if (!this->one_wire_triple_(&branch, &id_bit, &cmp_id_bit)) {
|
||||
ESP_LOGW(TAG, "one wire triple error, quitting");
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (id_bit && cmp_id_bit) {
|
||||
ESP_LOGW(TAG, "no devices on the bus, quitting");
|
||||
// No devices participating in search
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (!id_bit && !cmp_id_bit && !branch) {
|
||||
last_zero = bit_number;
|
||||
}
|
||||
|
||||
ESP_LOGVV(TAG, "%d %d branch: %d %d", id_bit, cmp_id_bit, branch_before, branch);
|
||||
|
||||
if (branch) {
|
||||
address |= bit_mask;
|
||||
} else {
|
||||
address &= ~bit_mask;
|
||||
}
|
||||
}
|
||||
ESP_LOGVV(TAG, "last_discepancy: %d", last_zero);
|
||||
ESP_LOGVV(TAG, "address: %llx", address);
|
||||
this->last_discrepancy_ = last_zero;
|
||||
if (this->last_discrepancy_ == 0) {
|
||||
// we're at root and have no choices left, so this was the last one.
|
||||
this->last_device_flag_ = true;
|
||||
}
|
||||
|
||||
this->address_ = address;
|
||||
return address;
|
||||
}
|
||||
|
||||
} // namespace ds2484
|
||||
} // namespace esphome
|
43
esphome/components/ds2484/ds2484.h
Normal file
43
esphome/components/ds2484/ds2484.h
Normal file
@ -0,0 +1,43 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/preferences.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
#include "esphome/components/one_wire/one_wire.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ds2484 {
|
||||
|
||||
class DS2484OneWireBus : public one_wire::OneWireBus, public i2c::I2CDevice, public Component {
|
||||
public:
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override { return setup_priority::BUS - 1.0; }
|
||||
|
||||
bool reset_device();
|
||||
int reset_int() override;
|
||||
void write8(uint8_t) override;
|
||||
void write64(uint64_t) override;
|
||||
uint8_t read8() override;
|
||||
uint64_t read64() override;
|
||||
|
||||
void set_active_pullup(bool value) { this->active_pullup_ = value; }
|
||||
void set_strong_pullup(bool value) { this->strong_pullup_ = value; }
|
||||
|
||||
protected:
|
||||
void reset_search() override;
|
||||
uint64_t search_int() override;
|
||||
bool read_status_(uint8_t *);
|
||||
bool wait_for_completion_();
|
||||
void write8_(uint8_t);
|
||||
bool one_wire_triple_(bool *branch, bool *id_bit, bool *cmp_id_bit);
|
||||
|
||||
uint64_t address_;
|
||||
uint8_t last_discrepancy_{0};
|
||||
bool last_device_flag_{false};
|
||||
bool active_pullup_{false};
|
||||
bool strong_pullup_{false};
|
||||
};
|
||||
} // namespace ds2484
|
||||
} // namespace esphome
|
37
esphome/components/ds2484/one_wire.py
Normal file
37
esphome/components/ds2484/one_wire.py
Normal file
@ -0,0 +1,37 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import i2c
|
||||
from esphome.components.one_wire import OneWireBus
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ID
|
||||
|
||||
ds2484_ns = cg.esphome_ns.namespace("ds2484")
|
||||
|
||||
CONF_ACTIVE_PULLUP = "active_pullup"
|
||||
CONF_STRONG_PULLUP = "strong_pullup"
|
||||
|
||||
CODEOWNERS = ["@mrk-its"]
|
||||
DEPENDENCIES = ["i2c"]
|
||||
|
||||
DS2484OneWireBus = ds2484_ns.class_(
|
||||
"DS2484OneWireBus", OneWireBus, i2c.I2CDevice, cg.Component
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(DS2484OneWireBus),
|
||||
cv.Optional(CONF_ACTIVE_PULLUP, default=False): cv.boolean,
|
||||
cv.Optional(CONF_STRONG_PULLUP, default=False): cv.boolean,
|
||||
}
|
||||
)
|
||||
.extend(cv.COMPONENT_SCHEMA)
|
||||
.extend(i2c.i2c_device_schema(0x18))
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await i2c.register_i2c_device(var, config)
|
||||
await cg.register_component(var, config)
|
||||
cg.add(var.set_active_pullup(config[CONF_ACTIVE_PULLUP]))
|
||||
cg.add(var.set_strong_pullup(config[CONF_STRONG_PULLUP]))
|
@ -51,7 +51,7 @@ enum IoCapability {
|
||||
IO_CAP_KBDISP = ESP_IO_CAP_KBDISP,
|
||||
};
|
||||
|
||||
enum BLEComponentState {
|
||||
enum BLEComponentState : uint8_t {
|
||||
/** Nothing has been initialized yet. */
|
||||
BLE_COMPONENT_STATE_OFF = 0,
|
||||
/** BLE should be disabled on next loop. */
|
||||
@ -141,21 +141,31 @@ class ESP32BLE : public Component {
|
||||
private:
|
||||
template<typename... Args> friend void enqueue_ble_event(Args... args);
|
||||
|
||||
// Vectors (12 bytes each on 32-bit, naturally aligned to 4 bytes)
|
||||
std::vector<GAPEventHandler *> gap_event_handlers_;
|
||||
std::vector<GAPScanEventHandler *> gap_scan_event_handlers_;
|
||||
std::vector<GATTcEventHandler *> gattc_event_handlers_;
|
||||
std::vector<GATTsEventHandler *> gatts_event_handlers_;
|
||||
std::vector<BLEStatusEventHandler *> ble_status_event_handlers_;
|
||||
BLEComponentState state_{BLE_COMPONENT_STATE_OFF};
|
||||
|
||||
// Large objects (size depends on template parameters, but typically aligned to 4 bytes)
|
||||
esphome::LockFreeQueue<BLEEvent, MAX_BLE_QUEUE_SIZE> ble_events_;
|
||||
esphome::EventPool<BLEEvent, MAX_BLE_QUEUE_SIZE> ble_event_pool_;
|
||||
BLEAdvertising *advertising_{};
|
||||
esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE};
|
||||
uint32_t advertising_cycle_time_{};
|
||||
bool enable_on_boot_{};
|
||||
|
||||
// optional<string> (typically 16+ bytes on 32-bit, aligned to 4 bytes)
|
||||
optional<std::string> name_;
|
||||
uint16_t appearance_{0};
|
||||
|
||||
// 4-byte aligned members
|
||||
BLEAdvertising *advertising_{}; // 4 bytes (pointer)
|
||||
esp_ble_io_cap_t io_cap_{ESP_IO_CAP_NONE}; // 4 bytes (enum)
|
||||
uint32_t advertising_cycle_time_{}; // 4 bytes
|
||||
|
||||
// 2-byte aligned members
|
||||
uint16_t appearance_{0}; // 2 bytes
|
||||
|
||||
// 1-byte aligned members (grouped together to minimize padding)
|
||||
BLEComponentState state_{BLE_COMPONENT_STATE_OFF}; // 1 byte (uint8_t enum)
|
||||
bool enable_on_boot_{}; // 1 byte
|
||||
};
|
||||
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
@ -23,7 +23,7 @@ from esphome.core.entity_helpers import setup_entity
|
||||
|
||||
DEPENDENCIES = ["esp32"]
|
||||
|
||||
AUTO_LOAD = ["psram"]
|
||||
AUTO_LOAD = ["camera", "psram"]
|
||||
|
||||
esp32_camera_ns = cg.esphome_ns.namespace("esp32_camera")
|
||||
ESP32Camera = esp32_camera_ns.class_("ESP32Camera", cg.PollingComponent, cg.EntityBase)
|
||||
@ -283,6 +283,7 @@ SETTERS = {
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
cg.add_define("USE_CAMERA")
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await setup_entity(var, config, "camera")
|
||||
await cg.register_component(var, config)
|
||||
|
@ -14,8 +14,6 @@ static const char *const TAG = "esp32_camera";
|
||||
|
||||
/* ---------------- public API (derivated) ---------------- */
|
||||
void ESP32Camera::setup() {
|
||||
global_esp32_camera = this;
|
||||
|
||||
#ifdef USE_I2C
|
||||
if (this->i2c_bus_ != nullptr) {
|
||||
this->config_.sccb_i2c_port = this->i2c_bus_->get_port();
|
||||
@ -43,7 +41,7 @@ void ESP32Camera::setup() {
|
||||
xTaskCreatePinnedToCore(&ESP32Camera::framebuffer_task,
|
||||
"framebuffer_task", // name
|
||||
1024, // stack size
|
||||
nullptr, // task pv params
|
||||
this, // task pv params
|
||||
1, // priority
|
||||
nullptr, // handle
|
||||
1 // core
|
||||
@ -176,7 +174,7 @@ void ESP32Camera::loop() {
|
||||
const uint32_t now = App.get_loop_component_start_time();
|
||||
if (this->idle_update_interval_ != 0 && now - this->last_idle_request_ > this->idle_update_interval_) {
|
||||
this->last_idle_request_ = now;
|
||||
this->request_image(IDLE);
|
||||
this->request_image(camera::IDLE);
|
||||
}
|
||||
|
||||
// Check if we should fetch a new image
|
||||
@ -202,7 +200,7 @@ void ESP32Camera::loop() {
|
||||
xQueueSend(this->framebuffer_return_queue_, &fb, portMAX_DELAY);
|
||||
return;
|
||||
}
|
||||
this->current_image_ = std::make_shared<CameraImage>(fb, this->single_requesters_ | this->stream_requesters_);
|
||||
this->current_image_ = std::make_shared<ESP32CameraImage>(fb, this->single_requesters_ | this->stream_requesters_);
|
||||
|
||||
ESP_LOGD(TAG, "Got Image: len=%u", fb->len);
|
||||
this->new_image_callback_.call(this->current_image_);
|
||||
@ -225,8 +223,6 @@ ESP32Camera::ESP32Camera() {
|
||||
this->config_.fb_count = 1;
|
||||
this->config_.grab_mode = CAMERA_GRAB_WHEN_EMPTY;
|
||||
this->config_.fb_location = CAMERA_FB_IN_PSRAM;
|
||||
|
||||
global_esp32_camera = this;
|
||||
}
|
||||
|
||||
/* ---------------- setters ---------------- */
|
||||
@ -356,7 +352,7 @@ void ESP32Camera::set_frame_buffer_count(uint8_t fb_count) {
|
||||
}
|
||||
|
||||
/* ---------------- public API (specific) ---------------- */
|
||||
void ESP32Camera::add_image_callback(std::function<void(std::shared_ptr<CameraImage>)> &&callback) {
|
||||
void ESP32Camera::add_image_callback(std::function<void(std::shared_ptr<camera::CameraImage>)> &&callback) {
|
||||
this->new_image_callback_.add(std::move(callback));
|
||||
}
|
||||
void ESP32Camera::add_stream_start_callback(std::function<void()> &&callback) {
|
||||
@ -365,15 +361,16 @@ void ESP32Camera::add_stream_start_callback(std::function<void()> &&callback) {
|
||||
void ESP32Camera::add_stream_stop_callback(std::function<void()> &&callback) {
|
||||
this->stream_stop_callback_.add(std::move(callback));
|
||||
}
|
||||
void ESP32Camera::start_stream(CameraRequester requester) {
|
||||
void ESP32Camera::start_stream(camera::CameraRequester requester) {
|
||||
this->stream_start_callback_.call();
|
||||
this->stream_requesters_ |= (1U << requester);
|
||||
}
|
||||
void ESP32Camera::stop_stream(CameraRequester requester) {
|
||||
void ESP32Camera::stop_stream(camera::CameraRequester requester) {
|
||||
this->stream_stop_callback_.call();
|
||||
this->stream_requesters_ &= ~(1U << requester);
|
||||
}
|
||||
void ESP32Camera::request_image(CameraRequester requester) { this->single_requesters_ |= (1U << requester); }
|
||||
void ESP32Camera::request_image(camera::CameraRequester requester) { this->single_requesters_ |= (1U << requester); }
|
||||
camera::CameraImageReader *ESP32Camera::create_image_reader() { return new ESP32CameraImageReader; }
|
||||
void ESP32Camera::update_camera_parameters() {
|
||||
sensor_t *s = esp_camera_sensor_get();
|
||||
/* update image */
|
||||
@ -402,39 +399,39 @@ void ESP32Camera::update_camera_parameters() {
|
||||
bool ESP32Camera::has_requested_image_() const { return this->single_requesters_ || this->stream_requesters_; }
|
||||
bool ESP32Camera::can_return_image_() const { return this->current_image_.use_count() == 1; }
|
||||
void ESP32Camera::framebuffer_task(void *pv) {
|
||||
ESP32Camera *that = (ESP32Camera *) pv;
|
||||
while (true) {
|
||||
camera_fb_t *framebuffer = esp_camera_fb_get();
|
||||
xQueueSend(global_esp32_camera->framebuffer_get_queue_, &framebuffer, portMAX_DELAY);
|
||||
xQueueSend(that->framebuffer_get_queue_, &framebuffer, portMAX_DELAY);
|
||||
// return is no-op for config with 1 fb
|
||||
xQueueReceive(global_esp32_camera->framebuffer_return_queue_, &framebuffer, portMAX_DELAY);
|
||||
xQueueReceive(that->framebuffer_return_queue_, &framebuffer, portMAX_DELAY);
|
||||
esp_camera_fb_return(framebuffer);
|
||||
}
|
||||
}
|
||||
|
||||
ESP32Camera *global_esp32_camera; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
|
||||
/* ---------------- CameraImageReader class ---------------- */
|
||||
void CameraImageReader::set_image(std::shared_ptr<CameraImage> image) {
|
||||
this->image_ = std::move(image);
|
||||
/* ---------------- ESP32CameraImageReader class ----------- */
|
||||
void ESP32CameraImageReader::set_image(std::shared_ptr<camera::CameraImage> image) {
|
||||
this->image_ = std::static_pointer_cast<ESP32CameraImage>(image);
|
||||
this->offset_ = 0;
|
||||
}
|
||||
size_t CameraImageReader::available() const {
|
||||
size_t ESP32CameraImageReader::available() const {
|
||||
if (!this->image_)
|
||||
return 0;
|
||||
|
||||
return this->image_->get_data_length() - this->offset_;
|
||||
}
|
||||
void CameraImageReader::return_image() { this->image_.reset(); }
|
||||
void CameraImageReader::consume_data(size_t consumed) { this->offset_ += consumed; }
|
||||
uint8_t *CameraImageReader::peek_data_buffer() { return this->image_->get_data_buffer() + this->offset_; }
|
||||
void ESP32CameraImageReader::return_image() { this->image_.reset(); }
|
||||
void ESP32CameraImageReader::consume_data(size_t consumed) { this->offset_ += consumed; }
|
||||
uint8_t *ESP32CameraImageReader::peek_data_buffer() { return this->image_->get_data_buffer() + this->offset_; }
|
||||
|
||||
/* ---------------- CameraImage class ---------------- */
|
||||
CameraImage::CameraImage(camera_fb_t *buffer, uint8_t requesters) : buffer_(buffer), requesters_(requesters) {}
|
||||
/* ---------------- ESP32CameraImage class ----------- */
|
||||
ESP32CameraImage::ESP32CameraImage(camera_fb_t *buffer, uint8_t requesters)
|
||||
: buffer_(buffer), requesters_(requesters) {}
|
||||
|
||||
camera_fb_t *CameraImage::get_raw_buffer() { return this->buffer_; }
|
||||
uint8_t *CameraImage::get_data_buffer() { return this->buffer_->buf; }
|
||||
size_t CameraImage::get_data_length() { return this->buffer_->len; }
|
||||
bool CameraImage::was_requested_by(CameraRequester requester) const {
|
||||
camera_fb_t *ESP32CameraImage::get_raw_buffer() { return this->buffer_; }
|
||||
uint8_t *ESP32CameraImage::get_data_buffer() { return this->buffer_->buf; }
|
||||
size_t ESP32CameraImage::get_data_length() { return this->buffer_->len; }
|
||||
bool ESP32CameraImage::was_requested_by(camera::CameraRequester requester) const {
|
||||
return (this->requesters_ & (1 << requester)) != 0;
|
||||
}
|
||||
|
||||
|
@ -7,7 +7,7 @@
|
||||
#include <freertos/queue.h>
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/entity_base.h"
|
||||
#include "esphome/components/camera/camera.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
#ifdef USE_I2C
|
||||
@ -19,9 +19,6 @@ namespace esp32_camera {
|
||||
|
||||
class ESP32Camera;
|
||||
|
||||
/* ---------------- enum classes ---------------- */
|
||||
enum CameraRequester { IDLE, API_REQUESTER, WEB_REQUESTER };
|
||||
|
||||
enum ESP32CameraFrameSize {
|
||||
ESP32_CAMERA_SIZE_160X120, // QQVGA
|
||||
ESP32_CAMERA_SIZE_176X144, // QCIF
|
||||
@ -77,13 +74,13 @@ enum ESP32SpecialEffect {
|
||||
};
|
||||
|
||||
/* ---------------- CameraImage class ---------------- */
|
||||
class CameraImage {
|
||||
class ESP32CameraImage : public camera::CameraImage {
|
||||
public:
|
||||
CameraImage(camera_fb_t *buffer, uint8_t requester);
|
||||
ESP32CameraImage(camera_fb_t *buffer, uint8_t requester);
|
||||
camera_fb_t *get_raw_buffer();
|
||||
uint8_t *get_data_buffer();
|
||||
size_t get_data_length();
|
||||
bool was_requested_by(CameraRequester requester) const;
|
||||
uint8_t *get_data_buffer() override;
|
||||
size_t get_data_length() override;
|
||||
bool was_requested_by(camera::CameraRequester requester) const override;
|
||||
|
||||
protected:
|
||||
camera_fb_t *buffer_;
|
||||
@ -96,21 +93,21 @@ struct CameraImageData {
|
||||
};
|
||||
|
||||
/* ---------------- CameraImageReader class ---------------- */
|
||||
class CameraImageReader {
|
||||
class ESP32CameraImageReader : public camera::CameraImageReader {
|
||||
public:
|
||||
void set_image(std::shared_ptr<CameraImage> image);
|
||||
size_t available() const;
|
||||
uint8_t *peek_data_buffer();
|
||||
void consume_data(size_t consumed);
|
||||
void return_image();
|
||||
void set_image(std::shared_ptr<camera::CameraImage> image) override;
|
||||
size_t available() const override;
|
||||
uint8_t *peek_data_buffer() override;
|
||||
void consume_data(size_t consumed) override;
|
||||
void return_image() override;
|
||||
|
||||
protected:
|
||||
std::shared_ptr<CameraImage> image_;
|
||||
std::shared_ptr<ESP32CameraImage> image_;
|
||||
size_t offset_{0};
|
||||
};
|
||||
|
||||
/* ---------------- ESP32Camera class ---------------- */
|
||||
class ESP32Camera : public EntityBase, public Component {
|
||||
class ESP32Camera : public camera::Camera {
|
||||
public:
|
||||
ESP32Camera();
|
||||
|
||||
@ -162,14 +159,15 @@ class ESP32Camera : public EntityBase, public Component {
|
||||
void dump_config() override;
|
||||
float get_setup_priority() const override;
|
||||
/* public API (specific) */
|
||||
void start_stream(CameraRequester requester);
|
||||
void stop_stream(CameraRequester requester);
|
||||
void request_image(CameraRequester requester);
|
||||
void start_stream(camera::CameraRequester requester) override;
|
||||
void stop_stream(camera::CameraRequester requester) override;
|
||||
void request_image(camera::CameraRequester requester) override;
|
||||
void update_camera_parameters();
|
||||
|
||||
void add_image_callback(std::function<void(std::shared_ptr<CameraImage>)> &&callback);
|
||||
void add_image_callback(std::function<void(std::shared_ptr<camera::CameraImage>)> &&callback) override;
|
||||
void add_stream_start_callback(std::function<void()> &&callback);
|
||||
void add_stream_stop_callback(std::function<void()> &&callback);
|
||||
camera::CameraImageReader *create_image_reader() override;
|
||||
|
||||
protected:
|
||||
/* internal methods */
|
||||
@ -206,12 +204,12 @@ class ESP32Camera : public EntityBase, public Component {
|
||||
uint32_t idle_update_interval_{15000};
|
||||
|
||||
esp_err_t init_error_{ESP_OK};
|
||||
std::shared_ptr<CameraImage> current_image_;
|
||||
std::shared_ptr<ESP32CameraImage> current_image_;
|
||||
uint8_t single_requesters_{0};
|
||||
uint8_t stream_requesters_{0};
|
||||
QueueHandle_t framebuffer_get_queue_;
|
||||
QueueHandle_t framebuffer_return_queue_;
|
||||
CallbackManager<void(std::shared_ptr<CameraImage>)> new_image_callback_{};
|
||||
CallbackManager<void(std::shared_ptr<camera::CameraImage>)> new_image_callback_{};
|
||||
CallbackManager<void()> stream_start_callback_{};
|
||||
CallbackManager<void()> stream_stop_callback_{};
|
||||
|
||||
@ -222,13 +220,10 @@ class ESP32Camera : public EntityBase, public Component {
|
||||
#endif // USE_I2C
|
||||
};
|
||||
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
extern ESP32Camera *global_esp32_camera;
|
||||
|
||||
class ESP32CameraImageTrigger : public Trigger<CameraImageData> {
|
||||
public:
|
||||
explicit ESP32CameraImageTrigger(ESP32Camera *parent) {
|
||||
parent->add_image_callback([this](const std::shared_ptr<esp32_camera::CameraImage> &image) {
|
||||
parent->add_image_callback([this](const std::shared_ptr<camera::CameraImage> &image) {
|
||||
CameraImageData camera_image_data{};
|
||||
camera_image_data.length = image->get_data_length();
|
||||
camera_image_data.data = image->get_data_buffer();
|
||||
|
@ -3,7 +3,8 @@ import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ID, CONF_MODE, CONF_PORT
|
||||
|
||||
CODEOWNERS = ["@ayufan"]
|
||||
DEPENDENCIES = ["esp32_camera", "network"]
|
||||
AUTO_LOAD = ["camera"]
|
||||
DEPENDENCIES = ["network"]
|
||||
MULTI_CONF = True
|
||||
|
||||
esp32_camera_web_server_ns = cg.esphome_ns.namespace("esp32_camera_web_server")
|
||||
|
@ -40,7 +40,7 @@ CameraWebServer::CameraWebServer() {}
|
||||
CameraWebServer::~CameraWebServer() {}
|
||||
|
||||
void CameraWebServer::setup() {
|
||||
if (!esp32_camera::global_esp32_camera || esp32_camera::global_esp32_camera->is_failed()) {
|
||||
if (!camera::Camera::instance() || camera::Camera::instance()->is_failed()) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
@ -67,8 +67,8 @@ void CameraWebServer::setup() {
|
||||
|
||||
httpd_register_uri_handler(this->httpd_, &uri);
|
||||
|
||||
esp32_camera::global_esp32_camera->add_image_callback([this](std::shared_ptr<esp32_camera::CameraImage> image) {
|
||||
if (this->running_ && image->was_requested_by(esp32_camera::WEB_REQUESTER)) {
|
||||
camera::Camera::instance()->add_image_callback([this](std::shared_ptr<camera::CameraImage> image) {
|
||||
if (this->running_ && image->was_requested_by(camera::WEB_REQUESTER)) {
|
||||
this->image_ = std::move(image);
|
||||
xSemaphoreGive(this->semaphore_);
|
||||
}
|
||||
@ -108,8 +108,8 @@ void CameraWebServer::loop() {
|
||||
}
|
||||
}
|
||||
|
||||
std::shared_ptr<esphome::esp32_camera::CameraImage> CameraWebServer::wait_for_image_() {
|
||||
std::shared_ptr<esphome::esp32_camera::CameraImage> image;
|
||||
std::shared_ptr<esphome::camera::CameraImage> CameraWebServer::wait_for_image_() {
|
||||
std::shared_ptr<esphome::camera::CameraImage> image;
|
||||
image.swap(this->image_);
|
||||
|
||||
if (!image) {
|
||||
@ -172,7 +172,7 @@ esp_err_t CameraWebServer::streaming_handler_(struct httpd_req *req) {
|
||||
uint32_t last_frame = millis();
|
||||
uint32_t frames = 0;
|
||||
|
||||
esp32_camera::global_esp32_camera->start_stream(esphome::esp32_camera::WEB_REQUESTER);
|
||||
camera::Camera::instance()->start_stream(esphome::camera::WEB_REQUESTER);
|
||||
|
||||
while (res == ESP_OK && this->running_) {
|
||||
auto image = this->wait_for_image_();
|
||||
@ -205,7 +205,7 @@ esp_err_t CameraWebServer::streaming_handler_(struct httpd_req *req) {
|
||||
res = httpd_send_all(req, STREAM_ERROR, strlen(STREAM_ERROR));
|
||||
}
|
||||
|
||||
esp32_camera::global_esp32_camera->stop_stream(esphome::esp32_camera::WEB_REQUESTER);
|
||||
camera::Camera::instance()->stop_stream(esphome::camera::WEB_REQUESTER);
|
||||
|
||||
ESP_LOGI(TAG, "STREAM: closed. Frames: %" PRIu32, frames);
|
||||
|
||||
@ -215,7 +215,7 @@ esp_err_t CameraWebServer::streaming_handler_(struct httpd_req *req) {
|
||||
esp_err_t CameraWebServer::snapshot_handler_(struct httpd_req *req) {
|
||||
esp_err_t res = ESP_OK;
|
||||
|
||||
esp32_camera::global_esp32_camera->request_image(esphome::esp32_camera::WEB_REQUESTER);
|
||||
camera::Camera::instance()->request_image(esphome::camera::WEB_REQUESTER);
|
||||
|
||||
auto image = this->wait_for_image_();
|
||||
|
||||
|
@ -6,7 +6,7 @@
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
|
||||
#include "esphome/components/esp32_camera/esp32_camera.h"
|
||||
#include "esphome/components/camera/camera.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/preferences.h"
|
||||
@ -32,7 +32,7 @@ class CameraWebServer : public Component {
|
||||
void loop() override;
|
||||
|
||||
protected:
|
||||
std::shared_ptr<esphome::esp32_camera::CameraImage> wait_for_image_();
|
||||
std::shared_ptr<camera::CameraImage> wait_for_image_();
|
||||
esp_err_t handler_(struct httpd_req *req);
|
||||
esp_err_t streaming_handler_(struct httpd_req *req);
|
||||
esp_err_t snapshot_handler_(struct httpd_req *req);
|
||||
@ -40,7 +40,7 @@ class CameraWebServer : public Component {
|
||||
uint16_t port_{0};
|
||||
void *httpd_{nullptr};
|
||||
SemaphoreHandle_t semaphore_;
|
||||
std::shared_ptr<esphome::esp32_camera::CameraImage> image_;
|
||||
std::shared_ptr<camera::CameraImage> image_;
|
||||
bool running_{false};
|
||||
Mode mode_{STREAM};
|
||||
};
|
||||
|
@ -93,7 +93,6 @@ class ESP32TouchComponent : public Component {
|
||||
uint32_t last_release_check_{0};
|
||||
uint32_t release_timeout_ms_{1500};
|
||||
uint32_t release_check_interval_ms_{50};
|
||||
bool initial_state_published_[TOUCH_PAD_MAX] = {false};
|
||||
|
||||
// Common configuration parameters
|
||||
uint16_t sleep_cycle_{4095};
|
||||
@ -123,13 +122,6 @@ class ESP32TouchComponent : public Component {
|
||||
};
|
||||
|
||||
protected:
|
||||
// Design note: last_touch_time_ does not require synchronization primitives because:
|
||||
// 1. ESP32 guarantees atomic 32-bit aligned reads/writes
|
||||
// 2. ISR only writes timestamps, main loop only reads
|
||||
// 3. Timing tolerance allows for occasional stale reads (50ms check interval)
|
||||
// 4. Queue operations provide implicit memory barriers
|
||||
// Using atomic/critical sections would add overhead without meaningful benefit
|
||||
uint32_t last_touch_time_[TOUCH_PAD_MAX] = {0};
|
||||
uint32_t iir_filter_{0};
|
||||
|
||||
bool iir_filter_enabled_() const { return this->iir_filter_ > 0; }
|
||||
@ -147,9 +139,6 @@ class ESP32TouchComponent : public Component {
|
||||
uint32_t intr_mask;
|
||||
};
|
||||
|
||||
// Track last touch time for timeout-based release detection
|
||||
uint32_t last_touch_time_[TOUCH_PAD_MAX] = {0};
|
||||
|
||||
protected:
|
||||
// Filter configuration
|
||||
touch_filter_mode_t filter_mode_{TOUCH_PAD_FILTER_MAX};
|
||||
@ -255,11 +244,22 @@ class ESP32TouchBinarySensor : public binary_sensor::BinarySensor {
|
||||
|
||||
touch_pad_t touch_pad_{TOUCH_PAD_MAX};
|
||||
uint32_t threshold_{0};
|
||||
uint32_t benchmark_{};
|
||||
#ifdef USE_ESP32_VARIANT_ESP32
|
||||
uint32_t value_{0};
|
||||
#endif
|
||||
bool last_state_{false};
|
||||
const uint32_t wakeup_threshold_{0};
|
||||
|
||||
// Track last touch time for timeout-based release detection
|
||||
// Design note: last_touch_time_ does not require synchronization primitives because:
|
||||
// 1. ESP32 guarantees atomic 32-bit aligned reads/writes
|
||||
// 2. ISR only writes timestamps, main loop only reads
|
||||
// 3. Timing tolerance allows for occasional stale reads (50ms check interval)
|
||||
// 4. Queue operations provide implicit memory barriers
|
||||
// Using atomic/critical sections would add overhead without meaningful benefit
|
||||
uint32_t last_touch_time_{};
|
||||
bool initial_state_published_{};
|
||||
};
|
||||
|
||||
} // namespace esp32_touch
|
||||
|
@ -22,16 +22,20 @@ void ESP32TouchComponent::dump_config_base_() {
|
||||
" Sleep cycle: %.2fms\n"
|
||||
" Low Voltage Reference: %s\n"
|
||||
" High Voltage Reference: %s\n"
|
||||
" Voltage Attenuation: %s",
|
||||
" Voltage Attenuation: %s\n"
|
||||
" Release Timeout: %" PRIu32 "ms\n",
|
||||
this->meas_cycle_ / (8000000.0f / 1000.0f), this->sleep_cycle_ / (150000.0f / 1000.0f), lv_s, hv_s,
|
||||
atten_s);
|
||||
atten_s, this->release_timeout_ms_);
|
||||
}
|
||||
|
||||
void ESP32TouchComponent::dump_config_sensors_() {
|
||||
for (auto *child : this->children_) {
|
||||
LOG_BINARY_SENSOR(" ", "Touch Pad", child);
|
||||
ESP_LOGCONFIG(TAG, " Pad: T%" PRIu32, (uint32_t) child->get_touch_pad());
|
||||
ESP_LOGCONFIG(TAG, " Threshold: %" PRIu32, child->get_threshold());
|
||||
ESP_LOGCONFIG(TAG,
|
||||
" Pad: T%u\n"
|
||||
" Threshold: %" PRIu32 "\n"
|
||||
" Benchmark: %" PRIu32,
|
||||
(unsigned) child->touch_pad_, child->threshold_, child->benchmark_);
|
||||
}
|
||||
}
|
||||
|
||||
@ -112,12 +116,11 @@ bool ESP32TouchComponent::should_check_for_releases_(uint32_t now) {
|
||||
}
|
||||
|
||||
void ESP32TouchComponent::publish_initial_state_if_needed_(ESP32TouchBinarySensor *child, uint32_t now) {
|
||||
touch_pad_t pad = child->get_touch_pad();
|
||||
if (!this->initial_state_published_[pad]) {
|
||||
if (!child->initial_state_published_) {
|
||||
// Check if enough time has passed since startup
|
||||
if (now > this->release_timeout_ms_) {
|
||||
child->publish_initial_state(false);
|
||||
this->initial_state_published_[pad] = true;
|
||||
child->initial_state_published_ = true;
|
||||
ESP_LOGV(TAG, "Touch Pad '%s' state: OFF (initial)", child->get_name().c_str());
|
||||
}
|
||||
}
|
||||
|
@ -104,7 +104,7 @@ void ESP32TouchComponent::loop() {
|
||||
|
||||
// Track when we last saw this pad as touched
|
||||
if (new_state) {
|
||||
this->last_touch_time_[event.pad] = now;
|
||||
child->last_touch_time_ = now;
|
||||
}
|
||||
|
||||
// Only publish if state changed - this filters out repeated events
|
||||
@ -127,15 +127,13 @@ void ESP32TouchComponent::loop() {
|
||||
|
||||
size_t pads_off = 0;
|
||||
for (auto *child : this->children_) {
|
||||
touch_pad_t pad = child->get_touch_pad();
|
||||
|
||||
// Handle initial state publication after startup
|
||||
this->publish_initial_state_if_needed_(child, now);
|
||||
|
||||
if (child->last_state_) {
|
||||
// Pad is currently in touched state - check for release timeout
|
||||
// Using subtraction handles 32-bit rollover correctly
|
||||
uint32_t time_diff = now - this->last_touch_time_[pad];
|
||||
uint32_t time_diff = now - child->last_touch_time_;
|
||||
|
||||
// Check if we haven't seen this pad recently
|
||||
if (time_diff > this->release_timeout_ms_) {
|
||||
|
@ -14,19 +14,16 @@ static const char *const TAG = "esp32_touch";
|
||||
void ESP32TouchComponent::update_touch_state_(ESP32TouchBinarySensor *child, bool is_touched) {
|
||||
// Always update timer when touched
|
||||
if (is_touched) {
|
||||
this->last_touch_time_[child->get_touch_pad()] = App.get_loop_component_start_time();
|
||||
child->last_touch_time_ = App.get_loop_component_start_time();
|
||||
}
|
||||
|
||||
if (child->last_state_ != is_touched) {
|
||||
// Read value for logging
|
||||
uint32_t value = this->read_touch_value(child->get_touch_pad());
|
||||
|
||||
child->last_state_ = is_touched;
|
||||
child->publish_state(is_touched);
|
||||
if (is_touched) {
|
||||
// ESP32-S2/S3 v2: touched when value > threshold
|
||||
ESP_LOGV(TAG, "Touch Pad '%s' state: ON (value: %" PRIu32 " > threshold: %" PRIu32 ")", child->get_name().c_str(),
|
||||
value, child->get_threshold());
|
||||
this->read_touch_value(child->touch_pad_), child->threshold_ + child->benchmark_);
|
||||
} else {
|
||||
ESP_LOGV(TAG, "Touch Pad '%s' state: OFF", child->get_name().c_str());
|
||||
}
|
||||
@ -36,10 +33,13 @@ void ESP32TouchComponent::update_touch_state_(ESP32TouchBinarySensor *child, boo
|
||||
// Helper to read touch value and update state for a given child (used for timeout events)
|
||||
bool ESP32TouchComponent::check_and_update_touch_state_(ESP32TouchBinarySensor *child) {
|
||||
// Read current touch value
|
||||
uint32_t value = this->read_touch_value(child->get_touch_pad());
|
||||
uint32_t value = this->read_touch_value(child->touch_pad_);
|
||||
|
||||
// ESP32-S2/S3 v2: Touch is detected when value > threshold
|
||||
bool is_touched = value > child->get_threshold();
|
||||
// ESP32-S2/S3 v2: Touch is detected when value > threshold + benchmark
|
||||
ESP_LOGV(TAG,
|
||||
"Checking touch state for '%s' (T%d): value = %" PRIu32 ", threshold = %" PRIu32 ", benchmark = %" PRIu32,
|
||||
child->get_name().c_str(), child->touch_pad_, value, child->threshold_, child->benchmark_);
|
||||
bool is_touched = value > child->benchmark_ + child->threshold_;
|
||||
|
||||
this->update_touch_state_(child, is_touched);
|
||||
return is_touched;
|
||||
@ -61,9 +61,9 @@ void ESP32TouchComponent::setup() {
|
||||
|
||||
// Configure each touch pad first
|
||||
for (auto *child : this->children_) {
|
||||
esp_err_t config_err = touch_pad_config(child->get_touch_pad());
|
||||
esp_err_t config_err = touch_pad_config(child->touch_pad_);
|
||||
if (config_err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to configure touch pad %d: %s", child->get_touch_pad(), esp_err_to_name(config_err));
|
||||
ESP_LOGE(TAG, "Failed to configure touch pad %d: %s", child->touch_pad_, esp_err_to_name(config_err));
|
||||
}
|
||||
}
|
||||
|
||||
@ -100,8 +100,8 @@ void ESP32TouchComponent::setup() {
|
||||
|
||||
// Configure measurement parameters
|
||||
touch_pad_set_voltage(this->high_voltage_reference_, this->low_voltage_reference_, this->voltage_attenuation_);
|
||||
// ESP32-S2/S3 always use the older API
|
||||
touch_pad_set_meas_time(this->sleep_cycle_, this->meas_cycle_);
|
||||
touch_pad_set_charge_discharge_times(this->meas_cycle_);
|
||||
touch_pad_set_measurement_interval(this->sleep_cycle_);
|
||||
|
||||
// Configure timeout if needed
|
||||
touch_pad_timeout_set(true, TOUCH_PAD_THRESHOLD_MAX);
|
||||
@ -118,8 +118,8 @@ void ESP32TouchComponent::setup() {
|
||||
|
||||
// Set thresholds for each pad BEFORE starting FSM
|
||||
for (auto *child : this->children_) {
|
||||
if (child->get_threshold() != 0) {
|
||||
touch_pad_set_thresh(child->get_touch_pad(), child->get_threshold());
|
||||
if (child->threshold_ != 0) {
|
||||
touch_pad_set_thresh(child->touch_pad_, child->threshold_);
|
||||
}
|
||||
}
|
||||
|
||||
@ -277,6 +277,7 @@ void ESP32TouchComponent::loop() {
|
||||
// Process any queued touch events from interrupts
|
||||
TouchPadEventV2 event;
|
||||
while (xQueueReceive(this->touch_queue_, &event, 0) == pdTRUE) {
|
||||
ESP_LOGD(TAG, "Event received, mask = 0x%" PRIx32 ", pad = %d", event.intr_mask, event.pad);
|
||||
// Handle timeout events
|
||||
if (event.intr_mask & TOUCH_PAD_INTR_MASK_TIMEOUT) {
|
||||
// Resume measurement after timeout
|
||||
@ -289,18 +290,16 @@ void ESP32TouchComponent::loop() {
|
||||
|
||||
// Find the child for the pad that triggered the interrupt
|
||||
for (auto *child : this->children_) {
|
||||
if (child->get_touch_pad() != event.pad) {
|
||||
continue;
|
||||
if (child->touch_pad_ == event.pad) {
|
||||
if (event.intr_mask & TOUCH_PAD_INTR_MASK_TIMEOUT) {
|
||||
// For timeout events, we need to read the value to determine state
|
||||
this->check_and_update_touch_state_(child);
|
||||
} else if (event.intr_mask & TOUCH_PAD_INTR_MASK_ACTIVE) {
|
||||
// We only get ACTIVE interrupts now, releases are detected by timeout
|
||||
this->update_touch_state_(child, true); // Always touched for ACTIVE interrupts
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
if (event.intr_mask & TOUCH_PAD_INTR_MASK_TIMEOUT) {
|
||||
// For timeout events, we need to read the value to determine state
|
||||
this->check_and_update_touch_state_(child);
|
||||
} else if (event.intr_mask & TOUCH_PAD_INTR_MASK_ACTIVE) {
|
||||
// We only get ACTIVE interrupts now, releases are detected by timeout
|
||||
this->update_touch_state_(child, true); // Always touched for ACTIVE interrupts
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@ -311,15 +310,15 @@ void ESP32TouchComponent::loop() {
|
||||
|
||||
size_t pads_off = 0;
|
||||
for (auto *child : this->children_) {
|
||||
touch_pad_t pad = child->get_touch_pad();
|
||||
|
||||
if (child->benchmark_ == 0)
|
||||
touch_pad_read_benchmark(child->touch_pad_, &child->benchmark_);
|
||||
// Handle initial state publication after startup
|
||||
this->publish_initial_state_if_needed_(child, now);
|
||||
|
||||
if (child->last_state_) {
|
||||
// Pad is currently in touched state - check for release timeout
|
||||
// Using subtraction handles 32-bit rollover correctly
|
||||
uint32_t time_diff = now - this->last_touch_time_[pad];
|
||||
uint32_t time_diff = now - child->last_touch_time_;
|
||||
|
||||
// Check if we haven't seen this pad recently
|
||||
if (time_diff > this->release_timeout_ms_) {
|
||||
|
@ -70,6 +70,7 @@ PROTOCOLS = {
|
||||
"airway": Protocol.PROTOCOL_AIRWAY,
|
||||
"bgh_aud": Protocol.PROTOCOL_BGH_AUD,
|
||||
"panasonic_altdke": Protocol.PROTOCOL_PANASONIC_ALTDKE,
|
||||
"philco_phs32": Protocol.PROTOCOL_PHILCO_PHS32,
|
||||
"vaillantvai8": Protocol.PROTOCOL_VAILLANTVAI8,
|
||||
"r51m": Protocol.PROTOCOL_R51M,
|
||||
}
|
||||
|
@ -65,6 +65,7 @@ const std::map<Protocol, std::function<HeatpumpIR *()>> PROTOCOL_CONSTRUCTOR_MAP
|
||||
{PROTOCOL_AIRWAY, []() { return new AIRWAYHeatpumpIR(); }}, // NOLINT
|
||||
{PROTOCOL_BGH_AUD, []() { return new BGHHeatpumpIR(); }}, // NOLINT
|
||||
{PROTOCOL_PANASONIC_ALTDKE, []() { return new PanasonicAltDKEHeatpumpIR(); }}, // NOLINT
|
||||
{PROTOCOL_PHILCO_PHS32, []() { return new PhilcoPHS32HeatpumpIR(); }}, // NOLINT
|
||||
{PROTOCOL_VAILLANTVAI8, []() { return new VaillantHeatpumpIR(); }}, // NOLINT
|
||||
{PROTOCOL_R51M, []() { return new R51MHeatpumpIR(); }}, // NOLINT
|
||||
};
|
||||
|
@ -65,6 +65,7 @@ enum Protocol {
|
||||
PROTOCOL_AIRWAY,
|
||||
PROTOCOL_BGH_AUD,
|
||||
PROTOCOL_PANASONIC_ALTDKE,
|
||||
PROTOCOL_PHILCO_PHS32,
|
||||
PROTOCOL_VAILLANTVAI8,
|
||||
PROTOCOL_R51M,
|
||||
};
|
||||
|
@ -50,7 +50,8 @@ void HttpRequestUpdate::update_task(void *params) {
|
||||
|
||||
if (container == nullptr || container->status_code != HTTP_STATUS_OK) {
|
||||
std::string msg = str_sprintf("Failed to fetch manifest from %s", this_update->source_url_.c_str());
|
||||
this_update->status_set_error(msg.c_str());
|
||||
// Defer to main loop to avoid race condition on component_state_ read-modify-write
|
||||
this_update->defer([this_update, msg]() { this_update->status_set_error(msg.c_str()); });
|
||||
UPDATE_RETURN;
|
||||
}
|
||||
|
||||
@ -58,7 +59,8 @@ void HttpRequestUpdate::update_task(void *params) {
|
||||
uint8_t *data = allocator.allocate(container->content_length);
|
||||
if (data == nullptr) {
|
||||
std::string msg = str_sprintf("Failed to allocate %zu bytes for manifest", container->content_length);
|
||||
this_update->status_set_error(msg.c_str());
|
||||
// Defer to main loop to avoid race condition on component_state_ read-modify-write
|
||||
this_update->defer([this_update, msg]() { this_update->status_set_error(msg.c_str()); });
|
||||
container->end();
|
||||
UPDATE_RETURN;
|
||||
}
|
||||
@ -120,7 +122,8 @@ void HttpRequestUpdate::update_task(void *params) {
|
||||
|
||||
if (!valid) {
|
||||
std::string msg = str_sprintf("Failed to parse JSON from %s", this_update->source_url_.c_str());
|
||||
this_update->status_set_error(msg.c_str());
|
||||
// Defer to main loop to avoid race condition on component_state_ read-modify-write
|
||||
this_update->defer([this_update, msg]() { this_update->status_set_error(msg.c_str()); });
|
||||
UPDATE_RETURN;
|
||||
}
|
||||
|
||||
@ -147,18 +150,34 @@ void HttpRequestUpdate::update_task(void *params) {
|
||||
this_update->update_info_.current_version = current_version;
|
||||
}
|
||||
|
||||
bool trigger_update_available = false;
|
||||
|
||||
if (this_update->update_info_.latest_version.empty() ||
|
||||
this_update->update_info_.latest_version == this_update->update_info_.current_version) {
|
||||
this_update->state_ = update::UPDATE_STATE_NO_UPDATE;
|
||||
} else {
|
||||
if (this_update->state_ != update::UPDATE_STATE_AVAILABLE) {
|
||||
trigger_update_available = true;
|
||||
}
|
||||
this_update->state_ = update::UPDATE_STATE_AVAILABLE;
|
||||
}
|
||||
|
||||
this_update->update_info_.has_progress = false;
|
||||
this_update->update_info_.progress = 0.0f;
|
||||
// Defer to main loop to ensure thread-safe execution of:
|
||||
// - status_clear_error() performs non-atomic read-modify-write on component_state_
|
||||
// - publish_state() triggers API callbacks that write to the shared protobuf buffer
|
||||
// which can be corrupted if accessed concurrently from task and main loop threads
|
||||
// - update_available trigger to ensure consistent state when the trigger fires
|
||||
this_update->defer([this_update, trigger_update_available]() {
|
||||
this_update->update_info_.has_progress = false;
|
||||
this_update->update_info_.progress = 0.0f;
|
||||
|
||||
this_update->status_clear_error();
|
||||
this_update->publish_state();
|
||||
this_update->status_clear_error();
|
||||
this_update->publish_state();
|
||||
|
||||
if (trigger_update_available) {
|
||||
this_update->get_update_available_trigger()->trigger(this_update->update_info_);
|
||||
}
|
||||
});
|
||||
|
||||
UPDATE_RETURN;
|
||||
}
|
||||
|
@ -14,8 +14,8 @@ from esphome.const import (
|
||||
|
||||
from .. import CONF_LD2410_ID, LD2410Component, ld2410_ns
|
||||
|
||||
FactoryResetButton = ld2410_ns.class_("FactoryResetButton", button.Button)
|
||||
QueryButton = ld2410_ns.class_("QueryButton", button.Button)
|
||||
ResetButton = ld2410_ns.class_("ResetButton", button.Button)
|
||||
RestartButton = ld2410_ns.class_("RestartButton", button.Button)
|
||||
|
||||
CONF_QUERY_PARAMS = "query_params"
|
||||
@ -23,7 +23,7 @@ CONF_QUERY_PARAMS = "query_params"
|
||||
CONFIG_SCHEMA = {
|
||||
cv.GenerateID(CONF_LD2410_ID): cv.use_id(LD2410Component),
|
||||
cv.Optional(CONF_FACTORY_RESET): button.button_schema(
|
||||
ResetButton,
|
||||
FactoryResetButton,
|
||||
device_class=DEVICE_CLASS_RESTART,
|
||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
||||
icon=ICON_RESTART_ALERT,
|
||||
@ -47,7 +47,7 @@ async def to_code(config):
|
||||
if factory_reset_config := config.get(CONF_FACTORY_RESET):
|
||||
b = await button.new_button(factory_reset_config)
|
||||
await cg.register_parented(b, config[CONF_LD2410_ID])
|
||||
cg.add(ld2410_component.set_reset_button(b))
|
||||
cg.add(ld2410_component.set_factory_reset_button(b))
|
||||
if restart_config := config.get(CONF_RESTART):
|
||||
b = await button.new_button(restart_config)
|
||||
await cg.register_parented(b, config[CONF_LD2410_ID])
|
||||
|
@ -0,0 +1,9 @@
|
||||
#include "factory_reset_button.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ld2410 {
|
||||
|
||||
void FactoryResetButton::press_action() { this->parent_->factory_reset(); }
|
||||
|
||||
} // namespace ld2410
|
||||
} // namespace esphome
|
@ -6,9 +6,9 @@
|
||||
namespace esphome {
|
||||
namespace ld2410 {
|
||||
|
||||
class ResetButton : public button::Button, public Parented<LD2410Component> {
|
||||
class FactoryResetButton : public button::Button, public Parented<LD2410Component> {
|
||||
public:
|
||||
ResetButton() = default;
|
||||
FactoryResetButton() = default;
|
||||
|
||||
protected:
|
||||
void press_action() override;
|
@ -1,9 +0,0 @@
|
||||
#include "reset_button.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ld2410 {
|
||||
|
||||
void ResetButton::press_action() { this->parent_->factory_reset(); }
|
||||
|
||||
} // namespace ld2410
|
||||
} // namespace esphome
|
@ -18,11 +18,10 @@ namespace esphome {
|
||||
namespace ld2410 {
|
||||
|
||||
static const char *const TAG = "ld2410";
|
||||
static const char *const NO_MAC = "08:05:04:03:02:01";
|
||||
static const char *const UNKNOWN_MAC = "unknown";
|
||||
static const char *const VERSION_FMT = "%u.%02X.%02X%02X%02X%02X";
|
||||
|
||||
enum BaudRateStructure : uint8_t {
|
||||
enum BaudRate : uint8_t {
|
||||
BAUD_RATE_9600 = 1,
|
||||
BAUD_RATE_19200 = 2,
|
||||
BAUD_RATE_38400 = 3,
|
||||
@ -33,23 +32,23 @@ enum BaudRateStructure : uint8_t {
|
||||
BAUD_RATE_460800 = 8,
|
||||
};
|
||||
|
||||
enum DistanceResolutionStructure : uint8_t {
|
||||
enum DistanceResolution : uint8_t {
|
||||
DISTANCE_RESOLUTION_0_2 = 0x01,
|
||||
DISTANCE_RESOLUTION_0_75 = 0x00,
|
||||
};
|
||||
|
||||
enum LightFunctionStructure : uint8_t {
|
||||
enum LightFunction : uint8_t {
|
||||
LIGHT_FUNCTION_OFF = 0x00,
|
||||
LIGHT_FUNCTION_BELOW = 0x01,
|
||||
LIGHT_FUNCTION_ABOVE = 0x02,
|
||||
};
|
||||
|
||||
enum OutPinLevelStructure : uint8_t {
|
||||
enum OutPinLevel : uint8_t {
|
||||
OUT_PIN_LEVEL_LOW = 0x00,
|
||||
OUT_PIN_LEVEL_HIGH = 0x01,
|
||||
};
|
||||
|
||||
enum PeriodicDataStructure : uint8_t {
|
||||
enum PeriodicData : uint8_t {
|
||||
DATA_TYPES = 6,
|
||||
TARGET_STATES = 8,
|
||||
MOVING_TARGET_LOW = 9,
|
||||
@ -67,12 +66,12 @@ enum PeriodicDataStructure : uint8_t {
|
||||
};
|
||||
|
||||
enum PeriodicDataValue : uint8_t {
|
||||
HEAD = 0xAA,
|
||||
END = 0x55,
|
||||
HEADER = 0xAA,
|
||||
FOOTER = 0x55,
|
||||
CHECK = 0x00,
|
||||
};
|
||||
|
||||
enum AckDataStructure : uint8_t {
|
||||
enum AckData : uint8_t {
|
||||
COMMAND = 6,
|
||||
COMMAND_STATUS = 7,
|
||||
};
|
||||
@ -80,11 +79,11 @@ enum AckDataStructure : uint8_t {
|
||||
// Memory-efficient lookup tables
|
||||
struct StringToUint8 {
|
||||
const char *str;
|
||||
uint8_t value;
|
||||
const uint8_t value;
|
||||
};
|
||||
|
||||
struct Uint8ToString {
|
||||
uint8_t value;
|
||||
const uint8_t value;
|
||||
const char *str;
|
||||
};
|
||||
|
||||
@ -144,96 +143,119 @@ template<size_t N> const char *find_str(const Uint8ToString (&arr)[N], uint8_t v
|
||||
}
|
||||
|
||||
// Commands
|
||||
static const uint8_t CMD_ENABLE_CONF = 0xFF;
|
||||
static const uint8_t CMD_DISABLE_CONF = 0xFE;
|
||||
static const uint8_t CMD_ENABLE_ENG = 0x62;
|
||||
static const uint8_t CMD_DISABLE_ENG = 0x63;
|
||||
static const uint8_t CMD_MAXDIST_DURATION = 0x60;
|
||||
static const uint8_t CMD_QUERY = 0x61;
|
||||
static const uint8_t CMD_GATE_SENS = 0x64;
|
||||
static const uint8_t CMD_VERSION = 0xA0;
|
||||
static const uint8_t CMD_QUERY_DISTANCE_RESOLUTION = 0xAB;
|
||||
static const uint8_t CMD_SET_DISTANCE_RESOLUTION = 0xAA;
|
||||
static const uint8_t CMD_QUERY_LIGHT_CONTROL = 0xAE;
|
||||
static const uint8_t CMD_SET_LIGHT_CONTROL = 0xAD;
|
||||
static const uint8_t CMD_SET_BAUD_RATE = 0xA1;
|
||||
static const uint8_t CMD_BT_PASSWORD = 0xA9;
|
||||
static const uint8_t CMD_MAC = 0xA5;
|
||||
static const uint8_t CMD_RESET = 0xA2;
|
||||
static const uint8_t CMD_RESTART = 0xA3;
|
||||
static const uint8_t CMD_BLUETOOTH = 0xA4;
|
||||
static constexpr uint8_t CMD_ENABLE_CONF = 0xFF;
|
||||
static constexpr uint8_t CMD_DISABLE_CONF = 0xFE;
|
||||
static constexpr uint8_t CMD_ENABLE_ENG = 0x62;
|
||||
static constexpr uint8_t CMD_DISABLE_ENG = 0x63;
|
||||
static constexpr uint8_t CMD_MAXDIST_DURATION = 0x60;
|
||||
static constexpr uint8_t CMD_QUERY = 0x61;
|
||||
static constexpr uint8_t CMD_GATE_SENS = 0x64;
|
||||
static constexpr uint8_t CMD_QUERY_VERSION = 0xA0;
|
||||
static constexpr uint8_t CMD_QUERY_DISTANCE_RESOLUTION = 0xAB;
|
||||
static constexpr uint8_t CMD_SET_DISTANCE_RESOLUTION = 0xAA;
|
||||
static constexpr uint8_t CMD_QUERY_LIGHT_CONTROL = 0xAE;
|
||||
static constexpr uint8_t CMD_SET_LIGHT_CONTROL = 0xAD;
|
||||
static constexpr uint8_t CMD_SET_BAUD_RATE = 0xA1;
|
||||
static constexpr uint8_t CMD_BT_PASSWORD = 0xA9;
|
||||
static constexpr uint8_t CMD_QUERY_MAC_ADDRESS = 0xA5;
|
||||
static constexpr uint8_t CMD_RESET = 0xA2;
|
||||
static constexpr uint8_t CMD_RESTART = 0xA3;
|
||||
static constexpr uint8_t CMD_BLUETOOTH = 0xA4;
|
||||
// Commands values
|
||||
static const uint8_t CMD_MAX_MOVE_VALUE = 0x00;
|
||||
static const uint8_t CMD_MAX_STILL_VALUE = 0x01;
|
||||
static const uint8_t CMD_DURATION_VALUE = 0x02;
|
||||
static constexpr uint8_t CMD_MAX_MOVE_VALUE = 0x00;
|
||||
static constexpr uint8_t CMD_MAX_STILL_VALUE = 0x01;
|
||||
static constexpr uint8_t CMD_DURATION_VALUE = 0x02;
|
||||
// Header & Footer size
|
||||
static constexpr uint8_t HEADER_FOOTER_SIZE = 4;
|
||||
// Command Header & Footer
|
||||
static const uint8_t CMD_FRAME_HEADER[4] = {0xFD, 0xFC, 0xFB, 0xFA};
|
||||
static const uint8_t CMD_FRAME_END[4] = {0x04, 0x03, 0x02, 0x01};
|
||||
static constexpr uint8_t CMD_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xFD, 0xFC, 0xFB, 0xFA};
|
||||
static constexpr uint8_t CMD_FRAME_FOOTER[HEADER_FOOTER_SIZE] = {0x04, 0x03, 0x02, 0x01};
|
||||
// Data Header & Footer
|
||||
static const uint8_t DATA_FRAME_HEADER[4] = {0xF4, 0xF3, 0xF2, 0xF1};
|
||||
static const uint8_t DATA_FRAME_END[4] = {0xF8, 0xF7, 0xF6, 0xF5};
|
||||
static constexpr uint8_t DATA_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xF4, 0xF3, 0xF2, 0xF1};
|
||||
static constexpr uint8_t DATA_FRAME_FOOTER[HEADER_FOOTER_SIZE] = {0xF8, 0xF7, 0xF6, 0xF5};
|
||||
// MAC address the module uses when Bluetooth is disabled
|
||||
static constexpr uint8_t NO_MAC[] = {0x08, 0x05, 0x04, 0x03, 0x02, 0x01};
|
||||
|
||||
static inline int two_byte_to_int(char firstbyte, char secondbyte) { return (int16_t) (secondbyte << 8) + firstbyte; }
|
||||
|
||||
static bool validate_header_footer(const uint8_t *header_footer, const uint8_t *buffer) {
|
||||
for (uint8_t i = 0; i < HEADER_FOOTER_SIZE; i++) {
|
||||
if (header_footer[i] != buffer[i]) {
|
||||
return false; // Mismatch in header/footer
|
||||
}
|
||||
}
|
||||
return true; // Valid header/footer
|
||||
}
|
||||
|
||||
void LD2410Component::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "LD2410:");
|
||||
std::string mac_str =
|
||||
mac_address_is_valid(this->mac_address_) ? format_mac_address_pretty(this->mac_address_) : UNKNOWN_MAC;
|
||||
std::string version = str_sprintf(VERSION_FMT, this->version_[1], this->version_[0], this->version_[5],
|
||||
this->version_[4], this->version_[3], this->version_[2]);
|
||||
ESP_LOGCONFIG(TAG,
|
||||
"LD2410:\n"
|
||||
" Firmware version: %s\n"
|
||||
" MAC address: %s\n"
|
||||
" Throttle: %u ms",
|
||||
version.c_str(), mac_str.c_str(), this->throttle_);
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
LOG_BINARY_SENSOR(" ", "TargetBinarySensor", this->target_binary_sensor_);
|
||||
LOG_BINARY_SENSOR(" ", "MovingTargetBinarySensor", this->moving_target_binary_sensor_);
|
||||
LOG_BINARY_SENSOR(" ", "StillTargetBinarySensor", this->still_target_binary_sensor_);
|
||||
LOG_BINARY_SENSOR(" ", "OutPinPresenceStatusBinarySensor", this->out_pin_presence_status_binary_sensor_);
|
||||
#endif
|
||||
#ifdef USE_SWITCH
|
||||
LOG_SWITCH(" ", "EngineeringModeSwitch", this->engineering_mode_switch_);
|
||||
LOG_SWITCH(" ", "BluetoothSwitch", this->bluetooth_switch_);
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
LOG_BUTTON(" ", "ResetButton", this->reset_button_);
|
||||
LOG_BUTTON(" ", "RestartButton", this->restart_button_);
|
||||
LOG_BUTTON(" ", "QueryButton", this->query_button_);
|
||||
ESP_LOGCONFIG(TAG, "Binary Sensors:");
|
||||
LOG_BINARY_SENSOR(" ", "Target", this->target_binary_sensor_);
|
||||
LOG_BINARY_SENSOR(" ", "MovingTarget", this->moving_target_binary_sensor_);
|
||||
LOG_BINARY_SENSOR(" ", "StillTarget", this->still_target_binary_sensor_);
|
||||
LOG_BINARY_SENSOR(" ", "OutPinPresenceStatus", this->out_pin_presence_status_binary_sensor_);
|
||||
#endif
|
||||
#ifdef USE_SENSOR
|
||||
LOG_SENSOR(" ", "LightSensor", this->light_sensor_);
|
||||
LOG_SENSOR(" ", "MovingTargetDistanceSensor", this->moving_target_distance_sensor_);
|
||||
LOG_SENSOR(" ", "StillTargetDistanceSensor", this->still_target_distance_sensor_);
|
||||
LOG_SENSOR(" ", "MovingTargetEnergySensor", this->moving_target_energy_sensor_);
|
||||
LOG_SENSOR(" ", "StillTargetEnergySensor", this->still_target_energy_sensor_);
|
||||
LOG_SENSOR(" ", "DetectionDistanceSensor", this->detection_distance_sensor_);
|
||||
for (sensor::Sensor *s : this->gate_still_sensors_) {
|
||||
LOG_SENSOR(" ", "NthGateStillSesnsor", s);
|
||||
}
|
||||
ESP_LOGCONFIG(TAG, "Sensors:");
|
||||
LOG_SENSOR(" ", "Light", this->light_sensor_);
|
||||
LOG_SENSOR(" ", "DetectionDistance", this->detection_distance_sensor_);
|
||||
LOG_SENSOR(" ", "MovingTargetDistance", this->moving_target_distance_sensor_);
|
||||
LOG_SENSOR(" ", "MovingTargetEnergy", this->moving_target_energy_sensor_);
|
||||
LOG_SENSOR(" ", "StillTargetDistance", this->still_target_distance_sensor_);
|
||||
LOG_SENSOR(" ", "StillTargetEnergy", this->still_target_energy_sensor_);
|
||||
for (sensor::Sensor *s : this->gate_move_sensors_) {
|
||||
LOG_SENSOR(" ", "NthGateMoveSesnsor", s);
|
||||
LOG_SENSOR(" ", "GateMove", s);
|
||||
}
|
||||
for (sensor::Sensor *s : this->gate_still_sensors_) {
|
||||
LOG_SENSOR(" ", "GateStill", s);
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
LOG_TEXT_SENSOR(" ", "VersionTextSensor", this->version_text_sensor_);
|
||||
LOG_TEXT_SENSOR(" ", "MacTextSensor", this->mac_text_sensor_);
|
||||
#endif
|
||||
#ifdef USE_SELECT
|
||||
LOG_SELECT(" ", "LightFunctionSelect", this->light_function_select_);
|
||||
LOG_SELECT(" ", "OutPinLevelSelect", this->out_pin_level_select_);
|
||||
LOG_SELECT(" ", "DistanceResolutionSelect", this->distance_resolution_select_);
|
||||
LOG_SELECT(" ", "BaudRateSelect", this->baud_rate_select_);
|
||||
ESP_LOGCONFIG(TAG, "Text Sensors:");
|
||||
LOG_TEXT_SENSOR(" ", "Mac", this->mac_text_sensor_);
|
||||
LOG_TEXT_SENSOR(" ", "Version", this->version_text_sensor_);
|
||||
#endif
|
||||
#ifdef USE_NUMBER
|
||||
LOG_NUMBER(" ", "LightThresholdNumber", this->light_threshold_number_);
|
||||
LOG_NUMBER(" ", "MaxStillDistanceGateNumber", this->max_still_distance_gate_number_);
|
||||
LOG_NUMBER(" ", "MaxMoveDistanceGateNumber", this->max_move_distance_gate_number_);
|
||||
LOG_NUMBER(" ", "TimeoutNumber", this->timeout_number_);
|
||||
for (number::Number *n : this->gate_still_threshold_numbers_) {
|
||||
LOG_NUMBER(" ", "Still Thresholds Number", n);
|
||||
}
|
||||
ESP_LOGCONFIG(TAG, "Numbers:");
|
||||
LOG_NUMBER(" ", "LightThreshold", this->light_threshold_number_);
|
||||
LOG_NUMBER(" ", "MaxMoveDistanceGate", this->max_move_distance_gate_number_);
|
||||
LOG_NUMBER(" ", "MaxStillDistanceGate", this->max_still_distance_gate_number_);
|
||||
LOG_NUMBER(" ", "Timeout", this->timeout_number_);
|
||||
for (number::Number *n : this->gate_move_threshold_numbers_) {
|
||||
LOG_NUMBER(" ", "Move Thresholds Number", n);
|
||||
LOG_NUMBER(" ", "MoveThreshold", n);
|
||||
}
|
||||
for (number::Number *n : this->gate_still_threshold_numbers_) {
|
||||
LOG_NUMBER(" ", "StillThreshold", n);
|
||||
}
|
||||
#endif
|
||||
this->read_all_info();
|
||||
ESP_LOGCONFIG(TAG,
|
||||
" Throttle: %ums\n"
|
||||
" MAC address: %s\n"
|
||||
" Firmware version: %s",
|
||||
this->throttle_, this->mac_ == NO_MAC ? UNKNOWN_MAC : this->mac_.c_str(), this->version_.c_str());
|
||||
#ifdef USE_SELECT
|
||||
ESP_LOGCONFIG(TAG, "Selects:");
|
||||
LOG_SELECT(" ", "BaudRate", this->baud_rate_select_);
|
||||
LOG_SELECT(" ", "DistanceResolution", this->distance_resolution_select_);
|
||||
LOG_SELECT(" ", "LightFunction", this->light_function_select_);
|
||||
LOG_SELECT(" ", "OutPinLevel", this->out_pin_level_select_);
|
||||
#endif
|
||||
#ifdef USE_SWITCH
|
||||
ESP_LOGCONFIG(TAG, "Switches:");
|
||||
LOG_SWITCH(" ", "Bluetooth", this->bluetooth_switch_);
|
||||
LOG_SWITCH(" ", "EngineeringMode", this->engineering_mode_switch_);
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
ESP_LOGCONFIG(TAG, "Buttons:");
|
||||
LOG_BUTTON(" ", "FactoryReset", this->factory_reset_button_);
|
||||
LOG_BUTTON(" ", "Query", this->query_button_);
|
||||
LOG_BUTTON(" ", "Restart", this->restart_button_);
|
||||
#endif
|
||||
}
|
||||
|
||||
void LD2410Component::setup() {
|
||||
@ -246,12 +268,12 @@ void LD2410Component::read_all_info() {
|
||||
this->get_version_();
|
||||
this->get_mac_();
|
||||
this->get_distance_resolution_();
|
||||
this->get_light_control_();
|
||||
this->query_light_control_();
|
||||
this->query_parameters_();
|
||||
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 && this->baud_rate_select_->state != baud_rate) {
|
||||
if (this->baud_rate_select_ != nullptr) {
|
||||
this->baud_rate_select_->publish_state(baud_rate);
|
||||
}
|
||||
#endif
|
||||
@ -264,66 +286,59 @@ void LD2410Component::restart_and_read_all_info() {
|
||||
}
|
||||
|
||||
void LD2410Component::loop() {
|
||||
const int max_line_length = 80;
|
||||
static uint8_t buffer[max_line_length];
|
||||
|
||||
while (available()) {
|
||||
this->readline_(read(), buffer, max_line_length);
|
||||
while (this->available()) {
|
||||
this->readline_(this->read());
|
||||
}
|
||||
}
|
||||
|
||||
void LD2410Component::send_command_(uint8_t command, const uint8_t *command_value, int command_value_len) {
|
||||
void LD2410Component::send_command_(uint8_t command, const uint8_t *command_value, uint8_t command_value_len) {
|
||||
ESP_LOGV(TAG, "Sending COMMAND %02X", command);
|
||||
// frame start bytes
|
||||
this->write_array(CMD_FRAME_HEADER, 4);
|
||||
// frame header bytes
|
||||
this->write_array(CMD_FRAME_HEADER, sizeof(CMD_FRAME_HEADER));
|
||||
// length bytes
|
||||
int len = 2;
|
||||
if (command_value != nullptr)
|
||||
uint8_t len = 2;
|
||||
if (command_value != nullptr) {
|
||||
len += command_value_len;
|
||||
this->write_byte(lowbyte(len));
|
||||
this->write_byte(highbyte(len));
|
||||
|
||||
// command
|
||||
this->write_byte(lowbyte(command));
|
||||
this->write_byte(highbyte(command));
|
||||
}
|
||||
uint8_t len_cmd[] = {lowbyte(len), highbyte(len), command, 0x00};
|
||||
this->write_array(len_cmd, sizeof(len_cmd));
|
||||
|
||||
// command value bytes
|
||||
if (command_value != nullptr) {
|
||||
for (int i = 0; i < command_value_len; i++) {
|
||||
for (uint8_t i = 0; i < command_value_len; i++) {
|
||||
this->write_byte(command_value[i]);
|
||||
}
|
||||
}
|
||||
// frame end bytes
|
||||
this->write_array(CMD_FRAME_END, 4);
|
||||
// frame footer bytes
|
||||
this->write_array(CMD_FRAME_FOOTER, sizeof(CMD_FRAME_FOOTER));
|
||||
// FIXME to remove
|
||||
delay(50); // NOLINT
|
||||
}
|
||||
|
||||
void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) {
|
||||
if (len < 12)
|
||||
return; // 4 frame start bytes + 2 length bytes + 1 data end byte + 1 crc byte + 4 frame end bytes
|
||||
if (buffer[0] != 0xF4 || buffer[1] != 0xF3 || buffer[2] != 0xF2 || buffer[3] != 0xF1) // check 4 frame start bytes
|
||||
void LD2410Component::handle_periodic_data_() {
|
||||
// Reduce data update rate to reduce home assistant database growth
|
||||
// Check this first to prevent unnecessary processing done in later checks/parsing
|
||||
if (App.get_loop_component_start_time() - this->last_periodic_millis_ < this->throttle_) {
|
||||
return;
|
||||
if (buffer[7] != HEAD || buffer[len - 6] != END || buffer[len - 5] != CHECK) // Check constant values
|
||||
return; // data head=0xAA, data end=0x55, crc=0x00
|
||||
|
||||
/*
|
||||
Reduce data update rate to prevent home assistant database size grow fast
|
||||
*/
|
||||
int32_t current_millis = App.get_loop_component_start_time();
|
||||
if (current_millis - last_periodic_millis_ < this->throttle_)
|
||||
}
|
||||
// 4 frame header bytes + 2 length bytes + 1 data end byte + 1 crc byte + 4 frame footer bytes
|
||||
// data header=0xAA, data footer=0x55, crc=0x00
|
||||
if (this->buffer_pos_ < 12 || !ld2410::validate_header_footer(DATA_FRAME_HEADER, this->buffer_data_) ||
|
||||
this->buffer_data_[7] != HEADER || this->buffer_data_[this->buffer_pos_ - 6] != FOOTER ||
|
||||
this->buffer_data_[this->buffer_pos_ - 5] != CHECK) {
|
||||
return;
|
||||
last_periodic_millis_ = current_millis;
|
||||
}
|
||||
// Save the timestamp after validating the frame so, if invalid, we'll take the next frame immediately
|
||||
this->last_periodic_millis_ = App.get_loop_component_start_time();
|
||||
|
||||
/*
|
||||
Data Type: 7th
|
||||
0x01: Engineering mode
|
||||
0x02: Normal mode
|
||||
*/
|
||||
bool engineering_mode = buffer[DATA_TYPES] == 0x01;
|
||||
bool engineering_mode = this->buffer_data_[DATA_TYPES] == 0x01;
|
||||
#ifdef USE_SWITCH
|
||||
if (this->engineering_mode_switch_ != nullptr &&
|
||||
current_millis - last_engineering_mode_change_millis_ > this->throttle_) {
|
||||
if (this->engineering_mode_switch_ != nullptr) {
|
||||
this->engineering_mode_switch_->publish_state(engineering_mode);
|
||||
}
|
||||
#endif
|
||||
@ -335,7 +350,7 @@ void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) {
|
||||
0x02 = Still targets
|
||||
0x03 = Moving+Still targets
|
||||
*/
|
||||
char target_state = buffer[TARGET_STATES];
|
||||
char target_state = this->buffer_data_[TARGET_STATES];
|
||||
if (this->target_binary_sensor_ != nullptr) {
|
||||
this->target_binary_sensor_->publish_state(target_state != 0x00);
|
||||
}
|
||||
@ -355,27 +370,30 @@ void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) {
|
||||
*/
|
||||
#ifdef USE_SENSOR
|
||||
if (this->moving_target_distance_sensor_ != nullptr) {
|
||||
int new_moving_target_distance = ld2410::two_byte_to_int(buffer[MOVING_TARGET_LOW], buffer[MOVING_TARGET_HIGH]);
|
||||
int new_moving_target_distance =
|
||||
ld2410::two_byte_to_int(this->buffer_data_[MOVING_TARGET_LOW], this->buffer_data_[MOVING_TARGET_HIGH]);
|
||||
if (this->moving_target_distance_sensor_->get_state() != new_moving_target_distance)
|
||||
this->moving_target_distance_sensor_->publish_state(new_moving_target_distance);
|
||||
}
|
||||
if (this->moving_target_energy_sensor_ != nullptr) {
|
||||
int new_moving_target_energy = buffer[MOVING_ENERGY];
|
||||
int new_moving_target_energy = this->buffer_data_[MOVING_ENERGY];
|
||||
if (this->moving_target_energy_sensor_->get_state() != new_moving_target_energy)
|
||||
this->moving_target_energy_sensor_->publish_state(new_moving_target_energy);
|
||||
}
|
||||
if (this->still_target_distance_sensor_ != nullptr) {
|
||||
int new_still_target_distance = ld2410::two_byte_to_int(buffer[STILL_TARGET_LOW], buffer[STILL_TARGET_HIGH]);
|
||||
int new_still_target_distance =
|
||||
ld2410::two_byte_to_int(this->buffer_data_[STILL_TARGET_LOW], this->buffer_data_[STILL_TARGET_HIGH]);
|
||||
if (this->still_target_distance_sensor_->get_state() != new_still_target_distance)
|
||||
this->still_target_distance_sensor_->publish_state(new_still_target_distance);
|
||||
}
|
||||
if (this->still_target_energy_sensor_ != nullptr) {
|
||||
int new_still_target_energy = buffer[STILL_ENERGY];
|
||||
int new_still_target_energy = this->buffer_data_[STILL_ENERGY];
|
||||
if (this->still_target_energy_sensor_->get_state() != new_still_target_energy)
|
||||
this->still_target_energy_sensor_->publish_state(new_still_target_energy);
|
||||
}
|
||||
if (this->detection_distance_sensor_ != nullptr) {
|
||||
int new_detect_distance = ld2410::two_byte_to_int(buffer[DETECT_DISTANCE_LOW], buffer[DETECT_DISTANCE_HIGH]);
|
||||
int new_detect_distance =
|
||||
ld2410::two_byte_to_int(this->buffer_data_[DETECT_DISTANCE_LOW], this->buffer_data_[DETECT_DISTANCE_HIGH]);
|
||||
if (this->detection_distance_sensor_->get_state() != new_detect_distance)
|
||||
this->detection_distance_sensor_->publish_state(new_detect_distance);
|
||||
}
|
||||
@ -388,7 +406,7 @@ void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) {
|
||||
for (std::vector<sensor::Sensor *>::size_type i = 0; i != this->gate_move_sensors_.size(); i++) {
|
||||
sensor::Sensor *s = this->gate_move_sensors_[i];
|
||||
if (s != nullptr) {
|
||||
s->publish_state(buffer[MOVING_SENSOR_START + i]);
|
||||
s->publish_state(this->buffer_data_[MOVING_SENSOR_START + i]);
|
||||
}
|
||||
}
|
||||
/*
|
||||
@ -397,16 +415,17 @@ void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) {
|
||||
for (std::vector<sensor::Sensor *>::size_type i = 0; i != this->gate_still_sensors_.size(); i++) {
|
||||
sensor::Sensor *s = this->gate_still_sensors_[i];
|
||||
if (s != nullptr) {
|
||||
s->publish_state(buffer[STILL_SENSOR_START + i]);
|
||||
s->publish_state(this->buffer_data_[STILL_SENSOR_START + i]);
|
||||
}
|
||||
}
|
||||
/*
|
||||
Light sensor: 38th bytes
|
||||
*/
|
||||
if (this->light_sensor_ != nullptr) {
|
||||
int new_light_sensor = buffer[LIGHT_SENSOR];
|
||||
if (this->light_sensor_->get_state() != new_light_sensor)
|
||||
int new_light_sensor = this->buffer_data_[LIGHT_SENSOR];
|
||||
if (this->light_sensor_->get_state() != new_light_sensor) {
|
||||
this->light_sensor_->publish_state(new_light_sensor);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (auto *s : this->gate_move_sensors_) {
|
||||
@ -427,7 +446,7 @@ void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) {
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
if (engineering_mode) {
|
||||
if (this->out_pin_presence_status_binary_sensor_ != nullptr) {
|
||||
this->out_pin_presence_status_binary_sensor_->publish_state(buffer[OUT_PIN_SENSOR] == 0x01);
|
||||
this->out_pin_presence_status_binary_sensor_->publish_state(this->buffer_data_[OUT_PIN_SENSOR] == 0x01);
|
||||
}
|
||||
} else {
|
||||
if (this->out_pin_presence_status_binary_sensor_ != nullptr) {
|
||||
@ -439,127 +458,149 @@ void LD2410Component::handle_periodic_data_(uint8_t *buffer, int len) {
|
||||
|
||||
#ifdef USE_NUMBER
|
||||
std::function<void(void)> set_number_value(number::Number *n, float value) {
|
||||
float normalized_value = value * 1.0;
|
||||
if (n != nullptr && (!n->has_state() || n->state != normalized_value)) {
|
||||
n->state = normalized_value;
|
||||
return [n, normalized_value]() { n->publish_state(normalized_value); };
|
||||
if (n != nullptr && (!n->has_state() || n->state != value)) {
|
||||
n->state = value;
|
||||
return [n, value]() { n->publish_state(value); };
|
||||
}
|
||||
return []() {};
|
||||
}
|
||||
#endif
|
||||
|
||||
bool LD2410Component::handle_ack_data_(uint8_t *buffer, int len) {
|
||||
ESP_LOGV(TAG, "Handling ACK DATA for COMMAND %02X", buffer[COMMAND]);
|
||||
if (len < 10) {
|
||||
bool LD2410Component::handle_ack_data_() {
|
||||
ESP_LOGV(TAG, "Handling ACK DATA for COMMAND %02X", this->buffer_data_[COMMAND]);
|
||||
if (this->buffer_pos_ < 10) {
|
||||
ESP_LOGE(TAG, "Invalid length");
|
||||
return true;
|
||||
}
|
||||
if (buffer[0] != 0xFD || buffer[1] != 0xFC || buffer[2] != 0xFB || buffer[3] != 0xFA) { // check 4 frame start bytes
|
||||
ESP_LOGE(TAG, "Invalid header");
|
||||
if (!ld2410::validate_header_footer(CMD_FRAME_HEADER, this->buffer_data_)) {
|
||||
ESP_LOGW(TAG, "Invalid header: %s", format_hex_pretty(this->buffer_data_, HEADER_FOOTER_SIZE).c_str());
|
||||
return true;
|
||||
}
|
||||
if (buffer[COMMAND_STATUS] != 0x01) {
|
||||
if (this->buffer_data_[COMMAND_STATUS] != 0x01) {
|
||||
ESP_LOGE(TAG, "Invalid status");
|
||||
return true;
|
||||
}
|
||||
if (ld2410::two_byte_to_int(buffer[8], buffer[9]) != 0x00) {
|
||||
ESP_LOGE(TAG, "Invalid command: %u, %u", buffer[8], buffer[9]);
|
||||
if (ld2410::two_byte_to_int(this->buffer_data_[8], this->buffer_data_[9]) != 0x00) {
|
||||
ESP_LOGW(TAG, "Invalid command: %02X, %02X", this->buffer_data_[8], this->buffer_data_[9]);
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (buffer[COMMAND]) {
|
||||
case lowbyte(CMD_ENABLE_CONF):
|
||||
switch (this->buffer_data_[COMMAND]) {
|
||||
case CMD_ENABLE_CONF:
|
||||
ESP_LOGV(TAG, "Enable conf");
|
||||
break;
|
||||
case lowbyte(CMD_DISABLE_CONF):
|
||||
|
||||
case CMD_DISABLE_CONF:
|
||||
ESP_LOGV(TAG, "Disabled conf");
|
||||
break;
|
||||
case lowbyte(CMD_SET_BAUD_RATE):
|
||||
|
||||
case CMD_SET_BAUD_RATE:
|
||||
ESP_LOGV(TAG, "Baud rate change");
|
||||
#ifdef USE_SELECT
|
||||
if (this->baud_rate_select_ != nullptr) {
|
||||
ESP_LOGE(TAG, "Configure baud rate to %s and reinstall", this->baud_rate_select_->state.c_str());
|
||||
ESP_LOGE(TAG, "Change baud rate to %s and reinstall", this->baud_rate_select_->state.c_str());
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
case lowbyte(CMD_VERSION):
|
||||
this->version_ = str_sprintf(VERSION_FMT, buffer[13], buffer[12], buffer[17], buffer[16], buffer[15], buffer[14]);
|
||||
ESP_LOGV(TAG, "Firmware version: %s", this->version_.c_str());
|
||||
|
||||
case CMD_QUERY_VERSION: {
|
||||
std::memcpy(this->version_, &this->buffer_data_[12], sizeof(this->version_));
|
||||
std::string version = str_sprintf(VERSION_FMT, this->version_[1], this->version_[0], this->version_[5],
|
||||
this->version_[4], this->version_[3], this->version_[2]);
|
||||
ESP_LOGV(TAG, "Firmware version: %s", version.c_str());
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
if (this->version_text_sensor_ != nullptr) {
|
||||
this->version_text_sensor_->publish_state(this->version_);
|
||||
this->version_text_sensor_->publish_state(version);
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
case lowbyte(CMD_QUERY_DISTANCE_RESOLUTION): {
|
||||
std::string distance_resolution =
|
||||
find_str(DISTANCE_RESOLUTIONS_BY_UINT, ld2410::two_byte_to_int(buffer[10], buffer[11]));
|
||||
ESP_LOGV(TAG, "Distance resolution: %s", distance_resolution.c_str());
|
||||
}
|
||||
|
||||
case CMD_QUERY_DISTANCE_RESOLUTION: {
|
||||
const auto *distance_resolution = find_str(DISTANCE_RESOLUTIONS_BY_UINT, this->buffer_data_[10]);
|
||||
ESP_LOGV(TAG, "Distance resolution: %s", distance_resolution);
|
||||
#ifdef USE_SELECT
|
||||
if (this->distance_resolution_select_ != nullptr &&
|
||||
this->distance_resolution_select_->state != distance_resolution) {
|
||||
if (this->distance_resolution_select_ != nullptr) {
|
||||
this->distance_resolution_select_->publish_state(distance_resolution);
|
||||
}
|
||||
#endif
|
||||
} break;
|
||||
case lowbyte(CMD_QUERY_LIGHT_CONTROL): {
|
||||
this->light_function_ = find_str(LIGHT_FUNCTIONS_BY_UINT, buffer[10]);
|
||||
this->light_threshold_ = buffer[11] * 1.0;
|
||||
this->out_pin_level_ = find_str(OUT_PIN_LEVELS_BY_UINT, buffer[12]);
|
||||
ESP_LOGV(TAG, "Light function: %s", const_cast<char *>(this->light_function_.c_str()));
|
||||
ESP_LOGV(TAG, "Light threshold: %f", this->light_threshold_);
|
||||
ESP_LOGV(TAG, "Out pin level: %s", const_cast<char *>(this->out_pin_level_.c_str()));
|
||||
break;
|
||||
}
|
||||
|
||||
case CMD_QUERY_LIGHT_CONTROL: {
|
||||
this->light_function_ = this->buffer_data_[10];
|
||||
this->light_threshold_ = this->buffer_data_[11];
|
||||
this->out_pin_level_ = this->buffer_data_[12];
|
||||
const auto *light_function_str = find_str(LIGHT_FUNCTIONS_BY_UINT, this->light_function_);
|
||||
const auto *out_pin_level_str = find_str(OUT_PIN_LEVELS_BY_UINT, this->out_pin_level_);
|
||||
ESP_LOGV(TAG,
|
||||
"Light function is: %s\n"
|
||||
"Light threshold is: %u\n"
|
||||
"Out pin level: %s",
|
||||
light_function_str, this->light_threshold_, out_pin_level_str);
|
||||
#ifdef USE_SELECT
|
||||
if (this->light_function_select_ != nullptr && this->light_function_select_->state != this->light_function_) {
|
||||
this->light_function_select_->publish_state(this->light_function_);
|
||||
if (this->light_function_select_ != nullptr) {
|
||||
this->light_function_select_->publish_state(light_function_str);
|
||||
}
|
||||
if (this->out_pin_level_select_ != nullptr && this->out_pin_level_select_->state != this->out_pin_level_) {
|
||||
this->out_pin_level_select_->publish_state(this->out_pin_level_);
|
||||
if (this->out_pin_level_select_ != nullptr) {
|
||||
this->out_pin_level_select_->publish_state(out_pin_level_str);
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_NUMBER
|
||||
if (this->light_threshold_number_ != nullptr &&
|
||||
(!this->light_threshold_number_->has_state() ||
|
||||
this->light_threshold_number_->state != this->light_threshold_)) {
|
||||
this->light_threshold_number_->publish_state(this->light_threshold_);
|
||||
if (this->light_threshold_number_ != nullptr) {
|
||||
this->light_threshold_number_->publish_state(static_cast<float>(this->light_threshold_));
|
||||
}
|
||||
#endif
|
||||
} break;
|
||||
case lowbyte(CMD_MAC):
|
||||
if (len < 20) {
|
||||
break;
|
||||
}
|
||||
case CMD_QUERY_MAC_ADDRESS: {
|
||||
if (this->buffer_pos_ < 20) {
|
||||
return false;
|
||||
}
|
||||
this->mac_ = format_mac_address_pretty(&buffer[10]);
|
||||
ESP_LOGV(TAG, "MAC address: %s", this->mac_.c_str());
|
||||
|
||||
this->bluetooth_on_ = std::memcmp(&this->buffer_data_[10], NO_MAC, sizeof(NO_MAC)) != 0;
|
||||
if (this->bluetooth_on_) {
|
||||
std::memcpy(this->mac_address_, &this->buffer_data_[10], sizeof(this->mac_address_));
|
||||
}
|
||||
|
||||
std::string mac_str =
|
||||
mac_address_is_valid(this->mac_address_) ? format_mac_address_pretty(this->mac_address_) : UNKNOWN_MAC;
|
||||
ESP_LOGV(TAG, "MAC address: %s", mac_str.c_str());
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
if (this->mac_text_sensor_ != nullptr) {
|
||||
this->mac_text_sensor_->publish_state(this->mac_ == NO_MAC ? UNKNOWN_MAC : this->mac_);
|
||||
this->mac_text_sensor_->publish_state(mac_str);
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_SWITCH
|
||||
if (this->bluetooth_switch_ != nullptr) {
|
||||
this->bluetooth_switch_->publish_state(this->mac_ != NO_MAC);
|
||||
this->bluetooth_switch_->publish_state(this->bluetooth_on_);
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
case lowbyte(CMD_GATE_SENS):
|
||||
}
|
||||
|
||||
case CMD_GATE_SENS:
|
||||
ESP_LOGV(TAG, "Sensitivity");
|
||||
break;
|
||||
case lowbyte(CMD_BLUETOOTH):
|
||||
|
||||
case CMD_BLUETOOTH:
|
||||
ESP_LOGV(TAG, "Bluetooth");
|
||||
break;
|
||||
case lowbyte(CMD_SET_DISTANCE_RESOLUTION):
|
||||
|
||||
case CMD_SET_DISTANCE_RESOLUTION:
|
||||
ESP_LOGV(TAG, "Set distance resolution");
|
||||
break;
|
||||
case lowbyte(CMD_SET_LIGHT_CONTROL):
|
||||
|
||||
case CMD_SET_LIGHT_CONTROL:
|
||||
ESP_LOGV(TAG, "Set light control");
|
||||
break;
|
||||
case lowbyte(CMD_BT_PASSWORD):
|
||||
|
||||
case CMD_BT_PASSWORD:
|
||||
ESP_LOGV(TAG, "Set bluetooth password");
|
||||
break;
|
||||
case lowbyte(CMD_QUERY): // Query parameters response
|
||||
{
|
||||
if (buffer[10] != 0xAA)
|
||||
|
||||
case CMD_QUERY: { // Query parameters response
|
||||
if (this->buffer_data_[10] != 0xAA)
|
||||
return true; // value head=0xAA
|
||||
#ifdef USE_NUMBER
|
||||
/*
|
||||
@ -567,29 +608,31 @@ bool LD2410Component::handle_ack_data_(uint8_t *buffer, int len) {
|
||||
Still distance range: 14th byte
|
||||
*/
|
||||
std::vector<std::function<void(void)>> updates;
|
||||
updates.push_back(set_number_value(this->max_move_distance_gate_number_, buffer[12]));
|
||||
updates.push_back(set_number_value(this->max_still_distance_gate_number_, buffer[13]));
|
||||
updates.push_back(set_number_value(this->max_move_distance_gate_number_, this->buffer_data_[12]));
|
||||
updates.push_back(set_number_value(this->max_still_distance_gate_number_, this->buffer_data_[13]));
|
||||
/*
|
||||
Moving Sensitivities: 15~23th bytes
|
||||
*/
|
||||
for (std::vector<number::Number *>::size_type i = 0; i != this->gate_move_threshold_numbers_.size(); i++) {
|
||||
updates.push_back(set_number_value(this->gate_move_threshold_numbers_[i], buffer[14 + i]));
|
||||
updates.push_back(set_number_value(this->gate_move_threshold_numbers_[i], this->buffer_data_[14 + i]));
|
||||
}
|
||||
/*
|
||||
Still Sensitivities: 24~32th bytes
|
||||
*/
|
||||
for (std::vector<number::Number *>::size_type i = 0; i != this->gate_still_threshold_numbers_.size(); i++) {
|
||||
updates.push_back(set_number_value(this->gate_still_threshold_numbers_[i], buffer[23 + i]));
|
||||
updates.push_back(set_number_value(this->gate_still_threshold_numbers_[i], this->buffer_data_[23 + i]));
|
||||
}
|
||||
/*
|
||||
None Duration: 33~34th bytes
|
||||
*/
|
||||
updates.push_back(set_number_value(this->timeout_number_, ld2410::two_byte_to_int(buffer[32], buffer[33])));
|
||||
updates.push_back(set_number_value(this->timeout_number_,
|
||||
ld2410::two_byte_to_int(this->buffer_data_[32], this->buffer_data_[33])));
|
||||
for (auto &update : updates) {
|
||||
update();
|
||||
}
|
||||
#endif
|
||||
} break;
|
||||
break;
|
||||
}
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@ -597,59 +640,66 @@ bool LD2410Component::handle_ack_data_(uint8_t *buffer, int len) {
|
||||
return true;
|
||||
}
|
||||
|
||||
void LD2410Component::readline_(int readch, uint8_t *buffer, int len) {
|
||||
static int pos = 0;
|
||||
void LD2410Component::readline_(int readch) {
|
||||
if (readch < 0) {
|
||||
return; // No data available
|
||||
}
|
||||
|
||||
if (readch >= 0) {
|
||||
if (pos < len - 1) {
|
||||
buffer[pos++] = readch;
|
||||
buffer[pos] = 0;
|
||||
if (this->buffer_pos_ < MAX_LINE_LENGTH - 1) {
|
||||
this->buffer_data_[this->buffer_pos_++] = readch;
|
||||
this->buffer_data_[this->buffer_pos_] = 0;
|
||||
} else {
|
||||
// We should never get here, but just in case...
|
||||
ESP_LOGW(TAG, "Max command length exceeded; ignoring");
|
||||
this->buffer_pos_ = 0;
|
||||
}
|
||||
if (this->buffer_pos_ < 4) {
|
||||
return; // Not enough data to process yet
|
||||
}
|
||||
if (this->buffer_data_[this->buffer_pos_ - 4] == DATA_FRAME_FOOTER[0] &&
|
||||
this->buffer_data_[this->buffer_pos_ - 3] == DATA_FRAME_FOOTER[1] &&
|
||||
this->buffer_data_[this->buffer_pos_ - 2] == DATA_FRAME_FOOTER[2] &&
|
||||
this->buffer_data_[this->buffer_pos_ - 1] == DATA_FRAME_FOOTER[3]) {
|
||||
ESP_LOGV(TAG, "Handling Periodic Data: %s", format_hex_pretty(this->buffer_data_, this->buffer_pos_).c_str());
|
||||
this->handle_periodic_data_();
|
||||
this->buffer_pos_ = 0; // Reset position index for next message
|
||||
} else if (this->buffer_data_[this->buffer_pos_ - 4] == CMD_FRAME_FOOTER[0] &&
|
||||
this->buffer_data_[this->buffer_pos_ - 3] == CMD_FRAME_FOOTER[1] &&
|
||||
this->buffer_data_[this->buffer_pos_ - 2] == CMD_FRAME_FOOTER[2] &&
|
||||
this->buffer_data_[this->buffer_pos_ - 1] == CMD_FRAME_FOOTER[3]) {
|
||||
ESP_LOGV(TAG, "Handling Ack Data: %s", format_hex_pretty(this->buffer_data_, this->buffer_pos_).c_str());
|
||||
if (this->handle_ack_data_()) {
|
||||
this->buffer_pos_ = 0; // Reset position index for next message
|
||||
} else {
|
||||
pos = 0;
|
||||
}
|
||||
if (pos >= 4) {
|
||||
if (buffer[pos - 4] == 0xF8 && buffer[pos - 3] == 0xF7 && buffer[pos - 2] == 0xF6 && buffer[pos - 1] == 0xF5) {
|
||||
ESP_LOGV(TAG, "Will handle Periodic Data");
|
||||
this->handle_periodic_data_(buffer, pos);
|
||||
pos = 0; // Reset position index ready for next time
|
||||
} else if (buffer[pos - 4] == 0x04 && buffer[pos - 3] == 0x03 && buffer[pos - 2] == 0x02 &&
|
||||
buffer[pos - 1] == 0x01) {
|
||||
ESP_LOGV(TAG, "Will handle ACK Data");
|
||||
if (this->handle_ack_data_(buffer, pos)) {
|
||||
pos = 0; // Reset position index ready for next time
|
||||
} else {
|
||||
ESP_LOGV(TAG, "ACK Data incomplete");
|
||||
}
|
||||
}
|
||||
ESP_LOGV(TAG, "Ack Data incomplete");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void LD2410Component::set_config_mode_(bool enable) {
|
||||
uint8_t cmd = enable ? CMD_ENABLE_CONF : CMD_DISABLE_CONF;
|
||||
uint8_t cmd_value[2] = {0x01, 0x00};
|
||||
this->send_command_(cmd, enable ? cmd_value : nullptr, 2);
|
||||
const uint8_t cmd = enable ? CMD_ENABLE_CONF : CMD_DISABLE_CONF;
|
||||
const uint8_t cmd_value[2] = {0x01, 0x00};
|
||||
this->send_command_(cmd, enable ? cmd_value : nullptr, sizeof(cmd_value));
|
||||
}
|
||||
|
||||
void LD2410Component::set_bluetooth(bool enable) {
|
||||
this->set_config_mode_(true);
|
||||
uint8_t enable_cmd_value[2] = {0x01, 0x00};
|
||||
uint8_t disable_cmd_value[2] = {0x00, 0x00};
|
||||
this->send_command_(CMD_BLUETOOTH, enable ? enable_cmd_value : disable_cmd_value, 2);
|
||||
const uint8_t cmd_value[2] = {enable ? (uint8_t) 0x01 : (uint8_t) 0x00, 0x00};
|
||||
this->send_command_(CMD_BLUETOOTH, cmd_value, sizeof(cmd_value));
|
||||
this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
|
||||
}
|
||||
|
||||
void LD2410Component::set_distance_resolution(const std::string &state) {
|
||||
this->set_config_mode_(true);
|
||||
uint8_t cmd_value[2] = {find_uint8(DISTANCE_RESOLUTIONS_BY_STR, state), 0x00};
|
||||
this->send_command_(CMD_SET_DISTANCE_RESOLUTION, cmd_value, 2);
|
||||
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 std::string &state) {
|
||||
this->set_config_mode_(true);
|
||||
uint8_t cmd_value[2] = {find_uint8(BAUD_RATES_BY_STR, state), 0x00};
|
||||
this->send_command_(CMD_SET_BAUD_RATE, cmd_value, 2);
|
||||
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));
|
||||
this->set_timeout(200, [this]() { this->restart_(); });
|
||||
}
|
||||
|
||||
@ -661,14 +711,13 @@ void LD2410Component::set_bluetooth_password(const std::string &password) {
|
||||
this->set_config_mode_(true);
|
||||
uint8_t cmd_value[6];
|
||||
std::copy(password.begin(), password.end(), std::begin(cmd_value));
|
||||
this->send_command_(CMD_BT_PASSWORD, cmd_value, 6);
|
||||
this->send_command_(CMD_BT_PASSWORD, cmd_value, sizeof(cmd_value));
|
||||
this->set_config_mode_(false);
|
||||
}
|
||||
|
||||
void LD2410Component::set_engineering_mode(bool enable) {
|
||||
const uint8_t cmd = enable ? CMD_ENABLE_ENG : CMD_DISABLE_ENG;
|
||||
this->set_config_mode_(true);
|
||||
last_engineering_mode_change_millis_ = App.get_loop_component_start_time();
|
||||
uint8_t cmd = enable ? CMD_ENABLE_ENG : CMD_DISABLE_ENG;
|
||||
this->send_command_(cmd, nullptr, 0);
|
||||
this->set_config_mode_(false);
|
||||
}
|
||||
@ -682,14 +731,17 @@ void LD2410Component::factory_reset() {
|
||||
void LD2410Component::restart_() { this->send_command_(CMD_RESTART, nullptr, 0); }
|
||||
|
||||
void LD2410Component::query_parameters_() { this->send_command_(CMD_QUERY, nullptr, 0); }
|
||||
void LD2410Component::get_version_() { this->send_command_(CMD_VERSION, nullptr, 0); }
|
||||
|
||||
void LD2410Component::get_version_() { this->send_command_(CMD_QUERY_VERSION, nullptr, 0); }
|
||||
|
||||
void LD2410Component::get_mac_() {
|
||||
uint8_t cmd_value[2] = {0x01, 0x00};
|
||||
this->send_command_(CMD_MAC, cmd_value, 2);
|
||||
const uint8_t cmd_value[2] = {0x01, 0x00};
|
||||
this->send_command_(CMD_QUERY_MAC_ADDRESS, cmd_value, sizeof(cmd_value));
|
||||
}
|
||||
|
||||
void LD2410Component::get_distance_resolution_() { this->send_command_(CMD_QUERY_DISTANCE_RESOLUTION, nullptr, 0); }
|
||||
|
||||
void LD2410Component::get_light_control_() { this->send_command_(CMD_QUERY_LIGHT_CONTROL, nullptr, 0); }
|
||||
void LD2410Component::query_light_control_() { this->send_command_(CMD_QUERY_LIGHT_CONTROL, nullptr, 0); }
|
||||
|
||||
#ifdef USE_NUMBER
|
||||
void LD2410Component::set_max_distances_timeout() {
|
||||
@ -719,7 +771,7 @@ void LD2410Component::set_max_distances_timeout() {
|
||||
0x00,
|
||||
0x00};
|
||||
this->set_config_mode_(true);
|
||||
this->send_command_(CMD_MAXDIST_DURATION, value, 18);
|
||||
this->send_command_(CMD_MAXDIST_DURATION, value, sizeof(value));
|
||||
delay(50); // NOLINT
|
||||
this->query_parameters_();
|
||||
this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
|
||||
@ -749,17 +801,17 @@ void LD2410Component::set_gate_threshold(uint8_t gate) {
|
||||
uint8_t value[18] = {0x00, 0x00, lowbyte(gate), highbyte(gate), 0x00, 0x00,
|
||||
0x01, 0x00, lowbyte(motion), highbyte(motion), 0x00, 0x00,
|
||||
0x02, 0x00, lowbyte(still), highbyte(still), 0x00, 0x00};
|
||||
this->send_command_(CMD_GATE_SENS, value, 18);
|
||||
this->send_command_(CMD_GATE_SENS, value, sizeof(value));
|
||||
delay(50); // NOLINT
|
||||
this->query_parameters_();
|
||||
this->set_config_mode_(false);
|
||||
}
|
||||
|
||||
void LD2410Component::set_gate_still_threshold_number(int gate, number::Number *n) {
|
||||
void LD2410Component::set_gate_still_threshold_number(uint8_t gate, number::Number *n) {
|
||||
this->gate_still_threshold_numbers_[gate] = n;
|
||||
}
|
||||
|
||||
void LD2410Component::set_gate_move_threshold_number(int gate, number::Number *n) {
|
||||
void LD2410Component::set_gate_move_threshold_number(uint8_t gate, number::Number *n) {
|
||||
this->gate_move_threshold_numbers_[gate] = n;
|
||||
}
|
||||
#endif
|
||||
@ -767,35 +819,29 @@ void LD2410Component::set_gate_move_threshold_number(int gate, number::Number *n
|
||||
void LD2410Component::set_light_out_control() {
|
||||
#ifdef USE_NUMBER
|
||||
if (this->light_threshold_number_ != nullptr && this->light_threshold_number_->has_state()) {
|
||||
this->light_threshold_ = this->light_threshold_number_->state;
|
||||
this->light_threshold_ = static_cast<uint8_t>(this->light_threshold_number_->state);
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_SELECT
|
||||
if (this->light_function_select_ != nullptr && this->light_function_select_->has_state()) {
|
||||
this->light_function_ = this->light_function_select_->state;
|
||||
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_ = this->out_pin_level_select_->state;
|
||||
this->out_pin_level_ = find_uint8(OUT_PIN_LEVELS_BY_STR, this->out_pin_level_select_->state);
|
||||
}
|
||||
#endif
|
||||
if (this->light_function_.empty() || this->out_pin_level_.empty() || this->light_threshold_ < 0) {
|
||||
return;
|
||||
}
|
||||
this->set_config_mode_(true);
|
||||
uint8_t light_function = find_uint8(LIGHT_FUNCTIONS_BY_STR, this->light_function_);
|
||||
uint8_t light_threshold = static_cast<uint8_t>(this->light_threshold_);
|
||||
uint8_t out_pin_level = find_uint8(OUT_PIN_LEVELS_BY_STR, this->out_pin_level_);
|
||||
uint8_t value[4] = {light_function, light_threshold, out_pin_level, 0x00};
|
||||
this->send_command_(CMD_SET_LIGHT_CONTROL, value, 4);
|
||||
uint8_t value[4] = {this->light_function_, this->light_threshold_, this->out_pin_level_, 0x00};
|
||||
this->send_command_(CMD_SET_LIGHT_CONTROL, value, sizeof(value));
|
||||
delay(50); // NOLINT
|
||||
this->get_light_control_();
|
||||
this->query_light_control_();
|
||||
this->set_timeout(200, [this]() { this->restart_and_read_all_info(); });
|
||||
this->set_config_mode_(false);
|
||||
}
|
||||
|
||||
#ifdef USE_SENSOR
|
||||
void LD2410Component::set_gate_move_sensor(int gate, sensor::Sensor *s) { this->gate_move_sensors_[gate] = s; }
|
||||
void LD2410Component::set_gate_still_sensor(int gate, sensor::Sensor *s) { this->gate_still_sensors_[gate] = s; }
|
||||
void LD2410Component::set_gate_move_sensor(uint8_t gate, sensor::Sensor *s) { this->gate_move_sensors_[gate] = s; }
|
||||
void LD2410Component::set_gate_still_sensor(uint8_t gate, sensor::Sensor *s) { this->gate_still_sensors_[gate] = s; }
|
||||
#endif
|
||||
|
||||
} // namespace ld2410
|
||||
|
@ -29,45 +29,48 @@
|
||||
namespace esphome {
|
||||
namespace ld2410 {
|
||||
|
||||
static const uint8_t MAX_LINE_LENGTH = 46; // Max characters for serial buffer
|
||||
static const uint8_t TOTAL_GATES = 9; // Total number of gates supported by the LD2410
|
||||
|
||||
class LD2410Component : public Component, public uart::UARTDevice {
|
||||
#ifdef USE_SENSOR
|
||||
SUB_SENSOR(moving_target_distance)
|
||||
SUB_SENSOR(still_target_distance)
|
||||
SUB_SENSOR(moving_target_energy)
|
||||
SUB_SENSOR(still_target_energy)
|
||||
SUB_SENSOR(light)
|
||||
SUB_SENSOR(detection_distance)
|
||||
#endif
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
SUB_BINARY_SENSOR(target)
|
||||
SUB_BINARY_SENSOR(out_pin_presence_status)
|
||||
SUB_BINARY_SENSOR(moving_target)
|
||||
SUB_BINARY_SENSOR(still_target)
|
||||
SUB_BINARY_SENSOR(out_pin_presence_status)
|
||||
SUB_BINARY_SENSOR(target)
|
||||
#endif
|
||||
#ifdef USE_SENSOR
|
||||
SUB_SENSOR(light)
|
||||
SUB_SENSOR(detection_distance)
|
||||
SUB_SENSOR(moving_target_distance)
|
||||
SUB_SENSOR(moving_target_energy)
|
||||
SUB_SENSOR(still_target_distance)
|
||||
SUB_SENSOR(still_target_energy)
|
||||
#endif
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
SUB_TEXT_SENSOR(version)
|
||||
SUB_TEXT_SENSOR(mac)
|
||||
#endif
|
||||
#ifdef USE_NUMBER
|
||||
SUB_NUMBER(light_threshold)
|
||||
SUB_NUMBER(max_move_distance_gate)
|
||||
SUB_NUMBER(max_still_distance_gate)
|
||||
SUB_NUMBER(timeout)
|
||||
#endif
|
||||
#ifdef USE_SELECT
|
||||
SUB_SELECT(distance_resolution)
|
||||
SUB_SELECT(baud_rate)
|
||||
SUB_SELECT(distance_resolution)
|
||||
SUB_SELECT(light_function)
|
||||
SUB_SELECT(out_pin_level)
|
||||
#endif
|
||||
#ifdef USE_SWITCH
|
||||
SUB_SWITCH(engineering_mode)
|
||||
SUB_SWITCH(bluetooth)
|
||||
SUB_SWITCH(engineering_mode)
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
SUB_BUTTON(reset)
|
||||
SUB_BUTTON(restart)
|
||||
SUB_BUTTON(factory_reset)
|
||||
SUB_BUTTON(query)
|
||||
#endif
|
||||
#ifdef USE_NUMBER
|
||||
SUB_NUMBER(max_still_distance_gate)
|
||||
SUB_NUMBER(max_move_distance_gate)
|
||||
SUB_NUMBER(timeout)
|
||||
SUB_NUMBER(light_threshold)
|
||||
SUB_BUTTON(restart)
|
||||
#endif
|
||||
|
||||
public:
|
||||
@ -76,14 +79,14 @@ class LD2410Component : public Component, public uart::UARTDevice {
|
||||
void loop() override;
|
||||
void set_light_out_control();
|
||||
#ifdef USE_NUMBER
|
||||
void set_gate_still_threshold_number(int gate, number::Number *n);
|
||||
void set_gate_move_threshold_number(int gate, number::Number *n);
|
||||
void set_gate_still_threshold_number(uint8_t gate, number::Number *n);
|
||||
void set_gate_move_threshold_number(uint8_t gate, number::Number *n);
|
||||
void set_max_distances_timeout();
|
||||
void set_gate_threshold(uint8_t gate);
|
||||
#endif
|
||||
#ifdef USE_SENSOR
|
||||
void set_gate_move_sensor(int gate, sensor::Sensor *s);
|
||||
void set_gate_still_sensor(int gate, sensor::Sensor *s);
|
||||
void set_gate_move_sensor(uint8_t gate, sensor::Sensor *s);
|
||||
void set_gate_still_sensor(uint8_t gate, sensor::Sensor *s);
|
||||
#endif
|
||||
void set_throttle(uint16_t value) { this->throttle_ = value; };
|
||||
void set_bluetooth_password(const std::string &password);
|
||||
@ -96,33 +99,35 @@ class LD2410Component : public Component, public uart::UARTDevice {
|
||||
void factory_reset();
|
||||
|
||||
protected:
|
||||
void send_command_(uint8_t command_str, const uint8_t *command_value, int command_value_len);
|
||||
void send_command_(uint8_t command_str, const uint8_t *command_value, uint8_t command_value_len);
|
||||
void set_config_mode_(bool enable);
|
||||
void handle_periodic_data_(uint8_t *buffer, int len);
|
||||
bool handle_ack_data_(uint8_t *buffer, int len);
|
||||
void readline_(int readch, uint8_t *buffer, int len);
|
||||
void handle_periodic_data_();
|
||||
bool handle_ack_data_();
|
||||
void readline_(int readch);
|
||||
void query_parameters_();
|
||||
void get_version_();
|
||||
void get_mac_();
|
||||
void get_distance_resolution_();
|
||||
void get_light_control_();
|
||||
void query_light_control_();
|
||||
void restart_();
|
||||
|
||||
int32_t last_periodic_millis_ = 0;
|
||||
int32_t last_engineering_mode_change_millis_ = 0;
|
||||
uint16_t throttle_;
|
||||
float light_threshold_ = -1;
|
||||
std::string version_;
|
||||
std::string mac_;
|
||||
std::string out_pin_level_;
|
||||
std::string light_function_;
|
||||
uint32_t last_periodic_millis_ = 0;
|
||||
uint16_t throttle_ = 0;
|
||||
uint8_t light_function_ = 0;
|
||||
uint8_t light_threshold_ = 0;
|
||||
uint8_t out_pin_level_ = 0;
|
||||
uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer
|
||||
uint8_t buffer_data_[MAX_LINE_LENGTH];
|
||||
uint8_t mac_address_[6] = {0, 0, 0, 0, 0, 0};
|
||||
uint8_t version_[6] = {0, 0, 0, 0, 0, 0};
|
||||
bool bluetooth_on_{false};
|
||||
#ifdef USE_NUMBER
|
||||
std::vector<number::Number *> gate_still_threshold_numbers_ = std::vector<number::Number *>(9);
|
||||
std::vector<number::Number *> gate_move_threshold_numbers_ = std::vector<number::Number *>(9);
|
||||
std::vector<number::Number *> gate_move_threshold_numbers_ = std::vector<number::Number *>(TOTAL_GATES);
|
||||
std::vector<number::Number *> gate_still_threshold_numbers_ = std::vector<number::Number *>(TOTAL_GATES);
|
||||
#endif
|
||||
#ifdef USE_SENSOR
|
||||
std::vector<sensor::Sensor *> gate_still_sensors_ = std::vector<sensor::Sensor *>(9);
|
||||
std::vector<sensor::Sensor *> gate_move_sensors_ = std::vector<sensor::Sensor *>(9);
|
||||
std::vector<sensor::Sensor *> gate_move_sensors_ = std::vector<sensor::Sensor *>(TOTAL_GATES);
|
||||
std::vector<sensor::Sensor *> gate_still_sensors_ = std::vector<sensor::Sensor *>(TOTAL_GATES);
|
||||
#endif
|
||||
};
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
#include "ld2450.h"
|
||||
#include <utility>
|
||||
#include <cmath>
|
||||
#ifdef USE_NUMBER
|
||||
#include "esphome/components/number/number.h"
|
||||
#endif
|
||||
@ -123,16 +124,11 @@ static const uint8_t CMD_SET_ZONE = 0xC2;
|
||||
|
||||
static inline uint16_t convert_seconds_to_ms(uint16_t value) { return value * 1000; };
|
||||
|
||||
static inline std::string convert_signed_int_to_hex(int value) {
|
||||
auto value_as_str = str_snprintf("%04x", 4, value & 0xFFFF);
|
||||
return value_as_str;
|
||||
}
|
||||
|
||||
static inline void convert_int_values_to_hex(const int *values, uint8_t *bytes) {
|
||||
for (int i = 0; i < 4; i++) {
|
||||
std::string temp_hex = convert_signed_int_to_hex(values[i]);
|
||||
bytes[i * 2] = std::stoi(temp_hex.substr(2, 2), nullptr, 16); // Store high byte
|
||||
bytes[i * 2 + 1] = std::stoi(temp_hex.substr(0, 2), nullptr, 16); // Store low byte
|
||||
uint16_t val = values[i] & 0xFFFF;
|
||||
bytes[i * 2] = val & 0xFF; // Store low byte first (little-endian)
|
||||
bytes[i * 2 + 1] = (val >> 8) & 0xFF; // Store high byte second
|
||||
}
|
||||
}
|
||||
|
||||
@ -428,6 +424,12 @@ void LD2450Component::send_command_(uint8_t command, const uint8_t *command_valu
|
||||
// [AA FF 03 00] [0E 03 B1 86 10 00 40 01] [00 00 00 00 00 00 00 00] [00 00 00 00 00 00 00 00] [55 CC]
|
||||
// Header Target 1 Target 2 Target 3 End
|
||||
void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) {
|
||||
// Early throttle check - moved before any processing to save CPU cycles
|
||||
if (App.get_loop_component_start_time() - this->last_periodic_millis_ < this->throttle_) {
|
||||
ESP_LOGV(TAG, "Throttling: %d", this->throttle_);
|
||||
return;
|
||||
}
|
||||
|
||||
if (len < 29) { // header (4 bytes) + 8 x 3 target data + footer (2 bytes)
|
||||
ESP_LOGE(TAG, "Invalid message length");
|
||||
return;
|
||||
@ -441,11 +443,6 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (App.get_loop_component_start_time() - this->last_periodic_millis_ < this->throttle_) {
|
||||
ESP_LOGV(TAG, "Throttling: %d", this->throttle_);
|
||||
return;
|
||||
}
|
||||
|
||||
this->last_periodic_millis_ = App.get_loop_component_start_time();
|
||||
|
||||
int16_t target_count = 0;
|
||||
@ -473,7 +470,10 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) {
|
||||
if (sx != nullptr) {
|
||||
val = ld2450::decode_coordinate(buffer[start], buffer[start + 1]);
|
||||
tx = val;
|
||||
sx->publish_state(val);
|
||||
if (this->cached_target_data_[index].x != val) {
|
||||
sx->publish_state(val);
|
||||
this->cached_target_data_[index].x = val;
|
||||
}
|
||||
}
|
||||
// Y
|
||||
start = TARGET_Y + index * 8;
|
||||
@ -481,14 +481,20 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) {
|
||||
if (sy != nullptr) {
|
||||
val = ld2450::decode_coordinate(buffer[start], buffer[start + 1]);
|
||||
ty = val;
|
||||
sy->publish_state(val);
|
||||
if (this->cached_target_data_[index].y != val) {
|
||||
sy->publish_state(val);
|
||||
this->cached_target_data_[index].y = val;
|
||||
}
|
||||
}
|
||||
// RESOLUTION
|
||||
start = TARGET_RESOLUTION + index * 8;
|
||||
sensor::Sensor *sr = this->move_resolution_sensors_[index];
|
||||
if (sr != nullptr) {
|
||||
val = (buffer[start + 1] << 8) | buffer[start];
|
||||
sr->publish_state(val);
|
||||
if (this->cached_target_data_[index].resolution != val) {
|
||||
sr->publish_state(val);
|
||||
this->cached_target_data_[index].resolution = val;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
// SPEED
|
||||
@ -502,13 +508,17 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) {
|
||||
#ifdef USE_SENSOR
|
||||
sensor::Sensor *ss = this->move_speed_sensors_[index];
|
||||
if (ss != nullptr) {
|
||||
ss->publish_state(val);
|
||||
if (this->cached_target_data_[index].speed != val) {
|
||||
ss->publish_state(val);
|
||||
this->cached_target_data_[index].speed = val;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
// DISTANCE
|
||||
val = (uint16_t) sqrt(
|
||||
pow(ld2450::decode_coordinate(buffer[TARGET_X + index * 8], buffer[(TARGET_X + index * 8) + 1]), 2) +
|
||||
pow(ld2450::decode_coordinate(buffer[TARGET_Y + index * 8], buffer[(TARGET_Y + index * 8) + 1]), 2));
|
||||
// Optimized: use already decoded tx and ty values, replace pow() with multiplication
|
||||
int32_t x_squared = (int32_t) tx * tx;
|
||||
int32_t y_squared = (int32_t) ty * ty;
|
||||
val = (uint16_t) sqrt(x_squared + y_squared);
|
||||
td = val;
|
||||
if (val > 0) {
|
||||
target_count++;
|
||||
@ -516,7 +526,10 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) {
|
||||
#ifdef USE_SENSOR
|
||||
sensor::Sensor *sd = this->move_distance_sensors_[index];
|
||||
if (sd != nullptr) {
|
||||
sd->publish_state(val);
|
||||
if (this->cached_target_data_[index].distance != val) {
|
||||
sd->publish_state(val);
|
||||
this->cached_target_data_[index].distance = val;
|
||||
}
|
||||
}
|
||||
// ANGLE
|
||||
angle = calculate_angle(static_cast<float>(ty), static_cast<float>(td));
|
||||
@ -525,7 +538,11 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) {
|
||||
}
|
||||
sensor::Sensor *sa = this->move_angle_sensors_[index];
|
||||
if (sa != nullptr) {
|
||||
sa->publish_state(angle);
|
||||
if (std::isnan(this->cached_target_data_[index].angle) ||
|
||||
std::abs(this->cached_target_data_[index].angle - angle) > 0.1f) {
|
||||
sa->publish_state(angle);
|
||||
this->cached_target_data_[index].angle = angle;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
@ -536,7 +553,10 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) {
|
||||
}
|
||||
text_sensor::TextSensor *tsd = this->direction_text_sensors_[index];
|
||||
if (tsd != nullptr) {
|
||||
tsd->publish_state(direction);
|
||||
if (this->cached_target_data_[index].direction != direction) {
|
||||
tsd->publish_state(direction);
|
||||
this->cached_target_data_[index].direction = direction;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@ -563,32 +583,50 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) {
|
||||
// Publish Still Target Count in Zones
|
||||
sensor::Sensor *szstc = this->zone_still_target_count_sensors_[index];
|
||||
if (szstc != nullptr) {
|
||||
szstc->publish_state(zone_still_targets);
|
||||
if (this->cached_zone_data_[index].still_count != zone_still_targets) {
|
||||
szstc->publish_state(zone_still_targets);
|
||||
this->cached_zone_data_[index].still_count = zone_still_targets;
|
||||
}
|
||||
}
|
||||
// Publish Moving Target Count in Zones
|
||||
sensor::Sensor *szmtc = this->zone_moving_target_count_sensors_[index];
|
||||
if (szmtc != nullptr) {
|
||||
szmtc->publish_state(zone_moving_targets);
|
||||
if (this->cached_zone_data_[index].moving_count != zone_moving_targets) {
|
||||
szmtc->publish_state(zone_moving_targets);
|
||||
this->cached_zone_data_[index].moving_count = zone_moving_targets;
|
||||
}
|
||||
}
|
||||
// Publish All Target Count in Zones
|
||||
sensor::Sensor *sztc = this->zone_target_count_sensors_[index];
|
||||
if (sztc != nullptr) {
|
||||
sztc->publish_state(zone_all_targets);
|
||||
if (this->cached_zone_data_[index].total_count != zone_all_targets) {
|
||||
sztc->publish_state(zone_all_targets);
|
||||
this->cached_zone_data_[index].total_count = zone_all_targets;
|
||||
}
|
||||
}
|
||||
|
||||
} // End loop thru zones
|
||||
|
||||
// Target Count
|
||||
if (this->target_count_sensor_ != nullptr) {
|
||||
this->target_count_sensor_->publish_state(target_count);
|
||||
if (this->cached_global_data_.target_count != target_count) {
|
||||
this->target_count_sensor_->publish_state(target_count);
|
||||
this->cached_global_data_.target_count = target_count;
|
||||
}
|
||||
}
|
||||
// Still Target Count
|
||||
if (this->still_target_count_sensor_ != nullptr) {
|
||||
this->still_target_count_sensor_->publish_state(still_target_count);
|
||||
if (this->cached_global_data_.still_count != still_target_count) {
|
||||
this->still_target_count_sensor_->publish_state(still_target_count);
|
||||
this->cached_global_data_.still_count = still_target_count;
|
||||
}
|
||||
}
|
||||
// Moving Target Count
|
||||
if (this->moving_target_count_sensor_ != nullptr) {
|
||||
this->moving_target_count_sensor_->publish_state(moving_target_count);
|
||||
if (this->cached_global_data_.moving_count != moving_target_count) {
|
||||
this->moving_target_count_sensor_->publish_state(moving_target_count);
|
||||
this->cached_global_data_.moving_count = moving_target_count;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -5,6 +5,8 @@
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/preferences.h"
|
||||
#include <limits>
|
||||
#include <cmath>
|
||||
#ifdef USE_SENSOR
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#endif
|
||||
@ -100,7 +102,7 @@ class LD2450Component : public Component, public uart::UARTDevice {
|
||||
void dump_config() override;
|
||||
void loop() override;
|
||||
void set_presence_timeout();
|
||||
void set_throttle(uint16_t value) { this->throttle_ = value; };
|
||||
void set_throttle(uint16_t value) { this->throttle_ = value; }
|
||||
void read_all_info();
|
||||
void query_zone_info();
|
||||
void restart_and_read_all_info();
|
||||
@ -164,6 +166,32 @@ class LD2450Component : public Component, public uart::UARTDevice {
|
||||
Zone zone_config_[MAX_ZONES];
|
||||
std::string version_{};
|
||||
std::string mac_{};
|
||||
|
||||
// Change detection - cache previous values to avoid redundant publishes
|
||||
// All values are initialized to sentinel values that are outside the valid sensor ranges
|
||||
// to ensure the first real measurement is always published
|
||||
struct CachedTargetData {
|
||||
int16_t x = std::numeric_limits<int16_t>::min(); // -32768, outside range of -4860 to 4860
|
||||
int16_t y = std::numeric_limits<int16_t>::min(); // -32768, outside range of 0 to 7560
|
||||
int16_t speed = std::numeric_limits<int16_t>::min(); // -32768, outside practical sensor range
|
||||
uint16_t resolution = std::numeric_limits<uint16_t>::max(); // 65535, unlikely resolution value
|
||||
uint16_t distance = std::numeric_limits<uint16_t>::max(); // 65535, outside range of 0 to ~8990
|
||||
float angle = NAN; // NAN, safe sentinel for floats
|
||||
std::string direction = ""; // Empty string, will differ from any real direction
|
||||
} cached_target_data_[MAX_TARGETS];
|
||||
|
||||
struct CachedZoneData {
|
||||
uint8_t still_count = std::numeric_limits<uint8_t>::max(); // 255, unlikely zone count
|
||||
uint8_t moving_count = std::numeric_limits<uint8_t>::max(); // 255, unlikely zone count
|
||||
uint8_t total_count = std::numeric_limits<uint8_t>::max(); // 255, unlikely zone count
|
||||
} cached_zone_data_[MAX_ZONES];
|
||||
|
||||
struct CachedGlobalData {
|
||||
uint8_t target_count = std::numeric_limits<uint8_t>::max(); // 255, max 3 targets possible
|
||||
uint8_t still_count = std::numeric_limits<uint8_t>::max(); // 255, max 3 targets possible
|
||||
uint8_t moving_count = std::numeric_limits<uint8_t>::max(); // 255, max 3 targets possible
|
||||
} cached_global_data_;
|
||||
|
||||
#ifdef USE_NUMBER
|
||||
ESPPreferenceObject pref_; // only used when numbers are in use
|
||||
ZoneOfNumbers zone_numbers_[MAX_ZONES];
|
||||
|
@ -5,17 +5,15 @@ namespace microphone {
|
||||
|
||||
void Microphone::add_data_callback(std::function<void(const std::vector<uint8_t> &)> &&data_callback) {
|
||||
std::function<void(const std::vector<uint8_t> &)> mute_handled_callback =
|
||||
[this, data_callback](const std::vector<uint8_t> &data) { data_callback(this->silence_audio_(data)); };
|
||||
[this, data_callback](const std::vector<uint8_t> &data) {
|
||||
if (this->mute_state_) {
|
||||
data_callback(std::vector<uint8_t>(data.size(), 0));
|
||||
} else {
|
||||
data_callback(data);
|
||||
};
|
||||
};
|
||||
this->data_callbacks_.add(std::move(mute_handled_callback));
|
||||
}
|
||||
|
||||
std::vector<uint8_t> Microphone::silence_audio_(std::vector<uint8_t> data) {
|
||||
if (this->mute_state_) {
|
||||
std::memset((void *) data.data(), 0, data.size());
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
} // namespace microphone
|
||||
} // namespace esphome
|
||||
|
@ -33,8 +33,6 @@ class Microphone {
|
||||
audio::AudioStreamInfo get_audio_stream_info() { return this->audio_stream_info_; }
|
||||
|
||||
protected:
|
||||
std::vector<uint8_t> silence_audio_(std::vector<uint8_t> data);
|
||||
|
||||
State state_{STATE_STOPPED};
|
||||
bool mute_state_{false};
|
||||
|
||||
|
@ -252,7 +252,7 @@ class MQTTBackendESP32 final : public MQTTBackend {
|
||||
#if defined(USE_MQTT_IDF_ENQUEUE)
|
||||
static void esphome_mqtt_task(void *params);
|
||||
EventPool<struct QueueElement, MQTT_QUEUE_LENGTH> mqtt_event_pool_;
|
||||
LockFreeQueue<struct QueueElement, MQTT_QUEUE_LENGTH> mqtt_queue_;
|
||||
NotifyingLockFreeQueue<struct QueueElement, MQTT_QUEUE_LENGTH> mqtt_queue_;
|
||||
TaskHandle_t task_handle_{nullptr};
|
||||
bool enqueue_(MqttQueueTypeT type, const char *topic, int qos = 0, bool retain = false, const char *payload = NULL,
|
||||
size_t len = 0);
|
||||
|
@ -167,6 +167,7 @@ async def to_code(config):
|
||||
cg.add(var.set_wake_up_page(config[CONF_WAKE_UP_PAGE]))
|
||||
|
||||
if CONF_START_UP_PAGE in config:
|
||||
cg.add_define("USE_NEXTION_CONF_START_UP_PAGE")
|
||||
cg.add(var.set_start_up_page(config[CONF_START_UP_PAGE]))
|
||||
|
||||
cg.add(var.set_auto_wake_on_touch(config[CONF_AUTO_WAKE_ON_TOUCH]))
|
||||
|
@ -11,7 +11,7 @@ static const char *const TAG = "nextion";
|
||||
|
||||
void Nextion::setup() {
|
||||
this->is_setup_ = false;
|
||||
this->ignore_is_setup_ = true;
|
||||
this->connection_state_.ignore_is_setup_ = true;
|
||||
|
||||
// Wake up the nextion
|
||||
this->send_command_("bkcmd=0");
|
||||
@ -23,16 +23,16 @@ void Nextion::setup() {
|
||||
// Reboot it
|
||||
this->send_command_("rest");
|
||||
|
||||
this->ignore_is_setup_ = false;
|
||||
this->connection_state_.ignore_is_setup_ = false;
|
||||
}
|
||||
|
||||
bool Nextion::send_command_(const std::string &command) {
|
||||
if (!this->ignore_is_setup_ && !this->is_setup()) {
|
||||
if (!this->connection_state_.ignore_is_setup_ && !this->is_setup()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
#ifdef USE_NEXTION_COMMAND_SPACING
|
||||
if (!this->ignore_is_setup_ && !this->command_pacer_.can_send()) {
|
||||
if (!this->connection_state_.ignore_is_setup_ && !this->command_pacer_.can_send()) {
|
||||
ESP_LOGN(TAG, "Command spacing: delaying command '%s'", command.c_str());
|
||||
return false;
|
||||
}
|
||||
@ -48,7 +48,7 @@ bool Nextion::send_command_(const std::string &command) {
|
||||
}
|
||||
|
||||
bool Nextion::check_connect_() {
|
||||
if (this->is_connected_)
|
||||
if (this->connection_state_.is_connected_)
|
||||
return true;
|
||||
|
||||
// Check if the handshake should be skipped for the Nextion connection
|
||||
@ -56,7 +56,7 @@ bool Nextion::check_connect_() {
|
||||
// Log the connection status without handshake
|
||||
ESP_LOGW(TAG, "Connected (no handshake)");
|
||||
// Set the connection status to true
|
||||
this->is_connected_ = true;
|
||||
this->connection_state_.is_connected_ = true;
|
||||
// Return true indicating the connection is set
|
||||
return true;
|
||||
}
|
||||
@ -64,7 +64,7 @@ bool Nextion::check_connect_() {
|
||||
if (this->comok_sent_ == 0) {
|
||||
this->reset_(false);
|
||||
|
||||
this->ignore_is_setup_ = true;
|
||||
this->connection_state_.ignore_is_setup_ = true;
|
||||
this->send_command_("boguscommand=0"); // bogus command. needed sometimes after updating
|
||||
if (this->exit_reparse_on_start_) {
|
||||
this->send_command_("DRAKJHSUYDGBNCJHGJKSHBDN");
|
||||
@ -72,7 +72,7 @@ bool Nextion::check_connect_() {
|
||||
this->send_command_("connect");
|
||||
|
||||
this->comok_sent_ = App.get_loop_component_start_time();
|
||||
this->ignore_is_setup_ = false;
|
||||
this->connection_state_.ignore_is_setup_ = false;
|
||||
|
||||
return false;
|
||||
}
|
||||
@ -101,9 +101,9 @@ bool Nextion::check_connect_() {
|
||||
return false;
|
||||
}
|
||||
|
||||
this->ignore_is_setup_ = true;
|
||||
this->connection_state_.ignore_is_setup_ = true;
|
||||
ESP_LOGI(TAG, "Connected");
|
||||
this->is_connected_ = true;
|
||||
this->connection_state_.is_connected_ = true;
|
||||
|
||||
ESP_LOGN(TAG, "connect: %s", response.c_str());
|
||||
|
||||
@ -127,7 +127,7 @@ bool Nextion::check_connect_() {
|
||||
ESP_LOGE(TAG, "Bad connect value: '%s'", response.c_str());
|
||||
}
|
||||
|
||||
this->ignore_is_setup_ = false;
|
||||
this->connection_state_.ignore_is_setup_ = false;
|
||||
this->dump_config();
|
||||
return true;
|
||||
}
|
||||
@ -158,7 +158,7 @@ void Nextion::dump_config() {
|
||||
ESP_LOGCONFIG(TAG,
|
||||
" Wake On Touch: %s\n"
|
||||
" Exit reparse: %s",
|
||||
YESNO(this->auto_wake_on_touch_), YESNO(this->exit_reparse_on_start_));
|
||||
YESNO(this->connection_state_.auto_wake_on_touch_), YESNO(this->exit_reparse_on_start_));
|
||||
#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
|
||||
ESP_LOGCONFIG(TAG, " Max commands per loop: %u", this->max_commands_per_loop_);
|
||||
#endif // USE_NEXTION_MAX_COMMANDS_PER_LOOP
|
||||
@ -167,13 +167,15 @@ void Nextion::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, " Touch Timeout: %" PRIu16, this->touch_sleep_timeout_);
|
||||
}
|
||||
|
||||
if (this->wake_up_page_ != -1) {
|
||||
ESP_LOGCONFIG(TAG, " Wake Up Page: %d", this->wake_up_page_);
|
||||
if (this->wake_up_page_ != 255) {
|
||||
ESP_LOGCONFIG(TAG, " Wake Up Page: %u", this->wake_up_page_);
|
||||
}
|
||||
|
||||
if (this->start_up_page_ != -1) {
|
||||
ESP_LOGCONFIG(TAG, " Start Up Page: %d", this->start_up_page_);
|
||||
#ifdef USE_NEXTION_CONF_START_UP_PAGE
|
||||
if (this->start_up_page_ != 255) {
|
||||
ESP_LOGCONFIG(TAG, " Start Up Page: %u", this->start_up_page_);
|
||||
}
|
||||
#endif // USE_NEXTION_CONF_START_UP_PAGE
|
||||
|
||||
#ifdef USE_NEXTION_COMMAND_SPACING
|
||||
ESP_LOGCONFIG(TAG, " Cmd spacing: %u ms", this->command_pacer_.get_spacing());
|
||||
@ -219,7 +221,7 @@ void Nextion::add_buffer_overflow_event_callback(std::function<void()> &&callbac
|
||||
}
|
||||
|
||||
void Nextion::update_all_components() {
|
||||
if ((!this->is_setup() && !this->ignore_is_setup_) || this->is_sleeping())
|
||||
if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || this->is_sleeping())
|
||||
return;
|
||||
|
||||
for (auto *binarysensortype : this->binarysensortype_) {
|
||||
@ -237,7 +239,7 @@ void Nextion::update_all_components() {
|
||||
}
|
||||
|
||||
bool Nextion::send_command(const char *command) {
|
||||
if ((!this->is_setup() && !this->ignore_is_setup_) || this->is_sleeping())
|
||||
if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || this->is_sleeping())
|
||||
return false;
|
||||
|
||||
if (this->send_command_(command)) {
|
||||
@ -248,7 +250,7 @@ bool Nextion::send_command(const char *command) {
|
||||
}
|
||||
|
||||
bool Nextion::send_command_printf(const char *format, ...) {
|
||||
if ((!this->is_setup() && !this->ignore_is_setup_) || this->is_sleeping())
|
||||
if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || this->is_sleeping())
|
||||
return false;
|
||||
|
||||
char buffer[256];
|
||||
@ -289,40 +291,42 @@ void Nextion::print_queue_members_() {
|
||||
#endif
|
||||
|
||||
void Nextion::loop() {
|
||||
if (!this->check_connect_() || this->is_updating_)
|
||||
if (!this->check_connect_() || this->connection_state_.is_updating_)
|
||||
return;
|
||||
|
||||
if (this->nextion_reports_is_setup_ && !this->sent_setup_commands_) {
|
||||
this->ignore_is_setup_ = true;
|
||||
this->sent_setup_commands_ = true;
|
||||
if (this->connection_state_.nextion_reports_is_setup_ && !this->connection_state_.sent_setup_commands_) {
|
||||
this->connection_state_.ignore_is_setup_ = true;
|
||||
this->connection_state_.sent_setup_commands_ = true;
|
||||
this->send_command_("bkcmd=3"); // Always, returns 0x00 to 0x23 result of serial command.
|
||||
|
||||
if (this->brightness_.has_value()) {
|
||||
this->set_backlight_brightness(this->brightness_.value());
|
||||
}
|
||||
|
||||
#ifdef USE_NEXTION_CONF_START_UP_PAGE
|
||||
// Check if a startup page has been set and send the command
|
||||
if (this->start_up_page_ >= 0) {
|
||||
if (this->start_up_page_ != 255) {
|
||||
this->goto_page(this->start_up_page_);
|
||||
}
|
||||
#endif // USE_NEXTION_CONF_START_UP_PAGE
|
||||
|
||||
if (this->wake_up_page_ >= 0) {
|
||||
if (this->wake_up_page_ != 255) {
|
||||
this->set_wake_up_page(this->wake_up_page_);
|
||||
}
|
||||
|
||||
this->ignore_is_setup_ = false;
|
||||
this->connection_state_.ignore_is_setup_ = false;
|
||||
}
|
||||
|
||||
this->process_serial_(); // Receive serial data
|
||||
this->process_nextion_commands_(); // Process nextion return commands
|
||||
|
||||
if (!this->nextion_reports_is_setup_) {
|
||||
if (!this->connection_state_.nextion_reports_is_setup_) {
|
||||
if (this->started_ms_ == 0)
|
||||
this->started_ms_ = App.get_loop_component_start_time();
|
||||
|
||||
if (this->started_ms_ + this->startup_override_ms_ < App.get_loop_component_start_time()) {
|
||||
ESP_LOGD(TAG, "Manual ready set");
|
||||
this->nextion_reports_is_setup_ = true;
|
||||
this->connection_state_.nextion_reports_is_setup_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
@ -665,7 +669,7 @@ void Nextion::process_nextion_commands_() {
|
||||
case 0x88: // system successful start up
|
||||
{
|
||||
ESP_LOGD(TAG, "System start: %zu", to_process_length);
|
||||
this->nextion_reports_is_setup_ = true;
|
||||
this->connection_state_.nextion_reports_is_setup_ = true;
|
||||
break;
|
||||
}
|
||||
case 0x89: { // start SD card upgrade
|
||||
@ -1048,7 +1052,7 @@ void Nextion::add_no_result_to_queue_(const std::string &variable_name) {
|
||||
* @param command
|
||||
*/
|
||||
void Nextion::add_no_result_to_queue_with_command_(const std::string &variable_name, const std::string &command) {
|
||||
if ((!this->is_setup() && !this->ignore_is_setup_) || command.empty())
|
||||
if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || command.empty())
|
||||
return;
|
||||
|
||||
if (this->send_command_(command)) {
|
||||
@ -1091,7 +1095,7 @@ void Nextion::add_no_result_to_queue_with_pending_command_(const std::string &va
|
||||
|
||||
bool Nextion::add_no_result_to_queue_with_ignore_sleep_printf_(const std::string &variable_name, const char *format,
|
||||
...) {
|
||||
if ((!this->is_setup() && !this->ignore_is_setup_))
|
||||
if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_))
|
||||
return false;
|
||||
|
||||
char buffer[256];
|
||||
@ -1116,7 +1120,7 @@ bool Nextion::add_no_result_to_queue_with_ignore_sleep_printf_(const std::string
|
||||
* @param ... The format arguments
|
||||
*/
|
||||
bool Nextion::add_no_result_to_queue_with_printf_(const std::string &variable_name, const char *format, ...) {
|
||||
if ((!this->is_setup() && !this->ignore_is_setup_) || this->is_sleeping())
|
||||
if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || this->is_sleeping())
|
||||
return false;
|
||||
|
||||
char buffer[256];
|
||||
@ -1155,7 +1159,7 @@ void Nextion::add_no_result_to_queue_with_set(const std::string &variable_name,
|
||||
void Nextion::add_no_result_to_queue_with_set_internal_(const std::string &variable_name,
|
||||
const std::string &variable_name_to_send, int32_t state_value,
|
||||
bool is_sleep_safe) {
|
||||
if ((!this->is_setup() && !this->ignore_is_setup_) || (!is_sleep_safe && this->is_sleeping()))
|
||||
if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || (!is_sleep_safe && this->is_sleeping()))
|
||||
return;
|
||||
|
||||
this->add_no_result_to_queue_with_ignore_sleep_printf_(variable_name, "%s=%" PRId32, variable_name_to_send.c_str(),
|
||||
@ -1183,7 +1187,7 @@ void Nextion::add_no_result_to_queue_with_set(const std::string &variable_name,
|
||||
void Nextion::add_no_result_to_queue_with_set_internal_(const std::string &variable_name,
|
||||
const std::string &variable_name_to_send,
|
||||
const std::string &state_value, bool is_sleep_safe) {
|
||||
if ((!this->is_setup() && !this->ignore_is_setup_) || (!is_sleep_safe && this->is_sleeping()))
|
||||
if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || (!is_sleep_safe && this->is_sleeping()))
|
||||
return;
|
||||
|
||||
this->add_no_result_to_queue_with_printf_(variable_name, "%s=\"%s\"", variable_name_to_send.c_str(),
|
||||
@ -1200,7 +1204,7 @@ void Nextion::add_no_result_to_queue_with_set_internal_(const std::string &varia
|
||||
* @param component Pointer to the Nextion component that will handle the response.
|
||||
*/
|
||||
void Nextion::add_to_get_queue(NextionComponentBase *component) {
|
||||
if ((!this->is_setup() && !this->ignore_is_setup_))
|
||||
if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_))
|
||||
return;
|
||||
|
||||
#ifdef USE_NEXTION_MAX_QUEUE_SIZE
|
||||
@ -1240,7 +1244,7 @@ void Nextion::add_to_get_queue(NextionComponentBase *component) {
|
||||
* @param buffer_size The buffer data
|
||||
*/
|
||||
void Nextion::add_addt_command_to_queue(NextionComponentBase *component) {
|
||||
if ((!this->is_setup() && !this->ignore_is_setup_) || this->is_sleeping())
|
||||
if ((!this->is_setup() && !this->connection_state_.ignore_is_setup_) || this->is_sleeping())
|
||||
return;
|
||||
|
||||
RAMAllocator<nextion::NextionQueue> allocator;
|
||||
@ -1281,7 +1285,7 @@ void Nextion::set_writer(const nextion_writer_t &writer) { this->writer_ = write
|
||||
ESPDEPRECATED("set_wait_for_ack(bool) deprecated, no effect", "v1.20")
|
||||
void Nextion::set_wait_for_ack(bool wait_for_ack) { ESP_LOGE(TAG, "Deprecated"); }
|
||||
|
||||
bool Nextion::is_updating() { return this->is_updating_; }
|
||||
bool Nextion::is_updating() { return this->connection_state_.is_updating_; }
|
||||
|
||||
} // namespace nextion
|
||||
} // namespace esphome
|
||||
|
@ -1194,7 +1194,7 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
|
||||
|
||||
/**
|
||||
* Sets which page Nextion loads when exiting sleep mode. Note this can be set even when Nextion is in sleep mode.
|
||||
* @param wake_up_page The page id, from 0 to the last page in Nextion. Set -1 (not set to any existing page) to
|
||||
* @param wake_up_page The page id, from 0 to the last page in Nextion. Set 255 (not set to any existing page) to
|
||||
* wakes up to current page.
|
||||
*
|
||||
* Example:
|
||||
@ -1204,11 +1204,12 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
|
||||
*
|
||||
* The display will wake up to page 2.
|
||||
*/
|
||||
void set_wake_up_page(int16_t wake_up_page = -1);
|
||||
void set_wake_up_page(uint8_t wake_up_page = 255);
|
||||
|
||||
#ifdef USE_NEXTION_CONF_START_UP_PAGE
|
||||
/**
|
||||
* Sets which page Nextion loads when connecting to ESPHome.
|
||||
* @param start_up_page The page id, from 0 to the last page in Nextion. Set -1 (not set to any existing page) to
|
||||
* @param start_up_page The page id, from 0 to the last page in Nextion. Set 255 (not set to any existing page) to
|
||||
* wakes up to current page.
|
||||
*
|
||||
* Example:
|
||||
@ -1218,7 +1219,8 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
|
||||
*
|
||||
* The display will go to page 2 when it establishes a connection to ESPHome.
|
||||
*/
|
||||
void set_start_up_page(int16_t start_up_page = -1) { this->start_up_page_ = start_up_page; }
|
||||
void set_start_up_page(uint8_t start_up_page = 255) { this->start_up_page_ = start_up_page; }
|
||||
#endif // USE_NEXTION_CONF_START_UP_PAGE
|
||||
|
||||
/**
|
||||
* Sets if Nextion should auto-wake from sleep when touch press occurs.
|
||||
@ -1300,7 +1302,7 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
|
||||
* @return true if the Nextion display is connected and ready to receive commands
|
||||
* @return false if the display is not yet connected or connection was lost
|
||||
*/
|
||||
bool is_connected() { return this->is_connected_; }
|
||||
bool is_connected() { return this->connection_state_.is_connected_; }
|
||||
|
||||
protected:
|
||||
#ifdef USE_NEXTION_MAX_COMMANDS_PER_LOOP
|
||||
@ -1334,19 +1336,28 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
|
||||
bool remove_from_q_(bool report_empty = true);
|
||||
|
||||
/**
|
||||
* @brief
|
||||
* Sends commands ignoring of the Nextion has been setup.
|
||||
* @brief Status flags for Nextion display state management
|
||||
*
|
||||
* Uses bitfields to pack multiple boolean states into a single byte,
|
||||
* saving 5 bytes of RAM compared to individual bool variables.
|
||||
*/
|
||||
bool ignore_is_setup_ = false;
|
||||
struct {
|
||||
uint8_t is_connected_ : 1; ///< Connection established with Nextion display
|
||||
uint8_t sent_setup_commands_ : 1; ///< Initial setup commands have been sent
|
||||
uint8_t ignore_is_setup_ : 1; ///< Temporarily ignore setup state for special operations
|
||||
uint8_t nextion_reports_is_setup_ : 1; ///< Nextion has reported successful initialization
|
||||
uint8_t is_updating_ : 1; ///< TFT firmware update is currently in progress
|
||||
uint8_t auto_wake_on_touch_ : 1; ///< Display should wake automatically on touch (default: true)
|
||||
uint8_t reserved_ : 2; ///< Reserved bits for future flag additions
|
||||
} connection_state_{}; ///< Zero-initialized status flags (all start as false)
|
||||
|
||||
bool nextion_reports_is_setup_ = false;
|
||||
void process_nextion_commands_();
|
||||
void process_serial_();
|
||||
bool is_updating_ = false;
|
||||
uint16_t touch_sleep_timeout_ = 0;
|
||||
int16_t wake_up_page_ = -1;
|
||||
int16_t start_up_page_ = -1;
|
||||
bool auto_wake_on_touch_ = true;
|
||||
uint8_t wake_up_page_ = 255;
|
||||
#ifdef USE_NEXTION_CONF_START_UP_PAGE
|
||||
uint8_t start_up_page_ = 255;
|
||||
#endif // USE_NEXTION_CONF_START_UP_PAGE
|
||||
bool exit_reparse_on_start_ = false;
|
||||
bool skip_connection_handshake_ = false;
|
||||
|
||||
@ -1468,11 +1479,9 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
|
||||
void reset_(bool reset_nextion = true);
|
||||
|
||||
std::string command_data_;
|
||||
bool is_connected_ = false;
|
||||
const uint16_t startup_override_ms_ = 8000;
|
||||
const uint16_t max_q_age_ms_ = 8000;
|
||||
uint32_t started_ms_ = 0;
|
||||
bool sent_setup_commands_ = false;
|
||||
};
|
||||
|
||||
} // namespace nextion
|
||||
|
@ -10,7 +10,7 @@ static const char *const TAG = "nextion";
|
||||
// Sleep safe commands
|
||||
void Nextion::soft_reset() { this->send_command_("rest"); }
|
||||
|
||||
void Nextion::set_wake_up_page(int16_t wake_up_page) {
|
||||
void Nextion::set_wake_up_page(uint8_t wake_up_page) {
|
||||
this->wake_up_page_ = wake_up_page;
|
||||
this->add_no_result_to_queue_with_set_internal_("wake_up_page", "wup", wake_up_page, true);
|
||||
}
|
||||
@ -38,7 +38,7 @@ void Nextion::sleep(bool sleep) {
|
||||
// Protocol reparse mode
|
||||
bool Nextion::set_protocol_reparse_mode(bool active_mode) {
|
||||
ESP_LOGV(TAG, "Reparse mode: %s", YESNO(active_mode));
|
||||
this->ignore_is_setup_ = true; // if not in reparse mode setup will fail, so it should be ignored
|
||||
this->connection_state_.ignore_is_setup_ = true; // if not in reparse mode setup will fail, so it should be ignored
|
||||
bool all_commands_sent = true;
|
||||
if (active_mode) { // Sets active protocol reparse mode
|
||||
all_commands_sent &= this->send_command_("recmod=1");
|
||||
@ -48,10 +48,10 @@ bool Nextion::set_protocol_reparse_mode(bool active_mode) {
|
||||
all_commands_sent &= this->send_command_("recmod=0"); // Sending recmode=0 twice is recommended
|
||||
all_commands_sent &= this->send_command_("recmod=0");
|
||||
}
|
||||
if (!this->nextion_reports_is_setup_) { // No need to connect if is already setup
|
||||
if (!this->connection_state_.nextion_reports_is_setup_) { // No need to connect if is already setup
|
||||
all_commands_sent &= this->send_command_("connect");
|
||||
}
|
||||
this->ignore_is_setup_ = false;
|
||||
this->connection_state_.ignore_is_setup_ = false;
|
||||
return all_commands_sent;
|
||||
}
|
||||
|
||||
@ -191,7 +191,7 @@ void Nextion::set_backlight_brightness(float brightness) {
|
||||
}
|
||||
|
||||
void Nextion::set_auto_wake_on_touch(bool auto_wake_on_touch) {
|
||||
this->auto_wake_on_touch_ = auto_wake_on_touch;
|
||||
this->connection_state_.auto_wake_on_touch_ = auto_wake_on_touch;
|
||||
this->add_no_result_to_queue_with_set("auto_wake_on_touch", "thup", auto_wake_on_touch ? 1 : 0);
|
||||
}
|
||||
|
||||
|
@ -16,8 +16,8 @@ bool Nextion::upload_end_(bool successful) {
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Upload failed");
|
||||
|
||||
this->is_updating_ = false;
|
||||
this->ignore_is_setup_ = false;
|
||||
this->connection_state_.is_updating_ = false;
|
||||
this->connection_state_.ignore_is_setup_ = false;
|
||||
|
||||
uint32_t baud_rate = this->parent_->get_baud_rate();
|
||||
if (baud_rate != this->original_baud_rate_) {
|
||||
|
@ -152,7 +152,7 @@ bool Nextion::upload_tft(uint32_t baud_rate, bool exit_reparse) {
|
||||
ESP_LOGD(TAG, "Exit reparse: %s", YESNO(exit_reparse));
|
||||
ESP_LOGD(TAG, "URL: %s", this->tft_url_.c_str());
|
||||
|
||||
if (this->is_updating_) {
|
||||
if (this->connection_state_.is_updating_) {
|
||||
ESP_LOGW(TAG, "Upload in progress");
|
||||
return false;
|
||||
}
|
||||
@ -162,7 +162,7 @@ bool Nextion::upload_tft(uint32_t baud_rate, bool exit_reparse) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this->is_updating_ = true;
|
||||
this->connection_state_.is_updating_ = true;
|
||||
|
||||
if (exit_reparse) {
|
||||
ESP_LOGD(TAG, "Exit reparse mode");
|
||||
@ -203,7 +203,7 @@ bool Nextion::upload_tft(uint32_t baud_rate, bool exit_reparse) {
|
||||
begin_status = http_client.begin(*this->get_wifi_client_(), this->tft_url_.c_str());
|
||||
#endif // USE_ESP8266
|
||||
if (!begin_status) {
|
||||
this->is_updating_ = false;
|
||||
this->connection_state_.is_updating_ = false;
|
||||
ESP_LOGD(TAG, "Connection failed");
|
||||
return false;
|
||||
} else {
|
||||
@ -254,7 +254,7 @@ bool Nextion::upload_tft(uint32_t baud_rate, bool exit_reparse) {
|
||||
|
||||
// The Nextion will ignore the upload command if it is sleeping
|
||||
ESP_LOGV(TAG, "Wake-up");
|
||||
this->ignore_is_setup_ = true;
|
||||
this->connection_state_.ignore_is_setup_ = true;
|
||||
this->send_command_("sleep=0");
|
||||
this->send_command_("dim=100");
|
||||
delay(250); // NOLINT
|
||||
|
@ -155,7 +155,7 @@ bool Nextion::upload_tft(uint32_t baud_rate, bool exit_reparse) {
|
||||
ESP_LOGD(TAG, "Exit reparse: %s", YESNO(exit_reparse));
|
||||
ESP_LOGD(TAG, "URL: %s", this->tft_url_.c_str());
|
||||
|
||||
if (this->is_updating_) {
|
||||
if (this->connection_state_.is_updating_) {
|
||||
ESP_LOGW(TAG, "Upload in progress");
|
||||
return false;
|
||||
}
|
||||
@ -165,7 +165,7 @@ bool Nextion::upload_tft(uint32_t baud_rate, bool exit_reparse) {
|
||||
return false;
|
||||
}
|
||||
|
||||
this->is_updating_ = true;
|
||||
this->connection_state_.is_updating_ = true;
|
||||
|
||||
if (exit_reparse) {
|
||||
ESP_LOGD(TAG, "Exit reparse mode");
|
||||
@ -246,7 +246,7 @@ bool Nextion::upload_tft(uint32_t baud_rate, bool exit_reparse) {
|
||||
|
||||
// The Nextion will ignore the upload command if it is sleeping
|
||||
ESP_LOGV(TAG, "Wake-up");
|
||||
this->ignore_is_setup_ = true;
|
||||
this->connection_state_.ignore_is_setup_ = true;
|
||||
this->send_command_("sleep=0");
|
||||
this->send_command_("dim=100");
|
||||
vTaskDelay(pdMS_TO_TICKS(250)); // NOLINT
|
||||
|
@ -11,6 +11,7 @@ from esphome.const import CONF_CHANNEL, CONF_ENABLE_IPV6, CONF_ID
|
||||
import esphome.final_validate as fv
|
||||
|
||||
from .const import (
|
||||
CONF_DEVICE_TYPE,
|
||||
CONF_EXT_PAN_ID,
|
||||
CONF_FORCE_DATASET,
|
||||
CONF_MDNS_ID,
|
||||
@ -32,6 +33,11 @@ AUTO_LOAD = ["network"]
|
||||
CONFLICTS_WITH = ["wifi"]
|
||||
DEPENDENCIES = ["esp32"]
|
||||
|
||||
CONF_DEVICE_TYPES = [
|
||||
"FTD",
|
||||
"MTD",
|
||||
]
|
||||
|
||||
|
||||
def set_sdkconfig_options(config):
|
||||
# and expose options for using SPI/UART RCPs
|
||||
@ -82,7 +88,7 @@ def set_sdkconfig_options(config):
|
||||
add_idf_sdkconfig_option("CONFIG_OPENTHREAD_SRP_CLIENT_MAX_SERVICES", 5)
|
||||
|
||||
# TODO: Add suport for sleepy end devices
|
||||
add_idf_sdkconfig_option("CONFIG_OPENTHREAD_FTD", True) # Full Thread Device
|
||||
add_idf_sdkconfig_option(f"CONFIG_OPENTHREAD_{config.get(CONF_DEVICE_TYPE)}", True)
|
||||
|
||||
|
||||
openthread_ns = cg.esphome_ns.namespace("openthread")
|
||||
@ -107,6 +113,9 @@ CONFIG_SCHEMA = cv.All(
|
||||
cv.GenerateID(): cv.declare_id(OpenThreadComponent),
|
||||
cv.GenerateID(CONF_SRP_ID): cv.declare_id(OpenThreadSrpComponent),
|
||||
cv.GenerateID(CONF_MDNS_ID): cv.use_id(MDNSComponent),
|
||||
cv.Optional(CONF_DEVICE_TYPE, default="FTD"): cv.one_of(
|
||||
*CONF_DEVICE_TYPES, upper=True
|
||||
),
|
||||
cv.Optional(CONF_FORCE_DATASET): cv.boolean,
|
||||
cv.Optional(CONF_TLV): cv.string_strict,
|
||||
}
|
||||
|
@ -1,3 +1,4 @@
|
||||
CONF_DEVICE_TYPE = "device_type"
|
||||
CONF_EXT_PAN_ID = "ext_pan_id"
|
||||
CONF_FORCE_DATASET = "force_dataset"
|
||||
CONF_MDNS_ID = "mdns_id"
|
||||
|
@ -63,6 +63,7 @@ BASE_SCHEMA = cv.All(
|
||||
cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_URL): cv.url,
|
||||
cv.Optional(CONF_PATH): cv.string,
|
||||
cv.Optional(CONF_USERNAME): cv.string,
|
||||
cv.Optional(CONF_PASSWORD): cv.string,
|
||||
cv.Exclusive(CONF_FILE, CONF_FILES): validate_yaml_filename,
|
||||
@ -116,6 +117,9 @@ def _process_base_package(config: dict) -> dict:
|
||||
)
|
||||
files = []
|
||||
|
||||
if base_path := config.get(CONF_PATH):
|
||||
repo_dir = repo_dir / base_path
|
||||
|
||||
for file in config[CONF_FILES]:
|
||||
if isinstance(file, str):
|
||||
files.append({CONF_PATH: file, CONF_VARS: {}})
|
||||
|
@ -1,19 +1,76 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import binary_sensor
|
||||
from esphome.const import CONF_ID
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_DATA,
|
||||
CONF_ID,
|
||||
CONF_NAME,
|
||||
CONF_STATUS,
|
||||
CONF_TYPE,
|
||||
DEVICE_CLASS_CONNECTIVITY,
|
||||
ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
)
|
||||
import esphome.final_validate as fv
|
||||
|
||||
from . import (
|
||||
CONF_ENCRYPTION,
|
||||
CONF_PING_PONG_ENABLE,
|
||||
CONF_PROVIDER,
|
||||
CONF_PROVIDERS,
|
||||
CONF_REMOTE_ID,
|
||||
CONF_TRANSPORT_ID,
|
||||
PacketTransport,
|
||||
packet_transport_sensor_schema,
|
||||
provider_name_validate,
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = packet_transport_sensor_schema(binary_sensor.binary_sensor_schema())
|
||||
STATUS_SENSOR_SCHEMA = binary_sensor.binary_sensor_schema(
|
||||
device_class=DEVICE_CLASS_CONNECTIVITY,
|
||||
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
|
||||
).extend(
|
||||
{
|
||||
cv.GenerateID(CONF_TRANSPORT_ID): cv.use_id(PacketTransport),
|
||||
cv.Required(CONF_PROVIDER): provider_name_validate,
|
||||
}
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = cv.typed_schema(
|
||||
{
|
||||
CONF_DATA: packet_transport_sensor_schema(binary_sensor.binary_sensor_schema()),
|
||||
CONF_STATUS: STATUS_SENSOR_SCHEMA,
|
||||
},
|
||||
key=CONF_TYPE,
|
||||
default_type=CONF_DATA,
|
||||
)
|
||||
|
||||
|
||||
def _final_validate(config):
|
||||
if config[CONF_TYPE] != CONF_STATUS:
|
||||
# Only run this validation if a status sensor is being configured
|
||||
return config
|
||||
full_config = fv.full_config.get()
|
||||
transport_path = full_config.get_path_for_id(config[CONF_TRANSPORT_ID])[:-1]
|
||||
transport_config = full_config.get_config_for_path(transport_path)
|
||||
if transport_config[CONF_PING_PONG_ENABLE] and any(
|
||||
CONF_ENCRYPTION in p
|
||||
for p in transport_config[CONF_PROVIDERS]
|
||||
if p[CONF_NAME] == config[CONF_PROVIDER]
|
||||
):
|
||||
return config
|
||||
raise cv.Invalid(
|
||||
"Status sensor requires ping-pong to be enabled and the nominated provider to use encryption."
|
||||
)
|
||||
|
||||
|
||||
FINAL_VALIDATE_SCHEMA = _final_validate
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = await binary_sensor.new_binary_sensor(config)
|
||||
comp = await cg.get_variable(config[CONF_TRANSPORT_ID])
|
||||
remote_id = str(config.get(CONF_REMOTE_ID) or config.get(CONF_ID))
|
||||
cg.add(comp.add_remote_binary_sensor(config[CONF_PROVIDER], remote_id, var))
|
||||
if config[CONF_TYPE] == CONF_STATUS:
|
||||
cg.add(comp.set_provider_status_sensor(config[CONF_PROVIDER], var))
|
||||
cg.add_define("USE_STATUS_SENSOR")
|
||||
else: # CONF_DATA is default
|
||||
remote_id = str(config.get(CONF_REMOTE_ID) or config.get(CONF_ID))
|
||||
cg.add(comp.add_remote_binary_sensor(config[CONF_PROVIDER], remote_id, var))
|
||||
|
@ -317,8 +317,37 @@ void PacketTransport::update() {
|
||||
auto now = millis() / 1000;
|
||||
if (this->last_key_time_ + this->ping_pong_recyle_time_ < now) {
|
||||
this->resend_ping_key_ = this->ping_pong_enable_;
|
||||
ESP_LOGV(TAG, "Ping request, age %u", now - this->last_key_time_);
|
||||
this->last_key_time_ = now;
|
||||
}
|
||||
for (const auto &provider : this->providers_) {
|
||||
uint32_t key_response_age = now - provider.second.last_key_response_time;
|
||||
if (key_response_age > (this->ping_pong_recyle_time_ * 2u)) {
|
||||
#ifdef USE_STATUS_SENSOR
|
||||
if (provider.second.status_sensor != nullptr && provider.second.status_sensor->state) {
|
||||
ESP_LOGI(TAG, "Ping status for %s timeout at %u with age %u", provider.first.c_str(), now, key_response_age);
|
||||
provider.second.status_sensor->publish_state(false);
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_SENSOR
|
||||
for (auto &sensor : this->remote_sensors_[provider.first]) {
|
||||
sensor.second->publish_state(NAN);
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
for (auto &sensor : this->remote_binary_sensors_[provider.first]) {
|
||||
sensor.second->invalidate_state();
|
||||
}
|
||||
#endif
|
||||
} else {
|
||||
#ifdef USE_STATUS_SENSOR
|
||||
if (provider.second.status_sensor != nullptr && !provider.second.status_sensor->state) {
|
||||
ESP_LOGI(TAG, "Ping status for %s restored at %u with age %u", provider.first.c_str(), now, key_response_age);
|
||||
provider.second.status_sensor->publish_state(true);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PacketTransport::add_key_(const char *name, uint32_t key) {
|
||||
@ -437,7 +466,8 @@ void PacketTransport::process_(const std::vector<uint8_t> &data) {
|
||||
if (decoder.decode(PING_KEY, key) == DECODE_OK) {
|
||||
if (key == this->ping_key_) {
|
||||
ping_key_seen = true;
|
||||
ESP_LOGV(TAG, "Found good ping key %X", (unsigned) key);
|
||||
provider.last_key_response_time = millis() / 1000;
|
||||
ESP_LOGV(TAG, "Found good ping key %X at timestamp %" PRIu32, (unsigned) key, provider.last_key_response_time);
|
||||
} else {
|
||||
ESP_LOGV(TAG, "Unknown ping key %X", (unsigned) key);
|
||||
}
|
||||
|
@ -8,7 +8,7 @@
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
#include "esphome/components/binary_sensor/binary_sensor.h"
|
||||
#endif
|
||||
#
|
||||
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
@ -27,6 +27,10 @@ struct Provider {
|
||||
std::vector<uint8_t> encryption_key;
|
||||
const char *name;
|
||||
uint32_t last_code[2];
|
||||
uint32_t last_key_response_time;
|
||||
#ifdef USE_STATUS_SENSOR
|
||||
binary_sensor::BinarySensor *status_sensor{nullptr};
|
||||
#endif
|
||||
};
|
||||
|
||||
#ifdef USE_SENSOR
|
||||
@ -75,10 +79,7 @@ class PacketTransport : public PollingComponent {
|
||||
|
||||
void add_provider(const char *hostname) {
|
||||
if (this->providers_.count(hostname) == 0) {
|
||||
Provider provider;
|
||||
provider.encryption_key = std::vector<uint8_t>{};
|
||||
provider.last_code[0] = 0;
|
||||
provider.last_code[1] = 0;
|
||||
Provider provider{};
|
||||
provider.name = hostname;
|
||||
this->providers_[hostname] = provider;
|
||||
#ifdef USE_SENSOR
|
||||
@ -97,6 +98,11 @@ class PacketTransport : public PollingComponent {
|
||||
void set_provider_encryption(const char *name, std::vector<uint8_t> key) {
|
||||
this->providers_[name].encryption_key = std::move(key);
|
||||
}
|
||||
#ifdef USE_STATUS_SENSOR
|
||||
void set_provider_status_sensor(const char *name, binary_sensor::BinarySensor *sensor) {
|
||||
this->providers_[name].status_sensor = sensor;
|
||||
}
|
||||
#endif
|
||||
void set_platform_name(const char *name) { this->platform_name_ = name; }
|
||||
|
||||
protected:
|
||||
|
@ -57,14 +57,14 @@ def validate_parent_output_config(value):
|
||||
platform = value.get(CONF_PLATFORM)
|
||||
PWM_GOOD = ["esp8266_pwm", "ledc"]
|
||||
PWM_BAD = [
|
||||
"ac_dimmer ",
|
||||
"ac_dimmer",
|
||||
"esp32_dac",
|
||||
"slow_pwm",
|
||||
"mcp4725",
|
||||
"pca9685",
|
||||
"tlc59208f",
|
||||
"my9231",
|
||||
"pca9685",
|
||||
"slow_pwm",
|
||||
"sm16716",
|
||||
"tlc59208f",
|
||||
]
|
||||
|
||||
if platform in PWM_BAD:
|
||||
|
@ -371,6 +371,7 @@ void Rtttl::finish_() {
|
||||
ESP_LOGD(TAG, "Playback finished");
|
||||
}
|
||||
|
||||
#if ESPHOME_LOG_LEVEL >= ESPHOME_LOG_LEVEL_DEBUG
|
||||
static const LogString *state_to_string(State state) {
|
||||
switch (state) {
|
||||
case STATE_STOPPED:
|
||||
@ -387,6 +388,7 @@ static const LogString *state_to_string(State state) {
|
||||
return LOG_STR("UNKNOWN");
|
||||
}
|
||||
};
|
||||
#endif
|
||||
|
||||
void Rtttl::set_state_(State state) {
|
||||
State old_state = this->state_;
|
||||
|
@ -7,6 +7,8 @@ namespace scd4x {
|
||||
|
||||
static const char *const TAG = "scd4x";
|
||||
|
||||
static const uint16_t SCD41_ID = 0x1408;
|
||||
static const uint16_t SCD40_ID = 0x440;
|
||||
static const uint16_t SCD4X_CMD_GET_SERIAL_NUMBER = 0x3682;
|
||||
static const uint16_t SCD4X_CMD_TEMPERATURE_OFFSET = 0x241d;
|
||||
static const uint16_t SCD4X_CMD_ALTITUDE_COMPENSATION = 0x2427;
|
||||
@ -23,8 +25,6 @@ static const uint16_t SCD4X_CMD_STOP_MEASUREMENTS = 0x3f86;
|
||||
static const uint16_t SCD4X_CMD_FACTORY_RESET = 0x3632;
|
||||
static const uint16_t SCD4X_CMD_GET_FEATURESET = 0x202f;
|
||||
static const float SCD4X_TEMPERATURE_OFFSET_MULTIPLIER = (1 << 16) / 175.0f;
|
||||
static const uint16_t SCD41_ID = 0x1408;
|
||||
static const uint16_t SCD40_ID = 0x440;
|
||||
|
||||
void SCD4XComponent::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Running setup");
|
||||
@ -51,47 +51,66 @@ void SCD4XComponent::setup() {
|
||||
|
||||
if (!this->write_command(SCD4X_CMD_TEMPERATURE_OFFSET,
|
||||
(uint16_t) (temperature_offset_ * SCD4X_TEMPERATURE_OFFSET_MULTIPLIER))) {
|
||||
ESP_LOGE(TAG, "Error setting temperature offset.");
|
||||
ESP_LOGE(TAG, "Error setting temperature offset");
|
||||
this->error_code_ = MEASUREMENT_INIT_FAILED;
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
// If pressure compensation available use it
|
||||
// else use altitude
|
||||
if (ambient_pressure_compensation_) {
|
||||
if (!this->update_ambient_pressure_compensation_(ambient_pressure_)) {
|
||||
ESP_LOGE(TAG, "Error setting ambient pressure compensation.");
|
||||
// If pressure compensation available use it, else use altitude
|
||||
if (this->ambient_pressure_) {
|
||||
if (!this->update_ambient_pressure_compensation_(this->ambient_pressure_)) {
|
||||
ESP_LOGE(TAG, "Error setting ambient pressure compensation");
|
||||
this->error_code_ = MEASUREMENT_INIT_FAILED;
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
} else {
|
||||
if (!this->write_command(SCD4X_CMD_ALTITUDE_COMPENSATION, altitude_compensation_)) {
|
||||
ESP_LOGE(TAG, "Error setting altitude compensation.");
|
||||
if (!this->write_command(SCD4X_CMD_ALTITUDE_COMPENSATION, this->altitude_compensation_)) {
|
||||
ESP_LOGE(TAG, "Error setting altitude compensation");
|
||||
this->error_code_ = MEASUREMENT_INIT_FAILED;
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (!this->write_command(SCD4X_CMD_AUTOMATIC_SELF_CALIBRATION, enable_asc_ ? 1 : 0)) {
|
||||
ESP_LOGE(TAG, "Error setting automatic self calibration.");
|
||||
if (!this->write_command(SCD4X_CMD_AUTOMATIC_SELF_CALIBRATION, this->enable_asc_ ? 1 : 0)) {
|
||||
ESP_LOGE(TAG, "Error setting automatic self calibration");
|
||||
this->error_code_ = MEASUREMENT_INIT_FAILED;
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
initialized_ = true;
|
||||
this->initialized_ = true;
|
||||
// Finally start sensor measurements
|
||||
this->start_measurement_();
|
||||
ESP_LOGD(TAG, "Sensor initialized");
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
void SCD4XComponent::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "scd4x:");
|
||||
static const char *const MM_PERIODIC_STR = "Periodic (5s)";
|
||||
static const char *const MM_LOW_POWER_PERIODIC_STR = "Low power periodic (30s)";
|
||||
static const char *const MM_SINGLE_SHOT_STR = "Single shot";
|
||||
static const char *const MM_SINGLE_SHOT_RHT_ONLY_STR = "Single shot rht only";
|
||||
const char *measurement_mode_str = MM_PERIODIC_STR;
|
||||
|
||||
switch (this->measurement_mode_) {
|
||||
case PERIODIC:
|
||||
// measurement_mode_str = MM_PERIODIC_STR;
|
||||
break;
|
||||
case LOW_POWER_PERIODIC:
|
||||
measurement_mode_str = MM_LOW_POWER_PERIODIC_STR;
|
||||
break;
|
||||
case SINGLE_SHOT:
|
||||
measurement_mode_str = MM_SINGLE_SHOT_STR;
|
||||
break;
|
||||
case SINGLE_SHOT_RHT_ONLY:
|
||||
measurement_mode_str = MM_SINGLE_SHOT_RHT_ONLY_STR;
|
||||
break;
|
||||
}
|
||||
|
||||
ESP_LOGCONFIG(TAG, "SCD4X:");
|
||||
LOG_I2C_DEVICE(this);
|
||||
if (this->is_failed()) {
|
||||
switch (this->error_code_) {
|
||||
@ -102,19 +121,23 @@ void SCD4XComponent::dump_config() {
|
||||
ESP_LOGW(TAG, "Measurement Initialization failed");
|
||||
break;
|
||||
case SERIAL_NUMBER_IDENTIFICATION_FAILED:
|
||||
ESP_LOGW(TAG, "Unable to read sensor firmware version");
|
||||
ESP_LOGW(TAG, "Unable to read firmware version");
|
||||
break;
|
||||
default:
|
||||
ESP_LOGW(TAG, "Unknown setup error");
|
||||
break;
|
||||
}
|
||||
}
|
||||
ESP_LOGCONFIG(TAG, " Automatic self calibration: %s", ONOFF(this->enable_asc_));
|
||||
ESP_LOGCONFIG(TAG,
|
||||
" Automatic self calibration: %s\n"
|
||||
" Measurement mode: %s\n"
|
||||
" Temperature offset: %.2f °C",
|
||||
ONOFF(this->enable_asc_), measurement_mode_str, this->temperature_offset_);
|
||||
if (this->ambient_pressure_source_ != nullptr) {
|
||||
ESP_LOGCONFIG(TAG, " Dynamic ambient pressure compensation using sensor '%s'",
|
||||
ESP_LOGCONFIG(TAG, " Dynamic ambient pressure compensation using '%s'",
|
||||
this->ambient_pressure_source_->get_name().c_str());
|
||||
} else {
|
||||
if (this->ambient_pressure_compensation_) {
|
||||
if (this->ambient_pressure_) {
|
||||
ESP_LOGCONFIG(TAG,
|
||||
" Altitude compensation disabled\n"
|
||||
" Ambient pressure compensation: %dmBar",
|
||||
@ -126,21 +149,6 @@ void SCD4XComponent::dump_config() {
|
||||
this->altitude_compensation_);
|
||||
}
|
||||
}
|
||||
switch (this->measurement_mode_) {
|
||||
case PERIODIC:
|
||||
ESP_LOGCONFIG(TAG, " Measurement mode: periodic (5s)");
|
||||
break;
|
||||
case LOW_POWER_PERIODIC:
|
||||
ESP_LOGCONFIG(TAG, " Measurement mode: low power periodic (30s)");
|
||||
break;
|
||||
case SINGLE_SHOT:
|
||||
ESP_LOGCONFIG(TAG, " Measurement mode: single shot");
|
||||
break;
|
||||
case SINGLE_SHOT_RHT_ONLY:
|
||||
ESP_LOGCONFIG(TAG, " Measurement mode: single shot rht only");
|
||||
break;
|
||||
}
|
||||
ESP_LOGCONFIG(TAG, " Temperature offset: %.2f °C", this->temperature_offset_);
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
LOG_SENSOR(" ", "CO2", this->co2_sensor_);
|
||||
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
|
||||
@ -148,20 +156,20 @@ void SCD4XComponent::dump_config() {
|
||||
}
|
||||
|
||||
void SCD4XComponent::update() {
|
||||
if (!initialized_) {
|
||||
if (!this->initialized_) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->ambient_pressure_source_ != nullptr) {
|
||||
float pressure = this->ambient_pressure_source_->state;
|
||||
if (!std::isnan(pressure)) {
|
||||
set_ambient_pressure_compensation(pressure);
|
||||
this->set_ambient_pressure_compensation(pressure);
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t wait_time = 0;
|
||||
if (this->measurement_mode_ == SINGLE_SHOT || this->measurement_mode_ == SINGLE_SHOT_RHT_ONLY) {
|
||||
start_measurement_();
|
||||
this->start_measurement_();
|
||||
wait_time =
|
||||
this->measurement_mode_ == SINGLE_SHOT ? 5000 : 50; // Single shot measurement takes 5 secs rht mode 50 ms
|
||||
}
|
||||
@ -176,12 +184,12 @@ void SCD4XComponent::update() {
|
||||
|
||||
if (!this->read_data(raw_read_status) || raw_read_status == 0x00) {
|
||||
this->status_set_warning();
|
||||
ESP_LOGW(TAG, "Data not ready yet!");
|
||||
ESP_LOGW(TAG, "Data not ready");
|
||||
return;
|
||||
}
|
||||
|
||||
if (!this->write_command(SCD4X_CMD_READ_MEASUREMENT)) {
|
||||
ESP_LOGW(TAG, "Error reading measurement!");
|
||||
ESP_LOGW(TAG, "Error reading measurement");
|
||||
this->status_set_warning();
|
||||
return; // NO RETRY
|
||||
}
|
||||
@ -218,19 +226,19 @@ bool SCD4XComponent::perform_forced_calibration(uint16_t current_co2_concentrati
|
||||
}
|
||||
this->set_timeout(500, [this, current_co2_concentration]() {
|
||||
if (this->write_command(SCD4X_CMD_PERFORM_FORCED_CALIBRATION, current_co2_concentration)) {
|
||||
ESP_LOGD(TAG, "setting forced calibration Co2 level %d ppm", current_co2_concentration);
|
||||
ESP_LOGD(TAG, "Setting forced calibration Co2 level %d ppm", current_co2_concentration);
|
||||
// frc takes 400 ms
|
||||
// because this method will be used very rarly
|
||||
// the simple approach with delay is ok
|
||||
delay(400); // NOLINT'
|
||||
delay(400); // NOLINT
|
||||
if (!this->start_measurement_()) {
|
||||
return false;
|
||||
} else {
|
||||
ESP_LOGD(TAG, "forced calibration complete");
|
||||
ESP_LOGD(TAG, "Forced calibration complete");
|
||||
}
|
||||
return true;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "force calibration failed");
|
||||
ESP_LOGE(TAG, "Force calibration failed");
|
||||
this->error_code_ = FRC_FAILED;
|
||||
this->status_set_warning();
|
||||
return false;
|
||||
@ -259,27 +267,26 @@ bool SCD4XComponent::factory_reset() {
|
||||
}
|
||||
|
||||
void SCD4XComponent::set_ambient_pressure_compensation(float pressure_in_hpa) {
|
||||
ambient_pressure_compensation_ = true;
|
||||
uint16_t new_ambient_pressure = (uint16_t) pressure_in_hpa;
|
||||
if (!initialized_) {
|
||||
ambient_pressure_ = new_ambient_pressure;
|
||||
uint16_t new_ambient_pressure = static_cast<uint16_t>(pressure_in_hpa);
|
||||
if (!this->initialized_) {
|
||||
this->ambient_pressure_ = new_ambient_pressure;
|
||||
return;
|
||||
}
|
||||
// Only send pressure value if it has changed since last update
|
||||
if (new_ambient_pressure != ambient_pressure_) {
|
||||
update_ambient_pressure_compensation_(new_ambient_pressure);
|
||||
ambient_pressure_ = new_ambient_pressure;
|
||||
if (new_ambient_pressure != this->ambient_pressure_) {
|
||||
this->update_ambient_pressure_compensation_(new_ambient_pressure);
|
||||
this->ambient_pressure_ = new_ambient_pressure;
|
||||
} else {
|
||||
ESP_LOGD(TAG, "ambient pressure compensation skipped - no change required");
|
||||
ESP_LOGD(TAG, "Ambient pressure compensation skipped; no change required");
|
||||
}
|
||||
}
|
||||
|
||||
bool SCD4XComponent::update_ambient_pressure_compensation_(uint16_t pressure_in_hpa) {
|
||||
if (this->write_command(SCD4X_CMD_AMBIENT_PRESSURE_COMPENSATION, pressure_in_hpa)) {
|
||||
ESP_LOGD(TAG, "setting ambient pressure compensation to %d hPa", pressure_in_hpa);
|
||||
ESP_LOGD(TAG, "Setting ambient pressure compensation to %d hPa", pressure_in_hpa);
|
||||
return true;
|
||||
} else {
|
||||
ESP_LOGE(TAG, "Error setting ambient pressure compensation.");
|
||||
ESP_LOGE(TAG, "Error setting ambient pressure compensation");
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -304,7 +311,7 @@ bool SCD4XComponent::start_measurement_() {
|
||||
static uint8_t remaining_retries = 3;
|
||||
while (remaining_retries) {
|
||||
if (!this->write_command(measurement_command)) {
|
||||
ESP_LOGE(TAG, "Error starting measurements.");
|
||||
ESP_LOGE(TAG, "Error starting measurements");
|
||||
this->error_code_ = MEASUREMENT_INIT_FAILED;
|
||||
this->status_set_warning();
|
||||
if (--remaining_retries == 0)
|
||||
|
@ -8,14 +8,20 @@
|
||||
namespace esphome {
|
||||
namespace scd4x {
|
||||
|
||||
enum ERRORCODE {
|
||||
enum ErrorCode : uint8_t {
|
||||
COMMUNICATION_FAILED,
|
||||
SERIAL_NUMBER_IDENTIFICATION_FAILED,
|
||||
MEASUREMENT_INIT_FAILED,
|
||||
FRC_FAILED,
|
||||
UNKNOWN
|
||||
UNKNOWN,
|
||||
};
|
||||
|
||||
enum MeasurementMode : uint8_t {
|
||||
PERIODIC,
|
||||
LOW_POWER_PERIODIC,
|
||||
SINGLE_SHOT,
|
||||
SINGLE_SHOT_RHT_ONLY,
|
||||
};
|
||||
enum MeasurementMode { PERIODIC, LOW_POWER_PERIODIC, SINGLE_SHOT, SINGLE_SHOT_RHT_ONLY };
|
||||
|
||||
class SCD4XComponent : public PollingComponent, public sensirion_common::SensirionI2CDevice {
|
||||
public:
|
||||
@ -39,21 +45,18 @@ class SCD4XComponent : public PollingComponent, public sensirion_common::Sensiri
|
||||
protected:
|
||||
bool update_ambient_pressure_compensation_(uint16_t pressure_in_hpa);
|
||||
bool start_measurement_();
|
||||
ERRORCODE error_code_;
|
||||
|
||||
bool initialized_{false};
|
||||
|
||||
float temperature_offset_;
|
||||
uint16_t altitude_compensation_;
|
||||
bool ambient_pressure_compensation_;
|
||||
uint16_t ambient_pressure_;
|
||||
bool enable_asc_;
|
||||
MeasurementMode measurement_mode_{PERIODIC};
|
||||
sensor::Sensor *co2_sensor_{nullptr};
|
||||
sensor::Sensor *temperature_sensor_{nullptr};
|
||||
sensor::Sensor *humidity_sensor_{nullptr};
|
||||
// used for compensation
|
||||
sensor::Sensor *ambient_pressure_source_{nullptr};
|
||||
sensor::Sensor *ambient_pressure_source_{nullptr}; // used for compensation
|
||||
float temperature_offset_;
|
||||
uint16_t altitude_compensation_{0};
|
||||
uint16_t ambient_pressure_{0}; // Per datasheet, valid values are 700 to 1200 hPa; 0 is a valid sentinel value
|
||||
bool initialized_{false};
|
||||
bool enable_asc_{false};
|
||||
ErrorCode error_code_;
|
||||
MeasurementMode measurement_mode_{PERIODIC};
|
||||
};
|
||||
|
||||
} // namespace scd4x
|
||||
|
325
esphome/components/sx127x/__init__.py
Normal file
325
esphome/components/sx127x/__init__.py
Normal file
@ -0,0 +1,325 @@
|
||||
from esphome import automation, pins
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import spi
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_DATA, CONF_FREQUENCY, CONF_ID
|
||||
|
||||
MULTI_CONF = True
|
||||
CODEOWNERS = ["@swoboda1337"]
|
||||
DEPENDENCIES = ["spi"]
|
||||
|
||||
CONF_SX127X_ID = "sx127x_id"
|
||||
|
||||
CONF_AUTO_CAL = "auto_cal"
|
||||
CONF_BANDWIDTH = "bandwidth"
|
||||
CONF_BITRATE = "bitrate"
|
||||
CONF_BITSYNC = "bitsync"
|
||||
CONF_CODING_RATE = "coding_rate"
|
||||
CONF_CRC_ENABLE = "crc_enable"
|
||||
CONF_DEVIATION = "deviation"
|
||||
CONF_DIO0_PIN = "dio0_pin"
|
||||
CONF_MODULATION = "modulation"
|
||||
CONF_ON_PACKET = "on_packet"
|
||||
CONF_PA_PIN = "pa_pin"
|
||||
CONF_PA_POWER = "pa_power"
|
||||
CONF_PA_RAMP = "pa_ramp"
|
||||
CONF_PACKET_MODE = "packet_mode"
|
||||
CONF_PAYLOAD_LENGTH = "payload_length"
|
||||
CONF_PREAMBLE_DETECT = "preamble_detect"
|
||||
CONF_PREAMBLE_ERRORS = "preamble_errors"
|
||||
CONF_PREAMBLE_POLARITY = "preamble_polarity"
|
||||
CONF_PREAMBLE_SIZE = "preamble_size"
|
||||
CONF_RST_PIN = "rst_pin"
|
||||
CONF_RX_FLOOR = "rx_floor"
|
||||
CONF_RX_START = "rx_start"
|
||||
CONF_SHAPING = "shaping"
|
||||
CONF_SPREADING_FACTOR = "spreading_factor"
|
||||
CONF_SYNC_VALUE = "sync_value"
|
||||
|
||||
sx127x_ns = cg.esphome_ns.namespace("sx127x")
|
||||
SX127x = sx127x_ns.class_("SX127x", cg.Component, spi.SPIDevice)
|
||||
SX127xListener = sx127x_ns.class_("SX127xListener")
|
||||
SX127xBw = sx127x_ns.enum("SX127xBw")
|
||||
SX127xOpMode = sx127x_ns.enum("SX127xOpMode")
|
||||
SX127xPaConfig = sx127x_ns.enum("SX127xPaConfig")
|
||||
SX127xPaRamp = sx127x_ns.enum("SX127xPaRamp")
|
||||
SX127xModemCfg1 = sx127x_ns.enum("SX127xModemCfg1")
|
||||
|
||||
BW = {
|
||||
"2_6kHz": SX127xBw.SX127X_BW_2_6,
|
||||
"3_1kHz": SX127xBw.SX127X_BW_3_1,
|
||||
"3_9kHz": SX127xBw.SX127X_BW_3_9,
|
||||
"5_2kHz": SX127xBw.SX127X_BW_5_2,
|
||||
"6_3kHz": SX127xBw.SX127X_BW_6_3,
|
||||
"7_8kHz": SX127xBw.SX127X_BW_7_8,
|
||||
"10_4kHz": SX127xBw.SX127X_BW_10_4,
|
||||
"12_5kHz": SX127xBw.SX127X_BW_12_5,
|
||||
"15_6kHz": SX127xBw.SX127X_BW_15_6,
|
||||
"20_8kHz": SX127xBw.SX127X_BW_20_8,
|
||||
"25_0kHz": SX127xBw.SX127X_BW_25_0,
|
||||
"31_3kHz": SX127xBw.SX127X_BW_31_3,
|
||||
"41_7kHz": SX127xBw.SX127X_BW_41_7,
|
||||
"50_0kHz": SX127xBw.SX127X_BW_50_0,
|
||||
"62_5kHz": SX127xBw.SX127X_BW_62_5,
|
||||
"83_3kHz": SX127xBw.SX127X_BW_83_3,
|
||||
"100_0kHz": SX127xBw.SX127X_BW_100_0,
|
||||
"125_0kHz": SX127xBw.SX127X_BW_125_0,
|
||||
"166_7kHz": SX127xBw.SX127X_BW_166_7,
|
||||
"200_0kHz": SX127xBw.SX127X_BW_200_0,
|
||||
"250_0kHz": SX127xBw.SX127X_BW_250_0,
|
||||
"500_0kHz": SX127xBw.SX127X_BW_500_0,
|
||||
}
|
||||
|
||||
CODING_RATE = {
|
||||
"CR_4_5": SX127xModemCfg1.CODING_RATE_4_5,
|
||||
"CR_4_6": SX127xModemCfg1.CODING_RATE_4_6,
|
||||
"CR_4_7": SX127xModemCfg1.CODING_RATE_4_7,
|
||||
"CR_4_8": SX127xModemCfg1.CODING_RATE_4_8,
|
||||
}
|
||||
|
||||
MOD = {
|
||||
"LORA": SX127xOpMode.MOD_LORA,
|
||||
"FSK": SX127xOpMode.MOD_FSK,
|
||||
"OOK": SX127xOpMode.MOD_OOK,
|
||||
}
|
||||
|
||||
PA_PIN = {
|
||||
"RFO": SX127xPaConfig.PA_PIN_RFO,
|
||||
"BOOST": SX127xPaConfig.PA_PIN_BOOST,
|
||||
}
|
||||
|
||||
RAMP = {
|
||||
"10us": SX127xPaRamp.PA_RAMP_10,
|
||||
"12us": SX127xPaRamp.PA_RAMP_12,
|
||||
"15us": SX127xPaRamp.PA_RAMP_15,
|
||||
"20us": SX127xPaRamp.PA_RAMP_20,
|
||||
"25us": SX127xPaRamp.PA_RAMP_25,
|
||||
"31us": SX127xPaRamp.PA_RAMP_31,
|
||||
"40us": SX127xPaRamp.PA_RAMP_40,
|
||||
"50us": SX127xPaRamp.PA_RAMP_50,
|
||||
"62us": SX127xPaRamp.PA_RAMP_62,
|
||||
"100us": SX127xPaRamp.PA_RAMP_100,
|
||||
"125us": SX127xPaRamp.PA_RAMP_125,
|
||||
"250us": SX127xPaRamp.PA_RAMP_250,
|
||||
"500us": SX127xPaRamp.PA_RAMP_500,
|
||||
"1000us": SX127xPaRamp.PA_RAMP_1000,
|
||||
"2000us": SX127xPaRamp.PA_RAMP_2000,
|
||||
"3400us": SX127xPaRamp.PA_RAMP_3400,
|
||||
}
|
||||
|
||||
SHAPING = {
|
||||
"CUTOFF_BR_X_2": SX127xPaRamp.CUTOFF_BR_X_2,
|
||||
"CUTOFF_BR_X_1": SX127xPaRamp.CUTOFF_BR_X_1,
|
||||
"GAUSSIAN_BT_0_3": SX127xPaRamp.GAUSSIAN_BT_0_3,
|
||||
"GAUSSIAN_BT_0_5": SX127xPaRamp.GAUSSIAN_BT_0_5,
|
||||
"GAUSSIAN_BT_1_0": SX127xPaRamp.GAUSSIAN_BT_1_0,
|
||||
"NONE": SX127xPaRamp.SHAPING_NONE,
|
||||
}
|
||||
|
||||
RunImageCalAction = sx127x_ns.class_(
|
||||
"RunImageCalAction", automation.Action, cg.Parented.template(SX127x)
|
||||
)
|
||||
SendPacketAction = sx127x_ns.class_(
|
||||
"SendPacketAction", automation.Action, cg.Parented.template(SX127x)
|
||||
)
|
||||
SetModeTxAction = sx127x_ns.class_(
|
||||
"SetModeTxAction", automation.Action, cg.Parented.template(SX127x)
|
||||
)
|
||||
SetModeRxAction = sx127x_ns.class_(
|
||||
"SetModeRxAction", automation.Action, cg.Parented.template(SX127x)
|
||||
)
|
||||
SetModeSleepAction = sx127x_ns.class_(
|
||||
"SetModeSleepAction", automation.Action, cg.Parented.template(SX127x)
|
||||
)
|
||||
SetModeStandbyAction = sx127x_ns.class_(
|
||||
"SetModeStandbyAction", automation.Action, cg.Parented.template(SX127x)
|
||||
)
|
||||
|
||||
|
||||
def validate_raw_data(value):
|
||||
if isinstance(value, str):
|
||||
return value.encode("utf-8")
|
||||
if isinstance(value, list):
|
||||
return cv.Schema([cv.hex_uint8_t])(value)
|
||||
raise cv.Invalid(
|
||||
"data must either be a string wrapped in quotes or a list of bytes"
|
||||
)
|
||||
|
||||
|
||||
def validate_config(config):
|
||||
if config[CONF_MODULATION] == "LORA":
|
||||
bws = [
|
||||
"7_8kHz",
|
||||
"10_4kHz",
|
||||
"15_6kHz",
|
||||
"20_8kHz",
|
||||
"31_3kHz",
|
||||
"41_7kHz",
|
||||
"62_5kHz",
|
||||
"125_0kHz",
|
||||
"250_0kHz",
|
||||
"500_0kHz",
|
||||
]
|
||||
if config[CONF_BANDWIDTH] not in bws:
|
||||
raise cv.Invalid(f"{config[CONF_BANDWIDTH]} is not available with LORA")
|
||||
if CONF_DIO0_PIN not in config:
|
||||
raise cv.Invalid("Cannot use LoRa without dio0_pin")
|
||||
if 0 < config[CONF_PREAMBLE_SIZE] < 6:
|
||||
raise cv.Invalid("Minimum preamble size is 6 with LORA")
|
||||
if config[CONF_SPREADING_FACTOR] == 6 and config[CONF_PAYLOAD_LENGTH] == 0:
|
||||
raise cv.Invalid("Payload length must be set when spreading factor is 6")
|
||||
else:
|
||||
if config[CONF_BANDWIDTH] == "500_0kHz":
|
||||
raise cv.Invalid(f"{config[CONF_BANDWIDTH]} is only available with LORA")
|
||||
if CONF_BITSYNC not in config:
|
||||
raise cv.Invalid("Config 'bitsync' required with FSK/OOK")
|
||||
if CONF_PACKET_MODE not in config:
|
||||
raise cv.Invalid("Config 'packet_mode' required with FSK/OOK")
|
||||
if config[CONF_PACKET_MODE] and CONF_DIO0_PIN not in config:
|
||||
raise cv.Invalid("Config 'dio0_pin' required in packet mode")
|
||||
if config[CONF_PAYLOAD_LENGTH] > 64:
|
||||
raise cv.Invalid("Payload length must be <= 64 with FSK/OOK")
|
||||
if config[CONF_PA_PIN] == "RFO" and config[CONF_PA_POWER] > 15:
|
||||
raise cv.Invalid("PA power must be <= 15 dbm when using the RFO pin")
|
||||
if config[CONF_PA_PIN] == "BOOST" and config[CONF_PA_POWER] < 2:
|
||||
raise cv.Invalid("PA power must be >= 2 dbm when using the BOOST pin")
|
||||
return config
|
||||
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(SX127x),
|
||||
cv.Optional(CONF_AUTO_CAL, default=True): cv.boolean,
|
||||
cv.Optional(CONF_BANDWIDTH, default="125_0kHz"): cv.enum(BW),
|
||||
cv.Optional(CONF_BITRATE, default=4800): cv.int_range(min=500, max=300000),
|
||||
cv.Optional(CONF_BITSYNC): cv.boolean,
|
||||
cv.Optional(CONF_CODING_RATE, default="CR_4_5"): cv.enum(CODING_RATE),
|
||||
cv.Optional(CONF_CRC_ENABLE, default=False): cv.boolean,
|
||||
cv.Optional(CONF_DEVIATION, default=5000): cv.int_range(min=0, max=100000),
|
||||
cv.Optional(CONF_DIO0_PIN): pins.internal_gpio_input_pin_schema,
|
||||
cv.Required(CONF_FREQUENCY): cv.int_range(min=137000000, max=1020000000),
|
||||
cv.Required(CONF_MODULATION): cv.enum(MOD),
|
||||
cv.Optional(CONF_ON_PACKET): automation.validate_automation(single=True),
|
||||
cv.Optional(CONF_PA_PIN, default="BOOST"): cv.enum(PA_PIN),
|
||||
cv.Optional(CONF_PA_POWER, default=17): cv.int_range(min=0, max=17),
|
||||
cv.Optional(CONF_PA_RAMP, default="40us"): cv.enum(RAMP),
|
||||
cv.Optional(CONF_PACKET_MODE): cv.boolean,
|
||||
cv.Optional(CONF_PAYLOAD_LENGTH, default=0): cv.int_range(min=0, max=256),
|
||||
cv.Optional(CONF_PREAMBLE_DETECT, default=0): cv.int_range(min=0, max=3),
|
||||
cv.Optional(CONF_PREAMBLE_ERRORS, default=0): cv.int_range(min=0, max=31),
|
||||
cv.Optional(CONF_PREAMBLE_POLARITY, default=0xAA): cv.All(
|
||||
cv.hex_int, cv.one_of(0xAA, 0x55)
|
||||
),
|
||||
cv.Optional(CONF_PREAMBLE_SIZE, default=0): cv.int_range(min=0, max=65535),
|
||||
cv.Required(CONF_RST_PIN): pins.internal_gpio_output_pin_schema,
|
||||
cv.Optional(CONF_RX_FLOOR, default=-94): cv.float_range(min=-128, max=-1),
|
||||
cv.Optional(CONF_RX_START, default=True): cv.boolean,
|
||||
cv.Optional(CONF_SHAPING, default="NONE"): cv.enum(SHAPING),
|
||||
cv.Optional(CONF_SPREADING_FACTOR, default=7): cv.int_range(min=6, max=12),
|
||||
cv.Optional(CONF_SYNC_VALUE, default=[]): cv.ensure_list(cv.hex_uint8_t),
|
||||
},
|
||||
)
|
||||
.extend(cv.COMPONENT_SCHEMA)
|
||||
.extend(spi.spi_device_schema(True, 8e6, "mode0"))
|
||||
.add_extra(validate_config)
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
await spi.register_spi_device(var, config)
|
||||
if CONF_ON_PACKET in config:
|
||||
await automation.build_automation(
|
||||
var.get_packet_trigger(),
|
||||
[
|
||||
(cg.std_vector.template(cg.uint8), "x"),
|
||||
(cg.float_, "rssi"),
|
||||
(cg.float_, "snr"),
|
||||
],
|
||||
config[CONF_ON_PACKET],
|
||||
)
|
||||
if CONF_DIO0_PIN in config:
|
||||
dio0_pin = await cg.gpio_pin_expression(config[CONF_DIO0_PIN])
|
||||
cg.add(var.set_dio0_pin(dio0_pin))
|
||||
rst_pin = await cg.gpio_pin_expression(config[CONF_RST_PIN])
|
||||
cg.add(var.set_rst_pin(rst_pin))
|
||||
cg.add(var.set_auto_cal(config[CONF_AUTO_CAL]))
|
||||
cg.add(var.set_bandwidth(config[CONF_BANDWIDTH]))
|
||||
cg.add(var.set_frequency(config[CONF_FREQUENCY]))
|
||||
cg.add(var.set_deviation(config[CONF_DEVIATION]))
|
||||
cg.add(var.set_modulation(config[CONF_MODULATION]))
|
||||
if config[CONF_MODULATION] != "LORA":
|
||||
cg.add(var.set_bitrate(config[CONF_BITRATE]))
|
||||
cg.add(var.set_bitsync(config[CONF_BITSYNC]))
|
||||
cg.add(var.set_packet_mode(config[CONF_PACKET_MODE]))
|
||||
cg.add(var.set_pa_pin(config[CONF_PA_PIN]))
|
||||
cg.add(var.set_pa_ramp(config[CONF_PA_RAMP]))
|
||||
cg.add(var.set_pa_power(config[CONF_PA_POWER]))
|
||||
cg.add(var.set_shaping(config[CONF_SHAPING]))
|
||||
cg.add(var.set_crc_enable(config[CONF_CRC_ENABLE]))
|
||||
cg.add(var.set_payload_length(config[CONF_PAYLOAD_LENGTH]))
|
||||
cg.add(var.set_preamble_detect(config[CONF_PREAMBLE_DETECT]))
|
||||
cg.add(var.set_preamble_size(config[CONF_PREAMBLE_SIZE]))
|
||||
cg.add(var.set_preamble_polarity(config[CONF_PREAMBLE_POLARITY]))
|
||||
cg.add(var.set_preamble_errors(config[CONF_PREAMBLE_ERRORS]))
|
||||
cg.add(var.set_coding_rate(config[CONF_CODING_RATE]))
|
||||
cg.add(var.set_spreading_factor(config[CONF_SPREADING_FACTOR]))
|
||||
cg.add(var.set_sync_value(config[CONF_SYNC_VALUE]))
|
||||
cg.add(var.set_rx_floor(config[CONF_RX_FLOOR]))
|
||||
cg.add(var.set_rx_start(config[CONF_RX_START]))
|
||||
|
||||
|
||||
NO_ARGS_ACTION_SCHEMA = automation.maybe_simple_id(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(SX127x),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"sx127x.run_image_cal", RunImageCalAction, NO_ARGS_ACTION_SCHEMA
|
||||
)
|
||||
@automation.register_action(
|
||||
"sx127x.set_mode_tx", SetModeTxAction, NO_ARGS_ACTION_SCHEMA
|
||||
)
|
||||
@automation.register_action(
|
||||
"sx127x.set_mode_rx", SetModeRxAction, NO_ARGS_ACTION_SCHEMA
|
||||
)
|
||||
@automation.register_action(
|
||||
"sx127x.set_mode_sleep", SetModeSleepAction, NO_ARGS_ACTION_SCHEMA
|
||||
)
|
||||
@automation.register_action(
|
||||
"sx127x.set_mode_standby", SetModeStandbyAction, NO_ARGS_ACTION_SCHEMA
|
||||
)
|
||||
async def no_args_action_to_code(config, action_id, template_arg, args):
|
||||
var = cg.new_Pvariable(action_id, template_arg)
|
||||
await cg.register_parented(var, config[CONF_ID])
|
||||
return var
|
||||
|
||||
|
||||
SEND_PACKET_ACTION_SCHEMA = cv.maybe_simple_value(
|
||||
{
|
||||
cv.GenerateID(): cv.use_id(SX127x),
|
||||
cv.Required(CONF_DATA): cv.templatable(validate_raw_data),
|
||||
},
|
||||
key=CONF_DATA,
|
||||
)
|
||||
|
||||
|
||||
@automation.register_action(
|
||||
"sx127x.send_packet", SendPacketAction, SEND_PACKET_ACTION_SCHEMA
|
||||
)
|
||||
async def send_packet_action_to_code(config, action_id, template_arg, args):
|
||||
var = cg.new_Pvariable(action_id, template_arg)
|
||||
await cg.register_parented(var, config[CONF_ID])
|
||||
data = config[CONF_DATA]
|
||||
if isinstance(data, bytes):
|
||||
data = list(data)
|
||||
if cg.is_template(data):
|
||||
templ = await cg.templatable(data, args, cg.std_vector.template(cg.uint8))
|
||||
cg.add(var.set_data_template(templ))
|
||||
else:
|
||||
cg.add(var.set_data_static(data))
|
||||
return var
|
62
esphome/components/sx127x/automation.h
Normal file
62
esphome/components/sx127x/automation.h
Normal file
@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/components/sx127x/sx127x.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace sx127x {
|
||||
|
||||
template<typename... Ts> class RunImageCalAction : public Action<Ts...>, public Parented<SX127x> {
|
||||
public:
|
||||
void play(Ts... x) override { this->parent_->run_image_cal(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class SendPacketAction : public Action<Ts...>, public Parented<SX127x> {
|
||||
public:
|
||||
void set_data_template(std::function<std::vector<uint8_t>(Ts...)> func) {
|
||||
this->data_func_ = func;
|
||||
this->static_ = false;
|
||||
}
|
||||
|
||||
void set_data_static(const std::vector<uint8_t> &data) {
|
||||
this->data_static_ = data;
|
||||
this->static_ = true;
|
||||
}
|
||||
|
||||
void play(Ts... x) override {
|
||||
if (this->static_) {
|
||||
this->parent_->transmit_packet(this->data_static_);
|
||||
} else {
|
||||
this->parent_->transmit_packet(this->data_func_(x...));
|
||||
}
|
||||
}
|
||||
|
||||
protected:
|
||||
bool static_{false};
|
||||
std::function<std::vector<uint8_t>(Ts...)> data_func_{};
|
||||
std::vector<uint8_t> data_static_{};
|
||||
};
|
||||
|
||||
template<typename... Ts> class SetModeTxAction : public Action<Ts...>, public Parented<SX127x> {
|
||||
public:
|
||||
void play(Ts... x) override { this->parent_->set_mode_tx(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class SetModeRxAction : public Action<Ts...>, public Parented<SX127x> {
|
||||
public:
|
||||
void play(Ts... x) override { this->parent_->set_mode_rx(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class SetModeSleepAction : public Action<Ts...>, public Parented<SX127x> {
|
||||
public:
|
||||
void play(Ts... x) override { this->parent_->set_mode_sleep(); }
|
||||
};
|
||||
|
||||
template<typename... Ts> class SetModeStandbyAction : public Action<Ts...>, public Parented<SX127x> {
|
||||
public:
|
||||
void play(Ts... x) override { this->parent_->set_mode_standby(); }
|
||||
};
|
||||
|
||||
} // namespace sx127x
|
||||
} // namespace esphome
|
26
esphome/components/sx127x/packet_transport/__init__.py
Normal file
26
esphome/components/sx127x/packet_transport/__init__.py
Normal file
@ -0,0 +1,26 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components.packet_transport import (
|
||||
PacketTransport,
|
||||
new_packet_transport,
|
||||
transport_schema,
|
||||
)
|
||||
import esphome.config_validation as cv
|
||||
from esphome.cpp_types import PollingComponent
|
||||
|
||||
from .. import CONF_SX127X_ID, SX127x, SX127xListener, sx127x_ns
|
||||
|
||||
SX127xTransport = sx127x_ns.class_(
|
||||
"SX127xTransport", PacketTransport, PollingComponent, SX127xListener
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = transport_schema(SX127xTransport).extend(
|
||||
{
|
||||
cv.GenerateID(CONF_SX127X_ID): cv.use_id(SX127x),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var, _ = await new_packet_transport(config)
|
||||
sx127x = await cg.get_variable(config[CONF_SX127X_ID])
|
||||
cg.add(var.set_parent(sx127x))
|
@ -0,0 +1,26 @@
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/application.h"
|
||||
#include "sx127x_transport.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace sx127x {
|
||||
|
||||
static const char *const TAG = "sx127x_transport";
|
||||
|
||||
void SX127xTransport::setup() {
|
||||
PacketTransport::setup();
|
||||
this->parent_->register_listener(this);
|
||||
}
|
||||
|
||||
void SX127xTransport::update() {
|
||||
PacketTransport::update();
|
||||
this->updated_ = true;
|
||||
this->resend_data_ = true;
|
||||
}
|
||||
|
||||
void SX127xTransport::send_packet(const std::vector<uint8_t> &buf) const { this->parent_->transmit_packet(buf); }
|
||||
|
||||
void SX127xTransport::on_packet(const std::vector<uint8_t> &packet, float rssi, float snr) { this->process_(packet); }
|
||||
|
||||
} // namespace sx127x
|
||||
} // namespace esphome
|
@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sx127x/sx127x.h"
|
||||
#include "esphome/components/packet_transport/packet_transport.h"
|
||||
#include <vector>
|
||||
|
||||
namespace esphome {
|
||||
namespace sx127x {
|
||||
|
||||
class SX127xTransport : public packet_transport::PacketTransport, public Parented<SX127x>, public SX127xListener {
|
||||
public:
|
||||
void setup() override;
|
||||
void update() override;
|
||||
void on_packet(const std::vector<uint8_t> &packet, float rssi, float snr) override;
|
||||
float get_setup_priority() const override { return setup_priority::AFTER_WIFI; }
|
||||
|
||||
protected:
|
||||
void send_packet(const std::vector<uint8_t> &buf) const override;
|
||||
bool should_send() override { return true; }
|
||||
size_t get_max_packet_size() override { return this->parent_->get_max_packet_size(); }
|
||||
};
|
||||
|
||||
} // namespace sx127x
|
||||
} // namespace esphome
|
498
esphome/components/sx127x/sx127x.cpp
Normal file
498
esphome/components/sx127x/sx127x.cpp
Normal file
@ -0,0 +1,498 @@
|
||||
#include "sx127x.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace sx127x {
|
||||
|
||||
static const char *const TAG = "sx127x";
|
||||
static const uint32_t FXOSC = 32000000u;
|
||||
static const uint16_t RAMP[16] = {3400, 2000, 1000, 500, 250, 125, 100, 62, 50, 40, 31, 25, 20, 15, 12, 10};
|
||||
static const uint32_t BW_HZ[22] = {2604, 3125, 3906, 5208, 6250, 7812, 10416, 12500, 15625, 20833, 25000,
|
||||
31250, 41666, 50000, 62500, 83333, 100000, 125000, 166666, 200000, 250000, 500000};
|
||||
static const uint8_t BW_LORA[22] = {BW_7_8, BW_7_8, BW_7_8, BW_7_8, BW_7_8, BW_7_8, BW_10_4, BW_15_6,
|
||||
BW_15_6, BW_20_8, BW_31_3, BW_31_3, BW_41_7, BW_62_5, BW_62_5, BW_125_0,
|
||||
BW_125_0, BW_125_0, BW_250_0, BW_250_0, BW_250_0, BW_500_0};
|
||||
static const uint8_t BW_FSK_OOK[22] = {RX_BW_2_6, RX_BW_3_1, RX_BW_3_9, RX_BW_5_2, RX_BW_6_3, RX_BW_7_8,
|
||||
RX_BW_10_4, RX_BW_12_5, RX_BW_15_6, RX_BW_20_8, RX_BW_25_0, RX_BW_31_3,
|
||||
RX_BW_41_7, RX_BW_50_0, RX_BW_62_5, RX_BW_83_3, RX_BW_100_0, RX_BW_125_0,
|
||||
RX_BW_166_7, RX_BW_200_0, RX_BW_250_0, RX_BW_250_0};
|
||||
static const int32_t RSSI_OFFSET_HF = 157;
|
||||
static const int32_t RSSI_OFFSET_LF = 164;
|
||||
|
||||
uint8_t SX127x::read_register_(uint8_t reg) {
|
||||
this->enable();
|
||||
this->write_byte(reg & 0x7F);
|
||||
uint8_t value = this->read_byte();
|
||||
this->disable();
|
||||
return value;
|
||||
}
|
||||
|
||||
void SX127x::write_register_(uint8_t reg, uint8_t value) {
|
||||
this->enable();
|
||||
this->write_byte(reg | 0x80);
|
||||
this->write_byte(value);
|
||||
this->disable();
|
||||
}
|
||||
|
||||
void SX127x::read_fifo_(std::vector<uint8_t> &packet) {
|
||||
this->enable();
|
||||
this->write_byte(REG_FIFO & 0x7F);
|
||||
this->read_array(packet.data(), packet.size());
|
||||
this->disable();
|
||||
}
|
||||
|
||||
void SX127x::write_fifo_(const std::vector<uint8_t> &packet) {
|
||||
this->enable();
|
||||
this->write_byte(REG_FIFO | 0x80);
|
||||
this->write_array(packet.data(), packet.size());
|
||||
this->disable();
|
||||
}
|
||||
|
||||
void SX127x::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Running setup");
|
||||
|
||||
// setup reset
|
||||
this->rst_pin_->setup();
|
||||
|
||||
// setup dio0
|
||||
if (this->dio0_pin_) {
|
||||
this->dio0_pin_->setup();
|
||||
}
|
||||
|
||||
// start spi
|
||||
this->spi_setup();
|
||||
|
||||
// configure rf
|
||||
this->configure();
|
||||
}
|
||||
|
||||
void SX127x::configure() {
|
||||
// toggle chip reset
|
||||
this->rst_pin_->digital_write(false);
|
||||
delayMicroseconds(1000);
|
||||
this->rst_pin_->digital_write(true);
|
||||
delayMicroseconds(10000);
|
||||
|
||||
// check silicon version to make sure hw is ok
|
||||
if (this->read_register_(REG_VERSION) != 0x12) {
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
|
||||
// enter sleep mode
|
||||
this->set_mode_(MOD_FSK, MODE_SLEEP);
|
||||
|
||||
// set freq
|
||||
uint64_t frf = ((uint64_t) this->frequency_ << 19) / FXOSC;
|
||||
this->write_register_(REG_FRF_MSB, (uint8_t) ((frf >> 16) & 0xFF));
|
||||
this->write_register_(REG_FRF_MID, (uint8_t) ((frf >> 8) & 0xFF));
|
||||
this->write_register_(REG_FRF_LSB, (uint8_t) ((frf >> 0) & 0xFF));
|
||||
|
||||
// enter standby mode
|
||||
this->set_mode_(MOD_FSK, MODE_STDBY);
|
||||
|
||||
// run image cal
|
||||
this->run_image_cal();
|
||||
|
||||
// go back to sleep
|
||||
this->set_mode_sleep();
|
||||
|
||||
// config pa
|
||||
if (this->pa_pin_ == PA_PIN_BOOST) {
|
||||
this->pa_power_ = std::max(this->pa_power_, (uint8_t) 2);
|
||||
this->pa_power_ = std::min(this->pa_power_, (uint8_t) 17);
|
||||
this->write_register_(REG_PA_CONFIG, (this->pa_power_ - 2) | this->pa_pin_ | PA_MAX_POWER);
|
||||
} else {
|
||||
this->pa_power_ = std::min(this->pa_power_, (uint8_t) 14);
|
||||
this->write_register_(REG_PA_CONFIG, (this->pa_power_ - 0) | this->pa_pin_ | PA_MAX_POWER);
|
||||
}
|
||||
if (this->modulation_ != MOD_LORA) {
|
||||
this->write_register_(REG_PA_RAMP, this->pa_ramp_ | this->shaping_);
|
||||
} else {
|
||||
this->write_register_(REG_PA_RAMP, this->pa_ramp_);
|
||||
}
|
||||
|
||||
// configure modem
|
||||
if (this->modulation_ != MOD_LORA) {
|
||||
this->configure_fsk_ook_();
|
||||
} else {
|
||||
this->configure_lora_();
|
||||
}
|
||||
|
||||
// switch to rx or sleep
|
||||
if (this->rx_start_) {
|
||||
this->set_mode_rx();
|
||||
} else {
|
||||
this->set_mode_sleep();
|
||||
}
|
||||
}
|
||||
|
||||
void SX127x::configure_fsk_ook_() {
|
||||
// set the channel bw
|
||||
this->write_register_(REG_RX_BW, BW_FSK_OOK[this->bandwidth_]);
|
||||
|
||||
// set fdev
|
||||
uint32_t fdev = std::min((this->deviation_ * 4096) / 250000, (uint32_t) 0x3FFF);
|
||||
this->write_register_(REG_FDEV_MSB, (uint8_t) ((fdev >> 8) & 0xFF));
|
||||
this->write_register_(REG_FDEV_LSB, (uint8_t) ((fdev >> 0) & 0xFF));
|
||||
|
||||
// set bitrate
|
||||
uint64_t bitrate = (FXOSC + this->bitrate_ / 2) / this->bitrate_; // round up
|
||||
this->write_register_(REG_BITRATE_MSB, (uint8_t) ((bitrate >> 8) & 0xFF));
|
||||
this->write_register_(REG_BITRATE_LSB, (uint8_t) ((bitrate >> 0) & 0xFF));
|
||||
|
||||
// configure rx and afc
|
||||
uint8_t trigger = (this->preamble_detect_ > 0) ? TRIGGER_PREAMBLE : TRIGGER_RSSI;
|
||||
this->write_register_(REG_AFC_FEI, AFC_AUTO_CLEAR_ON);
|
||||
if (this->modulation_ == MOD_FSK) {
|
||||
this->write_register_(REG_RX_CONFIG, AFC_AUTO_ON | AGC_AUTO_ON | trigger);
|
||||
} else {
|
||||
this->write_register_(REG_RX_CONFIG, AGC_AUTO_ON | trigger);
|
||||
}
|
||||
|
||||
// configure packet mode
|
||||
if (this->packet_mode_) {
|
||||
uint8_t crc_mode = (this->crc_enable_) ? CRC_ON : CRC_OFF;
|
||||
this->write_register_(REG_FIFO_THRESH, TX_START_FIFO_EMPTY);
|
||||
if (this->payload_length_ > 0) {
|
||||
this->write_register_(REG_PAYLOAD_LENGTH_LSB, this->payload_length_);
|
||||
this->write_register_(REG_PACKET_CONFIG_1, crc_mode | FIXED_LENGTH);
|
||||
} else {
|
||||
this->write_register_(REG_PAYLOAD_LENGTH_LSB, this->get_max_packet_size() - 1);
|
||||
this->write_register_(REG_PACKET_CONFIG_1, crc_mode | VARIABLE_LENGTH);
|
||||
}
|
||||
this->write_register_(REG_PACKET_CONFIG_2, PACKET_MODE);
|
||||
} else {
|
||||
this->write_register_(REG_PACKET_CONFIG_2, CONTINUOUS_MODE);
|
||||
}
|
||||
this->write_register_(REG_DIO_MAPPING1, DIO0_MAPPING_00);
|
||||
|
||||
// config bit synchronizer
|
||||
uint8_t polarity = (this->preamble_polarity_ == 0xAA) ? PREAMBLE_AA : PREAMBLE_55;
|
||||
if (!this->sync_value_.empty()) {
|
||||
uint8_t size = this->sync_value_.size() - 1;
|
||||
this->write_register_(REG_SYNC_CONFIG, AUTO_RESTART_PLL_LOCK | polarity | SYNC_ON | size);
|
||||
for (uint32_t i = 0; i < this->sync_value_.size(); i++) {
|
||||
this->write_register_(REG_SYNC_VALUE1 + i, this->sync_value_[i]);
|
||||
}
|
||||
} else {
|
||||
this->write_register_(REG_SYNC_CONFIG, AUTO_RESTART_PLL_LOCK | polarity);
|
||||
}
|
||||
|
||||
// config preamble detector
|
||||
if (this->preamble_detect_ > 0) {
|
||||
uint8_t size = (this->preamble_detect_ - 1) << PREAMBLE_DETECTOR_SIZE_SHIFT;
|
||||
uint8_t tol = this->preamble_errors_ << PREAMBLE_DETECTOR_TOL_SHIFT;
|
||||
this->write_register_(REG_PREAMBLE_DETECT, PREAMBLE_DETECTOR_ON | size | tol);
|
||||
} else {
|
||||
this->write_register_(REG_PREAMBLE_DETECT, PREAMBLE_DETECTOR_OFF);
|
||||
}
|
||||
this->write_register_(REG_PREAMBLE_SIZE_MSB, this->preamble_size_ >> 16);
|
||||
this->write_register_(REG_PREAMBLE_SIZE_LSB, this->preamble_size_ & 0xFF);
|
||||
|
||||
// config sync generation and setup ook threshold
|
||||
uint8_t bitsync = this->bitsync_ ? BIT_SYNC_ON : BIT_SYNC_OFF;
|
||||
this->write_register_(REG_OOK_PEAK, bitsync | OOK_THRESH_STEP_0_5 | OOK_THRESH_PEAK);
|
||||
this->write_register_(REG_OOK_AVG, OOK_AVG_RESERVED | OOK_THRESH_DEC_1_8);
|
||||
|
||||
// set rx floor
|
||||
this->write_register_(REG_OOK_FIX, 256 + int(this->rx_floor_ * 2.0));
|
||||
this->write_register_(REG_RSSI_THRESH, std::abs(int(this->rx_floor_ * 2.0)));
|
||||
}
|
||||
|
||||
void SX127x::configure_lora_() {
|
||||
// config modem
|
||||
uint8_t header_mode = this->payload_length_ > 0 ? IMPLICIT_HEADER : EXPLICIT_HEADER;
|
||||
uint8_t crc_mode = (this->crc_enable_) ? RX_PAYLOAD_CRC_ON : RX_PAYLOAD_CRC_OFF;
|
||||
uint8_t spreading_factor = this->spreading_factor_ << SPREADING_FACTOR_SHIFT;
|
||||
this->write_register_(REG_MODEM_CONFIG1, BW_LORA[this->bandwidth_] | this->coding_rate_ | header_mode);
|
||||
this->write_register_(REG_MODEM_CONFIG2, spreading_factor | crc_mode);
|
||||
|
||||
// config fifo and payload length
|
||||
this->write_register_(REG_FIFO_TX_BASE_ADDR, 0x00);
|
||||
this->write_register_(REG_FIFO_RX_BASE_ADDR, 0x00);
|
||||
this->write_register_(REG_PAYLOAD_LENGTH, std::max(this->payload_length_, (uint32_t) 1));
|
||||
|
||||
// config preamble
|
||||
if (this->preamble_size_ >= 6) {
|
||||
this->write_register_(REG_PREAMBLE_LEN_MSB, this->preamble_size_ >> 16);
|
||||
this->write_register_(REG_PREAMBLE_LEN_LSB, this->preamble_size_ & 0xFF);
|
||||
}
|
||||
|
||||
// optimize detection
|
||||
float duration = 1000.0f * std::pow(2, this->spreading_factor_) / BW_HZ[this->bandwidth_];
|
||||
if (duration > 16) {
|
||||
this->write_register_(REG_MODEM_CONFIG3, MODEM_AGC_AUTO_ON | LOW_DATA_RATE_OPTIMIZE_ON);
|
||||
} else {
|
||||
this->write_register_(REG_MODEM_CONFIG3, MODEM_AGC_AUTO_ON);
|
||||
}
|
||||
if (this->spreading_factor_ == 6) {
|
||||
this->write_register_(REG_DETECT_OPTIMIZE, 0xC5);
|
||||
this->write_register_(REG_DETECT_THRESHOLD, 0x0C);
|
||||
} else {
|
||||
this->write_register_(REG_DETECT_OPTIMIZE, 0xC3);
|
||||
this->write_register_(REG_DETECT_THRESHOLD, 0x0A);
|
||||
}
|
||||
|
||||
// config sync word
|
||||
if (!this->sync_value_.empty()) {
|
||||
this->write_register_(REG_SYNC_WORD, this->sync_value_[0]);
|
||||
}
|
||||
}
|
||||
|
||||
size_t SX127x::get_max_packet_size() {
|
||||
if (this->payload_length_ > 0) {
|
||||
return this->payload_length_;
|
||||
}
|
||||
if (this->modulation_ == MOD_LORA) {
|
||||
return 256;
|
||||
} else {
|
||||
return 64;
|
||||
}
|
||||
}
|
||||
|
||||
SX127xError SX127x::transmit_packet(const std::vector<uint8_t> &packet) {
|
||||
if (this->payload_length_ > 0 && this->payload_length_ != packet.size()) {
|
||||
ESP_LOGE(TAG, "Packet size does not match config");
|
||||
return SX127xError::INVALID_PARAMS;
|
||||
}
|
||||
if (packet.empty() || packet.size() > this->get_max_packet_size()) {
|
||||
ESP_LOGE(TAG, "Packet size out of range");
|
||||
return SX127xError::INVALID_PARAMS;
|
||||
}
|
||||
|
||||
SX127xError ret = SX127xError::NONE;
|
||||
if (this->modulation_ == MOD_LORA) {
|
||||
this->set_mode_standby();
|
||||
if (this->payload_length_ == 0) {
|
||||
this->write_register_(REG_PAYLOAD_LENGTH, packet.size());
|
||||
}
|
||||
this->write_register_(REG_IRQ_FLAGS, 0xFF);
|
||||
this->write_register_(REG_FIFO_ADDR_PTR, 0);
|
||||
this->write_fifo_(packet);
|
||||
this->set_mode_tx();
|
||||
} else {
|
||||
this->set_mode_standby();
|
||||
if (this->payload_length_ == 0) {
|
||||
this->write_register_(REG_FIFO, packet.size());
|
||||
}
|
||||
this->write_fifo_(packet);
|
||||
this->set_mode_tx();
|
||||
}
|
||||
|
||||
// wait until transmit completes, typically the delay will be less than 100 ms
|
||||
uint32_t start = millis();
|
||||
while (!this->dio0_pin_->digital_read()) {
|
||||
if (millis() - start > 4000) {
|
||||
ESP_LOGE(TAG, "Transmit packet failure");
|
||||
ret = SX127xError::TIMEOUT;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (this->rx_start_) {
|
||||
this->set_mode_rx();
|
||||
} else {
|
||||
this->set_mode_sleep();
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
|
||||
void SX127x::call_listeners_(const std::vector<uint8_t> &packet, float rssi, float snr) {
|
||||
for (auto &listener : this->listeners_) {
|
||||
listener->on_packet(packet, rssi, snr);
|
||||
}
|
||||
this->packet_trigger_->trigger(packet, rssi, snr);
|
||||
}
|
||||
|
||||
void SX127x::loop() {
|
||||
if (this->dio0_pin_ == nullptr || !this->dio0_pin_->digital_read()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->modulation_ == MOD_LORA) {
|
||||
uint8_t status = this->read_register_(REG_IRQ_FLAGS);
|
||||
this->write_register_(REG_IRQ_FLAGS, 0xFF);
|
||||
if ((status & PAYLOAD_CRC_ERROR) == 0) {
|
||||
uint8_t bytes = this->read_register_(REG_NB_RX_BYTES);
|
||||
uint8_t addr = this->read_register_(REG_FIFO_RX_CURR_ADDR);
|
||||
uint8_t rssi = this->read_register_(REG_PKT_RSSI_VALUE);
|
||||
int8_t snr = (int8_t) this->read_register_(REG_PKT_SNR_VALUE);
|
||||
this->packet_.resize(bytes);
|
||||
this->write_register_(REG_FIFO_ADDR_PTR, addr);
|
||||
this->read_fifo_(this->packet_);
|
||||
if (this->frequency_ > 700000000) {
|
||||
this->call_listeners_(this->packet_, (float) rssi - RSSI_OFFSET_HF, (float) snr / 4);
|
||||
} else {
|
||||
this->call_listeners_(this->packet_, (float) rssi - RSSI_OFFSET_LF, (float) snr / 4);
|
||||
}
|
||||
}
|
||||
} else if (this->packet_mode_) {
|
||||
uint8_t payload_length = this->payload_length_;
|
||||
if (payload_length == 0) {
|
||||
payload_length = this->read_register_(REG_FIFO);
|
||||
}
|
||||
this->packet_.resize(payload_length);
|
||||
this->read_fifo_(this->packet_);
|
||||
this->call_listeners_(this->packet_, 0.0f, 0.0f);
|
||||
}
|
||||
}
|
||||
|
||||
void SX127x::run_image_cal() {
|
||||
if (this->modulation_ == MOD_LORA) {
|
||||
this->set_mode_(MOD_FSK, MODE_SLEEP);
|
||||
this->set_mode_(MOD_FSK, MODE_STDBY);
|
||||
}
|
||||
if (this->auto_cal_) {
|
||||
this->write_register_(REG_IMAGE_CAL, IMAGE_CAL_START | AUTO_IMAGE_CAL_ON | TEMP_THRESHOLD_10C);
|
||||
} else {
|
||||
this->write_register_(REG_IMAGE_CAL, IMAGE_CAL_START);
|
||||
}
|
||||
uint32_t start = millis();
|
||||
while (this->read_register_(REG_IMAGE_CAL) & IMAGE_CAL_RUNNING) {
|
||||
if (millis() - start > 20) {
|
||||
ESP_LOGE(TAG, "Image cal failure");
|
||||
this->mark_failed();
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (this->modulation_ == MOD_LORA) {
|
||||
this->set_mode_(this->modulation_, MODE_SLEEP);
|
||||
this->set_mode_(this->modulation_, MODE_STDBY);
|
||||
}
|
||||
}
|
||||
|
||||
void SX127x::set_mode_(uint8_t modulation, uint8_t mode) {
|
||||
uint32_t start = millis();
|
||||
this->write_register_(REG_OP_MODE, modulation | mode);
|
||||
while (true) {
|
||||
uint8_t curr = this->read_register_(REG_OP_MODE) & MODE_MASK;
|
||||
if ((curr == mode) || (mode == MODE_RX && curr == MODE_RX_FS)) {
|
||||
if (mode == MODE_SLEEP) {
|
||||
this->write_register_(REG_OP_MODE, modulation | mode);
|
||||
}
|
||||
break;
|
||||
}
|
||||
if (millis() - start > 20) {
|
||||
ESP_LOGE(TAG, "Set mode failure");
|
||||
this->mark_failed();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void SX127x::set_mode_rx() {
|
||||
this->set_mode_(this->modulation_, MODE_RX);
|
||||
if (this->modulation_ == MOD_LORA) {
|
||||
this->write_register_(REG_IRQ_FLAGS_MASK, 0x00);
|
||||
this->write_register_(REG_DIO_MAPPING1, DIO0_MAPPING_00);
|
||||
}
|
||||
}
|
||||
|
||||
void SX127x::set_mode_tx() {
|
||||
this->set_mode_(this->modulation_, MODE_TX);
|
||||
if (this->modulation_ == MOD_LORA) {
|
||||
this->write_register_(REG_IRQ_FLAGS_MASK, 0x00);
|
||||
this->write_register_(REG_DIO_MAPPING1, DIO0_MAPPING_01);
|
||||
}
|
||||
}
|
||||
|
||||
void SX127x::set_mode_standby() { this->set_mode_(this->modulation_, MODE_STDBY); }
|
||||
|
||||
void SX127x::set_mode_sleep() { this->set_mode_(this->modulation_, MODE_SLEEP); }
|
||||
|
||||
void SX127x::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "SX127x:");
|
||||
LOG_PIN(" CS Pin: ", this->cs_);
|
||||
LOG_PIN(" RST Pin: ", this->rst_pin_);
|
||||
LOG_PIN(" DIO0 Pin: ", this->dio0_pin_);
|
||||
const char *pa_pin = "RFO";
|
||||
if (this->pa_pin_ == PA_PIN_BOOST) {
|
||||
pa_pin = "BOOST";
|
||||
}
|
||||
ESP_LOGCONFIG(TAG,
|
||||
" Auto Cal: %s\n"
|
||||
" Frequency: %" PRIu32 " Hz\n"
|
||||
" Bandwidth: %" PRIu32 " Hz\n"
|
||||
" PA Pin: %s\n"
|
||||
" PA Power: %" PRIu8 " dBm\n"
|
||||
" PA Ramp: %" PRIu16 " us",
|
||||
TRUEFALSE(this->auto_cal_), this->frequency_, BW_HZ[this->bandwidth_], pa_pin, this->pa_power_,
|
||||
RAMP[this->pa_ramp_]);
|
||||
if (this->modulation_ == MOD_FSK) {
|
||||
ESP_LOGCONFIG(TAG, " Deviation: %" PRIu32 " Hz", this->deviation_);
|
||||
}
|
||||
if (this->modulation_ == MOD_LORA) {
|
||||
const char *cr = "4/8";
|
||||
if (this->coding_rate_ == CODING_RATE_4_5) {
|
||||
cr = "4/5";
|
||||
} else if (this->coding_rate_ == CODING_RATE_4_6) {
|
||||
cr = "4/6";
|
||||
} else if (this->coding_rate_ == CODING_RATE_4_7) {
|
||||
cr = "4/7";
|
||||
}
|
||||
ESP_LOGCONFIG(TAG,
|
||||
" Modulation: LORA\n"
|
||||
" Preamble Size: %" PRIu16 "\n"
|
||||
" Spreading Factor: %" PRIu8 "\n"
|
||||
" Coding Rate: %s\n"
|
||||
" CRC Enable: %s",
|
||||
this->preamble_size_, this->spreading_factor_, cr, TRUEFALSE(this->crc_enable_));
|
||||
if (this->payload_length_ > 0) {
|
||||
ESP_LOGCONFIG(TAG, " Payload Length: %" PRIu32, this->payload_length_);
|
||||
}
|
||||
if (!this->sync_value_.empty()) {
|
||||
ESP_LOGCONFIG(TAG, " Sync Value: 0x%02x", this->sync_value_[0]);
|
||||
}
|
||||
} else {
|
||||
const char *shaping = "NONE";
|
||||
if (this->modulation_ == MOD_FSK) {
|
||||
if (this->shaping_ == GAUSSIAN_BT_0_3) {
|
||||
shaping = "GAUSSIAN_BT_0_3";
|
||||
} else if (this->shaping_ == GAUSSIAN_BT_0_5) {
|
||||
shaping = "GAUSSIAN_BT_0_5";
|
||||
} else if (this->shaping_ == GAUSSIAN_BT_1_0) {
|
||||
shaping = "GAUSSIAN_BT_1_0";
|
||||
}
|
||||
} else {
|
||||
if (this->shaping_ == CUTOFF_BR_X_2) {
|
||||
shaping = "CUTOFF_BR_X_2";
|
||||
} else if (this->shaping_ == CUTOFF_BR_X_1) {
|
||||
shaping = "CUTOFF_BR_X_1";
|
||||
}
|
||||
}
|
||||
ESP_LOGCONFIG(TAG,
|
||||
" Shaping: %s\n"
|
||||
" Modulation: %s\n"
|
||||
" Bitrate: %" PRIu32 "b/s\n"
|
||||
" Bitsync: %s\n"
|
||||
" Rx Start: %s\n"
|
||||
" Rx Floor: %.1f dBm\n"
|
||||
" Packet Mode: %s",
|
||||
shaping, this->modulation_ == MOD_FSK ? "FSK" : "OOK", this->bitrate_, TRUEFALSE(this->bitsync_),
|
||||
TRUEFALSE(this->rx_start_), this->rx_floor_, TRUEFALSE(this->packet_mode_));
|
||||
if (this->packet_mode_) {
|
||||
ESP_LOGCONFIG(TAG, " CRC Enable: %s", TRUEFALSE(this->crc_enable_));
|
||||
}
|
||||
if (this->payload_length_ > 0) {
|
||||
ESP_LOGCONFIG(TAG, " Payload Length: %" PRIu32, this->payload_length_);
|
||||
}
|
||||
if (!this->sync_value_.empty()) {
|
||||
ESP_LOGCONFIG(TAG, " Sync Value: 0x%s", format_hex(this->sync_value_).c_str());
|
||||
}
|
||||
if (this->preamble_size_ > 0 || this->preamble_detect_ > 0) {
|
||||
ESP_LOGCONFIG(TAG,
|
||||
" Preamble Polarity: 0x%X\n"
|
||||
" Preamble Size: %" PRIu16 "\n"
|
||||
" Preamble Detect: %" PRIu8 "\n"
|
||||
" Preamble Errors: %" PRIu8,
|
||||
this->preamble_polarity_, this->preamble_size_, this->preamble_detect_, this->preamble_errors_);
|
||||
}
|
||||
}
|
||||
if (this->is_failed()) {
|
||||
ESP_LOGE(TAG, "Configuring SX127x failed");
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace sx127x
|
||||
} // namespace esphome
|
128
esphome/components/sx127x/sx127x.h
Normal file
128
esphome/components/sx127x/sx127x.h
Normal file
@ -0,0 +1,128 @@
|
||||
#pragma once
|
||||
|
||||
#include "sx127x_reg.h"
|
||||
#include "esphome/components/spi/spi.h"
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include <vector>
|
||||
|
||||
namespace esphome {
|
||||
namespace sx127x {
|
||||
|
||||
enum SX127xBw : uint8_t {
|
||||
SX127X_BW_2_6,
|
||||
SX127X_BW_3_1,
|
||||
SX127X_BW_3_9,
|
||||
SX127X_BW_5_2,
|
||||
SX127X_BW_6_3,
|
||||
SX127X_BW_7_8,
|
||||
SX127X_BW_10_4,
|
||||
SX127X_BW_12_5,
|
||||
SX127X_BW_15_6,
|
||||
SX127X_BW_20_8,
|
||||
SX127X_BW_25_0,
|
||||
SX127X_BW_31_3,
|
||||
SX127X_BW_41_7,
|
||||
SX127X_BW_50_0,
|
||||
SX127X_BW_62_5,
|
||||
SX127X_BW_83_3,
|
||||
SX127X_BW_100_0,
|
||||
SX127X_BW_125_0,
|
||||
SX127X_BW_166_7,
|
||||
SX127X_BW_200_0,
|
||||
SX127X_BW_250_0,
|
||||
SX127X_BW_500_0,
|
||||
};
|
||||
|
||||
enum class SX127xError { NONE = 0, TIMEOUT, INVALID_PARAMS };
|
||||
|
||||
class SX127xListener {
|
||||
public:
|
||||
virtual void on_packet(const std::vector<uint8_t> &packet, float rssi, float snr) = 0;
|
||||
};
|
||||
|
||||
class SX127x : public Component,
|
||||
public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
|
||||
spi::DATA_RATE_8MHZ> {
|
||||
public:
|
||||
size_t get_max_packet_size();
|
||||
float get_setup_priority() const override { return setup_priority::PROCESSOR; }
|
||||
void setup() override;
|
||||
void loop() override;
|
||||
void dump_config() override;
|
||||
void set_auto_cal(bool auto_cal) { this->auto_cal_ = auto_cal; }
|
||||
void set_bandwidth(SX127xBw bandwidth) { this->bandwidth_ = bandwidth; }
|
||||
void set_bitrate(uint32_t bitrate) { this->bitrate_ = bitrate; }
|
||||
void set_bitsync(bool bitsync) { this->bitsync_ = bitsync; }
|
||||
void set_coding_rate(uint8_t coding_rate) { this->coding_rate_ = coding_rate; }
|
||||
void set_crc_enable(bool crc_enable) { this->crc_enable_ = crc_enable; }
|
||||
void set_deviation(uint32_t deviation) { this->deviation_ = deviation; }
|
||||
void set_dio0_pin(InternalGPIOPin *dio0_pin) { this->dio0_pin_ = dio0_pin; }
|
||||
void set_frequency(uint32_t frequency) { this->frequency_ = frequency; }
|
||||
void set_mode_rx();
|
||||
void set_mode_tx();
|
||||
void set_mode_standby();
|
||||
void set_mode_sleep();
|
||||
void set_modulation(uint8_t modulation) { this->modulation_ = modulation; }
|
||||
void set_pa_pin(uint8_t pin) { this->pa_pin_ = pin; }
|
||||
void set_pa_power(uint8_t power) { this->pa_power_ = power; }
|
||||
void set_pa_ramp(uint8_t ramp) { this->pa_ramp_ = ramp; }
|
||||
void set_packet_mode(bool packet_mode) { this->packet_mode_ = packet_mode; }
|
||||
void set_payload_length(uint8_t payload_length) { this->payload_length_ = payload_length; }
|
||||
void set_preamble_errors(uint8_t preamble_errors) { this->preamble_errors_ = preamble_errors; }
|
||||
void set_preamble_polarity(uint8_t preamble_polarity) { this->preamble_polarity_ = preamble_polarity; }
|
||||
void set_preamble_size(uint16_t preamble_size) { this->preamble_size_ = preamble_size; }
|
||||
void set_preamble_detect(uint8_t preamble_detect) { this->preamble_detect_ = preamble_detect; }
|
||||
void set_rst_pin(InternalGPIOPin *rst_pin) { this->rst_pin_ = rst_pin; }
|
||||
void set_rx_floor(float floor) { this->rx_floor_ = floor; }
|
||||
void set_rx_start(bool start) { this->rx_start_ = start; }
|
||||
void set_shaping(uint8_t shaping) { this->shaping_ = shaping; }
|
||||
void set_spreading_factor(uint8_t spreading_factor) { this->spreading_factor_ = spreading_factor; }
|
||||
void set_sync_value(const std::vector<uint8_t> &sync_value) { this->sync_value_ = sync_value; }
|
||||
void run_image_cal();
|
||||
void configure();
|
||||
SX127xError transmit_packet(const std::vector<uint8_t> &packet);
|
||||
void register_listener(SX127xListener *listener) { this->listeners_.push_back(listener); }
|
||||
Trigger<std::vector<uint8_t>, float, float> *get_packet_trigger() const { return this->packet_trigger_; };
|
||||
|
||||
protected:
|
||||
void configure_fsk_ook_();
|
||||
void configure_lora_();
|
||||
void set_mode_(uint8_t modulation, uint8_t mode);
|
||||
void write_fifo_(const std::vector<uint8_t> &packet);
|
||||
void read_fifo_(std::vector<uint8_t> &packet);
|
||||
void write_register_(uint8_t reg, uint8_t value);
|
||||
void call_listeners_(const std::vector<uint8_t> &packet, float rssi, float snr);
|
||||
uint8_t read_register_(uint8_t reg);
|
||||
Trigger<std::vector<uint8_t>, float, float> *packet_trigger_{new Trigger<std::vector<uint8_t>, float, float>()};
|
||||
std::vector<SX127xListener *> listeners_;
|
||||
std::vector<uint8_t> packet_;
|
||||
std::vector<uint8_t> sync_value_;
|
||||
InternalGPIOPin *dio0_pin_{nullptr};
|
||||
InternalGPIOPin *rst_pin_{nullptr};
|
||||
SX127xBw bandwidth_;
|
||||
uint32_t bitrate_;
|
||||
uint32_t deviation_;
|
||||
uint32_t frequency_;
|
||||
uint32_t payload_length_;
|
||||
uint16_t preamble_size_;
|
||||
uint8_t coding_rate_;
|
||||
uint8_t modulation_;
|
||||
uint8_t pa_pin_;
|
||||
uint8_t pa_power_;
|
||||
uint8_t pa_ramp_;
|
||||
uint8_t preamble_detect_;
|
||||
uint8_t preamble_errors_;
|
||||
uint8_t preamble_polarity_;
|
||||
uint8_t shaping_;
|
||||
uint8_t spreading_factor_;
|
||||
float rx_floor_;
|
||||
bool auto_cal_{false};
|
||||
bool bitsync_{false};
|
||||
bool crc_enable_{false};
|
||||
bool packet_mode_{false};
|
||||
bool rx_start_{false};
|
||||
};
|
||||
|
||||
} // namespace sx127x
|
||||
} // namespace esphome
|
295
esphome/components/sx127x/sx127x_reg.h
Normal file
295
esphome/components/sx127x/sx127x_reg.h
Normal file
@ -0,0 +1,295 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/hal.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace sx127x {
|
||||
|
||||
enum SX127xReg : uint8_t {
|
||||
// Common registers
|
||||
REG_FIFO = 0x00,
|
||||
REG_OP_MODE = 0x01,
|
||||
REG_BITRATE_MSB = 0x02,
|
||||
REG_BITRATE_LSB = 0x03,
|
||||
REG_FDEV_MSB = 0x04,
|
||||
REG_FDEV_LSB = 0x05,
|
||||
REG_FRF_MSB = 0x06,
|
||||
REG_FRF_MID = 0x07,
|
||||
REG_FRF_LSB = 0x08,
|
||||
REG_PA_CONFIG = 0x09,
|
||||
REG_PA_RAMP = 0x0A,
|
||||
REG_DIO_MAPPING1 = 0x40,
|
||||
REG_DIO_MAPPING2 = 0x41,
|
||||
REG_VERSION = 0x42,
|
||||
// FSK/OOK registers
|
||||
REG_RX_CONFIG = 0x0D,
|
||||
REG_RSSI_THRESH = 0x10,
|
||||
REG_RX_BW = 0x12,
|
||||
REG_OOK_PEAK = 0x14,
|
||||
REG_OOK_FIX = 0x15,
|
||||
REG_OOK_AVG = 0x16,
|
||||
REG_AFC_FEI = 0x1A,
|
||||
REG_PREAMBLE_DETECT = 0x1F,
|
||||
REG_PREAMBLE_SIZE_MSB = 0x25,
|
||||
REG_PREAMBLE_SIZE_LSB = 0x26,
|
||||
REG_SYNC_CONFIG = 0x27,
|
||||
REG_SYNC_VALUE1 = 0x28,
|
||||
REG_SYNC_VALUE2 = 0x29,
|
||||
REG_SYNC_VALUE3 = 0x2A,
|
||||
REG_SYNC_VALUE4 = 0x2B,
|
||||
REG_SYNC_VALUE5 = 0x2C,
|
||||
REG_SYNC_VALUE6 = 0x2D,
|
||||
REG_SYNC_VALUE7 = 0x2E,
|
||||
REG_SYNC_VALUE8 = 0x2F,
|
||||
REG_PACKET_CONFIG_1 = 0x30,
|
||||
REG_PACKET_CONFIG_2 = 0x31,
|
||||
REG_PAYLOAD_LENGTH_LSB = 0x32,
|
||||
REG_FIFO_THRESH = 0x35,
|
||||
REG_IMAGE_CAL = 0x3B,
|
||||
// LoRa registers
|
||||
REG_FIFO_ADDR_PTR = 0x0D,
|
||||
REG_FIFO_TX_BASE_ADDR = 0x0E,
|
||||
REG_FIFO_RX_BASE_ADDR = 0x0F,
|
||||
REG_FIFO_RX_CURR_ADDR = 0x10,
|
||||
REG_IRQ_FLAGS_MASK = 0x11,
|
||||
REG_IRQ_FLAGS = 0x12,
|
||||
REG_NB_RX_BYTES = 0x13,
|
||||
REG_MODEM_STAT = 0x18,
|
||||
REG_PKT_SNR_VALUE = 0x19,
|
||||
REG_PKT_RSSI_VALUE = 0x1A,
|
||||
REG_RSSI_VALUE = 0x1B,
|
||||
REG_HOP_CHANNEL = 0x1C,
|
||||
REG_MODEM_CONFIG1 = 0x1D,
|
||||
REG_MODEM_CONFIG2 = 0x1E,
|
||||
REG_SYMB_TIMEOUT_LSB = 0x1F,
|
||||
REG_PREAMBLE_LEN_MSB = 0x20,
|
||||
REG_PREAMBLE_LEN_LSB = 0x21,
|
||||
REG_PAYLOAD_LENGTH = 0x22,
|
||||
REG_HOP_PERIOD = 0x24,
|
||||
REG_FIFO_RX_BYTE_ADDR = 0x25,
|
||||
REG_MODEM_CONFIG3 = 0x26,
|
||||
REG_FEI_MSB = 0x28,
|
||||
REG_FEI_MIB = 0x29,
|
||||
REG_FEI_LSB = 0x2A,
|
||||
REG_DETECT_OPTIMIZE = 0x31,
|
||||
REG_INVERT_IQ = 0x33,
|
||||
REG_DETECT_THRESHOLD = 0x37,
|
||||
REG_SYNC_WORD = 0x39,
|
||||
};
|
||||
|
||||
enum SX127xOpMode : uint8_t {
|
||||
MOD_LORA = 0x80,
|
||||
ACCESS_FSK_REGS = 0x40,
|
||||
ACCESS_LORA_REGS = 0x00,
|
||||
MOD_OOK = 0x20,
|
||||
MOD_FSK = 0x00,
|
||||
ACCESS_LF_REGS = 0x08,
|
||||
ACCESS_HF_REGS = 0x00,
|
||||
MODE_CAD = 0x07,
|
||||
MODE_RX_SINGLE = 0x06,
|
||||
MODE_RX = 0x05,
|
||||
MODE_RX_FS = 0x04,
|
||||
MODE_TX = 0x03,
|
||||
MODE_TX_FS = 0x02,
|
||||
MODE_STDBY = 0x01,
|
||||
MODE_SLEEP = 0x00,
|
||||
MODE_MASK = 0x07,
|
||||
};
|
||||
|
||||
enum SX127xPaConfig : uint8_t {
|
||||
PA_PIN_BOOST = 0x80,
|
||||
PA_PIN_RFO = 0x00,
|
||||
PA_MAX_POWER = 0x70,
|
||||
};
|
||||
|
||||
enum SX127xPaRamp : uint8_t {
|
||||
CUTOFF_BR_X_2 = 0x40,
|
||||
CUTOFF_BR_X_1 = 0x20,
|
||||
GAUSSIAN_BT_0_3 = 0x60,
|
||||
GAUSSIAN_BT_0_5 = 0x40,
|
||||
GAUSSIAN_BT_1_0 = 0x20,
|
||||
SHAPING_NONE = 0x00,
|
||||
PA_RAMP_10 = 0x0F,
|
||||
PA_RAMP_12 = 0x0E,
|
||||
PA_RAMP_15 = 0x0D,
|
||||
PA_RAMP_20 = 0x0C,
|
||||
PA_RAMP_25 = 0x0B,
|
||||
PA_RAMP_31 = 0x0A,
|
||||
PA_RAMP_40 = 0x09,
|
||||
PA_RAMP_50 = 0x08,
|
||||
PA_RAMP_62 = 0x07,
|
||||
PA_RAMP_100 = 0x06,
|
||||
PA_RAMP_125 = 0x05,
|
||||
PA_RAMP_250 = 0x04,
|
||||
PA_RAMP_500 = 0x03,
|
||||
PA_RAMP_1000 = 0x02,
|
||||
PA_RAMP_2000 = 0x01,
|
||||
PA_RAMP_3400 = 0x00,
|
||||
};
|
||||
|
||||
enum SX127xDioMapping1 : uint8_t {
|
||||
DIO0_MAPPING_00 = 0x00,
|
||||
DIO0_MAPPING_01 = 0x40,
|
||||
DIO0_MAPPING_10 = 0x80,
|
||||
DIO0_MAPPING_11 = 0xC0,
|
||||
};
|
||||
|
||||
enum SX127xRxConfig : uint8_t {
|
||||
RESTART_ON_COLLISION = 0x80,
|
||||
RESTART_NO_LOCK = 0x40,
|
||||
RESTART_PLL_LOCK = 0x20,
|
||||
AFC_AUTO_ON = 0x10,
|
||||
AGC_AUTO_ON = 0x08,
|
||||
TRIGGER_NONE = 0x00,
|
||||
TRIGGER_RSSI = 0x01,
|
||||
TRIGGER_PREAMBLE = 0x06,
|
||||
TRIGGER_ALL = 0x07,
|
||||
};
|
||||
|
||||
enum SX127xRxBw : uint8_t {
|
||||
RX_BW_2_6 = 0x17,
|
||||
RX_BW_3_1 = 0x0F,
|
||||
RX_BW_3_9 = 0x07,
|
||||
RX_BW_5_2 = 0x16,
|
||||
RX_BW_6_3 = 0x0E,
|
||||
RX_BW_7_8 = 0x06,
|
||||
RX_BW_10_4 = 0x15,
|
||||
RX_BW_12_5 = 0x0D,
|
||||
RX_BW_15_6 = 0x05,
|
||||
RX_BW_20_8 = 0x14,
|
||||
RX_BW_25_0 = 0x0C,
|
||||
RX_BW_31_3 = 0x04,
|
||||
RX_BW_41_7 = 0x13,
|
||||
RX_BW_50_0 = 0x0B,
|
||||
RX_BW_62_5 = 0x03,
|
||||
RX_BW_83_3 = 0x12,
|
||||
RX_BW_100_0 = 0x0A,
|
||||
RX_BW_125_0 = 0x02,
|
||||
RX_BW_166_7 = 0x11,
|
||||
RX_BW_200_0 = 0x09,
|
||||
RX_BW_250_0 = 0x01,
|
||||
};
|
||||
|
||||
enum SX127xOokPeak : uint8_t {
|
||||
BIT_SYNC_ON = 0x20,
|
||||
BIT_SYNC_OFF = 0x00,
|
||||
OOK_THRESH_AVG = 0x10,
|
||||
OOK_THRESH_PEAK = 0x08,
|
||||
OOK_THRESH_FIXED = 0x00,
|
||||
OOK_THRESH_STEP_6_0 = 0x07,
|
||||
OOK_THRESH_STEP_5_0 = 0x06,
|
||||
OOK_THRESH_STEP_4_0 = 0x05,
|
||||
OOK_THRESH_STEP_3_0 = 0x04,
|
||||
OOK_THRESH_STEP_2_0 = 0x03,
|
||||
OOK_THRESH_STEP_1_5 = 0x02,
|
||||
OOK_THRESH_STEP_1_0 = 0x01,
|
||||
OOK_THRESH_STEP_0_5 = 0x00,
|
||||
};
|
||||
|
||||
enum SX127xOokAvg : uint8_t {
|
||||
OOK_THRESH_DEC_16 = 0xE0,
|
||||
OOK_THRESH_DEC_8 = 0xC0,
|
||||
OOK_THRESH_DEC_4 = 0xA0,
|
||||
OOK_THRESH_DEC_2 = 0x80,
|
||||
OOK_THRESH_DEC_1_8 = 0x60,
|
||||
OOK_THRESH_DEC_1_4 = 0x40,
|
||||
OOK_THRESH_DEC_1_2 = 0x20,
|
||||
OOK_THRESH_DEC_1 = 0x00,
|
||||
OOK_AVG_RESERVED = 0x10,
|
||||
};
|
||||
|
||||
enum SX127xAfcFei : uint8_t {
|
||||
AFC_AUTO_CLEAR_ON = 0x01,
|
||||
};
|
||||
|
||||
enum SX127xPreambleDetect : uint8_t {
|
||||
PREAMBLE_DETECTOR_ON = 0x80,
|
||||
PREAMBLE_DETECTOR_OFF = 0x00,
|
||||
PREAMBLE_DETECTOR_SIZE_SHIFT = 5,
|
||||
PREAMBLE_DETECTOR_TOL_SHIFT = 0,
|
||||
};
|
||||
|
||||
enum SX127xSyncConfig : uint8_t {
|
||||
AUTO_RESTART_PLL_LOCK = 0x80,
|
||||
AUTO_RESTART_NO_LOCK = 0x40,
|
||||
AUTO_RESTART_OFF = 0x00,
|
||||
PREAMBLE_55 = 0x20,
|
||||
PREAMBLE_AA = 0x00,
|
||||
SYNC_ON = 0x10,
|
||||
SYNC_OFF = 0x00,
|
||||
};
|
||||
|
||||
enum SX127xPacketConfig1 : uint8_t {
|
||||
VARIABLE_LENGTH = 0x80,
|
||||
FIXED_LENGTH = 0x00,
|
||||
CRC_ON = 0x10,
|
||||
CRC_OFF = 0x00,
|
||||
};
|
||||
|
||||
enum SX127xPacketConfig2 : uint8_t {
|
||||
CONTINUOUS_MODE = 0x00,
|
||||
PACKET_MODE = 0x40,
|
||||
};
|
||||
|
||||
enum SX127xFifoThresh : uint8_t {
|
||||
TX_START_FIFO_EMPTY = 0x80,
|
||||
TX_START_FIFO_LEVEL = 0x00,
|
||||
};
|
||||
|
||||
enum SX127xImageCal : uint8_t {
|
||||
AUTO_IMAGE_CAL_ON = 0x80,
|
||||
IMAGE_CAL_START = 0x40,
|
||||
IMAGE_CAL_RUNNING = 0x20,
|
||||
TEMP_CHANGE = 0x08,
|
||||
TEMP_THRESHOLD_20C = 0x06,
|
||||
TEMP_THRESHOLD_15C = 0x04,
|
||||
TEMP_THRESHOLD_10C = 0x02,
|
||||
TEMP_THRESHOLD_5C = 0x00,
|
||||
TEMP_MONITOR_OFF = 0x01,
|
||||
TEMP_MONITOR_ON = 0x00,
|
||||
};
|
||||
|
||||
enum SX127xIrqFlags : uint8_t {
|
||||
RX_TIMEOUT = 0x80,
|
||||
RX_DONE = 0x40,
|
||||
PAYLOAD_CRC_ERROR = 0x20,
|
||||
VALID_HEADER = 0x10,
|
||||
TX_DONE = 0x08,
|
||||
CAD_DONE = 0x04,
|
||||
FHSS_CHANGE_CHANNEL = 0x02,
|
||||
CAD_DETECTED = 0x01,
|
||||
};
|
||||
|
||||
enum SX127xModemCfg1 : uint8_t {
|
||||
BW_7_8 = 0x00,
|
||||
BW_10_4 = 0x10,
|
||||
BW_15_6 = 0x20,
|
||||
BW_20_8 = 0x30,
|
||||
BW_31_3 = 0x40,
|
||||
BW_41_7 = 0x50,
|
||||
BW_62_5 = 0x60,
|
||||
BW_125_0 = 0x70,
|
||||
BW_250_0 = 0x80,
|
||||
BW_500_0 = 0x90,
|
||||
CODING_RATE_4_5 = 0x02,
|
||||
CODING_RATE_4_6 = 0x04,
|
||||
CODING_RATE_4_7 = 0x06,
|
||||
CODING_RATE_4_8 = 0x08,
|
||||
IMPLICIT_HEADER = 0x01,
|
||||
EXPLICIT_HEADER = 0x00,
|
||||
};
|
||||
|
||||
enum SX127xModemCfg2 : uint8_t {
|
||||
SPREADING_FACTOR_SHIFT = 4,
|
||||
TX_CONTINOUS_MODE = 0x08,
|
||||
RX_PAYLOAD_CRC_ON = 0x04,
|
||||
RX_PAYLOAD_CRC_OFF = 0x00,
|
||||
};
|
||||
|
||||
enum SX127xModemCfg3 : uint8_t {
|
||||
LOW_DATA_RATE_OPTIMIZE_ON = 0x08,
|
||||
MODEM_AGC_AUTO_ON = 0x04,
|
||||
};
|
||||
|
||||
} // namespace sx127x
|
||||
} // namespace esphome
|
@ -268,7 +268,19 @@ def validate_tz(value: str) -> str:
|
||||
|
||||
TIME_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.Optional(CONF_TIMEZONE, default=detect_tz): validate_tz,
|
||||
cv.SplitDefault(
|
||||
CONF_TIMEZONE,
|
||||
esp8266=detect_tz,
|
||||
esp32=detect_tz,
|
||||
rp2040=detect_tz,
|
||||
bk72xx=detect_tz,
|
||||
rtl87xx=detect_tz,
|
||||
ln882x=detect_tz,
|
||||
host=detect_tz,
|
||||
): cv.All(
|
||||
cv.only_with_framework(["arduino", "esp-idf", "host"]),
|
||||
validate_tz,
|
||||
),
|
||||
cv.Optional(CONF_ON_TIME): automation.validate_automation(
|
||||
{
|
||||
cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(CronTrigger),
|
||||
@ -293,7 +305,9 @@ TIME_SCHEMA = cv.Schema(
|
||||
|
||||
|
||||
async def setup_time_core_(time_var, config):
|
||||
cg.add(time_var.set_timezone(config[CONF_TIMEZONE]))
|
||||
if timezone := config.get(CONF_TIMEZONE):
|
||||
cg.add(time_var.set_timezone(timezone))
|
||||
cg.add_define("USE_TIME_TIMEZONE")
|
||||
|
||||
for conf in config.get(CONF_ON_TIME, []):
|
||||
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID], time_var)
|
||||
|
@ -35,8 +35,10 @@ void RealTimeClock::synchronize_epoch_(uint32_t epoch) {
|
||||
ret = settimeofday(&timev, nullptr);
|
||||
}
|
||||
|
||||
#ifdef USE_TIME_TIMEZONE
|
||||
// Move timezone back to local timezone.
|
||||
this->apply_timezone_();
|
||||
#endif
|
||||
|
||||
if (ret != 0) {
|
||||
ESP_LOGW(TAG, "setimeofday() failed with code %d", ret);
|
||||
@ -49,10 +51,12 @@ void RealTimeClock::synchronize_epoch_(uint32_t epoch) {
|
||||
this->time_sync_callback_.call();
|
||||
}
|
||||
|
||||
#ifdef USE_TIME_TIMEZONE
|
||||
void RealTimeClock::apply_timezone_() {
|
||||
setenv("TZ", this->timezone_.c_str(), 1);
|
||||
tzset();
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace time
|
||||
} // namespace esphome
|
||||
|
@ -20,6 +20,7 @@ class RealTimeClock : public PollingComponent {
|
||||
public:
|
||||
explicit RealTimeClock();
|
||||
|
||||
#ifdef USE_TIME_TIMEZONE
|
||||
/// Set the time zone.
|
||||
void set_timezone(const std::string &tz) {
|
||||
this->timezone_ = tz;
|
||||
@ -28,6 +29,7 @@ class RealTimeClock : public PollingComponent {
|
||||
|
||||
/// Get the time zone currently in use.
|
||||
std::string get_timezone() { return this->timezone_; }
|
||||
#endif
|
||||
|
||||
/// Get the time in the currently defined timezone.
|
||||
ESPTime now() { return ESPTime::from_epoch_local(this->timestamp_now()); }
|
||||
@ -38,7 +40,7 @@ class RealTimeClock : public PollingComponent {
|
||||
/// Get the current time as the UTC epoch since January 1st 1970.
|
||||
time_t timestamp_now() { return ::time(nullptr); }
|
||||
|
||||
void add_on_time_sync_callback(std::function<void()> callback) {
|
||||
void add_on_time_sync_callback(std::function<void()> &&callback) {
|
||||
this->time_sync_callback_.add(std::move(callback));
|
||||
};
|
||||
|
||||
@ -46,8 +48,10 @@ class RealTimeClock : public PollingComponent {
|
||||
/// Report a unix epoch as current time.
|
||||
void synchronize_epoch_(uint32_t epoch);
|
||||
|
||||
#ifdef USE_TIME_TIMEZONE
|
||||
std::string timezone_{};
|
||||
void apply_timezone_();
|
||||
#endif
|
||||
|
||||
CallbackManager<void()> time_sync_callback_;
|
||||
};
|
||||
|
@ -232,7 +232,7 @@ void TSL2591Component::set_integration_time_and_gain(TSL2591IntegrationTime inte
|
||||
this->integration_time_ = integration_time;
|
||||
this->gain_ = gain;
|
||||
if (!this->write_byte(TSL2591_COMMAND_BIT | TSL2591_REGISTER_CONTROL,
|
||||
this->integration_time_ | this->gain_)) { // NOLINT
|
||||
static_cast<uint8_t>(this->integration_time_) | static_cast<uint8_t>(this->gain_))) {
|
||||
ESP_LOGE(TAG, "I2C write failed");
|
||||
}
|
||||
// The ADC values can be confused if gain or integration time are changed in the middle of a cycle.
|
||||
|
@ -1,5 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "esphome/core/automation.h"
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/entity_base.h"
|
||||
@ -38,12 +39,19 @@ class UpdateEntity : public EntityBase, public EntityBase_DeviceClass {
|
||||
const UpdateState &state = state_;
|
||||
|
||||
void add_on_state_callback(std::function<void()> &&callback) { this->state_callback_.add(std::move(callback)); }
|
||||
Trigger<const UpdateInfo &> *get_update_available_trigger() {
|
||||
if (!update_available_trigger_) {
|
||||
update_available_trigger_ = std::make_unique<Trigger<const UpdateInfo &>>();
|
||||
}
|
||||
return update_available_trigger_.get();
|
||||
}
|
||||
|
||||
protected:
|
||||
UpdateState state_{UPDATE_STATE_UNKNOWN};
|
||||
UpdateInfo update_info_;
|
||||
|
||||
CallbackManager<void()> state_callback_{};
|
||||
std::unique_ptr<Trigger<const UpdateInfo &>> update_available_trigger_{nullptr};
|
||||
};
|
||||
|
||||
} // namespace update
|
||||
|
@ -46,70 +46,58 @@ static const char *const HEADER_CORS_REQ_PNA = "Access-Control-Request-Private-N
|
||||
static const char *const HEADER_CORS_ALLOW_PNA = "Access-Control-Allow-Private-Network";
|
||||
#endif
|
||||
|
||||
UrlMatch match_url(const std::string &url, bool only_domain = false) {
|
||||
UrlMatch match;
|
||||
match.valid = false;
|
||||
match.domain = nullptr;
|
||||
match.id = nullptr;
|
||||
match.method = nullptr;
|
||||
match.domain_len = 0;
|
||||
match.id_len = 0;
|
||||
match.method_len = 0;
|
||||
|
||||
const char *url_ptr = url.c_str();
|
||||
size_t url_len = url.length();
|
||||
// Parse URL and return match info
|
||||
static UrlMatch match_url(const char *url_ptr, size_t url_len, bool only_domain) {
|
||||
UrlMatch match{};
|
||||
|
||||
// URL must start with '/'
|
||||
if (url_len < 2 || url_ptr[0] != '/')
|
||||
if (url_len < 2 || url_ptr[0] != '/') {
|
||||
return match;
|
||||
}
|
||||
|
||||
// Find domain
|
||||
size_t domain_start = 1;
|
||||
size_t domain_end = url.find('/', domain_start);
|
||||
// Skip leading '/'
|
||||
const char *start = url_ptr + 1;
|
||||
const char *end = url_ptr + url_len;
|
||||
|
||||
if (domain_end == std::string::npos) {
|
||||
// URL is just "/domain"
|
||||
match.domain = url_ptr + domain_start;
|
||||
match.domain_len = url_len - domain_start;
|
||||
match.valid = true;
|
||||
// Find domain (everything up to next '/' or end)
|
||||
const char *domain_end = (const char *) memchr(start, '/', end - start);
|
||||
if (!domain_end) {
|
||||
// No second slash found - original behavior returns invalid
|
||||
return match;
|
||||
}
|
||||
|
||||
// Set domain
|
||||
match.domain = url_ptr + domain_start;
|
||||
match.domain_len = domain_end - domain_start;
|
||||
match.domain = start;
|
||||
match.domain_len = domain_end - start;
|
||||
match.valid = true;
|
||||
|
||||
if (only_domain) {
|
||||
match.valid = true;
|
||||
return match;
|
||||
}
|
||||
|
||||
// Check if there's anything after domain
|
||||
if (url_len == domain_end + 1)
|
||||
return match;
|
||||
// Parse ID if present
|
||||
if (domain_end + 1 >= end) {
|
||||
return match; // Nothing after domain slash
|
||||
}
|
||||
|
||||
// Find ID
|
||||
size_t id_begin = domain_end + 1;
|
||||
size_t id_end = url.find('/', id_begin);
|
||||
const char *id_start = domain_end + 1;
|
||||
const char *id_end = (const char *) memchr(id_start, '/', end - id_start);
|
||||
|
||||
match.valid = true;
|
||||
|
||||
if (id_end == std::string::npos) {
|
||||
// URL is "/domain/id" with no method
|
||||
match.id = url_ptr + id_begin;
|
||||
match.id_len = url_len - id_begin;
|
||||
if (!id_end) {
|
||||
// No more slashes, entire remaining string is ID
|
||||
match.id = id_start;
|
||||
match.id_len = end - id_start;
|
||||
return match;
|
||||
}
|
||||
|
||||
// Set ID
|
||||
match.id = url_ptr + id_begin;
|
||||
match.id_len = id_end - id_begin;
|
||||
match.id = id_start;
|
||||
match.id_len = id_end - id_start;
|
||||
|
||||
// Set method if present
|
||||
size_t method_begin = id_end + 1;
|
||||
if (method_begin < url_len) {
|
||||
match.method = url_ptr + method_begin;
|
||||
match.method_len = url_len - method_begin;
|
||||
// Parse method if present
|
||||
if (id_end + 1 < end) {
|
||||
match.method = id_end + 1;
|
||||
match.method_len = end - (id_end + 1);
|
||||
}
|
||||
|
||||
return match;
|
||||
@ -137,7 +125,16 @@ void DeferredUpdateEventSource::process_deferred_queue_() {
|
||||
if (this->send(message.c_str(), "state") != DISCARDED) {
|
||||
// O(n) but memory efficiency is more important than speed here which is why std::vector was chosen
|
||||
deferred_queue_.erase(deferred_queue_.begin());
|
||||
this->consecutive_send_failures_ = 0; // Reset failure count on successful send
|
||||
} else {
|
||||
this->consecutive_send_failures_++;
|
||||
if (this->consecutive_send_failures_ >= MAX_CONSECUTIVE_SEND_FAILURES) {
|
||||
// Too many failures, connection is likely dead
|
||||
ESP_LOGW(TAG, "Closing stuck EventSource connection after %" PRIu16 " failed sends",
|
||||
this->consecutive_send_failures_);
|
||||
this->close();
|
||||
this->deferred_queue_.clear();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
@ -176,6 +173,8 @@ void DeferredUpdateEventSource::deferrable_send_state(void *source, const char *
|
||||
std::string message = message_generator(web_server_, source);
|
||||
if (this->send(message.c_str(), "state") == DISCARDED) {
|
||||
deq_push_back_with_dedup_(source, message_generator);
|
||||
} else {
|
||||
this->consecutive_send_failures_ = 0; // Reset failure count on successful send
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -256,11 +255,7 @@ void DeferredUpdateEventSourceList::on_client_disconnect_(DeferredUpdateEventSou
|
||||
}
|
||||
#endif
|
||||
|
||||
WebServer::WebServer(web_server_base::WebServerBase *base) : base_(base) {
|
||||
#ifdef USE_ESP32
|
||||
to_schedule_lock_ = xSemaphoreCreateMutex();
|
||||
#endif
|
||||
}
|
||||
WebServer::WebServer(web_server_base::WebServerBase *base) : base_(base) {}
|
||||
|
||||
#ifdef USE_WEBSERVER_CSS_INCLUDE
|
||||
void WebServer::set_css_include(const char *css_include) { this->css_include_ = css_include; }
|
||||
@ -309,26 +304,7 @@ void WebServer::setup() {
|
||||
// getting a lot of events
|
||||
this->set_interval(10000, [this]() { this->events_.try_send_nodefer("", "ping", millis(), 30000); });
|
||||
}
|
||||
void WebServer::loop() {
|
||||
#ifdef USE_ESP32
|
||||
if (xSemaphoreTake(this->to_schedule_lock_, 0L)) {
|
||||
std::function<void()> fn;
|
||||
if (!to_schedule_.empty()) {
|
||||
// scheduler execute things out of order which may lead to incorrect state
|
||||
// this->defer(std::move(to_schedule_.front()));
|
||||
// let's execute it directly from the loop
|
||||
fn = std::move(to_schedule_.front());
|
||||
to_schedule_.pop_front();
|
||||
}
|
||||
xSemaphoreGive(this->to_schedule_lock_);
|
||||
if (fn) {
|
||||
fn();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
this->events_.loop();
|
||||
}
|
||||
void WebServer::loop() { this->events_.loop(); }
|
||||
void WebServer::dump_config() {
|
||||
ESP_LOGCONFIG(TAG,
|
||||
"Web Server:\n"
|
||||
@ -523,13 +499,13 @@ void WebServer::handle_switch_request(AsyncWebServerRequest *request, const UrlM
|
||||
std::string data = this->switch_json(obj, obj->state, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
} else if (match.method_equals("toggle")) {
|
||||
this->schedule_([obj]() { obj->toggle(); });
|
||||
this->defer([obj]() { obj->toggle(); });
|
||||
request->send(200);
|
||||
} else if (match.method_equals("turn_on")) {
|
||||
this->schedule_([obj]() { obj->turn_on(); });
|
||||
this->defer([obj]() { obj->turn_on(); });
|
||||
request->send(200);
|
||||
} else if (match.method_equals("turn_off")) {
|
||||
this->schedule_([obj]() { obj->turn_off(); });
|
||||
this->defer([obj]() { obj->turn_off(); });
|
||||
request->send(200);
|
||||
} else {
|
||||
request->send(404);
|
||||
@ -565,7 +541,7 @@ void WebServer::handle_button_request(AsyncWebServerRequest *request, const UrlM
|
||||
std::string data = this->button_json(obj, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
} else if (match.method_equals("press")) {
|
||||
this->schedule_([obj]() { obj->press(); });
|
||||
this->defer([obj]() { obj->press(); });
|
||||
request->send(200);
|
||||
return;
|
||||
} else {
|
||||
@ -645,7 +621,7 @@ void WebServer::handle_fan_request(AsyncWebServerRequest *request, const UrlMatc
|
||||
std::string data = this->fan_json(obj, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
} else if (match.method_equals("toggle")) {
|
||||
this->schedule_([obj]() { obj->toggle().perform(); });
|
||||
this->defer([obj]() { obj->toggle().perform(); });
|
||||
request->send(200);
|
||||
} else if (match.method_equals("turn_on") || match.method_equals("turn_off")) {
|
||||
auto call = match.method_equals("turn_on") ? obj->turn_on() : obj->turn_off();
|
||||
@ -677,7 +653,7 @@ void WebServer::handle_fan_request(AsyncWebServerRequest *request, const UrlMatc
|
||||
return;
|
||||
}
|
||||
}
|
||||
this->schedule_([call]() mutable { call.perform(); });
|
||||
this->defer([call]() mutable { call.perform(); });
|
||||
request->send(200);
|
||||
} else {
|
||||
request->send(404);
|
||||
@ -726,7 +702,7 @@ void WebServer::handle_light_request(AsyncWebServerRequest *request, const UrlMa
|
||||
std::string data = this->light_json(obj, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
} else if (match.method_equals("toggle")) {
|
||||
this->schedule_([obj]() { obj->toggle().perform(); });
|
||||
this->defer([obj]() { obj->toggle().perform(); });
|
||||
request->send(200);
|
||||
} else if (match.method_equals("turn_on")) {
|
||||
auto call = obj->turn_on();
|
||||
@ -783,7 +759,7 @@ void WebServer::handle_light_request(AsyncWebServerRequest *request, const UrlMa
|
||||
call.set_effect(effect);
|
||||
}
|
||||
|
||||
this->schedule_([call]() mutable { call.perform(); });
|
||||
this->defer([call]() mutable { call.perform(); });
|
||||
request->send(200);
|
||||
} else if (match.method_equals("turn_off")) {
|
||||
auto call = obj->turn_off();
|
||||
@ -793,7 +769,7 @@ void WebServer::handle_light_request(AsyncWebServerRequest *request, const UrlMa
|
||||
call.set_transition_length(*transition * 1000);
|
||||
}
|
||||
}
|
||||
this->schedule_([call]() mutable { call.perform(); });
|
||||
this->defer([call]() mutable { call.perform(); });
|
||||
request->send(200);
|
||||
} else {
|
||||
request->send(404);
|
||||
@ -878,7 +854,7 @@ void WebServer::handle_cover_request(AsyncWebServerRequest *request, const UrlMa
|
||||
}
|
||||
}
|
||||
|
||||
this->schedule_([call]() mutable { call.perform(); });
|
||||
this->defer([call]() mutable { call.perform(); });
|
||||
request->send(200);
|
||||
return;
|
||||
}
|
||||
@ -936,7 +912,7 @@ void WebServer::handle_number_request(AsyncWebServerRequest *request, const UrlM
|
||||
call.set_value(*value);
|
||||
}
|
||||
|
||||
this->schedule_([call]() mutable { call.perform(); });
|
||||
this->defer([call]() mutable { call.perform(); });
|
||||
request->send(200);
|
||||
return;
|
||||
}
|
||||
@ -1011,7 +987,7 @@ void WebServer::handle_date_request(AsyncWebServerRequest *request, const UrlMat
|
||||
call.set_date(value);
|
||||
}
|
||||
|
||||
this->schedule_([call]() mutable { call.perform(); });
|
||||
this->defer([call]() mutable { call.perform(); });
|
||||
request->send(200);
|
||||
return;
|
||||
}
|
||||
@ -1070,7 +1046,7 @@ void WebServer::handle_time_request(AsyncWebServerRequest *request, const UrlMat
|
||||
call.set_time(value);
|
||||
}
|
||||
|
||||
this->schedule_([call]() mutable { call.perform(); });
|
||||
this->defer([call]() mutable { call.perform(); });
|
||||
request->send(200);
|
||||
return;
|
||||
}
|
||||
@ -1128,7 +1104,7 @@ void WebServer::handle_datetime_request(AsyncWebServerRequest *request, const Ur
|
||||
call.set_datetime(value);
|
||||
}
|
||||
|
||||
this->schedule_([call]() mutable { call.perform(); });
|
||||
this->defer([call]() mutable { call.perform(); });
|
||||
request->send(200);
|
||||
return;
|
||||
}
|
||||
@ -1245,7 +1221,7 @@ void WebServer::handle_select_request(AsyncWebServerRequest *request, const UrlM
|
||||
call.set_option(option.c_str()); // NOLINT
|
||||
}
|
||||
|
||||
this->schedule_([call]() mutable { call.perform(); });
|
||||
this->defer([call]() mutable { call.perform(); });
|
||||
request->send(200);
|
||||
return;
|
||||
}
|
||||
@ -1332,7 +1308,7 @@ void WebServer::handle_climate_request(AsyncWebServerRequest *request, const Url
|
||||
call.set_target_temperature(*target_temperature);
|
||||
}
|
||||
|
||||
this->schedule_([call]() mutable { call.perform(); });
|
||||
this->defer([call]() mutable { call.perform(); });
|
||||
request->send(200);
|
||||
return;
|
||||
}
|
||||
@ -1449,13 +1425,13 @@ void WebServer::handle_lock_request(AsyncWebServerRequest *request, const UrlMat
|
||||
std::string data = this->lock_json(obj, obj->state, detail);
|
||||
request->send(200, "application/json", data.c_str());
|
||||
} else if (match.method_equals("lock")) {
|
||||
this->schedule_([obj]() { obj->lock(); });
|
||||
this->defer([obj]() { obj->lock(); });
|
||||
request->send(200);
|
||||
} else if (match.method_equals("unlock")) {
|
||||
this->schedule_([obj]() { obj->unlock(); });
|
||||
this->defer([obj]() { obj->unlock(); });
|
||||
request->send(200);
|
||||
} else if (match.method_equals("open")) {
|
||||
this->schedule_([obj]() { obj->open(); });
|
||||
this->defer([obj]() { obj->open(); });
|
||||
request->send(200);
|
||||
} else {
|
||||
request->send(404);
|
||||
@ -1526,7 +1502,7 @@ void WebServer::handle_valve_request(AsyncWebServerRequest *request, const UrlMa
|
||||
}
|
||||
}
|
||||
|
||||
this->schedule_([call]() mutable { call.perform(); });
|
||||
this->defer([call]() mutable { call.perform(); });
|
||||
request->send(200);
|
||||
return;
|
||||
}
|
||||
@ -1591,7 +1567,7 @@ void WebServer::handle_alarm_control_panel_request(AsyncWebServerRequest *reques
|
||||
return;
|
||||
}
|
||||
|
||||
this->schedule_([call]() mutable { call.perform(); });
|
||||
this->defer([call]() mutable { call.perform(); });
|
||||
request->send(200);
|
||||
return;
|
||||
}
|
||||
@ -1692,7 +1668,7 @@ void WebServer::handle_update_request(AsyncWebServerRequest *request, const UrlM
|
||||
return;
|
||||
}
|
||||
|
||||
this->schedule_([obj]() mutable { obj->perform(); });
|
||||
this->defer([obj]() mutable { obj->perform(); });
|
||||
request->send(200);
|
||||
return;
|
||||
}
|
||||
@ -1759,7 +1735,11 @@ bool WebServer::canHandle(AsyncWebServerRequest *request) const {
|
||||
}
|
||||
#endif
|
||||
|
||||
UrlMatch match = match_url(request->url().c_str(), true); // NOLINT
|
||||
// Store the URL to prevent temporary string destruction
|
||||
// request->url() returns a reference to a String (on Arduino) or std::string (on ESP-IDF)
|
||||
// UrlMatch stores pointers to the string's data, so we must ensure the string outlives match_url()
|
||||
const auto &url = request->url();
|
||||
UrlMatch match = match_url(url.c_str(), url.length(), true);
|
||||
if (!match.valid)
|
||||
return false;
|
||||
#ifdef USE_SENSOR
|
||||
@ -1898,7 +1878,10 @@ void WebServer::handleRequest(AsyncWebServerRequest *request) {
|
||||
}
|
||||
#endif
|
||||
|
||||
UrlMatch match = match_url(request->url().c_str()); // NOLINT
|
||||
// See comment in canHandle() for why we store the URL reference
|
||||
const auto &url = request->url();
|
||||
UrlMatch match = match_url(url.c_str(), url.length(), false);
|
||||
|
||||
#ifdef USE_SENSOR
|
||||
if (match.domain_equals("sensor")) {
|
||||
this->handle_sensor_request(request, match);
|
||||
@ -2062,16 +2045,6 @@ void WebServer::add_sorting_group(uint64_t group_id, const std::string &group_na
|
||||
}
|
||||
#endif
|
||||
|
||||
void WebServer::schedule_(std::function<void()> &&f) {
|
||||
#ifdef USE_ESP32
|
||||
xSemaphoreTake(this->to_schedule_lock_, portMAX_DELAY);
|
||||
to_schedule_.push_back(std::move(f));
|
||||
xSemaphoreGive(this->to_schedule_lock_);
|
||||
#else
|
||||
this->defer(std::move(f));
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace web_server
|
||||
} // namespace esphome
|
||||
#endif
|
||||
|
@ -14,11 +14,6 @@
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#ifdef USE_ESP32
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/semphr.h>
|
||||
#include <deque>
|
||||
#endif
|
||||
|
||||
#if USE_WEBSERVER_VERSION >= 2
|
||||
extern const uint8_t ESPHOME_WEBSERVER_INDEX_HTML[] PROGMEM;
|
||||
@ -126,6 +121,8 @@ class DeferredUpdateEventSource : public AsyncEventSource {
|
||||
// footprint is more important than speed here)
|
||||
std::vector<DeferredEvent> deferred_queue_;
|
||||
WebServer *web_server_;
|
||||
uint16_t consecutive_send_failures_{0};
|
||||
static constexpr uint16_t MAX_CONSECUTIVE_SEND_FAILURES = 2500; // ~20 seconds at 125Hz loop rate
|
||||
|
||||
// helper for allowing only unique entries in the queue
|
||||
void deq_push_back_with_dedup_(void *source, message_generator_t *message_generator);
|
||||
@ -501,7 +498,6 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
|
||||
|
||||
protected:
|
||||
void add_sorting_info_(JsonObject &root, EntityBase *entity);
|
||||
void schedule_(std::function<void()> &&f);
|
||||
web_server_base::WebServerBase *base_;
|
||||
#ifdef USE_ARDUINO
|
||||
DeferredUpdateEventSourceList events_;
|
||||
@ -521,10 +517,6 @@ class WebServer : public Controller, public Component, public AsyncWebHandler {
|
||||
const char *js_include_{nullptr};
|
||||
#endif
|
||||
bool expose_log_{true};
|
||||
#ifdef USE_ESP32
|
||||
std::deque<std::function<void()>> to_schedule_;
|
||||
SemaphoreHandle_t to_schedule_lock_;
|
||||
#endif
|
||||
};
|
||||
|
||||
} // namespace web_server
|
||||
|
@ -37,6 +37,15 @@ namespace web_server_idf {
|
||||
|
||||
static const char *const TAG = "web_server_idf";
|
||||
|
||||
// Global instance to avoid guard variable (saves 8 bytes)
|
||||
// This is initialized at program startup before any threads
|
||||
namespace {
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
DefaultHeaders default_headers_instance;
|
||||
} // namespace
|
||||
|
||||
DefaultHeaders &DefaultHeaders::Instance() { return default_headers_instance; }
|
||||
|
||||
void AsyncWebServer::end() {
|
||||
if (this->server_) {
|
||||
httpd_stop(this->server_);
|
||||
|
@ -328,10 +328,7 @@ class DefaultHeaders {
|
||||
void addHeader(const char *name, const char *value) { this->headers_.emplace_back(name, value); }
|
||||
|
||||
// NOLINTNEXTLINE(readability-identifier-naming)
|
||||
static DefaultHeaders &Instance() {
|
||||
static DefaultHeaders instance;
|
||||
return instance;
|
||||
}
|
||||
static DefaultHeaders &Instance();
|
||||
|
||||
protected:
|
||||
std::vector<std::pair<std::string, std::string>> headers_;
|
||||
|
@ -2,10 +2,8 @@
|
||||
|
||||
namespace esphome {
|
||||
|
||||
const Color Color::BLACK(0, 0, 0, 0);
|
||||
const Color Color::WHITE(255, 255, 255, 255);
|
||||
|
||||
const Color COLOR_BLACK(0, 0, 0, 0);
|
||||
const Color COLOR_WHITE(255, 255, 255, 255);
|
||||
// C++20 constinit ensures compile-time initialization (stored in ROM)
|
||||
constinit const Color Color::BLACK(0, 0, 0, 0);
|
||||
constinit const Color Color::WHITE(255, 255, 255, 255);
|
||||
|
||||
} // namespace esphome
|
||||
|
@ -5,7 +5,9 @@
|
||||
|
||||
namespace esphome {
|
||||
|
||||
inline static uint8_t esp_scale8(uint8_t i, uint8_t scale) { return (uint16_t(i) * (1 + uint16_t(scale))) / 256; }
|
||||
inline static constexpr uint8_t esp_scale8(uint8_t i, uint8_t scale) {
|
||||
return (uint16_t(i) * (1 + uint16_t(scale))) / 256;
|
||||
}
|
||||
|
||||
struct Color {
|
||||
union {
|
||||
@ -31,17 +33,20 @@ struct Color {
|
||||
uint32_t raw_32;
|
||||
};
|
||||
|
||||
inline Color() ESPHOME_ALWAYS_INLINE : r(0), g(0), b(0), w(0) {} // NOLINT
|
||||
inline Color(uint8_t red, uint8_t green, uint8_t blue) ESPHOME_ALWAYS_INLINE : r(red), g(green), b(blue), w(0) {}
|
||||
inline constexpr Color() ESPHOME_ALWAYS_INLINE : raw_32(0) {} // NOLINT
|
||||
inline constexpr Color(uint8_t red, uint8_t green, uint8_t blue) ESPHOME_ALWAYS_INLINE : r(red),
|
||||
g(green),
|
||||
b(blue),
|
||||
w(0) {}
|
||||
|
||||
inline Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ESPHOME_ALWAYS_INLINE : r(red),
|
||||
g(green),
|
||||
b(blue),
|
||||
w(white) {}
|
||||
inline explicit Color(uint32_t colorcode) ESPHOME_ALWAYS_INLINE : r((colorcode >> 16) & 0xFF),
|
||||
g((colorcode >> 8) & 0xFF),
|
||||
b((colorcode >> 0) & 0xFF),
|
||||
w((colorcode >> 24) & 0xFF) {}
|
||||
inline constexpr Color(uint8_t red, uint8_t green, uint8_t blue, uint8_t white) ESPHOME_ALWAYS_INLINE : r(red),
|
||||
g(green),
|
||||
b(blue),
|
||||
w(white) {}
|
||||
inline explicit constexpr Color(uint32_t colorcode) ESPHOME_ALWAYS_INLINE : r((colorcode >> 16) & 0xFF),
|
||||
g((colorcode >> 8) & 0xFF),
|
||||
b((colorcode >> 0) & 0xFF),
|
||||
w((colorcode >> 24) & 0xFF) {}
|
||||
|
||||
inline bool is_on() ESPHOME_ALWAYS_INLINE { return this->raw_32 != 0; }
|
||||
|
||||
@ -169,9 +174,4 @@ struct Color {
|
||||
static const Color WHITE;
|
||||
};
|
||||
|
||||
ESPDEPRECATED("Use Color::BLACK instead of COLOR_BLACK", "v1.21")
|
||||
extern const Color COLOR_BLACK;
|
||||
ESPDEPRECATED("Use Color::WHITE instead of COLOR_WHITE", "v1.21")
|
||||
extern const Color COLOR_WHITE;
|
||||
|
||||
} // namespace esphome
|
||||
|
@ -248,6 +248,9 @@ bool Component::cancel_defer(const std::string &name) { // NOLINT
|
||||
void Component::defer(const std::string &name, std::function<void()> &&f) { // NOLINT
|
||||
App.scheduler.set_timeout(this, name, 0, std::move(f));
|
||||
}
|
||||
void Component::defer(const char *name, std::function<void()> &&f) { // NOLINT
|
||||
App.scheduler.set_timeout(this, name, 0, std::move(f));
|
||||
}
|
||||
void Component::set_timeout(uint32_t timeout, std::function<void()> &&f) { // NOLINT
|
||||
App.scheduler.set_timeout(this, "", timeout, std::move(f));
|
||||
}
|
||||
|
@ -380,6 +380,21 @@ class Component {
|
||||
*/
|
||||
void defer(const std::string &name, std::function<void()> &&f); // NOLINT
|
||||
|
||||
/** Defer a callback to the next loop() call with a const char* name.
|
||||
*
|
||||
* IMPORTANT: The provided name pointer must remain valid for the lifetime of the deferred task.
|
||||
* This means the name should be:
|
||||
* - A string literal (e.g., "update")
|
||||
* - A static const char* variable
|
||||
* - A pointer with lifetime >= the deferred execution
|
||||
*
|
||||
* For dynamic strings, use the std::string overload instead.
|
||||
*
|
||||
* @param name The name of the defer function (must have static lifetime)
|
||||
* @param f The callback
|
||||
*/
|
||||
void defer(const char *name, std::function<void()> &&f); // NOLINT
|
||||
|
||||
/// Defer a callback to the next loop() call.
|
||||
void defer(std::function<void()> &&f); // NOLINT
|
||||
|
||||
|
@ -158,16 +158,16 @@ void ComponentIterator::advance() {
|
||||
}
|
||||
break;
|
||||
#endif
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
#ifdef USE_CAMERA
|
||||
case IteratorState::CAMERA:
|
||||
if (esp32_camera::global_esp32_camera == nullptr) {
|
||||
if (camera::Camera::instance() == nullptr) {
|
||||
advance_platform = true;
|
||||
} else {
|
||||
if (esp32_camera::global_esp32_camera->is_internal() && !this->include_internal_) {
|
||||
if (camera::Camera::instance()->is_internal() && !this->include_internal_) {
|
||||
advance_platform = success = true;
|
||||
break;
|
||||
} else {
|
||||
advance_platform = success = this->on_camera(esp32_camera::global_esp32_camera);
|
||||
advance_platform = success = this->on_camera(camera::Camera::instance());
|
||||
}
|
||||
}
|
||||
break;
|
||||
@ -386,8 +386,8 @@ bool ComponentIterator::on_begin() { return true; }
|
||||
#ifdef USE_API
|
||||
bool ComponentIterator::on_service(api::UserServiceDescriptor *service) { return true; }
|
||||
#endif
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
bool ComponentIterator::on_camera(esp32_camera::ESP32Camera *camera) { return true; }
|
||||
#ifdef USE_CAMERA
|
||||
bool ComponentIterator::on_camera(camera::Camera *camera) { return true; }
|
||||
#endif
|
||||
#ifdef USE_MEDIA_PLAYER
|
||||
bool ComponentIterator::on_media_player(media_player::MediaPlayer *media_player) { return true; }
|
||||
|
@ -4,8 +4,8 @@
|
||||
#include "esphome/core/controller.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
#include "esphome/components/esp32_camera/esp32_camera.h"
|
||||
#ifdef USE_CAMERA
|
||||
#include "esphome/components/camera/camera.h"
|
||||
#endif
|
||||
|
||||
namespace esphome {
|
||||
@ -48,8 +48,8 @@ class ComponentIterator {
|
||||
#ifdef USE_API
|
||||
virtual bool on_service(api::UserServiceDescriptor *service);
|
||||
#endif
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
virtual bool on_camera(esp32_camera::ESP32Camera *camera);
|
||||
#ifdef USE_CAMERA
|
||||
virtual bool on_camera(camera::Camera *camera);
|
||||
#endif
|
||||
#ifdef USE_CLIMATE
|
||||
virtual bool on_climate(climate::Climate *climate) = 0;
|
||||
@ -125,7 +125,7 @@ class ComponentIterator {
|
||||
#ifdef USE_API
|
||||
SERVICE,
|
||||
#endif
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
#ifdef USE_CAMERA
|
||||
CAMERA,
|
||||
#endif
|
||||
#ifdef USE_CLIMATE
|
||||
|
@ -23,6 +23,7 @@
|
||||
#define USE_AREAS
|
||||
#define USE_BINARY_SENSOR
|
||||
#define USE_BUTTON
|
||||
#define USE_CAMERA
|
||||
#define USE_CLIMATE
|
||||
#define USE_COVER
|
||||
#define USE_DATETIME
|
||||
@ -86,6 +87,7 @@
|
||||
#define USE_SELECT
|
||||
#define USE_SENSOR
|
||||
#define USE_STATUS_LED
|
||||
#define USE_STATUS_SENSOR
|
||||
#define USE_SWITCH
|
||||
#define USE_TEXT
|
||||
#define USE_TEXT_SENSOR
|
||||
@ -116,6 +118,7 @@
|
||||
#define USE_OTA_PASSWORD
|
||||
#define USE_OTA_STATE_CALLBACK
|
||||
#define USE_OTA_VERSION 2
|
||||
#define USE_TIME_TIMEZONE
|
||||
#define USE_WIFI
|
||||
#define USE_WIFI_AP
|
||||
#define USE_WIREGUARD
|
||||
@ -142,7 +145,6 @@
|
||||
#define USE_ESP32_BLE
|
||||
#define USE_ESP32_BLE_CLIENT
|
||||
#define USE_ESP32_BLE_SERVER
|
||||
#define USE_ESP32_CAMERA
|
||||
#define USE_I2C
|
||||
#define USE_IMPROV
|
||||
#define USE_MICROPHONE
|
||||
|
@ -645,7 +645,7 @@ void hsv_to_rgb(int hue, float saturation, float value, float &red, float &green
|
||||
}
|
||||
|
||||
// System APIs
|
||||
#if defined(USE_ESP8266) || defined(USE_RP2040) || defined(USE_HOST)
|
||||
#if defined(USE_ESP8266) || defined(USE_RP2040)
|
||||
// ESP8266 doesn't have mutexes, but that shouldn't be an issue as it's single-core and non-preemptive OS.
|
||||
Mutex::Mutex() {}
|
||||
Mutex::~Mutex() {}
|
||||
@ -658,6 +658,13 @@ Mutex::~Mutex() {}
|
||||
void Mutex::lock() { xSemaphoreTake(this->handle_, portMAX_DELAY); }
|
||||
bool Mutex::try_lock() { return xSemaphoreTake(this->handle_, 0) == pdTRUE; }
|
||||
void Mutex::unlock() { xSemaphoreGive(this->handle_); }
|
||||
#elif defined(USE_HOST)
|
||||
// Host platform uses std::mutex for proper thread synchronization
|
||||
Mutex::Mutex() { handle_ = new std::mutex(); }
|
||||
Mutex::~Mutex() { delete static_cast<std::mutex *>(handle_); }
|
||||
void Mutex::lock() { static_cast<std::mutex *>(handle_)->lock(); }
|
||||
bool Mutex::try_lock() { return static_cast<std::mutex *>(handle_)->try_lock(); }
|
||||
void Mutex::unlock() { static_cast<std::mutex *>(handle_)->unlock(); }
|
||||
#endif
|
||||
|
||||
#if defined(USE_ESP8266)
|
||||
|
@ -32,6 +32,10 @@
|
||||
#include <semphr.h>
|
||||
#endif
|
||||
|
||||
#ifdef USE_HOST
|
||||
#include <mutex>
|
||||
#endif
|
||||
|
||||
#define HOT __attribute__((hot))
|
||||
#define ESPDEPRECATED(msg, when) __attribute__((deprecated(msg)))
|
||||
#define ESPHOME_ALWAYS_INLINE __attribute__((always_inline))
|
||||
|
@ -31,11 +31,20 @@
|
||||
|
||||
namespace esphome {
|
||||
|
||||
// Base lock-free queue without task notification
|
||||
template<class T, uint8_t SIZE> class LockFreeQueue {
|
||||
public:
|
||||
LockFreeQueue() : head_(0), tail_(0), dropped_count_(0), task_to_notify_(nullptr) {}
|
||||
LockFreeQueue() : head_(0), tail_(0), dropped_count_(0) {}
|
||||
|
||||
bool push(T *element) {
|
||||
bool was_empty;
|
||||
uint8_t old_tail;
|
||||
return push_internal_(element, was_empty, old_tail);
|
||||
}
|
||||
|
||||
protected:
|
||||
// Internal push that reports queue state - for use by derived classes
|
||||
bool push_internal_(T *element, bool &was_empty, uint8_t &old_tail) {
|
||||
if (element == nullptr)
|
||||
return false;
|
||||
|
||||
@ -51,34 +60,16 @@ template<class T, uint8_t SIZE> class LockFreeQueue {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Check if queue was empty before push
|
||||
bool was_empty = (current_tail == head_before);
|
||||
was_empty = (current_tail == head_before);
|
||||
old_tail = current_tail;
|
||||
|
||||
buffer_[current_tail] = element;
|
||||
tail_.store(next_tail, std::memory_order_release);
|
||||
|
||||
// Notify optimization: only notify if we need to
|
||||
if (task_to_notify_ != nullptr) {
|
||||
if (was_empty) {
|
||||
// Queue was empty - consumer might be going to sleep, must notify
|
||||
xTaskNotifyGive(task_to_notify_);
|
||||
} else {
|
||||
// Queue wasn't empty - check if consumer has caught up to previous tail
|
||||
uint8_t head_after = head_.load(std::memory_order_acquire);
|
||||
if (head_after == current_tail) {
|
||||
// Consumer just caught up to where tail was - might go to sleep, must notify
|
||||
// Note: There's a benign race here - between reading head_after and calling
|
||||
// xTaskNotifyGive(), the consumer could advance further. This would result
|
||||
// in an unnecessary wake-up, but is harmless and extremely rare in practice.
|
||||
xTaskNotifyGive(task_to_notify_);
|
||||
}
|
||||
// Otherwise: consumer is still behind, no need to notify
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
public:
|
||||
T *pop() {
|
||||
uint8_t current_head = head_.load(std::memory_order_relaxed);
|
||||
|
||||
@ -108,11 +99,6 @@ template<class T, uint8_t SIZE> class LockFreeQueue {
|
||||
return next_tail == head_.load(std::memory_order_acquire);
|
||||
}
|
||||
|
||||
// Set the FreeRTOS task handle to notify when items are pushed to the queue
|
||||
// This enables efficient wake-up of a consumer task that's waiting for data
|
||||
// @param task The FreeRTOS task handle to notify, or nullptr to disable notifications
|
||||
void set_task_to_notify(TaskHandle_t task) { task_to_notify_ = task; }
|
||||
|
||||
protected:
|
||||
T *buffer_[SIZE];
|
||||
// Atomic: written by producer (push/increment), read+reset by consumer (get_and_reset)
|
||||
@ -123,7 +109,40 @@ template<class T, uint8_t SIZE> class LockFreeQueue {
|
||||
std::atomic<uint8_t> head_;
|
||||
// Atomic: written by producer (push), read by consumer (pop) to check if empty
|
||||
std::atomic<uint8_t> tail_;
|
||||
// Task handle for notification (optional)
|
||||
};
|
||||
|
||||
// Extended queue with task notification support
|
||||
template<class T, uint8_t SIZE> class NotifyingLockFreeQueue : public LockFreeQueue<T, SIZE> {
|
||||
public:
|
||||
NotifyingLockFreeQueue() : LockFreeQueue<T, SIZE>(), task_to_notify_(nullptr) {}
|
||||
|
||||
bool push(T *element) {
|
||||
bool was_empty;
|
||||
uint8_t old_tail;
|
||||
bool result = this->push_internal_(element, was_empty, old_tail);
|
||||
|
||||
// Notify optimization: only notify if we need to
|
||||
if (result && task_to_notify_ != nullptr &&
|
||||
(was_empty || this->head_.load(std::memory_order_acquire) == old_tail)) {
|
||||
// Notify in two cases:
|
||||
// 1. Queue was empty - consumer might be going to sleep
|
||||
// 2. Consumer just caught up to where tail was - might go to sleep
|
||||
// Note: There's a benign race in case 2 - between reading head and calling
|
||||
// xTaskNotifyGive(), the consumer could advance further. This would result
|
||||
// in an unnecessary wake-up, but is harmless and extremely rare in practice.
|
||||
xTaskNotifyGive(task_to_notify_);
|
||||
}
|
||||
// Otherwise: consumer is still behind, no need to notify
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
// Set the FreeRTOS task handle to notify when items are pushed to the queue
|
||||
// This enables efficient wake-up of a consumer task that's waiting for data
|
||||
// @param task The FreeRTOS task handle to notify, or nullptr to disable notifications
|
||||
void set_task_to_notify(TaskHandle_t task) { task_to_notify_ = task; }
|
||||
|
||||
private:
|
||||
TaskHandle_t task_to_notify_;
|
||||
};
|
||||
|
||||
|
@ -73,8 +73,6 @@ void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type
|
||||
if (delay == SCHEDULER_DONT_RUN)
|
||||
return;
|
||||
|
||||
const auto now = this->millis_();
|
||||
|
||||
// Create and populate the scheduler item
|
||||
auto item = make_unique<SchedulerItem>();
|
||||
item->component = component;
|
||||
@ -83,6 +81,19 @@ void HOT Scheduler::set_timer_common_(Component *component, SchedulerItem::Type
|
||||
item->callback = std::move(func);
|
||||
item->remove = false;
|
||||
|
||||
#if !defined(USE_ESP8266) && !defined(USE_RP2040)
|
||||
// Special handling for defer() (delay = 0, type = TIMEOUT)
|
||||
// ESP8266 and RP2040 are excluded because they don't need thread-safe defer handling
|
||||
if (delay == 0 && type == SchedulerItem::TIMEOUT) {
|
||||
// Put in defer queue for guaranteed FIFO execution
|
||||
LockGuard guard{this->lock_};
|
||||
this->defer_queue_.push_back(std::move(item));
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
const auto now = this->millis_();
|
||||
|
||||
// Type-specific setup
|
||||
if (type == SchedulerItem::INTERVAL) {
|
||||
item->interval = delay;
|
||||
@ -209,6 +220,35 @@ optional<uint32_t> HOT Scheduler::next_schedule_in() {
|
||||
return item->next_execution_ - now;
|
||||
}
|
||||
void HOT Scheduler::call() {
|
||||
#if !defined(USE_ESP8266) && !defined(USE_RP2040)
|
||||
// Process defer queue first to guarantee FIFO execution order for deferred items.
|
||||
// Previously, defer() used the heap which gave undefined order for equal timestamps,
|
||||
// causing race conditions on multi-core systems (ESP32, BK7200).
|
||||
// With the defer queue:
|
||||
// - Deferred items (delay=0) go directly to defer_queue_ in set_timer_common_
|
||||
// - Items execute in exact order they were deferred (FIFO guarantee)
|
||||
// - No deferred items exist in to_add_, so processing order doesn't affect correctness
|
||||
// ESP8266 and RP2040 don't use this queue - they fall back to the heap-based approach
|
||||
// (ESP8266: single-core, RP2040: empty mutex implementation).
|
||||
while (!this->defer_queue_.empty()) {
|
||||
// The outer check is done without a lock for performance. If the queue
|
||||
// appears non-empty, we lock and process an item. We don't need to check
|
||||
// empty() again inside the lock because only this thread can remove items.
|
||||
std::unique_ptr<SchedulerItem> item;
|
||||
{
|
||||
LockGuard lock(this->lock_);
|
||||
item = std::move(this->defer_queue_.front());
|
||||
this->defer_queue_.pop_front();
|
||||
}
|
||||
|
||||
// Execute callback without holding lock to prevent deadlocks
|
||||
// if the callback tries to call defer() again
|
||||
if (!this->should_skip_item_(item.get())) {
|
||||
this->execute_item_(item.get());
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
const auto now = this->millis_();
|
||||
this->process_to_add();
|
||||
|
||||
@ -282,8 +322,6 @@ void HOT Scheduler::call() {
|
||||
this->pop_raw_();
|
||||
continue;
|
||||
}
|
||||
App.set_current_component(item->component);
|
||||
|
||||
#ifdef ESPHOME_DEBUG_SCHEDULER
|
||||
const char *item_name = item->get_name();
|
||||
ESP_LOGV(TAG, "Running %s '%s/%s' with interval=%" PRIu32 " next_execution=%" PRIu64 " (now=%" PRIu64 ")",
|
||||
@ -294,13 +332,7 @@ void HOT Scheduler::call() {
|
||||
// Warning: During callback(), a lot of stuff can happen, including:
|
||||
// - timeouts/intervals get added, potentially invalidating vector pointers
|
||||
// - timeouts/intervals get cancelled
|
||||
{
|
||||
uint32_t now_ms = millis();
|
||||
WarnIfComponentBlockingGuard guard{item->component, now_ms};
|
||||
item->callback();
|
||||
// Call finish to ensure blocking time is properly calculated and reported
|
||||
guard.finish();
|
||||
}
|
||||
this->execute_item_(item.get());
|
||||
}
|
||||
|
||||
{
|
||||
@ -364,6 +396,26 @@ void HOT Scheduler::push_(std::unique_ptr<Scheduler::SchedulerItem> item) {
|
||||
LockGuard guard{this->lock_};
|
||||
this->to_add_.push_back(std::move(item));
|
||||
}
|
||||
// Helper function to check if item matches criteria for cancellation
|
||||
bool HOT Scheduler::matches_item_(const std::unique_ptr<SchedulerItem> &item, Component *component,
|
||||
const char *name_cstr, SchedulerItem::Type type) {
|
||||
if (item->component != component || item->type != type || item->remove) {
|
||||
return false;
|
||||
}
|
||||
const char *item_name = item->get_name();
|
||||
return item_name != nullptr && strcmp(name_cstr, item_name) == 0;
|
||||
}
|
||||
|
||||
// Helper to execute a scheduler item
|
||||
void HOT Scheduler::execute_item_(SchedulerItem *item) {
|
||||
App.set_current_component(item->component);
|
||||
|
||||
uint32_t now_ms = millis();
|
||||
WarnIfComponentBlockingGuard guard{item->component, now_ms};
|
||||
item->callback();
|
||||
guard.finish();
|
||||
}
|
||||
|
||||
// Common implementation for cancel operations
|
||||
bool HOT Scheduler::cancel_item_common_(Component *component, bool is_static_string, const void *name_ptr,
|
||||
SchedulerItem::Type type) {
|
||||
@ -379,19 +431,28 @@ bool HOT Scheduler::cancel_item_common_(Component *component, bool is_static_str
|
||||
LockGuard guard{this->lock_};
|
||||
bool ret = false;
|
||||
|
||||
for (auto &it : this->items_) {
|
||||
const char *item_name = it->get_name();
|
||||
if (it->component == component && item_name != nullptr && strcmp(name_cstr, item_name) == 0 && it->type == type &&
|
||||
!it->remove) {
|
||||
to_remove_++;
|
||||
it->remove = true;
|
||||
// Check all containers for matching items
|
||||
#if !defined(USE_ESP8266) && !defined(USE_RP2040)
|
||||
// Only check defer_queue_ on platforms that have it
|
||||
for (auto &item : this->defer_queue_) {
|
||||
if (this->matches_item_(item, component, name_cstr, type)) {
|
||||
item->remove = true;
|
||||
ret = true;
|
||||
}
|
||||
}
|
||||
for (auto &it : this->to_add_) {
|
||||
const char *item_name = it->get_name();
|
||||
if (it->component == component && item_name != nullptr && strcmp(name_cstr, item_name) == 0 && it->type == type) {
|
||||
it->remove = true;
|
||||
#endif
|
||||
|
||||
for (auto &item : this->items_) {
|
||||
if (this->matches_item_(item, component, name_cstr, type)) {
|
||||
item->remove = true;
|
||||
ret = true;
|
||||
this->to_remove_++; // Only track removals for heap items
|
||||
}
|
||||
}
|
||||
|
||||
for (auto &item : this->to_add_) {
|
||||
if (this->matches_item_(item, component, name_cstr, type)) {
|
||||
item->remove = true;
|
||||
ret = true;
|
||||
}
|
||||
}
|
||||
|
@ -2,6 +2,7 @@
|
||||
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
#include <deque>
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/core/helpers.h"
|
||||
@ -142,9 +143,22 @@ class Scheduler {
|
||||
// Common implementation for cancel operations
|
||||
bool cancel_item_common_(Component *component, bool is_static_string, const void *name_ptr, SchedulerItem::Type type);
|
||||
|
||||
private:
|
||||
bool cancel_item_(Component *component, const std::string &name, SchedulerItem::Type type);
|
||||
bool cancel_item_(Component *component, const char *name, SchedulerItem::Type type);
|
||||
|
||||
// Helper functions for cancel operations
|
||||
bool matches_item_(const std::unique_ptr<SchedulerItem> &item, Component *component, const char *name_cstr,
|
||||
SchedulerItem::Type type);
|
||||
|
||||
// Helper to execute a scheduler item
|
||||
void execute_item_(SchedulerItem *item);
|
||||
|
||||
// Helper to check if item should be skipped
|
||||
bool should_skip_item_(const SchedulerItem *item) const {
|
||||
return item->remove || (item->component != nullptr && item->component->is_failed());
|
||||
}
|
||||
|
||||
bool empty_() {
|
||||
this->cleanup_();
|
||||
return this->items_.empty();
|
||||
@ -153,6 +167,13 @@ class Scheduler {
|
||||
Mutex lock_;
|
||||
std::vector<std::unique_ptr<SchedulerItem>> items_;
|
||||
std::vector<std::unique_ptr<SchedulerItem>> to_add_;
|
||||
#if !defined(USE_ESP8266) && !defined(USE_RP2040)
|
||||
// ESP8266 and RP2040 don't need the defer queue because:
|
||||
// ESP8266: Single-core with no preemptive multitasking
|
||||
// RP2040: Currently has empty mutex implementation in ESPHome
|
||||
// Both platforms save 40 bytes of RAM by excluding this
|
||||
std::deque<std::unique_ptr<SchedulerItem>> defer_queue_; // FIFO queue for defer() calls
|
||||
#endif
|
||||
uint32_t last_millis_{0};
|
||||
uint16_t millis_major_{0};
|
||||
uint32_t to_remove_{0};
|
||||
|
@ -226,11 +226,11 @@ int32_t ESPTime::timezone_offset() {
|
||||
return offset;
|
||||
}
|
||||
|
||||
bool ESPTime::operator<(ESPTime other) { return this->timestamp < other.timestamp; }
|
||||
bool ESPTime::operator<=(ESPTime other) { return this->timestamp <= other.timestamp; }
|
||||
bool ESPTime::operator==(ESPTime other) { return this->timestamp == other.timestamp; }
|
||||
bool ESPTime::operator>=(ESPTime other) { return this->timestamp >= other.timestamp; }
|
||||
bool ESPTime::operator>(ESPTime other) { return this->timestamp > other.timestamp; }
|
||||
bool ESPTime::operator<(const ESPTime &other) const { return this->timestamp < other.timestamp; }
|
||||
bool ESPTime::operator<=(const ESPTime &other) const { return this->timestamp <= other.timestamp; }
|
||||
bool ESPTime::operator==(const ESPTime &other) const { return this->timestamp == other.timestamp; }
|
||||
bool ESPTime::operator>=(const ESPTime &other) const { return this->timestamp >= other.timestamp; }
|
||||
bool ESPTime::operator>(const ESPTime &other) const { return this->timestamp > other.timestamp; }
|
||||
|
||||
template<typename T> bool increment_time_value(T ¤t, uint16_t begin, uint16_t end) {
|
||||
current++;
|
||||
|
@ -109,10 +109,10 @@ struct ESPTime {
|
||||
void increment_second();
|
||||
/// Increment this clock instance by one day.
|
||||
void increment_day();
|
||||
bool operator<(ESPTime other);
|
||||
bool operator<=(ESPTime other);
|
||||
bool operator==(ESPTime other);
|
||||
bool operator>=(ESPTime other);
|
||||
bool operator>(ESPTime other);
|
||||
bool operator<(const ESPTime &other) const;
|
||||
bool operator<=(const ESPTime &other) const;
|
||||
bool operator==(const ESPTime &other) const;
|
||||
bool operator>=(const ESPTime &other) const;
|
||||
bool operator>(const ESPTime &other) const;
|
||||
};
|
||||
} // namespace esphome
|
||||
|
@ -107,6 +107,11 @@ def storage_should_clean(old: StorageJSON, new: StorageJSON) -> bool:
|
||||
return True
|
||||
if old.build_path != new.build_path:
|
||||
return True
|
||||
|
||||
return False
|
||||
|
||||
|
||||
def storage_should_update_cmake_cache(old: StorageJSON, new: StorageJSON) -> bool:
|
||||
if (
|
||||
old.loaded_integrations != new.loaded_integrations
|
||||
or old.loaded_platforms != new.loaded_platforms
|
||||
@ -126,10 +131,11 @@ def update_storage_json():
|
||||
return
|
||||
|
||||
if storage_should_clean(old, new):
|
||||
_LOGGER.info(
|
||||
"Core config, version or integrations changed, cleaning build files..."
|
||||
)
|
||||
_LOGGER.info("Core config, version changed, cleaning build files...")
|
||||
clean_build()
|
||||
elif storage_should_update_cmake_cache(old, new):
|
||||
_LOGGER.info("Integrations changed, cleaning cmake cache...")
|
||||
clean_cmake_cache()
|
||||
|
||||
new.save(path)
|
||||
|
||||
@ -353,6 +359,15 @@ def write_cpp(code_s):
|
||||
write_file_if_changed(path, full_file)
|
||||
|
||||
|
||||
def clean_cmake_cache():
|
||||
pioenvs = CORE.relative_pioenvs_path()
|
||||
if os.path.isdir(pioenvs):
|
||||
pioenvs_cmake_path = CORE.relative_pioenvs_path(CORE.name, "CMakeCache.txt")
|
||||
if os.path.isfile(pioenvs_cmake_path):
|
||||
_LOGGER.info("Deleting %s", pioenvs_cmake_path)
|
||||
os.remove(pioenvs_cmake_path)
|
||||
|
||||
|
||||
def clean_build():
|
||||
import shutil
|
||||
|
||||
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
x
Reference in New Issue
Block a user