Compare commits

..
Author SHA1 Message Date
J. Nick Koston 61b99d91bf Keep the frame end for a one-compare fast path, own the handle/resync loop, check formats differ 2026-11-18 13:02:59 -06:00
J. Nick Koston 69a7f7a408 Handle a byte in the middle of a frame inline, keep the rest out of line 2026-11-18 12:48:37 -06:00
J. Nick Koston 9fb6cb4867 Merge remote-tracking branch 'origin/dev' into frame-reader-length-byte
# Conflicts:
#	AGENTS.md
2026-11-18 12:35:26 -06:00
J. Nick Koston e0a5fad2de Cover one or two byte lengths of either byte order, several frame formats and fixed frames; recover every frame held back 2026-11-18 12:35:00 -06:00
J. Nick Koston 00125a448d [ai] Add the cleanups reviews keep missing to AGENTS.md and the review skill (#22708) 2026-11-18 12:33:37 -06:00
J. Nick Koston d76c655553 Keep one copy of replay_() 2026-11-18 12:16:27 -06:00
J. Nick Koston 9bdd466e51 Make resync() harmless while idle, test a second resync() 2026-11-18 12:15:26 -06:00
Kevin Ahrendt 3fc8bcbd14 [audio] Bump microFLAC to v0.3.1 (#22878) 2026-11-18 13:10:09 -05:00
J. Nick Koston 03880d5ea6 Skip the empty replay on a noise byte and keep replay_() out of line 2026-11-18 12:03:23 -06:00
Keith Burzinski 9898eb124a [light] Keep the colour of a dim addressable pixel (#22868) 2026-11-19 06:58:14 +13:00
J. Nick Koston 4a04c61ab5 Fold the length checks, resync a partial frame and return a frame found inside, add a benchmark 2026-11-18 11:56:49 -06:00
J. Nick Koston 2cd9ddf246 [core] Add LengthFrameReader for frames that give their own length 2026-11-18 11:51:59 -06:00
Kevin Ahrendt cac0e29421 [sendspin] Default static_delay_adjustable to true (#22872) 2026-11-18 11:51:00 -06:00
39 changed files with 835 additions and 78 deletions

No files matched your search

+46 -1
View File
@@ -120,7 +120,7 @@ helper; `AGENTS.md` section 4 and the `component-codegen` skill have the detail.
| hand-written `set_x_sensor()` plus `x_sensor_` member | `SUB_SENSOR(x)`, `SUB_BINARY_SENSOR`, `SUB_TEXT_SENSOR`, `SUB_SWITCH`, ... |
| `if CONF_X in config: s = await sensor.new_sensor(config[CONF_X])` plus setter | `sensors = sensor.sub_sensors(config)` then `await sensors(CONF_X, hub.set_x_sensor)` |
| hand-written `{"a": MyEnum.MY_ENUM_A, ...}` dict | `MyEnum.options("a", "b", prefix="MY_ENUM_")` |
| loops on `available()` / `read_byte()` | UART `read_available(on_byte)`, `discard_available()`, `LineReader<N>`, `FrameReader<N, HEADER...>` |
| loops on `available()` / `read_byte()` | UART `read_available(on_byte)`, `discard_available()`, `LineReader<N>`, `FrameReader<N, HEADER...>`, `LengthFrameReader<N, FrameFormat<...>...>` |
| hand-rolled CRC, checksum or bit loop | `crc8`, `crc16`, `xor_bytes`, `sum16_bytes`, `encode_uint16`, ... (`esphome/core/helpers.h`) |
| integer split into buffer bytes by hand (`buf[0] = v >> 8; buf[1] = v;`) | `put_be16`/`24`/`32`, `put_le16`, `put_le32` (`esphome/core/helpers.h`) |
| bare `mark_failed()`, or `ESP_LOGE` then `mark_failed()` | `this->mark_failed(LOG_STR("reason"))` or `LOG_STR(ESP_LOG_MSG_COMM_FAIL)` |
@@ -128,6 +128,51 @@ helper; `AGENTS.md` section 4 and the `component-codegen` skill have the detail.
| action or condition class that only forwards to the parent | `register_apply_action` / `register_apply_condition` |
| enum-to-string `switch` returning literals | `PROGMEM_STRING_TABLE` with `get_log_str` / `get_progmem_str` |
| `if (x := config.get(CONF_X)) is not None: cg.add(var.set_x(x))` | `cg.add_optional(var.set_x, config.get(CONF_X))`, when the setter takes the value unchanged |
| `x = (x & ~mask) \| (on ? mask : 0)` or a bit-reversal loop | `assign_bits(x, mask, on)`, `reverse_bits(x)` (`esphome/core/helpers.h`) |
| `x < lo ? lo : (x > hi ? hi : x)` | `std::clamp(x, lo, hi)` |
| I2C read, mask, write by hand; private 16-bit register byte helpers | `update_register`, `update_register16`, `read_byte_reg16`, `write_byte_reg16` |
| custom validator that checks which keys are set, or one schema with model checks | `cv.has_exactly_one_key`, `cv.has_at_most_one_key`, `cv.Inclusive`/`cv.Exclusive`, `cv.typed_schema` per model |
## Cleanups automated reviews keep missing
Multi-pass cleanup reviews of contributor PRs find these again and again, and single-pass reviews
almost never flag them. Look for each one explicitly; the matching `AGENTS.md` section 4 entry has the
detail.
- **Copies within the PR** (the most common finding): two functions, branches, platform files or tests
that differ in a constant or two. Ask for one helper, loop or table.
- **State that copies other state:** a `write_pending_` bool beside a timer (use the return value of
`cancel_timeout(ID)`), a `has_x_` bool beside `x_` (use `NAN` or a sentinel), an enum that can be worked
out from stored fields.
- **Dead guards:** null checks on members codegen always sets, `is_failed()` checks in `loop()`, range
checks the schema already enforces, `default:` in a `switch` over every value of an `enum class` the
component sets itself (not one cast from bus data), calls that set a member to its default
(`set_cs_pin(nullptr)`).
- **Member layout:** field order that leaves padding, `int`/`uint32_t` for values that fit a `uint8_t`,
an `enum class` without `: uint8_t`, several `bool` members that could share a byte. Each costs RAM per
instance.
- **C++ work that belongs in codegen:** scaling, clamping, checksums or register bytes computed at boot
from config values; ask for the plain value from `to_code`.
- **Two `switch` ladders for one mapping:** encode and decode of the same value pairs should share one
`static constexpr` table (`PROGMEM` on ESP8266).
- **Compile-time constants:** `static const` or `#define` with a constant value (use `static constexpr`),
constant tables not in `PROGMEM`, unused constants.
- **Code for one model or feature compiled into every build:** ask for a `cg.add_define()` emitted only
when the config uses it, with the runtime check inside the `#ifdef`.
- **Failure handling:** `mark_failed()` outside `setup()` (runtime failures set a warning that the next
success clears), an error enum that only picks a log message.
- **Logging** that does not follow https://developers.esphome.io/architecture/logging/: `ESP_LOGD` on every
reading, "Setting up" or "initialized" lines in `setup()`, a `dump_config()` made of single-line
`ESP_LOGCONFIG` calls where related lines fit one call, a name helper that returns `const char *` instead
of `const LogString *`.
- **Leaf classes** that are not `final`.
- **Unused helpers, includes and imports** left behind; lint does not catch most of them.
- **Comments that restate the code** or tell its history: a block comment that walks through the next
ten lines, a docstring that repeats the signature, a derivation that a one-line note plus a datasheet
section would cover.
- **Tests:** near-identical test functions that differ only in inputs (ask for `pytest.mark.parametrize`
or a table-driven gtest), tests that repeat another test, test YAML that sets options to their defaults.
## Python conventions
+26 -2
View File
@@ -246,7 +246,9 @@ file does, and it is the authority when they disagree. The most useful starting
`time::decode_bcd_calendar<LAYOUT>` and `encode_bcd_calendar<LAYOUT>` with `EPSON_BCD_CALENDAR`,
`DS_BCD_CALENDAR` or `PCF_BCD_CALENDAR`.
* **Bus helpers:** UART drains with `read_available(on_byte)` and `discard_available()`, text lines with
`LineReader<N>`, fixed-length binary frames with `FrameReader<N, HEADER...>`; I2C has `update_register`,
`LineReader<N>`, fixed-length binary frames with `FrameReader<N, HEADER...>`, frames that give their own length
with `LengthFrameReader<N, FrameFormat<FrameLengthU16LE<POS, ADD>, HEADER...>...>`; I2C has `update_register`,
`update_register16`, `read_byte_reg16` and `write_byte_reg16` for 16-bit register addresses,
`write_registers` (PROGMEM `I2CRegValue` table), `check_chip_id` and `read_bytes_16_le`.
`if (!this->status_comm_result(ok)) return;` sets or clears the warning; an earlier step of a multi-step
poll uses `status_comm_check(ok)`, which only sets it, so the last step must clear it; in `setup()`,
@@ -311,6 +313,27 @@ file does, and it is the authority when they disagree. The most useful starting
text::Text *source_{nullptr};
};
```
* **No state that copies other state:** a flag that mirrors a timer, a `has_x_` bool beside `x_`, or an enum
that can be worked out from fields already stored is a second source of truth. Use the source:
`pending = this->cancel_timeout(ID)`, an unset value of `NAN` or an out-of-range sentinel.
* **No dead guards:** do not check what the caller or core already guarantees: a member that codegen always
sets, `is_failed()` in `loop()` (core stops calling it), a value the config validation already limits, a
`default:` in a `switch` over every value of an `enum class` the component sets itself (keep it when the
value is cast from bus data).
* **Member layout:** order fields so the compiler adds no padding (usually largest first), store small codes
as `uint8_t`, declare `enum class X : uint8_t`, and pack flags into one byte.
* **Work out constants in Python:** math that only depends on the configuration (register bytes, scale
factors, thresholds, checksums) is done in `to_code` and passed as a plain value, not done in C++ at
boot.
* **One table for both directions:** a value that is encoded and decoded through the same mapping uses one
`static constexpr` (`PROGMEM` on ESP8266) table of `{value, code}` pairs, not two `switch` ladders.
* **Logging:** follow https://developers.esphome.io/architecture/logging/ for log levels and `dump_config()`.
In short: a reading is logged with `ESP_LOGV`, not `ESP_LOGD` on every update; `setup()` logs no
"Setting up" or "initialized" line (core logs setup); `dump_config()` combines related lines into one
`ESP_LOGCONFIG` call, within the 512-byte log buffer.
* **Leaf classes are `final`:** a component or entity class nothing derives from is declared `final`.
* **No copies within the PR:** two functions, branches or platform files that differ in a constant or two
become one helper, loop or table; helpers, includes and imports nothing uses are deleted.
* **Component Code Generation:** every component's `__init__.py` defines `DOMAIN` (its own name, checked by
CI). Hub entities go in platform files (`sensor.py`, ...) that take the hub's id, not under the hub's own
@@ -357,7 +380,8 @@ file does, and it is the authority when they disagree. The most useful starting
`script/test_build_components -c <component>`. Never define buses directly in test YAML: include the
shared packages from `tests/test_build_components/common/` with dict-style `packages:`. Test file naming,
`validate.*.yaml` config-only tests, CI grouping, C++ unit tests and integration tests are in the
`component-tests` skill.
`component-tests` skill. Tests that differ only in their inputs are one `pytest.mark.parametrize` test
or a table-driven gtest.
* **Debugging and Troubleshooting:**
* **Debug Tools:**
+7 -2
View File
@@ -10,7 +10,12 @@ from typing import TYPE_CHECKING
from esphome.build_helpers.ccache import resolve_absolute_ccache_path
from esphome.build_helpers.native import warn_ignored_platformio_options
from esphome.build_helpers.ninja import refresh_compile_commands
from esphome.const import CONF_COMPILE_PROCESS_LIMIT, CONF_ESPHOME
from esphome.const import (
CONF_COMPILE_PROCESS_LIMIT,
CONF_ESPHOME,
KEY_CORE,
KEY_FRAMEWORK_VERSION,
)
from esphome.core import CORE
from esphome.types import ConfigType
@@ -68,7 +73,7 @@ def run_compile(config: ConfigType, verbose: bool) -> int:
from esphome.core.config import NATIVE_ARDUINO_CONSUMED_PIO_OPTIONS
warn_ignored_platformio_options(NATIVE_ARDUINO_CONSUMED_PIO_OPTIONS)
paths = framework.check_and_install(CORE.framework_version)
paths = framework.check_and_install(CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION])
# Resolved once: the probe is not free and three consumers need it
ccache = resolve_absolute_ccache_path()
ninja_changed = build_gen.write_project(paths, ccache)
+1 -1
View File
@@ -385,7 +385,7 @@ async def to_code(config: ConfigType) -> None:
# Adds a define and IDF component for legacy `audio_decoder.cpp`.
if data.flac_support:
cg.add_define("USE_AUDIO_FLAC_SUPPORT")
add_idf_component(name="esphome/micro-flac", ref="0.3.0")
add_idf_component(name="esphome/micro-flac", ref="0.3.1")
_emit_memory_pair(
data.flac.buffer_memory,
"CONFIG_MICRO_FLAC_PREFER_PSRAM",
+3 -1
View File
@@ -36,6 +36,8 @@ from esphome.const import (
CONF_ESPHOME,
CONF_ID,
CONF_PLATFORMIO_OPTIONS,
KEY_CORE,
KEY_FRAMEWORK_VERSION,
)
from esphome.core import CORE, EsphomeError
import esphome.final_validate as fv
@@ -126,7 +128,7 @@ def _final_validate(config: ConfigType) -> None:
_LOGGER.warning("%s (this configuration cannot compile)", msg)
options = fv.full_config.get()[CONF_ESPHOME].get(CONF_PLATFORMIO_OPTIONS) or {}
check_bdk_version(options.get(BEKEN_BDK_VERSION_OPTION))
check_libretiny_version(CORE.framework_version)
check_libretiny_version(CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION])
FINAL_VALIDATE_SCHEMA = _final_validate
+4 -3
View File
@@ -3001,7 +3001,7 @@ async def _finalize_arduino_aware_flags():
async def to_code(config):
framework_ver = CORE.framework_version
framework_ver: cv.Version = CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]
conf = config[CONF_FRAMEWORK]
# Check if using ESP-IDF toolchain
@@ -3838,8 +3838,9 @@ def _write_sdkconfig():
# version switch that happens to leave the option set unchanged still
# bumps this file's content -- which is what has_outdated_files()
# uses to decide whether to reconfigure.
framework_version = CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]
contents = (
f"# ESPHOME_IDF_VERSION={CORE.framework_version}\n"
f"# ESPHOME_IDF_VERSION={framework_version}\n"
+ "\n".join(
f"{name}={_format_sdkconfig_val(value)}"
for name, value in sorted(want_opts.items())
@@ -3990,7 +3991,7 @@ def _write_idf_component_yml():
if CORE.using_toolchain_esp_idf:
add_idf_component(
name=ARDUINO_ESP32_COMPONENT_NAME,
ref=str(CORE.framework_version),
ref=str(CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]),
)
for component in converted:
+1 -1
View File
@@ -529,7 +529,7 @@ async def to_code(config: ConfigType) -> None:
# keeping one source of truth
cg.add_platformio_option("board_build.flash_mode", config[CONF_BOARD_FLASH_MODE])
ver = CORE.framework_version
ver: cv.Version = CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]
cg.add_define(
"USE_ARDUINO_VERSION_CODE",
cg.RawExpression(f"VERSION_CODE({ver.major}, {ver.minor}, {ver.patch})"),
+4 -2
View File
@@ -46,6 +46,8 @@ from esphome.const import (
CONF_USE_ADDRESS,
CONF_VALUE,
CONF_WIFI,
KEY_CORE,
KEY_FRAMEWORK_VERSION,
Platform,
PlatformFramework,
)
@@ -345,13 +347,13 @@ def _validate(config: ConfigType) -> ConfigType:
if CONF_POLLING_INTERVAL in config:
raise cv.Invalid(
"In this version of the framework "
f"({CORE.target_framework} {CORE.framework_version}), "
f"({CORE.target_framework} {CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]}), "
f"'{CONF_POLLING_INTERVAL}' is not supported."
)
if CONF_INTERRUPT_PIN not in config:
raise cv.Invalid(
"In this version of the framework "
f"({CORE.target_framework} {CORE.framework_version}), "
f"({CORE.target_framework} {CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]}), "
f"'{CONF_INTERRUPT_PIN}' is a required option for [ethernet]."
)
elif config[CONF_TYPE] in ("GENERIC", "YT8531"):
+3 -2
View File
@@ -241,7 +241,7 @@ def has_software_serial() -> bool:
def can_choose_uart_pins() -> bool:
"""True when SerialClass::begin() takes the pins, which LibreTiny added in 1.12.0."""
return CORE.framework_version >= cv.Version(1, 12, 0)
return CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION] >= cv.Version(1, 12, 0)
def only_on_family(*, supported=None, unsupported=None):
@@ -643,7 +643,8 @@ async def component_to_code(config):
build_src_flags += " -Os"
cg.add_platformio_option("build_src_flags", build_src_flags)
# LibreTiny before 1.14.0 breaks under SCons 4.11 without this script; see the script
if CORE.framework_version < cv.Version(1, 14, 0):
version = CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]
if (version.major, version.minor, version.patch) < (1, 14, 0):
cg.add_platformio_option("extra_scripts", ["pre:scons_dont_inherit.py"])
cg.add_platformio_option("extra_scripts", ["pre:ccache.py"])
# IRAM_ATTR is a no-op on BK72xx (SDK masks FIQ+IRQ around flash ops).
@@ -151,7 +151,8 @@ Color color_from_light_color_values(LightColorValues val) {
auto r = to_uint8_scale(val.get_color_brightness() * val.get_red());
auto g = to_uint8_scale(val.get_color_brightness() * val.get_green());
auto b = to_uint8_scale(val.get_color_brightness() * val.get_blue());
auto w = to_uint8_scale(val.get_white());
// White keeps its default of 100% in RGB mode, where it must not count as the strongest channel
uint8_t w = val.get_color_mode() == ColorMode::RGB ? 0 : to_uint8_scale(val.get_white());
return Color(r, g, b, w);
}
@@ -12,6 +12,45 @@ uint8_t ESPColorCorrection::gamma_correct_(uint8_t value) const {
return result;
}
uint16_t ESPColorCorrection::gamma_level_(uint8_t value) const {
if (this->gamma_table_ == nullptr)
return value * 257;
return progmem_read_uint16(&this->gamma_table_[value]);
}
uint8_t ESPColorCorrection::gamma_round_(uint8_t value) const {
if (this->gamma_table_ == nullptr)
return value;
return (progmem_read_uint16(&this->gamma_table_[value]) + 128) / 257;
}
// Lighting every non-zero channel instead would turn a dim red with a trace of green into yellow
Color ESPColorCorrection::correct_dark_pixel_(Color color) const {
// Compared without the local brightness, which is the same for every channel: the scaled 8-bit
// values are too coarse to compare at the lowest brightness
uint16_t strengths[4];
uint16_t max_strength = 0;
bool lit = false;
for (uint8_t i = 0; i < 4; i++) {
strengths[i] = color[i] * this->max_brightness_[i];
max_strength = std::max(max_strength, strengths[i]);
lit |= esp_scale8_twice(color[i], this->max_brightness_[i], this->local_brightness_) != 0;
}
Color res;
if (!lit)
return res;
// Stretch the strongest channel to full scale and round the others against it; the table is too
// coarse to compare its lowest levels
for (uint8_t i = 0; i < 4; i++) {
if (strengths[i] == max_strength) {
res[i] = 1;
} else if (strengths[i] != 0) {
res[i] = this->gamma_level_(strengths[i] * 255 / max_strength) >= 0x8000 ? 1 : 0;
}
}
return res;
}
uint8_t ESPColorCorrection::gamma_uncorrect_(uint8_t value) const {
if (this->gamma_table_ == nullptr)
return value;
@@ -40,10 +40,13 @@ class ESPColorCorrection {
#endif
void set_gamma_table(const uint16_t *table) { this->gamma_table_ = table; }
/// corrected = (uncorrected * max_brightness * local_brightness) ^ gamma, keeping a lit pixel lit
inline Color color_correct(Color color) const ESPHOME_ALWAYS_INLINE {
// corrected = (uncorrected * max_brightness * local_brightness) ^ gamma
return Color(this->color_correct_red(color.red), this->color_correct_green(color.green),
this->color_correct_blue(color.blue), this->color_correct_white(color.white));
Color res(this->round_channel_(COLOR_CHANNEL_R, color.red), this->round_channel_(COLOR_CHANNEL_G, color.green),
this->round_channel_(COLOR_CHANNEL_B, color.blue), this->round_channel_(COLOR_CHANNEL_W, color.white));
if (res.raw_32 == 0 && color.raw_32 != 0)
res = this->correct_dark_pixel_(color);
return Color(this->limit_(res.red), this->limit_(res.green), this->limit_(res.blue), this->limit_(res.white));
}
inline uint8_t color_correct_red(uint8_t red) const ESPHOME_ALWAYS_INLINE {
return this->color_correct_channel(COLOR_CHANNEL_R, red);
@@ -90,6 +93,15 @@ class ESPColorCorrection {
#endif
return res;
}
inline uint8_t round_channel_(uint8_t channel, uint8_t value) const ESPHOME_ALWAYS_INLINE {
return this->gamma_round_(esp_scale8_twice(value, this->max_brightness_[channel], this->local_brightness_));
}
/// Forward gamma as the table's 16-bit level
uint16_t gamma_level_(uint8_t value) const;
/// Forward gamma rounded to uint8, where a low level becomes 0
uint8_t gamma_round_(uint8_t value) const;
/// The levels of a pixel whose channels all round to 0: 1 for its strongest channels, unless it is black
Color correct_dark_pixel_(Color color) const;
/// Forward gamma: read uint16 PROGMEM table, convert to uint8
uint8_t gamma_correct_(uint8_t value) const;
/// Reverse gamma and limit for one stored level
+18 -1
View File
@@ -36,6 +36,10 @@ class ESPColorAccessor {
#if defined(USE_LIGHT_PARTITION) && defined(USE_LIGHT_POWER_ESTIMATION)
virtual const ESPColorCorrection *get_color_correction_internal(int32_t index) const { return nullptr; }
#endif
#ifdef USE_LIGHT_PARTITION
// For internal use only: whether the LED at `index` stores a white level.
virtual bool has_white_internal(int32_t index) const { return true; }
#endif
};
/// Color accessor that is completely at peace with the void of nothingness.
@@ -78,6 +82,10 @@ class BufferColorAccessor : public ESPColorAccessor {
}
void set_effect_data(int32_t index, uint8_t value) override { this->effect_data[index] = value; }
#ifdef USE_LIGHT_PARTITION
bool has_white_internal(int32_t index) const override { return this->channel_colors.has_white(); }
#endif
/// Allocate a zeroed block of buffer_bytes() plus `extra_bytes` for the caller, which start at
/// `effect_data + num_leds`, and pass it to set_buffer(). Returns false if it failed.
bool allocate(RAMAllocator<uint8_t> &allocator, size_t num_leds, size_t extra_bytes = 0) {
@@ -151,7 +159,12 @@ class CorrectedBufferColorAccessor final : public BufferColorAccessor {
return this->color_correction->color_uncorrect_channel(channel, this->get_channel_raw(index, channel));
}
void set(int32_t index, Color color) override { this->set_raw(index, this->color_correction->color_correct(color)); }
void set(int32_t index, Color color) override {
// A white level the strip cannot show must not count as the pixel's strongest channel
if (!this->channel_colors.has_white())
color.white = 0;
this->set_raw(index, this->color_correction->color_correct(color));
}
void set_channel(int32_t index, uint8_t channel, uint8_t value) override {
this->set_channel_raw(index, channel, this->color_correction->color_correct_channel(channel, value));
}
@@ -273,6 +286,10 @@ class ESPColorView final : public ESPColorSettable {
return this->accessor_->get_color_correction_internal(this->index_);
}
#endif
#ifdef USE_LIGHT_PARTITION
// This method is for internal use only.
bool has_white_internal() const { return this->accessor_->has_white_internal(this->index_); }
#endif
protected:
ESPColorAccessor *const accessor_;
+5 -5
View File
@@ -588,7 +588,7 @@ async def to_code(config: ConfigType) -> None:
cg.add_platformio_option(
"platform_packages",
[
f"platformio/framework-zephyr@https://github.com/tomaszduda23/framework-sdk-nrf/archive/refs/tags/v{CORE.framework_version}.zip",
f"platformio/framework-zephyr@https://github.com/tomaszduda23/framework-sdk-nrf/archive/refs/tags/v{CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]}.zip",
],
)
if config[KEY_BOOTLOADER] != BOOTLOADER_MCUBOOT:
@@ -634,7 +634,7 @@ async def to_code(config: ConfigType) -> None:
if dfu_config := config.get(CONF_DFU):
CORE.add_job(_dfu_to_code, dfu_config)
framework_ver = CORE.framework_version
framework_ver: cv.Version = CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]
if (dcdc := config.get(CONF_DCDC)) is not None:
if framework_ver < cv.Version(2, 9, 2):
zephyr_add_prj_conf("BOARD_ENABLE_DCDC", dcdc)
@@ -1298,13 +1298,13 @@ def _write_pch_checksum(build_dir: Path, source_dir: Path) -> None:
The app image dir follows the SDK version, like get_elf_path;
2.9.2+ always wraps the build in sysbuild."""
app_dir = build_dir
if CORE.framework_version >= cv.Version(2, 9, 2):
if CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION] >= cv.Version(2, 9, 2):
app_dir = build_dir / "zephyr"
checksum = pch.pch_checksum(
CORE.relative_src_path(),
pch.PCH_DEFAULT_HEADERS,
(
str(CORE.framework_version),
str(CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]),
zephyr_data()[KEY_BOARD],
# Kconfig inputs that reach autoconf.h without a .conf line
",".join(sorted(wanted_west_projects())),
@@ -1552,7 +1552,7 @@ def run_compile(args, config: ConfigType) -> bool:
raise EsphomeError("nRF52 native build failed")
zephyr_dir = build_dir / "zephyr"
framework_ver = CORE.framework_version
framework_ver = CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]
bootloader = zephyr_data()[KEY_BOOTLOADER]
# (dev_type, sd_req) per bootloader — values from Nordic SoftDevice release notes.
+3 -3
View File
@@ -8,7 +8,7 @@ import subprocess
import sys
import esphome.config_validation as cv
from esphome.const import CONF_OTA, CONF_PLATFORM
from esphome.const import CONF_OTA, CONF_PLATFORM, KEY_CORE, KEY_FRAMEWORK_VERSION
from esphome.core import CORE, EsphomeError
from esphome.framework_helpers import run_command_ok
@@ -82,7 +82,7 @@ def can_upload_using_cmsis_dap() -> bool:
if (
CORE.using_toolchain_platformio
or variant not in _PYOCD_TARGETS
or CORE.framework_version < cv.Version(2, 9, 2)
or CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION] < cv.Version(2, 9, 2)
):
return False
if (target_file := _PYOCD_TARGET_FILES.get(variant)) is None:
@@ -113,7 +113,7 @@ def _images(build_dir: Path) -> list[Path]:
Chosen by SDK version and by whether MCUboot is built, so a file left by another build is
never picked and a missing part is an error rather than a partial flash.
"""
framework_ver = CORE.framework_version
framework_ver = CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]
if framework_ver < cv.Version(2, 9, 2):
raise EsphomeError(
"Flashing with pyOCD needs nRF52 framework version 2.9.2 or newer"
+7 -5
View File
@@ -24,6 +24,7 @@ from esphome.components.zephyr.sdk_install import (
sdk_toolchain_path,
)
import esphome.config_validation as cv
from esphome.const import KEY_CORE, KEY_FRAMEWORK_VERSION
from esphome.core import CORE, EsphomeError
from esphome.framework_helpers import (
create_venv,
@@ -47,7 +48,7 @@ _TOOLCHAIN_VERSIONS = (TOOLCHAIN_VERSION, _TOOLCHAIN_VERSION_NCS_3_4_0)
def _uses_sdk_ng_1_toolchain() -> bool:
"""True when the framework needs Zephyr SDK 1.0+."""
return CORE.framework_version >= cv.Version(3, 4, 0)
return CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION] >= cv.Version(3, 4, 0)
def _get_toolchain_version() -> str:
@@ -96,7 +97,7 @@ def toolchain_tool(name: str) -> Path:
def _get_version_str() -> str:
framework_ver = CORE.framework_version
framework_ver = CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]
return f"v{framework_ver.major}.{framework_ver.minor}.{framework_ver.patch}"
@@ -389,7 +390,7 @@ def include_west_project(name: str) -> None:
def bluetooth_west_projects() -> tuple[str, ...]:
"""Bluetooth's crypto: TinyCrypt up to SDK 3.1, PSA (mbedtls, Oberon) from 3.2."""
if CORE.framework_version >= cv.Version(3, 2, 0):
if CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION] >= cv.Version(3, 2, 0):
return ("mbedtls", "oberon-psa-crypto")
return ("tinycrypt",)
@@ -397,7 +398,7 @@ def bluetooth_west_projects() -> tuple[str, ...]:
def wanted_west_projects() -> set[str]:
projects = set(_get_data().west_projects)
# Zephyr 4.1 moved the Cortex-M core headers to cmsis_6
if CORE.framework_version >= cv.Version(3, 1, 0):
if CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION] >= cv.Version(3, 1, 0):
projects.add("cmsis_6")
# Sysbuild builds the MCUboot image with any bootloader
if CORE.data.get(KEY_ZEPHYR, {}).get(KEY_SYSBUILD):
@@ -579,7 +580,8 @@ def _check_and_install(version: str) -> None:
projects = wanted_west_projects()
if not sentinel.exists() or not zephyr_reqs.exists():
_install_framework(env_python_path, framework_path, version, projects)
if CORE.framework_version < cv.Version(2, 9, 2):
framework_ver = CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]
if framework_ver < cv.Version(2, 9, 2):
_patch_uf2conv_escape_sequences(framework_path)
sentinel.touch()
else:
+2 -1
View File
@@ -7,6 +7,7 @@ gives the binutils, the linked image and idedata of that build.
from pathlib import Path
import esphome.config_validation as cv
from esphome.const import KEY_CORE, KEY_FRAMEWORK_VERSION
from esphome.core import CORE
from .framework import toolchain_tool
@@ -26,7 +27,7 @@ def get_elf_path() -> Path:
Chosen by version rather than by probing so a leftover image from another
SDK layout is never analyzed in place of the current build.
"""
if CORE.framework_version < cv.Version(2, 9, 2):
if CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION] < cv.Version(2, 9, 2):
return CORE.relative_pioenvs_path(CORE.name, "zephyr", "zephyr.elf")
return CORE.relative_pioenvs_path(CORE.name, "zephyr", "zephyr", "zephyr.elf")
+3 -1
View File
@@ -56,6 +56,8 @@ from esphome.const import (
CONF_TX_PIN,
CONF_USE_ADDRESS,
CONF_WIFI,
KEY_CORE,
KEY_FRAMEWORK_VERSION,
PLATFORM_ESP32,
PlatformFramework,
)
@@ -582,7 +584,7 @@ async def to_code(config: ConfigType) -> None:
# Crypto through PSA: mbedtls, plus Oberon from SDK 2.7
include_west_project("mbedtls")
include_west_project("openthread")
if CORE.framework_version >= cv.Version(2, 7, 0):
if CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION] >= cv.Version(2, 7, 0):
include_west_project("oberon-psa-crypto")
zephyr_add_prj_conf("NET_L2_OPENTHREAD", True)
zephyr_add_prj_conf(
@@ -83,6 +83,9 @@ class PartitionLightOutput final : public light::AddressableLight {
void set(int32_t index, Color color) override {
light::ESPColorView color_view = this->parent_->get_color_view_(index);
// A white level the LED cannot show must not count as the pixel's strongest channel
if (!color_view.has_white_internal()) // NOLINT
color.white = 0;
color_view.set_raw(this->parent_->get_color_correction_(color_view)->color_correct(color));
}
void set_channel(int32_t index, uint8_t channel, uint8_t value) override {
@@ -98,6 +101,10 @@ class PartitionLightOutput final : public light::AddressableLight {
this->parent_->get_color_view_(index).set_effect_data(value);
}
bool has_white_internal(int32_t index) const override { // NOLINT
return this->parent_->get_color_view_(index).has_white_internal();
}
#ifdef USE_LIGHT_POWER_ESTIMATION
const light::ESPColorCorrection *get_color_correction_internal(int32_t index) const override { // NOLINT
return this->parent_->get_color_correction_(this->parent_->get_color_view_(index));
+1 -1
View File
@@ -382,7 +382,7 @@ async def to_code(config: ConfigType) -> None:
"board_build.filesystem_size", "0m" if CORE.testing_mode else "1m"
)
ver = CORE.framework_version
ver: cv.Version = CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]
cg.add_define(
"USE_ARDUINO_VERSION_CODE",
cg.RawExpression(f"VERSION_CODE({ver.major}, {ver.minor}, {ver.patch})"),
@@ -98,7 +98,7 @@ CONFIG_SCHEMA = cv.All(
cv.positive_time_period_milliseconds,
cv.Range(max=cv.TimePeriod(milliseconds=5000)),
),
cv.Optional(CONF_STATIC_DELAY_ADJUSTABLE, default=False): cv.boolean,
cv.Optional(CONF_STATIC_DELAY_ADJUSTABLE, default=True): cv.boolean,
cv.Optional(CONF_FIXED_DELAY, default="0us"): cv.All(
cv.positive_time_period_microseconds,
cv.Range(max=cv.TimePeriod(microseconds=10000)),
@@ -61,7 +61,7 @@ class SendspinMediaSource final : public SendspinChild,
bool cached_muted_{false};
bool pending_start_{false};
bool static_delay_adjustable_{false};
bool static_delay_adjustable_{true};
};
} // namespace esphome::sendspin_
+3 -1
View File
@@ -26,6 +26,8 @@ from esphome.const import (
CONF_SECONDS,
CONF_TIMEZONE,
CONF_TRIGGER_ID,
KEY_CORE,
KEY_FRAMEWORK_VERSION,
PLATFORM_BK72XX,
PLATFORM_ESP32,
PLATFORM_ESP8266,
@@ -471,7 +473,7 @@ async def to_code(config):
# From sdk-nrf 2.8.0 newlib time() gets the clock through gettimeofday(),
# which Zephyr only provides with XSI_SINGLE_PROCESS. Without it time()
# always returns -1.
if CORE.framework_version >= cv.Version(2, 8, 0):
if CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION] >= cv.Version(2, 8, 0):
zephyr_add_prj_conf("POSIX_SINGLE_PROCESS", True)
zephyr_add_prj_conf("XSI_SINGLE_PROCESS", True)
cg.add_define("USE_TIME")
+3 -2
View File
@@ -5,7 +5,7 @@ from typing import Literal, NamedTuple, TypedDict
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import CONF_BOARD
from esphome.const import CONF_BOARD, KEY_CORE, KEY_FRAMEWORK_VERSION
from esphome.core import CORE, CoroPriority, coroutine_with_priority
from esphome.helpers import copy_file_if_changed, write_file_if_changed
from esphome.types import ConfigType
@@ -281,7 +281,8 @@ def zephyr_use_usb_device_next() -> bool:
The new stack (device_next) is used from nRF Connect SDK 3.4.1, where the
legacy stack is deprecated. Older SDKs keep the legacy stack.
"""
return CORE.is_nrf52 and CORE.framework_version >= cv.Version(3, 4, 1)
framework_ver: cv.Version = CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]
return CORE.is_nrf52 and framework_ver >= cv.Version(3, 4, 1)
def _disable_board_cdc_acm_overlay() -> str:
@@ -17,7 +17,7 @@ from esphome.components.zephyr.const import (
KEY_PM_STATIC,
)
import esphome.config_validation as cv
from esphome.const import CONF_ID
from esphome.const import CONF_ID, KEY_CORE, KEY_FRAMEWORK_VERSION
from esphome.core import CORE, CoroPriority, coroutine_with_priority
from esphome.types import ConfigType
@@ -40,6 +40,10 @@ CONFIG_SCHEMA = cv.All(
)
def _framework_version() -> cv.Version:
return CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]
def _partition_address() -> int:
"""Place the partition below the lowest section reserved above the SoftDevice
(the bootloader), or at the end of flash with MCUboot, on an erase page boundary."""
@@ -87,7 +91,7 @@ async def to_code(config: ConfigType) -> None:
cg.add_define("USE_CRASH_HANDLER")
zephyr_add_prj_conf("DEBUG_COREDUMP", True)
zephyr_add_prj_conf("DEBUG_COREDUMP_BACKEND_FLASH_PARTITION", True)
framework_ver = CORE.framework_version
framework_ver = _framework_version()
if framework_ver >= DUMP_THREADS_MIN_VERSION:
zephyr_add_prj_conf("DEBUG_COREDUMP_MEMORY_DUMP_THREADS", True)
else:
@@ -36,6 +36,8 @@ from esphome.const import (
CONF_NUMBER,
CONF_PIN,
CONF_STATUS,
KEY_CORE,
KEY_FRAMEWORK_VERSION,
Framework,
)
from esphome.core import CORE, coroutine_with_priority
@@ -131,7 +133,8 @@ def _validate_bootloader(config: ConfigType) -> None:
return
if bootloader not in MCUMGR_OTA_BOOTLOADERS:
raise cv.Invalid(f"{bootloader} does not support OTA")
if CORE.framework_version < cv.Version(2, 9, 2):
framework_ver: cv.Version = CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]
if framework_ver < cv.Version(2, 9, 2):
raise cv.Invalid(
"OTA with Adafruit_nRF52_Bootloader requires at least SDK 2.9.2"
)
@@ -249,7 +252,8 @@ async def to_code(config: ConfigType) -> None:
""",
image="mcuboot",
)
if CORE.framework_version >= cv.Version(2, 9, 2):
framework_ver = CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]
if framework_ver >= cv.Version(2, 9, 2):
zephyr_data()[KEY_SYSBUILD] = True
bootloader = zephyr_data()[KEY_BOOTLOADER]
+7 -3
View File
@@ -68,6 +68,9 @@ from esphome.const import (
ENTITY_CATEGORY_CONFIG,
ENTITY_CATEGORY_DIAGNOSTIC,
ENTITY_CATEGORY_NONE,
KEY_CORE,
KEY_FRAMEWORK_VERSION,
KEY_TARGET_FRAMEWORK,
PLATFORM_ESP32,
PLATFORM_ESP8266,
PLATFORM_NRF52,
@@ -2934,7 +2937,8 @@ def require_framework_version(
"""
def validator(value):
framework = CORE.target_framework
core_data = CORE.data[KEY_CORE]
framework = core_data[KEY_TARGET_FRAMEWORK]
keys_to_try: list[str] = []
if CORE.is_host and framework == "host":
@@ -2980,14 +2984,14 @@ def require_framework_version(
required = kwargs[key]
if max_version:
if CORE.framework_version > required:
if core_data[KEY_FRAMEWORK_VERSION] > required:
msg = f"This feature requires framework version {required} or lower"
if extra_message:
msg += f". {extra_message}"
raise Invalid(msg)
return value
if CORE.framework_version < required:
if core_data[KEY_FRAMEWORK_VERSION] < required:
msg = f"This feature requires at least framework version {required}"
if extra_message:
msg += f". {extra_message}"
-6
View File
@@ -20,7 +20,6 @@ from esphome.const import (
CONF_WEB_SERVER,
CONF_WIFI,
KEY_CORE,
KEY_FRAMEWORK_VERSION,
KEY_TARGET_FRAMEWORK,
KEY_TARGET_PLATFORM,
NATIVE_TOOLCHAINS,
@@ -1036,11 +1035,6 @@ class EsphomeCore:
def target_framework(self):
return self.data[KEY_CORE][KEY_TARGET_FRAMEWORK]
@property
def framework_version(self) -> Version:
"""The version of the target platform's framework, set by the platform component."""
return self.data[KEY_CORE][KEY_FRAMEWORK_VERSION]
@property
def using_arduino(self):
return self.target_framework == "arduino"
+227 -13
View File
@@ -1,11 +1,33 @@
#pragma once
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <type_traits>
// Out of line, one copy: the frame readers' rare paths call each other, and GCC would add specialised clones
#if defined(__clang__)
#define ESPHOME_FRAME_READER_COLD __attribute__((noinline))
#else
#define ESPHOME_FRAME_READER_COLD __attribute__((noinline, noclone))
#endif
namespace esphome {
namespace frame_reader_detail {
// A fold, not an array, so no header table lands in RAM on ESP8266
template<uint8_t... HEADER> constexpr uint8_t header_byte(size_t i) {
size_t j = 0;
uint8_t r = 0;
((r = (j++ == i) ? HEADER : r), ...);
return r;
}
template<class FIRST, class... REST> struct First {
using type = FIRST;
};
} // namespace frame_reader_detail
/// Collects fixed length binary frames of N bytes that start with the header bytes HEADER...; the binary twin of
/// LineReader. Bytes before a header are skipped; checksums are left to the caller.
template<size_t N, uint8_t... HEADER> class FrameReader {
@@ -18,11 +40,12 @@ template<size_t N, uint8_t... HEADER> class FrameReader {
/// Feed one byte; returns the completed frame, header included, valid until the next call, or nullptr.
const uint8_t *feed(uint8_t c) {
pos_t pos = this->pos_;
if (pos < HEADER_LEN && c != header_byte(pos)) {
if (pos < HEADER_LEN && c != frame_reader_detail::header_byte<HEADER...>(pos)) {
// A later byte of the broken header, or c, may start the real one
if (pos == 0)
return nullptr;
this->buf_[pos] = c;
this->replay_(pos);
return nullptr;
return this->replay_(pos);
}
this->buf_[pos++] = c;
if (pos < N) {
@@ -44,24 +67,215 @@ template<size_t N, uint8_t... HEADER> class FrameReader {
void resync() { this->replay_(N - 1); }
protected:
// A fold, not an array, so no header table lands in RAM on ESP8266
static constexpr uint8_t header_byte(size_t i) {
size_t j = 0;
uint8_t r = 0;
((r = (j++ == i) ? HEADER : r), ...);
return r;
}
// Start over and feed buf_[1..last] again; never completes a frame as last < N
void replay_(size_t last) {
// Start over and feed buf_[1..last] again; never completes a frame as last < N, so it returns nullptr, which lets
// feed() end in a tail call. Out of line, as feed() and replay_() call each other.
const uint8_t *ESPHOME_FRAME_READER_COLD replay_(size_t last) {
this->pos_ = 0;
for (size_t i = 1; i <= last; i++) {
this->feed(this->buf_[i]);
}
return nullptr;
}
uint8_t buf_[N];
pos_t pos_{0};
};
/// Length rules for FrameFormat: the frame length is known once KNOWN_AT bytes are collected, and get() returns it
/// for the whole frame, header included. A component can pass its own type with these two members.
/// One length byte at POS; ADD is added to give the whole frame length.
template<size_t POS, size_t ADD = 0> struct FrameLengthU8 {
static constexpr size_t KNOWN_AT = POS + 1;
static constexpr size_t get(const uint8_t *buf) { return buf[POS] + ADD; }
};
/// Two length bytes at POS, high byte first.
template<size_t POS, size_t ADD = 0> struct FrameLengthU16BE {
static constexpr size_t KNOWN_AT = POS + 2;
static constexpr size_t get(const uint8_t *buf) { return ((size_t(buf[POS]) << 8) | buf[POS + 1]) + ADD; }
};
/// Two length bytes at POS, low byte first.
template<size_t POS, size_t ADD = 0> struct FrameLengthU16LE {
static constexpr size_t KNOWN_AT = POS + 2;
static constexpr size_t get(const uint8_t *buf) { return (buf[POS] | (size_t(buf[POS + 1]) << 8)) + ADD; }
};
/// A frame type that always has LEN bytes.
template<size_t LEN> struct FrameFixedLength {
static constexpr size_t KNOWN_AT = 1;
static constexpr size_t get(const uint8_t * /*buf*/) { return LEN; }
};
/// One frame type for LengthFrameReader: frames that start with the header bytes HEADER... and whose length follows
/// LENGTH, one of the FrameLength types above.
template<class LENGTH, uint8_t... HEADER> struct FrameFormat {
using Length = LENGTH;
static constexpr size_t HEADER_LEN = sizeof...(HEADER);
static constexpr uint8_t FIRST = frame_reader_detail::header_byte<HEADER...>(0);
// Bytes collected when the length is checked; also the shortest frame
static constexpr size_t MIN_LEN = std::max<size_t>({LENGTH::KNOWN_AT, HEADER_LEN, 2});
static_assert(HEADER_LEN >= 1 && HEADER_LEN <= 8, "FrameFormat takes 1 to 8 header bytes");
static_assert(LENGTH::KNOWN_AT == 1 || LENGTH::KNOWN_AT > HEADER_LEN, "The length field must follow the header");
static constexpr uint8_t header_byte(size_t i) { return frame_reader_detail::header_byte<HEADER...>(i); }
};
/// Collects binary frames that give their own length, of up to N bytes, in one or more FrameFormats; the first byte
/// picks the format, so each format needs a different first header byte. Bytes before a header are skipped. A length
/// that ends the frame before its length field, or is longer than N, is not a frame, and the reader looks for a
/// header after its first byte. Checksums, footers and a minimum length that covers them are left to the caller,
/// which passes a handler that returns false for a frame it rejects:
///
/// this->reader_.feed(c, [this](const uint8_t *frame, size_t len) { return this->handle_frame_(frame, len); });
template<size_t N, class... FORMATS> class LengthFrameReader {
using pos_t = std::conditional_t<(N <= 0xFF), uint8_t, uint16_t>;
static_assert(sizeof...(FORMATS) >= 1, "LengthFrameReader takes at least one FrameFormat");
static_assert(N >= 2 && N <= 0xFFFF, "LengthFrameReader size out of range");
static constexpr bool distinct_first() {
const uint8_t first[] = {FORMATS::FIRST...};
for (size_t i = 0; i < sizeof...(FORMATS); i++) {
for (size_t j = i + 1; j < sizeof...(FORMATS); j++) {
if (first[i] == first[j])
return false;
}
}
return true;
}
static_assert(distinct_first(), "Each FrameFormat needs a different first header byte");
public:
/// Feed one byte; calls handle(frame, length) for each frame it completes, header included. A rejected frame
/// (handle returns false) is searched for frames inside it and after it, which go to handle the same way.
template<class F> inline void __attribute__((always_inline)) feed(uint8_t c, F &&handle) {
if (const uint8_t *frame = this->feed(c))
this->drain_(frame, handle);
}
/// For a partial frame left too long without a byte, which a corrupt length can cause: give up on it, and pass the
/// frames it held back to handle.
template<class F> void flush(F &&handle) {
while (this->pos_ != 0)
this->drain_(this->resync(), handle);
}
/// Lower level: feed one byte; returns the completed frame, valid until the next call, or nullptr. After it, call
/// next() when the frame is handled or resync() when it is rejected, until they return nullptr.
inline const uint8_t *__attribute__((always_inline)) feed(uint8_t c) {
// Most bytes are in the middle of a frame whose length is known, and not its last byte
const pos_t pos = this->pos_;
if (pos + 1u < this->end_) {
this->buf_[pos] = c;
this->pos_ = pos + 1;
return nullptr;
}
return this->feed_edge_(c);
}
/// Length of the frame feed(), resync() or next() returned; valid until the next call on the reader.
size_t frame_length() const {
if constexpr (sizeof...(FORMATS) == 1) {
return frame_reader_detail::First<FORMATS...>::type::Length::get(this->buf_);
} else {
const uint8_t first = this->buf_[0];
size_t len = 0;
(void) ((first == FORMATS::FIRST && ((len = FORMATS::Length::get(this->buf_)), true)) || ...);
return len;
}
}
/// Number of bytes of the partial frame; 0 after a frame is returned and while looking for a header.
size_t length() const { return this->pos_; }
/// Forget the partial frame and any held bytes, and look for a header from the next byte.
void reset() {
this->pos_ = 0;
this->end_ = 0;
this->held_end_ = 0;
}
/// Lower level: for a rejected frame, or a partial one: drop its first byte and look for a frame in the rest.
/// Returns the next frame found, or nullptr. Does nothing when no frame is held and nothing is collected.
const uint8_t *resync() { return this->replay_(1, this->pos_ != 0 ? this->pos_ : this->held_end_); }
/// Lower level: after handling a frame, look for the next one in the bytes that followed it in a resync().
/// Returns that frame, or nullptr once they are used up.
const uint8_t *next() { return this->replay_(this->frame_length(), this->held_end_); }
protected:
template<class F> void ESPHOME_FRAME_READER_COLD drain_(const uint8_t *frame, F &handle) {
while (frame != nullptr)
frame = handle(frame, this->frame_length()) ? this->next() : this->resync();
}
// A header byte, the bytes up to the length, the last byte of a frame, or a byte between frames
const uint8_t *ESPHOME_FRAME_READER_COLD feed_edge_(uint8_t c) {
const pos_t pos = this->pos_;
if (pos == 0) {
this->held_end_ = 0;
const bool header = ((c == FORMATS::FIRST) || ...);
if (header) {
this->buf_[0] = c;
this->pos_ = 1;
}
return nullptr;
}
if constexpr (sizeof...(FORMATS) == 1) {
return this->feed_as_<FORMATS...>(pos, c);
} else {
const uint8_t first = this->buf_[0];
const uint8_t *frame = nullptr;
(void) ((first == FORMATS::FIRST && ((frame = this->feed_as_<FORMATS>(pos, c)), true)) || ...);
return frame;
}
}
template<class F> const uint8_t *feed_as_(pos_t pos, uint8_t c) {
if (pos < F::HEADER_LEN && c != F::header_byte(pos)) {
// A later byte of the broken header, or c, may start the real one
this->buf_[pos] = c;
return this->replay_(1, pos + 1);
}
this->buf_[pos++] = c;
if (pos == F::MIN_LEN) {
const size_t len = F::Length::get(this->buf_);
if (len < F::MIN_LEN || len > N)
return this->replay_(1, pos);
this->end_ = len;
}
if (pos == this->end_) {
this->pos_ = 0;
this->end_ = 0;
this->held_end_ = pos;
return this->buf_;
}
this->pos_ = pos;
return nullptr;
}
// Start over and feed buf_[from..to) again; writes stay behind the read position. On a completed frame, moves the
// bytes not fed yet right behind it, for next() and resync(), and returns it.
const uint8_t *ESPHOME_FRAME_READER_COLD replay_(size_t from, size_t to) {
this->pos_ = 0;
this->end_ = 0;
this->held_end_ = 0;
for (size_t i = from; i < to; i++) {
if (this->feed_edge_(this->buf_[i]) == nullptr)
continue;
// A nested replay may already have put bytes of its own behind the frame
const size_t held = this->held_end_;
const size_t rest = to - i - 1;
std::memmove(this->buf_ + held, this->buf_ + i + 1, rest);
this->held_end_ = held + rest;
return this->buf_;
}
return nullptr;
}
uint8_t buf_[N];
pos_t pos_{0};
// Length of the frame being collected once its length is known; 0 otherwise
pos_t end_{0};
// While a frame is held: end of the frame plus the bytes after it that are not fed yet; 0 otherwise
pos_t held_end_{0};
};
} // namespace esphome
#undef ESPHOME_FRAME_READER_COLD
+1 -1
View File
@@ -10,7 +10,7 @@ dependencies:
esphome/micro-decoder:
version: 0.4.0
esphome/micro-flac:
version: 0.3.0
version: 0.3.1
esphome/micro-mp3:
version: 0.4.0
esphome/micro-opus:
+1 -1
View File
@@ -214,7 +214,7 @@ class StorageJSON:
hardware = esp32.get_esp32_variant(esph)
framework_version = str(esp32.idf_version())
elif esph.is_nrf52:
framework_version = str(esph.framework_version)
framework_version = str(esph.data[KEY_CORE][KEY_FRAMEWORK_VERSION])
return StorageJSON(
storage_version=1,
name=esph.name,
@@ -20,6 +20,12 @@ static constexpr uint8_t FRAME_30[30] = {0xAA, 0xFF, 0x03, 0x00, 0x0E, 0x03, 0xB
static constexpr uint8_t ULTRASONIC_NOISY[] = {0x12, 0xFF, 0x07, 0xA1, 0xA7, 0x34, 0xFF, 0xFF, 0x07, 0xA1, 0xA7,
0x00, 0xFF, 0x07, 0xA1, 0xA7, 0x56, 0xFF, 0x07, 0xA1, 0xA7};
// Pioneer WT shaped frames (A5 01 header, whole-frame length at byte 7) of 12, 20 and 12 bytes among noise
static constexpr uint8_t LENGTH_FRAMES[] = {0x00, 0xA5, 0x01, 0x01, 0x23, 0x00, 0xA5, 0x00, 0x0C, 0x11, 0x22, 0x33,
0x44, 0xA5, 0x01, 0x01, 0x23, 0x00, 0xA5, 0x00, 0x14, 0x11, 0x22, 0x33,
0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xAA, 0xBB, 0xCC, 0x7E, 0xA5, 0x01,
0x01, 0x23, 0x00, 0xA5, 0x00, 0x0C, 0x11, 0x22, 0x33, 0x44};
// --- LineReader: one GGA sentence fed byte by byte ---
static void LineReader_NmeaLine(benchmark::State &state) {
@@ -93,4 +99,28 @@ static void FrameReader_UltrasonicNoisy(benchmark::State &state) {
}
BENCHMARK(FrameReader_UltrasonicNoisy);
// --- LengthFrameReader: frames of different lengths among noise ---
static void LengthFrameReader_MixedFrames(benchmark::State &state) {
LengthFrameReader<255, FrameFormat<FrameLengthU8<7>, 0xA5, 0x01>> reader;
const uint8_t *bytes = LENGTH_FRAMES;
uint32_t frames = 0;
for (auto _ : state) {
benchmark::DoNotOptimize(bytes);
for (int i = 0; i < kInnerIterations; i++) {
for (size_t j = 0; j < sizeof(LENGTH_FRAMES); j++) {
const uint8_t *frame = reader.feed(bytes[j]);
if (frame == nullptr)
continue;
benchmark::DoNotOptimize(frame);
frames++;
}
}
benchmark::DoNotOptimize(frames);
}
state.SetItemsProcessed(state.iterations() * kInnerIterations);
}
BENCHMARK(LengthFrameReader_MixedFrames);
} // namespace esphome::benchmarks
+207
View File
@@ -140,4 +140,211 @@ TEST(FrameReaderTest, SixteenBitIndex) {
EXPECT_EQ(frames[0][299], 0x42);
}
// Feeds bytes and collects every completed frame of a LengthFrameReader, taking each one with next().
template<size_t N, class... F> static Frames feed_all(LengthFrameReader<N, F...> &reader, const Bytes &input) {
Frames frames;
for (uint8_t c : input) {
for (const uint8_t *frame = reader.feed(c); frame != nullptr; frame = reader.next())
frames.emplace_back(frame, frame + reader.frame_length());
}
return frames;
}
// Feeds bytes and returns the last frame feed() completed, or nullptr.
template<size_t N, class... F> static const uint8_t *feed_last(LengthFrameReader<N, F...> &reader, const Bytes &input) {
const uint8_t *last = nullptr;
for (uint8_t c : input) {
if (const uint8_t *frame = reader.feed(c))
last = frame;
}
return last;
}
template<size_t N, class... F> static Bytes held(const LengthFrameReader<N, F...> &reader, const uint8_t *frame) {
return Bytes(frame, frame + reader.frame_length());
}
// pioneer_wt: A5 01, one byte at 7 holds the whole length
using OneByte = FrameFormat<FrameLengthU8<7>, 0xA5, 0x01>;
// tuya: 55 AA, payload length big-endian at 4, header, length and checksum add 7
using TwoByteBE = FrameFormat<FrameLengthU16BE<4, 7>, 0x55, 0xAA>;
// ld24xx command and data frames: payload length little-endian at 4, header, length and footer add 10
using LdCmd = FrameFormat<FrameLengthU16LE<4, 10>, 0xFD, 0xFC, 0xFB, 0xFA>;
using LdData = FrameFormat<FrameLengthU16LE<4, 10>, 0xF4, 0xF3, 0xF2, 0xF1>;
// ld2410s short frame: 6E, always 5 bytes
using LdShort = FrameFormat<FrameFixedLength<5>, 0x6E>;
// Small formats for the recovery tests: A5, one byte at 1 holds the whole length
using Small = FrameFormat<FrameLengthU8<1>, 0xA5>;
static_assert(sizeof(LengthFrameReader<255, OneByte>) == 258);
static_assert(sizeof(LengthFrameReader<300, TwoByteBE>) == 306);
TEST(LengthFrameReaderTest, OneByteLength) {
LengthFrameReader<16, OneByte> reader;
EXPECT_EQ(feed_all(reader, {0x00, 0xA5, 0x01, 0x10, 0x11, 0x12, 0x13, 0x00, 0x09, 0x42}),
(Frames{{0xA5, 0x01, 0x10, 0x11, 0x12, 0x13, 0x00, 0x09, 0x42}}));
}
TEST(LengthFrameReaderTest, TwoByteBigEndianPayloadLength) {
LengthFrameReader<300, TwoByteBE> reader;
// Payload of 0x0102 = 258 bytes: 265 in all
Bytes input{0x55, 0xAA, 0x03, 0x07, 0x01, 0x02};
input.resize(265, 0x33);
Frames frames = feed_all(reader, input);
ASSERT_EQ(frames.size(), 1u);
EXPECT_EQ(frames[0].size(), 265u);
// Too long for N: not a frame
EXPECT_EQ(feed_all(reader, {0x55, 0xAA, 0x03, 0x07, 0x01, 0x30}), Frames{});
EXPECT_EQ(reader.length(), 0u);
}
TEST(LengthFrameReaderTest, TwoFormatsPickedByFirstByte) {
LengthFrameReader<32, LdCmd, LdData> reader;
const Bytes cmd{0xFD, 0xFC, 0xFB, 0xFA, 0x02, 0x00, 0xA0, 0x01, 0x04, 0x03, 0x02, 0x01};
const Bytes data{0xF4, 0xF3, 0xF2, 0xF1, 0x01, 0x00, 0x55, 0xF8, 0xF7, 0xF6, 0xF5};
Bytes input{0x00};
input.insert(input.end(), data.begin(), data.end());
input.insert(input.end(), cmd.begin(), cmd.end());
EXPECT_EQ(feed_all(reader, input), (Frames{data, cmd}));
}
TEST(LengthFrameReaderTest, FixedAndLengthFormatsTogether) {
LengthFrameReader<32, LdShort, LdData> reader;
const Bytes small{0x6E, 0x01, 0x02, 0x03, 0x62};
const Bytes data{0xF4, 0xF3, 0xF2, 0xF1, 0x00, 0x00, 0xF8, 0xF7, 0xF6, 0xF5};
Bytes input = small;
input.insert(input.end(), data.begin(), data.end());
input.insert(input.end(), small.begin(), small.end());
EXPECT_EQ(feed_all(reader, input), (Frames{small, data, small}));
}
TEST(LengthFrameReaderTest, BrokenHeaderRealigns) {
LengthFrameReader<32, LdCmd, LdData> reader;
// FD FC then F4 breaks the command header and starts a data header
const Bytes data{0xF4, 0xF3, 0xF2, 0xF1, 0x00, 0x00, 0xF8, 0xF7, 0xF6, 0xF5};
Bytes input{0xFD, 0xFC};
input.insert(input.end(), data.begin(), data.end());
EXPECT_EQ(feed_all(reader, input), (Frames{data}));
}
TEST(LengthFrameReaderTest, BadLengthRealignsAfterFirstByte) {
LengthFrameReader<8, Small> reader;
// 0x01 ends the frame before its length byte and 0x09 is longer than 8: neither is a frame.
// The second A5 inside the bad one starts the real frame.
EXPECT_EQ(feed_all(reader, {0xA5, 0x01, 0xA5, 0x09, 0xA5, 0x04, 0x01, 0x02}), (Frames{{0xA5, 0x04, 0x01, 0x02}}));
}
TEST(LengthFrameReaderTest, LengthByteCanStartHeader) {
LengthFrameReader<8, FrameFormat<FrameLengthU8<1>, 0x09>> reader;
// Length 9 is longer than 8, so the length byte becomes the next header
EXPECT_EQ(feed_all(reader, {0x09, 0x09, 0x03, 0x07}), (Frames{{0x09, 0x03, 0x07}}));
}
TEST(LengthFrameReaderTest, LengthTracksPartialFrame) {
LengthFrameReader<8, Small> reader;
feed_all(reader, {0xA5, 0x05});
EXPECT_EQ(reader.length(), 2u);
reader.reset();
EXPECT_EQ(reader.length(), 0u);
EXPECT_EQ(feed_all(reader, {0x01, 0xA5, 0x03, 0x07}), (Frames{{0xA5, 0x03, 0x07}}));
}
TEST(LengthFrameReaderTest, ResyncFindsHeaderInRejectedFrame) {
LengthFrameReader<8, Small> reader;
// The A5 taken as a header was payload; the real frame starts at the second A5
const uint8_t *frame = feed_last(reader, {0xA5, 0x05, 0x00, 0xA5, 0x04});
ASSERT_NE(frame, nullptr);
EXPECT_EQ(reader.frame_length(), 5u);
EXPECT_EQ(reader.resync(), nullptr);
EXPECT_EQ(reader.length(), 2u);
EXPECT_EQ(feed_all(reader, {0x10, 0x20}), (Frames{{0xA5, 0x04, 0x10, 0x20}}));
}
TEST(LengthFrameReaderTest, ResyncReturnsEveryFrameInsideRejectedOne) {
LengthFrameReader<16, Small> reader;
// A bad checksum rejects the 9 byte frame; two complete frames sit inside it, and a partial one after them
const uint8_t *frame = feed_last(reader, {0xA5, 0x09, 0xA5, 0x03, 0x01, 0xA5, 0x02, 0xA5, 0x04});
ASSERT_NE(frame, nullptr);
frame = reader.resync();
ASSERT_NE(frame, nullptr);
EXPECT_EQ(held(reader, frame), (Bytes{0xA5, 0x03, 0x01}));
frame = reader.next();
ASSERT_NE(frame, nullptr);
EXPECT_EQ(held(reader, frame), (Bytes{0xA5, 0x02}));
EXPECT_EQ(reader.next(), nullptr);
EXPECT_EQ(reader.length(), 2u);
EXPECT_EQ(feed_all(reader, {0x01, 0x02}), (Frames{{0xA5, 0x04, 0x01, 0x02}}));
}
TEST(LengthFrameReaderTest, ResyncAgainAfterRecoveredFrameIsRejected) {
LengthFrameReader<16, Small> reader;
// The 9 byte frame holds a 4 byte candidate, which holds a 3 byte one, followed by two more bytes
const uint8_t *frame = feed_last(reader, {0xA5, 0x09, 0xA5, 0x04, 0xA5, 0x03, 0x01, 0x02, 0x03});
ASSERT_NE(frame, nullptr);
frame = reader.resync();
ASSERT_NE(frame, nullptr);
EXPECT_EQ(held(reader, frame), (Bytes{0xA5, 0x04, 0xA5, 0x03}));
// Rejected too: the 3 byte frame inside it comes back, and the bytes after the 4 byte one are kept
frame = reader.resync();
ASSERT_NE(frame, nullptr);
EXPECT_EQ(held(reader, frame), (Bytes{0xA5, 0x03, 0x01}));
EXPECT_EQ(reader.next(), nullptr);
EXPECT_EQ(reader.length(), 0u);
}
TEST(LengthFrameReaderTest, ResyncPartialFrameAfterSilence) {
LengthFrameReader<16, Small> reader;
// A corrupt length of 15 holds back two complete frames until the caller gives up on the partial one
EXPECT_EQ(feed_all(reader, {0xA5, 0x0F, 0xA5, 0x04, 0x01, 0x02, 0xA5, 0x03, 0x09}), Frames{});
EXPECT_EQ(reader.length(), 9u);
Frames found;
for (const uint8_t *frame = reader.resync(); frame != nullptr; frame = reader.next())
found.push_back(held(reader, frame));
EXPECT_EQ(found, (Frames{{0xA5, 0x04, 0x01, 0x02}, {0xA5, 0x03, 0x09}}));
EXPECT_EQ(reader.length(), 0u);
}
TEST(LengthFrameReaderTest, ResyncWhileIdleIsHarmless) {
LengthFrameReader<8, Small> reader;
EXPECT_EQ(reader.resync(), nullptr);
// A handled frame, then noise: nothing is held any more
EXPECT_EQ(feed_all(reader, {0xA5, 0x03, 0x01, 0x00}), (Frames{{0xA5, 0x03, 0x01}}));
EXPECT_EQ(reader.resync(), nullptr);
// A rejected length leaves nothing collected
EXPECT_EQ(feed_all(reader, {0xA5, 0x09}), Frames{});
EXPECT_EQ(reader.resync(), nullptr);
EXPECT_EQ(feed_all(reader, {0xA5, 0x03, 0x01}), (Frames{{0xA5, 0x03, 0x01}}));
}
TEST(LengthFrameReaderTest, HandlerRejectionRecoversFramesInside) {
LengthFrameReader<16, Small> reader;
Frames seen;
// The handler rejects the 9 byte frame; the two frames inside it reach the handler too
auto handle = [&seen](const uint8_t *frame, size_t len) {
seen.emplace_back(frame, frame + len);
return len != 9;
};
for (uint8_t c : {0xA5, 0x09, 0xA5, 0x03, 0x01, 0xA5, 0x02, 0xA5, 0x04})
reader.feed(c, handle);
EXPECT_EQ(seen, (Frames{{0xA5, 0x09, 0xA5, 0x03, 0x01, 0xA5, 0x02, 0xA5, 0x04}, {0xA5, 0x03, 0x01}, {0xA5, 0x02}}));
EXPECT_EQ(reader.length(), 2u);
}
TEST(LengthFrameReaderTest, FlushHandsOverFramesHeldBack) {
LengthFrameReader<16, Small> reader;
Frames seen;
auto handle = [&seen](const uint8_t *frame, size_t len) {
seen.emplace_back(frame, frame + len);
return true;
};
for (uint8_t c : {0xA5, 0x0F, 0xA5, 0x04, 0x01, 0x02, 0xA5, 0x03, 0x09})
reader.feed(c, handle);
EXPECT_EQ(seen, Frames{});
reader.flush(handle);
EXPECT_EQ(seen, (Frames{{0xA5, 0x04, 0x01, 0x02}, {0xA5, 0x03, 0x09}}));
EXPECT_EQ(reader.length(), 0u);
reader.flush(handle); // nothing collected: nothing to do
EXPECT_EQ(seen.size(), 2u);
}
} // namespace esphome::core::testing
@@ -6,6 +6,7 @@
#include <cstdint>
#include "esphome/components/light/esp_color_correction.h"
#include "esphome/components/light/esp_color_view.h"
namespace esphome::light::testing {
@@ -67,6 +68,69 @@ TEST(GammaCorrection, DeadZoneFixedAtGamma28) {
}
}
// A whole pixel keeps its colour when dim: only the strongest channel is lifted to 1.
TEST(GammaCorrection, DimPixelKeepsDominantChannel) {
GammaFixture fixture(2.8);
fixture.correction.set_local_brightness(13);
EXPECT_EQ(fixture.correction.color_correct(Color(255, 30, 0, 0)), Color(1, 0, 0, 0));
EXPECT_EQ(fixture.correction.color_correct(Color(0, 0, 255, 30)), Color(0, 0, 1, 0));
}
// Orange at 2% brightness and below: the table reads 1 for both channels there, and at the lowest
// step both scale to the same 8-bit value, so neither can be used to compare them.
TEST(GammaCorrection, VeryDimPixelKeepsDominantChannel) {
GammaFixture fixture(2.8);
for (uint8_t brightness : {1, 2, 3, 4, 5, 6}) {
fixture.correction.set_local_brightness(brightness);
EXPECT_EQ(fixture.correction.color_correct(Color(255, 130, 0, 0)), Color(1, 0, 0, 0))
<< "brightness=" << int(brightness);
}
}
TEST(GammaCorrection, DimPixelKeepsSimilarChannels) {
GammaFixture fixture(2.8);
fixture.correction.set_local_brightness(13);
EXPECT_EQ(fixture.correction.color_correct(Color(255, 255, 255, 0)), Color(1, 1, 1, 0));
EXPECT_EQ(fixture.correction.color_correct(Color(255, 230, 0, 0)), Color(1, 1, 0, 0));
fixture.correction.set_local_brightness(1);
EXPECT_EQ(fixture.correction.color_correct(Color(255, 250, 250, 0)), Color(1, 1, 1, 0));
}
// A channel that color_correct sets to 0% stays dark even when it is the strongest in the pixel.
TEST(GammaCorrection, DimPixelLeavesDisabledChannelDark) {
GammaFixture fixture(2.8);
fixture.correction.set_max_brightness(Color(255, 0, 255, 255));
EXPECT_EQ(fixture.correction.color_correct(Color(1, 255, 0, 0)), Color(1, 0, 0, 0));
}
// A strip without white drops the white level before the pixel is rounded, so it cannot outrank red.
TEST(GammaCorrection, UnusedWhiteDoesNotDarkenRgbStrip) {
GammaFixture fixture(2.8);
fixture.correction.set_local_brightness(13);
std::array<uint8_t, 3> color_data{};
std::array<uint8_t, 1> effect_data{};
CorrectedBufferColorAccessor accessor(color_data.data(), effect_data.data(), ChannelColors{0, 1, 2},
&fixture.correction);
accessor.set(0, Color(128, 0, 0, 255));
EXPECT_EQ(color_data, (std::array<uint8_t, 3>{1, 0, 0}));
}
TEST(GammaCorrection, LitPixelDropsChannelThatRoundsToZero) {
GammaFixture fixture(2.8);
EXPECT_EQ(fixture.correction.color_correct(Color(255, 5, 0, 0)), Color(255, 0, 0, 0));
}
// The pixel-level form of esphome/esphome#18842: a non-zero pixel never goes fully dark.
TEST(GammaCorrection, NonZeroPixelStaysLit) {
for (double gamma : {1.0, 2.2, 2.8, 4.0}) {
GammaFixture fixture(gamma);
for (int i = 1; i < 256; i++) {
EXPECT_GE(fixture.correction.color_correct(Color(i, 0, 0, 0)).red, 1) << "gamma=" << gamma << " index=" << i;
}
EXPECT_EQ(fixture.correction.color_correct(Color(0, 0, 0, 0)), Color(0, 0, 0, 0)) << "gamma=" << gamma;
}
}
TEST(GammaCorrection, ReverseSearchFindsLargestIndexLessEqualTarget) {
auto table = build_gamma_table(2.8);
for (uint16_t target : {0, 128, 129, 135, 1000, 32768, 65535}) {
+12
View File
@@ -0,0 +1,12 @@
import esphome.codegen as cg
from tests.testing_helpers import ComponentManifestOverride
def override_manifest(manifest: ComponentManifestOverride) -> None:
async def to_code_testing(config):
# The accessor hooks that partitions use, and the gamma table of the light under test
cg.add_define("USE_LIGHT_PARTITION")
cg.add_define("USE_LIGHT_GAMMA_LUT")
manifest.to_code = to_code_testing
manifest.dependencies = manifest.dependencies + ["light"]
@@ -0,0 +1,56 @@
#include <gtest/gtest.h>
#include <array>
#include <cmath>
#include <cstdint>
#include "esphome/components/light/addressable_light.h"
#include "esphome/components/light/light_state.h"
#include "esphome/components/partition/light_partition.h"
namespace esphome::partition::testing {
namespace {
// One RGB pixel in memory
class RgbStrip : public light::AddressableLight {
public:
RgbStrip()
: internal_accessor_(this->buffer_.data(), this->buffer_.data() + 3, light::ChannelColors{0, 1, 2},
&this->correction_) {
this->accessor_ = &this->internal_accessor_;
}
int32_t size() const override { return 1; }
void clear_effect_data() override {}
light::LightTraits get_traits() override { return {}; }
void write_state(light::LightState *state) override {}
protected:
std::array<uint8_t, 4> buffer_{};
light::CorrectedBufferColorAccessor internal_accessor_;
};
} // namespace
// The partition corrects a pixel before the strip drops its white level, so it has to drop it first.
TEST(PartitionWhite, UnusedWhiteDoesNotDarkenRgbSegment) {
light::GammaTable table{};
for (int i = 1; i < 256; i++)
table.lut[i] = std::max(1.0, std::round(std::pow(i / 255.0, 2.8) * 65535.0));
table.gamma_x100 = 280;
RgbStrip strip;
light::LightState strip_state(&strip);
PartitionLightOutput partition({AddressableSegment(&strip_state, 0, 1, false)});
light::LightState partition_state(&partition);
partition_state.set_gamma_table(&table);
partition.setup_state(&partition_state);
partition_state.current_values.set_state(1.0f);
partition_state.current_values.set_brightness(13 / 255.0f);
partition.update_state(&partition_state);
partition[0] = Color(128, 0, 0, 255);
EXPECT_EQ(strip[0].get_raw(), Color(1, 0, 0, 0));
}
} // namespace esphome::partition::testing
@@ -15,7 +15,13 @@ from esphome.components.nrf52.const import (
)
from esphome.components.nrf52.framework import get_nrf52_variant, set_nrf52_variant
import esphome.config_validation as cv
from esphome.const import CONF_BOARD, CONF_FRAMEWORK, CONF_VERSION, KEY_CORE
from esphome.const import (
CONF_BOARD,
CONF_FRAMEWORK,
CONF_VERSION,
KEY_CORE,
KEY_FRAMEWORK_VERSION,
)
from esphome.core import CORE, EsphomeCore
from esphome.types import ConfigType
@@ -73,7 +79,7 @@ def test_default_sdk_version(variant: str, default: str) -> None:
set_nrf52_variant(variant)
config = _set_framework(None)
assert config[CONF_FRAMEWORK][CONF_VERSION] == default
assert CORE.framework_version == cv.Version.parse(default)
assert CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION] == cv.Version.parse(default)
@pytest.mark.usefixtures("platformio")
-5
View File
@@ -582,11 +582,6 @@ class TestEsphomeCore:
assert t == other
def test_framework_version(self, target: core.EsphomeCore) -> None:
version = core.Version(1, 12, 0)
target.data[const.KEY_CORE] = {const.KEY_FRAMEWORK_VERSION: version}
assert target.framework_version is version
def test_address__none(self, target):
target.config = {}
assert target.address is None
+5 -2
View File
@@ -369,6 +369,8 @@ def test_storage_json_from_esphome_core_mdns_enabled(setup_core: Path) -> None:
def test_storage_json_from_esphome_core_nrf52(setup_core: Path) -> None:
"""Test from_esphome_core captures the framework version on nRF52."""
from esphome.const import KEY_CORE, KEY_FRAMEWORK_VERSION
mock_core = MagicMock()
mock_core.name = "nrf_device"
mock_core.friendly_name = "nRF Device"
@@ -378,7 +380,7 @@ def test_storage_json_from_esphome_core_nrf52(setup_core: Path) -> None:
mock_core.target_platform = "nrf52"
mock_core.is_esp32 = False
mock_core.is_nrf52 = True
mock_core.framework_version = cv.Version(2, 9, 2)
mock_core.data = {KEY_CORE: {KEY_FRAMEWORK_VERSION: cv.Version(2, 9, 2)}}
mock_core.build_path = "/build/nrf_device"
mock_core.firmware_bin = "/build/nrf_device/firmware.bin"
mock_core.loaded_integrations = set()
@@ -840,6 +842,7 @@ def _make_nrf52_storage(
def test_storage_json_nrf52_framework_version_round_trip(setup_core: Path) -> None:
"""Sidecar framework_version restores CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION]."""
from esphome.const import KEY_CORE, KEY_FRAMEWORK_VERSION
storage = _make_nrf52_storage("2.9.2")
path = setup_core / "storage.json"
@@ -852,7 +855,7 @@ def test_storage_json_nrf52_framework_version_round_trip(setup_core: Path) -> No
assert loaded.framework_version == "2.9.2"
loaded.apply_to_core()
assert CORE.framework_version == cv.Version(2, 9, 2)
assert CORE.data[KEY_CORE][KEY_FRAMEWORK_VERSION] == cv.Version(2, 9, 2)
def test_storage_json_nrf52_apply_to_core_without_framework_version(