mirror of
https://github.com/esphome/esphome.git
synced 2025-11-01 15:41:52 +00:00
Compare commits
67 Commits
modbus_hea
...
jesserockz
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
9946592196 | ||
|
|
0ffc446315 | ||
|
|
a692bd98ef | ||
|
|
6178ab7513 | ||
|
|
d24e237967 | ||
|
|
267574f24c | ||
|
|
5235c80781 | ||
|
|
0ccc5e340e | ||
|
|
86c6e4da2a | ||
|
|
5c8b330eaa | ||
|
|
4158a5c2a3 | ||
|
|
05c5364490 | ||
|
|
78eb236a4a | ||
|
|
691cc5f7dc | ||
|
|
b3d7f001af | ||
|
|
3f8b691c32 | ||
|
|
a30f01d668 | ||
|
|
4648804db6 | ||
|
|
51377b2625 | ||
|
|
256f9f9943 | ||
|
|
a72905191a | ||
|
|
7150f2806f | ||
|
|
ee8ee4e646 | ||
|
|
fb357b8965 | ||
|
|
c4fac1a2ae | ||
|
|
42a1f6922f | ||
|
|
206659ddb8 | ||
|
|
440de12e3f | ||
|
|
b122112d58 | ||
|
|
fe258e1007 | ||
|
|
3976fd02ea | ||
|
|
e58c793da2 | ||
|
|
90fb3680d4 | ||
|
|
832a787271 | ||
|
|
29747fc730 | ||
|
|
e2de6ee29d | ||
|
|
053feb5e3b | ||
|
|
31f36df4ba | ||
|
|
3ef392d433 | ||
|
|
138ff749f3 | ||
|
|
e88b8d10ec | ||
|
|
8147d117a0 | ||
|
|
c6f7e84256 | ||
|
|
db877e688a | ||
|
|
4e25b6da7b | ||
|
|
83512b88c4 | ||
|
|
fde5f88192 | ||
|
|
2510b5ffb5 | ||
|
|
364b6ca8d0 | ||
|
|
e49b89a051 | ||
|
|
bdd52dbaa4 | ||
|
|
765793505d | ||
|
|
a303f93236 | ||
|
|
492580edc3 | ||
|
|
1368139f4d | ||
|
|
b6fade7339 | ||
|
|
8da322fe9e | ||
|
|
e5a699a004 | ||
|
|
e061b6dc55 | ||
|
|
4673a5b48c | ||
|
|
0bc18a8281 | ||
|
|
20ba035e3b | ||
|
|
f7019a4ed7 | ||
|
|
a1291c2730 | ||
|
|
b0f8922056 | ||
|
|
4e9e48e2e7 | ||
|
|
86e7013f40 |
39
.github/workflows/ci.yml
vendored
39
.github/workflows/ci.yml
vendored
@@ -214,17 +214,51 @@ jobs:
|
||||
if: matrix.os == 'windows-latest'
|
||||
run: |
|
||||
./venv/Scripts/activate
|
||||
pytest -vv --cov-report=xml --tb=native -n auto tests
|
||||
pytest -vv --cov-report=xml --tb=native -n auto tests --ignore=tests/integration/
|
||||
- name: Run pytest
|
||||
if: matrix.os == 'ubuntu-latest' || matrix.os == 'macOS-latest'
|
||||
run: |
|
||||
. venv/bin/activate
|
||||
pytest -vv --cov-report=xml --tb=native -n auto tests
|
||||
pytest -vv --cov-report=xml --tb=native -n auto tests --ignore=tests/integration/
|
||||
- name: Upload coverage to Codecov
|
||||
uses: codecov/codecov-action@v5.4.3
|
||||
with:
|
||||
token: ${{ secrets.CODECOV_TOKEN }}
|
||||
|
||||
integration-tests:
|
||||
name: Run integration tests
|
||||
runs-on: ubuntu-latest
|
||||
needs:
|
||||
- common
|
||||
steps:
|
||||
- name: Check out code from GitHub
|
||||
uses: actions/checkout@v4.2.2
|
||||
- name: Set up Python 3.13
|
||||
id: python
|
||||
uses: actions/setup-python@v5.6.0
|
||||
with:
|
||||
python-version: "3.13"
|
||||
- name: Restore Python virtual environment
|
||||
id: cache-venv
|
||||
uses: actions/cache@v4.2.3
|
||||
with:
|
||||
path: venv
|
||||
key: ${{ runner.os }}-${{ steps.python.outputs.python-version }}-venv-${{ needs.common.outputs.cache-key }}
|
||||
- name: Create Python virtual environment
|
||||
if: steps.cache-venv.outputs.cache-hit != 'true'
|
||||
run: |
|
||||
python -m venv venv
|
||||
. venv/bin/activate
|
||||
python --version
|
||||
pip install -r requirements.txt -r requirements_test.txt
|
||||
pip install -e .
|
||||
- name: Register matcher
|
||||
run: echo "::add-matcher::.github/workflows/matchers/pytest.json"
|
||||
- name: Run integration tests
|
||||
run: |
|
||||
. venv/bin/activate
|
||||
pytest -vv --no-cov --tb=native -n auto tests/integration/
|
||||
|
||||
clang-format:
|
||||
name: Check clang-format
|
||||
runs-on: ubuntu-24.04
|
||||
@@ -494,6 +528,7 @@ jobs:
|
||||
- flake8
|
||||
- pylint
|
||||
- pytest
|
||||
- integration-tests
|
||||
- pyupgrade
|
||||
- clang-tidy
|
||||
- list-components
|
||||
|
||||
@@ -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
|
||||
@@ -169,6 +170,7 @@ esphome/components/ft5x06/* @clydebarrow
|
||||
esphome/components/ft63x6/* @gpambrozio
|
||||
esphome/components/gcja5/* @gcormier
|
||||
esphome/components/gdk101/* @Szewcson
|
||||
esphome/components/gl_r01_i2c/* @pkejval
|
||||
esphome/components/globals/* @esphome/core
|
||||
esphome/components/gp2y1010au0f/* @zry98
|
||||
esphome/components/gp8403/* @jesserockz
|
||||
@@ -253,6 +255,7 @@ esphome/components/ln882x/* @lamauny
|
||||
esphome/components/lock/* @esphome/core
|
||||
esphome/components/logger/* @esphome/core
|
||||
esphome/components/logger/select/* @clydebarrow
|
||||
esphome/components/lps22/* @nagisa
|
||||
esphome/components/ltr390/* @latonita @sjtrny
|
||||
esphome/components/ltr501/* @latonita
|
||||
esphome/components/ltr_als_ps/* @latonita
|
||||
@@ -441,6 +444,7 @@ esphome/components/sun/* @OttoWinter
|
||||
esphome/components/sun_gtil2/* @Mat931
|
||||
esphome/components/switch/* @esphome/core
|
||||
esphome/components/switch/binary_sensor/* @ssieb
|
||||
esphome/components/sx126x/* @swoboda1337
|
||||
esphome/components/sx127x/* @swoboda1337
|
||||
esphome/components/syslog/* @clydebarrow
|
||||
esphome/components/t6615/* @tylermenezes
|
||||
|
||||
2
Doxyfile
2
Doxyfile
@@ -48,7 +48,7 @@ PROJECT_NAME = ESPHome
|
||||
# could be handy for archiving the generated documentation or if some version
|
||||
# control system is used.
|
||||
|
||||
PROJECT_NUMBER = 2025.7.0-dev
|
||||
PROJECT_NUMBER = 2025.8.0-dev
|
||||
|
||||
# Using the PROJECT_BRIEF tag one can provide an optional one line description
|
||||
# for a project that appears at the top of each page and should give viewer a
|
||||
|
||||
@@ -10,8 +10,15 @@ from esphome.components.esp32.const import (
|
||||
VARIANT_ESP32S2,
|
||||
VARIANT_ESP32S3,
|
||||
)
|
||||
from esphome.config_helpers import filter_source_files_from_platform
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import CONF_ANALOG, CONF_INPUT, CONF_NUMBER, PLATFORM_ESP8266
|
||||
from esphome.const import (
|
||||
CONF_ANALOG,
|
||||
CONF_INPUT,
|
||||
CONF_NUMBER,
|
||||
PLATFORM_ESP8266,
|
||||
PlatformFramework,
|
||||
)
|
||||
from esphome.core import CORE
|
||||
|
||||
CODEOWNERS = ["@esphome/core"]
|
||||
@@ -229,3 +236,20 @@ def validate_adc_pin(value):
|
||||
)(value)
|
||||
|
||||
raise NotImplementedError
|
||||
|
||||
|
||||
FILTER_SOURCE_FILES = filter_source_files_from_platform(
|
||||
{
|
||||
"adc_sensor_esp32.cpp": {
|
||||
PlatformFramework.ESP32_ARDUINO,
|
||||
PlatformFramework.ESP32_IDF,
|
||||
},
|
||||
"adc_sensor_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO},
|
||||
"adc_sensor_rp2040.cpp": {PlatformFramework.RP2040_ARDUINO},
|
||||
"adc_sensor_libretiny.cpp": {
|
||||
PlatformFramework.BK72XX_ARDUINO,
|
||||
PlatformFramework.RTL87XX_ARDUINO,
|
||||
PlatformFramework.LN882X_ARDUINO,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -23,7 +23,7 @@ void APDS9960::setup() {
|
||||
return;
|
||||
}
|
||||
|
||||
if (id != 0xAB && id != 0x9C && id != 0xA8) { // APDS9960 all should have one of these IDs
|
||||
if (id != 0xAB && id != 0x9C && id != 0xA8 && id != 0x9E) { // APDS9960 all should have one of these IDs
|
||||
this->error_code_ = WRONG_ID;
|
||||
this->mark_failed();
|
||||
return;
|
||||
|
||||
@@ -3,6 +3,7 @@ import base64
|
||||
from esphome import automation
|
||||
from esphome.automation import Condition
|
||||
import esphome.codegen as cg
|
||||
from esphome.config_helpers import get_logger_level
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_ACTION,
|
||||
@@ -313,3 +314,17 @@ async def homeassistant_tag_scanned_to_code(config, action_id, template_arg, arg
|
||||
@automation.register_condition("api.connected", APIConnectedCondition, {})
|
||||
async def api_connected_to_code(config, condition_id, template_arg, args):
|
||||
return cg.new_Pvariable(condition_id, template_arg)
|
||||
|
||||
|
||||
def FILTER_SOURCE_FILES() -> list[str]:
|
||||
"""Filter out api_pb2_dump.cpp when proto message dumping is not enabled."""
|
||||
# api_pb2_dump.cpp is only needed when HAS_PROTO_MESSAGE_DUMP is defined
|
||||
# This is a particularly large file that still needs to be opened and read
|
||||
# all the way to the end even when ifdef'd out
|
||||
#
|
||||
# HAS_PROTO_MESSAGE_DUMP is defined when ESPHOME_LOG_HAS_VERY_VERBOSE is set,
|
||||
# which happens when the logger level is VERY_VERBOSE
|
||||
if get_logger_level() != "VERY_VERBOSE":
|
||||
return ["api_pb2_dump.cpp"]
|
||||
|
||||
return []
|
||||
|
||||
@@ -836,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;
|
||||
@@ -851,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;
|
||||
@@ -860,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;
|
||||
|
||||
@@ -38,10 +38,23 @@ 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
|
||||
|
||||
// Helper macro for entity command handlers - gets entity by key, returns if not found, and creates call object
|
||||
#define ENTITY_COMMAND_MAKE_CALL(entity_type, entity_var, getter_name) \
|
||||
entity_type *entity_var = App.get_##getter_name##_by_key(msg.key); \
|
||||
if ((entity_var) == nullptr) \
|
||||
return; \
|
||||
auto call = (entity_var)->make_call();
|
||||
|
||||
// Helper macro for entity command handlers that don't use make_call() - gets entity by key and returns if not found
|
||||
#define ENTITY_COMMAND_GET(entity_type, entity_var, getter_name) \
|
||||
entity_type *entity_var = App.get_##getter_name##_by_key(msg.key); \
|
||||
if ((entity_var) == nullptr) \
|
||||
return;
|
||||
|
||||
APIConnection::APIConnection(std::unique_ptr<socket::Socket> sock, APIServer *parent)
|
||||
: parent_(parent), initial_state_iterator_(this), list_entities_iterator_(this) {
|
||||
#if defined(USE_API_PLAINTEXT) && defined(USE_API_NOISE)
|
||||
@@ -58,6 +71,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(); }
|
||||
@@ -180,10 +198,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
|
||||
@@ -193,18 +211,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();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -356,11 +374,7 @@ uint16_t APIConnection::try_send_cover_info(EntityBase *entity, APIConnection *c
|
||||
return encode_message_to_buffer(msg, ListEntitiesCoverResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
void APIConnection::cover_command(const CoverCommandRequest &msg) {
|
||||
cover::Cover *cover = App.get_cover_by_key(msg.key);
|
||||
if (cover == nullptr)
|
||||
return;
|
||||
|
||||
auto call = cover->make_call();
|
||||
ENTITY_COMMAND_MAKE_CALL(cover::Cover, cover, cover)
|
||||
if (msg.has_legacy_command) {
|
||||
switch (msg.legacy_command) {
|
||||
case enums::LEGACY_COVER_COMMAND_OPEN:
|
||||
@@ -422,11 +436,7 @@ uint16_t APIConnection::try_send_fan_info(EntityBase *entity, APIConnection *con
|
||||
return encode_message_to_buffer(msg, ListEntitiesFanResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
void APIConnection::fan_command(const FanCommandRequest &msg) {
|
||||
fan::Fan *fan = App.get_fan_by_key(msg.key);
|
||||
if (fan == nullptr)
|
||||
return;
|
||||
|
||||
auto call = fan->make_call();
|
||||
ENTITY_COMMAND_MAKE_CALL(fan::Fan, fan, fan)
|
||||
if (msg.has_state)
|
||||
call.set_state(msg.state);
|
||||
if (msg.has_oscillating)
|
||||
@@ -499,11 +509,7 @@ uint16_t APIConnection::try_send_light_info(EntityBase *entity, APIConnection *c
|
||||
return encode_message_to_buffer(msg, ListEntitiesLightResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
void APIConnection::light_command(const LightCommandRequest &msg) {
|
||||
light::LightState *light = App.get_light_by_key(msg.key);
|
||||
if (light == nullptr)
|
||||
return;
|
||||
|
||||
auto call = light->make_call();
|
||||
ENTITY_COMMAND_MAKE_CALL(light::LightState, light, light)
|
||||
if (msg.has_state)
|
||||
call.set_state(msg.state);
|
||||
if (msg.has_brightness)
|
||||
@@ -592,9 +598,7 @@ uint16_t APIConnection::try_send_switch_info(EntityBase *entity, APIConnection *
|
||||
return encode_message_to_buffer(msg, ListEntitiesSwitchResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
void APIConnection::switch_command(const SwitchCommandRequest &msg) {
|
||||
switch_::Switch *a_switch = App.get_switch_by_key(msg.key);
|
||||
if (a_switch == nullptr)
|
||||
return;
|
||||
ENTITY_COMMAND_GET(switch_::Switch, a_switch, switch)
|
||||
|
||||
if (msg.state) {
|
||||
a_switch->turn_on();
|
||||
@@ -703,11 +707,7 @@ uint16_t APIConnection::try_send_climate_info(EntityBase *entity, APIConnection
|
||||
return encode_message_to_buffer(msg, ListEntitiesClimateResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
void APIConnection::climate_command(const ClimateCommandRequest &msg) {
|
||||
climate::Climate *climate = App.get_climate_by_key(msg.key);
|
||||
if (climate == nullptr)
|
||||
return;
|
||||
|
||||
auto call = climate->make_call();
|
||||
ENTITY_COMMAND_MAKE_CALL(climate::Climate, climate, climate)
|
||||
if (msg.has_mode)
|
||||
call.set_mode(static_cast<climate::ClimateMode>(msg.mode));
|
||||
if (msg.has_target_temperature)
|
||||
@@ -762,11 +762,7 @@ uint16_t APIConnection::try_send_number_info(EntityBase *entity, APIConnection *
|
||||
return encode_message_to_buffer(msg, ListEntitiesNumberResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
void APIConnection::number_command(const NumberCommandRequest &msg) {
|
||||
number::Number *number = App.get_number_by_key(msg.key);
|
||||
if (number == nullptr)
|
||||
return;
|
||||
|
||||
auto call = number->make_call();
|
||||
ENTITY_COMMAND_MAKE_CALL(number::Number, number, number)
|
||||
call.set_value(msg.state);
|
||||
call.perform();
|
||||
}
|
||||
@@ -796,11 +792,7 @@ uint16_t APIConnection::try_send_date_info(EntityBase *entity, APIConnection *co
|
||||
return encode_message_to_buffer(msg, ListEntitiesDateResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
void APIConnection::date_command(const DateCommandRequest &msg) {
|
||||
datetime::DateEntity *date = App.get_date_by_key(msg.key);
|
||||
if (date == nullptr)
|
||||
return;
|
||||
|
||||
auto call = date->make_call();
|
||||
ENTITY_COMMAND_MAKE_CALL(datetime::DateEntity, date, date)
|
||||
call.set_date(msg.year, msg.month, msg.day);
|
||||
call.perform();
|
||||
}
|
||||
@@ -830,11 +822,7 @@ uint16_t APIConnection::try_send_time_info(EntityBase *entity, APIConnection *co
|
||||
return encode_message_to_buffer(msg, ListEntitiesTimeResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
void APIConnection::time_command(const TimeCommandRequest &msg) {
|
||||
datetime::TimeEntity *time = App.get_time_by_key(msg.key);
|
||||
if (time == nullptr)
|
||||
return;
|
||||
|
||||
auto call = time->make_call();
|
||||
ENTITY_COMMAND_MAKE_CALL(datetime::TimeEntity, time, time)
|
||||
call.set_time(msg.hour, msg.minute, msg.second);
|
||||
call.perform();
|
||||
}
|
||||
@@ -866,11 +854,7 @@ uint16_t APIConnection::try_send_datetime_info(EntityBase *entity, APIConnection
|
||||
return encode_message_to_buffer(msg, ListEntitiesDateTimeResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
void APIConnection::datetime_command(const DateTimeCommandRequest &msg) {
|
||||
datetime::DateTimeEntity *datetime = App.get_datetime_by_key(msg.key);
|
||||
if (datetime == nullptr)
|
||||
return;
|
||||
|
||||
auto call = datetime->make_call();
|
||||
ENTITY_COMMAND_MAKE_CALL(datetime::DateTimeEntity, datetime, datetime)
|
||||
call.set_datetime(msg.epoch_seconds);
|
||||
call.perform();
|
||||
}
|
||||
@@ -904,11 +888,7 @@ uint16_t APIConnection::try_send_text_info(EntityBase *entity, APIConnection *co
|
||||
return encode_message_to_buffer(msg, ListEntitiesTextResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
void APIConnection::text_command(const TextCommandRequest &msg) {
|
||||
text::Text *text = App.get_text_by_key(msg.key);
|
||||
if (text == nullptr)
|
||||
return;
|
||||
|
||||
auto call = text->make_call();
|
||||
ENTITY_COMMAND_MAKE_CALL(text::Text, text, text)
|
||||
call.set_value(msg.state);
|
||||
call.perform();
|
||||
}
|
||||
@@ -940,11 +920,7 @@ uint16_t APIConnection::try_send_select_info(EntityBase *entity, APIConnection *
|
||||
return encode_message_to_buffer(msg, ListEntitiesSelectResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
void APIConnection::select_command(const SelectCommandRequest &msg) {
|
||||
select::Select *select = App.get_select_by_key(msg.key);
|
||||
if (select == nullptr)
|
||||
return;
|
||||
|
||||
auto call = select->make_call();
|
||||
ENTITY_COMMAND_MAKE_CALL(select::Select, select, select)
|
||||
call.set_option(msg.state);
|
||||
call.perform();
|
||||
}
|
||||
@@ -961,10 +937,7 @@ uint16_t APIConnection::try_send_button_info(EntityBase *entity, APIConnection *
|
||||
return encode_message_to_buffer(msg, ListEntitiesButtonResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
void esphome::api::APIConnection::button_command(const ButtonCommandRequest &msg) {
|
||||
button::Button *button = App.get_button_by_key(msg.key);
|
||||
if (button == nullptr)
|
||||
return;
|
||||
|
||||
ENTITY_COMMAND_GET(button::Button, button, button)
|
||||
button->press();
|
||||
}
|
||||
#endif
|
||||
@@ -995,9 +968,7 @@ uint16_t APIConnection::try_send_lock_info(EntityBase *entity, APIConnection *co
|
||||
return encode_message_to_buffer(msg, ListEntitiesLockResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
void APIConnection::lock_command(const LockCommandRequest &msg) {
|
||||
lock::Lock *a_lock = App.get_lock_by_key(msg.key);
|
||||
if (a_lock == nullptr)
|
||||
return;
|
||||
ENTITY_COMMAND_GET(lock::Lock, a_lock, lock)
|
||||
|
||||
switch (msg.command) {
|
||||
case enums::LOCK_UNLOCK:
|
||||
@@ -1040,11 +1011,7 @@ uint16_t APIConnection::try_send_valve_info(EntityBase *entity, APIConnection *c
|
||||
return encode_message_to_buffer(msg, ListEntitiesValveResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
void APIConnection::valve_command(const ValveCommandRequest &msg) {
|
||||
valve::Valve *valve = App.get_valve_by_key(msg.key);
|
||||
if (valve == nullptr)
|
||||
return;
|
||||
|
||||
auto call = valve->make_call();
|
||||
ENTITY_COMMAND_MAKE_CALL(valve::Valve, valve, valve)
|
||||
if (msg.has_position)
|
||||
call.set_position(msg.position);
|
||||
if (msg.stop)
|
||||
@@ -1091,11 +1058,7 @@ uint16_t APIConnection::try_send_media_player_info(EntityBase *entity, APIConnec
|
||||
return encode_message_to_buffer(msg, ListEntitiesMediaPlayerResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
void APIConnection::media_player_command(const MediaPlayerCommandRequest &msg) {
|
||||
media_player::MediaPlayer *media_player = App.get_media_player_by_key(msg.key);
|
||||
if (media_player == nullptr)
|
||||
return;
|
||||
|
||||
auto call = media_player->make_call();
|
||||
ENTITY_COMMAND_MAKE_CALL(media_player::MediaPlayer, media_player, media_player)
|
||||
if (msg.has_command) {
|
||||
call.set_command(static_cast<media_player::MediaPlayerCommand>(msg.command));
|
||||
}
|
||||
@@ -1112,36 +1075,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
|
||||
@@ -1213,66 +1176,53 @@ void APIConnection::bluetooth_scanner_set_mode(const BluetoothScannerSetModeRequ
|
||||
#endif
|
||||
|
||||
#ifdef USE_VOICE_ASSISTANT
|
||||
bool APIConnection::check_voice_assistant_api_connection_() const {
|
||||
return voice_assistant::global_voice_assistant != nullptr &&
|
||||
voice_assistant::global_voice_assistant->get_api_connection() == this;
|
||||
}
|
||||
|
||||
void APIConnection::subscribe_voice_assistant(const SubscribeVoiceAssistantRequest &msg) {
|
||||
if (voice_assistant::global_voice_assistant != nullptr) {
|
||||
voice_assistant::global_voice_assistant->client_subscription(this, msg.subscribe);
|
||||
}
|
||||
}
|
||||
void APIConnection::on_voice_assistant_response(const VoiceAssistantResponse &msg) {
|
||||
if (voice_assistant::global_voice_assistant != nullptr) {
|
||||
if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
|
||||
return;
|
||||
}
|
||||
if (!this->check_voice_assistant_api_connection_()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.error) {
|
||||
voice_assistant::global_voice_assistant->failed_to_start();
|
||||
return;
|
||||
}
|
||||
if (msg.port == 0) {
|
||||
// Use API Audio
|
||||
voice_assistant::global_voice_assistant->start_streaming();
|
||||
} else {
|
||||
struct sockaddr_storage storage;
|
||||
socklen_t len = sizeof(storage);
|
||||
this->helper_->getpeername((struct sockaddr *) &storage, &len);
|
||||
voice_assistant::global_voice_assistant->start_streaming(&storage, msg.port);
|
||||
}
|
||||
if (msg.error) {
|
||||
voice_assistant::global_voice_assistant->failed_to_start();
|
||||
return;
|
||||
}
|
||||
if (msg.port == 0) {
|
||||
// Use API Audio
|
||||
voice_assistant::global_voice_assistant->start_streaming();
|
||||
} else {
|
||||
struct sockaddr_storage storage;
|
||||
socklen_t len = sizeof(storage);
|
||||
this->helper_->getpeername((struct sockaddr *) &storage, &len);
|
||||
voice_assistant::global_voice_assistant->start_streaming(&storage, msg.port);
|
||||
}
|
||||
};
|
||||
void APIConnection::on_voice_assistant_event_response(const VoiceAssistantEventResponse &msg) {
|
||||
if (voice_assistant::global_voice_assistant != nullptr) {
|
||||
if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->check_voice_assistant_api_connection_()) {
|
||||
voice_assistant::global_voice_assistant->on_event(msg);
|
||||
}
|
||||
}
|
||||
void APIConnection::on_voice_assistant_audio(const VoiceAssistantAudio &msg) {
|
||||
if (voice_assistant::global_voice_assistant != nullptr) {
|
||||
if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->check_voice_assistant_api_connection_()) {
|
||||
voice_assistant::global_voice_assistant->on_audio(msg);
|
||||
}
|
||||
};
|
||||
void APIConnection::on_voice_assistant_timer_event_response(const VoiceAssistantTimerEventResponse &msg) {
|
||||
if (voice_assistant::global_voice_assistant != nullptr) {
|
||||
if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->check_voice_assistant_api_connection_()) {
|
||||
voice_assistant::global_voice_assistant->on_timer_event(msg);
|
||||
}
|
||||
};
|
||||
|
||||
void APIConnection::on_voice_assistant_announce_request(const VoiceAssistantAnnounceRequest &msg) {
|
||||
if (voice_assistant::global_voice_assistant != nullptr) {
|
||||
if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->check_voice_assistant_api_connection_()) {
|
||||
voice_assistant::global_voice_assistant->on_announce(msg);
|
||||
}
|
||||
}
|
||||
@@ -1280,35 +1230,29 @@ void APIConnection::on_voice_assistant_announce_request(const VoiceAssistantAnno
|
||||
VoiceAssistantConfigurationResponse APIConnection::voice_assistant_get_configuration(
|
||||
const VoiceAssistantConfigurationRequest &msg) {
|
||||
VoiceAssistantConfigurationResponse resp;
|
||||
if (voice_assistant::global_voice_assistant != nullptr) {
|
||||
if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
|
||||
return resp;
|
||||
}
|
||||
|
||||
auto &config = voice_assistant::global_voice_assistant->get_configuration();
|
||||
for (auto &wake_word : config.available_wake_words) {
|
||||
VoiceAssistantWakeWord resp_wake_word;
|
||||
resp_wake_word.id = wake_word.id;
|
||||
resp_wake_word.wake_word = wake_word.wake_word;
|
||||
for (const auto &lang : wake_word.trained_languages) {
|
||||
resp_wake_word.trained_languages.push_back(lang);
|
||||
}
|
||||
resp.available_wake_words.push_back(std::move(resp_wake_word));
|
||||
}
|
||||
for (auto &wake_word_id : config.active_wake_words) {
|
||||
resp.active_wake_words.push_back(wake_word_id);
|
||||
}
|
||||
resp.max_active_wake_words = config.max_active_wake_words;
|
||||
if (!this->check_voice_assistant_api_connection_()) {
|
||||
return resp;
|
||||
}
|
||||
|
||||
auto &config = voice_assistant::global_voice_assistant->get_configuration();
|
||||
for (auto &wake_word : config.available_wake_words) {
|
||||
VoiceAssistantWakeWord resp_wake_word;
|
||||
resp_wake_word.id = wake_word.id;
|
||||
resp_wake_word.wake_word = wake_word.wake_word;
|
||||
for (const auto &lang : wake_word.trained_languages) {
|
||||
resp_wake_word.trained_languages.push_back(lang);
|
||||
}
|
||||
resp.available_wake_words.push_back(std::move(resp_wake_word));
|
||||
}
|
||||
for (auto &wake_word_id : config.active_wake_words) {
|
||||
resp.active_wake_words.push_back(wake_word_id);
|
||||
}
|
||||
resp.max_active_wake_words = config.max_active_wake_words;
|
||||
return resp;
|
||||
}
|
||||
|
||||
void APIConnection::voice_assistant_set_configuration(const VoiceAssistantSetConfiguration &msg) {
|
||||
if (voice_assistant::global_voice_assistant != nullptr) {
|
||||
if (voice_assistant::global_voice_assistant->get_api_connection() != this) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->check_voice_assistant_api_connection_()) {
|
||||
voice_assistant::global_voice_assistant->on_set_configuration(msg.active_wake_words);
|
||||
}
|
||||
}
|
||||
@@ -1341,11 +1285,7 @@ uint16_t APIConnection::try_send_alarm_control_panel_info(EntityBase *entity, AP
|
||||
is_single);
|
||||
}
|
||||
void APIConnection::alarm_control_panel_command(const AlarmControlPanelCommandRequest &msg) {
|
||||
alarm_control_panel::AlarmControlPanel *a_alarm_control_panel = App.get_alarm_control_panel_by_key(msg.key);
|
||||
if (a_alarm_control_panel == nullptr)
|
||||
return;
|
||||
|
||||
auto call = a_alarm_control_panel->make_call();
|
||||
ENTITY_COMMAND_MAKE_CALL(alarm_control_panel::AlarmControlPanel, a_alarm_control_panel, alarm_control_panel)
|
||||
switch (msg.command) {
|
||||
case enums::ALARM_CONTROL_PANEL_DISARM:
|
||||
call.disarm();
|
||||
@@ -1433,9 +1373,7 @@ uint16_t APIConnection::try_send_update_info(EntityBase *entity, APIConnection *
|
||||
return encode_message_to_buffer(msg, ListEntitiesUpdateResponse::MESSAGE_TYPE, conn, remaining_size, is_single);
|
||||
}
|
||||
void APIConnection::update_command(const UpdateCommandRequest &msg) {
|
||||
update::UpdateEntity *update = App.get_update_by_key(msg.key);
|
||||
if (update == nullptr)
|
||||
return;
|
||||
ENTITY_COMMAND_GET(update::UpdateEntity, update, update)
|
||||
|
||||
switch (msg.command) {
|
||||
case enums::UPDATE_COMMAND_UPDATE:
|
||||
@@ -1454,12 +1392,11 @@ void APIConnection::update_command(const UpdateCommandRequest &msg) {
|
||||
}
|
||||
#endif
|
||||
|
||||
bool APIConnection::try_send_log_message(int level, const char *tag, const char *line) {
|
||||
bool APIConnection::try_send_log_message(int level, const char *tag, const char *line, size_t message_len) {
|
||||
if (this->flags_.log_subscription < level)
|
||||
return false;
|
||||
|
||||
// Pre-calculate message size to avoid reallocations
|
||||
const size_t line_length = strlen(line);
|
||||
uint32_t msg_size = 0;
|
||||
|
||||
// Add size for level field (field ID 1, varint type)
|
||||
@@ -1468,14 +1405,14 @@ bool APIConnection::try_send_log_message(int level, const char *tag, const char
|
||||
|
||||
// Add size for string field (field ID 3, string type)
|
||||
// 1 byte for field tag + size of length varint + string length
|
||||
msg_size += 1 + api::ProtoSize::varint(static_cast<uint32_t>(line_length)) + line_length;
|
||||
msg_size += 1 + api::ProtoSize::varint(static_cast<uint32_t>(message_len)) + message_len;
|
||||
|
||||
// Create a pre-sized buffer
|
||||
auto buffer = this->create_buffer(msg_size);
|
||||
|
||||
// Encode the message (SubscribeLogsResponse)
|
||||
buffer.encode_uint32(1, static_cast<uint32_t>(level)); // LogLevel level = 1
|
||||
buffer.encode_string(3, line, line_length); // string message = 3
|
||||
buffer.encode_string(3, line, message_len); // string message = 3
|
||||
|
||||
// SubscribeLogsResponse - 29
|
||||
return this->send_buffer(buffer, SubscribeLogsResponse::MESSAGE_TYPE);
|
||||
|
||||
@@ -60,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
|
||||
@@ -107,7 +107,7 @@ class APIConnection : public APIServerConnection {
|
||||
bool send_media_player_state(media_player::MediaPlayer *media_player);
|
||||
void media_player_command(const MediaPlayerCommandRequest &msg) override;
|
||||
#endif
|
||||
bool try_send_log_message(int level, const char *tag, const char *line);
|
||||
bool try_send_log_message(int level, const char *tag, const char *line, size_t message_len);
|
||||
void send_homeassistant_service_call(const HomeassistantServiceResponse &call) {
|
||||
if (!this->flags_.service_call_subscription)
|
||||
return;
|
||||
@@ -301,6 +301,11 @@ class APIConnection : public APIServerConnection {
|
||||
static uint16_t encode_message_to_buffer(ProtoMessage &msg, uint16_t message_type, APIConnection *conn,
|
||||
uint32_t remaining_size, bool is_single);
|
||||
|
||||
#ifdef USE_VOICE_ASSISTANT
|
||||
// Helper to check voice assistant validity and connection ownership
|
||||
inline bool check_voice_assistant_api_connection_() const;
|
||||
#endif
|
||||
|
||||
// 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();
|
||||
@@ -425,7 +430,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
|
||||
@@ -455,8 +460,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)
|
||||
|
||||
@@ -225,6 +225,22 @@ APIError APIFrameHelper::init_common_() {
|
||||
}
|
||||
|
||||
#define HELPER_LOG(msg, ...) ESP_LOGVV(TAG, "%s: " msg, this->info_.c_str(), ##__VA_ARGS__)
|
||||
|
||||
APIError APIFrameHelper::handle_socket_read_result_(ssize_t received) {
|
||||
if (received == -1) {
|
||||
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
||||
return APIError::WOULD_BLOCK;
|
||||
}
|
||||
state_ = State::FAILED;
|
||||
HELPER_LOG("Socket read failed with errno %d", errno);
|
||||
return APIError::SOCKET_READ_FAILED;
|
||||
} else if (received == 0) {
|
||||
state_ = State::FAILED;
|
||||
HELPER_LOG("Connection closed");
|
||||
return APIError::CONNECTION_CLOSED;
|
||||
}
|
||||
return APIError::OK;
|
||||
}
|
||||
// uncomment to log raw packets
|
||||
//#define HELPER_LOG_PACKETS
|
||||
|
||||
@@ -327,17 +343,9 @@ APIError APINoiseFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
||||
// no header information yet
|
||||
uint8_t to_read = 3 - rx_header_buf_len_;
|
||||
ssize_t received = this->socket_->read(&rx_header_buf_[rx_header_buf_len_], to_read);
|
||||
if (received == -1) {
|
||||
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
||||
return APIError::WOULD_BLOCK;
|
||||
}
|
||||
state_ = State::FAILED;
|
||||
HELPER_LOG("Socket read failed with errno %d", errno);
|
||||
return APIError::SOCKET_READ_FAILED;
|
||||
} else if (received == 0) {
|
||||
state_ = State::FAILED;
|
||||
HELPER_LOG("Connection closed");
|
||||
return APIError::CONNECTION_CLOSED;
|
||||
APIError err = handle_socket_read_result_(received);
|
||||
if (err != APIError::OK) {
|
||||
return err;
|
||||
}
|
||||
rx_header_buf_len_ += static_cast<uint8_t>(received);
|
||||
if (static_cast<uint8_t>(received) != to_read) {
|
||||
@@ -372,17 +380,9 @@ APIError APINoiseFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
||||
// more data to read
|
||||
uint16_t to_read = msg_size - rx_buf_len_;
|
||||
ssize_t received = this->socket_->read(&rx_buf_[rx_buf_len_], to_read);
|
||||
if (received == -1) {
|
||||
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
||||
return APIError::WOULD_BLOCK;
|
||||
}
|
||||
state_ = State::FAILED;
|
||||
HELPER_LOG("Socket read failed with errno %d", errno);
|
||||
return APIError::SOCKET_READ_FAILED;
|
||||
} else if (received == 0) {
|
||||
state_ = State::FAILED;
|
||||
HELPER_LOG("Connection closed");
|
||||
return APIError::CONNECTION_CLOSED;
|
||||
APIError err = handle_socket_read_result_(received);
|
||||
if (err != APIError::OK) {
|
||||
return err;
|
||||
}
|
||||
rx_buf_len_ += static_cast<uint16_t>(received);
|
||||
if (static_cast<uint16_t>(received) != to_read) {
|
||||
@@ -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
|
||||
@@ -865,17 +855,9 @@ APIError APIPlaintextFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
||||
// Try to get to at least 3 bytes total (indicator + 2 varint bytes), then read one byte at a time
|
||||
ssize_t received =
|
||||
this->socket_->read(&rx_header_buf_[rx_header_buf_pos_], rx_header_buf_pos_ < 3 ? 3 - rx_header_buf_pos_ : 1);
|
||||
if (received == -1) {
|
||||
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
||||
return APIError::WOULD_BLOCK;
|
||||
}
|
||||
state_ = State::FAILED;
|
||||
HELPER_LOG("Socket read failed with errno %d", errno);
|
||||
return APIError::SOCKET_READ_FAILED;
|
||||
} else if (received == 0) {
|
||||
state_ = State::FAILED;
|
||||
HELPER_LOG("Connection closed");
|
||||
return APIError::CONNECTION_CLOSED;
|
||||
APIError err = handle_socket_read_result_(received);
|
||||
if (err != APIError::OK) {
|
||||
return err;
|
||||
}
|
||||
|
||||
// If this was the first read, validate the indicator byte
|
||||
@@ -959,17 +941,9 @@ APIError APIPlaintextFrameHelper::try_read_frame_(ParsedFrame *frame) {
|
||||
// more data to read
|
||||
uint16_t to_read = rx_header_parsed_len_ - rx_buf_len_;
|
||||
ssize_t received = this->socket_->read(&rx_buf_[rx_buf_len_], to_read);
|
||||
if (received == -1) {
|
||||
if (errno == EWOULDBLOCK || errno == EAGAIN) {
|
||||
return APIError::WOULD_BLOCK;
|
||||
}
|
||||
state_ = State::FAILED;
|
||||
HELPER_LOG("Socket read failed with errno %d", errno);
|
||||
return APIError::SOCKET_READ_FAILED;
|
||||
} else if (received == 0) {
|
||||
state_ = State::FAILED;
|
||||
HELPER_LOG("Connection closed");
|
||||
return APIError::CONNECTION_CLOSED;
|
||||
APIError err = handle_socket_read_result_(received);
|
||||
if (err != APIError::OK) {
|
||||
return err;
|
||||
}
|
||||
rx_buf_len_ += static_cast<uint16_t>(received);
|
||||
if (static_cast<uint16_t>(received) != to_read) {
|
||||
@@ -1029,18 +1003,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 +1017,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 +1053,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
|
||||
@@ -175,6 +176,9 @@ class APIFrameHelper {
|
||||
|
||||
// Common initialization for both plaintext and noise protocols
|
||||
APIError init_common_();
|
||||
|
||||
// Helper method to handle socket read results
|
||||
APIError handle_socket_read_result_(ssize_t received);
|
||||
};
|
||||
|
||||
#ifdef USE_API_NOISE
|
||||
@@ -194,7 +198,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 +252,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_; }
|
||||
|
||||
@@ -2216,7 +2216,7 @@ void ExecuteServiceRequest::calculate_size(uint32_t &total_size) const {
|
||||
ProtoSize::add_fixed_field<4>(total_size, 1, this->key != 0, false);
|
||||
ProtoSize::add_repeated_message(total_size, 1, this->args);
|
||||
}
|
||||
#ifdef USE_ESP32_CAMERA
|
||||
#ifdef USE_CAMERA
|
||||
bool ListEntitiesCameraResponse::decode_varint(uint32_t field_id, ProtoVarInt value) {
|
||||
switch (field_id) {
|
||||
case 5: {
|
||||
|
||||
@@ -1273,7 +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_ESP32_CAMERA
|
||||
#ifdef USE_CAMERA
|
||||
class ListEntitiesCameraResponse : public InfoResponseProtoMessage {
|
||||
public:
|
||||
static constexpr uint16_t MESSAGE_TYPE = 43;
|
||||
|
||||
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);
|
||||
|
||||
@@ -71,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
|
||||
|
||||
@@ -223,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
|
||||
@@ -340,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
|
||||
@@ -96,30 +104,30 @@ void APIServer::setup() {
|
||||
|
||||
#ifdef USE_LOGGER
|
||||
if (logger::global_logger != nullptr) {
|
||||
logger::global_logger->add_on_log_callback([this](int level, const char *tag, const char *message) {
|
||||
if (this->shutting_down_) {
|
||||
// Don't try to send logs during shutdown
|
||||
// as it could result in a recursion and
|
||||
// we would be filling a buffer we are trying to clear
|
||||
return;
|
||||
}
|
||||
for (auto &c : this->clients_) {
|
||||
if (!c->flags_.remove)
|
||||
c->try_send_log_message(level, tag, message);
|
||||
}
|
||||
});
|
||||
logger::global_logger->add_on_log_callback(
|
||||
[this](int level, const char *tag, const char *message, size_t message_len) {
|
||||
if (this->shutting_down_) {
|
||||
// Don't try to send logs during shutdown
|
||||
// as it could result in a recursion and
|
||||
// we would be filling a buffer we are trying to clear
|
||||
return;
|
||||
}
|
||||
for (auto &c : this->clients_) {
|
||||
if (!c->flags_.remove)
|
||||
c->try_send_log_message(level, tag, message, message_len);
|
||||
}
|
||||
});
|
||||
}
|
||||
#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
|
||||
}
|
||||
@@ -253,180 +261,114 @@ bool APIServer::check_password(const std::string &password) const {
|
||||
|
||||
void APIServer::handle_disconnect(APIConnection *conn) {}
|
||||
|
||||
// Macro for entities without extra parameters
|
||||
#define API_DISPATCH_UPDATE(entity_type, entity_name) \
|
||||
void APIServer::on_##entity_name##_update(entity_type *obj) { /* NOLINT(bugprone-macro-parentheses) */ \
|
||||
if (obj->is_internal()) \
|
||||
return; \
|
||||
for (auto &c : this->clients_) \
|
||||
c->send_##entity_name##_state(obj); \
|
||||
}
|
||||
|
||||
// Macro for entities with extra parameters (but parameters not used in send)
|
||||
#define API_DISPATCH_UPDATE_IGNORE_PARAMS(entity_type, entity_name, ...) \
|
||||
void APIServer::on_##entity_name##_update(entity_type *obj, __VA_ARGS__) { /* NOLINT(bugprone-macro-parentheses) */ \
|
||||
if (obj->is_internal()) \
|
||||
return; \
|
||||
for (auto &c : this->clients_) \
|
||||
c->send_##entity_name##_state(obj); \
|
||||
}
|
||||
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
void APIServer::on_binary_sensor_update(binary_sensor::BinarySensor *obj) {
|
||||
if (obj->is_internal())
|
||||
return;
|
||||
for (auto &c : this->clients_)
|
||||
c->send_binary_sensor_state(obj);
|
||||
}
|
||||
API_DISPATCH_UPDATE(binary_sensor::BinarySensor, binary_sensor)
|
||||
#endif
|
||||
|
||||
#ifdef USE_COVER
|
||||
void APIServer::on_cover_update(cover::Cover *obj) {
|
||||
if (obj->is_internal())
|
||||
return;
|
||||
for (auto &c : this->clients_)
|
||||
c->send_cover_state(obj);
|
||||
}
|
||||
API_DISPATCH_UPDATE(cover::Cover, cover)
|
||||
#endif
|
||||
|
||||
#ifdef USE_FAN
|
||||
void APIServer::on_fan_update(fan::Fan *obj) {
|
||||
if (obj->is_internal())
|
||||
return;
|
||||
for (auto &c : this->clients_)
|
||||
c->send_fan_state(obj);
|
||||
}
|
||||
API_DISPATCH_UPDATE(fan::Fan, fan)
|
||||
#endif
|
||||
|
||||
#ifdef USE_LIGHT
|
||||
void APIServer::on_light_update(light::LightState *obj) {
|
||||
if (obj->is_internal())
|
||||
return;
|
||||
for (auto &c : this->clients_)
|
||||
c->send_light_state(obj);
|
||||
}
|
||||
API_DISPATCH_UPDATE(light::LightState, light)
|
||||
#endif
|
||||
|
||||
#ifdef USE_SENSOR
|
||||
void APIServer::on_sensor_update(sensor::Sensor *obj, float state) {
|
||||
if (obj->is_internal())
|
||||
return;
|
||||
for (auto &c : this->clients_)
|
||||
c->send_sensor_state(obj);
|
||||
}
|
||||
API_DISPATCH_UPDATE_IGNORE_PARAMS(sensor::Sensor, sensor, float state)
|
||||
#endif
|
||||
|
||||
#ifdef USE_SWITCH
|
||||
void APIServer::on_switch_update(switch_::Switch *obj, bool state) {
|
||||
if (obj->is_internal())
|
||||
return;
|
||||
for (auto &c : this->clients_)
|
||||
c->send_switch_state(obj);
|
||||
}
|
||||
API_DISPATCH_UPDATE_IGNORE_PARAMS(switch_::Switch, switch, bool state)
|
||||
#endif
|
||||
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
void APIServer::on_text_sensor_update(text_sensor::TextSensor *obj, const std::string &state) {
|
||||
if (obj->is_internal())
|
||||
return;
|
||||
for (auto &c : this->clients_)
|
||||
c->send_text_sensor_state(obj);
|
||||
}
|
||||
API_DISPATCH_UPDATE_IGNORE_PARAMS(text_sensor::TextSensor, text_sensor, const std::string &state)
|
||||
#endif
|
||||
|
||||
#ifdef USE_CLIMATE
|
||||
void APIServer::on_climate_update(climate::Climate *obj) {
|
||||
if (obj->is_internal())
|
||||
return;
|
||||
for (auto &c : this->clients_)
|
||||
c->send_climate_state(obj);
|
||||
}
|
||||
API_DISPATCH_UPDATE(climate::Climate, climate)
|
||||
#endif
|
||||
|
||||
#ifdef USE_NUMBER
|
||||
void APIServer::on_number_update(number::Number *obj, float state) {
|
||||
if (obj->is_internal())
|
||||
return;
|
||||
for (auto &c : this->clients_)
|
||||
c->send_number_state(obj);
|
||||
}
|
||||
API_DISPATCH_UPDATE_IGNORE_PARAMS(number::Number, number, float state)
|
||||
#endif
|
||||
|
||||
#ifdef USE_DATETIME_DATE
|
||||
void APIServer::on_date_update(datetime::DateEntity *obj) {
|
||||
if (obj->is_internal())
|
||||
return;
|
||||
for (auto &c : this->clients_)
|
||||
c->send_date_state(obj);
|
||||
}
|
||||
API_DISPATCH_UPDATE(datetime::DateEntity, date)
|
||||
#endif
|
||||
|
||||
#ifdef USE_DATETIME_TIME
|
||||
void APIServer::on_time_update(datetime::TimeEntity *obj) {
|
||||
if (obj->is_internal())
|
||||
return;
|
||||
for (auto &c : this->clients_)
|
||||
c->send_time_state(obj);
|
||||
}
|
||||
API_DISPATCH_UPDATE(datetime::TimeEntity, time)
|
||||
#endif
|
||||
|
||||
#ifdef USE_DATETIME_DATETIME
|
||||
void APIServer::on_datetime_update(datetime::DateTimeEntity *obj) {
|
||||
if (obj->is_internal())
|
||||
return;
|
||||
for (auto &c : this->clients_)
|
||||
c->send_datetime_state(obj);
|
||||
}
|
||||
API_DISPATCH_UPDATE(datetime::DateTimeEntity, datetime)
|
||||
#endif
|
||||
|
||||
#ifdef USE_TEXT
|
||||
void APIServer::on_text_update(text::Text *obj, const std::string &state) {
|
||||
if (obj->is_internal())
|
||||
return;
|
||||
for (auto &c : this->clients_)
|
||||
c->send_text_state(obj);
|
||||
}
|
||||
API_DISPATCH_UPDATE_IGNORE_PARAMS(text::Text, text, const std::string &state)
|
||||
#endif
|
||||
|
||||
#ifdef USE_SELECT
|
||||
void APIServer::on_select_update(select::Select *obj, const std::string &state, size_t index) {
|
||||
if (obj->is_internal())
|
||||
return;
|
||||
for (auto &c : this->clients_)
|
||||
c->send_select_state(obj);
|
||||
}
|
||||
API_DISPATCH_UPDATE_IGNORE_PARAMS(select::Select, select, const std::string &state, size_t index)
|
||||
#endif
|
||||
|
||||
#ifdef USE_LOCK
|
||||
void APIServer::on_lock_update(lock::Lock *obj) {
|
||||
if (obj->is_internal())
|
||||
return;
|
||||
for (auto &c : this->clients_)
|
||||
c->send_lock_state(obj);
|
||||
}
|
||||
API_DISPATCH_UPDATE(lock::Lock, lock)
|
||||
#endif
|
||||
|
||||
#ifdef USE_VALVE
|
||||
void APIServer::on_valve_update(valve::Valve *obj) {
|
||||
if (obj->is_internal())
|
||||
return;
|
||||
for (auto &c : this->clients_)
|
||||
c->send_valve_state(obj);
|
||||
}
|
||||
API_DISPATCH_UPDATE(valve::Valve, valve)
|
||||
#endif
|
||||
|
||||
#ifdef USE_MEDIA_PLAYER
|
||||
void APIServer::on_media_player_update(media_player::MediaPlayer *obj) {
|
||||
if (obj->is_internal())
|
||||
return;
|
||||
for (auto &c : this->clients_)
|
||||
c->send_media_player_state(obj);
|
||||
}
|
||||
API_DISPATCH_UPDATE(media_player::MediaPlayer, media_player)
|
||||
#endif
|
||||
|
||||
#ifdef USE_EVENT
|
||||
// Event is a special case - it's the only entity that passes extra parameters to the send method
|
||||
void APIServer::on_event(event::Event *obj, const std::string &event_type) {
|
||||
if (obj->is_internal())
|
||||
return;
|
||||
for (auto &c : this->clients_)
|
||||
c->send_event(obj, event_type);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_UPDATE
|
||||
// Update is a special case - the method is called on_update, not on_update_update
|
||||
void APIServer::on_update(update::UpdateEntity *obj) {
|
||||
if (obj->is_internal())
|
||||
return;
|
||||
for (auto &c : this->clients_)
|
||||
c->send_update_state(obj);
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef USE_ALARM_CONTROL_PANEL
|
||||
void APIServer::on_alarm_control_panel_update(alarm_control_panel::AlarmControlPanel *obj) {
|
||||
if (obj->is_internal())
|
||||
return;
|
||||
for (auto &c : this->clients_)
|
||||
c->send_alarm_control_panel_state(obj);
|
||||
}
|
||||
API_DISPATCH_UPDATE(alarm_control_panel::AlarmControlPanel, alarm_control_panel)
|
||||
#endif
|
||||
|
||||
float APIServer::get_setup_priority() const { return setup_priority::AFTER_WIFI; }
|
||||
|
||||
@@ -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();
|
||||
@@ -151,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
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -52,11 +52,21 @@ bool BluetoothProxy::parse_device(const esp32_ble_tracker::ESPBTDevice &device)
|
||||
return true;
|
||||
}
|
||||
|
||||
static constexpr size_t FLUSH_BATCH_SIZE = 8;
|
||||
static std::vector<api::BluetoothLERawAdvertisement> &get_batch_buffer() {
|
||||
static std::vector<api::BluetoothLERawAdvertisement> batch_buffer;
|
||||
return batch_buffer;
|
||||
}
|
||||
// Batch size for BLE advertisements to maximize WiFi efficiency
|
||||
// Each advertisement is up to 80 bytes when packaged (including protocol overhead)
|
||||
// Most advertisements are 20-30 bytes, allowing even more to fit per packet
|
||||
// 16 advertisements × 80 bytes (worst case) = 1280 bytes out of ~1320 bytes usable payload
|
||||
// This achieves ~97% WiFi MTU utilization while staying under the limit
|
||||
static constexpr size_t FLUSH_BATCH_SIZE = 16;
|
||||
|
||||
namespace {
|
||||
// Batch buffer in anonymous namespace to avoid guard variable (saves 8 bytes)
|
||||
// This is initialized at program startup before any threads
|
||||
// NOLINTNEXTLINE(cppcoreguidelines-avoid-non-const-global-variables)
|
||||
std::vector<api::BluetoothLERawAdvertisement> batch_buffer;
|
||||
} // namespace
|
||||
|
||||
static std::vector<api::BluetoothLERawAdvertisement> &get_batch_buffer() { return batch_buffer; }
|
||||
|
||||
bool BluetoothProxy::parse_devices(const esp32_ble::BLEScanResult *scan_results, size_t count) {
|
||||
if (!api::global_api_server->is_connected() || this->api_connection_ == nullptr || !this->raw_advertisements_)
|
||||
@@ -170,7 +180,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
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
CODEOWNERS = ["@esphome/core"]
|
||||
|
||||
CONF_BYTE_ORDER = "byte_order"
|
||||
CONF_DRAW_ROUNDING = "draw_rounding"
|
||||
CONF_ON_STATE_CHANGE = "on_state_change"
|
||||
CONF_REQUEST_HEADERS = "request_headers"
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.config_helpers import filter_source_files_from_platform
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_BLOCK,
|
||||
@@ -7,6 +8,7 @@ from esphome.const import (
|
||||
CONF_FREE,
|
||||
CONF_ID,
|
||||
CONF_LOOP_TIME,
|
||||
PlatformFramework,
|
||||
)
|
||||
|
||||
CODEOWNERS = ["@OttoWinter"]
|
||||
@@ -44,3 +46,21 @@ CONFIG_SCHEMA = cv.All(
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
|
||||
|
||||
FILTER_SOURCE_FILES = filter_source_files_from_platform(
|
||||
{
|
||||
"debug_esp32.cpp": {
|
||||
PlatformFramework.ESP32_ARDUINO,
|
||||
PlatformFramework.ESP32_IDF,
|
||||
},
|
||||
"debug_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO},
|
||||
"debug_host.cpp": {PlatformFramework.HOST_NATIVE},
|
||||
"debug_rp2040.cpp": {PlatformFramework.RP2040_ARDUINO},
|
||||
"debug_libretiny.cpp": {
|
||||
PlatformFramework.BK72XX_ARDUINO,
|
||||
PlatformFramework.RTL87XX_ARDUINO,
|
||||
PlatformFramework.LN882X_ARDUINO,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
from esphome import automation, pins
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import time
|
||||
from esphome.components import esp32, time
|
||||
from esphome.components.esp32 import get_esp32_variant
|
||||
from esphome.components.esp32.const import (
|
||||
VARIANT_ESP32,
|
||||
@@ -11,6 +11,7 @@ from esphome.components.esp32.const import (
|
||||
VARIANT_ESP32S2,
|
||||
VARIANT_ESP32S3,
|
||||
)
|
||||
from esphome.config_helpers import filter_source_files_from_platform
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_DEFAULT,
|
||||
@@ -27,6 +28,7 @@ from esphome.const import (
|
||||
CONF_WAKEUP_PIN,
|
||||
PLATFORM_ESP32,
|
||||
PLATFORM_ESP8266,
|
||||
PlatformFramework,
|
||||
)
|
||||
|
||||
WAKEUP_PINS = {
|
||||
@@ -114,12 +116,20 @@ def validate_pin_number(value):
|
||||
return value
|
||||
|
||||
|
||||
def validate_config(config):
|
||||
if get_esp32_variant() == VARIANT_ESP32C3 and CONF_ESP32_EXT1_WAKEUP in config:
|
||||
raise cv.Invalid("ESP32-C3 does not support wakeup from touch.")
|
||||
if get_esp32_variant() == VARIANT_ESP32C3 and CONF_TOUCH_WAKEUP in config:
|
||||
raise cv.Invalid("ESP32-C3 does not support wakeup from ext1")
|
||||
return config
|
||||
def _validate_ex1_wakeup_mode(value):
|
||||
if value == "ALL_LOW":
|
||||
esp32.only_on_variant(supported=[VARIANT_ESP32], msg_prefix="ALL_LOW")(value)
|
||||
if value == "ANY_LOW":
|
||||
esp32.only_on_variant(
|
||||
supported=[
|
||||
VARIANT_ESP32S2,
|
||||
VARIANT_ESP32S3,
|
||||
VARIANT_ESP32C6,
|
||||
VARIANT_ESP32H2,
|
||||
],
|
||||
msg_prefix="ANY_LOW",
|
||||
)(value)
|
||||
return value
|
||||
|
||||
|
||||
deep_sleep_ns = cg.esphome_ns.namespace("deep_sleep")
|
||||
@@ -146,6 +156,7 @@ WAKEUP_PIN_MODES = {
|
||||
esp_sleep_ext1_wakeup_mode_t = cg.global_ns.enum("esp_sleep_ext1_wakeup_mode_t")
|
||||
Ext1Wakeup = deep_sleep_ns.struct("Ext1Wakeup")
|
||||
EXT1_WAKEUP_MODES = {
|
||||
"ANY_LOW": esp_sleep_ext1_wakeup_mode_t.ESP_EXT1_WAKEUP_ANY_LOW,
|
||||
"ALL_LOW": esp_sleep_ext1_wakeup_mode_t.ESP_EXT1_WAKEUP_ALL_LOW,
|
||||
"ANY_HIGH": esp_sleep_ext1_wakeup_mode_t.ESP_EXT1_WAKEUP_ANY_HIGH,
|
||||
}
|
||||
@@ -185,16 +196,28 @@ CONFIG_SCHEMA = cv.All(
|
||||
),
|
||||
cv.Optional(CONF_ESP32_EXT1_WAKEUP): cv.All(
|
||||
cv.only_on_esp32,
|
||||
esp32.only_on_variant(
|
||||
unsupported=[VARIANT_ESP32C3], msg_prefix="Wakeup from ext1"
|
||||
),
|
||||
cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_PINS): cv.ensure_list(
|
||||
pins.internal_gpio_input_pin_schema, validate_pin_number
|
||||
),
|
||||
cv.Required(CONF_MODE): cv.enum(EXT1_WAKEUP_MODES, upper=True),
|
||||
cv.Required(CONF_MODE): cv.All(
|
||||
cv.enum(EXT1_WAKEUP_MODES, upper=True),
|
||||
_validate_ex1_wakeup_mode,
|
||||
),
|
||||
}
|
||||
),
|
||||
),
|
||||
cv.Optional(CONF_TOUCH_WAKEUP): cv.All(cv.only_on_esp32, cv.boolean),
|
||||
cv.Optional(CONF_TOUCH_WAKEUP): cv.All(
|
||||
cv.only_on_esp32,
|
||||
esp32.only_on_variant(
|
||||
unsupported=[VARIANT_ESP32C3], msg_prefix="Wakeup from touch"
|
||||
),
|
||||
cv.boolean,
|
||||
),
|
||||
}
|
||||
).extend(cv.COMPONENT_SCHEMA),
|
||||
cv.only_on([PLATFORM_ESP32, PLATFORM_ESP8266]),
|
||||
@@ -313,3 +336,14 @@ async def deep_sleep_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
|
||||
|
||||
|
||||
FILTER_SOURCE_FILES = filter_source_files_from_platform(
|
||||
{
|
||||
"deep_sleep_esp32.cpp": {
|
||||
PlatformFramework.ESP32_ARDUINO,
|
||||
PlatformFramework.ESP32_IDF,
|
||||
},
|
||||
"deep_sleep_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -189,7 +189,7 @@ def get_download_types(storage_json):
|
||||
]
|
||||
|
||||
|
||||
def only_on_variant(*, supported=None, unsupported=None):
|
||||
def only_on_variant(*, supported=None, unsupported=None, msg_prefix="This feature"):
|
||||
"""Config validator for features only available on some ESP32 variants."""
|
||||
if supported is not None and not isinstance(supported, list):
|
||||
supported = [supported]
|
||||
@@ -200,11 +200,11 @@ def only_on_variant(*, supported=None, unsupported=None):
|
||||
variant = get_esp32_variant()
|
||||
if supported is not None and variant not in supported:
|
||||
raise cv.Invalid(
|
||||
f"This feature is only available on {', '.join(supported)}"
|
||||
f"{msg_prefix} is only available on {', '.join(supported)}"
|
||||
)
|
||||
if unsupported is not None and variant in unsupported:
|
||||
raise cv.Invalid(
|
||||
f"This feature is not available on {', '.join(unsupported)}"
|
||||
f"{msg_prefix} is not available on {', '.join(unsupported)}"
|
||||
)
|
||||
return obj
|
||||
|
||||
|
||||
69
esphome/components/esp32/helpers.cpp
Normal file
69
esphome/components/esp32/helpers.cpp
Normal file
@@ -0,0 +1,69 @@
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
#ifdef USE_ESP32
|
||||
|
||||
#include "esp_efuse.h"
|
||||
#include "esp_efuse_table.h"
|
||||
#include "esp_mac.h"
|
||||
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/portmacro.h>
|
||||
#include "esp_random.h"
|
||||
#include "esp_system.h"
|
||||
|
||||
namespace esphome {
|
||||
|
||||
uint32_t random_uint32() { return esp_random(); }
|
||||
bool random_bytes(uint8_t *data, size_t len) {
|
||||
esp_fill_random(data, len);
|
||||
return true;
|
||||
}
|
||||
|
||||
Mutex::Mutex() { handle_ = xSemaphoreCreateMutex(); }
|
||||
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_); }
|
||||
|
||||
// only affects the executing core
|
||||
// so should not be used as a mutex lock, only to get accurate timing
|
||||
IRAM_ATTR InterruptLock::InterruptLock() { portDISABLE_INTERRUPTS(); }
|
||||
IRAM_ATTR InterruptLock::~InterruptLock() { portENABLE_INTERRUPTS(); }
|
||||
|
||||
void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
|
||||
#if defined(CONFIG_SOC_IEEE802154_SUPPORTED)
|
||||
// When CONFIG_SOC_IEEE802154_SUPPORTED is defined, esp_efuse_mac_get_default
|
||||
// returns the 802.15.4 EUI-64 address, so we read directly from eFuse instead.
|
||||
if (has_custom_mac_address()) {
|
||||
esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, 48);
|
||||
} else {
|
||||
esp_efuse_read_field_blob(ESP_EFUSE_MAC_FACTORY, mac, 48);
|
||||
}
|
||||
#else
|
||||
if (has_custom_mac_address()) {
|
||||
esp_efuse_mac_get_custom(mac);
|
||||
} else {
|
||||
esp_efuse_mac_get_default(mac);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
|
||||
void set_mac_address(uint8_t *mac) { esp_base_mac_addr_set(mac); }
|
||||
|
||||
bool has_custom_mac_address() {
|
||||
#if !defined(USE_ESP32_IGNORE_EFUSE_CUSTOM_MAC)
|
||||
uint8_t mac[6];
|
||||
// do not use 'esp_efuse_mac_get_custom(mac)' because it drops an error in the logs whenever it fails
|
||||
#ifndef USE_ESP32_VARIANT_ESP32
|
||||
return (esp_efuse_read_field_blob(ESP_EFUSE_USER_DATA_MAC_CUSTOM, mac, 48) == ESP_OK) && mac_address_is_valid(mac);
|
||||
#else
|
||||
return (esp_efuse_read_field_blob(ESP_EFUSE_MAC_CUSTOM, mac, 48) == ESP_OK) && mac_address_is_valid(mac);
|
||||
#endif
|
||||
#else
|
||||
return false;
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_ESP32
|
||||
@@ -25,10 +25,15 @@ namespace esphome {
|
||||
namespace esp32_ble {
|
||||
|
||||
// Maximum number of BLE scan results to buffer
|
||||
// Sized to handle bursts of advertisements while allowing for processing delays
|
||||
// With 16 advertisements per batch and some safety margin:
|
||||
// - Without PSRAM: 24 entries (1.5× batch size)
|
||||
// - With PSRAM: 36 entries (2.25× batch size)
|
||||
// The reduced structure size (~80 bytes vs ~400 bytes) allows for larger buffers
|
||||
#ifdef USE_PSRAM
|
||||
static constexpr uint8_t SCAN_RESULT_BUFFER_SIZE = 32;
|
||||
static constexpr uint8_t SCAN_RESULT_BUFFER_SIZE = 36;
|
||||
#else
|
||||
static constexpr uint8_t SCAN_RESULT_BUFFER_SIZE = 20;
|
||||
static constexpr uint8_t SCAN_RESULT_BUFFER_SIZE = 24;
|
||||
#endif
|
||||
|
||||
// Maximum size of the BLE event queue - must be power of 2 for lock-free queue
|
||||
@@ -51,7 +56,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 +146,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};
|
||||
};
|
||||
|
||||
31
esphome/components/esp8266/helpers.cpp
Normal file
31
esphome/components/esp8266/helpers.cpp
Normal file
@@ -0,0 +1,31 @@
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
#ifdef USE_ESP8266
|
||||
|
||||
#include <osapi.h>
|
||||
#include <user_interface.h>
|
||||
// for xt_rsil()/xt_wsr_ps()
|
||||
#include <Arduino.h>
|
||||
|
||||
namespace esphome {
|
||||
|
||||
uint32_t random_uint32() { return os_random(); }
|
||||
bool random_bytes(uint8_t *data, size_t len) { return os_get_random(data, len) == 0; }
|
||||
|
||||
// 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() {}
|
||||
void Mutex::lock() {}
|
||||
bool Mutex::try_lock() { return true; }
|
||||
void Mutex::unlock() {}
|
||||
|
||||
IRAM_ATTR InterruptLock::InterruptLock() { state_ = xt_rsil(15); }
|
||||
IRAM_ATTR InterruptLock::~InterruptLock() { xt_wsr_ps(state_); }
|
||||
|
||||
void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
|
||||
wifi_get_macaddr(STATION_IF, mac);
|
||||
}
|
||||
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_ESP8266
|
||||
0
esphome/components/gl_r01_i2c/__init__.py
Normal file
0
esphome/components/gl_r01_i2c/__init__.py
Normal file
68
esphome/components/gl_r01_i2c/gl_r01_i2c.cpp
Normal file
68
esphome/components/gl_r01_i2c/gl_r01_i2c.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/hal.h"
|
||||
#include "gl_r01_i2c.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace gl_r01_i2c {
|
||||
|
||||
static const char *const TAG = "gl_r01_i2c";
|
||||
|
||||
// Register definitions from datasheet
|
||||
static const uint8_t REG_VERSION = 0x00;
|
||||
static const uint8_t REG_DISTANCE = 0x02;
|
||||
static const uint8_t REG_TRIGGER = 0x10;
|
||||
static const uint8_t CMD_TRIGGER = 0xB0;
|
||||
static const uint8_t RESTART_CMD1 = 0x5A;
|
||||
static const uint8_t RESTART_CMD2 = 0xA5;
|
||||
static const uint8_t READ_DELAY = 40; // minimum milliseconds from datasheet to safely read measurement result
|
||||
|
||||
void GLR01I2CComponent::setup() {
|
||||
ESP_LOGCONFIG(TAG, "Setting up GL-R01 I2C...");
|
||||
// Verify sensor presence
|
||||
if (!this->read_byte_16(REG_VERSION, &this->version_)) {
|
||||
ESP_LOGE(TAG, "Failed to communicate with GL-R01 I2C sensor!");
|
||||
this->mark_failed();
|
||||
return;
|
||||
}
|
||||
ESP_LOGD(TAG, "Found GL-R01 I2C with version 0x%04X", this->version_);
|
||||
}
|
||||
|
||||
void GLR01I2CComponent::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "GL-R01 I2C:");
|
||||
ESP_LOGCONFIG(TAG, " Firmware Version: 0x%04X", this->version_);
|
||||
LOG_I2C_DEVICE(this);
|
||||
LOG_SENSOR(" ", "Distance", this);
|
||||
}
|
||||
|
||||
void GLR01I2CComponent::update() {
|
||||
// Trigger a new measurement
|
||||
if (!this->write_byte(REG_TRIGGER, CMD_TRIGGER)) {
|
||||
ESP_LOGE(TAG, "Failed to trigger measurement!");
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
|
||||
// Schedule reading the result after the read delay
|
||||
this->set_timeout(READ_DELAY, [this]() { this->read_distance_(); });
|
||||
}
|
||||
|
||||
void GLR01I2CComponent::read_distance_() {
|
||||
uint16_t distance = 0;
|
||||
if (!this->read_byte_16(REG_DISTANCE, &distance)) {
|
||||
ESP_LOGE(TAG, "Failed to read distance value!");
|
||||
this->status_set_warning();
|
||||
return;
|
||||
}
|
||||
|
||||
if (distance == 0xFFFF) {
|
||||
ESP_LOGW(TAG, "Invalid measurement received!");
|
||||
this->status_set_warning();
|
||||
} else {
|
||||
ESP_LOGV(TAG, "Distance: %umm", distance);
|
||||
this->publish_state(distance);
|
||||
this->status_clear_warning();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace gl_r01_i2c
|
||||
} // namespace esphome
|
||||
22
esphome/components/gl_r01_i2c/gl_r01_i2c.h
Normal file
22
esphome/components/gl_r01_i2c/gl_r01_i2c.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace gl_r01_i2c {
|
||||
|
||||
class GLR01I2CComponent : public sensor::Sensor, public i2c::I2CDevice, public PollingComponent {
|
||||
public:
|
||||
void setup() override;
|
||||
void dump_config() override;
|
||||
void update() override;
|
||||
|
||||
protected:
|
||||
void read_distance_();
|
||||
uint16_t version_{0};
|
||||
};
|
||||
|
||||
} // namespace gl_r01_i2c
|
||||
} // namespace esphome
|
||||
36
esphome/components/gl_r01_i2c/sensor.py
Normal file
36
esphome/components/gl_r01_i2c/sensor.py
Normal file
@@ -0,0 +1,36 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import i2c, sensor
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
DEVICE_CLASS_DISTANCE,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
UNIT_MILLIMETER,
|
||||
)
|
||||
|
||||
CODEOWNERS = ["@pkejval"]
|
||||
DEPENDENCIES = ["i2c"]
|
||||
|
||||
gl_r01_i2c_ns = cg.esphome_ns.namespace("gl_r01_i2c")
|
||||
GLR01I2CComponent = gl_r01_i2c_ns.class_(
|
||||
"GLR01I2CComponent", i2c.I2CDevice, cg.PollingComponent
|
||||
)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
sensor.sensor_schema(
|
||||
GLR01I2CComponent,
|
||||
unit_of_measurement=UNIT_MILLIMETER,
|
||||
accuracy_decimals=0,
|
||||
device_class=DEVICE_CLASS_DISTANCE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
)
|
||||
.extend(cv.polling_component_schema("60s"))
|
||||
.extend(i2c.i2c_device_schema(0x74))
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
await sensor.register_sensor(var, config)
|
||||
await i2c.register_i2c_device(var, config)
|
||||
57
esphome/components/host/helpers.cpp
Normal file
57
esphome/components/host/helpers.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
#ifdef USE_HOST
|
||||
|
||||
#ifndef _WIN32
|
||||
#include <net/if.h>
|
||||
#include <netinet/in.h>
|
||||
#include <sys/ioctl.h>
|
||||
#endif
|
||||
#include <unistd.h>
|
||||
#include <limits>
|
||||
#include <random>
|
||||
|
||||
#include "esphome/core/defines.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
|
||||
static const char *const TAG = "helpers.host";
|
||||
|
||||
uint32_t random_uint32() {
|
||||
std::random_device dev;
|
||||
std::mt19937 rng(dev());
|
||||
std::uniform_int_distribution<uint32_t> dist(0, std::numeric_limits<uint32_t>::max());
|
||||
return dist(rng);
|
||||
}
|
||||
|
||||
bool random_bytes(uint8_t *data, size_t len) {
|
||||
FILE *fp = fopen("/dev/urandom", "r");
|
||||
if (fp == nullptr) {
|
||||
ESP_LOGW(TAG, "Could not open /dev/urandom, errno=%d", errno);
|
||||
exit(1);
|
||||
}
|
||||
size_t read = fread(data, 1, len, fp);
|
||||
if (read != len) {
|
||||
ESP_LOGW(TAG, "Not enough data from /dev/urandom");
|
||||
exit(1);
|
||||
}
|
||||
fclose(fp);
|
||||
return true;
|
||||
}
|
||||
|
||||
// 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(); }
|
||||
|
||||
void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
|
||||
static const uint8_t esphome_host_mac_address[6] = USE_ESPHOME_HOST_MAC_ADDRESS;
|
||||
memcpy(mac, esphome_host_mac_address, sizeof(esphome_host_mac_address));
|
||||
}
|
||||
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_HOST
|
||||
@@ -2,6 +2,7 @@ from esphome import automation
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import esp32
|
||||
from esphome.components.const import CONF_REQUEST_HEADERS
|
||||
from esphome.config_helpers import filter_source_files_from_platform
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_ESP8266_DISABLE_SSL_SUPPORT,
|
||||
@@ -13,6 +14,7 @@ from esphome.const import (
|
||||
CONF_URL,
|
||||
CONF_WATCHDOG_TIMEOUT,
|
||||
PLATFORM_HOST,
|
||||
PlatformFramework,
|
||||
__version__,
|
||||
)
|
||||
from esphome.core import CORE, Lambda
|
||||
@@ -319,3 +321,19 @@ async def http_request_action_to_code(config, action_id, template_arg, args):
|
||||
await automation.build_automation(trigger, [], conf)
|
||||
|
||||
return var
|
||||
|
||||
|
||||
FILTER_SOURCE_FILES = filter_source_files_from_platform(
|
||||
{
|
||||
"http_request_host.cpp": {PlatformFramework.HOST_NATIVE},
|
||||
"http_request_arduino.cpp": {
|
||||
PlatformFramework.ESP32_ARDUINO,
|
||||
PlatformFramework.ESP8266_ARDUINO,
|
||||
PlatformFramework.RP2040_ARDUINO,
|
||||
PlatformFramework.BK72XX_ARDUINO,
|
||||
PlatformFramework.RTL87XX_ARDUINO,
|
||||
PlatformFramework.LN882X_ARDUINO,
|
||||
},
|
||||
"http_request_idf.cpp": {PlatformFramework.ESP32_IDF},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -111,8 +111,8 @@ CONFIG_SCHEMA = cv.All(
|
||||
cv.Optional(CONF_MOISTURE): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_INTENSITY,
|
||||
accuracy_decimals=0,
|
||||
device_class=DEVICE_CLASS_PRECIPITATION_INTENSITY,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
icon="mdi:weather-rainy",
|
||||
),
|
||||
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_CELSIUS,
|
||||
|
||||
@@ -3,6 +3,7 @@ import logging
|
||||
from esphome import pins
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import esp32
|
||||
from esphome.config_helpers import filter_source_files_from_platform
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_ADDRESS,
|
||||
@@ -18,6 +19,7 @@ from esphome.const import (
|
||||
PLATFORM_ESP32,
|
||||
PLATFORM_ESP8266,
|
||||
PLATFORM_RP2040,
|
||||
PlatformFramework,
|
||||
)
|
||||
from esphome.core import CORE, coroutine_with_priority
|
||||
import esphome.final_validate as fv
|
||||
@@ -205,3 +207,18 @@ def final_validate_device_schema(
|
||||
{cv.Required(CONF_I2C_ID): fv.id_declaration_match_schema(hub_schema)},
|
||||
extra=cv.ALLOW_EXTRA,
|
||||
)
|
||||
|
||||
|
||||
FILTER_SOURCE_FILES = filter_source_files_from_platform(
|
||||
{
|
||||
"i2c_bus_arduino.cpp": {
|
||||
PlatformFramework.ESP32_ARDUINO,
|
||||
PlatformFramework.ESP8266_ARDUINO,
|
||||
PlatformFramework.RP2040_ARDUINO,
|
||||
PlatformFramework.BK72XX_ARDUINO,
|
||||
PlatformFramework.RTL87XX_ARDUINO,
|
||||
PlatformFramework.LN882X_ARDUINO,
|
||||
},
|
||||
"i2c_bus_esp_idf.cpp": {PlatformFramework.ESP32_IDF},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -10,8 +10,10 @@ from PIL import Image, UnidentifiedImageError
|
||||
|
||||
from esphome import core, external_files
|
||||
import esphome.codegen as cg
|
||||
from esphome.components.const import CONF_BYTE_ORDER
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_DEFAULTS,
|
||||
CONF_DITHER,
|
||||
CONF_FILE,
|
||||
CONF_ICON,
|
||||
@@ -38,6 +40,7 @@ CONF_OPAQUE = "opaque"
|
||||
CONF_CHROMA_KEY = "chroma_key"
|
||||
CONF_ALPHA_CHANNEL = "alpha_channel"
|
||||
CONF_INVERT_ALPHA = "invert_alpha"
|
||||
CONF_IMAGES = "images"
|
||||
|
||||
TRANSPARENCY_TYPES = (
|
||||
CONF_OPAQUE,
|
||||
@@ -188,6 +191,10 @@ class ImageRGB565(ImageEncoder):
|
||||
dither,
|
||||
invert_alpha,
|
||||
)
|
||||
self.big_endian = True
|
||||
|
||||
def set_big_endian(self, big_endian: bool) -> None:
|
||||
self.big_endian = big_endian
|
||||
|
||||
def convert(self, image, path):
|
||||
return image.convert("RGBA")
|
||||
@@ -205,10 +212,16 @@ class ImageRGB565(ImageEncoder):
|
||||
g = 1
|
||||
b = 0
|
||||
rgb = (r << 11) | (g << 5) | b
|
||||
self.data[self.index] = rgb >> 8
|
||||
self.index += 1
|
||||
self.data[self.index] = rgb & 0xFF
|
||||
self.index += 1
|
||||
if self.big_endian:
|
||||
self.data[self.index] = rgb >> 8
|
||||
self.index += 1
|
||||
self.data[self.index] = rgb & 0xFF
|
||||
self.index += 1
|
||||
else:
|
||||
self.data[self.index] = rgb & 0xFF
|
||||
self.index += 1
|
||||
self.data[self.index] = rgb >> 8
|
||||
self.index += 1
|
||||
if self.transparency == CONF_ALPHA_CHANNEL:
|
||||
if self.invert_alpha:
|
||||
a ^= 0xFF
|
||||
@@ -364,7 +377,7 @@ def validate_file_shorthand(value):
|
||||
value = cv.string_strict(value)
|
||||
parts = value.strip().split(":")
|
||||
if len(parts) == 2 and parts[0] in MDI_SOURCES:
|
||||
match = re.match(r"[a-zA-Z0-9\-]+", parts[1])
|
||||
match = re.match(r"^[a-zA-Z0-9\-]+$", parts[1])
|
||||
if match is None:
|
||||
raise cv.Invalid(f"Could not parse mdi icon name from '{value}'.")
|
||||
return download_gh_svg(parts[1], parts[0])
|
||||
@@ -434,20 +447,29 @@ def validate_type(image_types):
|
||||
|
||||
|
||||
def validate_settings(value):
|
||||
type = value[CONF_TYPE]
|
||||
"""
|
||||
Validate the settings for a single image configuration.
|
||||
"""
|
||||
conf_type = value[CONF_TYPE]
|
||||
type_class = IMAGE_TYPE[conf_type]
|
||||
transparency = value[CONF_TRANSPARENCY].lower()
|
||||
allow_config = IMAGE_TYPE[type].allow_config
|
||||
if transparency not in allow_config:
|
||||
if transparency not in type_class.allow_config:
|
||||
raise cv.Invalid(
|
||||
f"Image format '{type}' cannot have transparency: {transparency}"
|
||||
f"Image format '{conf_type}' cannot have transparency: {transparency}"
|
||||
)
|
||||
invert_alpha = value.get(CONF_INVERT_ALPHA, False)
|
||||
if (
|
||||
invert_alpha
|
||||
and transparency != CONF_ALPHA_CHANNEL
|
||||
and CONF_INVERT_ALPHA not in allow_config
|
||||
and CONF_INVERT_ALPHA not in type_class.allow_config
|
||||
):
|
||||
raise cv.Invalid("No alpha channel to invert")
|
||||
if value.get(CONF_BYTE_ORDER) is not None and not callable(
|
||||
getattr(type_class, "set_big_endian", None)
|
||||
):
|
||||
raise cv.Invalid(
|
||||
f"Image format '{conf_type}' does not support byte order configuration"
|
||||
)
|
||||
if file := value.get(CONF_FILE):
|
||||
file = Path(file)
|
||||
if is_svg_file(file):
|
||||
@@ -456,31 +478,82 @@ def validate_settings(value):
|
||||
try:
|
||||
Image.open(file)
|
||||
except UnidentifiedImageError as exc:
|
||||
raise cv.Invalid(f"File can't be opened as image: {file}") from exc
|
||||
raise cv.Invalid(
|
||||
f"File can't be opened as image: {file.absolute()}"
|
||||
) from exc
|
||||
return value
|
||||
|
||||
|
||||
IMAGE_ID_SCHEMA = {
|
||||
cv.Required(CONF_ID): cv.declare_id(Image_),
|
||||
cv.Required(CONF_FILE): cv.Any(validate_file_shorthand, TYPED_FILE_SCHEMA),
|
||||
cv.GenerateID(CONF_RAW_DATA_ID): cv.declare_id(cg.uint8),
|
||||
}
|
||||
|
||||
|
||||
OPTIONS_SCHEMA = {
|
||||
cv.Optional(CONF_RESIZE): cv.dimensions,
|
||||
cv.Optional(CONF_DITHER, default="NONE"): cv.one_of(
|
||||
"NONE", "FLOYDSTEINBERG", upper=True
|
||||
),
|
||||
cv.Optional(CONF_INVERT_ALPHA, default=False): cv.boolean,
|
||||
cv.Optional(CONF_BYTE_ORDER): cv.one_of("BIG_ENDIAN", "LITTLE_ENDIAN", upper=True),
|
||||
cv.Optional(CONF_TRANSPARENCY, default=CONF_OPAQUE): validate_transparency(),
|
||||
cv.Optional(CONF_TYPE): validate_type(IMAGE_TYPE),
|
||||
}
|
||||
|
||||
OPTIONS = [key.schema for key in OPTIONS_SCHEMA]
|
||||
|
||||
# image schema with no defaults, used with `CONF_IMAGES` in the config
|
||||
IMAGE_SCHEMA_NO_DEFAULTS = {
|
||||
**IMAGE_ID_SCHEMA,
|
||||
**{cv.Optional(key): OPTIONS_SCHEMA[key] for key in OPTIONS},
|
||||
}
|
||||
|
||||
BASE_SCHEMA = cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_ID): cv.declare_id(Image_),
|
||||
cv.Required(CONF_FILE): cv.Any(validate_file_shorthand, TYPED_FILE_SCHEMA),
|
||||
cv.Optional(CONF_RESIZE): cv.dimensions,
|
||||
cv.Optional(CONF_DITHER, default="NONE"): cv.one_of(
|
||||
"NONE", "FLOYDSTEINBERG", upper=True
|
||||
),
|
||||
cv.Optional(CONF_INVERT_ALPHA, default=False): cv.boolean,
|
||||
cv.GenerateID(CONF_RAW_DATA_ID): cv.declare_id(cg.uint8),
|
||||
**IMAGE_ID_SCHEMA,
|
||||
**OPTIONS_SCHEMA,
|
||||
}
|
||||
).add_extra(validate_settings)
|
||||
|
||||
IMAGE_SCHEMA = BASE_SCHEMA.extend(
|
||||
{
|
||||
cv.Required(CONF_TYPE): validate_type(IMAGE_TYPE),
|
||||
cv.Optional(CONF_TRANSPARENCY, default=CONF_OPAQUE): validate_transparency(),
|
||||
}
|
||||
)
|
||||
|
||||
|
||||
def validate_defaults(value):
|
||||
"""
|
||||
Validate the options for images with defaults
|
||||
"""
|
||||
defaults = value[CONF_DEFAULTS]
|
||||
result = []
|
||||
for index, image in enumerate(value[CONF_IMAGES]):
|
||||
type = image.get(CONF_TYPE, defaults.get(CONF_TYPE))
|
||||
if type is None:
|
||||
raise cv.Invalid(
|
||||
"Type is required either in the image config or in the defaults",
|
||||
path=[CONF_IMAGES, index],
|
||||
)
|
||||
type_class = IMAGE_TYPE[type]
|
||||
# A default byte order should be simply ignored if the type does not support it
|
||||
available_options = [*OPTIONS]
|
||||
if (
|
||||
not callable(getattr(type_class, "set_big_endian", None))
|
||||
and CONF_BYTE_ORDER not in image
|
||||
):
|
||||
available_options.remove(CONF_BYTE_ORDER)
|
||||
config = {
|
||||
**{key: image.get(key, defaults.get(key)) for key in available_options},
|
||||
**{key.schema: image[key.schema] for key in IMAGE_ID_SCHEMA},
|
||||
}
|
||||
validate_settings(config)
|
||||
result.append(config)
|
||||
return result
|
||||
|
||||
|
||||
def typed_image_schema(image_type):
|
||||
"""
|
||||
Construct a schema for a specific image type, allowing transparency options
|
||||
@@ -523,10 +596,33 @@ def typed_image_schema(image_type):
|
||||
|
||||
# The config schema can be a (possibly empty) single list of images,
|
||||
# or a dictionary of image types each with a list of images
|
||||
CONFIG_SCHEMA = cv.Any(
|
||||
cv.Schema({cv.Optional(t.lower()): typed_image_schema(t) for t in IMAGE_TYPE}),
|
||||
cv.ensure_list(IMAGE_SCHEMA),
|
||||
)
|
||||
# or a dictionary with keys `defaults:` and `images:`
|
||||
|
||||
|
||||
def _config_schema(config):
|
||||
if isinstance(config, list):
|
||||
return cv.Schema([IMAGE_SCHEMA])(config)
|
||||
if not isinstance(config, dict):
|
||||
raise cv.Invalid(
|
||||
"Badly formed image configuration, expected a list or a dictionary"
|
||||
)
|
||||
if CONF_DEFAULTS in config or CONF_IMAGES in config:
|
||||
return validate_defaults(
|
||||
cv.Schema(
|
||||
{
|
||||
cv.Required(CONF_DEFAULTS): OPTIONS_SCHEMA,
|
||||
cv.Required(CONF_IMAGES): cv.ensure_list(IMAGE_SCHEMA_NO_DEFAULTS),
|
||||
}
|
||||
)(config)
|
||||
)
|
||||
if CONF_ID in config or CONF_FILE in config:
|
||||
return cv.ensure_list(IMAGE_SCHEMA)([config])
|
||||
return cv.Schema(
|
||||
{cv.Optional(t.lower()): typed_image_schema(t) for t in IMAGE_TYPE}
|
||||
)(config)
|
||||
|
||||
|
||||
CONFIG_SCHEMA = _config_schema
|
||||
|
||||
|
||||
async def write_image(config, all_frames=False):
|
||||
@@ -585,6 +681,9 @@ async def write_image(config, all_frames=False):
|
||||
|
||||
total_rows = height * frame_count
|
||||
encoder = IMAGE_TYPE[type](width, total_rows, transparency, dither, invert_alpha)
|
||||
if byte_order := config.get(CONF_BYTE_ORDER):
|
||||
# Check for valid type has already been done in validate_settings
|
||||
encoder.set_big_endian(byte_order == "BIG_ENDIAN")
|
||||
for frame_index in range(frame_count):
|
||||
image.seek(frame_index)
|
||||
pixels = encoder.convert(image.resize((width, height)), path).getdata()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from esphome import pins
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import display, i2c
|
||||
from esphome.components.esp32 import CONF_CPU_FREQUENCY
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_FULL_UPDATE_EVERY,
|
||||
@@ -13,7 +14,9 @@ from esphome.const import (
|
||||
CONF_PAGES,
|
||||
CONF_TRANSFORM,
|
||||
CONF_WAKEUP_PIN,
|
||||
PLATFORM_ESP32,
|
||||
)
|
||||
import esphome.final_validate as fv
|
||||
|
||||
DEPENDENCIES = ["i2c", "esp32"]
|
||||
AUTO_LOAD = ["psram"]
|
||||
@@ -120,6 +123,18 @@ CONFIG_SCHEMA = cv.All(
|
||||
)
|
||||
|
||||
|
||||
def _validate_cpu_frequency(config):
|
||||
esp32_config = fv.full_config.get()[PLATFORM_ESP32]
|
||||
if esp32_config[CONF_CPU_FREQUENCY] != "240MHZ":
|
||||
raise cv.Invalid(
|
||||
"Inkplate requires 240MHz CPU frequency (set in esp32 component)"
|
||||
)
|
||||
return config
|
||||
|
||||
|
||||
FINAL_VALIDATE_SCHEMA = _validate_cpu_frequency
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
|
||||
|
||||
@@ -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
|
||||
};
|
||||
|
||||
|
||||
@@ -13,13 +13,13 @@ from esphome.const import (
|
||||
|
||||
from .. import CONF_LD2450_ID, LD2450Component, ld2450_ns
|
||||
|
||||
ResetButton = ld2450_ns.class_("ResetButton", button.Button)
|
||||
FactoryResetButton = ld2450_ns.class_("FactoryResetButton", button.Button)
|
||||
RestartButton = ld2450_ns.class_("RestartButton", button.Button)
|
||||
|
||||
CONFIG_SCHEMA = {
|
||||
cv.GenerateID(CONF_LD2450_ID): cv.use_id(LD2450Component),
|
||||
cv.Optional(CONF_FACTORY_RESET): button.button_schema(
|
||||
ResetButton,
|
||||
FactoryResetButton,
|
||||
device_class=DEVICE_CLASS_RESTART,
|
||||
entity_category=ENTITY_CATEGORY_CONFIG,
|
||||
icon=ICON_RESTART_ALERT,
|
||||
@@ -38,7 +38,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_LD2450_ID])
|
||||
cg.add(ld2450_component.set_reset_button(b))
|
||||
cg.add(ld2450_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_LD2450_ID])
|
||||
|
||||
@@ -0,0 +1,9 @@
|
||||
#include "factory_reset_button.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ld2450 {
|
||||
|
||||
void FactoryResetButton::press_action() { this->parent_->factory_reset(); }
|
||||
|
||||
} // namespace ld2450
|
||||
} // namespace esphome
|
||||
@@ -6,9 +6,9 @@
|
||||
namespace esphome {
|
||||
namespace ld2450 {
|
||||
|
||||
class ResetButton : public button::Button, public Parented<LD2450Component> {
|
||||
class FactoryResetButton : public button::Button, public Parented<LD2450Component> {
|
||||
public:
|
||||
ResetButton() = default;
|
||||
FactoryResetButton() = default;
|
||||
|
||||
protected:
|
||||
void press_action() override;
|
||||
@@ -1,9 +0,0 @@
|
||||
#include "reset_button.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace ld2450 {
|
||||
|
||||
void ResetButton::press_action() { this->parent_->factory_reset(); }
|
||||
|
||||
} // namespace ld2450
|
||||
} // namespace esphome
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "ld2450.h"
|
||||
#include <utility>
|
||||
#include <cmath>
|
||||
#ifdef USE_NUMBER
|
||||
#include "esphome/components/number/number.h"
|
||||
#endif
|
||||
@@ -17,11 +18,10 @@ namespace esphome {
|
||||
namespace ld2450 {
|
||||
|
||||
static const char *const TAG = "ld2450";
|
||||
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,
|
||||
@@ -32,14 +32,13 @@ enum BaudRateStructure : uint8_t {
|
||||
BAUD_RATE_460800 = 8
|
||||
};
|
||||
|
||||
// Zone type struct
|
||||
enum ZoneTypeStructure : uint8_t {
|
||||
enum ZoneType : uint8_t {
|
||||
ZONE_DISABLED = 0,
|
||||
ZONE_DETECTION = 1,
|
||||
ZONE_FILTER = 2,
|
||||
};
|
||||
|
||||
enum PeriodicDataStructure : uint8_t {
|
||||
enum PeriodicData : uint8_t {
|
||||
TARGET_X = 4,
|
||||
TARGET_Y = 6,
|
||||
TARGET_SPEED = 8,
|
||||
@@ -47,12 +46,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,
|
||||
};
|
||||
@@ -60,11 +59,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;
|
||||
};
|
||||
|
||||
@@ -74,6 +73,13 @@ constexpr StringToUint8 BAUD_RATES_BY_STR[] = {
|
||||
{"256000", BAUD_RATE_256000}, {"460800", BAUD_RATE_460800},
|
||||
};
|
||||
|
||||
constexpr Uint8ToString DIRECTION_BY_UINT[] = {
|
||||
{DIRECTION_APPROACHING, "Approaching"},
|
||||
{DIRECTION_MOVING_AWAY, "Moving away"},
|
||||
{DIRECTION_STATIONARY, "Stationary"},
|
||||
{DIRECTION_NA, "NA"},
|
||||
};
|
||||
|
||||
constexpr Uint8ToString ZONE_TYPE_BY_UINT[] = {
|
||||
{ZONE_DISABLED, "Disabled"},
|
||||
{ZONE_DETECTION, "Detection"},
|
||||
@@ -103,36 +109,38 @@ template<size_t N> const char *find_str(const Uint8ToString (&arr)[N], uint8_t v
|
||||
return ""; // Not found
|
||||
}
|
||||
|
||||
// LD2450 serial 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};
|
||||
// LD2450 UART Serial Commands
|
||||
static const uint8_t CMD_ENABLE_CONF = 0xFF;
|
||||
static const uint8_t CMD_DISABLE_CONF = 0xFE;
|
||||
static const uint8_t CMD_VERSION = 0xA0;
|
||||
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 const uint8_t CMD_SINGLE_TARGET_MODE = 0x80;
|
||||
static const uint8_t CMD_MULTI_TARGET_MODE = 0x90;
|
||||
static const uint8_t CMD_QUERY_TARGET_MODE = 0x91;
|
||||
static const uint8_t CMD_SET_BAUD_RATE = 0xA1;
|
||||
static const uint8_t CMD_QUERY_ZONE = 0xC1;
|
||||
static const uint8_t CMD_SET_ZONE = 0xC2;
|
||||
static constexpr uint8_t CMD_ENABLE_CONF = 0xFF;
|
||||
static constexpr uint8_t CMD_DISABLE_CONF = 0xFE;
|
||||
static constexpr uint8_t CMD_QUERY_VERSION = 0xA0;
|
||||
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;
|
||||
static constexpr uint8_t CMD_SINGLE_TARGET_MODE = 0x80;
|
||||
static constexpr uint8_t CMD_MULTI_TARGET_MODE = 0x90;
|
||||
static constexpr uint8_t CMD_QUERY_TARGET_MODE = 0x91;
|
||||
static constexpr uint8_t CMD_SET_BAUD_RATE = 0xA1;
|
||||
static constexpr uint8_t CMD_QUERY_ZONE = 0xC1;
|
||||
static constexpr uint8_t CMD_SET_ZONE = 0xC2;
|
||||
// Header & Footer size
|
||||
static constexpr uint8_t HEADER_FOOTER_SIZE = 4;
|
||||
// Command Header & Footer
|
||||
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 constexpr uint8_t DATA_FRAME_HEADER[HEADER_FOOTER_SIZE] = {0xAA, 0xFF, 0x03, 0x00};
|
||||
static constexpr uint8_t DATA_FRAME_FOOTER[2] = {0x55, 0xCC};
|
||||
// MAC address the module uses when Bluetooth is disabled
|
||||
static constexpr uint8_t NO_MAC[] = {0x08, 0x05, 0x04, 0x03, 0x02, 0x01};
|
||||
|
||||
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
|
||||
for (uint8_t i = 0; i < 4; i++) {
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
@@ -170,18 +178,13 @@ static inline float calculate_angle(float base, float hypotenuse) {
|
||||
return angle_degrees;
|
||||
}
|
||||
|
||||
static inline std::string get_direction(int16_t speed) {
|
||||
static const char *const APPROACHING = "Approaching";
|
||||
static const char *const MOVING_AWAY = "Moving away";
|
||||
static const char *const STATIONARY = "Stationary";
|
||||
|
||||
if (speed > 0) {
|
||||
return MOVING_AWAY;
|
||||
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
|
||||
}
|
||||
}
|
||||
if (speed < 0) {
|
||||
return APPROACHING;
|
||||
}
|
||||
return STATIONARY;
|
||||
return true; // Valid header/footer
|
||||
}
|
||||
|
||||
void LD2450Component::setup() {
|
||||
@@ -196,84 +199,93 @@ void LD2450Component::setup() {
|
||||
}
|
||||
|
||||
void LD2450Component::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "LD2450:");
|
||||
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,
|
||||
"LD2450:\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_);
|
||||
#endif
|
||||
#ifdef USE_SWITCH
|
||||
LOG_SWITCH(" ", "BluetoothSwitch", this->bluetooth_switch_);
|
||||
LOG_SWITCH(" ", "MultiTargetSwitch", this->multi_target_switch_);
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
LOG_BUTTON(" ", "ResetButton", this->reset_button_);
|
||||
LOG_BUTTON(" ", "RestartButton", this->restart_button_);
|
||||
ESP_LOGCONFIG(TAG, "Binary Sensors:");
|
||||
LOG_BINARY_SENSOR(" ", "MovingTarget", this->moving_target_binary_sensor_);
|
||||
LOG_BINARY_SENSOR(" ", "StillTarget", this->still_target_binary_sensor_);
|
||||
LOG_BINARY_SENSOR(" ", "Target", this->target_binary_sensor_);
|
||||
#endif
|
||||
#ifdef USE_SENSOR
|
||||
LOG_SENSOR(" ", "TargetCountSensor", this->target_count_sensor_);
|
||||
LOG_SENSOR(" ", "StillTargetCountSensor", this->still_target_count_sensor_);
|
||||
LOG_SENSOR(" ", "MovingTargetCountSensor", this->moving_target_count_sensor_);
|
||||
ESP_LOGCONFIG(TAG, "Sensors:");
|
||||
LOG_SENSOR(" ", "MovingTargetCount", this->moving_target_count_sensor_);
|
||||
LOG_SENSOR(" ", "StillTargetCount", this->still_target_count_sensor_);
|
||||
LOG_SENSOR(" ", "TargetCount", this->target_count_sensor_);
|
||||
for (sensor::Sensor *s : this->move_x_sensors_) {
|
||||
LOG_SENSOR(" ", "NthTargetXSensor", s);
|
||||
LOG_SENSOR(" ", "TargetX", s);
|
||||
}
|
||||
for (sensor::Sensor *s : this->move_y_sensors_) {
|
||||
LOG_SENSOR(" ", "NthTargetYSensor", s);
|
||||
}
|
||||
for (sensor::Sensor *s : this->move_speed_sensors_) {
|
||||
LOG_SENSOR(" ", "NthTargetSpeedSensor", s);
|
||||
LOG_SENSOR(" ", "TargetY", s);
|
||||
}
|
||||
for (sensor::Sensor *s : this->move_angle_sensors_) {
|
||||
LOG_SENSOR(" ", "NthTargetAngleSensor", s);
|
||||
LOG_SENSOR(" ", "TargetAngle", s);
|
||||
}
|
||||
for (sensor::Sensor *s : this->move_distance_sensors_) {
|
||||
LOG_SENSOR(" ", "NthTargetDistanceSensor", s);
|
||||
LOG_SENSOR(" ", "TargetDistance", s);
|
||||
}
|
||||
for (sensor::Sensor *s : this->move_resolution_sensors_) {
|
||||
LOG_SENSOR(" ", "NthTargetResolutionSensor", s);
|
||||
LOG_SENSOR(" ", "TargetResolution", s);
|
||||
}
|
||||
for (sensor::Sensor *s : this->move_speed_sensors_) {
|
||||
LOG_SENSOR(" ", "TargetSpeed", s);
|
||||
}
|
||||
for (sensor::Sensor *s : this->zone_target_count_sensors_) {
|
||||
LOG_SENSOR(" ", "NthZoneTargetCountSensor", s);
|
||||
}
|
||||
for (sensor::Sensor *s : this->zone_still_target_count_sensors_) {
|
||||
LOG_SENSOR(" ", "NthZoneStillTargetCountSensor", s);
|
||||
LOG_SENSOR(" ", "ZoneTargetCount", s);
|
||||
}
|
||||
for (sensor::Sensor *s : this->zone_moving_target_count_sensors_) {
|
||||
LOG_SENSOR(" ", "NthZoneMovingTargetCountSensor", s);
|
||||
LOG_SENSOR(" ", "ZoneMovingTargetCount", s);
|
||||
}
|
||||
for (sensor::Sensor *s : this->zone_still_target_count_sensors_) {
|
||||
LOG_SENSOR(" ", "ZoneStillTargetCount", s);
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
LOG_TEXT_SENSOR(" ", "VersionTextSensor", this->version_text_sensor_);
|
||||
LOG_TEXT_SENSOR(" ", "MacTextSensor", this->mac_text_sensor_);
|
||||
ESP_LOGCONFIG(TAG, "Text Sensors:");
|
||||
LOG_TEXT_SENSOR(" ", "Version", this->version_text_sensor_);
|
||||
LOG_TEXT_SENSOR(" ", "Mac", this->mac_text_sensor_);
|
||||
for (text_sensor::TextSensor *s : this->direction_text_sensors_) {
|
||||
LOG_TEXT_SENSOR(" ", "NthDirectionTextSensor", s);
|
||||
LOG_TEXT_SENSOR(" ", "Direction", s);
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_NUMBER
|
||||
ESP_LOGCONFIG(TAG, "Numbers:");
|
||||
LOG_NUMBER(" ", "PresenceTimeout", this->presence_timeout_number_);
|
||||
for (auto n : this->zone_numbers_) {
|
||||
LOG_NUMBER(" ", "ZoneX1Number", n.x1);
|
||||
LOG_NUMBER(" ", "ZoneY1Number", n.y1);
|
||||
LOG_NUMBER(" ", "ZoneX2Number", n.x2);
|
||||
LOG_NUMBER(" ", "ZoneY2Number", n.y2);
|
||||
LOG_NUMBER(" ", "ZoneX1", n.x1);
|
||||
LOG_NUMBER(" ", "ZoneY1", n.y1);
|
||||
LOG_NUMBER(" ", "ZoneX2", n.x2);
|
||||
LOG_NUMBER(" ", "ZoneY2", n.y2);
|
||||
}
|
||||
#endif
|
||||
#ifdef USE_SELECT
|
||||
LOG_SELECT(" ", "BaudRateSelect", this->baud_rate_select_);
|
||||
LOG_SELECT(" ", "ZoneTypeSelect", this->zone_type_select_);
|
||||
ESP_LOGCONFIG(TAG, "Selects:");
|
||||
LOG_SELECT(" ", "BaudRate", this->baud_rate_select_);
|
||||
LOG_SELECT(" ", "ZoneType", this->zone_type_select_);
|
||||
#endif
|
||||
#ifdef USE_NUMBER
|
||||
LOG_NUMBER(" ", "PresenceTimeoutNumber", this->presence_timeout_number_);
|
||||
#ifdef USE_SWITCH
|
||||
ESP_LOGCONFIG(TAG, "Switches:");
|
||||
LOG_SWITCH(" ", "Bluetooth", this->bluetooth_switch_);
|
||||
LOG_SWITCH(" ", "MultiTarget", this->multi_target_switch_);
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
ESP_LOGCONFIG(TAG, "Buttons:");
|
||||
LOG_BUTTON(" ", "FactoryReset", this->factory_reset_button_);
|
||||
LOG_BUTTON(" ", "Restart", this->restart_button_);
|
||||
#endif
|
||||
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());
|
||||
}
|
||||
|
||||
void LD2450Component::loop() {
|
||||
while (this->available()) {
|
||||
this->readline_(read(), this->buffer_data_, MAX_LINE_LENGTH);
|
||||
this->readline_(this->read());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -308,7 +320,7 @@ void LD2450Component::set_radar_zone(int32_t zone_type, int32_t zone1_x1, int32_
|
||||
this->zone_type_ = zone_type;
|
||||
int zone_parameters[12] = {zone1_x1, zone1_y1, zone1_x2, zone1_y2, zone2_x1, zone2_y1,
|
||||
zone2_x2, zone2_y2, zone3_x1, zone3_y1, zone3_x2, zone3_y2};
|
||||
for (int i = 0; i < MAX_ZONES; i++) {
|
||||
for (uint8_t i = 0; i < MAX_ZONES; i++) {
|
||||
this->zone_config_[i].x1 = zone_parameters[i * 4];
|
||||
this->zone_config_[i].y1 = zone_parameters[i * 4 + 1];
|
||||
this->zone_config_[i].x2 = zone_parameters[i * 4 + 2];
|
||||
@@ -322,15 +334,15 @@ void LD2450Component::send_set_zone_command_() {
|
||||
uint8_t cmd_value[26] = {};
|
||||
uint8_t zone_type_bytes[2] = {static_cast<uint8_t>(this->zone_type_), 0x00};
|
||||
uint8_t area_config[24] = {};
|
||||
for (int i = 0; i < MAX_ZONES; i++) {
|
||||
for (uint8_t i = 0; i < MAX_ZONES; i++) {
|
||||
int values[4] = {this->zone_config_[i].x1, this->zone_config_[i].y1, this->zone_config_[i].x2,
|
||||
this->zone_config_[i].y2};
|
||||
ld2450::convert_int_values_to_hex(values, area_config + (i * 8));
|
||||
}
|
||||
std::memcpy(cmd_value, zone_type_bytes, 2);
|
||||
std::memcpy(cmd_value + 2, area_config, 24);
|
||||
std::memcpy(cmd_value, zone_type_bytes, sizeof(zone_type_bytes));
|
||||
std::memcpy(cmd_value + 2, area_config, sizeof(area_config));
|
||||
this->set_config_mode_(true);
|
||||
this->send_command_(CMD_SET_ZONE, cmd_value, 26);
|
||||
this->send_command_(CMD_SET_ZONE, cmd_value, sizeof(cmd_value));
|
||||
this->set_config_mode_(false);
|
||||
}
|
||||
|
||||
@@ -346,14 +358,14 @@ bool LD2450Component::get_timeout_status_(uint32_t check_millis) {
|
||||
}
|
||||
|
||||
// Extract, store and publish zone details LD2450 buffer
|
||||
void LD2450Component::process_zone_(uint8_t *buffer) {
|
||||
void LD2450Component::process_zone_() {
|
||||
uint8_t index, start;
|
||||
for (index = 0; index < MAX_ZONES; index++) {
|
||||
start = 12 + index * 8;
|
||||
this->zone_config_[index].x1 = ld2450::hex_to_signed_int(buffer, start);
|
||||
this->zone_config_[index].y1 = ld2450::hex_to_signed_int(buffer, start + 2);
|
||||
this->zone_config_[index].x2 = ld2450::hex_to_signed_int(buffer, start + 4);
|
||||
this->zone_config_[index].y2 = ld2450::hex_to_signed_int(buffer, start + 6);
|
||||
this->zone_config_[index].x1 = ld2450::hex_to_signed_int(this->buffer_data_, start);
|
||||
this->zone_config_[index].y1 = ld2450::hex_to_signed_int(this->buffer_data_, start + 2);
|
||||
this->zone_config_[index].x2 = ld2450::hex_to_signed_int(this->buffer_data_, start + 4);
|
||||
this->zone_config_[index].y2 = ld2450::hex_to_signed_int(this->buffer_data_, start + 6);
|
||||
#ifdef USE_NUMBER
|
||||
// only one null check as all coordinates are required for a single zone
|
||||
if (this->zone_numbers_[index].x1 != nullptr) {
|
||||
@@ -399,27 +411,25 @@ void LD2450Component::restart_and_read_all_info() {
|
||||
|
||||
// Send command with values to LD2450
|
||||
void LD2450Component::send_command_(uint8_t command, const uint8_t *command_value, uint8_t command_value_len) {
|
||||
ESP_LOGV(TAG, "Sending command %02X", command);
|
||||
// frame header
|
||||
this->write_array(CMD_FRAME_HEADER, 4);
|
||||
ESP_LOGV(TAG, "Sending COMMAND %02X", command);
|
||||
// frame header bytes
|
||||
this->write_array(CMD_FRAME_HEADER, sizeof(CMD_FRAME_HEADER));
|
||||
// length bytes
|
||||
int len = 2;
|
||||
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]);
|
||||
}
|
||||
}
|
||||
// footer
|
||||
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
|
||||
}
|
||||
@@ -427,25 +437,23 @@ void LD2450Component::send_command_(uint8_t command, const uint8_t *command_valu
|
||||
// LD2450 Radar data message:
|
||||
// [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) {
|
||||
if (len < 29) { // header (4 bytes) + 8 x 3 target data + footer (2 bytes)
|
||||
ESP_LOGE(TAG, "Invalid message length");
|
||||
return;
|
||||
}
|
||||
if (buffer[0] != 0xAA || buffer[1] != 0xFF || buffer[2] != 0x03 || buffer[3] != 0x00) { // header
|
||||
ESP_LOGE(TAG, "Invalid message header");
|
||||
return;
|
||||
}
|
||||
if (buffer[len - 2] != 0x55 || buffer[len - 1] != 0xCC) { // footer
|
||||
ESP_LOGE(TAG, "Invalid message footer");
|
||||
return;
|
||||
}
|
||||
|
||||
void LD2450Component::handle_periodic_data_() {
|
||||
// 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 (this->buffer_pos_ < 29) { // header (4 bytes) + 8 x 3 target data + footer (2 bytes)
|
||||
ESP_LOGE(TAG, "Invalid length");
|
||||
return;
|
||||
}
|
||||
if (!ld2450::validate_header_footer(DATA_FRAME_HEADER, this->buffer_data_) ||
|
||||
this->buffer_data_[this->buffer_pos_ - 2] != DATA_FRAME_FOOTER[0] ||
|
||||
this->buffer_data_[this->buffer_pos_ - 1] != DATA_FRAME_FOOTER[1]) {
|
||||
ESP_LOGE(TAG, "Invalid header/footer");
|
||||
return;
|
||||
}
|
||||
// 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();
|
||||
|
||||
int16_t target_count = 0;
|
||||
@@ -453,13 +461,13 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) {
|
||||
int16_t moving_target_count = 0;
|
||||
int16_t start = 0;
|
||||
int16_t val = 0;
|
||||
uint8_t index = 0;
|
||||
int16_t tx = 0;
|
||||
int16_t ty = 0;
|
||||
int16_t td = 0;
|
||||
int16_t ts = 0;
|
||||
int16_t angle = 0;
|
||||
std::string direction{};
|
||||
uint8_t index = 0;
|
||||
Direction direction{DIRECTION_UNDEFINED};
|
||||
bool is_moving = false;
|
||||
|
||||
#if defined(USE_BINARY_SENSOR) || defined(USE_SENSOR) || defined(USE_TEXT_SENSOR)
|
||||
@@ -471,29 +479,38 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) {
|
||||
is_moving = false;
|
||||
sensor::Sensor *sx = this->move_x_sensors_[index];
|
||||
if (sx != nullptr) {
|
||||
val = ld2450::decode_coordinate(buffer[start], buffer[start + 1]);
|
||||
val = ld2450::decode_coordinate(this->buffer_data_[start], this->buffer_data_[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;
|
||||
sensor::Sensor *sy = this->move_y_sensors_[index];
|
||||
if (sy != nullptr) {
|
||||
val = ld2450::decode_coordinate(buffer[start], buffer[start + 1]);
|
||||
val = ld2450::decode_coordinate(this->buffer_data_[start], this->buffer_data_[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);
|
||||
val = (this->buffer_data_[start + 1] << 8) | this->buffer_data_[start];
|
||||
if (this->cached_target_data_[index].resolution != val) {
|
||||
sr->publish_state(val);
|
||||
this->cached_target_data_[index].resolution = val;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
// SPEED
|
||||
start = TARGET_SPEED + index * 8;
|
||||
val = ld2450::decode_speed(buffer[start], buffer[start + 1]);
|
||||
val = ld2450::decode_speed(this->buffer_data_[start], this->buffer_data_[start + 1]);
|
||||
ts = val;
|
||||
if (val) {
|
||||
is_moving = true;
|
||||
@@ -502,13 +519,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,27 +537,42 @@ 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));
|
||||
angle = ld2450::calculate_angle(static_cast<float>(ty), static_cast<float>(td));
|
||||
if (tx > 0) {
|
||||
angle = angle * -1;
|
||||
}
|
||||
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
|
||||
// DIRECTION
|
||||
direction = get_direction(ts);
|
||||
if (td == 0) {
|
||||
direction = "NA";
|
||||
direction = DIRECTION_NA;
|
||||
} else if (ts > 0) {
|
||||
direction = DIRECTION_MOVING_AWAY;
|
||||
} else if (ts < 0) {
|
||||
direction = DIRECTION_APPROACHING;
|
||||
} else {
|
||||
direction = DIRECTION_STATIONARY;
|
||||
}
|
||||
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(find_str(ld2450::DIRECTION_BY_UINT, direction));
|
||||
this->cached_target_data_[index].direction = direction;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -563,32 +599,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
|
||||
|
||||
@@ -640,117 +694,139 @@ void LD2450Component::handle_periodic_data_(uint8_t *buffer, uint8_t len) {
|
||||
#endif
|
||||
}
|
||||
|
||||
bool LD2450Component::handle_ack_data_(uint8_t *buffer, uint8_t len) {
|
||||
ESP_LOGV(TAG, "Handling ack data for command %02X", buffer[COMMAND]);
|
||||
if (len < 10) {
|
||||
ESP_LOGE(TAG, "Invalid ack length");
|
||||
bool LD2450Component::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) { // frame header
|
||||
ESP_LOGE(TAG, "Invalid ack header (command %02X)", buffer[COMMAND]);
|
||||
if (!ld2450::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) {
|
||||
ESP_LOGE(TAG, "Invalid ack status");
|
||||
if (this->buffer_data_[COMMAND_STATUS] != 0x01) {
|
||||
ESP_LOGE(TAG, "Invalid status");
|
||||
return true;
|
||||
}
|
||||
if (buffer[8] || buffer[9]) {
|
||||
ESP_LOGE(TAG, "Last buffer was %u, %u", buffer[8], buffer[9]);
|
||||
if (this->buffer_data_[8] || this->buffer_data_[9]) {
|
||||
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):
|
||||
ESP_LOGV(TAG, "Enable conf command");
|
||||
switch (this->buffer_data_[COMMAND]) {
|
||||
case CMD_ENABLE_CONF:
|
||||
ESP_LOGV(TAG, "Enable conf");
|
||||
break;
|
||||
case lowbyte(CMD_DISABLE_CONF):
|
||||
ESP_LOGV(TAG, "Disable conf command");
|
||||
|
||||
case CMD_DISABLE_CONF:
|
||||
ESP_LOGV(TAG, "Disabled conf");
|
||||
break;
|
||||
case lowbyte(CMD_SET_BAUD_RATE):
|
||||
ESP_LOGV(TAG, "Baud rate change command");
|
||||
|
||||
case CMD_SET_BAUD_RATE:
|
||||
ESP_LOGV(TAG, "Baud rate change");
|
||||
#ifdef USE_SELECT
|
||||
if (this->baud_rate_select_ != nullptr) {
|
||||
ESP_LOGV(TAG, "Change baud rate to %s", 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_MAC):
|
||||
if (len < 20) {
|
||||
}
|
||||
|
||||
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_BLUETOOTH):
|
||||
ESP_LOGV(TAG, "Bluetooth command");
|
||||
}
|
||||
|
||||
case CMD_BLUETOOTH:
|
||||
ESP_LOGV(TAG, "Bluetooth");
|
||||
break;
|
||||
case lowbyte(CMD_SINGLE_TARGET_MODE):
|
||||
ESP_LOGV(TAG, "Single target conf command");
|
||||
|
||||
case CMD_SINGLE_TARGET_MODE:
|
||||
ESP_LOGV(TAG, "Single target conf");
|
||||
#ifdef USE_SWITCH
|
||||
if (this->multi_target_switch_ != nullptr) {
|
||||
this->multi_target_switch_->publish_state(false);
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
case lowbyte(CMD_MULTI_TARGET_MODE):
|
||||
ESP_LOGV(TAG, "Multi target conf command");
|
||||
|
||||
case CMD_MULTI_TARGET_MODE:
|
||||
ESP_LOGV(TAG, "Multi target conf");
|
||||
#ifdef USE_SWITCH
|
||||
if (this->multi_target_switch_ != nullptr) {
|
||||
this->multi_target_switch_->publish_state(true);
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
case lowbyte(CMD_QUERY_TARGET_MODE):
|
||||
ESP_LOGV(TAG, "Query target tracking mode command");
|
||||
|
||||
case CMD_QUERY_TARGET_MODE:
|
||||
ESP_LOGV(TAG, "Query target tracking mode");
|
||||
#ifdef USE_SWITCH
|
||||
if (this->multi_target_switch_ != nullptr) {
|
||||
this->multi_target_switch_->publish_state(buffer[10] == 0x02);
|
||||
this->multi_target_switch_->publish_state(this->buffer_data_[10] == 0x02);
|
||||
}
|
||||
#endif
|
||||
break;
|
||||
case lowbyte(CMD_QUERY_ZONE):
|
||||
ESP_LOGV(TAG, "Query zone conf command");
|
||||
this->zone_type_ = std::stoi(std::to_string(buffer[10]), nullptr, 16);
|
||||
|
||||
case CMD_QUERY_ZONE:
|
||||
ESP_LOGV(TAG, "Query zone conf");
|
||||
this->zone_type_ = std::stoi(std::to_string(this->buffer_data_[10]), nullptr, 16);
|
||||
this->publish_zone_type();
|
||||
#ifdef USE_SELECT
|
||||
if (this->zone_type_select_ != nullptr) {
|
||||
ESP_LOGV(TAG, "Change zone type to: %s", this->zone_type_select_->state.c_str());
|
||||
}
|
||||
#endif
|
||||
if (buffer[10] == 0x00) {
|
||||
if (this->buffer_data_[10] == 0x00) {
|
||||
ESP_LOGV(TAG, "Zone: Disabled");
|
||||
}
|
||||
if (buffer[10] == 0x01) {
|
||||
if (this->buffer_data_[10] == 0x01) {
|
||||
ESP_LOGV(TAG, "Zone: Area detection");
|
||||
}
|
||||
if (buffer[10] == 0x02) {
|
||||
if (this->buffer_data_[10] == 0x02) {
|
||||
ESP_LOGV(TAG, "Zone: Area filter");
|
||||
}
|
||||
this->process_zone_(buffer);
|
||||
this->process_zone_();
|
||||
break;
|
||||
case lowbyte(CMD_SET_ZONE):
|
||||
ESP_LOGV(TAG, "Set zone conf command");
|
||||
|
||||
case CMD_SET_ZONE:
|
||||
ESP_LOGV(TAG, "Set zone conf");
|
||||
this->query_zone_info();
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
@@ -758,55 +834,57 @@ bool LD2450Component::handle_ack_data_(uint8_t *buffer, uint8_t len) {
|
||||
}
|
||||
|
||||
// Read LD2450 buffer data
|
||||
void LD2450Component::readline_(int readch, uint8_t *buffer, uint8_t len) {
|
||||
void LD2450Component::readline_(int readch) {
|
||||
if (readch < 0) {
|
||||
return;
|
||||
return; // No data available
|
||||
}
|
||||
if (this->buffer_pos_ < len - 1) {
|
||||
buffer[this->buffer_pos_++] = readch;
|
||||
buffer[this->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;
|
||||
return; // Not enough data to process yet
|
||||
}
|
||||
if (buffer[this->buffer_pos_ - 2] == 0x55 && buffer[this->buffer_pos_ - 1] == 0xCC) {
|
||||
ESP_LOGV(TAG, "Handle periodic radar data");
|
||||
this->handle_periodic_data_(buffer, this->buffer_pos_);
|
||||
if (this->buffer_data_[this->buffer_pos_ - 2] == DATA_FRAME_FOOTER[0] &&
|
||||
this->buffer_data_[this->buffer_pos_ - 1] == DATA_FRAME_FOOTER[1]) {
|
||||
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 frame
|
||||
} else if (buffer[this->buffer_pos_ - 4] == 0x04 && buffer[this->buffer_pos_ - 3] == 0x03 &&
|
||||
buffer[this->buffer_pos_ - 2] == 0x02 && buffer[this->buffer_pos_ - 1] == 0x01) {
|
||||
ESP_LOGV(TAG, "Handle command ack data");
|
||||
if (this->handle_ack_data_(buffer, this->buffer_pos_)) {
|
||||
this->buffer_pos_ = 0; // Reset position index for next frame
|
||||
} else if (ld2450::validate_header_footer(CMD_FRAME_FOOTER, &this->buffer_data_[this->buffer_pos_ - 4])) {
|
||||
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 {
|
||||
ESP_LOGV(TAG, "Command ack data invalid");
|
||||
ESP_LOGV(TAG, "Ack Data incomplete");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set Config Mode - Pre-requisite sending commands
|
||||
void LD2450Component::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));
|
||||
}
|
||||
|
||||
// Set Bluetooth Enable/Disable
|
||||
void LD2450Component::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(); });
|
||||
}
|
||||
|
||||
// Set Baud rate
|
||||
void LD2450Component::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_(); });
|
||||
}
|
||||
|
||||
@@ -847,12 +925,12 @@ void LD2450Component::factory_reset() {
|
||||
void LD2450Component::restart_() { this->send_command_(CMD_RESTART, nullptr, 0); }
|
||||
|
||||
// Get LD2450 firmware version
|
||||
void LD2450Component::get_version_() { this->send_command_(CMD_VERSION, nullptr, 0); }
|
||||
void LD2450Component::get_version_() { this->send_command_(CMD_QUERY_VERSION, nullptr, 0); }
|
||||
|
||||
// Get LD2450 mac address
|
||||
void LD2450Component::get_mac_() {
|
||||
uint8_t cmd_value[2] = {0x01, 0x00};
|
||||
this->send_command_(CMD_MAC, cmd_value, 2);
|
||||
this->send_command_(CMD_QUERY_MAC_ADDRESS, cmd_value, 2);
|
||||
}
|
||||
|
||||
// Query for target tracking mode
|
||||
|
||||
@@ -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
|
||||
@@ -36,10 +38,18 @@ namespace ld2450 {
|
||||
|
||||
// Constants
|
||||
static const uint8_t DEFAULT_PRESENCE_TIMEOUT = 5; // Timeout to reset presense status 5 sec.
|
||||
static const uint8_t MAX_LINE_LENGTH = 60; // Max characters for serial buffer
|
||||
static const uint8_t MAX_LINE_LENGTH = 41; // Max characters for serial buffer
|
||||
static const uint8_t MAX_TARGETS = 3; // Max 3 Targets in LD2450
|
||||
static const uint8_t MAX_ZONES = 3; // Max 3 Zones in LD2450
|
||||
|
||||
enum Direction : uint8_t {
|
||||
DIRECTION_APPROACHING = 0,
|
||||
DIRECTION_MOVING_AWAY = 1,
|
||||
DIRECTION_STATIONARY = 2,
|
||||
DIRECTION_NA = 3,
|
||||
DIRECTION_UNDEFINED = 4,
|
||||
};
|
||||
|
||||
// Target coordinate struct
|
||||
struct Target {
|
||||
int16_t x;
|
||||
@@ -65,19 +75,22 @@ struct ZoneOfNumbers {
|
||||
#endif
|
||||
|
||||
class LD2450Component : public Component, public uart::UARTDevice {
|
||||
#ifdef USE_SENSOR
|
||||
SUB_SENSOR(target_count)
|
||||
SUB_SENSOR(still_target_count)
|
||||
SUB_SENSOR(moving_target_count)
|
||||
#endif
|
||||
#ifdef USE_BINARY_SENSOR
|
||||
SUB_BINARY_SENSOR(target)
|
||||
SUB_BINARY_SENSOR(moving_target)
|
||||
SUB_BINARY_SENSOR(still_target)
|
||||
SUB_BINARY_SENSOR(target)
|
||||
#endif
|
||||
#ifdef USE_SENSOR
|
||||
SUB_SENSOR(moving_target_count)
|
||||
SUB_SENSOR(still_target_count)
|
||||
SUB_SENSOR(target_count)
|
||||
#endif
|
||||
#ifdef USE_TEXT_SENSOR
|
||||
SUB_TEXT_SENSOR(version)
|
||||
SUB_TEXT_SENSOR(mac)
|
||||
SUB_TEXT_SENSOR(version)
|
||||
#endif
|
||||
#ifdef USE_NUMBER
|
||||
SUB_NUMBER(presence_timeout)
|
||||
#endif
|
||||
#ifdef USE_SELECT
|
||||
SUB_SELECT(baud_rate)
|
||||
@@ -88,19 +101,16 @@ class LD2450Component : public Component, public uart::UARTDevice {
|
||||
SUB_SWITCH(multi_target)
|
||||
#endif
|
||||
#ifdef USE_BUTTON
|
||||
SUB_BUTTON(reset)
|
||||
SUB_BUTTON(factory_reset)
|
||||
SUB_BUTTON(restart)
|
||||
#endif
|
||||
#ifdef USE_NUMBER
|
||||
SUB_NUMBER(presence_timeout)
|
||||
#endif
|
||||
|
||||
public:
|
||||
void setup() override;
|
||||
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();
|
||||
@@ -136,10 +146,10 @@ class LD2450Component : public Component, public uart::UARTDevice {
|
||||
protected:
|
||||
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, uint8_t len);
|
||||
bool handle_ack_data_(uint8_t *buffer, uint8_t len);
|
||||
void process_zone_(uint8_t *buffer);
|
||||
void readline_(int readch, uint8_t *buffer, uint8_t len);
|
||||
void handle_periodic_data_();
|
||||
bool handle_ack_data_();
|
||||
void process_zone_();
|
||||
void readline_(int readch);
|
||||
void get_version_();
|
||||
void get_mac_();
|
||||
void query_target_tracking_mode_();
|
||||
@@ -157,13 +167,40 @@ class LD2450Component : public Component, public uart::UARTDevice {
|
||||
uint32_t moving_presence_millis_ = 0;
|
||||
uint16_t throttle_ = 0;
|
||||
uint16_t timeout_ = 5;
|
||||
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};
|
||||
uint8_t buffer_pos_ = 0; // where to resume processing/populating buffer
|
||||
uint8_t zone_type_ = 0;
|
||||
bool bluetooth_on_{false};
|
||||
Target target_info_[MAX_TARGETS];
|
||||
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
|
||||
Direction direction = DIRECTION_UNDEFINED; // Undefined, will differ from any real direction
|
||||
float angle = NAN; // NAN, safe sentinel for floats
|
||||
} 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];
|
||||
|
||||
35
esphome/components/libretiny/helpers.cpp
Normal file
35
esphome/components/libretiny/helpers.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include "esphome/core/helpers.h"
|
||||
|
||||
#ifdef USE_LIBRETINY
|
||||
|
||||
#include "esphome/core/hal.h"
|
||||
|
||||
#include <WiFi.h> // for macAddress()
|
||||
|
||||
namespace esphome {
|
||||
|
||||
uint32_t random_uint32() { return rand(); }
|
||||
|
||||
bool random_bytes(uint8_t *data, size_t len) {
|
||||
lt_rand_bytes(data, len);
|
||||
return true;
|
||||
}
|
||||
|
||||
Mutex::Mutex() { handle_ = xSemaphoreCreateMutex(); }
|
||||
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_); }
|
||||
|
||||
// only affects the executing core
|
||||
// so should not be used as a mutex lock, only to get accurate timing
|
||||
IRAM_ATTR InterruptLock::InterruptLock() { portDISABLE_INTERRUPTS(); }
|
||||
IRAM_ATTR InterruptLock::~InterruptLock() { portENABLE_INTERRUPTS(); }
|
||||
|
||||
void get_mac_address_raw(uint8_t *mac) { // NOLINT(readability-non-const-parameter)
|
||||
WiFi.macAddress(mac);
|
||||
}
|
||||
|
||||
} // namespace esphome
|
||||
|
||||
#endif // USE_LIBRETINY
|
||||
@@ -97,12 +97,12 @@ class AddressableLight : public LightOutput, public Component {
|
||||
}
|
||||
virtual ESPColorView get_view_internal(int32_t index) const = 0;
|
||||
|
||||
bool effect_active_{false};
|
||||
ESPColorCorrection correction_{};
|
||||
LightState *state_parent_{nullptr};
|
||||
#ifdef USE_POWER_SUPPLY
|
||||
power_supply::PowerSupplyRequester power_;
|
||||
#endif
|
||||
LightState *state_parent_{nullptr};
|
||||
bool effect_active_{false};
|
||||
};
|
||||
|
||||
class AddressableLightTransformer : public LightTransitionTransformer {
|
||||
@@ -114,9 +114,9 @@ class AddressableLightTransformer : public LightTransitionTransformer {
|
||||
|
||||
protected:
|
||||
AddressableLight &light_;
|
||||
Color target_color_{};
|
||||
float last_transition_progress_{0.0f};
|
||||
float accumulated_alpha_{0.0f};
|
||||
Color target_color_{};
|
||||
};
|
||||
|
||||
} // namespace light
|
||||
|
||||
@@ -69,8 +69,8 @@ class ESPColorCorrection {
|
||||
protected:
|
||||
uint8_t gamma_table_[256];
|
||||
uint8_t gamma_reverse_table_[256];
|
||||
uint8_t local_brightness_{255};
|
||||
Color max_brightness_;
|
||||
uint8_t local_brightness_{255};
|
||||
};
|
||||
|
||||
} // namespace light
|
||||
|
||||
@@ -2,12 +2,28 @@
|
||||
#include "light_call.h"
|
||||
#include "light_state.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "esphome/core/optional.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace light {
|
||||
|
||||
static const char *const TAG = "light";
|
||||
|
||||
// Macro to reduce repetitive setter code
|
||||
#define IMPLEMENT_LIGHT_CALL_SETTER(name, type, flag) \
|
||||
LightCall &LightCall::set_##name(optional<type>(name)) { \
|
||||
if ((name).has_value()) { \
|
||||
this->name##_ = (name).value(); \
|
||||
} \
|
||||
this->set_flag_(flag, (name).has_value()); \
|
||||
return *this; \
|
||||
} \
|
||||
LightCall &LightCall::set_##name(type name) { \
|
||||
this->name##_ = name; \
|
||||
this->set_flag_(flag, true); \
|
||||
return *this; \
|
||||
}
|
||||
|
||||
static const LogString *color_mode_to_human(ColorMode color_mode) {
|
||||
if (color_mode == ColorMode::UNKNOWN)
|
||||
return LOG_STR("Unknown");
|
||||
@@ -32,41 +48,43 @@ void LightCall::perform() {
|
||||
const char *name = this->parent_->get_name().c_str();
|
||||
LightColorValues v = this->validate_();
|
||||
|
||||
if (this->publish_) {
|
||||
if (this->get_publish_()) {
|
||||
ESP_LOGD(TAG, "'%s' Setting:", name);
|
||||
|
||||
// Only print color mode when it's being changed
|
||||
ColorMode current_color_mode = this->parent_->remote_values.get_color_mode();
|
||||
if (this->color_mode_.value_or(current_color_mode) != current_color_mode) {
|
||||
ColorMode target_color_mode = this->has_color_mode() ? this->color_mode_ : current_color_mode;
|
||||
if (target_color_mode != current_color_mode) {
|
||||
ESP_LOGD(TAG, " Color mode: %s", LOG_STR_ARG(color_mode_to_human(v.get_color_mode())));
|
||||
}
|
||||
|
||||
// Only print state when it's being changed
|
||||
bool current_state = this->parent_->remote_values.is_on();
|
||||
if (this->state_.value_or(current_state) != current_state) {
|
||||
bool target_state = this->has_state() ? this->state_ : current_state;
|
||||
if (target_state != current_state) {
|
||||
ESP_LOGD(TAG, " State: %s", ONOFF(v.is_on()));
|
||||
}
|
||||
|
||||
if (this->brightness_.has_value()) {
|
||||
if (this->has_brightness()) {
|
||||
ESP_LOGD(TAG, " Brightness: %.0f%%", v.get_brightness() * 100.0f);
|
||||
}
|
||||
|
||||
if (this->color_brightness_.has_value()) {
|
||||
if (this->has_color_brightness()) {
|
||||
ESP_LOGD(TAG, " Color brightness: %.0f%%", v.get_color_brightness() * 100.0f);
|
||||
}
|
||||
if (this->red_.has_value() || this->green_.has_value() || this->blue_.has_value()) {
|
||||
if (this->has_red() || this->has_green() || this->has_blue()) {
|
||||
ESP_LOGD(TAG, " Red: %.0f%%, Green: %.0f%%, Blue: %.0f%%", v.get_red() * 100.0f, v.get_green() * 100.0f,
|
||||
v.get_blue() * 100.0f);
|
||||
}
|
||||
|
||||
if (this->white_.has_value()) {
|
||||
if (this->has_white()) {
|
||||
ESP_LOGD(TAG, " White: %.0f%%", v.get_white() * 100.0f);
|
||||
}
|
||||
if (this->color_temperature_.has_value()) {
|
||||
if (this->has_color_temperature()) {
|
||||
ESP_LOGD(TAG, " Color temperature: %.1f mireds", v.get_color_temperature());
|
||||
}
|
||||
|
||||
if (this->cold_white_.has_value() || this->warm_white_.has_value()) {
|
||||
if (this->has_cold_white() || this->has_warm_white()) {
|
||||
ESP_LOGD(TAG, " Cold white: %.0f%%, warm white: %.0f%%", v.get_cold_white() * 100.0f,
|
||||
v.get_warm_white() * 100.0f);
|
||||
}
|
||||
@@ -74,58 +92,57 @@ void LightCall::perform() {
|
||||
|
||||
if (this->has_flash_()) {
|
||||
// FLASH
|
||||
if (this->publish_) {
|
||||
ESP_LOGD(TAG, " Flash length: %.1fs", *this->flash_length_ / 1e3f);
|
||||
if (this->get_publish_()) {
|
||||
ESP_LOGD(TAG, " Flash length: %.1fs", this->flash_length_ / 1e3f);
|
||||
}
|
||||
|
||||
this->parent_->start_flash_(v, *this->flash_length_, this->publish_);
|
||||
this->parent_->start_flash_(v, this->flash_length_, this->get_publish_());
|
||||
} else if (this->has_transition_()) {
|
||||
// TRANSITION
|
||||
if (this->publish_) {
|
||||
ESP_LOGD(TAG, " Transition length: %.1fs", *this->transition_length_ / 1e3f);
|
||||
if (this->get_publish_()) {
|
||||
ESP_LOGD(TAG, " Transition length: %.1fs", this->transition_length_ / 1e3f);
|
||||
}
|
||||
|
||||
// Special case: Transition and effect can be set when turning off
|
||||
if (this->has_effect_()) {
|
||||
if (this->publish_) {
|
||||
if (this->get_publish_()) {
|
||||
ESP_LOGD(TAG, " Effect: 'None'");
|
||||
}
|
||||
this->parent_->stop_effect_();
|
||||
}
|
||||
|
||||
this->parent_->start_transition_(v, *this->transition_length_, this->publish_);
|
||||
this->parent_->start_transition_(v, this->transition_length_, this->get_publish_());
|
||||
|
||||
} else if (this->has_effect_()) {
|
||||
// EFFECT
|
||||
auto effect = this->effect_;
|
||||
const char *effect_s;
|
||||
if (effect == 0u) {
|
||||
if (this->effect_ == 0u) {
|
||||
effect_s = "None";
|
||||
} else {
|
||||
effect_s = this->parent_->effects_[*this->effect_ - 1]->get_name().c_str();
|
||||
effect_s = this->parent_->effects_[this->effect_ - 1]->get_name().c_str();
|
||||
}
|
||||
|
||||
if (this->publish_) {
|
||||
if (this->get_publish_()) {
|
||||
ESP_LOGD(TAG, " Effect: '%s'", effect_s);
|
||||
}
|
||||
|
||||
this->parent_->start_effect_(*this->effect_);
|
||||
this->parent_->start_effect_(this->effect_);
|
||||
|
||||
// Also set light color values when starting an effect
|
||||
// For example to turn off the light
|
||||
this->parent_->set_immediately_(v, true);
|
||||
} else {
|
||||
// INSTANT CHANGE
|
||||
this->parent_->set_immediately_(v, this->publish_);
|
||||
this->parent_->set_immediately_(v, this->get_publish_());
|
||||
}
|
||||
|
||||
if (!this->has_transition_()) {
|
||||
this->parent_->target_state_reached_callback_.call();
|
||||
}
|
||||
if (this->publish_) {
|
||||
if (this->get_publish_()) {
|
||||
this->parent_->publish_state();
|
||||
}
|
||||
if (this->save_) {
|
||||
if (this->get_save_()) {
|
||||
this->parent_->save_remote_values_();
|
||||
}
|
||||
}
|
||||
@@ -135,82 +152,80 @@ LightColorValues LightCall::validate_() {
|
||||
auto traits = this->parent_->get_traits();
|
||||
|
||||
// Color mode check
|
||||
if (this->color_mode_.has_value() && !traits.supports_color_mode(this->color_mode_.value())) {
|
||||
ESP_LOGW(TAG, "'%s' does not support color mode %s", name,
|
||||
LOG_STR_ARG(color_mode_to_human(this->color_mode_.value())));
|
||||
this->color_mode_.reset();
|
||||
if (this->has_color_mode() && !traits.supports_color_mode(this->color_mode_)) {
|
||||
ESP_LOGW(TAG, "'%s' does not support color mode %s", name, LOG_STR_ARG(color_mode_to_human(this->color_mode_)));
|
||||
this->set_flag_(FLAG_HAS_COLOR_MODE, false);
|
||||
}
|
||||
|
||||
// Ensure there is always a color mode set
|
||||
if (!this->color_mode_.has_value()) {
|
||||
if (!this->has_color_mode()) {
|
||||
this->color_mode_ = this->compute_color_mode_();
|
||||
this->set_flag_(FLAG_HAS_COLOR_MODE, true);
|
||||
}
|
||||
auto color_mode = *this->color_mode_;
|
||||
auto color_mode = this->color_mode_;
|
||||
|
||||
// Transform calls that use non-native parameters for the current mode.
|
||||
this->transform_parameters_();
|
||||
|
||||
// Brightness exists check
|
||||
if (this->brightness_.has_value() && *this->brightness_ > 0.0f && !(color_mode & ColorCapability::BRIGHTNESS)) {
|
||||
if (this->has_brightness() && this->brightness_ > 0.0f && !(color_mode & ColorCapability::BRIGHTNESS)) {
|
||||
ESP_LOGW(TAG, "'%s': setting brightness not supported", name);
|
||||
this->brightness_.reset();
|
||||
this->set_flag_(FLAG_HAS_BRIGHTNESS, false);
|
||||
}
|
||||
|
||||
// Transition length possible check
|
||||
if (this->transition_length_.has_value() && *this->transition_length_ != 0 &&
|
||||
!(color_mode & ColorCapability::BRIGHTNESS)) {
|
||||
if (this->has_transition_() && this->transition_length_ != 0 && !(color_mode & ColorCapability::BRIGHTNESS)) {
|
||||
ESP_LOGW(TAG, "'%s': transitions not supported", name);
|
||||
this->transition_length_.reset();
|
||||
this->set_flag_(FLAG_HAS_TRANSITION, false);
|
||||
}
|
||||
|
||||
// Color brightness exists check
|
||||
if (this->color_brightness_.has_value() && *this->color_brightness_ > 0.0f && !(color_mode & ColorCapability::RGB)) {
|
||||
if (this->has_color_brightness() && this->color_brightness_ > 0.0f && !(color_mode & ColorCapability::RGB)) {
|
||||
ESP_LOGW(TAG, "'%s': color mode does not support setting RGB brightness", name);
|
||||
this->color_brightness_.reset();
|
||||
this->set_flag_(FLAG_HAS_COLOR_BRIGHTNESS, false);
|
||||
}
|
||||
|
||||
// RGB exists check
|
||||
if ((this->red_.has_value() && *this->red_ > 0.0f) || (this->green_.has_value() && *this->green_ > 0.0f) ||
|
||||
(this->blue_.has_value() && *this->blue_ > 0.0f)) {
|
||||
if ((this->has_red() && this->red_ > 0.0f) || (this->has_green() && this->green_ > 0.0f) ||
|
||||
(this->has_blue() && this->blue_ > 0.0f)) {
|
||||
if (!(color_mode & ColorCapability::RGB)) {
|
||||
ESP_LOGW(TAG, "'%s': color mode does not support setting RGB color", name);
|
||||
this->red_.reset();
|
||||
this->green_.reset();
|
||||
this->blue_.reset();
|
||||
this->set_flag_(FLAG_HAS_RED, false);
|
||||
this->set_flag_(FLAG_HAS_GREEN, false);
|
||||
this->set_flag_(FLAG_HAS_BLUE, false);
|
||||
}
|
||||
}
|
||||
|
||||
// White value exists check
|
||||
if (this->white_.has_value() && *this->white_ > 0.0f &&
|
||||
if (this->has_white() && this->white_ > 0.0f &&
|
||||
!(color_mode & ColorCapability::WHITE || color_mode & ColorCapability::COLD_WARM_WHITE)) {
|
||||
ESP_LOGW(TAG, "'%s': color mode does not support setting white value", name);
|
||||
this->white_.reset();
|
||||
this->set_flag_(FLAG_HAS_WHITE, false);
|
||||
}
|
||||
|
||||
// Color temperature exists check
|
||||
if (this->color_temperature_.has_value() &&
|
||||
if (this->has_color_temperature() &&
|
||||
!(color_mode & ColorCapability::COLOR_TEMPERATURE || color_mode & ColorCapability::COLD_WARM_WHITE)) {
|
||||
ESP_LOGW(TAG, "'%s': color mode does not support setting color temperature", name);
|
||||
this->color_temperature_.reset();
|
||||
this->set_flag_(FLAG_HAS_COLOR_TEMPERATURE, false);
|
||||
}
|
||||
|
||||
// Cold/warm white value exists check
|
||||
if ((this->cold_white_.has_value() && *this->cold_white_ > 0.0f) ||
|
||||
(this->warm_white_.has_value() && *this->warm_white_ > 0.0f)) {
|
||||
if ((this->has_cold_white() && this->cold_white_ > 0.0f) || (this->has_warm_white() && this->warm_white_ > 0.0f)) {
|
||||
if (!(color_mode & ColorCapability::COLD_WARM_WHITE)) {
|
||||
ESP_LOGW(TAG, "'%s': color mode does not support setting cold/warm white value", name);
|
||||
this->cold_white_.reset();
|
||||
this->warm_white_.reset();
|
||||
this->set_flag_(FLAG_HAS_COLD_WHITE, false);
|
||||
this->set_flag_(FLAG_HAS_WARM_WHITE, false);
|
||||
}
|
||||
}
|
||||
|
||||
#define VALIDATE_RANGE_(name_, upper_name, min, max) \
|
||||
if (name_##_.has_value()) { \
|
||||
auto val = *name_##_; \
|
||||
if (this->has_##name_()) { \
|
||||
auto val = this->name_##_; \
|
||||
if (val < (min) || val > (max)) { \
|
||||
ESP_LOGW(TAG, "'%s': %s value %.2f is out of range [%.1f - %.1f]", name, LOG_STR_LITERAL(upper_name), val, \
|
||||
(min), (max)); \
|
||||
name_##_ = clamp(val, (min), (max)); \
|
||||
this->name_##_ = clamp(val, (min), (max)); \
|
||||
} \
|
||||
}
|
||||
#define VALIDATE_RANGE(name, upper_name) VALIDATE_RANGE_(name, upper_name, 0.0f, 1.0f)
|
||||
@@ -227,110 +242,116 @@ LightColorValues LightCall::validate_() {
|
||||
VALIDATE_RANGE_(color_temperature, "Color temperature", traits.get_min_mireds(), traits.get_max_mireds())
|
||||
|
||||
// Flag whether an explicit turn off was requested, in which case we'll also stop the effect.
|
||||
bool explicit_turn_off_request = this->state_.has_value() && !*this->state_;
|
||||
bool explicit_turn_off_request = this->has_state() && !this->state_;
|
||||
|
||||
// Turn off when brightness is set to zero, and reset brightness (so that it has nonzero brightness when turned on).
|
||||
if (this->brightness_.has_value() && *this->brightness_ == 0.0f) {
|
||||
this->state_ = optional<float>(false);
|
||||
this->brightness_ = optional<float>(1.0f);
|
||||
if (this->has_brightness() && this->brightness_ == 0.0f) {
|
||||
this->state_ = false;
|
||||
this->set_flag_(FLAG_HAS_STATE, true);
|
||||
this->brightness_ = 1.0f;
|
||||
}
|
||||
|
||||
// Set color brightness to 100% if currently zero and a color is set.
|
||||
if (this->red_.has_value() || this->green_.has_value() || this->blue_.has_value()) {
|
||||
if (!this->color_brightness_.has_value() && this->parent_->remote_values.get_color_brightness() == 0.0f)
|
||||
this->color_brightness_ = optional<float>(1.0f);
|
||||
if (this->has_red() || this->has_green() || this->has_blue()) {
|
||||
if (!this->has_color_brightness() && this->parent_->remote_values.get_color_brightness() == 0.0f) {
|
||||
this->color_brightness_ = 1.0f;
|
||||
this->set_flag_(FLAG_HAS_COLOR_BRIGHTNESS, true);
|
||||
}
|
||||
}
|
||||
|
||||
// Create color values for the light with this call applied.
|
||||
auto v = this->parent_->remote_values;
|
||||
if (this->color_mode_.has_value())
|
||||
v.set_color_mode(*this->color_mode_);
|
||||
if (this->state_.has_value())
|
||||
v.set_state(*this->state_);
|
||||
if (this->brightness_.has_value())
|
||||
v.set_brightness(*this->brightness_);
|
||||
if (this->color_brightness_.has_value())
|
||||
v.set_color_brightness(*this->color_brightness_);
|
||||
if (this->red_.has_value())
|
||||
v.set_red(*this->red_);
|
||||
if (this->green_.has_value())
|
||||
v.set_green(*this->green_);
|
||||
if (this->blue_.has_value())
|
||||
v.set_blue(*this->blue_);
|
||||
if (this->white_.has_value())
|
||||
v.set_white(*this->white_);
|
||||
if (this->color_temperature_.has_value())
|
||||
v.set_color_temperature(*this->color_temperature_);
|
||||
if (this->cold_white_.has_value())
|
||||
v.set_cold_white(*this->cold_white_);
|
||||
if (this->warm_white_.has_value())
|
||||
v.set_warm_white(*this->warm_white_);
|
||||
if (this->has_color_mode())
|
||||
v.set_color_mode(this->color_mode_);
|
||||
if (this->has_state())
|
||||
v.set_state(this->state_);
|
||||
if (this->has_brightness())
|
||||
v.set_brightness(this->brightness_);
|
||||
if (this->has_color_brightness())
|
||||
v.set_color_brightness(this->color_brightness_);
|
||||
if (this->has_red())
|
||||
v.set_red(this->red_);
|
||||
if (this->has_green())
|
||||
v.set_green(this->green_);
|
||||
if (this->has_blue())
|
||||
v.set_blue(this->blue_);
|
||||
if (this->has_white())
|
||||
v.set_white(this->white_);
|
||||
if (this->has_color_temperature())
|
||||
v.set_color_temperature(this->color_temperature_);
|
||||
if (this->has_cold_white())
|
||||
v.set_cold_white(this->cold_white_);
|
||||
if (this->has_warm_white())
|
||||
v.set_warm_white(this->warm_white_);
|
||||
|
||||
v.normalize_color();
|
||||
|
||||
// Flash length check
|
||||
if (this->has_flash_() && *this->flash_length_ == 0) {
|
||||
if (this->has_flash_() && this->flash_length_ == 0) {
|
||||
ESP_LOGW(TAG, "'%s': flash length must be greater than zero", name);
|
||||
this->flash_length_.reset();
|
||||
this->set_flag_(FLAG_HAS_FLASH, false);
|
||||
}
|
||||
|
||||
// validate transition length/flash length/effect not used at the same time
|
||||
bool supports_transition = color_mode & ColorCapability::BRIGHTNESS;
|
||||
|
||||
// If effect is already active, remove effect start
|
||||
if (this->has_effect_() && *this->effect_ == this->parent_->active_effect_index_) {
|
||||
this->effect_.reset();
|
||||
if (this->has_effect_() && this->effect_ == this->parent_->active_effect_index_) {
|
||||
this->set_flag_(FLAG_HAS_EFFECT, false);
|
||||
}
|
||||
|
||||
// validate effect index
|
||||
if (this->has_effect_() && *this->effect_ > this->parent_->effects_.size()) {
|
||||
ESP_LOGW(TAG, "'%s': invalid effect index %" PRIu32, name, *this->effect_);
|
||||
this->effect_.reset();
|
||||
if (this->has_effect_() && this->effect_ > this->parent_->effects_.size()) {
|
||||
ESP_LOGW(TAG, "'%s': invalid effect index %" PRIu32, name, this->effect_);
|
||||
this->set_flag_(FLAG_HAS_EFFECT, false);
|
||||
}
|
||||
|
||||
if (this->has_effect_() && (this->has_transition_() || this->has_flash_())) {
|
||||
ESP_LOGW(TAG, "'%s': effect cannot be used with transition/flash", name);
|
||||
this->transition_length_.reset();
|
||||
this->flash_length_.reset();
|
||||
this->set_flag_(FLAG_HAS_TRANSITION, false);
|
||||
this->set_flag_(FLAG_HAS_FLASH, false);
|
||||
}
|
||||
|
||||
if (this->has_flash_() && this->has_transition_()) {
|
||||
ESP_LOGW(TAG, "'%s': flash cannot be used with transition", name);
|
||||
this->transition_length_.reset();
|
||||
this->set_flag_(FLAG_HAS_TRANSITION, false);
|
||||
}
|
||||
|
||||
if (!this->has_transition_() && !this->has_flash_() && (!this->has_effect_() || *this->effect_ == 0) &&
|
||||
if (!this->has_transition_() && !this->has_flash_() && (!this->has_effect_() || this->effect_ == 0) &&
|
||||
supports_transition) {
|
||||
// nothing specified and light supports transitions, set default transition length
|
||||
this->transition_length_ = this->parent_->default_transition_length_;
|
||||
this->set_flag_(FLAG_HAS_TRANSITION, true);
|
||||
}
|
||||
|
||||
if (this->transition_length_.value_or(0) == 0) {
|
||||
if (this->has_transition_() && this->transition_length_ == 0) {
|
||||
// 0 transition is interpreted as no transition (instant change)
|
||||
this->transition_length_.reset();
|
||||
this->set_flag_(FLAG_HAS_TRANSITION, false);
|
||||
}
|
||||
|
||||
if (this->has_transition_() && !supports_transition) {
|
||||
ESP_LOGW(TAG, "'%s': transitions not supported", name);
|
||||
this->transition_length_.reset();
|
||||
this->set_flag_(FLAG_HAS_TRANSITION, false);
|
||||
}
|
||||
|
||||
// If not a flash and turning the light off, then disable the light
|
||||
// Do not use light color values directly, so that effects can set 0% brightness
|
||||
// Reason: When user turns off the light in frontend, the effect should also stop
|
||||
if (!this->has_flash_() && !this->state_.value_or(v.is_on())) {
|
||||
bool target_state = this->has_state() ? this->state_ : v.is_on();
|
||||
if (!this->has_flash_() && !target_state) {
|
||||
if (this->has_effect_()) {
|
||||
ESP_LOGW(TAG, "'%s': cannot start effect when turning off", name);
|
||||
this->effect_.reset();
|
||||
this->set_flag_(FLAG_HAS_EFFECT, false);
|
||||
} else if (this->parent_->active_effect_index_ != 0 && explicit_turn_off_request) {
|
||||
// Auto turn off effect
|
||||
this->effect_ = 0;
|
||||
this->set_flag_(FLAG_HAS_EFFECT, true);
|
||||
}
|
||||
}
|
||||
|
||||
// Disable saving for flashes
|
||||
if (this->has_flash_())
|
||||
this->save_ = false;
|
||||
this->set_flag_(FLAG_SAVE, false);
|
||||
|
||||
return v;
|
||||
}
|
||||
@@ -343,24 +364,27 @@ void LightCall::transform_parameters_() {
|
||||
// - RGBWW lights with color_interlock=true, which also sets "brightness" and
|
||||
// "color_temperature" (without color_interlock, CW/WW are set directly)
|
||||
// - Legacy Home Assistant (pre-colormode), which sets "white" and "color_temperature"
|
||||
if (((this->white_.has_value() && *this->white_ > 0.0f) || this->color_temperature_.has_value()) && //
|
||||
(*this->color_mode_ & ColorCapability::COLD_WARM_WHITE) && //
|
||||
!(*this->color_mode_ & ColorCapability::WHITE) && //
|
||||
!(*this->color_mode_ & ColorCapability::COLOR_TEMPERATURE) && //
|
||||
if (((this->has_white() && this->white_ > 0.0f) || this->has_color_temperature()) && //
|
||||
(this->color_mode_ & ColorCapability::COLD_WARM_WHITE) && //
|
||||
!(this->color_mode_ & ColorCapability::WHITE) && //
|
||||
!(this->color_mode_ & ColorCapability::COLOR_TEMPERATURE) && //
|
||||
traits.get_min_mireds() > 0.0f && traits.get_max_mireds() > 0.0f) {
|
||||
ESP_LOGD(TAG, "'%s': setting cold/warm white channels using white/color temperature values",
|
||||
this->parent_->get_name().c_str());
|
||||
if (this->color_temperature_.has_value()) {
|
||||
const float color_temp = clamp(*this->color_temperature_, traits.get_min_mireds(), traits.get_max_mireds());
|
||||
if (this->has_color_temperature()) {
|
||||
const float color_temp = clamp(this->color_temperature_, traits.get_min_mireds(), traits.get_max_mireds());
|
||||
const float ww_fraction =
|
||||
(color_temp - traits.get_min_mireds()) / (traits.get_max_mireds() - traits.get_min_mireds());
|
||||
const float cw_fraction = 1.0f - ww_fraction;
|
||||
const float max_cw_ww = std::max(ww_fraction, cw_fraction);
|
||||
this->cold_white_ = gamma_uncorrect(cw_fraction / max_cw_ww, this->parent_->get_gamma_correct());
|
||||
this->warm_white_ = gamma_uncorrect(ww_fraction / max_cw_ww, this->parent_->get_gamma_correct());
|
||||
this->set_flag_(FLAG_HAS_COLD_WHITE, true);
|
||||
this->set_flag_(FLAG_HAS_WARM_WHITE, true);
|
||||
}
|
||||
if (this->white_.has_value()) {
|
||||
this->brightness_ = *this->white_;
|
||||
if (this->has_white()) {
|
||||
this->brightness_ = this->white_;
|
||||
this->set_flag_(FLAG_HAS_BRIGHTNESS, true);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -378,7 +402,7 @@ ColorMode LightCall::compute_color_mode_() {
|
||||
|
||||
// Don't change if the light is being turned off.
|
||||
ColorMode current_mode = this->parent_->remote_values.get_color_mode();
|
||||
if (this->state_.has_value() && !*this->state_)
|
||||
if (this->has_state() && !this->state_)
|
||||
return current_mode;
|
||||
|
||||
// If no color mode is specified, we try to guess the color mode. This is needed for backward compatibility to
|
||||
@@ -411,12 +435,12 @@ ColorMode LightCall::compute_color_mode_() {
|
||||
return color_mode;
|
||||
}
|
||||
std::set<ColorMode> LightCall::get_suitable_color_modes_() {
|
||||
bool has_white = this->white_.has_value() && *this->white_ > 0.0f;
|
||||
bool has_ct = this->color_temperature_.has_value();
|
||||
bool has_cwww = (this->cold_white_.has_value() && *this->cold_white_ > 0.0f) ||
|
||||
(this->warm_white_.has_value() && *this->warm_white_ > 0.0f);
|
||||
bool has_rgb = (this->color_brightness_.has_value() && *this->color_brightness_ > 0.0f) ||
|
||||
(this->red_.has_value() || this->green_.has_value() || this->blue_.has_value());
|
||||
bool has_white = this->has_white() && this->white_ > 0.0f;
|
||||
bool has_ct = this->has_color_temperature();
|
||||
bool has_cwww =
|
||||
(this->has_cold_white() && this->cold_white_ > 0.0f) || (this->has_warm_white() && this->warm_white_ > 0.0f);
|
||||
bool has_rgb = (this->has_color_brightness() && this->color_brightness_ > 0.0f) ||
|
||||
(this->has_red() || this->has_green() || this->has_blue());
|
||||
|
||||
#define KEY(white, ct, cwww, rgb) ((white) << 0 | (ct) << 1 | (cwww) << 2 | (rgb) << 3)
|
||||
#define ENTRY(white, ct, cwww, rgb, ...) \
|
||||
@@ -491,7 +515,7 @@ LightCall &LightCall::from_light_color_values(const LightColorValues &values) {
|
||||
return *this;
|
||||
}
|
||||
ColorMode LightCall::get_active_color_mode_() {
|
||||
return this->color_mode_.value_or(this->parent_->remote_values.get_color_mode());
|
||||
return this->has_color_mode() ? this->color_mode_ : this->parent_->remote_values.get_color_mode();
|
||||
}
|
||||
LightCall &LightCall::set_transition_length_if_supported(uint32_t transition_length) {
|
||||
if (this->get_active_color_mode_() & ColorCapability::BRIGHTNESS)
|
||||
@@ -505,7 +529,7 @@ LightCall &LightCall::set_brightness_if_supported(float brightness) {
|
||||
}
|
||||
LightCall &LightCall::set_color_mode_if_supported(ColorMode color_mode) {
|
||||
if (this->parent_->get_traits().supports_color_mode(color_mode))
|
||||
this->color_mode_ = color_mode;
|
||||
this->set_color_mode(color_mode);
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_color_brightness_if_supported(float brightness) {
|
||||
@@ -549,110 +573,19 @@ LightCall &LightCall::set_warm_white_if_supported(float warm_white) {
|
||||
this->set_warm_white(warm_white);
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_state(optional<bool> state) {
|
||||
this->state_ = state;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_state(bool state) {
|
||||
this->state_ = state;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_transition_length(optional<uint32_t> transition_length) {
|
||||
this->transition_length_ = transition_length;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_transition_length(uint32_t transition_length) {
|
||||
this->transition_length_ = transition_length;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_flash_length(optional<uint32_t> flash_length) {
|
||||
this->flash_length_ = flash_length;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_flash_length(uint32_t flash_length) {
|
||||
this->flash_length_ = flash_length;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_brightness(optional<float> brightness) {
|
||||
this->brightness_ = brightness;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_brightness(float brightness) {
|
||||
this->brightness_ = brightness;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_color_mode(optional<ColorMode> color_mode) {
|
||||
this->color_mode_ = color_mode;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_color_mode(ColorMode color_mode) {
|
||||
this->color_mode_ = color_mode;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_color_brightness(optional<float> brightness) {
|
||||
this->color_brightness_ = brightness;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_color_brightness(float brightness) {
|
||||
this->color_brightness_ = brightness;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_red(optional<float> red) {
|
||||
this->red_ = red;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_red(float red) {
|
||||
this->red_ = red;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_green(optional<float> green) {
|
||||
this->green_ = green;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_green(float green) {
|
||||
this->green_ = green;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_blue(optional<float> blue) {
|
||||
this->blue_ = blue;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_blue(float blue) {
|
||||
this->blue_ = blue;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_white(optional<float> white) {
|
||||
this->white_ = white;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_white(float white) {
|
||||
this->white_ = white;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_color_temperature(optional<float> color_temperature) {
|
||||
this->color_temperature_ = color_temperature;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_color_temperature(float color_temperature) {
|
||||
this->color_temperature_ = color_temperature;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_cold_white(optional<float> cold_white) {
|
||||
this->cold_white_ = cold_white;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_cold_white(float cold_white) {
|
||||
this->cold_white_ = cold_white;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_warm_white(optional<float> warm_white) {
|
||||
this->warm_white_ = warm_white;
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_warm_white(float warm_white) {
|
||||
this->warm_white_ = warm_white;
|
||||
return *this;
|
||||
}
|
||||
IMPLEMENT_LIGHT_CALL_SETTER(state, bool, FLAG_HAS_STATE)
|
||||
IMPLEMENT_LIGHT_CALL_SETTER(transition_length, uint32_t, FLAG_HAS_TRANSITION)
|
||||
IMPLEMENT_LIGHT_CALL_SETTER(flash_length, uint32_t, FLAG_HAS_FLASH)
|
||||
IMPLEMENT_LIGHT_CALL_SETTER(brightness, float, FLAG_HAS_BRIGHTNESS)
|
||||
IMPLEMENT_LIGHT_CALL_SETTER(color_mode, ColorMode, FLAG_HAS_COLOR_MODE)
|
||||
IMPLEMENT_LIGHT_CALL_SETTER(color_brightness, float, FLAG_HAS_COLOR_BRIGHTNESS)
|
||||
IMPLEMENT_LIGHT_CALL_SETTER(red, float, FLAG_HAS_RED)
|
||||
IMPLEMENT_LIGHT_CALL_SETTER(green, float, FLAG_HAS_GREEN)
|
||||
IMPLEMENT_LIGHT_CALL_SETTER(blue, float, FLAG_HAS_BLUE)
|
||||
IMPLEMENT_LIGHT_CALL_SETTER(white, float, FLAG_HAS_WHITE)
|
||||
IMPLEMENT_LIGHT_CALL_SETTER(color_temperature, float, FLAG_HAS_COLOR_TEMPERATURE)
|
||||
IMPLEMENT_LIGHT_CALL_SETTER(cold_white, float, FLAG_HAS_COLD_WHITE)
|
||||
IMPLEMENT_LIGHT_CALL_SETTER(warm_white, float, FLAG_HAS_WARM_WHITE)
|
||||
LightCall &LightCall::set_effect(optional<std::string> effect) {
|
||||
if (effect.has_value())
|
||||
this->set_effect(*effect);
|
||||
@@ -660,18 +593,22 @@ LightCall &LightCall::set_effect(optional<std::string> effect) {
|
||||
}
|
||||
LightCall &LightCall::set_effect(uint32_t effect_number) {
|
||||
this->effect_ = effect_number;
|
||||
this->set_flag_(FLAG_HAS_EFFECT, true);
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_effect(optional<uint32_t> effect_number) {
|
||||
this->effect_ = effect_number;
|
||||
if (effect_number.has_value()) {
|
||||
this->effect_ = effect_number.value();
|
||||
}
|
||||
this->set_flag_(FLAG_HAS_EFFECT, effect_number.has_value());
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_publish(bool publish) {
|
||||
this->publish_ = publish;
|
||||
this->set_flag_(FLAG_PUBLISH, publish);
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_save(bool save) {
|
||||
this->save_ = save;
|
||||
this->set_flag_(FLAG_SAVE, save);
|
||||
return *this;
|
||||
}
|
||||
LightCall &LightCall::set_rgb(float red, float green, float blue) {
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/optional.h"
|
||||
#include "light_color_values.h"
|
||||
#include <set>
|
||||
|
||||
@@ -10,6 +9,11 @@ namespace light {
|
||||
class LightState;
|
||||
|
||||
/** This class represents a requested change in a light state.
|
||||
*
|
||||
* Light state changes are tracked using a bitfield flags_ to minimize memory usage.
|
||||
* Each possible light property has a flag indicating whether it has been set.
|
||||
* This design keeps LightCall at ~56 bytes to minimize heap fragmentation on
|
||||
* ESP8266 and other memory-constrained devices.
|
||||
*/
|
||||
class LightCall {
|
||||
public:
|
||||
@@ -131,6 +135,19 @@ class LightCall {
|
||||
/// Set whether this light call should trigger a save state to recover them at startup..
|
||||
LightCall &set_save(bool save);
|
||||
|
||||
// Getter methods to check if values are set
|
||||
bool has_state() const { return (flags_ & FLAG_HAS_STATE) != 0; }
|
||||
bool has_brightness() const { return (flags_ & FLAG_HAS_BRIGHTNESS) != 0; }
|
||||
bool has_color_brightness() const { return (flags_ & FLAG_HAS_COLOR_BRIGHTNESS) != 0; }
|
||||
bool has_red() const { return (flags_ & FLAG_HAS_RED) != 0; }
|
||||
bool has_green() const { return (flags_ & FLAG_HAS_GREEN) != 0; }
|
||||
bool has_blue() const { return (flags_ & FLAG_HAS_BLUE) != 0; }
|
||||
bool has_white() const { return (flags_ & FLAG_HAS_WHITE) != 0; }
|
||||
bool has_color_temperature() const { return (flags_ & FLAG_HAS_COLOR_TEMPERATURE) != 0; }
|
||||
bool has_cold_white() const { return (flags_ & FLAG_HAS_COLD_WHITE) != 0; }
|
||||
bool has_warm_white() const { return (flags_ & FLAG_HAS_WARM_WHITE) != 0; }
|
||||
bool has_color_mode() const { return (flags_ & FLAG_HAS_COLOR_MODE) != 0; }
|
||||
|
||||
/** Set the RGB color of the light by RGB values.
|
||||
*
|
||||
* Please note that this only changes the color of the light, not the brightness.
|
||||
@@ -170,27 +187,62 @@ class LightCall {
|
||||
/// Some color modes also can be set using non-native parameters, transform those calls.
|
||||
void transform_parameters_();
|
||||
|
||||
bool has_transition_() { return this->transition_length_.has_value(); }
|
||||
bool has_flash_() { return this->flash_length_.has_value(); }
|
||||
bool has_effect_() { return this->effect_.has_value(); }
|
||||
// Bitfield flags - each flag indicates whether a corresponding value has been set.
|
||||
enum FieldFlags : uint16_t {
|
||||
FLAG_HAS_STATE = 1 << 0,
|
||||
FLAG_HAS_TRANSITION = 1 << 1,
|
||||
FLAG_HAS_FLASH = 1 << 2,
|
||||
FLAG_HAS_EFFECT = 1 << 3,
|
||||
FLAG_HAS_BRIGHTNESS = 1 << 4,
|
||||
FLAG_HAS_COLOR_BRIGHTNESS = 1 << 5,
|
||||
FLAG_HAS_RED = 1 << 6,
|
||||
FLAG_HAS_GREEN = 1 << 7,
|
||||
FLAG_HAS_BLUE = 1 << 8,
|
||||
FLAG_HAS_WHITE = 1 << 9,
|
||||
FLAG_HAS_COLOR_TEMPERATURE = 1 << 10,
|
||||
FLAG_HAS_COLD_WHITE = 1 << 11,
|
||||
FLAG_HAS_WARM_WHITE = 1 << 12,
|
||||
FLAG_HAS_COLOR_MODE = 1 << 13,
|
||||
FLAG_PUBLISH = 1 << 14,
|
||||
FLAG_SAVE = 1 << 15,
|
||||
};
|
||||
|
||||
bool has_transition_() { return (this->flags_ & FLAG_HAS_TRANSITION) != 0; }
|
||||
bool has_flash_() { return (this->flags_ & FLAG_HAS_FLASH) != 0; }
|
||||
bool has_effect_() { return (this->flags_ & FLAG_HAS_EFFECT) != 0; }
|
||||
bool get_publish_() { return (this->flags_ & FLAG_PUBLISH) != 0; }
|
||||
bool get_save_() { return (this->flags_ & FLAG_SAVE) != 0; }
|
||||
|
||||
// Helper to set flag
|
||||
void set_flag_(FieldFlags flag, bool value) {
|
||||
if (value) {
|
||||
this->flags_ |= flag;
|
||||
} else {
|
||||
this->flags_ &= ~flag;
|
||||
}
|
||||
}
|
||||
|
||||
LightState *parent_;
|
||||
optional<bool> state_;
|
||||
optional<uint32_t> transition_length_;
|
||||
optional<uint32_t> flash_length_;
|
||||
optional<ColorMode> color_mode_;
|
||||
optional<float> brightness_;
|
||||
optional<float> color_brightness_;
|
||||
optional<float> red_;
|
||||
optional<float> green_;
|
||||
optional<float> blue_;
|
||||
optional<float> white_;
|
||||
optional<float> color_temperature_;
|
||||
optional<float> cold_white_;
|
||||
optional<float> warm_white_;
|
||||
optional<uint32_t> effect_;
|
||||
bool publish_{true};
|
||||
bool save_{true};
|
||||
|
||||
// Light state values - use flags_ to check if a value has been set.
|
||||
// Group 4-byte aligned members first
|
||||
uint32_t transition_length_;
|
||||
uint32_t flash_length_;
|
||||
uint32_t effect_;
|
||||
float brightness_;
|
||||
float color_brightness_;
|
||||
float red_;
|
||||
float green_;
|
||||
float blue_;
|
||||
float white_;
|
||||
float color_temperature_;
|
||||
float cold_white_;
|
||||
float warm_white_;
|
||||
|
||||
// Smaller members at the end for better packing
|
||||
uint16_t flags_{FLAG_PUBLISH | FLAG_SAVE}; // Tracks which values are set
|
||||
ColorMode color_mode_;
|
||||
bool state_;
|
||||
};
|
||||
|
||||
} // namespace light
|
||||
|
||||
@@ -46,8 +46,7 @@ class LightColorValues {
|
||||
public:
|
||||
/// Construct the LightColorValues with all attributes enabled, but state set to off.
|
||||
LightColorValues()
|
||||
: color_mode_(ColorMode::UNKNOWN),
|
||||
state_(0.0f),
|
||||
: state_(0.0f),
|
||||
brightness_(1.0f),
|
||||
color_brightness_(1.0f),
|
||||
red_(1.0f),
|
||||
@@ -56,7 +55,8 @@ class LightColorValues {
|
||||
white_(1.0f),
|
||||
color_temperature_{0.0f},
|
||||
cold_white_{1.0f},
|
||||
warm_white_{1.0f} {}
|
||||
warm_white_{1.0f},
|
||||
color_mode_(ColorMode::UNKNOWN) {}
|
||||
|
||||
LightColorValues(ColorMode color_mode, float state, float brightness, float color_brightness, float red, float green,
|
||||
float blue, float white, float color_temperature, float cold_white, float warm_white) {
|
||||
@@ -292,7 +292,6 @@ class LightColorValues {
|
||||
void set_warm_white(float warm_white) { this->warm_white_ = clamp(warm_white, 0.0f, 1.0f); }
|
||||
|
||||
protected:
|
||||
ColorMode color_mode_;
|
||||
float state_; ///< ON / OFF, float for transition
|
||||
float brightness_;
|
||||
float color_brightness_;
|
||||
@@ -303,6 +302,7 @@ class LightColorValues {
|
||||
float color_temperature_; ///< Color Temperature in Mired
|
||||
float cold_white_;
|
||||
float warm_white_;
|
||||
ColorMode color_mode_;
|
||||
};
|
||||
|
||||
} // namespace light
|
||||
|
||||
@@ -31,9 +31,7 @@ enum LightRestoreMode : uint8_t {
|
||||
struct LightStateRTCState {
|
||||
LightStateRTCState(ColorMode color_mode, bool state, float brightness, float color_brightness, float red, float green,
|
||||
float blue, float white, float color_temp, float cold_white, float warm_white)
|
||||
: color_mode(color_mode),
|
||||
state(state),
|
||||
brightness(brightness),
|
||||
: brightness(brightness),
|
||||
color_brightness(color_brightness),
|
||||
red(red),
|
||||
green(green),
|
||||
@@ -41,10 +39,12 @@ struct LightStateRTCState {
|
||||
white(white),
|
||||
color_temp(color_temp),
|
||||
cold_white(cold_white),
|
||||
warm_white(warm_white) {}
|
||||
warm_white(warm_white),
|
||||
effect(0),
|
||||
color_mode(color_mode),
|
||||
state(state) {}
|
||||
LightStateRTCState() = default;
|
||||
ColorMode color_mode{ColorMode::UNKNOWN};
|
||||
bool state{false};
|
||||
// Group 4-byte aligned members first
|
||||
float brightness{1.0f};
|
||||
float color_brightness{1.0f};
|
||||
float red{1.0f};
|
||||
@@ -55,6 +55,9 @@ struct LightStateRTCState {
|
||||
float cold_white{1.0f};
|
||||
float warm_white{1.0f};
|
||||
uint32_t effect{0};
|
||||
// Group smaller members at the end
|
||||
ColorMode color_mode{ColorMode::UNKNOWN};
|
||||
bool state{false};
|
||||
};
|
||||
|
||||
/** This class represents the communication layer between the front-end MQTT layer and the
|
||||
@@ -216,6 +219,8 @@ class LightState : public EntityBase, public Component {
|
||||
std::unique_ptr<LightTransformer> transformer_{nullptr};
|
||||
/// List of effects for this light.
|
||||
std::vector<LightEffect *> effects_;
|
||||
/// Object used to store the persisted values of the light.
|
||||
ESPPreferenceObject rtc_;
|
||||
/// Value for storing the index of the currently active effect. 0 if no effect is active
|
||||
uint32_t active_effect_index_{};
|
||||
/// Default transition length for all transitions in ms.
|
||||
@@ -224,15 +229,11 @@ class LightState : public EntityBase, public Component {
|
||||
uint32_t flash_transition_length_{};
|
||||
/// Gamma correction factor for the light.
|
||||
float gamma_correct_{};
|
||||
|
||||
/// Whether the light value should be written in the next cycle.
|
||||
bool next_write_{true};
|
||||
// for effects, true if a transformer (transition) is active.
|
||||
bool is_transformer_active_ = false;
|
||||
|
||||
/// Object used to store the persisted values of the light.
|
||||
ESPPreferenceObject rtc_;
|
||||
|
||||
/** Callback to call when new values for the frontend are available.
|
||||
*
|
||||
* "Remote values" are light color values that are reported to the frontend and have a lower
|
||||
|
||||
@@ -59,9 +59,9 @@ class LightTransitionTransformer : public LightTransformer {
|
||||
// transition from 0 to 1 on x = [0, 1]
|
||||
static float smoothed_progress(float x) { return x * x * x * (x * (x * 6.0f - 15.0f) + 10.0f); }
|
||||
|
||||
bool changing_color_mode_{false};
|
||||
LightColorValues end_values_{};
|
||||
LightColorValues intermediate_values_{};
|
||||
bool changing_color_mode_{false};
|
||||
};
|
||||
|
||||
class LightFlashTransformer : public LightTransformer {
|
||||
@@ -117,8 +117,8 @@ class LightFlashTransformer : public LightTransformer {
|
||||
|
||||
protected:
|
||||
LightState &state_;
|
||||
uint32_t transition_length_;
|
||||
std::unique_ptr<LightTransformer> transformer_{nullptr};
|
||||
uint32_t transition_length_;
|
||||
bool begun_lightstate_restore_;
|
||||
};
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ from esphome.components.libretiny.const import (
|
||||
COMPONENT_LN882X,
|
||||
COMPONENT_RTL87XX,
|
||||
)
|
||||
from esphome.config_helpers import filter_source_files_from_platform
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_ARGS,
|
||||
@@ -42,6 +43,7 @@ from esphome.const import (
|
||||
PLATFORM_LN882X,
|
||||
PLATFORM_RP2040,
|
||||
PLATFORM_RTL87XX,
|
||||
PlatformFramework,
|
||||
)
|
||||
from esphome.core import CORE, Lambda, coroutine_with_priority
|
||||
|
||||
@@ -444,3 +446,25 @@ async def logger_set_level_to_code(config, action_id, template_arg, args):
|
||||
|
||||
lambda_ = await cg.process_lambda(Lambda(text), args, return_type=cg.void)
|
||||
return cg.new_Pvariable(action_id, template_arg, lambda_)
|
||||
|
||||
|
||||
FILTER_SOURCE_FILES = filter_source_files_from_platform(
|
||||
{
|
||||
"logger_esp32.cpp": {
|
||||
PlatformFramework.ESP32_ARDUINO,
|
||||
PlatformFramework.ESP32_IDF,
|
||||
},
|
||||
"logger_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO},
|
||||
"logger_host.cpp": {PlatformFramework.HOST_NATIVE},
|
||||
"logger_rp2040.cpp": {PlatformFramework.RP2040_ARDUINO},
|
||||
"logger_libretiny.cpp": {
|
||||
PlatformFramework.BK72XX_ARDUINO,
|
||||
PlatformFramework.RTL87XX_ARDUINO,
|
||||
PlatformFramework.LN882X_ARDUINO,
|
||||
},
|
||||
"task_log_buffer.cpp": {
|
||||
PlatformFramework.ESP32_ARDUINO,
|
||||
PlatformFramework.ESP32_IDF,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -90,6 +90,25 @@ void HOT Logger::log_vprintf_(uint8_t level, const char *tag, int line, const ch
|
||||
#ifdef USE_STORE_LOG_STR_IN_FLASH
|
||||
// Implementation for ESP8266 with flash string support.
|
||||
// Note: USE_STORE_LOG_STR_IN_FLASH is only defined for ESP8266.
|
||||
//
|
||||
// This function handles format strings stored in flash memory (PROGMEM) to save RAM.
|
||||
// The buffer is used in a special way to avoid allocating extra memory:
|
||||
//
|
||||
// Memory layout during execution:
|
||||
// Step 1: Copy format string from flash to buffer
|
||||
// tx_buffer_: [format_string][null][.....................]
|
||||
// tx_buffer_at_: ------------------^
|
||||
// msg_start: saved here -----------^
|
||||
//
|
||||
// Step 2: format_log_to_buffer_with_terminator_ reads format string from beginning
|
||||
// and writes formatted output starting at msg_start position
|
||||
// tx_buffer_: [format_string][null][formatted_message][null]
|
||||
// tx_buffer_at_: -------------------------------------^
|
||||
//
|
||||
// Step 3: Output the formatted message (starting at msg_start)
|
||||
// write_msg_ and callbacks receive: this->tx_buffer_ + msg_start
|
||||
// which points to: [formatted_message][null]
|
||||
//
|
||||
void Logger::log_vprintf_(uint8_t level, const char *tag, int line, const __FlashStringHelper *format,
|
||||
va_list args) { // NOLINT
|
||||
if (level > this->level_for(tag) || global_recursion_guard_)
|
||||
@@ -121,7 +140,9 @@ void Logger::log_vprintf_(uint8_t level, const char *tag, int line, const __Flas
|
||||
if (this->baud_rate_ > 0) {
|
||||
this->write_msg_(this->tx_buffer_ + msg_start);
|
||||
}
|
||||
this->log_callback_.call(level, tag, this->tx_buffer_ + msg_start);
|
||||
size_t msg_length =
|
||||
this->tx_buffer_at_ - msg_start; // Don't subtract 1 - tx_buffer_at_ is already at the null terminator position
|
||||
this->log_callback_.call(level, tag, this->tx_buffer_ + msg_start, msg_length);
|
||||
|
||||
global_recursion_guard_ = false;
|
||||
}
|
||||
@@ -185,7 +206,8 @@ void Logger::loop() {
|
||||
this->tx_buffer_size_);
|
||||
this->write_footer_to_buffer_(this->tx_buffer_, &this->tx_buffer_at_, this->tx_buffer_size_);
|
||||
this->tx_buffer_[this->tx_buffer_at_] = '\0';
|
||||
this->log_callback_.call(message->level, message->tag, this->tx_buffer_);
|
||||
size_t msg_len = this->tx_buffer_at_; // We already know the length from tx_buffer_at_
|
||||
this->log_callback_.call(message->level, message->tag, this->tx_buffer_, msg_len);
|
||||
// At this point all the data we need from message has been transferred to the tx_buffer
|
||||
// so we can release the message to allow other tasks to use it as soon as possible.
|
||||
this->log_buffer_->release_message_main_loop(received_token);
|
||||
@@ -214,7 +236,7 @@ void Logger::set_log_level(const std::string &tag, uint8_t log_level) { this->lo
|
||||
UARTSelection Logger::get_uart() const { return this->uart_; }
|
||||
#endif
|
||||
|
||||
void Logger::add_on_log_callback(std::function<void(uint8_t, const char *, const char *)> &&callback) {
|
||||
void Logger::add_on_log_callback(std::function<void(uint8_t, const char *, const char *, size_t)> &&callback) {
|
||||
this->log_callback_.add(std::move(callback));
|
||||
}
|
||||
float Logger::get_setup_priority() const { return setup_priority::BUS + 500.0f; }
|
||||
|
||||
@@ -143,7 +143,7 @@ class Logger : public Component {
|
||||
inline uint8_t level_for(const char *tag);
|
||||
|
||||
/// Register a callback that will be called for every log message sent
|
||||
void add_on_log_callback(std::function<void(uint8_t, const char *, const char *)> &&callback);
|
||||
void add_on_log_callback(std::function<void(uint8_t, const char *, const char *, size_t)> &&callback);
|
||||
|
||||
// add a listener for log level changes
|
||||
void add_listener(std::function<void(uint8_t)> &&callback) { this->level_callback_.add(std::move(callback)); }
|
||||
@@ -192,7 +192,7 @@ class Logger : public Component {
|
||||
if (this->baud_rate_ > 0) {
|
||||
this->write_msg_(this->tx_buffer_); // If logging is enabled, write to console
|
||||
}
|
||||
this->log_callback_.call(level, tag, this->tx_buffer_);
|
||||
this->log_callback_.call(level, tag, this->tx_buffer_, this->tx_buffer_at_);
|
||||
}
|
||||
|
||||
// Write the body of the log message to the buffer
|
||||
@@ -246,7 +246,7 @@ class Logger : public Component {
|
||||
|
||||
// Large objects (internally aligned)
|
||||
std::map<std::string, uint8_t> log_levels_{};
|
||||
CallbackManager<void(uint8_t, const char *, const char *)> log_callback_{};
|
||||
CallbackManager<void(uint8_t, const char *, const char *, size_t)> log_callback_{};
|
||||
CallbackManager<void(uint8_t)> level_callback_{};
|
||||
#ifdef USE_ESPHOME_TASK_LOG_BUFFER
|
||||
std::unique_ptr<logger::TaskLogBuffer> log_buffer_; // Will be initialized with init_log_buffer
|
||||
@@ -355,7 +355,7 @@ class Logger : public Component {
|
||||
}
|
||||
|
||||
inline void HOT write_footer_to_buffer_(char *buffer, uint16_t *buffer_at, uint16_t buffer_size) {
|
||||
static const uint16_t RESET_COLOR_LEN = strlen(ESPHOME_LOG_RESET_COLOR);
|
||||
static constexpr uint16_t RESET_COLOR_LEN = sizeof(ESPHOME_LOG_RESET_COLOR) - 1;
|
||||
this->write_body_to_buffer_(ESPHOME_LOG_RESET_COLOR, RESET_COLOR_LEN, buffer, buffer_at, buffer_size);
|
||||
}
|
||||
|
||||
@@ -385,7 +385,7 @@ class LoggerMessageTrigger : public Trigger<uint8_t, const char *, const char *>
|
||||
public:
|
||||
explicit LoggerMessageTrigger(Logger *parent, uint8_t level) {
|
||||
this->level_ = level;
|
||||
parent->add_on_log_callback([this](uint8_t level, const char *tag, const char *message) {
|
||||
parent->add_on_log_callback([this](uint8_t level, const char *tag, const char *message, size_t message_len) {
|
||||
if (level <= this->level_) {
|
||||
this->trigger(level, tag, message);
|
||||
}
|
||||
|
||||
@@ -184,7 +184,9 @@ void HOT Logger::write_msg_(const char *msg) {
|
||||
) {
|
||||
puts(msg);
|
||||
} else {
|
||||
uart_write_bytes(this->uart_num_, msg, strlen(msg));
|
||||
// Use tx_buffer_at_ if msg points to tx_buffer_, otherwise fall back to strlen
|
||||
size_t len = (msg == this->tx_buffer_) ? this->tx_buffer_at_ : strlen(msg);
|
||||
uart_write_bytes(this->uart_num_, msg, len);
|
||||
uart_write_bytes(this->uart_num_, "\n", 1);
|
||||
}
|
||||
}
|
||||
|
||||
0
esphome/components/lps22/__init__.py
Normal file
0
esphome/components/lps22/__init__.py
Normal file
75
esphome/components/lps22/lps22.cpp
Normal file
75
esphome/components/lps22/lps22.cpp
Normal file
@@ -0,0 +1,75 @@
|
||||
#include "lps22.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace lps22 {
|
||||
|
||||
static constexpr const char *const TAG = "lps22";
|
||||
|
||||
static constexpr uint8_t WHO_AM_I = 0x0F;
|
||||
static constexpr uint8_t LPS22HB_ID = 0xB1;
|
||||
static constexpr uint8_t LPS22HH_ID = 0xB3;
|
||||
static constexpr uint8_t CTRL_REG2 = 0x11;
|
||||
static constexpr uint8_t CTRL_REG2_ONE_SHOT_MASK = 0b1;
|
||||
static constexpr uint8_t STATUS = 0x27;
|
||||
static constexpr uint8_t STATUS_T_DA_MASK = 0b10;
|
||||
static constexpr uint8_t STATUS_P_DA_MASK = 0b01;
|
||||
static constexpr uint8_t TEMP_L = 0x2b;
|
||||
static constexpr uint8_t PRES_OUT_XL = 0x28;
|
||||
static constexpr uint8_t REF_P_XL = 0x28;
|
||||
static constexpr uint8_t READ_ATTEMPTS = 10;
|
||||
static constexpr uint8_t READ_INTERVAL = 5;
|
||||
static constexpr float PRESSURE_SCALE = 1.0f / 4096.0f;
|
||||
static constexpr float TEMPERATURE_SCALE = 0.01f;
|
||||
|
||||
void LPS22Component::setup() {
|
||||
uint8_t value = 0x00;
|
||||
this->read_register(WHO_AM_I, &value, 1);
|
||||
if (value != LPS22HB_ID && value != LPS22HH_ID) {
|
||||
ESP_LOGW(TAG, "device IDs as %02x, which isn't a known LPS22HB or LPS22HH ID", value);
|
||||
this->mark_failed();
|
||||
}
|
||||
}
|
||||
|
||||
void LPS22Component::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "LPS22:");
|
||||
LOG_SENSOR(" ", "Temperature", this->temperature_sensor_);
|
||||
LOG_SENSOR(" ", "Pressure", this->pressure_sensor_);
|
||||
LOG_I2C_DEVICE(this);
|
||||
LOG_UPDATE_INTERVAL(this);
|
||||
}
|
||||
|
||||
void LPS22Component::update() {
|
||||
uint8_t value = 0x00;
|
||||
this->read_register(CTRL_REG2, &value, 1);
|
||||
value |= CTRL_REG2_ONE_SHOT_MASK;
|
||||
this->write_register(CTRL_REG2, &value, 1);
|
||||
this->set_retry(READ_INTERVAL, READ_ATTEMPTS, [this](uint8_t _) { return this->try_read_(); });
|
||||
}
|
||||
|
||||
RetryResult LPS22Component::try_read_() {
|
||||
uint8_t value = 0x00;
|
||||
this->read_register(STATUS, &value, 1);
|
||||
const uint8_t expected_status_mask = STATUS_T_DA_MASK | STATUS_P_DA_MASK;
|
||||
if ((value & expected_status_mask) != expected_status_mask) {
|
||||
ESP_LOGD(TAG, "STATUS not ready: %x", value);
|
||||
return RetryResult::RETRY;
|
||||
}
|
||||
|
||||
if (this->temperature_sensor_ != nullptr) {
|
||||
uint8_t t_buf[2]{0};
|
||||
this->read_register(TEMP_L, t_buf, 2);
|
||||
int16_t encoded = static_cast<int16_t>(encode_uint16(t_buf[1], t_buf[0]));
|
||||
float temp = TEMPERATURE_SCALE * static_cast<float>(encoded);
|
||||
this->temperature_sensor_->publish_state(temp);
|
||||
}
|
||||
if (this->pressure_sensor_ != nullptr) {
|
||||
uint8_t p_buf[3]{0};
|
||||
this->read_register(PRES_OUT_XL, p_buf, 3);
|
||||
uint32_t p_lsb = encode_uint24(p_buf[2], p_buf[1], p_buf[0]);
|
||||
this->pressure_sensor_->publish_state(PRESSURE_SCALE * static_cast<float>(p_lsb));
|
||||
}
|
||||
return RetryResult::DONE;
|
||||
}
|
||||
|
||||
} // namespace lps22
|
||||
} // namespace esphome
|
||||
27
esphome/components/lps22/lps22.h
Normal file
27
esphome/components/lps22/lps22.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include "esphome/core/component.h"
|
||||
#include "esphome/components/sensor/sensor.h"
|
||||
#include "esphome/components/i2c/i2c.h"
|
||||
|
||||
namespace esphome {
|
||||
namespace lps22 {
|
||||
|
||||
class LPS22Component : public sensor::Sensor, public PollingComponent, public i2c::I2CDevice {
|
||||
public:
|
||||
void set_temperature_sensor(sensor::Sensor *temperature_sensor) { this->temperature_sensor_ = temperature_sensor; }
|
||||
void set_pressure_sensor(sensor::Sensor *pressure_sensor) { this->pressure_sensor_ = pressure_sensor; }
|
||||
|
||||
void setup() override;
|
||||
void update() override;
|
||||
void dump_config() override;
|
||||
|
||||
protected:
|
||||
sensor::Sensor *temperature_sensor_{nullptr};
|
||||
sensor::Sensor *pressure_sensor_{nullptr};
|
||||
|
||||
RetryResult try_read_();
|
||||
};
|
||||
|
||||
} // namespace lps22
|
||||
} // namespace esphome
|
||||
58
esphome/components/lps22/sensor.py
Normal file
58
esphome/components/lps22/sensor.py
Normal file
@@ -0,0 +1,58 @@
|
||||
import esphome.codegen as cg
|
||||
import esphome.config_validation as cv
|
||||
from esphome.components import i2c, sensor
|
||||
from esphome.const import (
|
||||
CONF_ID,
|
||||
CONF_TEMPERATURE,
|
||||
CONF_PRESSURE,
|
||||
STATE_CLASS_MEASUREMENT,
|
||||
UNIT_CELSIUS,
|
||||
UNIT_HECTOPASCAL,
|
||||
ICON_THERMOMETER,
|
||||
DEVICE_CLASS_TEMPERATURE,
|
||||
DEVICE_CLASS_PRESSURE,
|
||||
)
|
||||
|
||||
CODEOWNERS = ["@nagisa"]
|
||||
DEPENDENCIES = ["i2c"]
|
||||
|
||||
lps22 = cg.esphome_ns.namespace("lps22")
|
||||
|
||||
LPS22Component = lps22.class_("LPS22Component", cg.PollingComponent, i2c.I2CDevice)
|
||||
|
||||
CONFIG_SCHEMA = (
|
||||
cv.Schema(
|
||||
{
|
||||
cv.GenerateID(): cv.declare_id(LPS22Component),
|
||||
cv.Optional(CONF_TEMPERATURE): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_CELSIUS,
|
||||
icon=ICON_THERMOMETER,
|
||||
accuracy_decimals=2,
|
||||
device_class=DEVICE_CLASS_TEMPERATURE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
cv.Optional(CONF_PRESSURE): sensor.sensor_schema(
|
||||
unit_of_measurement=UNIT_HECTOPASCAL,
|
||||
accuracy_decimals=2,
|
||||
device_class=DEVICE_CLASS_PRESSURE,
|
||||
state_class=STATE_CLASS_MEASUREMENT,
|
||||
),
|
||||
}
|
||||
)
|
||||
.extend(cv.polling_component_schema("60s"))
|
||||
.extend(i2c.i2c_device_schema(0x5D)) # can also be 0x5C
|
||||
)
|
||||
|
||||
|
||||
async def to_code(config):
|
||||
var = cg.new_Pvariable(config[CONF_ID])
|
||||
await cg.register_component(var, config)
|
||||
await i2c.register_i2c_device(var, config)
|
||||
|
||||
if temperature_config := config.get(CONF_TEMPERATURE):
|
||||
sens = await sensor.new_sensor(temperature_config)
|
||||
cg.add(var.set_temperature_sensor(sens))
|
||||
|
||||
if pressure_config := config.get(CONF_PRESSURE):
|
||||
sens = await sensor.new_sensor(pressure_config)
|
||||
cg.add(var.set_pressure_sensor(sens))
|
||||
@@ -1,5 +1,6 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components.esp32 import add_idf_component
|
||||
from esphome.config_helpers import filter_source_files_from_platform
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_DISABLED,
|
||||
@@ -8,6 +9,7 @@ from esphome.const import (
|
||||
CONF_PROTOCOL,
|
||||
CONF_SERVICE,
|
||||
CONF_SERVICES,
|
||||
PlatformFramework,
|
||||
)
|
||||
from esphome.core import CORE, coroutine_with_priority
|
||||
|
||||
@@ -108,3 +110,21 @@ async def to_code(config):
|
||||
)
|
||||
|
||||
cg.add(var.add_extra_service(exp))
|
||||
|
||||
|
||||
FILTER_SOURCE_FILES = filter_source_files_from_platform(
|
||||
{
|
||||
"mdns_esp32.cpp": {
|
||||
PlatformFramework.ESP32_ARDUINO,
|
||||
PlatformFramework.ESP32_IDF,
|
||||
},
|
||||
"mdns_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO},
|
||||
"mdns_host.cpp": {PlatformFramework.HOST_NATIVE},
|
||||
"mdns_rp2040.cpp": {PlatformFramework.RP2040_ARDUINO},
|
||||
"mdns_libretiny.cpp": {
|
||||
PlatformFramework.BK72XX_ARDUINO,
|
||||
PlatformFramework.RTL87XX_ARDUINO,
|
||||
PlatformFramework.LN882X_ARDUINO,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -5,6 +5,7 @@ from esphome.automation import Condition
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import logger
|
||||
from esphome.components.esp32 import add_idf_sdkconfig_option
|
||||
from esphome.config_helpers import filter_source_files_from_platform
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_AVAILABILITY,
|
||||
@@ -54,6 +55,7 @@ from esphome.const import (
|
||||
PLATFORM_BK72XX,
|
||||
PLATFORM_ESP32,
|
||||
PLATFORM_ESP8266,
|
||||
PlatformFramework,
|
||||
)
|
||||
from esphome.core import CORE, coroutine_with_priority
|
||||
|
||||
@@ -596,3 +598,13 @@ async def mqtt_enable_to_code(config, action_id, template_arg, args):
|
||||
async def mqtt_disable_to_code(config, action_id, template_arg, args):
|
||||
paren = await cg.get_variable(config[CONF_ID])
|
||||
return cg.new_Pvariable(action_id, template_arg, paren)
|
||||
|
||||
|
||||
FILTER_SOURCE_FILES = filter_source_files_from_platform(
|
||||
{
|
||||
"mqtt_backend_esp32.cpp": {
|
||||
PlatformFramework.ESP32_ARDUINO,
|
||||
PlatformFramework.ESP32_IDF,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -57,14 +57,15 @@ void MQTTClientComponent::setup() {
|
||||
});
|
||||
#ifdef USE_LOGGER
|
||||
if (this->is_log_message_enabled() && logger::global_logger != nullptr) {
|
||||
logger::global_logger->add_on_log_callback([this](int level, const char *tag, const char *message) {
|
||||
if (level <= this->log_level_ && this->is_connected()) {
|
||||
this->publish({.topic = this->log_message_.topic,
|
||||
.payload = message,
|
||||
.qos = this->log_message_.qos,
|
||||
.retain = this->log_message_.retain});
|
||||
}
|
||||
});
|
||||
logger::global_logger->add_on_log_callback(
|
||||
[this](int level, const char *tag, const char *message, size_t message_len) {
|
||||
if (level <= this->log_level_ && this->is_connected()) {
|
||||
this->publish({.topic = this->log_message_.topic,
|
||||
.payload = std::string(message, message_len),
|
||||
.qos = this->log_message_.qos,
|
||||
.retain = this->log_message_.retain});
|
||||
}
|
||||
});
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import uart
|
||||
from esphome.config_helpers import filter_source_files_from_platform
|
||||
from esphome.const import PlatformFramework
|
||||
|
||||
nextion_ns = cg.esphome_ns.namespace("nextion")
|
||||
Nextion = nextion_ns.class_("Nextion", cg.PollingComponent, uart.UARTDevice)
|
||||
@@ -8,3 +10,17 @@ nextion_ref = Nextion.operator("ref")
|
||||
CONF_NEXTION_ID = "nextion_id"
|
||||
CONF_PUBLISH_STATE = "publish_state"
|
||||
CONF_SEND_TO_NEXTION = "send_to_nextion"
|
||||
|
||||
FILTER_SOURCE_FILES = filter_source_files_from_platform(
|
||||
{
|
||||
"nextion_upload_arduino.cpp": {
|
||||
PlatformFramework.ESP32_ARDUINO,
|
||||
PlatformFramework.ESP8266_ARDUINO,
|
||||
PlatformFramework.RP2040_ARDUINO,
|
||||
PlatformFramework.BK72XX_ARDUINO,
|
||||
PlatformFramework.RTL87XX_ARDUINO,
|
||||
PlatformFramework.LN882X_ARDUINO,
|
||||
},
|
||||
"nextion_upload_idf.cpp": {PlatformFramework.ESP32_IDF},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -11,6 +11,7 @@ CONF_AUTO_WAKE_ON_TOUCH = "auto_wake_on_touch"
|
||||
CONF_BACKGROUND_PRESSED_COLOR = "background_pressed_color"
|
||||
CONF_COMMAND_SPACING = "command_spacing"
|
||||
CONF_COMPONENT_NAME = "component_name"
|
||||
CONF_DUMP_DEVICE_INFO = "dump_device_info"
|
||||
CONF_EXIT_REPARSE_ON_START = "exit_reparse_on_start"
|
||||
CONF_FONT_ID = "font_id"
|
||||
CONF_FOREGROUND_PRESSED_COLOR = "foreground_pressed_color"
|
||||
|
||||
@@ -44,7 +44,7 @@ void NextionBinarySensor::set_state(bool state, bool publish, bool send_to_nexti
|
||||
return;
|
||||
|
||||
if (send_to_nextion) {
|
||||
if (this->nextion_->is_sleeping() || !this->visible_) {
|
||||
if (this->nextion_->is_sleeping() || !this->component_flags_.visible) {
|
||||
this->needs_to_send_update_ = true;
|
||||
} else {
|
||||
this->needs_to_send_update_ = false;
|
||||
|
||||
@@ -15,6 +15,7 @@ from . import Nextion, nextion_ns, nextion_ref
|
||||
from .base_component import (
|
||||
CONF_AUTO_WAKE_ON_TOUCH,
|
||||
CONF_COMMAND_SPACING,
|
||||
CONF_DUMP_DEVICE_INFO,
|
||||
CONF_EXIT_REPARSE_ON_START,
|
||||
CONF_MAX_COMMANDS_PER_LOOP,
|
||||
CONF_MAX_QUEUE_SIZE,
|
||||
@@ -57,6 +58,7 @@ CONFIG_SCHEMA = (
|
||||
cv.positive_time_period_milliseconds,
|
||||
cv.Range(max=TimePeriod(milliseconds=255)),
|
||||
),
|
||||
cv.Optional(CONF_DUMP_DEVICE_INFO, default=False): cv.boolean,
|
||||
cv.Optional(CONF_EXIT_REPARSE_ON_START, default=False): cv.boolean,
|
||||
cv.Optional(CONF_MAX_COMMANDS_PER_LOOP): cv.uint16_t,
|
||||
cv.Optional(CONF_MAX_QUEUE_SIZE): cv.positive_int,
|
||||
@@ -95,7 +97,9 @@ CONFIG_SCHEMA = (
|
||||
cv.Optional(CONF_SKIP_CONNECTION_HANDSHAKE, default=False): cv.boolean,
|
||||
cv.Optional(CONF_START_UP_PAGE): cv.uint8_t,
|
||||
cv.Optional(CONF_TFT_URL): cv.url,
|
||||
cv.Optional(CONF_TOUCH_SLEEP_TIMEOUT): cv.int_range(min=3, max=65535),
|
||||
cv.Optional(CONF_TOUCH_SLEEP_TIMEOUT): cv.Any(
|
||||
0, cv.int_range(min=3, max=65535)
|
||||
),
|
||||
cv.Optional(CONF_WAKE_UP_PAGE): cv.uint8_t,
|
||||
}
|
||||
)
|
||||
@@ -167,13 +171,19 @@ 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]))
|
||||
|
||||
cg.add(var.set_exit_reparse_on_start(config[CONF_EXIT_REPARSE_ON_START]))
|
||||
if config[CONF_DUMP_DEVICE_INFO]:
|
||||
cg.add_define("USE_NEXTION_CONFIG_DUMP_DEVICE_INFO")
|
||||
|
||||
cg.add(var.set_skip_connection_handshake(config[CONF_SKIP_CONNECTION_HANDSHAKE]))
|
||||
if config[CONF_EXIT_REPARSE_ON_START]:
|
||||
cg.add_define("USE_NEXTION_CONFIG_EXIT_REPARSE_ON_START")
|
||||
|
||||
if config[CONF_SKIP_CONNECTION_HANDSHAKE]:
|
||||
cg.add_define("USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE")
|
||||
|
||||
if max_commands_per_loop := config.get(CONF_MAX_COMMANDS_PER_LOOP):
|
||||
cg.add_define("USE_NEXTION_MAX_COMMANDS_PER_LOOP")
|
||||
|
||||
@@ -11,28 +11,25 @@ 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");
|
||||
this->send_command_("sleep=0");
|
||||
// Wake up the nextion and ensure clean communication state
|
||||
this->send_command_("sleep=0"); // Exit sleep mode if sleeping
|
||||
this->send_command_("bkcmd=0"); // Disable return data during init sequence
|
||||
|
||||
this->send_command_("bkcmd=0");
|
||||
this->send_command_("sleep=0");
|
||||
|
||||
// Reboot it
|
||||
// Reset device for clean state - critical for reliable communication
|
||||
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,31 +45,26 @@ 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
|
||||
if (this->skip_connection_handshake_) {
|
||||
// Log the connection status without handshake
|
||||
ESP_LOGW(TAG, "Connected (no handshake)");
|
||||
// Set the connection status to true
|
||||
this->is_connected_ = true;
|
||||
// Return true indicating the connection is set
|
||||
return true;
|
||||
}
|
||||
|
||||
#ifdef USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE
|
||||
ESP_LOGW(TAG, "Connected (no handshake)"); // Log the connection status without handshake
|
||||
this->is_connected_ = true; // Set the connection status to true
|
||||
return true; // Return true indicating the connection is set
|
||||
#else // USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE
|
||||
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");
|
||||
}
|
||||
#ifdef USE_NEXTION_CONFIG_EXIT_REPARSE_ON_START
|
||||
this->send_command_("DRAKJHSUYDGBNCJHGJKSHBDN");
|
||||
#endif // USE_NEXTION_CONFIG_EXIT_REPARSE_ON_START
|
||||
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;
|
||||
}
|
||||
@@ -94,16 +86,16 @@ bool Nextion::check_connect_() {
|
||||
for (size_t i = 0; i < response.length(); i++) {
|
||||
ESP_LOGN(TAG, "resp: %s %d %d %c", response.c_str(), i, response[i], response[i]);
|
||||
}
|
||||
#endif
|
||||
#endif // NEXTION_PROTOCOL_LOG
|
||||
|
||||
ESP_LOGW(TAG, "Not connected");
|
||||
comok_sent_ = 0;
|
||||
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());
|
||||
|
||||
@@ -118,18 +110,27 @@ bool Nextion::check_connect_() {
|
||||
this->is_detected_ = (connect_info.size() == 7);
|
||||
if (this->is_detected_) {
|
||||
ESP_LOGN(TAG, "Connect info: %zu", connect_info.size());
|
||||
|
||||
#ifdef USE_NEXTION_CONFIG_DUMP_DEVICE_INFO
|
||||
this->device_model_ = connect_info[2];
|
||||
this->firmware_version_ = connect_info[3];
|
||||
this->serial_number_ = connect_info[5];
|
||||
this->flash_size_ = connect_info[6];
|
||||
#else // USE_NEXTION_CONFIG_DUMP_DEVICE_INFO
|
||||
ESP_LOGI(TAG,
|
||||
" Device Model: %s\n"
|
||||
" FW Version: %s\n"
|
||||
" Serial Number: %s\n"
|
||||
" Flash Size: %s\n",
|
||||
connect_info[2].c_str(), connect_info[3].c_str(), connect_info[5].c_str(), connect_info[6].c_str());
|
||||
#endif // USE_NEXTION_CONFIG_DUMP_DEVICE_INFO
|
||||
} else {
|
||||
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;
|
||||
#endif // USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE
|
||||
}
|
||||
|
||||
void Nextion::reset_(bool reset_nextion) {
|
||||
@@ -144,36 +145,42 @@ void Nextion::reset_(bool reset_nextion) {
|
||||
|
||||
void Nextion::dump_config() {
|
||||
ESP_LOGCONFIG(TAG, "Nextion:");
|
||||
if (this->skip_connection_handshake_) {
|
||||
ESP_LOGCONFIG(TAG, " Skip handshake: %s", YESNO(this->skip_connection_handshake_));
|
||||
} else {
|
||||
ESP_LOGCONFIG(TAG,
|
||||
" Device Model: %s\n"
|
||||
" FW Version: %s\n"
|
||||
" Serial Number: %s\n"
|
||||
" Flash Size: %s",
|
||||
this->device_model_.c_str(), this->firmware_version_.c_str(), this->serial_number_.c_str(),
|
||||
this->flash_size_.c_str());
|
||||
}
|
||||
|
||||
#ifdef USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE
|
||||
ESP_LOGCONFIG(TAG, " Skip handshake: YES");
|
||||
#else // USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE
|
||||
ESP_LOGCONFIG(TAG,
|
||||
#ifdef USE_NEXTION_CONFIG_DUMP_DEVICE_INFO
|
||||
" Device Model: %s\n"
|
||||
" FW Version: %s\n"
|
||||
" Serial Number: %s\n"
|
||||
" Flash Size: %s\n"
|
||||
#endif // USE_NEXTION_CONFIG_DUMP_DEVICE_INFO
|
||||
#ifdef USE_NEXTION_CONFIG_EXIT_REPARSE_ON_START
|
||||
" Exit reparse: YES\n"
|
||||
#endif // USE_NEXTION_CONFIG_EXIT_REPARSE_ON_START
|
||||
" Wake On Touch: %s\n"
|
||||
" Exit reparse: %s",
|
||||
YESNO(this->auto_wake_on_touch_), YESNO(this->exit_reparse_on_start_));
|
||||
" Touch Timeout: %" PRIu16,
|
||||
#ifdef USE_NEXTION_CONFIG_DUMP_DEVICE_INFO
|
||||
this->device_model_.c_str(), this->firmware_version_.c_str(), this->serial_number_.c_str(),
|
||||
this->flash_size_.c_str(),
|
||||
#endif // USE_NEXTION_CONFIG_DUMP_DEVICE_INFO
|
||||
YESNO(this->connection_state_.auto_wake_on_touch_), this->touch_sleep_timeout_);
|
||||
#endif // USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE
|
||||
|
||||
#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
|
||||
|
||||
if (this->touch_sleep_timeout_ != 0) {
|
||||
ESP_LOGCONFIG(TAG, " Touch Timeout: %" PRIu16, this->touch_sleep_timeout_);
|
||||
if (this->wake_up_page_ != 255) {
|
||||
ESP_LOGCONFIG(TAG, " Wake Up Page: %u", this->wake_up_page_);
|
||||
}
|
||||
|
||||
if (this->wake_up_page_ != -1) {
|
||||
ESP_LOGCONFIG(TAG, " Wake Up Page: %d", 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 +226,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 +244,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 +255,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 +296,46 @@ 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;
|
||||
if (this->touch_sleep_timeout_ != 0) {
|
||||
this->set_touch_sleep_timeout(this->touch_sleep_timeout_);
|
||||
}
|
||||
|
||||
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 +678,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 +1061,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 +1104,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 +1129,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 +1168,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 +1196,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 +1213,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 +1253,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 +1294,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
|
||||
|
||||
@@ -932,21 +932,6 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
|
||||
*/
|
||||
void set_backlight_brightness(float brightness);
|
||||
|
||||
/**
|
||||
* Sets whether the Nextion display should skip the connection handshake process.
|
||||
* @param skip_handshake True or false. When skip_connection_handshake is true,
|
||||
* the connection will be established without performing the handshake.
|
||||
* This can be useful when using Nextion Simulator.
|
||||
*
|
||||
* Example:
|
||||
* ```cpp
|
||||
* it.set_skip_connection_handshake(true);
|
||||
* ```
|
||||
*
|
||||
* When set to true, the display will be marked as connected without performing a handshake.
|
||||
*/
|
||||
void set_skip_connection_handshake(bool skip_handshake) { this->skip_connection_handshake_ = skip_handshake; }
|
||||
|
||||
/**
|
||||
* Sets Nextion mode between sleep and awake
|
||||
* @param True or false. Sleep=true to enter sleep mode or sleep=false to exit sleep mode.
|
||||
@@ -1179,22 +1164,43 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
|
||||
void update_components_by_prefix(const std::string &prefix);
|
||||
|
||||
/**
|
||||
* Set the touch sleep timeout of the display.
|
||||
* @param timeout Timeout in seconds.
|
||||
* Set the touch sleep timeout of the display using the `thsp` command.
|
||||
*
|
||||
* Sets internal No-touch-then-sleep timer to specified value in seconds.
|
||||
* Nextion will auto-enter sleep mode if and when this timer expires.
|
||||
*
|
||||
* @param touch_sleep_timeout Timeout in seconds.
|
||||
* Range: 3 to 65535 seconds (minimum 3 seconds, maximum ~18 hours 12 minutes 15 seconds)
|
||||
* Use 0 to disable touch sleep timeout.
|
||||
*
|
||||
* @note Once `thsp` is set, it will persist until reboot or reset. The Nextion device
|
||||
* needs to exit sleep mode to issue `thsp=0` to disable sleep on no touch.
|
||||
*
|
||||
* @note The display will only wake up by a restart or by setting up `thup` (auto wake on touch).
|
||||
* See set_auto_wake_on_touch() to configure wake behavior.
|
||||
*
|
||||
* Example:
|
||||
* ```cpp
|
||||
* // Set 30 second touch timeout
|
||||
* it.set_touch_sleep_timeout(30);
|
||||
*
|
||||
* // Set maximum timeout (~18 hours)
|
||||
* it.set_touch_sleep_timeout(65535);
|
||||
*
|
||||
* // Disable touch sleep timeout
|
||||
* it.set_touch_sleep_timeout(0);
|
||||
* ```
|
||||
*
|
||||
* After 30 seconds the display will go to sleep. Note: the display will only wakeup by a restart or by setting up
|
||||
* `thup`.
|
||||
* Related Nextion instruction: `thsp=<value>`
|
||||
*
|
||||
* @see set_auto_wake_on_touch() Configure automatic wake on touch
|
||||
* @see sleep() Manually control sleep state
|
||||
*/
|
||||
void set_touch_sleep_timeout(uint16_t touch_sleep_timeout);
|
||||
void set_touch_sleep_timeout(uint16_t touch_sleep_timeout = 0);
|
||||
|
||||
/**
|
||||
* 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 +1210,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 +1225,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.
|
||||
@@ -1234,20 +1242,6 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
|
||||
*/
|
||||
void set_auto_wake_on_touch(bool auto_wake_on_touch);
|
||||
|
||||
/**
|
||||
* Sets if Nextion should exit the active reparse mode before the "connect" command is sent
|
||||
* @param exit_reparse_on_start True or false. When exit_reparse_on_start is true, the exit reparse command
|
||||
* will be sent before requesting the connection from Nextion.
|
||||
*
|
||||
* Example:
|
||||
* ```cpp
|
||||
* it.set_exit_reparse_on_start(true);
|
||||
* ```
|
||||
*
|
||||
* The display will be requested to leave active reparse mode before setup.
|
||||
*/
|
||||
void set_exit_reparse_on_start(bool exit_reparse_on_start) { this->exit_reparse_on_start_ = exit_reparse_on_start; }
|
||||
|
||||
/**
|
||||
* @brief Retrieves the number of commands pending in the Nextion command queue.
|
||||
*
|
||||
@@ -1290,7 +1284,7 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
|
||||
* the Nextion display. A connection is considered established when:
|
||||
*
|
||||
* - The initial handshake with the display is completed successfully, or
|
||||
* - The handshake is skipped via skip_connection_handshake_ flag
|
||||
* - The handshake is skipped via USE_NEXTION_CONFIG_SKIP_CONNECTION_HANDSHAKE flag
|
||||
*
|
||||
* The connection status is particularly useful when:
|
||||
* - Troubleshooting communication issues
|
||||
@@ -1300,7 +1294,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,21 +1328,29 @@ 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;
|
||||
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 auto_wake_on_touch_ = true;
|
||||
bool exit_reparse_on_start_ = false;
|
||||
bool skip_connection_handshake_ = false;
|
||||
|
||||
/**
|
||||
* Manually send a raw command to the display and don't wait for an acknowledgement packet.
|
||||
@@ -1455,10 +1457,12 @@ class Nextion : public NextionBase, public PollingComponent, public uart::UARTDe
|
||||
optional<nextion_writer_t> writer_;
|
||||
optional<float> brightness_;
|
||||
|
||||
#ifdef USE_NEXTION_CONFIG_DUMP_DEVICE_INFO
|
||||
std::string device_model_;
|
||||
std::string firmware_version_;
|
||||
std::string serial_number_;
|
||||
std::string flash_size_;
|
||||
#endif // USE_NEXTION_CONFIG_DUMP_DEVICE_INFO
|
||||
|
||||
void remove_front_no_sensors_();
|
||||
|
||||
@@ -1468,11 +1472,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,19 +10,20 @@ 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);
|
||||
}
|
||||
|
||||
void Nextion::set_touch_sleep_timeout(uint16_t touch_sleep_timeout) {
|
||||
if (touch_sleep_timeout < 3) {
|
||||
ESP_LOGD(TAG, "Sleep timeout out of bounds (3-65535)");
|
||||
return;
|
||||
void Nextion::set_touch_sleep_timeout(const uint16_t touch_sleep_timeout) {
|
||||
// Validate range: Nextion thsp command requires min 3, max 65535 seconds (0 disables)
|
||||
if (touch_sleep_timeout != 0 && touch_sleep_timeout < 3) {
|
||||
this->touch_sleep_timeout_ = 3; // Auto-correct to minimum valid value
|
||||
} else {
|
||||
this->touch_sleep_timeout_ = touch_sleep_timeout;
|
||||
}
|
||||
|
||||
this->touch_sleep_timeout_ = touch_sleep_timeout;
|
||||
this->add_no_result_to_queue_with_set_internal_("touch_sleep_timeout", "thsp", touch_sleep_timeout, true);
|
||||
this->add_no_result_to_queue_with_set_internal_("touch_sleep_timeout", "thsp", this->touch_sleep_timeout_, true);
|
||||
}
|
||||
|
||||
void Nextion::sleep(bool sleep) {
|
||||
@@ -38,7 +39,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 +49,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 +192,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);
|
||||
}
|
||||
|
||||
|
||||
@@ -8,8 +8,8 @@ void NextionComponent::set_background_color(Color bco) {
|
||||
return; // This is a variable. no need to set color
|
||||
}
|
||||
this->bco_ = bco;
|
||||
this->bco_needs_update_ = true;
|
||||
this->bco_is_set_ = true;
|
||||
this->component_flags_.bco_needs_update = true;
|
||||
this->component_flags_.bco_is_set = true;
|
||||
this->update_component_settings();
|
||||
}
|
||||
|
||||
@@ -19,8 +19,8 @@ void NextionComponent::set_background_pressed_color(Color bco2) {
|
||||
}
|
||||
|
||||
this->bco2_ = bco2;
|
||||
this->bco2_needs_update_ = true;
|
||||
this->bco2_is_set_ = true;
|
||||
this->component_flags_.bco2_needs_update = true;
|
||||
this->component_flags_.bco2_is_set = true;
|
||||
this->update_component_settings();
|
||||
}
|
||||
|
||||
@@ -29,8 +29,8 @@ void NextionComponent::set_foreground_color(Color pco) {
|
||||
return; // This is a variable. no need to set color
|
||||
}
|
||||
this->pco_ = pco;
|
||||
this->pco_needs_update_ = true;
|
||||
this->pco_is_set_ = true;
|
||||
this->component_flags_.pco_needs_update = true;
|
||||
this->component_flags_.pco_is_set = true;
|
||||
this->update_component_settings();
|
||||
}
|
||||
|
||||
@@ -39,8 +39,8 @@ void NextionComponent::set_foreground_pressed_color(Color pco2) {
|
||||
return; // This is a variable. no need to set color
|
||||
}
|
||||
this->pco2_ = pco2;
|
||||
this->pco2_needs_update_ = true;
|
||||
this->pco2_is_set_ = true;
|
||||
this->component_flags_.pco2_needs_update = true;
|
||||
this->component_flags_.pco2_is_set = true;
|
||||
this->update_component_settings();
|
||||
}
|
||||
|
||||
@@ -49,8 +49,8 @@ void NextionComponent::set_font_id(uint8_t font_id) {
|
||||
return; // This is a variable. no need to set color
|
||||
}
|
||||
this->font_id_ = font_id;
|
||||
this->font_id_needs_update_ = true;
|
||||
this->font_id_is_set_ = true;
|
||||
this->component_flags_.font_id_needs_update = true;
|
||||
this->component_flags_.font_id_is_set = true;
|
||||
this->update_component_settings();
|
||||
}
|
||||
|
||||
@@ -58,20 +58,20 @@ void NextionComponent::set_visible(bool visible) {
|
||||
if (this->variable_name_ == this->variable_name_to_send_) {
|
||||
return; // This is a variable. no need to set color
|
||||
}
|
||||
this->visible_ = visible;
|
||||
this->visible_needs_update_ = true;
|
||||
this->visible_is_set_ = true;
|
||||
this->component_flags_.visible = visible;
|
||||
this->component_flags_.visible_needs_update = true;
|
||||
this->component_flags_.visible_is_set = true;
|
||||
this->update_component_settings();
|
||||
}
|
||||
|
||||
void NextionComponent::update_component_settings(bool force_update) {
|
||||
if (this->nextion_->is_sleeping() || !this->nextion_->is_setup() || !this->visible_is_set_ ||
|
||||
(!this->visible_needs_update_ && !this->visible_)) {
|
||||
if (this->nextion_->is_sleeping() || !this->nextion_->is_setup() || !this->component_flags_.visible_is_set ||
|
||||
(!this->component_flags_.visible_needs_update && !this->component_flags_.visible)) {
|
||||
this->needs_to_send_update_ = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (this->visible_needs_update_ || (force_update && this->visible_is_set_)) {
|
||||
if (this->component_flags_.visible_needs_update || (force_update && this->component_flags_.visible_is_set)) {
|
||||
std::string name_to_send = this->variable_name_;
|
||||
|
||||
size_t pos = name_to_send.find_last_of('.');
|
||||
@@ -79,9 +79,9 @@ void NextionComponent::update_component_settings(bool force_update) {
|
||||
name_to_send = name_to_send.substr(pos + 1);
|
||||
}
|
||||
|
||||
this->visible_needs_update_ = false;
|
||||
this->component_flags_.visible_needs_update = false;
|
||||
|
||||
if (this->visible_) {
|
||||
if (this->component_flags_.visible) {
|
||||
this->nextion_->show_component(name_to_send.c_str());
|
||||
this->send_state_to_nextion();
|
||||
} else {
|
||||
@@ -90,26 +90,26 @@ void NextionComponent::update_component_settings(bool force_update) {
|
||||
}
|
||||
}
|
||||
|
||||
if (this->bco_needs_update_ || (force_update && this->bco2_is_set_)) {
|
||||
if (this->component_flags_.bco_needs_update || (force_update && this->component_flags_.bco2_is_set)) {
|
||||
this->nextion_->set_component_background_color(this->variable_name_.c_str(), this->bco_);
|
||||
this->bco_needs_update_ = false;
|
||||
this->component_flags_.bco_needs_update = false;
|
||||
}
|
||||
if (this->bco2_needs_update_ || (force_update && this->bco2_is_set_)) {
|
||||
if (this->component_flags_.bco2_needs_update || (force_update && this->component_flags_.bco2_is_set)) {
|
||||
this->nextion_->set_component_pressed_background_color(this->variable_name_.c_str(), this->bco2_);
|
||||
this->bco2_needs_update_ = false;
|
||||
this->component_flags_.bco2_needs_update = false;
|
||||
}
|
||||
if (this->pco_needs_update_ || (force_update && this->pco_is_set_)) {
|
||||
if (this->component_flags_.pco_needs_update || (force_update && this->component_flags_.pco_is_set)) {
|
||||
this->nextion_->set_component_foreground_color(this->variable_name_.c_str(), this->pco_);
|
||||
this->pco_needs_update_ = false;
|
||||
this->component_flags_.pco_needs_update = false;
|
||||
}
|
||||
if (this->pco2_needs_update_ || (force_update && this->pco2_is_set_)) {
|
||||
if (this->component_flags_.pco2_needs_update || (force_update && this->component_flags_.pco2_is_set)) {
|
||||
this->nextion_->set_component_pressed_foreground_color(this->variable_name_.c_str(), this->pco2_);
|
||||
this->pco2_needs_update_ = false;
|
||||
this->component_flags_.pco2_needs_update = false;
|
||||
}
|
||||
|
||||
if (this->font_id_needs_update_ || (force_update && this->font_id_is_set_)) {
|
||||
if (this->component_flags_.font_id_needs_update || (force_update && this->component_flags_.font_id_is_set)) {
|
||||
this->nextion_->set_component_font(this->variable_name_.c_str(), this->font_id_);
|
||||
this->font_id_needs_update_ = false;
|
||||
this->component_flags_.font_id_needs_update = false;
|
||||
}
|
||||
}
|
||||
} // namespace nextion
|
||||
|
||||
@@ -21,29 +21,64 @@ class NextionComponent : public NextionComponentBase {
|
||||
void set_visible(bool visible);
|
||||
|
||||
protected:
|
||||
/**
|
||||
* @brief Constructor initializes component state with visible=true (default state)
|
||||
*/
|
||||
NextionComponent() {
|
||||
component_flags_ = {}; // Zero-initialize all state
|
||||
component_flags_.visible = 1; // Set default visibility to true
|
||||
}
|
||||
|
||||
NextionBase *nextion_;
|
||||
|
||||
bool bco_needs_update_ = false;
|
||||
bool bco_is_set_ = false;
|
||||
Color bco_;
|
||||
bool bco2_needs_update_ = false;
|
||||
bool bco2_is_set_ = false;
|
||||
Color bco2_;
|
||||
bool pco_needs_update_ = false;
|
||||
bool pco_is_set_ = false;
|
||||
Color pco_;
|
||||
bool pco2_needs_update_ = false;
|
||||
bool pco2_is_set_ = false;
|
||||
Color pco2_;
|
||||
// Color and styling properties
|
||||
Color bco_; // Background color
|
||||
Color bco2_; // Pressed background color
|
||||
Color pco_; // Foreground color
|
||||
Color pco2_; // Pressed foreground color
|
||||
uint8_t font_id_ = 0;
|
||||
bool font_id_needs_update_ = false;
|
||||
bool font_id_is_set_ = false;
|
||||
|
||||
bool visible_ = true;
|
||||
bool visible_needs_update_ = false;
|
||||
bool visible_is_set_ = false;
|
||||
/**
|
||||
* @brief Component state management using compact bitfield structure
|
||||
*
|
||||
* Stores all component state flags and properties in a single 16-bit bitfield
|
||||
* for efficient memory usage and improved cache locality.
|
||||
*
|
||||
* Each component property maintains two state flags:
|
||||
* - needs_update: Indicates the property requires synchronization with the display
|
||||
* - is_set: Tracks whether the property has been explicitly configured
|
||||
*
|
||||
* The visible field stores both the update flags and the actual visibility state.
|
||||
*/
|
||||
struct ComponentState {
|
||||
// Background color flags
|
||||
uint16_t bco_needs_update : 1;
|
||||
uint16_t bco_is_set : 1;
|
||||
|
||||
// void send_state_to_nextion() = 0;
|
||||
// Pressed background color flags
|
||||
uint16_t bco2_needs_update : 1;
|
||||
uint16_t bco2_is_set : 1;
|
||||
|
||||
// Foreground color flags
|
||||
uint16_t pco_needs_update : 1;
|
||||
uint16_t pco_is_set : 1;
|
||||
|
||||
// Pressed foreground color flags
|
||||
uint16_t pco2_needs_update : 1;
|
||||
uint16_t pco2_is_set : 1;
|
||||
|
||||
// Font ID flags
|
||||
uint16_t font_id_needs_update : 1;
|
||||
uint16_t font_id_is_set : 1;
|
||||
|
||||
// Visibility flags
|
||||
uint16_t visible_needs_update : 1;
|
||||
uint16_t visible_is_set : 1;
|
||||
uint16_t visible : 1; // Actual visibility state
|
||||
|
||||
// Reserved bits for future expansion
|
||||
uint16_t reserved : 3;
|
||||
} component_flags_;
|
||||
};
|
||||
} // namespace nextion
|
||||
} // namespace esphome
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -53,7 +53,7 @@ void NextionSensor::set_state(float state, bool publish, bool send_to_nextion) {
|
||||
|
||||
if (this->wave_chan_id_ == UINT8_MAX) {
|
||||
if (send_to_nextion) {
|
||||
if (this->nextion_->is_sleeping() || !this->visible_) {
|
||||
if (this->nextion_->is_sleeping() || !this->component_flags_.visible) {
|
||||
this->needs_to_send_update_ = true;
|
||||
} else {
|
||||
this->needs_to_send_update_ = false;
|
||||
|
||||
@@ -28,7 +28,7 @@ void NextionSwitch::set_state(bool state, bool publish, bool send_to_nextion) {
|
||||
return;
|
||||
|
||||
if (send_to_nextion) {
|
||||
if (this->nextion_->is_sleeping() || !this->visible_) {
|
||||
if (this->nextion_->is_sleeping() || !this->component_flags_.visible) {
|
||||
this->needs_to_send_update_ = true;
|
||||
} else {
|
||||
this->needs_to_send_update_ = false;
|
||||
|
||||
@@ -26,7 +26,7 @@ void NextionTextSensor::set_state(const std::string &state, bool publish, bool s
|
||||
return;
|
||||
|
||||
if (send_to_nextion) {
|
||||
if (this->nextion_->is_sleeping() || !this->visible_) {
|
||||
if (this->nextion_->is_sleeping() || !this->component_flags_.visible) {
|
||||
this->needs_to_send_update_ = true;
|
||||
} else {
|
||||
this->nextion_->add_no_result_to_queue_with_set(this, state);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
#include "nfc.h"
|
||||
#include <cstdio>
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
|
||||
namespace esphome {
|
||||
@@ -7,29 +8,9 @@ namespace nfc {
|
||||
|
||||
static const char *const TAG = "nfc";
|
||||
|
||||
std::string format_uid(std::vector<uint8_t> &uid) {
|
||||
char buf[(uid.size() * 2) + uid.size() - 1];
|
||||
int offset = 0;
|
||||
for (size_t i = 0; i < uid.size(); i++) {
|
||||
const char *format = "%02X";
|
||||
if (i + 1 < uid.size())
|
||||
format = "%02X-";
|
||||
offset += sprintf(buf + offset, format, uid[i]);
|
||||
}
|
||||
return std::string(buf);
|
||||
}
|
||||
std::string format_uid(const std::vector<uint8_t> &uid) { return format_hex_pretty(uid, '-', false); }
|
||||
|
||||
std::string format_bytes(std::vector<uint8_t> &bytes) {
|
||||
char buf[(bytes.size() * 2) + bytes.size() - 1];
|
||||
int offset = 0;
|
||||
for (size_t i = 0; i < bytes.size(); i++) {
|
||||
const char *format = "%02X";
|
||||
if (i + 1 < bytes.size())
|
||||
format = "%02X ";
|
||||
offset += sprintf(buf + offset, format, bytes[i]);
|
||||
}
|
||||
return std::string(buf);
|
||||
}
|
||||
std::string format_bytes(const std::vector<uint8_t> &bytes) { return format_hex_pretty(bytes, ' ', false); }
|
||||
|
||||
uint8_t guess_tag_type(uint8_t uid_length) {
|
||||
if (uid_length == 4) {
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
|
||||
#include "esphome/core/helpers.h"
|
||||
#include "esphome/core/log.h"
|
||||
#include "ndef_record.h"
|
||||
#include "ndef_message.h"
|
||||
#include "ndef_record.h"
|
||||
#include "nfc_tag.h"
|
||||
|
||||
#include <vector>
|
||||
@@ -53,8 +53,8 @@ static const uint8_t DEFAULT_KEY[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
||||
static const uint8_t NDEF_KEY[6] = {0xD3, 0xF7, 0xD3, 0xF7, 0xD3, 0xF7};
|
||||
static const uint8_t MAD_KEY[6] = {0xA0, 0xA1, 0xA2, 0xA3, 0xA4, 0xA5};
|
||||
|
||||
std::string format_uid(std::vector<uint8_t> &uid);
|
||||
std::string format_bytes(std::vector<uint8_t> &bytes);
|
||||
std::string format_uid(const std::vector<uint8_t> &uid);
|
||||
std::string format_bytes(const std::vector<uint8_t> &bytes);
|
||||
|
||||
uint8_t guess_tag_type(uint8_t uid_length);
|
||||
uint8_t get_mifare_classic_ndef_start_index(std::vector<uint8_t> &data);
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from esphome import automation
|
||||
import esphome.codegen as cg
|
||||
from esphome.config_helpers import filter_source_files_from_platform
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_ESPHOME,
|
||||
@@ -7,6 +8,7 @@ from esphome.const import (
|
||||
CONF_OTA,
|
||||
CONF_PLATFORM,
|
||||
CONF_TRIGGER_ID,
|
||||
PlatformFramework,
|
||||
)
|
||||
from esphome.core import CORE, coroutine_with_priority
|
||||
|
||||
@@ -120,3 +122,18 @@ async def ota_to_code(var, config):
|
||||
use_state_callback = True
|
||||
if use_state_callback:
|
||||
cg.add_define("USE_OTA_STATE_CALLBACK")
|
||||
|
||||
|
||||
FILTER_SOURCE_FILES = filter_source_files_from_platform(
|
||||
{
|
||||
"ota_backend_arduino_esp32.cpp": {PlatformFramework.ESP32_ARDUINO},
|
||||
"ota_backend_esp_idf.cpp": {PlatformFramework.ESP32_IDF},
|
||||
"ota_backend_arduino_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO},
|
||||
"ota_backend_arduino_rp2040.cpp": {PlatformFramework.RP2040_ARDUINO},
|
||||
"ota_backend_arduino_libretiny.cpp": {
|
||||
PlatformFramework.BK72XX_ARDUINO,
|
||||
PlatformFramework.RTL87XX_ARDUINO,
|
||||
PlatformFramework.LN882X_ARDUINO,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from esphome import pins
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import esp32, esp32_rmt, remote_base
|
||||
from esphome.config_helpers import filter_source_files_from_platform
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_BUFFER_SIZE,
|
||||
@@ -15,6 +16,7 @@ from esphome.const import (
|
||||
CONF_TYPE,
|
||||
CONF_USE_DMA,
|
||||
CONF_VALUE,
|
||||
PlatformFramework,
|
||||
)
|
||||
from esphome.core import CORE, TimePeriod
|
||||
|
||||
@@ -170,3 +172,19 @@ async def to_code(config):
|
||||
cg.add(var.set_buffer_size(config[CONF_BUFFER_SIZE]))
|
||||
cg.add(var.set_filter_us(config[CONF_FILTER]))
|
||||
cg.add(var.set_idle_us(config[CONF_IDLE]))
|
||||
|
||||
|
||||
FILTER_SOURCE_FILES = filter_source_files_from_platform(
|
||||
{
|
||||
"remote_receiver_esp32.cpp": {
|
||||
PlatformFramework.ESP32_ARDUINO,
|
||||
PlatformFramework.ESP32_IDF,
|
||||
},
|
||||
"remote_receiver_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO},
|
||||
"remote_receiver_libretiny.cpp": {
|
||||
PlatformFramework.BK72XX_ARDUINO,
|
||||
PlatformFramework.RTL87XX_ARDUINO,
|
||||
PlatformFramework.LN882X_ARDUINO,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
from esphome import automation, pins
|
||||
import esphome.codegen as cg
|
||||
from esphome.components import esp32, esp32_rmt, remote_base
|
||||
from esphome.config_helpers import filter_source_files_from_platform
|
||||
import esphome.config_validation as cv
|
||||
from esphome.const import (
|
||||
CONF_CARRIER_DUTY_PERCENT,
|
||||
@@ -12,6 +13,7 @@ from esphome.const import (
|
||||
CONF_PIN,
|
||||
CONF_RMT_SYMBOLS,
|
||||
CONF_USE_DMA,
|
||||
PlatformFramework,
|
||||
)
|
||||
from esphome.core import CORE
|
||||
|
||||
@@ -95,3 +97,19 @@ async def to_code(config):
|
||||
await automation.build_automation(
|
||||
var.get_complete_trigger(), [], on_complete_config
|
||||
)
|
||||
|
||||
|
||||
FILTER_SOURCE_FILES = filter_source_files_from_platform(
|
||||
{
|
||||
"remote_transmitter_esp32.cpp": {
|
||||
PlatformFramework.ESP32_ARDUINO,
|
||||
PlatformFramework.ESP32_IDF,
|
||||
},
|
||||
"remote_transmitter_esp8266.cpp": {PlatformFramework.ESP8266_ARDUINO},
|
||||
"remote_transmitter_libretiny.cpp": {
|
||||
PlatformFramework.BK72XX_ARDUINO,
|
||||
PlatformFramework.RTL87XX_ARDUINO,
|
||||
PlatformFramework.LN882X_ARDUINO,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user