Compare commits

...
Author SHA1 Message Date
J. Nick Koston cb5555129c Register the action with register_apply_action and test every field 2026-10-15 13:20:49 -10:00
J. Nick Koston 17e942826b [http_request] Build ota.http_request.flash with register_apply_action 2026-10-15 13:20:35 -10:00
J. Nick Koston c838d4826e [core] Add ESPHOME_strstr_P (#20811) 2026-10-15 23:09:02 +00:00
J. Nick Koston 69215deb58 [core] Make the YAML load listener hook public (#20805) 2026-10-15 12:54:34 -10:00
J. Nick Koston 5b234bcc20 [wifi] Leave the cooldown from the loop after a credential save, not from the caller's context (#20802) 2026-10-15 12:53:51 -10:00
Panoplianandclydebarrow c101b3c101 [mipi_spi] Honour show_test_card when pages or a lambda are set (#20807)
Co-authored-by: clydebarrow <2366188+clydebarrow@users.noreply.github.com>
2026-10-15 22:38:28 +00:00
Anton ViktorovandJ. Nick Koston c0d8a0dfe9 [as734x] [3/3] Add illuminance, photopic irradiance, PAR, PPFD, colour temperature and RGB (#18012)
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
2026-10-15 11:38:15 -10:00
J. Nick Koston 27c6b7caf5 [ci] Add hardware-test agent skill (#20817) 2026-10-15 21:36:36 +00:00
699e3bd037 [usb_uart] Runtime modem pin control for USB and host (#20330)
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
2026-10-15 11:03:27 -10:00
Anton ViktorovandJ. Nick Koston a58ce2d641 [as734x] [2/3] Add basic counts, calibration and saturation level (#18011)
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
2026-10-15 10:48:18 -10:00
Remco van EssenandClaude Opus 5.5 27d8d49b17 [dfrobot_sen0609] Add number, switch and select settings (#20813)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 10:41:20 -10:00
Remco van Essen 304627efd3 [ld2410][ld2412] Move CONF_OUT_PIN_LEVEL to the shared constants (#20820) 2026-10-15 10:34:00 -10:00
7ed06e0ada [as734x] [1/3] Add AS7341/AS7343/TCS3448 spectral sensor hub with counts sensors (#17038)
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
Co-authored-by: J. Nick Koston <nick@koston.org>
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
2026-10-15 10:19:56 -10:00
dependabot[bot] 7e564c12dd Bump py7zr from 1.1.3 to 1.1.4 (#20816)
Signed-off-by: dependabot[bot] <support@github.com>
2026-10-15 09:58:07 -10:00
dependabot[bot] 6f6fdd0f8a Bump actions/create-github-app-token from 3.2.0 to 3.3.0 (#20815)
Signed-off-by: dependabot[bot] <support@github.com>
2026-10-15 09:55:47 -10:00
45ea5f4220 [nrf52] nrf52840dongle support (#19009)
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
Co-authored-by: J. Nick Koston <nick@koston.org>
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
2026-10-15 09:03:53 -10:00
techdregsandJ. Nick Koston 108ed67d31 [rx8111] Add basic RX8111CE real-time clock support (#20785)
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
2026-10-15 08:30:25 -10:00
Remco van EssenandClaude Opus 5.5 6356458081 [dfrobot_sen0609] Add DFRobot SEN0609/SEN0610 radar (#20809)
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-15 18:27:57 +00:00
Panoplian bf67b49104 [i2c] Let host unit tests with I2C build on macOS (#20738) 2026-10-15 07:47:52 -10:00
Panoplian 1947c3d355 [improv_ble] Start Improv again after a session has stopped (#20808) 2026-10-15 07:47:14 -10:00
J. Nick Koston cf4e69273c [shelly_dimmer] Keep the STM32 device table in flash (#20643) 2026-10-15 07:28:55 -10:00
J. Nick Koston c433e96734 [homeassistant] Share the Python list parser between the platforms (#20793) 2026-10-15 07:28:27 -10:00
J. Nick Koston 93dc80d895 [esp8266] Refuse two components that drive hardware timer 1 (#20794) 2026-10-15 07:28:07 -10:00
J. Nick Koston 7f342054fe [wifi] Retry a failed fallback AP once per ap_timeout and start the captive portal once (#20784) 2026-10-15 07:27:19 -10:00
Jeroenandjeroen85 d924d2daf7 [core] Extend fast value formatting to four decimal places (#20800)
Co-authored-by: jeroen85 <26403565+jeroen85@users.noreply.github.com>
2026-10-15 07:15:11 -10:00
Clyde Stubbs 49b12daa58 [epaper_spi] Ensure test card gets drawn (#20804) 2026-10-15 10:55:19 +00:00
Clyde Stubbs d3faafc578 [display] Always treat 888 pixel blocks as b,g,r in memory (#20799) 2026-10-15 20:49:50 +11:00
Remco van Essen 7b08e738d3 [fusb302b] Add sensors, contract text sensor and automations (#20796) 2026-10-14 22:13:58 -10:00
J. Nick Koston 62e3c25c83 [lvgl] Fix the menu test after lambdas gained a line reset (#20798) 2026-10-14 21:50:47 -10:00
6a50f5c166 [apc_proteous] Cover component for APC Proteous gate motors (#13403)
Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
2026-10-15 17:46:40 +11:00
Clyde Stubbs bb0837d814 [lvgl] Implement menu widget (#18022) 2026-10-15 17:44:23 +11:00
Bonne Egglestonandclydebarrow ecd5980c5d [epaper_spi] Pass bits per pixel and plane count to the base constructor (#20683)
Co-authored-by: clydebarrow <2366188+clydebarrow@users.noreply.github.com>
2026-10-15 17:41:37 +11:00
8d963bde5b [openthread] Add ESP32 OpenThread Border Router support (#18104)
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
Co-authored-by: J. Nick Koston <nick@home-assistant.io>
Co-authored-by: J. Nick Koston <nick@koston.org>
2026-10-14 20:02:33 -10:00
J. Nick Koston ea6d5499b3 [lsm6ds] Add the LSM6DSO and match all chips to their datasheets (#20733) 2026-10-14 19:24:00 -10:00
214 changed files with 11433 additions and 825 deletions
+123
View File
@@ -0,0 +1,123 @@
---
name: hardware-test
description: Test an ESPHome change on real boards over USB. Use this whenever a task asks to test, verify, reproduce, bench, A/B or soak a pull request, a branch or a crash on hardware, or mentions flashing, serial logs, esphome upload, a captive portal or fallback AP test, or results to post on a PR. Covers finding the serial ports, building from the right checkout, flashing each chip family (including the LibreTiny kits that stock tools cannot flash), bounded log capture, proven test flows and how to report results.
allowed-tools: Read, Write, Edit, Bash, Glob, Grep
---
# Hardware testing
The goal of a hardware test is evidence a reviewer can trust: which build ran on which board, what was
driven, what the logs show, and the same flow on a `dev` build when a quirk could be pre-existing.
`$SKILL` below is this directory (`.agents/skills/hardware-test`).
## Ground rules
1. **Serial ports cannot be shared.** One bounded capture per port, stopped before any flash on that
port, never an open-ended `esphome logs`. Stop a capture with `pkill -f "[s]erial_capture.py <port> "`,
naming the port so a capture on another board survives; the bracket keeps the pattern from matching
the shell that runs it. Never run `esphome logs` in a
`while true` loop: the loop respawns it and holds the port.
2. **Credentials stay local.** Put a `secrets.yaml` next to the scratch configs and reference it with
`!secret`. Never paste a config with credentials into a PR, never commit scratch configs or logs.
3. **Build from the tree under test.** `python -m esphome` puts the current directory first on
`sys.path`, so run it with the checkout or worktree as the working directory and pass the yaml by
its absolute path. A venv `esphome` entry point may import an installed snapshot instead of the
checkout. Confirm with `python -c 'import esphome; print(esphome.__file__)'`, and for an A/B grep
the build's `src/` for the change before trusting any number.
4. **PR build and baseline build get different `name:` values.** Builds share
`.esphome/build/<name>`, so the same name means `esphome upload` can flash the wrong binary. Saved
Wi-Fi credentials are keyed by the name too, so a test that must start unprovisioned gets a fresh
name every run.
5. **Flash and RAM deltas are measured against the PR's merge base** (`git merge-base origin/dev HEAD`
checked out in a throwaway worktree), with the baseline commit named in the table. When a delta
looks odd, diff symbol sizes (`nm -S -C --size-sort` from the matching toolchain) before quoting it.
6. **Prove a quirk is pre-existing before blaming the PR:** run the identical flow on a `dev` build in
the same session and say so in the results.
7. **Pipe safely.** Set `PYTHONUNBUFFERED=1` on any `esphome run` or `esphome logs` whose output is
piped, or lines land minutes late. Bound every long command with `timeout` (coreutils) or
`perl -e 'alarm shift; exec @ARGV' <seconds> <command>` on macOS without it.
## Workflow
1. **Find the boards:** `python $SKILL/scripts/ports.py` (any Python with pyserial, for example the
ESPHome venv). Port names follow the USB hub port and change on every replug, so never reuse a name
from an earlier session. CH340 adapters (D1 mini, BW15 kit, BK7238, CB3S) all look the same; tell
them apart with a 10 s capture using `serial_capture.py --reset` (ESP8266 prints its ROM banner at
74880 baud, LibreTiny boards print `[LT]` and ESPHome lines, BK7238 prints its bootloader version).
`ports.py --wait 2e8a` blocks until a Pico re-enumerates.
2. **Make a scratch directory** outside the checkout, with `secrets.yaml` and the templates you need.
One yaml per board per variant, file name equal to `name:`.
3. **Compile** in the right tree:
`(cd <worktree> && PYTHONUNBUFFERED=1 timeout 1500 python -m esphome compile <abs yaml> > <name>-compile.log 2>&1)`.
Keep the RAM and flash lines at the end. Two ESP-IDF or two LibreTiny builds at the same time race
over tool caches; run them one at a time.
4. **Flash** per chip family (table below). Stop the capture on that port first.
5. **Capture** with `serial_capture.py PORT SECONDS OUT` in the background, then poll the file with
`grep` in an `until ... sleep 2` loop with a cap. USB CDC boards (Pico, nRF52, native USB ESP32)
drop the port on reboot; the script reopens it until its deadline.
6. **Drive** the test: a second board running a client config, `curl`, an aioesphomeapi script that
presses a template button and subscribes to logs, MQTT publishes, or `interval:` and `on_boot:`
automations in the config itself.
7. **Analyze** with `grep -a -c` counts over the capture and keep an `-events.txt` extract of the lines
that matter. Decode crashes with the platform's `addr2line` (see `reference/boards.md`).
8. **Report** on the PR as a table: build SHA, board and framework, what was driven, counts, result,
plus the `dev` control row and the exact flow in a `<details>` block. No credentials, no port names,
no IP addresses.
## Flashing by chip family
| Board | Flash | Log |
|---|---|---|
| ESP8266 D1 mini (CH340) | `python -m esphome upload <yaml> --device <port>` | `serial_capture.py` at 115200; `esphome logs` on some CH340s emits garbage after the RTS reset. A failed esptool write can wedge the adapter until the board is replugged. `esptool erase_flash` before a test that needs an unprovisioned device: the preference sector survives a reflash |
| ESP32 devkit (CP2102 or CH340) | `esphome upload --device <port>`, ESP-IDF or Arduino | `serial_capture.py`. Default Wi-Fi light sleep delays API driven actions by seconds; judge timing from device logs. `power_save_mode: none` for OTA overlap tests |
| ESP32-S3, C3, C6, H2 native USB | `esphome upload --device /dev/cu.usbmodem*` | `logger: hardware_uart: USB_SERIAL_JTAG`. The USB console can wedge when the app takes over the console (0 bytes, esptool `No serial data received`); replug, or watch through `esphome logs --device <name>.local` |
| Pico W / Pico 2 W | `esphome upload --device <port>` (1200 baud touch); first flash ever via BOOTSEL and `--device /Volumes/RPI-RP2` or `RP2350` | port re-enumerates after upload: `ports.py --wait 2e8a` then capture |
| BW15 kit (RTL8720CF, rtl87xx) | `~/.platformio/penv/.libretiny/bin/python $SKILL/scripts/flash_rtl87xx_kit.py flash write -d <port> <build>/.pioenvs/<name>/*.uf2`. Plain `esphome upload` and plain ltchiptool time out because opening the port asserts DTR/RTS and holds the chip in reset | `serial_capture.py --released`, never `esphome logs` |
| BK7238 / CB3S (bk72xx, RTS on CEN) | `~/.platformio/penv/.libretiny/bin/python $SKILL/scripts/flash_bk72xx.py flash write -d <port> <uf2>` (about 75 s) | logs are on UART2 by default; `logger: hardware_uart: UART1` puts them on the flashing adapter. Opening the port resets the chip, so keep one capture open for the whole test |
| LN882H | ltchiptool forces 1 Mbaud for writes; weak adapters need `LN882H_YM_BAUDRATE` patched to 115200 in the penv's `ln882htool.py`; short BOOT to GND while the tool connects | OTA after the first flash |
| nRF52840 Dongle (PCA10059) | Nordic `nrfutil nrf5sdk-tools dfu usb-serial` on the bootloader port, or `dfu: true` in the config so the 1200 baud touch pin-resets into the bootloader; adafruit-nrfutil is refused | app has two CDC ports, ESPHome log on the second |
| Adafruit-style nRF52 (UF2 bootloader) | `UF2BOOT` volume or adafruit-nrfutil; `dfu: true` works through GPREGRET | single CDC port |
| Any reachable device | `esphome upload <yaml> --device <ip or name.local>`; `--file <bin>` for a prebuilt image | `esphome logs --device <ip>` bounded with `timeout` |
ESP-IDF OTA image: `.esphome/build/<name>/build/firmware.ota.bin`. LibreTiny OTA port is 8892.
## Scripts in `$SKILL/scripts`
- `ports.py`: list ports with VID:PID, product, serial and a board guess; `--wait VID[:PID]`,
`--product NAME`, `--serial SN` (one specific board, whatever path it comes back under) and
`--serial-of PORT` (the serial number behind a path).
- `serial_capture.py PORT SECONDS OUT [--baud] [--released] [--reset] [--append]`: bounded timestamped
capture, ANSI stripped, `LOG_SECRET` text written as `<secret>`, reopens a vanished port, exits at the
deadline. OUT is truncated unless `--append` is given, so counts over it cover one run.
- `flash_rtl87xx_kit.py`: ltchiptool wrapper for the BW15 kit whose CH340 drives CEN (RTS) and the boot
strap (DTR). `flash read` needs the family `realtek-ambz2`; `flash write` does not.
- `flash_bk72xx.py`: ltchiptool wrapper for BK72xx boards whose RTS drives CEN.
- `wifisave_hammer.sh KIND DUT_PORT DUT_YAML DUT_TREE CLIENT_PORT CLIENT_YAML CLIENT_TREE [SETTLE]`:
the fallback AP plus captive portal hammer end to end (compile, flash, capture, counts) with the
templates below. KIND is `esp`, `pico`, `bw15` or `bk`. It ends with `RESULT PASS` (exit 0),
`RESULT FAIL` (exit 3: a crash, a reboot after the AP came up, or the device never joined the network),
`RESULT INVALID`
(exit 2: a precondition was missed, the stimulus was insufficient, or a capture did not last the run)
or exits 1 when a build, flash or port step failed.
## Templates in `$SKILL/templates`
- `dut-fallback-ap.yaml` (package): a device that never finds its network, falls back to its AP and
serves the captive portal. `example-dut.yaml` is the per-run file with the platform block.
- `client-wifisave.yaml` (package): joins that AP, posts N bogus `/wifisave` requests, then the real
credentials from `!secret`. `example-client.yaml` is the per-run file. `bogus_saves: "1"` gives a
plain provisioning check.
## When something looks wrong
- A capture stopped early or a line is missing: check the timestamps before trusting it.
- The client never joins the AP: another board may still run an AP with the same SSID, or the device
booted with saved credentials because the name was reused. One AP device at a time on the bench.
- Builds come out byte-identical between branches: the wrong tree was imported (rule 3).
- Multicast from the host never arrives on some access points: test it device to device. Broadcast
is lossy under Wi-Fi power save, unicast is not. Throttle UDP floods to a fixed packet rate.
- `api:` reboots a board after 15 min without a client: `reboot_timeout: 0s` on `api:` and `mqtt:` for
soak tests.
`reference/boards.md` has per-chip facts and crash decoding; `reference/recipes.md` has the proven
flows. Add new findings there.
@@ -0,0 +1,138 @@
# Board facts
Ports are not listed on purpose: they follow the USB hub port and change on every replug. Run
`scripts/ports.py`.
## ESP8266 (D1 mini, NodeMCU, CH340 or CP2102)
- Opening the port usually resets the board through DTR, but not always; `serial_capture.py --reset`
pulses RTS to capture from boot. 74880 baud shows the ROM boot messages (`rst cause`, `boot mode`).
- `esphome logs` on some CH340s emits thousands of duplicated garbage lines right after the RTS reset;
a plain pyserial capture at 115200 is reliable.
- A failed esptool write can wedge the adapter (`termios` error 22, or an open that hangs) until replug.
- The preference sector survives a reflash: a provisioned API key or saved Wi-Fi credentials stay
active. `esptool erase_flash` gives a clean device.
- `Panic core_esp8266_main.cpp:191 __yield` with `ctx: sys` means a yield from lwIP context (a web
handler, an lwIP callback). Decode stacks with
`xtensa-lx106-elf-addr2line -e <build>/.pioenvs/<name>/firmware.elf -f -C -i <addr...>` from the
toolchain under `~/.platformio/packages/toolchain-xtensa/bin/` (PlatformIO builds) or the ESPHome
cache directory (native toolchain builds).
- Constant data lives in RAM unless marked `PROGMEM`; with 80 KB of RAM a few hundred bytes are worth a
row in the PR.
- AsyncMqttClient drops inbound topics over 128 characters; topics of 255 or more wrap a counter and
stall the link until the broker drops TCP.
## ESP32 classic devkit
- Takes ESP-IDF and Arduino. Default Wi-Fi light sleep delays API driven actions by up to 6 s; judge
timings from device logs, not from the host. `power_save_mode: none` for OTA overlap tests (an IDF web
upload of 739 KB took 107 s with power save and 45 s without).
- Receives only a few percent of IPv6 multicast on some access points with power save on; a raw lwIP
socket loses the same, so that is the AP or driver, not a PR.
- Decode with `xtensa-esp32-elf-addr2line` from the IDF tools directory against
`<build>/.pioenvs/<name>/firmware.elf` (PlatformIO) or `<build>/build/<name>.elf` (IDF native).
- Clean reboot between runs with the capture stopped: `esptool --after hard_reset chip_id`.
- Trigger a forced crash only after the app is marked valid (45 s after boot is plenty), never on an
image still pending OTA rollback.
- Signed OTA testing: build with `esp32.framework.advanced.signed_ota_verification.verification_keys`
under a different name, sign with `python -m espsecure sign_data --version 2 --keyfile
tests/components/esp32/dummy_signing_key.pem`, upload with `esphome upload --file`. The verifier
refuses unsigned images afterwards, so restore with a signed plain build.
## ESP32-S3, C3, C6, H2 with native USB
- `/dev/cu.usbmodem*`, esptool works, `logger: hardware_uart: USB_SERIAL_JTAG`.
- The USB console can die at the moment the app takes over the console during a reboot (log stops after
`Disabling RNG early entropy source`, then 0 bytes, esptool reports `No serial data received`). Only a
USB replug fixes it. Fallback: `esphome logs --device <name>.local` (API log stream, which drops lines
while Wi-Fi is off).
- The ESP32-P4 before silicon revision 3 is bricked by rev3-only firmware (boot loop at bootloader entry).
## RP2040 / RP2350 (Pico W, Pico 2 W)
- The CDC port re-enumerates after every reboot and after `esphome upload` (1200 baud touch). Re-detect
with `ports.py --wait 2e8a` before capturing; `serial_capture.py` reopens on its own if the port
vanishes mid-capture.
- First flash ever: BOOTSEL, then `esphome upload --device /Volumes/RPI-RP2` (or `RP2350`).
- arduino-pico creates the lwIP netif in `WiFi.begin()`, so any IGMP join during component setup fails;
joins must wait for `network::is_connected()`.
- Parallel Pico builds race on package extraction after a PlatformIO package wipe; one at a time.
- An RTS pulse from pyserial does not reset the board; reboot with `App.safe_reboot()` or the touch.
## BW15 kit (RTL8720CF, LibreTiny rtl87xx)
- The kit's CH340 wires RTS to CEN and DTR to PA00 (boot strap). A plain pyserial open asserts both and
holds the chip in reset, which is why `esphome upload`, plain `ltchiptool` and `esphome logs` all fail
("Timeout while linking", or silence). `scripts/flash_rtl87xx_kit.py` sets DTR high and RTS released
on every open, pulses RTS once so the ROM answers `ping`, and after the command drops DTR and pulses
RTS to boot the app. Run it with the PlatformIO LibreTiny penv Python.
- `flash write -d <port> <uf2>` takes no family argument; `flash read -d <port> -s 0 -l <len>
realtek-ambz2 <out>` does.
- Capture with `serial_capture.py --released`. The ESPHome log is on the flashing UART already.
- The macOS CH340 driver only pushes DTR/RTS to the wire once a session has driven them, so the first
plain open after a replug streams logs and the second is silent; always open with the lines released.
- Driver quirks seen on `dev`: `WiFi.begin` can refuse the real network on the first try after bogus
credentials (it connects after two cooldowns); right after connecting while the AP is still up the
driver can bus-fault in `rtw_is_adapter_up` (`R0 = 0xdeadbeef`) and reboot; the AP restarts on every
STA retry, so stations cannot stay joined; `WiFi.begin` fails synchronously for an absent network and
lands in a cooldown at once; connecting blocks the loop 2.5 to 4.1 s.
- Decode faults with `arm-none-eabi-addr2line -e <build>/.pioenvs/<name>/raw_firmware.elf -f -C -i <PC>`
from `~/.platformio/packages/toolchain-gccarmnoneeabi/bin/` (`raw_firmware.elf`, not `firmware.elf`).
Fault dumps print PC, LR and R0 to R3.
- The AmebaZ2 SDK sets `LWIP_TCPIP_CORE_LOCKING 0`.
## BK7238, WB3S, CB3S (BK7231N) (LibreTiny bk72xx, CH340, RTS on CEN)
- Stock ltchiptool closes and reopens the port after its reset, and pyserial raises both lines on open,
holding CEN low through the BootROM window. `scripts/flash_bk72xx.py` opens with both released and
pulses the reset right before the link checks. A write takes about 75 s. Only a CEN or power reset
reaches the downloader; no software reboot does.
- ESPHome logs on UART2 by default; `logger: hardware_uart: UART1` puts them on the flashing adapter.
- Opening the port resets the chip (bootloader banner on every open, even with DTR/RTS low), so keep one
capture open for the whole test and never reopen mid-phase.
- Callbacks run on the `async_tcp` task and log lines can lose a byte. Wi-Fi connect takes tens of ms;
radio enable 160 to 278 ms, disable about 200 ms.
## BW16 kit (RTL8720DN, AmebaD)
- The kit's own USB-C (CH340) is on LOG_UART and flashes fine. Its two-transistor circuit resets while
RTS alone is asserted and straps BURN while DTR alone is; both asserted does nothing. ROM entry is
esptool's classic sequence (`dtr:false,rts:true` 100 ms, `dtr:true,rts:false` 400 ms, both false); the
first pulse after a replug can be lost, retry up to three times.
- Any reopen resets the chip, 460800 is flaky, 115200 is solid: one open per session, in-place baud
changes only. The ROM and the loader NAK continuously while idle, so ACK scans must drain first.
- The AmebaD core has no mDNS responder (`mdns: disabled: true`).
## LN882H
- ltchiptool forces 1 Mbaud for writes; adapters that cannot hold it fail with "Timeout while linking"
or die mid-write. Patch `LN882H_YM_BAUDRATE` in the penv's `ltchiptool/soc/ln882h/util/ln882htool.py`
to 115200 (lost on reprovision). Weak 3.3 V from the adapter: feed the module's 5 V pad instead. Short
BOOT to GND while the tool connects. OTA afterwards.
- `USE_WIFI_AP_EXCLUSIVE` (one radio mode at a time) exists only on LN882x; compile-check changes there.
## nRF52840
- Nordic PCA10059 dongle: the factory Open DFU bootloader enumerates as `1915:521F` on its own port
name; the app as `2FE3:0100` with two CDC ports, ESPHome log on the second. Only Nordic
`nrfutil nrf5sdk-tools` works (`pkg generate --hw-version 52 --sd-req 0x00 --application-version 1
--application zephyr.hex app.zip`, then `dfu usb-serial -pkg app.zip -p <port>`); ESPHome downloads
it on the first build. GPREGRET does not enter the bootloader; a pin reset through P0.19 does, which
`dfu: true` wires up on this board. A pulsing red LED means bootloader; the side RESET button
re-enters it.
- Makerdiary MDK USB dongle: UF2 bootloader without SoftDevice, app at 0x1000, `UF2BOOT` volume,
adafruit-nrfutil, GPREGRET 0x57 works. RGB LED active low (red P0.23, green P0.22, blue P0.24),
button P0.18.
## ESP Thread Border Router board v1.2 (S3 host + H2 RCP)
- S3 host on USB JTAG serial, H2 RCP on its own USB. Spinel UART1 from the host: `rx_pin: GPIO17`,
`tx_pin: GPIO18`, RCP reset `GPIO7` active low, 460800. The config needs `network: enable_ipv6: true`.
- The shipped H2 firmware fails `CheckRadioCapabilities` with IDF 5.5; build `examples/openthread/ot_rcp`
for esp32h2 with ESPHome's own IDF (`IDF_PATH` at the cached framework, its penv and tools,
`IDF_PYTHON_CHECK_CONSTRAINTS=no`) and flash with esptool at 0x0, 0x8000 and 0x10000.
- A crash-looping S3 pulses the H2 reset every boot, so the H2 cannot be flashed and the S3 will not
enter its bootloader over USB. Hold S3 BOOT, tap S3 RST, flash the H2, release, then replug both.
- `dns-sd -L <name> _meshcop._udp local.` shows the `omr` prefix; it disappears when routing is dead.
Keep one capture per chip; never reset the H2 over USB mid-test unless that is the test (the host
logs `Unexpected RCP reset: RESET_OTHER` and recovers).
@@ -0,0 +1,117 @@
# Proven test flows
Each flow names the pull requests it was used on, so their result tables show what to post.
## Captive portal `/wifisave` hammer (#20802, #20784)
Reproduces a `/wifisave` arriving during the STA cooldown, and proves a fix on every board.
Device under test: `templates/example-dut.yaml` (bogus network, `ap:` with `ap_timeout: 15s`,
`captive_portal:`). Client: `templates/example-client.yaml` on an ESP32 or a D1 mini; it joins the
device's AP, posts 15 bogus saves 3 s apart, then the real credentials. With the portal up the cooldown
is 30 s, so nearly every save lands inside one. Driver: `scripts/wifisave_hammer.sh`. Settle 40 s on
ESP boards, 90 s on the BW15 (its first attempt on the real network fails, see boards.md).
Counts that matter: `Requested WiFi Settings Change` (saves received), `Exiting cooldown early`, crash
markers, boots (`Log initialized`), and the client's `bogus save status 200` and `real save status 200`.
A healthy run: every save answered 200, one cooldown exit per save (two on the BW15, whose retry enters
the adapter restart), no panic, and the real credentials connect the device within about 10 s.
Fault injection for AP start failures: wrap `this->ap_setup_ = this->wifi_start_ap_(this->ap_)` in
`setup_ap_config_()` with an `HWTEST_AP_FAIL_COUNT` build flag
(`esphome: platformio_options: build_flags: -DHWTEST_AP_FAIL_COUNT=2`), raise the portal's "Captive
portal started" from LOGV to LOGI, and after provisioning curl the device's STA address: connection
refused proves the portal's server is gone.
## `reboot_timeout` with a fallback AP (#9958)
Only one board runs the AP at a time, or stations join the wrong one. AP probe: bogus network,
`ap: {ssid, password, ap_timeout: 5s}`, `reboot_timeout: 60s`, `logger: level: VERBOSE` (ESP32 and
ESP8266 log `AP client connected MAC=` at verbose), `on_boot: logger.log: BOOT`. Station probes:
`reboot_timeout: 0s`, `interval: 150s` running `wifi.disable`, `delay: 20s`, `wifi.enable` (60 s cycles
are too short to cover the due moment). Pass pattern: "Can't connect; rebooting" exactly 60 s after boot
with no station joined, no reboot for minutes while one is, then a reboot within a second of the last
disconnect. The BW15 restarts its AP on every STA retry and cannot be tested this way.
## MQTT (#20433, #17935, #20762, #20767, #20768)
A local mosquitto with `allow_anonymous true`, `log_type all` and `log_dest file`: the broker log gives
each subscription's QoS, the device address (`New client connected from <ip> as <name>-<mac>`) and exact
subscribe counts. Kill and restart mosquitto to exercise `on_disconnect` (reason 0) and resubscription.
Keep `reboot_timeout: 0s` on `api:` and `mqtt:`. The ESP-IDF outbox swings free heap about 2 kB right
after bulk subscribe and unsubscribe; read the heap seconds later over several rounds before calling a
leak. Spawned subscriptions need about 2.5 s to reach the broker before the next publish.
`esphome logs --device MQTT` only uses the MQTT transport when the config has no `api:`. Driver pattern: a
paho script publishes and polls the serial capture file.
## UDP, multicast, sockets (#20556 to #20559, #20585, #20590)
A listener on port 9 needs no privileges on macOS. Test multicast device to device; host to device
multicast never arrives on some access points, and `239.255.x.x` groups are unreliable even to a stock
listener. Broadcast to boards is lossy under power save, unicast is not. Flood at a fixed packet rate
(the host hits `ENOBUFS` unthrottled); 1500 packets per second survived on every board. A forced Wi-Fi
reconnect on the Pico keeps IGMP membership. Set a long loop interval from `wifi.on_connect`
(`App.set_loop_interval(20000)`), not `on_boot`, or Wi-Fi connect crawls. The `logger.log` action
rejects `%.*s`; use a lambda.
## OTA overlap and lock (#20588), OTA timeouts (#19041), deflate (#19037)
All overlap orders (a web upload during a native OTA and the reverse) must be refused; the lock release
after an interrupted web upload waits about 14 s (ESP8266) or 7 s (ESP-IDF) for disconnect detection. A
refused web client on `web_server_idf` never gets the busy reply; the connection drops at reboot.
`pkill -f "a\|b"` does not alternate; use two pkills.
## DHCP lease cache (#17767)
Cycle 20 s awake, `deep_sleep.enter 30s`, wake, 20 s, `App.safe_reboot()`, logging `millis()` at the
`wifi.connected` condition. An RTS hardware reset must fall back to DHCP; a software reboot and a deep
sleep wake must apply the cache. `esphome run --no-logs` silently skipped the upload on one board; use
`upload` then `logs`.
## HDMI-CEC two-board wire (#9189)
Two boards on one wire: promiscuous mode, a 0x8F request answered with 0x90 0x00, a `destination:`
filter on the reply handler, template buttons for each target plus lambdas calling
`send(source, dest, span)` as another initiator. Healthy numbers: device-side turnaround 15 to 22 ms,
round trip 144 to 156 ms. Press buttons over the API; judge timings from device logs (ESP32 light sleep
lags the press).
## LD2450 polygon zones (#20154)
To make a target "leave" without anyone moving, set the radar's Zone-1 numbers over the target and
switch the zone type to `Filter` (the count drops to 0 within 0.5 s); set it back to `Disabled` after.
Writing those numbers blocks the ld2450 loop about 50 ms.
## Scheduler and loop wake A/B (#20356)
An external component whose template button arms an SDK `os_timer` (50 ms, one shot) on the ESP8266;
the callback runs in SYS context, records `millis()` and calls `set_timeout` or `defer`; the timeout
logs the elapsed time and loop count delta. Drive with an aioesphomeapi script (`subscribe_logs` plus
`button_command`). Build the same yaml twice with different names, one per tree.
## Noise handshake and OTA encryption on the ESP8266 (#19062, #19233)
Host side timing is useless on a weak link; use an on-device bench with the minimum of five runs over
two boots. Provision a key with `noise_encryption_set_key` through aioesphomeapi and upload with
`espota2.run_ota(host, 8266, None, bin, noise_psk=key)` when the key is not in the yaml.
## ESP-NOW on the ESP8266 (#14407)
The NONOS SDK only delivers broadcast ESP-NOW frames while the station is associated or a soft-AP is
up; a hidden, open, client-less soft-AP (60 s beacon, DHCP stopped) makes broadcasts arrive. In AP-only
mode the device transmits from the soft-AP MAC.
## Wi-Fi Easy Connect (DPP) enrollee (#17934)
Only an Android phone configures: Settings, network details, Add device, Share Wi-Fi (the Wi-Fi list's
QR scanner makes the phone an enrollee). Render the URI with the `qrcode` module and open the PNG.
`esp32: framework: log_level: DEBUG` plus `sdkconfig_options: CONFIG_WPA_DEBUG_PRINT: y` prints the
supplicant state machine.
## Thread border router (#18104)
See boards.md for the ESP Thread BR board. Check that the H2 answers Spinel before blaming a PR.
## nRF52840 dongle boards (#19009, #20814)
Test config: the two LEDs as inverted GPIO outputs, the button as an inverted pull-up binary sensor, a
10 s alive interval. To retest the nrfutil download path, move the cached `sdk-nrf/nrfutil` aside first.
@@ -0,0 +1,51 @@
"""ltchiptool for boards whose RTS drives CEN: reset and link on one open port.
Stock ltchiptool closes and reopens the port after its DTR/RTS reset, and
pyserial raises both lines on open, holding CEN low through the BootROM's
listen window. Open with both lines released and pulse the reset right
before the LinkChecks instead.
"""
import sys
import bk7231tools.serial.main as bk_main
from ltchiptool.__main__ import cli
from ltchiptool.soc.bk72xx.flash import BK72XXFlash
from serial import Serial
class ReleasedSerial(Serial):
def __init__(self, port=None, **kwargs):
super().__init__(None, **kwargs)
self.dtr = False
self.rts = False
self.port = port
self.open()
bk_main.Serial = ReleasedSerial
_connect = BK72XXFlash.flash_connect
def flash_hw_reset(self) -> None:
self._reset_pending = True
def flash_connect(self) -> None:
pending = getattr(self, "_reset_pending", False)
if self.bk and self.conn.linked and not pending:
return
self.flash_build_protocol()
if pending:
self._reset_pending = False
self.bk.hw_reset()
self.conn.linked = False # the chip rebooted, so link again below
_connect(self)
BK72XXFlash.flash_hw_reset = flash_hw_reset
BK72XXFlash.flash_connect = flash_connect
sys.argv = ["ltchiptool", *sys.argv[1:]]
cli()
@@ -0,0 +1,87 @@
"""ltchiptool for the BW15 kit whose CH340 drives CEN (RTS) and PA00 (DTR).
A plain open asserts both lines and holds the chip in reset. Here every open keeps DTR high and RTS
released; the first open pulses RTS so the ROM comes up; after the command DTR low plus an RTS pulse
boots the app. The exit status is ltchiptool's; 2 means the flash succeeded but the app was not
booted (boot pulse failed, or no -d PORT to send it to), so the board sits in the ROM until reset.
Usage: python flash_rtl87xx_kit.py flash write -d <port> firmware.uf2
"""
import sys
import time
import serial
_first = [True]
_orig_open = serial.Serial.open
def _open(self):
self.dtr = True # PA00 high: ROM download mode on the next reset
self.rts = False # CEN released
_orig_open(self)
if _first[0]:
_first[0] = False
self.rts = True
time.sleep(0.1)
self.rts = False
time.sleep(0.3) # reset into the ROM
self.reset_input_buffer()
serial.Serial.open = _open
from ltchiptool.__main__ import cli_entrypoint # noqa: E402
def port_argument(argv: list[str]) -> str | None:
for i, a in enumerate(argv):
if a in ("-d", "--device") and i + 1 < len(argv):
return argv[i + 1]
if a.startswith(("-d=", "--device=")):
return a.split("=", 1)[1]
if a.startswith("-d") and len(a) > 2:
return a[2:]
return None
def boot_app(port: str) -> None:
s = serial.Serial(port=None)
s.port = port
s.dtr = False
s.rts = False
_orig_open(s)
s.rts = True
time.sleep(0.1)
s.rts = False
s.close()
def main() -> int:
rc = 1
try:
result = cli_entrypoint.main(args=sys.argv[1:], standalone_mode=False)
rc = int(result) if isinstance(result, int) else 0
finally:
port = port_argument(sys.argv)
if not port:
print(
"[flash_rtl87xx_kit] no -d PORT given, the application was not booted",
file=sys.stderr,
)
if rc == 0:
rc = 2
else:
try:
boot_app(port)
print("[flash_rtl87xx_kit] application booted")
except (serial.SerialException, OSError) as err:
print(f"[flash_rtl87xx_kit] boot pulse failed: {err}", file=sys.stderr)
if rc == 0:
rc = 2 # flashed, but the board is still in the ROM: press reset
return rc
if __name__ == "__main__":
sys.exit(main())
@@ -0,0 +1,123 @@
"""List the boards on USB, or wait for one to enumerate.
ports.py table: port, vid:pid, product, serial number, guess
ports.py --wait 2e8a print the first port with VID 0x2e8a once it appears (default 60 s)
ports.py --wait 1a86:7523 --timeout 30
ports.py --product "USB Serial" first port whose USB product string matches (case-insensitive)
ports.py --wait 2e8a --serial SN the port of one specific board, whatever name it comes back under
ports.py --serial-of PORT print the USB serial number behind a port name
Run with a Python that has pyserial (any ESPHome venv). Port names follow the hub port on macOS, so
re-run this after every replug instead of trusting a name from an earlier session.
"""
import argparse
import sys
import time
from serial.tools import list_ports
GUESS = [
(
(0x1A86, 0x7523),
"CH340: D1 mini, BW15 kit, BK7238/CB3S or LN882H adapter; identify by the boot banner",
),
((0x1A86, 0x55D3), "CH343: ESP32-P4 devkit console"),
((0x1A86, 0x55D4), "CH343: ESP32 devkit"),
((0x1A86, None), "WCH CH34x USB serial bridge"),
((0x10C4, 0xEA60), "CP2102: classic ESP32 devkit (esp32dev)"),
(
(0x303A, 0x1001),
"Espressif USB JTAG serial: S3/C3/C6/H2 native USB (logger hardware_uart USB_SERIAL_JTAG)",
),
(
(0x2E8A, None),
"Raspberry Pi Pico W / Pico 2 W CDC; the port vanishes and comes back on every reboot",
),
((0x1915, 0x521F), "nRF52840 Dongle (PCA10059) in the Open DFU bootloader"),
(
(0x2FE3, 0x0100),
"nRF52840 Dongle running a Zephyr app; two CDC ports, ESPHome log on the second",
),
((0x1915, 0xCAFE), "Makerdiary nRF52840 MDK dongle running a Zephyr app"),
((0x239A, None), "Adafruit style nRF52 board (UF2 bootloader family)"),
]
def guess(vid: int, pid: int) -> str:
for (gv, gp), text in GUESS:
if vid == gv and (gp is None or pid == gp):
return text
return ""
def parse_id(text: str) -> tuple[int, int | None]:
vid, _, pid = text.partition(":")
return int(vid, 16), (int(pid, 16) if pid else None)
def matches(
port, vid: int | None, pid: int | None, product: str | None, serial: str | None
) -> bool:
if vid is not None and (port.vid != vid or (pid is not None and port.pid != pid)):
return False
if product and (port.product or "").lower() != product.lower():
return False
return not serial or (port.serial_number or "") == serial
def main() -> int:
ap = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
ap.add_argument(
"--wait",
metavar="VID[:PID]",
help="block until a matching port appears, print it",
)
ap.add_argument("--product", help="USB product string to match")
ap.add_argument("--serial", help="USB serial number to match")
ap.add_argument(
"--serial-of", metavar="PORT", help="print the USB serial number of PORT"
)
ap.add_argument("--timeout", type=float, default=60)
args = ap.parse_args()
if args.serial_of:
for p in list_ports.comports():
if p.device == args.serial_of and p.serial_number:
print(p.serial_number)
return 0
print(f"no USB serial number for {args.serial_of}", file=sys.stderr)
return 1
vid = pid = None
if args.wait:
vid, pid = parse_id(args.wait)
if args.wait or args.product or args.serial:
deadline = time.time() + args.timeout
while True:
for p in sorted(list_ports.comports(), key=lambda p: p.device):
if p.vid and matches(p, vid, pid, args.product, args.serial):
print(p.device)
return 0
if time.time() >= deadline:
print("no matching port", file=sys.stderr)
return 1
time.sleep(1)
rows = [p for p in list_ports.comports() if p.vid]
if not rows:
print("no USB serial ports")
return 1
for p in sorted(rows, key=lambda p: p.device):
print(
f"{p.device:32} {p.vid:04x}:{p.pid:04x} {p.product or '?':34} "
f"{p.serial_number or '':18} {guess(p.vid, p.pid)}"
)
return 0
if __name__ == "__main__":
sys.exit(main())
@@ -0,0 +1,157 @@
"""Bounded, timestamped serial capture that always exits and closes the port.
serial_capture.py PORT SECONDS OUTFILE [--baud 115200] [--released] [--reset] [--append]
default open with DTR/RTS as pyserial leaves them (asserted); fine for ESP devkits and the Pico.
--released drive DTR and RTS low before opening: the BW15 and BK7238 CH340 kits strap the boot pin
(DTR) and reset (RTS), so a plain open holds the chip in reset.
--reset pulse reset after opening (DTR low, RTS high 100 ms, RTS low) to capture a fresh boot.
--baud 115200 by default; 74880 shows the ESP8266 ROM boot messages.
--append keep what OUTFILE already holds; by default it is truncated so counts cover this run only.
Exit status 1 when nothing was received (wrong port name, or a port another capture still holds).
Text ESPHome marks as secret (LOG_SECRET, the Wi-Fi password) is written as <secret>.
Lines are written as [HH:MM:SS]text with ANSI colour stripped and flushed one by one, so a shell
loop can grep the file while the capture runs. If the port is missing or disappears (USB CDC boards
re-enumerate on reboot) the script retries until the deadline, and follows the board by its USB serial
number when it comes back under another device path.
Never run an open-ended serial session: every capture has a deadline, and a flash on the same port
needs the capture stopped first (pkill -f "[s]erial_capture.py" does not match the killing shell).
"""
import argparse
import contextlib
import pathlib
import re
import sys
import time
import serial
from serial.tools import list_ports
ANSI = re.compile(rb"\x1b\[[0-9;]*[A-Za-z]")
# ESPHome's LOG_SECRET span (a Wi-Fi password); a span cut off before its end marker is secret to the end
SECRET = re.compile(rb"\x1b\[8m.*?(?:\x1b\[28m|$)", re.DOTALL)
PORT_ERRORS = (serial.SerialException, OSError)
def identify(port: str) -> tuple[str | None, int]:
"""USB serial number of the port and its position among that device's ports (a dongle has two)."""
ports = sorted(list_ports.comports(), key=lambda p: p.device)
serial_number = next((p.serial_number for p in ports if p.device == port), None)
siblings = [
p.device for p in ports if serial_number and p.serial_number == serial_number
]
return serial_number, siblings.index(port) if port in siblings else 0
def current_path(port: str, serial_number: str | None, index: int) -> str:
"""The port's current device path: a USB CDC board can come back under a new name after a reboot."""
if serial_number:
ports = sorted(list_ports.comports(), key=lambda p: p.device)
siblings = [p.device for p in ports if p.serial_number == serial_number]
if index < len(siblings):
return siblings[index]
return port
def open_port(args: argparse.Namespace, path: str) -> serial.Serial:
s = serial.Serial(port=None, baudrate=args.baud, timeout=0.2)
s.port = path
if args.released:
s.dtr = False
s.rts = False
s.open()
if args.reset:
try:
s.dtr = False
s.rts = True
time.sleep(0.1)
s.rts = False
s.reset_input_buffer()
except PORT_ERRORS:
s.close()
raise
return s
def write_line(out, tag: bytes, data: bytes) -> None:
stamp = time.strftime("[%H:%M:%S]").encode()
out.write(stamp + tag + ANSI.sub(b"", SECRET.sub(b"<secret>", data)) + b"\n")
out.flush()
def note(out, text: str) -> None:
write_line(out, b"[capture] ", text.encode())
def capture(args: argparse.Namespace, out) -> int:
"""Returns the number of bytes received."""
deadline = time.time() + args.seconds
buf = b""
s = None
received = 0
serial_number, index = None, 0
while time.time() < deadline:
if s is None:
path = current_path(args.port, serial_number, index)
try:
s = open_port(args, path)
args.reset = False # only reset on the first open
if serial_number is None:
serial_number, index = identify(path)
if path != args.port:
note(out, f"reopened as {path}")
except PORT_ERRORS as err:
note(out, f"open failed: {err}")
time.sleep(1)
continue
try:
data = s.read(4096)
except PORT_ERRORS as err:
if buf: # keep the cut-off line apart from the next boot's first line
write_line(out, b"[partial]", buf)
buf = b""
note(out, f"port lost: {err}")
with contextlib.suppress(*PORT_ERRORS):
s.close()
s = None
time.sleep(1)
continue
if not data:
continue
received += len(data)
buf += data
while b"\n" in buf:
line, buf = buf.split(b"\n", 1)
write_line(out, b"", line.rstrip(b"\r"))
if buf:
write_line(out, b"[partial]", buf)
if s is not None:
s.close()
return received
def main() -> int:
ap = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter
)
ap.add_argument("port")
ap.add_argument("seconds", type=float)
ap.add_argument("outfile")
ap.add_argument("--baud", type=int, default=115200)
ap.add_argument("--released", action="store_true")
ap.add_argument("--reset", action="store_true")
ap.add_argument("--append", action="store_true")
args = ap.parse_args()
with pathlib.Path(args.outfile).open("ab" if args.append else "wb") as out:
received = capture(args, out)
if received == 0:
print(f"[capture] nothing received from {args.port}", file=sys.stderr)
return 1
return 0
if __name__ == "__main__":
sys.exit(main())
+133
View File
@@ -0,0 +1,133 @@
#!/bin/bash
# wifisave_hammer.sh: the device under test (DUT) falls back to its AP; a client board joins it and posts
# /wifisave repeatedly, then the real credentials. Compiles, flashes, captures both serial logs and
# prints counts. Configs come from templates/example-dut.yaml and templates/example-client.yaml.
#
# Usage: wifisave_hammer.sh DUT_KIND DUT_PORT DUT_YAML DUT_TREE CLIENT_PORT CLIENT_YAML CLIENT_TREE [SETTLE_S]
# DUT_KIND esp (esphome upload), pico (upload, then re-find the CDC port by USB serial number),
# bw15 (flash_rtl87xx_kit.py) or bk (flash_bk72xx.py)
# *_YAML absolute path; the file name without .yaml must equal the config's `name`
# *_TREE checkout or worktree whose esphome code must build that yaml (cwd for python -m esphome)
# SETTLE_S seconds to keep capturing after the client reports the real save (default 40; BW15 wants 90)
# Env: PY (python with esphome; default DUT_TREE/venv/bin/python, else python3 on PATH; an explicit PY must exist), LTPY
# (LibreTiny penv python with ltchiptool; default ~/.platformio/penv/.libretiny/bin/python)
# Output next to each yaml: NAME-compile.log, NAME-upload.log, NAME-serial.log, NAME-capture.err, and DUT NAME-events.txt
# (addresses redacted so the extract can go on a PR; the serial logs themselves stay local).
# Exit status: 0 RESULT PASS (no crash marker or reboot after the AP came up, the device joined the network,
# and real stimulus: saves answered and a cooldown exit); 3 RESULT FAIL (the counts show
# a failure); 2 RESULT INVALID (a precondition or the stimulus was missed, or a capture did not last the run);
# 1 when a build, flash or port step fails (nothing is reported).
set -u
SKILL=$(cd "$(dirname "$0")/.." && pwd)
KIND=$1; DPORT=$2; DYAML=$3; DTREE=$4; CPORT=$5; CYAML=$6; CTREE=$7; SETTLE=${8:-40}
if [ -n "${PY:-}" ]; then [ -x "$PY" ] || { echo "[hammer] FAILED: PY=$PY is not executable"; exit 1; }
else PY=$DTREE/venv/bin/python; [ -x "$PY" ] || PY=$(command -v python3) || { echo "[hammer] FAILED: no python found, set PY"; exit 1; }; fi
LTPY=${LTPY:-$HOME/.platformio/penv/.libretiny/bin/python}
DDIR=$(dirname "$DYAML"); DNAME=$(basename "$DYAML" .yaml)
CDIR=$(dirname "$CYAML"); CNAME=$(basename "$CYAML" .yaml)
DLOG=$DDIR/$DNAME-serial.log; CLOG=$CDIR/$CNAME-serial.log
INVALID=""
AP_WAIT=120; UPLOAD_WAIT=300; SAVE_WAIT=240
# Crash signatures (extended regex), not bare words: "aborting connection attempt" is a normal Wi-Fi retry line
CRASH_RE='Panic |abort\(\)|Fatal exception|Exception \(|Guru Meditation|HardFault|Bus Fault|Backtrace:|Soft WDT reset|wdt reset|Brownout|rst cause:[24]'
# Boot banners and port losses are counted apart: a Pico reboot shows as a port loss (and maybe a banner too),
# while on a UART board a port loss comes from the adapter or the hub, not the chip
BANNER_RE='Log initialized'; LOSS_RE='\[capture\] port lost'
DUT_CAPTURE=$((AP_WAIT + UPLOAD_WAIT + SAVE_WAIT + SETTLE + 60)); CLIENT_CAPTURE=$((SAVE_WAIT + SETTLE + 60))
bound() { local t=$1; shift; if command -v timeout >/dev/null; then timeout "$t" "$@"; else perl -e 'alarm shift; exec @ARGV' "$t" "$@"; fi; }
count() { LC_ALL=C grep -a -c "$1" "$2"; }
wait_for() { # wait_for FILE PATTERN SECONDS -> prints elapsed, status 1 on timeout
local n=0; until LC_ALL=C grep -a -q "$2" "$1"; do sleep 2; n=$((n+2)); [ $n -ge "$3" ] && { echo "$n"; return 1; }; done; echo "$n"; }
# Only this run's captures: another capture on the host (a soak on a third board) must survive
stop_tails() { pkill -f "[s]erial_capture.py $DPORT " 2>/dev/null; pkill -f "[s]erial_capture.py $CPORT " 2>/dev/null; sleep 1; }
fail() { echo "[hammer] FAILED: $1"; stop_tails; exit 1; }
port_never_opened() { # an open failure and no line that is not a capture note: the port, not the firmware
LC_ALL=C grep -a -q '\[capture\] open failed' "$1" && ! LC_ALL=C grep -a -q -v '^\[[0-9:]*\]\[capture\]' "$1"; }
compile() { # compile YAML TREE
(cd "$2" && PYTHONUNBUFFERED=1 bound 1500 "$PY" -m esphome compile "$1" > "${1%.yaml}-compile.log" 2>&1); local rc=$?
echo "[hammer] compile $(basename "$1"): rc=$rc"; return $rc; }
esp_upload() { # esp_upload YAML PORT TREE
(cd "$3" && bound $UPLOAD_WAIT "$PY" -m esphome upload "$1" --device "$2" > "${1%.yaml}-upload.log" 2>&1)
local ok; ok=$(count 'Successfully uploaded' "${1%.yaml}-upload.log"); echo "[hammer] upload $(basename "$1"): ok=$ok"; [ "$ok" -ge 1 ]; }
stop_tails
compile "$DYAML" "$DTREE" || fail "DUT build failed, see $DDIR/$DNAME-compile.log"
compile "$CYAML" "$CTREE" || fail "client build failed, see $CDIR/$CNAME-compile.log"
TAILOPT=""
case $KIND in
esp) esp_upload "$DYAML" "$DPORT" "$DTREE" || fail "DUT upload failed" ;;
pico)
SN=$("$PY" "$SKILL/scripts/ports.py" --serial-of "$DPORT") || fail "no USB serial number for $DPORT"
esp_upload "$DYAML" "$DPORT" "$DTREE" || fail "DUT upload failed"
# The Pico drops its CDC port on reboot and may already be back: take whatever path this board has now;
# the capture follows the serial number if the path changes again
DPORT=$("$PY" "$SKILL/scripts/ports.py" --wait 2e8a --serial "$SN" --timeout 60) || fail "pico did not re-enumerate"
echo "[hammer] pico port after upload: $DPORT"; sleep 2 ;;
bw15|bk)
UF2=$(ls "$DDIR/.esphome/build/$DNAME/.pioenvs/$DNAME/"*.uf2 2>/dev/null | head -1)
[ -n "$UF2" ] || fail "no uf2 under $DDIR/.esphome/build/$DNAME"
echo "[hammer] uf2: ${UF2##*/}"
W=$SKILL/scripts/flash_rtl87xx_kit.py; [ "$KIND" = bk ] && W=$SKILL/scripts/flash_bk72xx.py
(cd "$DDIR" && bound 400 "$LTPY" "$W" flash write -d "$DPORT" "$UF2" > "$DDIR/$DNAME-upload.log" 2>&1); rc=$?
echo "[hammer] $KIND flash: rc=$rc"; [ $rc -eq 0 ] || fail "DUT flash failed, see $DDIR/$DNAME-upload.log"
TAILOPT="--released" ;;
*) echo "unknown DUT_KIND $KIND"; exit 1 ;;
esac
: > "$DLOG"; ("$PY" "$SKILL/scripts/serial_capture.py" "$DPORT" $DUT_CAPTURE "$DLOG" $TAILOPT 2> "${DLOG%-serial.log}-capture.err" &)
if ! n=$(wait_for "$DLOG" "Captive portal\|Starting fallback AP" $AP_WAIT); then
[ -s "$DLOG" ] || fail "the DUT capture produced nothing (the capture itself failed), see ${DLOG%-serial.log}-capture.err"
port_never_opened "$DLOG" && fail "could not open $DPORT (held by another process, or the wrong port), see $DLOG"
fail "DUT never raised its AP within ${n}s, see $DLOG"
fi
echo "[hammer] DUT fallback AP after ${n}s"; sleep 2
# reboots are counted from here; the first banner may predate the capture
BANNER_BASE=$(count "$BANNER_RE" "$DLOG"); LOSS_BASE=$(count "$LOSS_RE" "$DLOG")
esp_upload "$CYAML" "$CPORT" "$CTREE" || fail "client upload failed"
: > "$CLOG"; ("$PY" "$SKILL/scripts/serial_capture.py" "$CPORT" $CLIENT_CAPTURE "$CLOG" 2> "${CLOG%-serial.log}-capture.err" &)
if n=$(wait_for "$CLOG" "real save status\|posting the real" $SAVE_WAIT); then
echo "[hammer] client posted the real credentials after ${n}s; settling ${SETTLE}s"; sleep "$SETTLE"
else
[ -s "$CLOG" ] || fail "the client capture produced nothing (the capture itself failed), see ${CLOG%-serial.log}-capture.err"
port_never_opened "$CLOG" && fail "could not open $CPORT (held by another process, or the wrong port), see $CLOG"
INVALID="client never reached the real save within ${n}s"; echo "[hammer] WARNING: $INVALID"
fi
# A verdict needs complete evidence: both captures must have lasted the run and logged no error of their own
for spec in "$DPORT:${DLOG%-serial.log}-capture.err" "$CPORT:${CLOG%-serial.log}-capture.err"; do
port=${spec%%:*}; err=${spec#*:}
pgrep -f "[s]erial_capture.py $port " >/dev/null || INVALID="${INVALID:+$INVALID; }the capture on $port ended before the run did, see $err"
[ -s "$err" ] && INVALID="${INVALID:+$INVALID; }the capture on $port reported an error, see $err"
done
CRASHES=$(LC_ALL=C grep -a -c -E "$CRASH_RE" "$DLOG")
BANNERS=$(( $(count "$BANNER_RE" "$DLOG") - BANNER_BASE )); LOSSES=$(( $(count "$LOSS_RE" "$DLOG") - LOSS_BASE ))
if [ "$KIND" = pico ]; then
REBOOTS=$BANNERS; [ "$LOSSES" -gt "$REBOOTS" ] && REBOOTS=$LOSSES # one reboot can show as both, so take the larger
else
REBOOTS=$BANNERS; [ "$LOSSES" -gt 0 ] && INVALID="${INVALID:+$INVALID; }the DUT port dropped $LOSSES time(s) mid-run (adapter or hub, not the chip)"
fi
STA=$(LC_ALL=C grep -a 'IP Address:' "$DLOG" | grep -v -c '192\.168\.4\.'); REAL=$(LC_ALL=C grep -a -o 'real save status [-0-9]*' "$CLOG" | tail -1 | awk '{print $4}')
SAVES=$(count 'Requested WiFi Settings Change' "$DLOG"); EXITS=$(count 'cooldown early' "$DLOG"); ANSWERED=$(count 'bogus save status 200' "$CLOG")
echo "[hammer] $DNAME: saves requested $SAVES, cooldown exits $EXITS, crash markers $CRASHES, after the AP came up: boot banners $BANNERS, port losses $LOSSES, reboots $REBOOTS; STA addresses $STA"
FAILED=$(count 'bogus save status -1' "$CLOG")
echo "[hammer] $CNAME: bogus saves answered $ANSWERED, failed $FAILED, real save status ${REAL:-none}"
# Only whole messages, matched where the message starts, so a credential line or an odd SSID never gets in
LC_ALL=C grep -a -E "^\[[0-9:]+\](\[[A-Z]\]\[[a-z_]+:[0-9]+\]: *(Exiting cooldown early|Starting fallback AP|Captive portal|Requested WiFi Settings Change|Retry phase|Connected\$|IP Address: )|\[capture\] port lost|($CRASH_RE))" "$DLOG" | LC_ALL=C tr -cd '\11\12\15\40-\176' | sed -E 's/(IP Address: )[^ ]+/\1<redacted>/' | cut -c1-110 | head -80 > "$DDIR/$DNAME-events.txt"
echo "[hammer] events: $DDIR/$DNAME-events.txt"
stop_tails
[ -z "$INVALID" ] || { echo "[hammer] RESULT INVALID: $INVALID"; exit 2; }
# Provisioning is proven by the device joining the network; the client's answer to the real save can time
# out because the device leaves the AP before replying, so its status is information only
if [ "$CRASHES" -gt 0 ] || [ "$REBOOTS" -gt 0 ] || [ "$STA" -lt 1 ]; then
echo "[hammer] RESULT FAIL: crash markers $CRASHES, reboots $REBOOTS, STA addresses $STA, real save status ${REAL:-none}"; exit 3
fi
# A pass needs the stimulus the flow is about: at least half the configured bogus saves answered and one cooldown exit
CONFIGURED=$(LC_ALL=C grep -a -E '^[[:space:]]*bogus_saves:' "$CYAML" | head -1 | tr -dc '0-9'); CONFIGURED=${CONFIGURED:-15}
if [ "$EXITS" -lt 1 ] || [ $((ANSWERED * 2)) -lt "$CONFIGURED" ]; then
echo "[hammer] RESULT INVALID: insufficient stimulus (bogus saves answered $ANSWERED of $CONFIGURED, cooldown exits $EXITS)"; exit 2
fi
[ "$FAILED" -eq 0 ] || echo "[hammer] WARNING: $FAILED of $CONFIGURED bogus saves got no answer"
echo "[hammer] RESULT PASS"; exit 0
@@ -0,0 +1,77 @@
# Package for the client board: it joins the device under test's AP (priority 10), posts
# `bogus_saves` bogus /wifisave requests `save_interval` apart, then posts the real credentials once.
# Include from a per-run yaml that holds the platform block and the substitutions (example-client.yaml);
# real_ssid and real_psk come from the scratch directory's secrets.yaml and must be URL safe.
# Set bogus_saves to 1 for a plain provisioning check. Log lines the driver script counts:
# "bogus save N", "bogus save status 200", "posting the real credentials", "real save status 200"
# The hammer is a single-mode script, so a reconnect to the AP (the device under test rebooted) does not
# start a second series on top of the first.
substitutions:
ap_password: hwtestpass
bogus_saves: "15"
save_interval: 3s
bogus_ssid: hwtest-no-such-network
bogus_psk: nosuchpassword
esphome:
name: ${name}
logger:
level: DEBUG
http_request:
id: http
verify_ssl: false
timeout: 10s
wifi:
id: wifi_id
networks:
- ssid: ${ap_ssid}
password: ${ap_password}
priority: 10
- ssid: ${real_ssid}
password: ${real_psk}
on_connect:
- lambda: |-
char b[40];
ESP_LOGI("test", "connected, ip %s", id(wifi_id).get_ip_addresses()[0].str_to(b));
- if:
condition:
lambda: |-
char b[40];
return strncmp(id(wifi_id).get_ip_addresses()[0].str_to(b), "192.168.4.", 10) == 0;
then:
- script.execute: hammer
script:
- id: hammer
mode: single
then:
- delay: 3s
- logger.log: "hammering /wifisave with bogus credentials"
- repeat:
count: ${bogus_saves}
then:
- lambda: ESP_LOGI("test", "bogus save %u", (unsigned) iteration + 1);
- http_request.get:
url: "http://192.168.4.1/wifisave?ssid=${bogus_ssid}&psk=${bogus_psk}"
capture_response: true
on_response:
- lambda: ESP_LOGI("test", "bogus save status %d", response->status_code);
on_error:
- lambda: ESP_LOGW("test", "bogus save status -1");
- delay: ${save_interval}
- logger.log: "posting the real credentials"
- http_request.get:
url: "http://192.168.4.1/wifisave?ssid=${real_ssid}&psk=${real_psk}"
capture_response: true
on_response:
- lambda: ESP_LOGI("test", "real save status %d", response->status_code);
on_error:
- lambda: ESP_LOGW("test", "real save status -1");
interval:
- interval: 5s
then:
- lambda: ESP_LOGI("test", "alive %u s", (unsigned) (millis() / 1000));
@@ -0,0 +1,31 @@
# Package for a device under test that never finds its network, falls back to its AP and serves the
# captive portal. Include it from a per-run yaml that holds the platform block and the substitutions
# (see example-dut.yaml). Bump `name` on every run: saved credentials are keyed by it, and a reused
# name boots straight onto the real network and never raises the AP.
substitutions:
ap_password: hwtestpass
ap_timeout: 15s
bogus_ssid: hwtest-no-such-network
bogus_psk: nosuchpassword
esphome:
name: ${name}
logger:
level: DEBUG
wifi:
networks:
- ssid: ${bogus_ssid}
password: ${bogus_psk}
ap:
ssid: ${ap_ssid}
password: ${ap_password}
ap_timeout: ${ap_timeout}
captive_portal:
interval:
- interval: 5s
then:
- lambda: ESP_LOGI("test", "alive %u s", (unsigned) (millis() / 1000));
@@ -0,0 +1,18 @@
# Client board for the /wifisave hammer. Copy into a scratch directory next to a secrets.yaml;
# ap_ssid must match the device under test, real_ssid and real_psk must be URL safe.
substitutions:
name: hwtest-client-1
ap_ssid: hwtest-ap
real_ssid: !secret wifi_ssid
real_psk: !secret wifi_password
bogus_saves: "15"
esp32:
board: esp32dev
framework:
type: esp-idf
# esp8266: {board: d1_mini}
packages:
hwtest: !include client-wifisave.yaml
@@ -0,0 +1,16 @@
# Device under test for the /wifisave hammer. Copy into a scratch directory, keep the file name equal
# to `name`, pick one platform block, bump `name` every run.
substitutions:
name: hwtest-dut-1
ap_ssid: hwtest-ap
esp8266:
board: d1_mini
# esp32: {board: esp32dev, framework: {type: esp-idf}}
# rtl87xx: {board: bw15}
# bk72xx: {board: generic-bk7238} # plus logger: {hardware_uart: UART1}, BK72xx logs on UART2 by default
# rp2040: {board: rpipico2w}
packages:
hwtest: !include dut-fallback-ap.yaml
+1 -1
View File
@@ -28,7 +28,7 @@ jobs:
- name: Generate a token
id: generate-token
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ vars.ESPHOME_GITHUB_APP_CLIENT_ID }}
private-key: ${{ secrets.ESPHOME_GITHUB_APP_PRIVATE_KEY }}
+1 -1
View File
@@ -1003,7 +1003,7 @@ jobs:
- id: clang-tidy
name: Run script/clang-tidy for ESP32 S3
# yamllint disable-line rule:line-length
options: --environment esp32s3-idf-tidy --grep SOC_TEMP_SENSOR_SUPPORTED --grep USE_ESP32_VARIANT_ESP32S3 --grep USE_LOGGER_USB_CDC --grep SOC_I2S_SUPPORTS_TDM
options: --environment esp32s3-idf-tidy --grep SOC_TEMP_SENSOR_SUPPORTED --grep USE_ESP32_VARIANT_ESP32S3 --grep USE_LOGGER_USB_CDC --grep SOC_I2S_SUPPORTS_TDM --grep USE_OPENTHREAD
tidy_environment: esp32s3-idf-tidy
- id: clang-tidy
name: Run script/clang-tidy for ESP32 P4
@@ -35,7 +35,7 @@ jobs:
- name: Generate a token
id: generate-token
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ vars.ESPHOME_GITHUB_APP_CLIENT_ID }}
private-key: ${{ secrets.ESPHOME_GITHUB_APP_PRIVATE_KEY }}
+1 -1
View File
@@ -15,7 +15,7 @@ jobs:
steps:
- name: Generate a token
id: generate-token
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ vars.ESPHOME_GITHUB_APP_CLIENT_ID }}
private-key: ${{ secrets.ESPHOME_GITHUB_APP_PRIVATE_KEY }}
+1 -1
View File
@@ -245,7 +245,7 @@ jobs:
steps:
- name: Generate a token
id: generate-token
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ vars.ESPHOME_GITHUB_APP_CLIENT_ID }}
private-key: ${{ secrets.ESPHOME_GITHUB_APP_PRIVATE_KEY }}
@@ -43,7 +43,7 @@ jobs:
steps:
- name: Generate a token
id: generate-token
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ vars.ESPHOME_GITHUB_APP_CLIENT_ID }}
private-key: ${{ secrets.ESPHOME_GITHUB_APP_PRIVATE_KEY }}
+1 -1
View File
@@ -19,7 +19,7 @@ jobs:
steps:
- name: Generate a token
id: generate-token
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ vars.ESPHOME_GITHUB_APP_CLIENT_ID }}
private-key: ${{ secrets.ESPHOME_GITHUB_APP_PRIVATE_KEY }}
@@ -20,7 +20,7 @@ jobs:
steps:
- name: Generate a token
id: generate-token
uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
client-id: ${{ vars.ESPHOME_GITHUB_APP_CLIENT_ID }}
private-key: ${{ secrets.ESPHOME_GITHUB_APP_PRIVATE_KEY }}
+4
View File
@@ -40,6 +40,7 @@ esphome/components/analog_threshold/* @ianchi
esphome/components/animation/* @syndlex
esphome/components/anova/* @buxtronix
esphome/components/apc1/* @ademuri
esphome/components/apc_proteous/* @clydebarrow
esphome/components/apds9306/* @aodrenah
esphome/components/apds9930/* @iyesin
esphome/components/api/* @esphome/core
@@ -48,6 +49,7 @@ esphome/components/aranet/* @patrickod
esphome/components/as5600/* @ammmze
esphome/components/as5600/sensor/* @ammmze
esphome/components/as7341/* @mrgnr
esphome/components/as734x/* @latonita @mrgnr
esphome/components/async_tcp/* @esphome/core
esphome/components/at581x/* @X-Ryl669
esphome/components/atc_mithermometer/* @ahpohl
@@ -155,6 +157,7 @@ esphome/components/delonghi/* @grob6000
esphome/components/dew_point/* @CFlix
esphome/components/dfplayer/* @glmnet
esphome/components/dfrobot_sen0395/* @niklasweber
esphome/components/dfrobot_sen0609/* @remcom
esphome/components/dht/* @OttoWinter
esphome/components/display_menu_base/* @numo68
esphome/components/dlms_meter/* @latonita @PolarGoose @SimonFischer04 @Tomer27cz
@@ -508,6 +511,7 @@ esphome/components/rtttl/* @glmnet @ximex
esphome/components/runtime_image/* @clydebarrow @guillempages @kahrendt
esphome/components/runtime_stats/* @bdraco
esphome/components/rx8025t/* @remcom
esphome/components/rx8111/* @techdregs
esphome/components/rx8130/* @beormund
esphome/components/safe_mode/* @jsuanet @kbx81 @paulmonigatti
esphome/components/samsung/* @jorofi
@@ -0,0 +1,3 @@
CODEOWNERS = ["@clydebarrow"]
DEPENDENCIES = ["uart"]
DOMAIN = "apc_proteous"
@@ -0,0 +1,242 @@
#include "apc_proteous_cover.h"
#include "esphome/core/log.h"
#include "esphome/core/application.h"
namespace esphome::apc_proteous {
ESPHOME_LOG_TAG(TAG, "apc_proteous.cover");
static constexpr const char *START_CMD = "*1\r"; // start/stop toggle
static constexpr const char *OPEN_CMD = "*6\r";
static constexpr const char *CLOSE_CMD = "*7\r";
static constexpr const char *QUERY_S = "?s\r";
static constexpr const char *QUERY_X = "?x\r";
// Depending on calibration the reported position may stop a little short of the physical limits
// (e.g. 99% at the fully open reed switch), so readings this close to an end count as that end.
static constexpr uint8_t ENDPOINT_MARGIN = 2;
using namespace esphome::cover;
void APCProteousCover::setup() {
auto restore = this->restore_state_();
if (restore.has_value()) {
restore->apply(this);
} else {
this->position = 0.5f;
}
}
void APCProteousCover::loop() {
uint8_t data;
while (this->available() > 0) {
if (!this->read_byte(&data)) {
continue;
}
if (data == '\r') {
if (this->rx_len_ != 0) {
this->rx_buffer_[this->rx_len_] = '\0';
ESP_LOGVV(TAG, "rx: '%s'", this->rx_buffer_);
this->parse_response_();
this->rx_len_ = 0;
}
} else if (data != '\n') {
if (this->rx_len_ >= MAX_RESPONSE_LEN) {
ESP_LOGVV(TAG, "rx overflow, discarding");
this->rx_len_ = 0;
}
this->rx_buffer_[this->rx_len_++] = static_cast<char>(data);
}
}
}
void APCProteousCover::parse_response_() {
// Expected format: "?s=XX" or "?x=XX" where XX is a hex value
if (this->rx_len_ < 4 || this->rx_buffer_[0] != '?' || this->rx_buffer_[2] != '=') {
ESP_LOGV(TAG, "Invalid response: %s", this->rx_buffer_);
return;
}
const char *hex_str = this->rx_buffer_ + 3;
char *end_ptr;
uint8_t value = static_cast<uint8_t>(strtol(hex_str, &end_ptr, 16));
if (end_ptr == hex_str) {
ESP_LOGW(TAG, "Failed to parse hex value: %s", this->rx_buffer_);
return;
}
bool state_changed = false;
char type = this->rx_buffer_[1];
if (type == 's') {
// bit 0 = operating, bit 1 = direction (0 = opening, 1 = closing)
CoverOperation new_operation = COVER_OPERATION_IDLE;
if (value & 0x01) {
new_operation = (value & 0x02) ? COVER_OPERATION_CLOSING : COVER_OPERATION_OPENING;
}
if (this->current_operation != new_operation) {
this->current_operation = new_operation;
state_changed = true;
}
// Motion in the commanded direction acknowledges the pending command
if ((this->pending_command_ == OPEN_CMD && new_operation == COVER_OPERATION_OPENING) ||
(this->pending_command_ == CLOSE_CMD && new_operation == COVER_OPERATION_CLOSING)) {
this->clear_pending_();
}
// A partial move is over once the acknowledged motion has stopped, wherever it stopped, so an
// old target cannot halt a movement started from the wall button or the remote later on
if (new_operation == COVER_OPERATION_IDLE && this->pending_command_ == nullptr) {
this->target_position_.reset();
}
ESP_LOGV(TAG, "s-status: 0x%02X (operation=%d)", value, new_operation);
} else if (type == 'x') {
// Position percentage, snapped to the ends so is_open/is_closed can latch despite calibration offsets
float new_position;
if (value + ENDPOINT_MARGIN >= 100) {
new_position = COVER_OPEN;
} else if (value <= ENDPOINT_MARGIN) {
new_position = COVER_CLOSED;
} else {
new_position = value / 100.0f;
}
if (this->position != new_position) {
this->position = new_position;
state_changed = true;
// Until the controller has confirmed motion in the commanded direction, current_operation may
// still show the previous one, so a reversing partial move must not be judged against it
if (this->target_position_.has_value() && this->pending_command_ == nullptr &&
((this->current_operation == COVER_OPERATION_OPENING && this->position >= *this->target_position_) ||
(this->current_operation == COVER_OPERATION_CLOSING && this->position <= *this->target_position_))) {
ESP_LOGD(TAG, "Target position %.2f reached", *this->target_position_);
this->stop_cmd_(App.get_loop_component_start_time());
}
}
ESP_LOGV(TAG, "x-status: %d%% (position=%.2f)", value, this->position);
}
if (state_changed) {
this->publish_state();
}
}
void APCProteousCover::clear_pending_() {
this->pending_command_ = nullptr;
this->command_retries_ = 0;
}
void APCProteousCover::retry_pending_command_(uint32_t now) {
if (this->pending_command_ == nullptr || (now - this->pending_command_time_) < COMMAND_ACK_MS) {
return;
}
bool is_open_cmd = this->pending_command_ == OPEN_CMD;
// Nothing to resend once the gate is at the commanded end
if ((is_open_cmd && this->position >= COVER_OPEN) || (!is_open_cmd && this->position <= COVER_CLOSED)) {
this->clear_pending_();
return;
}
const LogString *label = is_open_cmd ? LOG_STR("open") : LOG_STR("close");
if (this->command_retries_ >= MAX_COMMAND_RETRIES) {
ESP_LOGW(TAG, "Gate did not acknowledge %s command after %u retries", LOG_STR_ARG(label), this->command_retries_);
this->clear_pending_();
this->target_position_.reset();
return;
}
this->command_retries_++;
ESP_LOGD(TAG, "Gate did not respond, resending %s command (retry %u)", LOG_STR_ARG(label), this->command_retries_);
this->pending_command_time_ = now;
this->write_command_(this->pending_command_, now);
}
void APCProteousCover::update() {
const uint32_t now = App.get_loop_component_start_time();
this->retry_pending_command_(now);
// Let the command and its echo clear the line before the next query
if ((now - this->last_command_tx_) < COMMAND_QUIET_MS) {
return;
}
// The line is idle, so anything left in the buffer is an unterminated echo
if (this->rx_len_ != 0) {
this->rx_buffer_[this->rx_len_] = '\0';
ESP_LOGVV(TAG, "discarding stale rx '%s' before query", this->rx_buffer_);
this->rx_len_ = 0;
}
const char *query = this->query_s_next_ ? QUERY_S : QUERY_X;
ESP_LOGVV(TAG, "tx: %s", query);
this->write_str(query);
this->query_s_next_ = !this->query_s_next_;
}
void APCProteousCover::dump_config() {
LOG_COVER("", "APC Proteous Cover", this);
LOG_UPDATE_INTERVAL(this);
}
CoverTraits APCProteousCover::get_traits() {
auto traits = CoverTraits();
traits.set_supports_position(true);
traits.set_supports_stop(true);
traits.set_supports_toggle(true);
return traits;
}
void APCProteousCover::write_command_(const char *cmd, uint32_t now) {
ESP_LOGVV(TAG, "tx: %s", cmd);
this->last_command_tx_ = now;
this->write_str(cmd);
}
void APCProteousCover::send_command_(const char *cmd) {
// The published state comes only from status reads; this just records the command so a stop
// can cancel it before the controller reports motion, and so it can be resent if dropped.
// The commands are idempotent, so only skip one when the gate is already moving that way.
CoverOperation operation = cmd == OPEN_CMD ? COVER_OPERATION_OPENING : COVER_OPERATION_CLOSING;
if (this->current_operation == operation) {
return;
}
ESP_LOGD(TAG, "Sending %s command",
operation == COVER_OPERATION_OPENING ? LOG_STR_LITERAL("open") : LOG_STR_LITERAL("close"));
const uint32_t now = App.get_loop_component_start_time();
this->pending_command_ = cmd;
this->pending_command_time_ = now;
this->command_retries_ = 0;
this->write_command_(cmd, now);
}
void APCProteousCover::stop_cmd_(uint32_t now) {
// START_CMD is a start/stop toggle, so it is only sent while the gate is moving, or while a
// movement command is in flight that the controller has not reported yet; otherwise it would start the gate.
if (this->current_operation == COVER_OPERATION_IDLE && this->pending_command_ == nullptr) {
ESP_LOGD(TAG, "Cover already idle, ignoring stop command");
return;
}
ESP_LOGD(TAG, "Sending stop command");
this->write_command_(START_CMD, now);
this->clear_pending_();
this->target_position_.reset();
}
void APCProteousCover::control(const CoverCall &call) {
if (call.get_stop()) {
this->stop_cmd_(App.get_loop_component_start_time());
} else if (call.get_position().has_value()) {
float target = *call.get_position();
ESP_LOGD(TAG, "Target position: %.2f, current: %.2f", target, this->position);
// There is no position command: a partial target is reached by moving towards it and stopping
// once the reported position gets there
this->target_position_.reset();
if (target == COVER_OPEN) {
this->send_command_(OPEN_CMD);
} else if (target == COVER_CLOSED) {
this->send_command_(CLOSE_CMD);
} else if (target != this->position) {
this->target_position_ = target;
this->send_command_(target > this->position ? OPEN_CMD : CLOSE_CMD);
}
} else if (call.get_toggle()) {
this->clear_pending_();
this->target_position_.reset();
this->write_command_(START_CMD, App.get_loop_component_start_time());
}
}
} // namespace esphome::apc_proteous
@@ -0,0 +1,52 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/components/uart/uart.h"
#include "esphome/components/cover/cover.h"
namespace esphome::apc_proteous {
class APCProteousCover : public cover::Cover, public PollingComponent, public uart::UARTDevice {
public:
void setup() override;
void loop() override;
void update() override;
void dump_config() override;
cover::CoverTraits get_traits() override;
protected:
void control(const cover::CoverCall &call) override;
void parse_response_();
void send_command_(const char *cmd);
void write_command_(const char *cmd, uint32_t now);
void stop_cmd_(uint32_t now);
void retry_pending_command_(uint32_t now);
void clear_pending_();
// The longest valid frame is "?s=XX"; anything longer is line noise
static constexpr size_t MAX_RESPONSE_LEN = 16;
// A movement command is acknowledged once the controller reports motion in the commanded
// direction; resend it this often, up to MAX_COMMAND_RETRIES times, since the serial link
// occasionally drops one. The stop toggle is never resent because a spurious toggle could start the gate.
static constexpr uint32_t COMMAND_ACK_MS = 2000;
static constexpr uint8_t MAX_COMMAND_RETRIES = 3;
// The controller echoes every command, sometimes without a terminator. A query sent too soon
// after a command collides with that echo, so polling pauses for this long after each command.
static constexpr uint32_t COMMAND_QUIET_MS = 250;
// Set only for a partial target; the gate is stopped when the reported position reaches it
optional<float> target_position_{};
// Last movement command sent and not yet acknowledged; lets a stop cancel it and drives the retries
const char *pending_command_{nullptr};
uint32_t pending_command_time_{0};
uint32_t last_command_tx_{0};
char rx_buffer_[MAX_RESPONSE_LEN + 1];
uint8_t rx_len_{0};
uint8_t command_retries_{0};
bool query_s_next_{true};
};
} // namespace esphome::apc_proteous
+33
View File
@@ -0,0 +1,33 @@
import esphome.codegen as cg
from esphome.components import cover, uart
import esphome.config_validation as cv
from esphome.types import ConfigType
from . import DOMAIN
apc_proteous_ns = cg.esphome_ns.namespace("apc_proteous")
APCProteousCover = apc_proteous_ns.class_(
"APCProteousCover", cover.Cover, cg.PollingComponent, uart.UARTDevice
)
CONFIG_SCHEMA = (
cover.cover_schema(APCProteousCover)
.extend(uart.UART_DEVICE_SCHEMA)
.extend(cv.polling_component_schema("500ms"))
)
FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema(
DOMAIN,
baud_rate=9600,
require_tx=True,
require_rx=True,
data_bits=8,
parity="NONE",
stop_bits=1,
)
async def to_code(config: ConfigType) -> None:
var = await cover.new_cover(config)
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
+188
View File
@@ -0,0 +1,188 @@
import esphome.codegen as cg
from esphome.components import i2c
import esphome.config_validation as cv
from esphome.const import (
CONF_CALIBRATION,
CONF_GAIN,
CONF_GLASS_ATTENUATION_FACTOR,
CONF_ID,
CONF_TYPE,
CONF_UPDATE_INTERVAL,
)
from esphome.types import ConfigType
CODEOWNERS = ["@latonita", "@mrgnr"]
DEPENDENCIES = ["i2c"]
AUTO_LOAD = ["sensor"]
MULTI_CONF = True
DOMAIN = "as734x"
CONF_AS734X_ID = "as734x_id"
CONF_ATIME = "atime"
CONF_ASTEP = "astep"
CONF_CHANNEL_CORRECTION = "channel_correction"
CONF_DARK_CURRENT = "dark_current"
CONF_NORMALIZE_BASIC_COUNTS = "normalize_basic_counts"
# Band order is a contract with the readout maps in as734x.cpp: the index selects the counts slot.
BANDS_43 = (
"f1",
"f2",
"fz",
"f3",
"f4",
"fy",
"f5",
"fxl",
"f6",
"f7",
"f8",
"nir",
"clear",
)
BANDS_41 = tuple(b for b in BANDS_43 if b not in ("fz", "fy", "fxl"))
as734x_ns = cg.esphome_ns.namespace("as734x")
AS734XComponent = as734x_ns.class_(
"AS734XComponent", cg.PollingComponent, i2c.I2CDevice
)
Gain = as734x_ns.enum("Gain")
GAINS_41 = {
f"X{name}": getattr(Gain, f"GAIN_{name.replace('.', '_')}X")
for name in ("0.5", "1", "2", "4", "8", "16", "32", "64", "128", "256", "512")
}
GAINS_43 = {**GAINS_41, "X1024": Gain.GAIN_1024X, "X2048": Gain.GAIN_2048X}
Normalization = as734x_ns.enum("Normalization", True)
NORMALIZATIONS = {
"NONE": Normalization.NORMALIZATION_NONE,
"ALL": Normalization.NORMALIZATION_ALL,
"BANDS": Normalization.NORMALIZATION_BANDS,
"CLEAR": Normalization.NORMALIZATION_CLEAR,
}
# Per-channel correction for the golden device, measured by the manufacturer.
DEFAULT_CHANNEL_CORRECTION_43 = [
1.055464349,
1.043509797,
1.029576268,
1.0175052,
1.00441899,
0.987356499,
0.957597044,
0.995863485,
1.014628964,
0.996500814,
0.933072749,
1.052236338,
0.999570232,
]
# One integration cycle lasts (ATIME + 1) * (ASTEP + 1) * 2.78 us
CYCLE_STEP_SECONDS = 2.78e-6
def _float_list(length: int) -> cv.All:
"""One float per band, so the list length has to match the chip."""
return cv.All(cv.ensure_list(cv.float_), cv.Length(min=length, max=length))
def _calibration_schema(bands: tuple[str, ...]) -> cv.Schema:
correction = (
DEFAULT_CHANNEL_CORRECTION_43 if bands == BANDS_43 else [1.0] * len(bands)
)
return cv.Schema(
{
cv.Optional(CONF_CHANNEL_CORRECTION, default=correction): _float_list(
len(bands)
),
cv.Optional(CONF_DARK_CURRENT, default=[0.0] * len(bands)): _float_list(
len(bands)
),
cv.Optional(CONF_GLASS_ATTENUATION_FACTOR, default=1.0): cv.positive_float,
}
)
def _validate_exposure(cycles: int):
"""Reject an exposure that is empty or longer than the update interval."""
def validator(config: ConfigType) -> ConfigType:
atime, astep = config[CONF_ATIME], config[CONF_ASTEP]
if atime == 0 and astep == 0:
raise cv.Invalid(f"{CONF_ATIME} and {CONF_ASTEP} must not both be 0")
measurement = (atime + 1) * (astep + 1) * CYCLE_STEP_SECONDS * cycles
if measurement > config[CONF_UPDATE_INTERVAL].total_seconds:
raise cv.Invalid(
f"A measurement takes {measurement:.1f}s with these {CONF_ATIME} and "
f"{CONF_ASTEP} values, longer than the {CONF_UPDATE_INTERVAL}"
)
return config
return validator
def _schema(
gains: dict, address: int, cycles: int, bands: tuple[str, ...]
) -> cv.Schema:
return cv.All(
cv.Schema(
{
cv.GenerateID(): cv.declare_id(AS734XComponent),
cv.Optional(CONF_GAIN, default="X8"): cv.enum(gains),
cv.Optional(CONF_ATIME, default=29): cv.int_range(min=0, max=255),
cv.Optional(CONF_ASTEP, default=599): cv.int_range(min=0, max=65534),
cv.Optional(CONF_CALIBRATION, default={}): _calibration_schema(bands),
cv.Optional(CONF_NORMALIZE_BASIC_COUNTS, default="NONE"): cv.enum(
NORMALIZATIONS, upper=True
),
}
)
.extend(cv.polling_component_schema("60s"))
.extend(i2c.i2c_device_schema(address)),
_validate_exposure(cycles),
)
CONFIG_SCHEMA = cv.typed_schema(
{
# two SMUX steps per update
"AS7341": _schema(GAINS_41, 0x39, cycles=2, bands=BANDS_41),
"AS7343": _schema(GAINS_43, 0x39, cycles=3, bands=BANDS_43),
"TCS3448": _schema(GAINS_43, 0x59, cycles=3, bands=BANDS_43),
},
upper=True,
)
def _per_band(
values: list[float], bands: tuple[str, ...], absent: float
) -> list[float]:
"""Spread per-band calibration over the 13 slots the C++ side indexes by band."""
spread = [absent] * len(BANDS_43)
for value, band in zip(values, bands, strict=True):
spread[BANDS_43.index(band)] = value
return spread
async def to_code(config: ConfigType) -> None:
chip = config[CONF_TYPE]
cg.add_define("USE_AS7341" if chip == "AS7341" else "USE_AS7343")
var = cg.new_Pvariable(config[CONF_ID], cg.RawExpression(f"&as734x::{chip}_CHIP"))
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
cg.add(var.set_gain(config[CONF_GAIN]))
cg.add(var.set_atime(config[CONF_ATIME]))
cg.add(var.set_astep(config[CONF_ASTEP]))
cg.add(var.set_basic_counts_normalization(config[CONF_NORMALIZE_BASIC_COUNTS]))
bands = BANDS_41 if chip == "AS7341" else BANDS_43
calibration = config[CONF_CALIBRATION]
cg.add(
var.set_channel_correction(
_per_band(calibration[CONF_CHANNEL_CORRECTION], bands, 1.0)
)
)
cg.add(var.set_dark_current(_per_band(calibration[CONF_DARK_CURRENT], bands, 0.0)))
cg.add(var.set_glass_attenuation_factor(calibration[CONF_GLASS_ATTENUATION_FACTOR]))
+674
View File
@@ -0,0 +1,674 @@
#include "as734x.h"
#include "color_helpers.h"
#include "esphome/core/application.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#include "esphome/core/progmem.h"
#include <algorithm>
#include <cstdio>
namespace esphome::as734x {
ESPHOME_LOG_TAG(TAG, "as734x");
// Registers and bits shared by both chips
static constexpr uint8_t REG_ENABLE = 0x80;
static constexpr uint8_t REG_ATIME = 0x81;
static constexpr uint8_t REG_ASTATUS = 0x94;
static constexpr uint8_t ENABLE_PON = 0x01;
static constexpr uint8_t ENABLE_SP_EN = 0x02;
static constexpr uint8_t ENABLE_SMUX_EN = 0x10;
static constexpr uint8_t CFG0_REG_BANK = 0x10;
static constexpr uint8_t BANK_SPLIT = 0x80; // registers below this need REG_BANK set
static constexpr uint8_t STATUS2_AVALID = 0x40;
static constexpr uint8_t ASTATUS_ASAT = 0x80;
static constexpr uint8_t LED_ACT = 0x80; // LED driver on, in the chip's LED register
static constexpr uint8_t CONFIG_LED_SEL = 0x08;
static constexpr uint8_t AS7341_CFG6 = 0xAF;
static constexpr uint8_t AS7341_SMUX_CMD_WRITE = 0x10; // copy the SMUX image from RAM to the chain
static constexpr uint8_t AS7341_SMUX_RAM = 0x00;
static constexpr uint8_t AS7343_CHAIN_CMD = 0xE4;
static constexpr uint8_t AS7343_CHAIN_SMUX = 0xE7;
static constexpr uint8_t AS7343_CHAINS = 3;
// Read one field of a ChipInfo kept in flash
template<typename T> static T pgm(const T &field) {
T value;
progmem_memcpy(&value, &field, sizeof(T));
return value;
}
static constexpr uint8_t NO_SLOT = 0xFF;
static constexpr uint8_t CLEAR_BAND = NUM_BANDS - 1;
static constexpr uint8_t MAX_SMUX_LEN = 20;
static constexpr uint8_t MAX_ADC_SLOTS = 18;
static constexpr uint32_t MIN_COLLECTION_TIMEOUT_MS = 30 * 1000;
static constexpr uint32_t MAX_ADC_COUNT = 65535;
PROGMEM_STRING_TABLE(BandNames, "F1", "F2", "FZ", "F3", "F4", "FY", "F5", "FXL", "F6", "F7", "F8", "NIR", "Clear");
#ifdef USE_AS7341
static const char AS7341_NAME[] PROGMEM = "AS7341";
static constexpr uint8_t AS7341_ADC_SLOTS = 6;
static constexpr uint8_t AS7341_SMUX_LEN = 20;
static constexpr uint8_t AS7341_SMUX_STEPS = 2;
static const RegValue AS7341_INIT[] PROGMEM = {{0x70, 0x00}}; // CONFIG: SPM mode, INT_SEL and LED_SEL cleared
static const uint8_t AS7341_SMUX[] PROGMEM = {
0x30, 0x01, 0x00, 0x00, 0x00, 0x42, 0x00, 0x00, 0x50, 0x00, // step 0: F1 to F4, NIR, Clear
0x00, 0x00, 0x20, 0x04, 0x00, 0x30, 0x01, 0x50, 0x00, 0x06, // step 0, continued
0x00, 0x00, 0x00, 0x40, 0x02, 0x00, 0x10, 0x03, 0x50, 0x10, // step 1: F5 to F8, NIR, Clear
0x03, 0x00, 0x00, 0x00, 0x24, 0x00, 0x00, 0x50, 0x00, 0x06, // step 1, continued
};
static_assert(sizeof(AS7341_SMUX) == AS7341_SMUX_STEPS * AS7341_SMUX_LEN && AS7341_SMUX_LEN <= MAX_SMUX_LEN);
static constexpr uint8_t N = NO_SLOT;
static const uint8_t AS7341_READOUT[] PROGMEM = {
0, 1, N, 2, 3, N, N, N, N, N, N, N, N, // step 0
N, N, N, N, N, N, 0, N, 1, 2, 3, 5, 4, // step 1: SMUX routes clear to ADC4 and NIR to ADC5
};
static_assert(sizeof(AS7341_READOUT) == AS7341_SMUX_STEPS * NUM_BANDS && AS7341_ADC_SLOTS <= MAX_ADC_SLOTS);
// This chip has no FZ, FY or FXL band, so those slots are marked absent.
// clang-format off
static const uint16_t AS7341_WAVELENGTHS[] PROGMEM = {
415, 445, ABSENT_NM, 480, 515, ABSENT_NM, 555, ABSENT_NM, 590, 630, 680, 910, WIDEBAND_NM,
};
// clang-format on
static_assert(sizeof(AS7341_WAVELENGTHS) == NUM_BANDS * sizeof(uint16_t));
// Datasheet values for the golden device: gain correction per gain step, the same for every band.
// The last two entries stay at zero because this chip has no 1024x or 2048x gain.
static const float AS7341_GAIN_CORRECTION[] PROGMEM = {1.0577f, 1.0491f, 1.0479f, 1.0491f, 1.0207f, 1.0158f, 1.0109f,
1.0000f, 1.0003f, 0.9873f, 0.9593f, 0.0f, 0.0f};
static_assert(sizeof(AS7341_GAIN_CORRECTION) == GAIN_COUNT * sizeof(float));
// clang-format off
// The three bands this chip does not have contribute nothing to the weighted sums.
static const ChannelContribution AS7341_CONTRIBUTIONS[] PROGMEM = {
{0.005261294f, 5.936230324f, 0.020518273f}, // F1
{0.242615252f, 3.572528495f, 0.013525490f}, // F2
{0.0f, 0.0f, 0.0f}, // FZ, absent
{0.174482552f, 2.914113437f, 0.011585145f}, // F3
{1.479499075f, 2.452851227f, 0.010604986f}, // F4
{0.0f, 0.0f, 0.0f}, // FY, absent
{1.906818555f, 1.940656433f, 0.008890531f}, // F5
{0.0f, 0.0f, 0.0f}, // FXL, absent
{1.162058142f, 1.533976606f, 0.007441544f}, // F6
{0.462374845f, 1.819838250f, 0.009393590f}, // F7
{-0.017794572f, 1.232942274f, 0.006890812f}, // F8
{-0.260730901f, -1.102368000f, -0.004647911f}, // NIR
{-0.022604075f, -0.033304813f, -0.000129106f}, // Clear
};
static const ChannelTristimulus AS7341_TRISTIMULUS[] PROGMEM = {
{0.39814f, 0.01396f, 1.95010f}, // F1
{1.29540f, 0.16748f, 6.45490f}, // F2
{0.0f, 0.0f, 0.0f}, // FZ, absent
{0.36956f, 0.23538f, 2.78010f}, // F3
{0.10902f, 1.42750f, 0.18501f}, // F4
{0.0f, 0.0f, 0.0f}, // FY, absent
{0.71942f, 1.88670f, 0.15325f}, // F5
{0.0f, 0.0f, 0.0f}, // FXL, absent
{1.78180f, 1.14200f, 0.09539f}, // F6
{1.10110f, 0.46497f, 0.10563f}, // F7
{-0.03991f, -0.02702f, 0.08866f}, // F8
{-0.27597f, -0.24468f, -0.61140f}, // NIR
{-0.02347f, -0.01993f, -0.00938f}, // Clear
};
// clang-format on
static_assert(sizeof(AS7341_CONTRIBUTIONS) == NUM_BANDS * sizeof(ChannelContribution));
static_assert(sizeof(AS7341_TRISTIMULUS) == NUM_BANDS * sizeof(ChannelTristimulus));
const ChipInfo AS7341_CHIP PROGMEM = {
.name = AS7341_NAME,
.init = AS7341_INIT,
.smux = AS7341_SMUX,
.readout = AS7341_READOUT,
.wavelengths = AS7341_WAVELENGTHS,
.gain_correction = AS7341_GAIN_CORRECTION,
.contributions = AS7341_CONTRIBUTIONS,
.tristimulus = AS7341_TRISTIMULUS,
.init_count = 1,
.astep_reg = 0xCA,
.cfg0_reg = 0xA9,
.cfg1_reg = 0xAA,
.status2_reg = 0xA3,
.id_reg = 0x92,
.id_mask = 0xFC,
.id_value = 0x09 << 2,
.led_reg = 0x74,
.led_sel_reg = 0x70, // CONFIG, whose LED_SEL bit connects the pin to the driver
.adc_slots = AS7341_ADC_SLOTS,
.smux_steps = AS7341_SMUX_STEPS,
.integration_cycles = 1,
.smux_len = AS7341_SMUX_LEN,
.auto_smux = false,
.gain_correction_per_band = false,
.extra_clear_slot = NO_SLOT,
};
#endif
#ifdef USE_AS7343
static const char AS7343_NAME[] PROGMEM = "AS7343";
static constexpr uint8_t AS7343_ADC_SLOTS = 18;
static constexpr uint8_t AS7343_SMUX_LEN = 10;
static const char TCS3448_NAME[] PROGMEM = "TCS3448";
// CFG6, FD_CFG0, CFG10, CFG8, CFG20 (auto_smux, 18 channels in 3 cycles), AGC_GAIN_MAX, FD_TIME_1, FD_TIME_2
static const RegValue AS7343_INIT[] PROGMEM = {{0xF5, 0x00}, {0xDF, 0xA1}, {0x65, 0xF2}, {0xC9, 0xC8},
{0xD6, 0x62}, {0xD7, 0x99}, {0xE0, 0x64}, {0xE2, 0x21}};
static const uint8_t AS7343_SMUX[] PROGMEM = {0x00, 0x04, 0x65, 0x02, 0x00, 0x05, 0x00, 0x01, 0x00, 0x30,
0x00, 0x00, 0x60, 0x20, 0x04, 0x50, 0x03, 0x00, 0x01, 0x05,
0x05, 0x00, 0x60, 0x00, 0x30, 0x00, 0x40, 0x10, 0x20, 0x00};
// Clear is the average of the cycle 1 (slot 4) and cycle 2 (slot 10) readings; cycle 3 always reads 0.
static const uint8_t AS7343_READOUT[] PROGMEM = {12, 6, 0, 7, 8, 1, 15, 2, 9, 13, 14, 3, 10};
static_assert(sizeof(AS7343_SMUX) == AS7343_CHAINS * AS7343_SMUX_LEN && AS7343_SMUX_LEN <= MAX_SMUX_LEN);
static_assert(sizeof(AS7343_READOUT) == NUM_BANDS && AS7343_ADC_SLOTS <= MAX_ADC_SLOTS);
// clang-format off
static const uint16_t AS7343_WAVELENGTHS[] PROGMEM = {
405, 425, 450, 475, 515, 555, 550, 600, 640, 690, 745, 855, WIDEBAND_NM,
};
// clang-format on
static_assert(sizeof(AS7343_WAVELENGTHS) == NUM_BANDS * sizeof(uint16_t));
// Datasheet values for the golden device: gain correction per gain step, per band.
// clang-format off
static const float AS7343_GAIN_CORRECTION[] PROGMEM = {
1.149f, 1.100f, 1.060f, 1.070f, 1.063f, 1.051f, 1.062f, 1.056f, 1.049f, 1.040f, 1.080f, 1.038f, 1.065f, // 0.5x
1.090f, 1.128f, 1.064f, 1.071f, 1.063f, 1.050f, 1.068f, 1.055f, 1.047f, 1.039f, 1.075f, 1.038f, 1.085f, // 1x
1.083f, 1.086f, 1.062f, 1.070f, 1.062f, 1.049f, 1.057f, 1.053f, 1.045f, 1.038f, 1.063f, 1.037f, 1.069f, // 2x
1.059f, 1.068f, 1.056f, 1.066f, 1.058f, 1.046f, 1.051f, 1.051f, 1.044f, 1.036f, 1.059f, 1.035f, 1.053f, // 4x
1.100f, 1.109f, 1.096f, 1.108f, 1.099f, 1.089f, 1.091f, 1.092f, 1.082f, 1.078f, 1.100f, 1.076f, 1.088f, // 8x
1.099f, 1.109f, 1.096f, 1.108f, 1.099f, 1.089f, 1.091f, 1.092f, 1.082f, 1.078f, 1.100f, 1.075f, 1.087f, // 16x
1.088f, 1.096f, 1.085f, 1.097f, 1.087f, 1.078f, 1.079f, 1.080f, 1.071f, 1.067f, 1.087f, 1.064f, 1.076f, // 32x
1.083f, 1.091f, 1.078f, 1.090f, 1.079f, 1.072f, 1.072f, 1.073f, 1.064f, 1.062f, 1.080f, 1.057f, 1.069f, // 64x
1.076f, 1.084f, 1.072f, 1.085f, 1.074f, 1.066f, 1.062f, 1.067f, 1.055f, 1.056f, 1.074f, 1.051f, 1.061f, // 128x
1.067f, 1.074f, 1.063f, 1.075f, 1.064f, 1.059f, 1.055f, 1.058f, 1.049f, 1.051f, 1.064f, 1.044f, 1.053f, // 256x
1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, 1.000f, // 512x
1.033303857f, 1.034763813f, 1.029855013f, 1.036911249f, 0.988150537f, 1.029003501f, 1.021363258f,
0.98762089f, 1.021713376f, 1.040108204f, 0.987417698f, 1.019481421f, 0.987270057f, // 1024x
0.986859143f, 0.980877221f, 0.9469769f, 0.985535324f, 0.926243961f, 0.959814787f, 1.007963896f,
0.942685187f, 1.007630587f, 1.025460005f, 0.928696275f, 1.001296639f, 1.041275263f, // 2048x
};
// clang-format on
static_assert(sizeof(AS7343_GAIN_CORRECTION) == GAIN_COUNT * NUM_BANDS * sizeof(float));
// clang-format off
static const ChannelContribution AS7343_CONTRIBUTIONS[] PROGMEM = {
{-0.028559089f, 0.450293995f, 0.001266925f}, // F1
{-0.042150948f, 3.566379984f, 0.012515925f}, // F2
{0.000661721f, 2.877424672f, 0.010243005f}, // FZ
{-0.108791278f, 2.273837581f, 0.009376157f}, // F3
{-0.167172180f, 1.519298503f, 0.006017556f}, // F4
{5.726660294f, 4.199345859f, 0.019357905f}, // FY
{0.0f, 0.0f, 0.0f}, // F5
{-0.241492014f, 1.185259160f, 0.006516325f}, // FXL
{-0.047665296f, 1.759469826f, 0.009780714f}, // F6
{-0.012516990f, 1.702006611f, 0.009950228f}, // F7
{-0.026424974f, -1.059196137f, -0.005425656f}, // F8
{-0.007565221f, -0.064827881f, -0.000232267f}, // NIR
{0.0f, 0.0f, 0.0f}, // Clear
};
static const ChannelTristimulus AS7343_TRISTIMULUS[] PROGMEM = {
{-0.07879f, -0.03269f, -0.31295f}, // F1
{-0.12235f, -0.01297f, -0.57885f}, // F2
{1.99879f, -0.04011f, 10.00197f}, // FZ
{-0.33364f, -0.06889f, -0.31281f}, // F3
{-0.06795f, -0.17453f, -0.19657f}, // F4
{-0.45419f, 5.69083f, -0.11077f}, // FY
{0.0f, 0.0f, 0.0f}, // F5
{5.27242f, -0.22956f, -0.06132f}, // FXL
{-0.05072f, -0.04762f, -0.03536f}, // F6
{-0.04666f, -0.01295f, -0.06025f}, // F7
{-0.03931f, -0.02797f, -0.12174f}, // F8
{0.0f, 0.0f, 0.0f}, // NIR
{0.0f, 0.0f, 0.0f}, // Clear
};
// clang-format on
static_assert(sizeof(AS7343_CONTRIBUTIONS) == NUM_BANDS * sizeof(ChannelContribution));
static_assert(sizeof(AS7343_TRISTIMULUS) == NUM_BANDS * sizeof(ChannelTristimulus));
static constexpr ChipInfo as7343_chip(const char *name) {
return {
.name = name,
.init = AS7343_INIT,
.smux = AS7343_SMUX,
.readout = AS7343_READOUT,
.wavelengths = AS7343_WAVELENGTHS,
.gain_correction = AS7343_GAIN_CORRECTION,
.contributions = AS7343_CONTRIBUTIONS,
.tristimulus = AS7343_TRISTIMULUS,
.init_count = sizeof(AS7343_INIT) / sizeof(RegValue),
.astep_reg = 0xD4,
.cfg0_reg = 0xBF,
.cfg1_reg = 0xC6,
.status2_reg = 0x90,
.id_reg = 0x5A,
.id_mask = 0xFF,
.id_value = 0x81,
.led_reg = 0xCD,
.led_sel_reg = 0, // the LED register drives the pin on its own
.adc_slots = AS7343_ADC_SLOTS,
.smux_steps = 1,
.integration_cycles = 3,
.smux_len = AS7343_SMUX_LEN,
.auto_smux = true,
.gain_correction_per_band = true,
.extra_clear_slot = 4,
};
}
const ChipInfo AS7343_CHIP PROGMEM = as7343_chip(AS7343_NAME);
const ChipInfo TCS3448_CHIP PROGMEM = as7343_chip(TCS3448_NAME); // AS7343 register map, address 0x59
#endif
// Registers below 0x80 are only reachable with the REG_BANK bit of CFG0 set; CFG0 itself always answers.
bool AS734XComponent::select_bank_(uint8_t reg) {
const bool low = reg < BANK_SPLIT;
if (low == this->bank_low_) {
return true;
}
this->bank_low_ = low;
return this->write_byte(pgm(this->chip_->cfg0_reg), low ? CFG0_REG_BANK : 0);
}
bool AS734XComponent::update_bits_(uint8_t reg, uint8_t mask, bool value) {
uint8_t data = 0;
if (!this->select_bank_(reg) || !this->read_byte(reg, &data)) {
return false;
}
data = value ? (data | mask) : (data & ~mask);
return this->write_byte(reg, data);
}
// The exposure is applied before every measurement, so a gain, ATIME or ASTEP changed at runtime
// takes effect on the next reading rather than only after a restart.
bool AS734XComponent::write_exposure_() {
const ChipInfo *chip = this->chip_;
if (!this->select_bank_(REG_ATIME) || !this->write_byte(REG_ATIME, this->atime_) ||
!this->write_byte_16(pgm(chip->astep_reg), byteswap(this->astep_)) ||
!this->write_byte(pgm(chip->cfg1_reg), this->gain_)) {
return false;
}
this->reading_gain_ = this->gain_;
this->reading_atime_ = this->atime_;
this->reading_astep_ = this->astep_;
// One integration cycle lasts (ATIME + 1) * (ASTEP + 1) * 2.78 us.
const uint32_t cycle_ms = (this->atime_ + 1UL) * (this->astep_ + 1UL) * 139 / 50000;
this->integration_ms_ = cycle_ms * pgm(chip->integration_cycles);
this->timeout_ms_ = std::max(MIN_COLLECTION_TIMEOUT_MS, 2 * this->integration_ms_);
return true;
}
void AS734XComponent::setup() {
const ChipInfo *chip = this->chip_;
const uint8_t id_reg = pgm(chip->id_reg);
uint8_t id = 0;
// The sensor keeps its bank across an MCU reset, so the first access always writes CFG0
bool ok = this->select_bank_(REG_ENABLE) && this->write_byte(REG_ENABLE, ENABLE_PON);
delayMicroseconds(200); // oscillator start-up
ok = ok && this->select_bank_(id_reg) && this->read_byte(id_reg, &id);
if (ok && (id & pgm(chip->id_mask)) != pgm(chip->id_value)) {
this->mark_failed(LOG_STR("Wrong chip ID"));
return;
}
const RegValue *init = pgm(chip->init);
for (uint8_t i = 0, n = pgm(chip->init_count); ok && i < n; i++) {
RegValue rv;
progmem_memcpy(&rv, &init[i], sizeof(rv));
ok = this->select_bank_(rv.reg) && this->write_byte(rv.reg, rv.value);
}
// Everything from here on, including the measurement registers, is in the high bank.
ok = ok && this->write_exposure_();
if (pgm(chip->auto_smux)) {
const uint8_t *smux = pgm(chip->smux);
const uint8_t len = pgm(chip->smux_len);
for (uint8_t i = 0; ok && i < AS7343_CHAINS * len; i++) {
ok = this->write_byte(AS7343_CHAIN_SMUX, progmem_read_byte(&smux[i])) &&
this->write_byte(AS7343_CHAIN_CMD, 0x46 + (i / len) * 0x10);
}
}
if (!ok) {
this->mark_failed(LOG_STR(ESP_LOG_MSG_COMM_FAIL));
return;
}
this->disable_loop();
}
void AS734XComponent::dump_config() {
const float gain = this->gain_ == GAIN_0_5X ? 0.5f : static_cast<float>(1 << (this->gain_ - 1));
ESP_LOGCONFIG(TAG,
"AS734x:\n"
" Chip: %s\n"
" Gain: %gx\n"
" ATIME: %u\n"
" ASTEP: %u\n"
" Glass attenuation factor: %.3f",
LOG_STR_ARG(pgm(this->chip_->name)), gain, this->atime_, this->astep_, this->glass_attenuation_factor_);
LOG_I2C_DEVICE(this);
LOG_UPDATE_INTERVAL(this);
for (uint8_t band = 0; band < NUM_BANDS; band++) {
char label[24];
const char *name = LOG_STR_ARG(BandNames::get_log_str(band, BandNames::LAST_INDEX));
snprintf(label, sizeof(label), "Counts %s", name);
sensor::log_sensor(TAG, " ", label, this->sensors_[band]);
snprintf(label, sizeof(label), "Basic counts %s", name);
sensor::log_sensor(TAG, " ", label, this->basic_counts_sensors_[band]);
}
LOG_SENSOR(" ", "Saturation level", this->saturation_level_sensor_);
LOG_SENSOR(" ", "Illuminance", this->illuminance_sensor_);
LOG_SENSOR(" ", "Irradiance photopic", this->irradiance_photopic_sensor_);
LOG_SENSOR(" ", "Irradiance PAR", this->irradiance_par_sensor_);
LOG_SENSOR(" ", "PPFD", this->ppfd_sensor_);
LOG_SENSOR(" ", "Color temperature", this->color_temperature_sensor_);
#ifdef USE_AS734X_RGB
LOG_TEXT_SENSOR(" ", "RGB hex", this->rgb_hex_sensor_);
#endif
if (this->is_failed()) {
ESP_LOGE(TAG, ESP_LOG_MSG_COMM_FAIL);
}
}
void AS734XComponent::enable_led(bool enable) {
const uint8_t led_reg = pgm(this->chip_->led_reg);
if (led_reg == 0) {
return;
}
// The LED register only drives the LED while LED_SEL is set, so the pin is handed over before
// switching on and released only after switching off. The other order would leave the LED lit
// with nothing able to turn it off.
const uint8_t led_sel_reg = pgm(this->chip_->led_sel_reg);
bool ok = true;
if (enable && led_sel_reg != 0) {
ok = this->update_bits_(led_sel_reg, CONFIG_LED_SEL, true);
}
ok = this->update_bits_(led_reg, LED_ACT, enable) && ok;
if (!enable && led_sel_reg != 0) {
ok = this->update_bits_(led_sel_reg, CONFIG_LED_SEL, false) && ok;
}
// The measurement path assumes the high bank, so leave the chip there for a reading in flight
ok = this->select_bank_(REG_ENABLE) && ok;
if (!ok) {
ESP_LOGW(TAG, "Failed to switch the LED");
}
}
void AS734XComponent::update() {
if (this->state_ != State::STATE_IDLE) {
ESP_LOGW(TAG, "Skipping update, previous measurement still running");
return;
}
if (!this->write_exposure_()) {
this->abort_measurement_(LOG_STR("Failed to apply measurement settings"));
return;
}
this->step_ = 0;
this->peak_raw_count_ = 0;
this->raw_counts_.fill(0);
this->enable_loop();
if (!this->start_step_()) {
this->abort_measurement_(LOG_STR("Failed to start measurement"));
}
}
bool AS734XComponent::start_step_() {
this->start_ms_ = App.get_loop_component_start_time();
if (pgm(this->chip_->auto_smux)) {
this->state_ = State::STATE_READ_DATA;
return this->write_byte(REG_ENABLE, ENABLE_PON | ENABLE_SP_EN);
}
// Load this step's image into SMUX RAM; SMUX_EN copies it to the chain and clears itself when done.
std::array<uint8_t, MAX_SMUX_LEN> image{};
const uint8_t len = pgm(this->chip_->smux_len);
progmem_memcpy(image.data(), pgm(this->chip_->smux) + this->step_ * len, len);
this->state_ = State::STATE_WAIT_SMUX;
return this->write_byte(AS7341_CFG6, AS7341_SMUX_CMD_WRITE) &&
this->write_bytes(AS7341_SMUX_RAM, image.data(), len) &&
this->write_byte(REG_ENABLE, ENABLE_PON | ENABLE_SMUX_EN);
}
void AS734XComponent::loop() {
const uint32_t now = App.get_loop_component_start_time();
uint8_t value = 0;
switch (this->state_) {
case State::STATE_WAIT_SMUX:
// A failed read counts as busy, so the timeout below catches a chip that stops answering.
if (this->read_byte(REG_ENABLE, &value) && !(value & ENABLE_SMUX_EN)) {
this->start_ms_ = now;
this->state_ = State::STATE_READ_DATA;
if (!this->write_byte(REG_ENABLE, ENABLE_PON | ENABLE_SP_EN)) {
this->abort_measurement_(LOG_STR("Failed to start integration"));
}
return;
}
break;
case State::STATE_READ_DATA:
if (now - this->start_ms_ < this->integration_ms_) {
return;
}
if (this->read_byte(pgm(this->chip_->status2_reg), &value) && (value & STATUS2_AVALID)) {
this->read_step_();
return;
}
break;
case State::STATE_IDLE:
this->disable_loop();
return;
}
if (now - this->start_ms_ > this->timeout_ms_) {
this->abort_measurement_(LOG_STR("Measurement timeout"));
}
}
void AS734XComponent::read_step_() {
// Reading ASTATUS latches the data, which is only consistent within one burst starting there.
std::array<uint8_t, 1 + 2 * MAX_ADC_SLOTS> frame{};
if (!this->read_bytes(REG_ASTATUS, frame.data(), 1 + 2 * pgm(this->chip_->adc_slots)) ||
!this->write_byte(REG_ENABLE, ENABLE_PON)) {
this->abort_measurement_(LOG_STR("Failed to read channel data"));
return;
}
if (frame[0] & ASTATUS_ASAT) {
ESP_LOGV(TAG, "Step %u saturated", this->step_);
}
auto slot_value = [&frame](uint8_t slot) { return encode_uint16(frame[2 + 2 * slot], frame[1 + 2 * slot]); };
const uint8_t *readout = pgm(this->chip_->readout) + this->step_ * NUM_BANDS;
const uint8_t extra_clear = pgm(this->chip_->extra_clear_slot);
for (uint8_t band = 0; band < NUM_BANDS; band++) {
const uint8_t slot = progmem_read_byte(&readout[band]);
if (slot == NO_SLOT) {
continue;
}
const uint16_t raw = slot_value(slot);
this->peak_raw_count_ = std::max(this->peak_raw_count_, raw);
uint16_t counts = raw;
if (band == CLEAR_BAND && extra_clear != NO_SLOT) {
const uint16_t extra = slot_value(extra_clear);
this->peak_raw_count_ = std::max(this->peak_raw_count_, extra);
counts = (static_cast<uint32_t>(raw) + extra) / 2;
}
this->raw_counts_[band] = counts;
}
if (++this->step_ < pgm(this->chip_->smux_steps)) {
if (!this->start_step_()) {
this->abort_measurement_(LOG_STR("Failed to configure SMUX"));
}
return;
}
this->publish_measurement_();
this->status_clear_warning();
this->state_ = State::STATE_IDLE;
this->disable_loop();
}
// A basic count is the raw count scaled to one unit of gain and one millisecond of integration
// time, which makes readings taken with different settings comparable. The manufacturer's
// conversion factors are calibrated against that definition.
void AS734XComponent::calculate_basic_counts_() {
this->basic_counts_.fill(0.0f);
this->max_basic_count_ = 0.0f;
this->max_band_basic_count_ = 0.0f;
this->clear_basic_count_ = 0.0f;
// The gain enum counts in powers of two starting at 0.5x, so the multiplier is 2^gain / 2.
const float gain_x = static_cast<float>(1 << this->reading_gain_) / 2.0f;
const float integration_ms = (1.0f + this->reading_atime_) * (1.0f + this->reading_astep_) * 2.78e-3f;
if (gain_x <= 0.0f || integration_ms <= 0.0f) {
return;
}
const float inv_exposure = 1.0f / (gain_x * integration_ms);
const ChipInfo *chip = this->chip_;
const uint16_t *wavelengths = pgm(chip->wavelengths);
const float *gain_correction = pgm(chip->gain_correction);
const bool per_band = pgm(chip->gain_correction_per_band);
const uint8_t correction_base = per_band ? this->reading_gain_ * NUM_BANDS : this->reading_gain_;
for (uint8_t band = 0; band < NUM_BANDS; band++) {
const uint16_t wavelength = pgm(wavelengths[band]);
if (wavelength == ABSENT_NM) {
continue;
}
float basic_count = this->raw_counts_[band] * inv_exposure;
basic_count -= this->dark_current_[band];
basic_count = std::max(basic_count, 0.0f);
basic_count *= pgm(gain_correction[per_band ? correction_base + band : correction_base]);
basic_count *= this->channel_correction_[band];
basic_count *= this->glass_attenuation_factor_;
this->basic_counts_[band] = basic_count;
this->max_basic_count_ = std::max(this->max_basic_count_, basic_count);
if (wavelength == WIDEBAND_NM) {
this->clear_basic_count_ = basic_count;
} else if (wavelength >= VISIBLE_MIN_NM && wavelength <= VISIBLE_MAX_NM) {
this->max_band_basic_count_ = std::max(this->max_band_basic_count_, basic_count);
}
}
}
float AS734XComponent::normalization_divisor_() const {
float divisor = 1.0f;
switch (this->normalization_) {
case Normalization::NORMALIZATION_ALL:
divisor = this->max_basic_count_;
break;
case Normalization::NORMALIZATION_BANDS:
divisor = this->max_band_basic_count_;
break;
case Normalization::NORMALIZATION_CLEAR:
divisor = this->clear_basic_count_;
break;
case Normalization::NORMALIZATION_NONE:
return 1.0f;
}
// In the dark the reference can be zero, and dividing by that would publish infinities.
return divisor > 0.0f ? divisor : 1.0f;
}
void AS734XComponent::publish_basic_counts_() {
// Normalization describes the shape of the spectrum rather than its absolute level. Channels
// outside the chosen reference can exceed 1.0 and are left that way rather than clipped: with
// BANDS the near infrared channel legitimately sits well above the visible bands.
const float scale = 1.0f / this->normalization_divisor_();
for (uint8_t band = 0; band < NUM_BANDS; band++) {
if (this->basic_counts_sensors_[band] != nullptr) {
this->basic_counts_sensors_[band]->publish_state(this->basic_counts_[band] * scale);
}
}
}
// Each band contributes a fixed amount per basic count to every integrated quantity, so the
// totals are a weighted sum over the bands.
// The weighted sums can go negative under light the tables were not fitted for, such as pure blue or
// far red, so the published values are floored at zero.
void AS734XComponent::publish_light_metrics_() {
float irradiance_photopic = 0.0f;
float irradiance_par = 0.0f;
float ppfd = 0.0f;
const ChannelContribution *contributions = pgm(this->chip_->contributions);
for (uint8_t band = 0; band < NUM_BANDS; band++) {
const float basic_count = this->basic_counts_[band];
const ChannelContribution contribution = pgm(contributions[band]);
irradiance_photopic += contribution.irradiance_photopic * basic_count;
irradiance_par += contribution.irradiance_par * basic_count;
ppfd += contribution.ppfd * basic_count;
}
irradiance_photopic = std::max(irradiance_photopic, 0.0f);
irradiance_par = std::max(irradiance_par, 0.0f);
ppfd = std::max(ppfd, 0.0f) * 1e3f;
const float illuminance = irradiance_photopic * LUMENS_PER_WATT;
ESP_LOGV(TAG, "Photopic irradiance %.4f, PAR %.4f, PPFD %.4f, illuminance %.2f lx", irradiance_photopic,
irradiance_par, ppfd, illuminance);
if (this->illuminance_sensor_ != nullptr) {
this->illuminance_sensor_->publish_state(illuminance);
}
if (this->irradiance_photopic_sensor_ != nullptr) {
this->irradiance_photopic_sensor_->publish_state(irradiance_photopic);
}
if (this->irradiance_par_sensor_ != nullptr) {
this->irradiance_par_sensor_->publish_state(irradiance_par);
}
if (this->ppfd_sensor_ != nullptr) {
this->ppfd_sensor_->publish_state(ppfd);
}
}
void AS734XComponent::publish_color_() {
float tri_x = 0.0f;
float tri_y = 0.0f;
float tri_z = 0.0f;
const ChannelTristimulus *table = pgm(this->chip_->tristimulus);
for (uint8_t band = 0; band < NUM_BANDS; band++) {
const float basic_count = this->basic_counts_[band];
const ChannelTristimulus tristimulus = pgm(table[band]);
tri_x += tristimulus.x * basic_count;
tri_y += tristimulus.y * basic_count;
tri_z += tristimulus.z * basic_count;
}
const float color_temperature = tristimulus_to_cct(tri_x, tri_y, tri_z);
ESP_LOGV(TAG, "XYZ %.4f, %.4f, %.4f -> CCT %.0f K", tri_x, tri_y, tri_z, color_temperature);
if (this->color_temperature_sensor_ != nullptr) {
this->color_temperature_sensor_->publish_state(color_temperature);
}
#ifdef USE_AS734X_RGB
if (this->rgb_hex_sensor_ == nullptr) {
return;
}
// Convert to chromaticity first, so the colour describes the hue rather than the brightness.
// Absolute tristimulus values clamp to white under anything but dim light.
char rgb_hex[7] = "000000"; // no usable chromaticity reads as black rather than an empty string
const float sum = tri_x + tri_y + tri_z;
if (sum > MIN_TRISTIMULUS_SUM) {
uint8_t rgb[3];
tristimulus_to_rgb(tri_x / sum, tri_y / sum, tri_z / sum, rgb[0], rgb[1], rgb[2]);
format_hex_to(rgb_hex, rgb, sizeof(rgb));
}
this->rgb_hex_sensor_->publish_state(rgb_hex);
#endif
}
void AS734XComponent::publish_measurement_() {
this->calculate_basic_counts_();
for (uint8_t band = 0; band < NUM_BANDS; band++) {
if (this->sensors_[band] != nullptr) {
this->sensors_[band]->publish_state(this->raw_counts_[band]);
}
}
this->publish_basic_counts_();
this->publish_light_metrics_();
this->publish_color_();
if (this->saturation_level_sensor_ != nullptr) {
const uint32_t max_adc =
std::min<uint32_t>((this->reading_atime_ + 1UL) * (this->reading_astep_ + 1UL), MAX_ADC_COUNT);
const float level = max_adc == 0 ? 0.0f : 100.0f * this->peak_raw_count_ / max_adc;
ESP_LOGV(TAG, "Highest ADC count %u of %u (%.1f%%)", this->peak_raw_count_, static_cast<unsigned>(max_adc), level);
this->saturation_level_sensor_->publish_state(level);
}
}
void AS734XComponent::abort_measurement_(const LogString *reason) {
ESP_LOGW(TAG, "%s", LOG_STR_ARG(reason));
this->write_byte(REG_ENABLE, ENABLE_PON);
this->status_set_warning(reason);
this->state_ = State::STATE_IDLE;
this->disable_loop();
}
} // namespace esphome::as734x
+199
View File
@@ -0,0 +1,199 @@
#pragma once
#include <array>
#include <cstdint>
#include "esphome/core/component.h"
#include "esphome/core/hal.h"
#include "esphome/components/i2c/i2c.h"
#include "esphome/components/sensor/sensor.h"
#ifdef USE_AS734X_RGB
#include "esphome/components/text_sensor/text_sensor.h"
#endif
namespace esphome::as734x {
enum Gain : uint8_t {
GAIN_0_5X,
GAIN_1X,
GAIN_2X,
GAIN_4X,
GAIN_8X,
GAIN_16X,
GAIN_32X,
GAIN_64X,
GAIN_128X,
GAIN_256X,
GAIN_512X,
GAIN_1024X, // AS7343 only
GAIN_2048X, // AS7343 only
};
static constexpr uint8_t GAIN_COUNT = 13; // entries in Gain, for sizing the correction tables
static constexpr uint8_t NUM_BANDS = 13; // band order of BANDS_43 in __init__.py
// What the published basic counts are divided by. The clear channel is wideband, so it is roughly
// the size of all the bands together, and the near infrared channel outgrows them under daylight.
// Dividing by the largest of every channel therefore leaves the visible bands squeezed into the
// bottom of the range.
enum class Normalization : uint8_t {
NORMALIZATION_NONE, // publish absolute basic counts
NORMALIZATION_ALL, // largest of every channel
NORMALIZATION_BANDS, // largest of the visible bands only
NORMALIZATION_CLEAR, // the wideband clear channel
};
// Nominal centre wavelengths in nanometres bound the visible bands. The clear channel is wideband
// and has no single centre wavelength, so it is marked with zero instead.
static constexpr uint16_t VISIBLE_MIN_NM = 380;
static constexpr uint16_t VISIBLE_MAX_NM = 700;
static constexpr uint16_t WIDEBAND_NM = 0;
static constexpr uint16_t ABSENT_NM = 0xFFFF; // band this chip does not have
// Luminous efficacy at 555 nm, for turning photopic irradiance into lux.
static constexpr float LUMENS_PER_WATT = 683.002f;
// How much one basic count of a band contributes to each integrated quantity. The factors come
// from the manufacturer's golden-device calibration, convolved for a reconstructed spectrum.
struct ChannelContribution {
float irradiance_photopic; // weighted by the CIE 1931 V(lambda) curve
float irradiance_par; // 400-700 nm only
float ppfd; // 400-700 nm only, scaled by 1e3 before publishing
};
// CIE 1931 tristimulus contribution of one basic count of a band.
struct ChannelTristimulus {
float x;
float y;
float z;
};
struct RegValue {
uint8_t reg;
uint8_t value;
};
/// Per-chip constants, stored in flash. The pointed-to tables are in flash too.
struct ChipInfo {
const char *name;
const RegValue *init; // written in order; the bank is selected per register address
const uint8_t *smux; // AS7341: SMUX RAM image per step; AS7343: the three chain images
const uint8_t *readout; // per step, the ADC slot of each band (0xFF: not read in this step)
const uint16_t *wavelengths; // per band, WIDEBAND_NM for clear and ABSENT_NM for a missing band
const float *gain_correction; // GAIN_COUNT entries, or GAIN_COUNT * NUM_BANDS when per band
const ChannelContribution *contributions; // per band
const ChannelTristimulus *tristimulus; // per band
uint8_t init_count;
uint8_t astep_reg;
uint8_t cfg0_reg;
uint8_t cfg1_reg;
uint8_t status2_reg;
uint8_t id_reg;
uint8_t id_mask;
uint8_t id_value;
uint8_t led_reg; // LED driver register (0: this chip has no LED control)
uint8_t led_sel_reg; // register whose LED_SEL bit hands the pin to the driver (0: not needed)
uint8_t adc_slots; // 16-bit data registers read after ASTATUS
uint8_t smux_steps; // measurements per update
uint8_t integration_cycles;
uint8_t smux_len; // bytes per SMUX image
bool auto_smux; // the chip cycles its SMUX itself after a one-time chain setup
bool gain_correction_per_band; // the correction table is indexed by gain and band, not gain alone
uint8_t extra_clear_slot; // averaged into the clear band (0xFF: none)
};
#ifdef USE_AS7341
extern const ChipInfo AS7341_CHIP;
#endif
#ifdef USE_AS7343
extern const ChipInfo AS7343_CHIP;
extern const ChipInfo TCS3448_CHIP;
#endif
class AS734XComponent : public PollingComponent, public i2c::I2CDevice {
public:
// Channel correction is multiplicative, so it starts at unity rather than zero in case no
// calibration is configured.
explicit AS734XComponent(const ChipInfo *chip) : chip_(chip) { this->channel_correction_.fill(1.0f); }
void setup() override;
void dump_config() override;
void update() override;
void loop() override;
void set_gain(Gain gain) { this->gain_ = gain; }
void set_atime(uint8_t atime) { this->atime_ = atime; }
void set_astep(uint16_t astep) { this->astep_ = astep; }
/// Turns the sensor's LED driver on or off. Meant for lambdas.
void enable_led(bool enable);
// Calibration applied while turning raw counts into basic counts.
void set_dark_current(const std::array<float, NUM_BANDS> &values) { this->dark_current_ = values; }
void set_channel_correction(const std::array<float, NUM_BANDS> &values) { this->channel_correction_ = values; }
void set_glass_attenuation_factor(float factor) { this->glass_attenuation_factor_ = factor; }
/// Chooses what the published basic counts are divided by.
void set_basic_counts_normalization(Normalization normalization) { this->normalization_ = normalization; }
void set_counts_sensor(sensor::Sensor *sensor, uint8_t band) { this->sensors_[band] = sensor; }
void set_basic_counts_sensor(sensor::Sensor *sensor, uint8_t band) { this->basic_counts_sensors_[band] = sensor; }
SUB_SENSOR(saturation_level)
SUB_SENSOR(illuminance)
SUB_SENSOR(irradiance_photopic)
SUB_SENSOR(irradiance_par)
SUB_SENSOR(ppfd)
SUB_SENSOR(color_temperature)
#ifdef USE_AS734X_RGB
void set_rgb_hex_sensor(text_sensor::TextSensor *sensor) { this->rgb_hex_sensor_ = sensor; }
#endif
protected:
bool select_bank_(uint8_t reg);
bool update_bits_(uint8_t reg, uint8_t mask, bool value);
bool write_exposure_();
bool start_step_();
void read_step_();
void abort_measurement_(const LogString *reason);
void calculate_basic_counts_();
void publish_light_metrics_();
void publish_color_();
void publish_measurement_();
void publish_basic_counts_();
float normalization_divisor_() const;
std::array<sensor::Sensor *, NUM_BANDS> sensors_{};
std::array<sensor::Sensor *, NUM_BANDS> basic_counts_sensors_{};
std::array<float, NUM_BANDS> dark_current_{};
std::array<float, NUM_BANDS> channel_correction_;
std::array<float, NUM_BANDS> basic_counts_{};
std::array<uint16_t, NUM_BANDS> raw_counts_{};
const ChipInfo *chip_; // in flash, read field by field with progmem_memcpy
uint32_t start_ms_{0};
uint32_t integration_ms_{0};
uint32_t timeout_ms_{0};
float glass_attenuation_factor_{1.0f};
float max_basic_count_{0.0f}; // largest of every channel
float max_band_basic_count_{0.0f}; // largest of the visible bands only
float clear_basic_count_{0.0f}; // the wideband clear channel
uint16_t astep_{0};
uint16_t peak_raw_count_{0}; // highest ADC value of this measurement, before the clear channels are averaged
// What the running measurement was started with, so the maths matches the data even if the
// configuration is changed while a measurement is in flight.
uint16_t reading_astep_{0};
Gain reading_gain_{GAIN_1X};
uint8_t reading_atime_{0};
enum class State : uint8_t { STATE_IDLE, STATE_WAIT_SMUX, STATE_READ_DATA } state_{State::STATE_IDLE};
Normalization normalization_{Normalization::NORMALIZATION_NONE};
Gain gain_{GAIN_1X};
uint8_t atime_{0};
uint8_t step_{0};
bool bank_low_{true}; // unknown until setup() writes CFG0
#ifdef USE_AS734X_RGB
text_sensor::TextSensor *rgb_hex_sensor_{nullptr};
#endif
};
} // namespace esphome::as734x
@@ -0,0 +1,63 @@
#include "color_helpers.h"
#include <algorithm>
#include <cmath>
namespace esphome::as734x {
namespace {
// Range McCamy's approximation is meaningful over.
constexpr float CCT_MIN_K = 1000.0f;
constexpr float CCT_MAX_K = 25000.0f;
#ifdef USE_AS734X_RGB
// sRGB transfer function.
float gamma_correct(float channel) {
if (channel <= 0.0031308f) {
return 12.92f * channel;
}
return 1.055f * std::pow(channel, 1.0f / 2.4f) - 0.055f;
}
uint8_t to_byte(float channel) { return static_cast<uint8_t>(std::clamp(channel, 0.0f, 1.0f) * 255.0f); }
#endif
} // namespace
float tristimulus_to_cct(float x, float y, float z) {
const float sum = x + y + z;
// The conversion matrices carry negative coefficients, so a spectrum unlike the one they were
// calibrated against can drive the sum to zero or below. Chromaticity is undefined there, and
// McCamy's formula would still return a plausible looking number from it.
if (sum <= MIN_TRISTIMULUS_SUM) {
return NAN;
}
const float chroma_x = x / sum;
const float chroma_y = y / sum;
// McCamy's approximation: cct = 437 n^3 + 3601 n^2 + 6861 n + 5517. The pole at
// chroma_y = 0.1858 sends the cubic off to either infinity, and it stays meaningless well
// either side of that, so the answer is only reported where the approximation holds.
const float denominator = 0.1858f - chroma_y;
if (std::abs(denominator) < 1e-4f) {
return NAN;
}
const float n = (chroma_x - 0.3320f) / denominator;
const float cct = ((437.0f * n + 3601.0f) * n + 6861.0f) * n + 5517.0f;
return (cct >= CCT_MIN_K && cct <= CCT_MAX_K) ? cct : NAN;
}
#ifdef USE_AS734X_RGB
void tristimulus_to_rgb(float x, float y, float z, uint8_t &r, uint8_t &g, uint8_t &b) {
const float r_linear = 3.2406f * x - 1.5372f * y - 0.4986f * z;
const float g_linear = -0.9689f * x + 1.8758f * y + 0.0415f * z;
const float b_linear = 0.0557f * x - 0.2040f * y + 1.0570f * z;
r = to_byte(gamma_correct(r_linear));
g = to_byte(gamma_correct(g_linear));
b = to_byte(gamma_correct(b_linear));
}
#endif
} // namespace esphome::as734x
+22
View File
@@ -0,0 +1,22 @@
#pragma once
#include <cstdint>
#include "esphome/core/defines.h"
namespace esphome::as734x {
// Smallest tristimulus sum that still carries a usable hue.
constexpr float MIN_TRISTIMULUS_SUM = 0.001f;
// Correlated colour temperature in kelvin from CIE 1931 tristimulus values, using McCamy's
// approximation. Returns NAN when the input carries no usable hue, or when the result falls
// outside the range the approximation holds over.
float tristimulus_to_cct(float x, float y, float z);
#ifdef USE_AS734X_RGB
// sRGB components for CIE 1931 tristimulus values. Pass chromaticity (x + y + z == 1) rather than
// absolute values, otherwise anything but dim light clamps to white.
void tristimulus_to_rgb(float x, float y, float z, uint8_t &r, uint8_t &g, uint8_t &b);
#endif
} // namespace esphome::as734x
+144
View File
@@ -0,0 +1,144 @@
import esphome.codegen as cg
from esphome.components import sensor
from esphome.components.const import UNIT_COUNTS
import esphome.config_validation as cv
from esphome.const import (
CONF_COLOR_TEMPERATURE,
CONF_COUNTS,
CONF_ILLUMINANCE,
CONF_TYPE,
DEVICE_CLASS_ILLUMINANCE,
DEVICE_CLASS_IRRADIANCE,
STATE_CLASS_MEASUREMENT,
UNIT_KELVIN,
UNIT_LUX,
UNIT_PERCENT,
)
import esphome.final_validate as fv
from esphome.types import ConfigType
from . import BANDS_41, BANDS_43, CONF_AS734X_ID, DOMAIN, AS734XComponent
DEPENDENCIES = [DOMAIN]
CONF_BASIC_COUNTS = "basic_counts"
CONF_IRRADIANCE_PAR = "irradiance_par"
CONF_IRRADIANCE_PHOTOPIC = "irradiance_photopic"
CONF_PPFD = "ppfd"
CONF_SATURATION_LEVEL = "saturation_level"
UNIT_IRRADIANCE = "W/m\u00b2"
UNIT_PPFD = "\u00b5mol/m\u00b2/s"
ICON_COUNTS = "mdi:counter"
ICON_SATURATION = "mdi:weather-sunny-alert"
ICON_ILLUMINANCE = "mdi:weather-sunny"
ICON_IRRADIANCE_PHOTOPIC = "mdi:sun-wireless-outline"
ICON_PAR = "mdi:sprout"
ICON_PPFD = "mdi:sprout-outline"
ICON_COLOR_TEMPERATURE = "mdi:sun-thermometer-outline"
_COUNTS_SENSOR = sensor.sensor_schema(
unit_of_measurement=UNIT_COUNTS,
icon=ICON_COUNTS,
accuracy_decimals=0,
state_class=STATE_CLASS_MEASUREMENT,
)
# Basic counts are fractional, so they need decimals where raw counts do not.
_BASIC_COUNTS_SENSOR = sensor.sensor_schema(
unit_of_measurement=UNIT_COUNTS,
icon=ICON_COUNTS,
accuracy_decimals=6,
state_class=STATE_CLASS_MEASUREMENT,
)
_BAND_KEYS = (CONF_COUNTS, CONF_BASIC_COUNTS)
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_AS734X_ID): cv.use_id(AS734XComponent),
cv.Optional(CONF_COUNTS, default={}): cv.Schema(
{cv.Optional(band): _COUNTS_SENSOR for band in BANDS_43}
),
cv.Optional(CONF_BASIC_COUNTS, default={}): cv.Schema(
{cv.Optional(band): _BASIC_COUNTS_SENSOR for band in BANDS_43}
),
cv.Optional(CONF_SATURATION_LEVEL): sensor.sensor_schema(
unit_of_measurement=UNIT_PERCENT,
icon=ICON_SATURATION,
accuracy_decimals=0,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_ILLUMINANCE): sensor.sensor_schema(
unit_of_measurement=UNIT_LUX,
icon=ICON_ILLUMINANCE,
accuracy_decimals=0,
device_class=DEVICE_CLASS_ILLUMINANCE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_IRRADIANCE_PHOTOPIC): sensor.sensor_schema(
unit_of_measurement=UNIT_IRRADIANCE,
icon=ICON_IRRADIANCE_PHOTOPIC,
accuracy_decimals=3,
device_class=DEVICE_CLASS_IRRADIANCE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_IRRADIANCE_PAR): sensor.sensor_schema(
unit_of_measurement=UNIT_IRRADIANCE,
icon=ICON_PAR,
accuracy_decimals=3,
device_class=DEVICE_CLASS_IRRADIANCE,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_PPFD): sensor.sensor_schema(
unit_of_measurement=UNIT_PPFD,
icon=ICON_PPFD,
accuracy_decimals=3,
state_class=STATE_CLASS_MEASUREMENT,
),
cv.Optional(CONF_COLOR_TEMPERATURE): sensor.sensor_schema(
unit_of_measurement=UNIT_KELVIN,
icon=ICON_COLOR_TEMPERATURE,
accuracy_decimals=0,
state_class=STATE_CLASS_MEASUREMENT,
),
}
)
def _final_validate(config: ConfigType) -> ConfigType:
full = fv.full_config.get()
hub = full.get_config_for_path(full.get_path_for_id(config[CONF_AS734X_ID])[:-1])
if hub[CONF_TYPE] != "AS7341":
return config
for key in _BAND_KEYS:
if extra := [band for band in config.get(key, {}) if band not in BANDS_41]:
raise cv.Invalid(
f"The AS7341 has no {', '.join(extra)} band", path=[key, extra[0]]
)
return config
FINAL_VALIDATE_SCHEMA = _final_validate
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_AS734X_ID])
counts = config[CONF_COUNTS]
basic_counts = config[CONF_BASIC_COUNTS]
for i, band in enumerate(BANDS_43):
if (conf := counts.get(band)) is not None:
sens = await sensor.new_sensor(conf)
cg.add(hub.set_counts_sensor(sens, i))
if (conf := basic_counts.get(band)) is not None:
sens = await sensor.new_sensor(conf)
cg.add(hub.set_basic_counts_sensor(sens, i))
sensors = sensor.sub_sensors(config)
await sensors(CONF_SATURATION_LEVEL, hub.set_saturation_level_sensor)
await sensors(CONF_ILLUMINANCE, hub.set_illuminance_sensor)
await sensors(CONF_IRRADIANCE_PHOTOPIC, hub.set_irradiance_photopic_sensor)
await sensors(CONF_IRRADIANCE_PAR, hub.set_irradiance_par_sensor)
await sensors(CONF_PPFD, hub.set_ppfd_sensor)
await sensors(CONF_COLOR_TEMPERATURE, hub.set_color_temperature_sensor)
+28
View File
@@ -0,0 +1,28 @@
import esphome.codegen as cg
from esphome.components import text_sensor
import esphome.config_validation as cv
from esphome.types import ConfigType
from . import CONF_AS734X_ID, DOMAIN, AS734XComponent
DEPENDENCIES = [DOMAIN]
CONF_RGB_HEX = "rgb_hex"
ICON_PALETTE = "mdi:palette"
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_AS734X_ID): cv.use_id(AS734XComponent),
cv.Optional(CONF_RGB_HEX): text_sensor.text_sensor_schema(icon=ICON_PALETTE),
}
)
async def to_code(config: ConfigType) -> None:
if (conf := config.get(CONF_RGB_HEX)) is None:
return
hub = await cg.get_variable(config[CONF_AS734X_ID])
cg.add_define("USE_AS734X_RGB")
sens = await text_sensor.new_text_sensor(conf)
cg.add(hub.set_rgb_hex_sensor(sens))
@@ -80,6 +80,9 @@ void CaptivePortal::setup() {
this->disable_loop();
}
void CaptivePortal::start() {
// Each start takes a web server reference that end() returns once
if (this->active_)
return;
this->base_->init();
if (!this->initialized_) {
this->base_->add_handler_without_auth(this);
+2
View File
@@ -15,6 +15,7 @@ CONF_AQI = "aqi"
CONF_AUTOMATIC_BASELINE_CALIBRATION = "automatic_baseline_calibration"
CONF_B_CONSTANT = "b_constant"
CONF_BLUE_CHANNEL = "blue_channel"
CONF_BORDER_ROUTER = "border_router"
CONF_BREATH_VOC_EQUIVALENT = "breath_voc_equivalent"
CONF_BYTE_ORDER = "byte_order"
CONF_CHANNEL_COLORS = "channel_colors"
@@ -51,6 +52,7 @@ CONF_ON_RECEIVE = "on_receive"
CONF_ON_SCAN_END = "on_scan_end"
CONF_ON_STATE_CHANGE = "on_state_change"
CONF_ON_STOP = "on_stop"
CONF_OUT_PIN_LEVEL = "out_pin_level"
CONF_PARITY = "parity"
CONF_RECEIVER_FREQUENCY = "receiver_frequency"
CONF_RECONNECT_INTERVAL = "reconnect_interval"
@@ -0,0 +1,84 @@
from collections.abc import Callable
import esphome.codegen as cg
from esphome.components import uart
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_MODE, CONF_MODEL
from esphome.core import ID
import esphome.final_validate as fv
from esphome.types import ConfigType
CODEOWNERS = ["@remcom"]
DEPENDENCIES = ["uart"]
DOMAIN = "dfrobot_sen0609"
MULTI_CONF = True
dfrobot_sen0609_ns = cg.esphome_ns.namespace("dfrobot_sen0609")
DFRobotSEN0609Component = dfrobot_sen0609_ns.class_(
"DFRobotSEN0609Component", cg.Component, uart.UARTDevice
)
RunMode = dfrobot_sen0609_ns.enum("RunMode", is_class=True)
CONF_DFROBOT_SEN0609_ID = "dfrobot_sen0609_id"
MODE_PRESENCE = "presence"
MODE_SPEED_AND_DISTANCE = "speed_and_distance"
RUN_MODES = {
MODE_PRESENCE: RunMode.RUN_MODE_PRESENCE,
MODE_SPEED_AND_DISTANCE: RunMode.RUN_MODE_SPEED_AND_DISTANCE,
}
MODEL_SEN0609 = "SEN0609"
MODEL_SEN0610 = "SEN0610"
# Longest detection range in metres
MAX_RANGES = {MODEL_SEN0609: 25.0, MODEL_SEN0610: 12.0}
CONFIG_SCHEMA = (
cv.Schema(
{
cv.GenerateID(): cv.declare_id(DFRobotSEN0609Component),
cv.Optional(CONF_MODEL, default=MODEL_SEN0609): cv.one_of(
*MAX_RANGES, upper=True
),
cv.Optional(CONF_MODE, default=MODE_PRESENCE): cv.one_of(
*RUN_MODES, lower=True
),
}
)
.extend(uart.UART_DEVICE_SCHEMA)
.extend(cv.COMPONENT_SCHEMA)
)
FINAL_VALIDATE_SCHEMA = uart.final_validate_device_schema(
DOMAIN,
baud_rate=9600,
require_tx=True,
require_rx=True,
parity="NONE",
stop_bits=1,
)
def get_hub_config(root: ConfigType, hub_id: ID) -> ConfigType:
"""Return the configuration of the hub with the given id."""
return next(conf for conf in root[DOMAIN] if conf[CONF_ID] == hub_id)
def final_validate_mode(keys: dict[str, str]) -> Callable[[ConfigType], None]:
"""Reject entity keys that need a different hub mode; `keys` maps each key to the mode it needs."""
def validator(config: ConfigType) -> None:
hub = get_hub_config(fv.full_config.get(), config[CONF_DFROBOT_SEN0609_ID])
for key, mode in keys.items():
if key in config and hub[CONF_MODE] != mode:
raise cv.Invalid(
f"'{key}' needs the {DOMAIN} 'mode' to be '{mode}'", path=[key]
)
return validator
async def to_code(config: ConfigType) -> None:
var = cg.new_Pvariable(config[CONF_ID], RUN_MODES[config[CONF_MODE]])
await cg.register_component(var, config)
await uart.register_uart_device(var, config)
@@ -0,0 +1,24 @@
import esphome.codegen as cg
from esphome.components import binary_sensor
import esphome.config_validation as cv
from esphome.const import CONF_HAS_TARGET, CONF_ID, DEVICE_CLASS_OCCUPANCY, ICON_ACCOUNT
from esphome.types import ConfigType
from . import CONF_DFROBOT_SEN0609_ID, DFRobotSEN0609Component
DEPENDENCIES = ["dfrobot_sen0609"]
CONFIG_SCHEMA = {
cv.GenerateID(CONF_ID): cv.declare_id(cg.EntityBase),
cv.GenerateID(CONF_DFROBOT_SEN0609_ID): cv.use_id(DFRobotSEN0609Component),
cv.Optional(CONF_HAS_TARGET): binary_sensor.binary_sensor_schema(
device_class=DEVICE_CLASS_OCCUPANCY,
icon=ICON_ACCOUNT,
),
}
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_DFROBOT_SEN0609_ID])
binary_sensors = binary_sensor.sub_binary_sensors(config)
await binary_sensors(CONF_HAS_TARGET, hub.set_has_target_binary_sensor)
@@ -0,0 +1,883 @@
#include "dfrobot_sen0609.h"
#include "esphome/core/application.h"
#include "esphome/core/hal.h"
#include "esphome/core/log.h"
#include "esphome/core/progmem.h"
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <cstring>
namespace esphome::dfrobot_sen0609 {
ESPHOME_LOG_TAG(TAG, "dfrobot_sen0609");
static constexpr uint8_t MAX_ATTEMPTS = 3;
// The module ignores a command that follows its last answer too closely
static constexpr uint32_t COMMAND_GAP_MS = 100;
// Gathers entity changes made close together into one flash write
static constexpr uint32_t COALESCE_MS = 1000;
static constexpr uint32_t DEFAULT_TIMEOUT_MS = 1000;
static constexpr uint32_t LONG_TIMEOUT_MS = 2000;
static constexpr uint32_t GIVE_UP_BACKOFF_MS = 30000;
// Presence frames come every second (setUartOutput period); this long without one means the module lost them
static constexpr uint32_t FRAME_TIMEOUT_MS = 10000;
static constexpr uint8_t FRAME_FIELDS = 5;
static constexpr uint8_t LINE_END[] = {'\r', '\n'};
// Indexed by Param; empty for parameters that cannot be read
PROGMEM_STRING_TABLE(ReadCommands, "", "", "getRange", "getTrigRange", "getSensitivity", "getLatency", "getInhibit",
"getThrFactor", "getMicroMotion", "getGpioMode 1", "", "", "getSWV", "getHWV");
static_assert(PARAM_COUNT <= 16, "pending_writes_ and pending_reads_ hold one bit per parameter");
static constexpr uint16_t param_bit(Param param) { return static_cast<uint16_t>(1U << param); }
// Parameters kept in the module's flash only after saveConfig
static constexpr uint16_t PERSISTENT_PARAMS =
param_bit(PARAM_RANGE) | param_bit(PARAM_TRIG_RANGE) | param_bit(PARAM_SENSITIVITY) | param_bit(PARAM_LATENCY) |
param_bit(PARAM_INHIBIT) | param_bit(PARAM_THR_FACTOR) | param_bit(PARAM_MICRO_MOTION) | param_bit(PARAM_GPIO_MODE);
// The frames depend on these, so their writes are kept until they succeed
static constexpr uint16_t FRAME_PARAMS = param_bit(PARAM_RUN_APP) | param_bit(PARAM_UART_OUTPUT);
#ifdef USE_SWITCH
static constexpr bool is_led(Param param) { return param == PARAM_RUN_LED || param == PARAM_OUT_LED; }
static constexpr SwitchSetting led_switch(Param param) {
return param == PARAM_RUN_LED ? SWITCH_RUN_LED : SWITCH_OUT_LED;
}
#endif
#ifdef USE_NUMBER
static_assert(NUMBER_COUNT <= 16, "changed_numbers_ holds one bit per number");
static constexpr uint8_t NUMBER_PARAMS[NUMBER_COUNT] PROGMEM = {
PARAM_RANGE, PARAM_RANGE, PARAM_TRIG_RANGE, PARAM_SENSITIVITY, PARAM_SENSITIVITY,
PARAM_LATENCY, PARAM_LATENCY, PARAM_INHIBIT, PARAM_THR_FACTOR,
};
#endif
static bool starts_with(const char *str, ESPHOME_PGM_P prefix) {
return ESPHOME_strncmp_P(str, prefix, ESPHOME_strlen_P(prefix)) == 0;
}
static bool contains(const char *str, ESPHOME_PGM_P needle) {
const size_t len = ESPHOME_strlen_P(needle);
for (; *str != '\0'; str++) {
if (ESPHOME_strncmp_P(str, needle, len) == 0)
return true;
}
return false;
}
// Splits "a,b,c*" in place; returns the number of fields stored in `fields`
static uint8_t split_fields(char *str, char **fields, uint8_t max_fields) {
uint8_t count = 0;
char *start = str;
for (;; str++) {
if (*str != ',' && *str != '*' && *str != '\0')
continue;
const bool last = *str != ',';
*str = '\0';
if (count < max_fields)
fields[count++] = start;
if (last)
return count;
start = str + 1;
}
}
// Answers that refuse a command whose effect is already in place
static bool answer_means_done(Operation operation, const char *line) {
switch (operation) {
case Operation::OPERATION_STOP:
return contains(line, ESPHOME_PSTR("stopped already"));
case Operation::OPERATION_START:
return contains(line, ESPHOME_PSTR("started already"));
case Operation::OPERATION_SAVE:
return starts_with(line, ESPHOME_PSTR("no parameter"));
default:
return false;
}
}
static bool parse_float(const char *str, float &value) {
char *end;
value = strtof(str, &end);
return end != str && *end == '\0';
}
static const LogString *operation_to_str(Operation operation) {
switch (operation) {
case Operation::OPERATION_STOP:
return LOG_STR("stop");
case Operation::OPERATION_START:
return LOG_STR("start");
case Operation::OPERATION_WRITE:
return LOG_STR("write");
case Operation::OPERATION_SAVE:
return LOG_STR("save");
case Operation::OPERATION_READ:
return LOG_STR("read");
default:
return LOG_STR("none");
}
}
DFRobotSEN0609Component::DFRobotSEN0609Component(RunMode mode) : mode_(mode) {
#ifdef USE_NUMBER
this->number_values_.fill(NAN);
#endif
}
void DFRobotSEN0609Component::dump_config() {
ESP_LOGCONFIG(
TAG,
"DFRobot SEN0609:\n"
" Mode: %s",
this->mode_ == RunMode::RUN_MODE_PRESENCE ? LOG_STR_LITERAL("presence") : LOG_STR_LITERAL("speed and distance"));
#ifdef USE_BINARY_SENSOR
LOG_BINARY_SENSOR(" ", "HasTarget", this->has_target_binary_sensor_);
#endif
#ifdef USE_SENSOR
LOG_SENSOR(" ", "TargetDistance", this->target_distance_sensor_);
LOG_SENSOR(" ", "TargetSpeed", this->target_speed_sensor_);
LOG_SENSOR(" ", "TargetEnergy", this->target_energy_sensor_);
#endif
#ifdef USE_TEXT_SENSOR
LOG_TEXT_SENSOR(" ", "Version", this->version_text_sensor_);
LOG_TEXT_SENSOR(" ", "HardwareVersion", this->hardware_version_text_sensor_);
#endif
#ifdef USE_NUMBER
for (number::Number *n : this->numbers_) {
LOG_NUMBER(" ", "Number", n);
}
#endif
#ifdef USE_SWITCH
for (switch_::Switch *s : this->switches_) {
LOG_SWITCH(" ", "Switch", s);
}
#endif
#ifdef USE_SELECT
LOG_SELECT(" ", "OutPinLevel", this->out_pin_level_select_);
#endif
}
void DFRobotSEN0609Component::setup() {
#ifdef USE_SWITCH
// The module forgets the LED states at power off
for (uint8_t i = SWITCH_RUN_LED; i <= SWITCH_OUT_LED; i++) {
switch_::Switch *sw = this->switches_[i];
if (sw == nullptr)
continue;
const bool on = sw->get_initial_state_with_restore_mode().value_or(true);
this->publish_switch_(static_cast<SwitchSetting>(i), on != sw->is_inverted());
}
#endif
this->queue_configuration_();
}
void DFRobotSEN0609Component::loop() {
uint8_t buf[MAX_LINE_LENGTH];
size_t avail = this->available();
while (avail > 0) {
const size_t to_read = std::min(avail, sizeof(buf));
if (!this->read_array(buf, to_read)) {
if (!this->read_failed_) {
ESP_LOGW(TAG, "UART read failed");
}
this->read_failed_ = true;
break;
}
this->read_failed_ = false;
avail -= to_read;
for (size_t i = 0; i < to_read; i++)
this->readline_(buf[i]);
}
this->run_sequencer_();
}
void DFRobotSEN0609Component::readline_(uint8_t c) {
if (c == '\r')
return;
if (c == '\n') {
if (!this->line_overflow_) {
this->line_[this->line_pos_] = '\0';
this->handle_line_(this->line_);
}
this->line_overflow_ = false;
this->line_pos_ = 0;
return;
}
if (this->line_overflow_)
return;
if (this->line_pos_ >= MAX_LINE_LENGTH - 1) {
ESP_LOGW(TAG, "Line too long, dropped");
this->line_overflow_ = true;
return;
}
this->line_[this->line_pos_++] = static_cast<char>(c);
// The prompt has no line end, and the echo of the next command follows it on the same line
if (c == '>') {
this->line_[this->line_pos_] = '\0';
if (ESPHOME_strcmp_P(this->line_, ESPHOME_PSTR("DFRobot:/>")) == 0)
this->line_pos_ = 0;
}
}
void DFRobotSEN0609Component::handle_line_(char *line) {
if (*line == '\0')
return;
ESP_LOGV(TAG, "RX: %s", line);
if (line[0] == '$') {
this->handle_frame_(line);
return;
}
// Version lines name their value, so they are used even when they arrive late
const bool software = starts_with(line, ESPHOME_PSTR("SoftwareVersion:"));
if (software || starts_with(line, ESPHOME_PSTR("HardwareVersion:"))) {
if (this->operation_ == Operation::OPERATION_READ &&
this->param_ == (software ? PARAM_SW_VERSION : PARAM_HW_VERSION))
this->version_received_ = true;
#ifdef USE_TEXT_SENSOR
text_sensor::TextSensor *sens = software ? this->version_text_sensor_ : this->hardware_version_text_sensor_;
if (sens != nullptr)
sens->publish_state(line + 16);
#endif
return;
}
if (this->operation_ == Operation::OPERATION_NONE) {
ESP_LOGD(TAG, "Ignored line, no command pending: %s", line);
return;
}
if (strcmp(line, this->command_) == 0) {
this->echo_seen_ = true;
return;
}
// The module echoes every command first; lines before the echo belong to an earlier command
if (!this->echo_seen_) {
ESP_LOGD(TAG, "Ignored line from an earlier command: %s", line);
return;
}
if (starts_with(line, ESPHOME_PSTR("Done")) || answer_means_done(this->operation_, line)) {
this->finish_operation_();
} else if (starts_with(line, ESPHOME_PSTR("Response "))) {
this->parse_response_(line + 9);
} else if (contains(line, ESPHOME_PSTR("not stopped"))) {
// Stop the sensor before the next attempt
this->stopped_ = false;
this->fail_operation_();
} else if (starts_with(line, ESPHOME_PSTR("Error")) || contains(line, ESPHOME_PSTR("not recognized"))) {
ESP_LOGW(TAG, "Command '%s' rejected: %s", this->command_, line);
this->fail_operation_();
} else {
ESP_LOGD(TAG, "Unexpected answer to '%s': %s", this->command_, line);
}
}
void DFRobotSEN0609Component::handle_frame_(char *line) {
// $DFHPD,<presence>, , , *
if (starts_with(line, ESPHOME_PSTR("$DFHPD,"))) {
const char presence = line[7];
if (presence != '0' && presence != '1') {
this->log_bad_frame_(LOG_STR("Malformed presence frame"), line);
return;
}
this->last_frame_ms_ = App.get_loop_component_start_time();
#ifdef USE_BINARY_SENSOR
if (this->has_target_binary_sensor_ != nullptr)
this->has_target_binary_sensor_->publish_state(presence == '1');
#endif
return;
}
// $DFDMD,<targets>,<reserved>,<distance m>,<speed m/s>,<energy>,...*
if (!starts_with(line, ESPHOME_PSTR("$DFDMD,"))) {
this->log_bad_frame_(LOG_STR("Unknown frame"), line);
return;
}
char *fields[FRAME_FIELDS];
float distance;
float speed;
float energy;
if (split_fields(line + 7, fields, FRAME_FIELDS) < FRAME_FIELDS) {
this->log_bad_frame_(LOG_STR("Malformed target frame"), line);
return;
}
const optional<uint8_t> targets = parse_number<uint8_t>(fields[0]);
if (!targets.has_value()) {
this->log_bad_frame_(LOG_STR("Malformed target frame"), line);
return;
}
if (*targets == 0) {
#ifdef USE_BINARY_SENSOR
if (this->has_target_binary_sensor_ != nullptr)
this->has_target_binary_sensor_->publish_state(false);
#endif
#ifdef USE_SENSOR
this->publish_target_(NAN, NAN, NAN);
#endif
return;
}
if (!parse_float(fields[2], distance) || !parse_float(fields[3], speed) || !parse_float(fields[4], energy)) {
this->log_bad_frame_(LOG_STR("Malformed target frame"), line);
return;
}
#ifdef USE_BINARY_SENSOR
if (this->has_target_binary_sensor_ != nullptr)
this->has_target_binary_sensor_->publish_state(true);
#endif
#ifdef USE_SENSOR
this->publish_target_(distance, speed, energy);
#endif
}
void DFRobotSEN0609Component::log_bad_frame_(const LogString *problem, const char *line) {
// The module sends frames several times a second, so only the first bad one is logged
if (this->bad_frame_logged_)
return;
this->bad_frame_logged_ = true;
ESP_LOGW(TAG, "%s: %s", LOG_STR_ARG(problem), line);
}
#ifdef USE_SENSOR
static void publish_deduplicated(sensor::Sensor *sens, Deduplicator<float> &dedup, float value) {
if (sens == nullptr)
return;
if (std::isnan(value) ? dedup.next_unknown() : dedup.next(value))
sens->publish_state(value);
}
void DFRobotSEN0609Component::publish_target_(float distance, float speed, float energy) {
publish_deduplicated(this->target_distance_sensor_, this->distance_dedup_, distance);
publish_deduplicated(this->target_speed_sensor_, this->speed_dedup_, speed);
publish_deduplicated(this->target_energy_sensor_, this->energy_dedup_, energy);
}
#endif
void DFRobotSEN0609Component::parse_response_(const char *args) {
this->response_count_ = 0;
while (this->response_count_ < MAX_RESPONSE_VALUES) {
char *end;
const float value = strtof(args, &end);
// strtof also takes "nan" and "inf", which no setting can hold
if (end == args || !std::isfinite(value))
break;
this->response_[this->response_count_++] = value;
args = end;
}
}
void DFRobotSEN0609Component::queue_configuration_() {
// The mode, the UART output and the LEDs are applied at every boot instead of being saved
uint16_t writes = param_bit(PARAM_RUN_APP);
if (this->mode_ == RunMode::RUN_MODE_PRESENCE)
writes |= param_bit(PARAM_UART_OUTPUT);
#ifdef USE_SWITCH
if (this->switches_[SWITCH_RUN_LED] != nullptr)
writes |= param_bit(PARAM_RUN_LED);
if (this->switches_[SWITCH_OUT_LED] != nullptr)
writes |= param_bit(PARAM_OUT_LED);
#endif
this->pending_writes_ |= writes;
this->pending_reads_ |= this->wanted_reads_();
}
uint16_t DFRobotSEN0609Component::wanted_reads_() const {
uint16_t reads = 0;
#ifdef USE_TEXT_SENSOR
if (this->version_text_sensor_ != nullptr)
reads |= param_bit(PARAM_SW_VERSION);
if (this->hardware_version_text_sensor_ != nullptr)
reads |= param_bit(PARAM_HW_VERSION);
#endif
#ifdef USE_NUMBER
for (uint8_t i = 0; i < NUMBER_COUNT; i++) {
if (this->numbers_[i] != nullptr)
reads |= param_bit(static_cast<Param>(progmem_read_byte(&NUMBER_PARAMS[i])));
}
#endif
#ifdef USE_SWITCH
if (this->switches_[SWITCH_MICRO_MOTION] != nullptr)
reads |= param_bit(PARAM_MICRO_MOTION);
#endif
#ifdef USE_SELECT
if (this->out_pin_level_select_ != nullptr)
reads |= param_bit(PARAM_GPIO_MODE);
#endif
return reads;
}
void DFRobotSEN0609Component::check_frames_(uint32_t now) {
// Speed mode frames are not watched: whether the module sends them without a target is not known
if (this->mode_ != RunMode::RUN_MODE_PRESENCE || now - this->last_frame_ms_ < FRAME_TIMEOUT_MS)
return;
// The module most likely restarted on its own and lost the configuration applied at boot
ESP_LOGW(TAG, "No presence frames, applying the configuration again");
this->status_set_warning();
#ifdef USE_BINARY_SENSOR
if (this->has_target_binary_sensor_ != nullptr)
this->has_target_binary_sensor_->invalidate_state();
#endif
this->last_frame_ms_ = now;
this->queue_configuration_();
}
void DFRobotSEN0609Component::request_write_(Param param) {
const uint32_t now = App.get_loop_component_start_time();
this->pending_writes_ |= param_bit(param);
this->last_change_ms_ = now;
if (this->operation_ != Operation::OPERATION_NONE || this->stopped_)
return;
// Start a new sequence a little later, so changes made together end in one flash write.
// A longer hold, such as the back-off from a module that does not answer, is not cut short.
const uint32_t elapsed = now - this->hold_start_ms_;
if (elapsed >= this->hold_ms_ || this->hold_ms_ - elapsed < COALESCE_MS)
this->hold_(COALESCE_MS);
}
#ifdef USE_NUMBER
void DFRobotSEN0609Component::control_number(NumberSetting setting, float value) {
// The module needs min_range <= trigger_range <= max_range; a value not read yet is NAN and passes
const auto &values = this->number_values_;
const float min_range = setting == NUMBER_MIN_RANGE ? value : values[NUMBER_MIN_RANGE];
const float max_range = setting == NUMBER_MAX_RANGE ? value : values[NUMBER_MAX_RANGE];
const float trigger_range = setting == NUMBER_TRIGGER_RANGE ? value : values[NUMBER_TRIGGER_RANGE];
if (setting <= NUMBER_TRIGGER_RANGE &&
(min_range > max_range || trigger_range < min_range || trigger_range > max_range)) {
ESP_LOGW(TAG, "Ranges must keep minimum <= trigger <= maximum, not written");
if (!std::isnan(this->number_values_[setting]))
this->publish_number_(setting, this->number_values_[setting]);
return;
}
this->changed_numbers_ |= 1U << setting;
this->publish_number_(setting, value);
this->request_write_(static_cast<Param>(progmem_read_byte(&NUMBER_PARAMS[setting])));
}
void DFRobotSEN0609Component::forget_number_changes_(Param param, bool unknown) {
for (uint8_t i = 0; i < NUMBER_COUNT; i++) {
if (progmem_read_byte(&NUMBER_PARAMS[i]) != param)
continue;
this->changed_numbers_ &= ~(1U << i);
if (unknown)
this->publish_number_(static_cast<NumberSetting>(i), NAN);
}
}
void DFRobotSEN0609Component::publish_number_(NumberSetting setting, float value) {
this->number_values_[setting] = value;
if (this->numbers_[setting] != nullptr)
this->numbers_[setting]->publish_state(value);
}
#endif
#ifdef USE_SWITCH
void DFRobotSEN0609Component::control_switch(SwitchSetting setting, bool state) {
this->publish_switch_(setting, state);
switch (setting) {
case SWITCH_MICRO_MOTION:
this->request_write_(PARAM_MICRO_MOTION);
break;
case SWITCH_RUN_LED:
this->request_write_(PARAM_RUN_LED);
break;
default:
this->request_write_(PARAM_OUT_LED);
break;
}
}
void DFRobotSEN0609Component::publish_switch_(SwitchSetting setting, bool state) {
this->switch_states_[setting] = state;
if (this->switches_[setting] != nullptr)
this->switches_[setting]->publish_state(state);
}
#endif
#ifdef USE_SELECT
void DFRobotSEN0609Component::control_out_pin_level(size_t index) {
this->out_pin_level_ = static_cast<uint8_t>(index);
this->out_pin_level_select_->publish_state(index);
this->request_write_(PARAM_GPIO_MODE);
}
#endif
void DFRobotSEN0609Component::hold_(uint32_t duration_ms) {
this->hold_start_ms_ = App.get_loop_component_start_time();
this->hold_ms_ = duration_ms;
}
void DFRobotSEN0609Component::run_sequencer_() {
const uint32_t now = App.get_loop_component_start_time();
if (this->operation_ != Operation::OPERATION_NONE) {
const bool slow = this->operation_ == Operation::OPERATION_STOP || this->operation_ == Operation::OPERATION_SAVE;
if (now - this->sent_ms_ < (slow ? LONG_TIMEOUT_MS : DEFAULT_TIMEOUT_MS))
return;
ESP_LOGD(TAG, "No answer to '%s'", this->command_);
this->fail_operation_();
return;
}
if (now - this->hold_start_ms_ < this->hold_ms_)
return;
const bool work_pending = this->save_pending_ || this->pending_writes_ != 0 || this->pending_reads_ != 0;
Operation operation;
Param param = PARAM_COUNT;
if (!work_pending) {
if (!this->stopped_) {
this->check_frames_(now);
// Work given up on is tried again in a later sequence, so the sensor runs in between
if ((this->deferred_writes_ != 0 || this->deferred_save_) && now - this->deferred_ms_ >= GIVE_UP_BACKOFF_MS) {
this->pending_writes_ |= this->deferred_writes_;
this->save_pending_ = this->save_pending_ || this->deferred_save_;
this->deferred_writes_ = 0;
this->deferred_save_ = false;
}
return;
}
operation = Operation::OPERATION_START;
} else if (!this->stopped_) {
operation = Operation::OPERATION_STOP;
} else if (this->pending_writes_ != 0) {
operation = Operation::OPERATION_WRITE;
param = static_cast<Param>(__builtin_ctz(this->pending_writes_));
} else if (this->save_pending_) {
// Changes made within a second of each other share one flash write, also while a sequence runs
if (now - this->last_change_ms_ < COALESCE_MS)
return;
operation = Operation::OPERATION_SAVE;
} else {
operation = Operation::OPERATION_READ;
param = static_cast<Param>(__builtin_ctz(this->pending_reads_));
}
const uint16_t bit = param_bit(param);
bool built = this->build_command_(operation, param);
if (!built && operation == Operation::OPERATION_WRITE) {
// The value this write is paired with is unknown: read it first, once, and keep the write pending
if ((this->paired_reads_ & bit) == 0)
this->pending_reads_ |= this->wanted_reads_() & bit;
if ((this->pending_reads_ & bit) != 0) {
this->paired_reads_ |= bit;
operation = Operation::OPERATION_READ;
built = this->build_command_(operation, param);
}
}
if (!built) {
if (operation == Operation::OPERATION_WRITE) {
ESP_LOGW(TAG, "Setting not written: the value it is paired with could not be read");
this->pending_writes_ &= ~bit;
this->paired_reads_ &= ~bit;
#ifdef USE_NUMBER
// Both values come from the read that failed, so what the module holds is unknown
this->forget_number_changes_(param, true);
#endif
} else {
ESP_LOGE(TAG, "No command for %s of parameter %u", LOG_STR_ARG(operation_to_str(operation)),
static_cast<unsigned>(param));
this->pending_reads_ &= ~bit;
}
this->sequence_failed_ = true;
this->status_set_warning();
return;
}
// Cleared now, so a change made while the write is on its way is written again
if (operation == Operation::OPERATION_WRITE) {
this->pending_writes_ &= ~bit;
this->paired_reads_ &= ~bit;
#ifdef USE_NUMBER
this->forget_number_changes_(param, false);
#endif
}
this->operation_ = operation;
this->param_ = param;
this->response_count_ = 0;
this->echo_seen_ = false;
this->version_received_ = false;
this->sent_ms_ = now;
ESP_LOGV(TAG, "TX: %s", this->command_);
this->write_str(this->command_);
this->write_array(LINE_END, sizeof(LINE_END));
}
bool DFRobotSEN0609Component::build_command_(Operation operation, Param param) {
char *buf = this->command_;
constexpr size_t len = MAX_COMMAND_LENGTH;
switch (operation) {
case Operation::OPERATION_STOP:
ESPHOME_strncpy_P(buf, ESPHOME_PSTR("sensorStop"), len);
return true;
case Operation::OPERATION_START:
ESPHOME_strncpy_P(buf, ESPHOME_PSTR("sensorStart"), len);
return true;
case Operation::OPERATION_SAVE:
ESPHOME_strncpy_P(buf, ESPHOME_PSTR("saveConfig"), len);
return true;
case Operation::OPERATION_READ:
ESPHOME_strncpy_P(buf, reinterpret_cast<ESPHOME_PGM_P>(ReadCommands::get_progmem_str(param, 0)), len);
return buf[0] != '\0';
default:
break;
}
switch (param) {
case PARAM_RUN_APP:
ESPHOME_snprintf_P(buf, len, ESPHOME_PSTR("setRunApp %u"), static_cast<unsigned>(this->mode_));
return true;
case PARAM_UART_OUTPUT:
ESPHOME_strncpy_P(buf, ESPHOME_PSTR("setUartOutput 1 1 1 1.0"), len);
return true;
#ifdef USE_SWITCH
case PARAM_MICRO_MOTION:
ESPHOME_snprintf_P(buf, len, ESPHOME_PSTR("setMicroMotion %u"),
static_cast<unsigned>(this->switch_states_[SWITCH_MICRO_MOTION]));
return true;
case PARAM_RUN_LED:
case PARAM_OUT_LED: {
// 0 turns the LED on
const bool on = this->switch_states_[led_switch(param)];
this->sent_led_on_ = on;
ESPHOME_snprintf_P(buf, len, ESPHOME_PSTR("setLedMode %u %u"), param == PARAM_RUN_LED ? 1U : 2U, on ? 0U : 1U);
return true;
}
#endif
#ifdef USE_SELECT
case PARAM_GPIO_MODE:
ESPHOME_snprintf_P(buf, len, ESPHOME_PSTR("setGpioMode 1 %u"), static_cast<unsigned>(this->out_pin_level_));
return true;
#endif
default:
break;
}
#ifdef USE_NUMBER
const auto &values = this->number_values_;
float first = NAN;
float second = 0.0f;
ESPHOME_PGM_P format = nullptr;
switch (param) {
case PARAM_RANGE:
first = values[NUMBER_MIN_RANGE];
second = values[NUMBER_MAX_RANGE];
format = ESPHOME_PSTR("setRange %.2f %.2f");
break;
case PARAM_TRIG_RANGE:
first = values[NUMBER_TRIGGER_RANGE];
format = ESPHOME_PSTR("setTrigRange %.2f");
break;
case PARAM_SENSITIVITY:
first = values[NUMBER_HOLD_SENSITIVITY];
second = values[NUMBER_TRIGGER_SENSITIVITY];
format = ESPHOME_PSTR("setSensitivity %.0f %.0f");
break;
case PARAM_LATENCY:
first = values[NUMBER_ON_LATENCY];
second = values[NUMBER_OFF_LATENCY];
format = ESPHOME_PSTR("setLatency %.2f %.1f");
break;
case PARAM_INHIBIT:
first = values[NUMBER_INHIBIT_TIME];
format = ESPHOME_PSTR("setInhibit %.2f");
break;
case PARAM_THR_FACTOR:
first = values[NUMBER_THRESHOLD_FACTOR];
format = ESPHOME_PSTR("setThrFactor %.0f");
break;
default:
return false;
}
// Commands that take two values need both read from the module first
if (std::isnan(first) || std::isnan(second))
return false;
ESPHOME_snprintf_P(buf, len, format, first, second);
return true;
#else
return false;
#endif
}
bool DFRobotSEN0609Component::apply_read_(Param param) {
const float *r = this->response_;
const uint8_t count = this->response_count_;
// A change still waiting to be written is newer than what the module holds
const bool write_pending = (this->pending_writes_ & param_bit(param)) != 0;
#ifdef USE_NUMBER
// Checked for each number: publishing runs automations, which may change the next one
auto read_number = [this](NumberSetting setting, float value) {
if ((this->changed_numbers_ & (1U << setting)) == 0)
this->publish_number_(setting, value);
};
#endif
switch (param) {
#ifdef USE_NUMBER
case PARAM_RANGE:
if (count < 2)
return false;
read_number(NUMBER_MIN_RANGE, r[0]);
read_number(NUMBER_MAX_RANGE, r[1]);
return true;
case PARAM_TRIG_RANGE:
if (count < 1)
return false;
read_number(NUMBER_TRIGGER_RANGE, r[0]);
return true;
case PARAM_SENSITIVITY:
if (count < 2)
return false;
read_number(NUMBER_HOLD_SENSITIVITY, r[0]);
read_number(NUMBER_TRIGGER_SENSITIVITY, r[1]);
return true;
case PARAM_LATENCY:
if (count < 2)
return false;
read_number(NUMBER_ON_LATENCY, r[0]);
read_number(NUMBER_OFF_LATENCY, r[1]);
return true;
case PARAM_INHIBIT:
if (count < 1)
return false;
read_number(NUMBER_INHIBIT_TIME, r[0]);
return true;
case PARAM_THR_FACTOR:
if (count < 1)
return false;
read_number(NUMBER_THRESHOLD_FACTOR, r[0]);
return true;
#endif
#ifdef USE_SWITCH
case PARAM_MICRO_MOTION:
if (count < 1)
return false;
if (!write_pending)
this->publish_switch_(SWITCH_MICRO_MOTION, r[0] != 0.0f);
return true;
#endif
#ifdef USE_SELECT
case PARAM_GPIO_MODE:
// Response <pin> <level>
if (count < 2 || (r[1] != 0.0f && r[1] != 1.0f))
return false;
if (!write_pending) {
this->out_pin_level_ = static_cast<uint8_t>(r[1]);
this->out_pin_level_select_->publish_state(this->out_pin_level_);
}
return true;
#endif
case PARAM_SW_VERSION:
case PARAM_HW_VERSION:
// The versions are published as their lines arrive
return this->version_received_;
default:
return true;
}
}
void DFRobotSEN0609Component::finish_operation_() {
switch (this->operation_) {
case Operation::OPERATION_STOP:
this->stopped_ = true;
break;
case Operation::OPERATION_START:
this->stopped_ = false;
this->last_frame_ms_ = App.get_loop_component_start_time();
if (!this->sequence_failed_)
this->status_clear_warning();
this->sequence_failed_ = false;
break;
case Operation::OPERATION_WRITE:
this->deferred_writes_ &= ~param_bit(this->param_);
if (PERSISTENT_PARAMS & param_bit(this->param_))
this->save_pending_ = true;
#ifdef USE_SWITCH
if (is_led(this->param_)) {
const uint8_t bit = 1U << led_switch(this->param_);
this->led_known_ |= bit;
this->led_on_ = this->sent_led_on_ ? (this->led_on_ | bit) : (this->led_on_ & ~bit);
}
#endif
break;
case Operation::OPERATION_SAVE:
this->save_pending_ = false;
this->deferred_save_ = false;
break;
case Operation::OPERATION_READ:
if (!this->apply_read_(this->param_)) {
ESP_LOGW(TAG, "Malformed answer to '%s'", this->command_);
this->fail_operation_();
return;
}
this->pending_reads_ &= ~param_bit(this->param_);
this->paired_reads_ &= ~param_bit(this->param_);
break;
default:
return;
}
if (this->is_aux_operation_()) {
this->aux_attempts_ = 0;
} else {
this->attempts_ = 0;
}
this->operation_ = Operation::OPERATION_NONE;
this->hold_(COMMAND_GAP_MS);
}
void DFRobotSEN0609Component::fail_operation_() {
const bool aux = this->is_aux_operation_();
uint8_t &attempts = aux ? this->aux_attempts_ : this->attempts_;
const Operation operation = this->operation_;
this->operation_ = Operation::OPERATION_NONE;
this->hold_(COMMAND_GAP_MS);
const bool keep_write = operation == Operation::OPERATION_WRITE && (FRAME_PARAMS & param_bit(this->param_)) != 0;
const bool retry = ++attempts < MAX_ATTEMPTS;
if (operation == Operation::OPERATION_WRITE && (retry || keep_write))
this->pending_writes_ |= param_bit(this->param_);
if (retry)
return;
attempts = 0;
this->status_set_warning();
if (aux || keep_write) {
// Without stop, start or the configuration the sensor cannot run as set up; try again later
ESP_LOGW(TAG, "Command '%s' failed, trying again later", this->command_);
this->hold_ms_ = GIVE_UP_BACKOFF_MS;
return;
}
ESP_LOGW(TAG, "Giving up on %s '%s'", LOG_STR_ARG(operation_to_str(operation)), this->command_);
this->sequence_failed_ = true;
switch (operation) {
case Operation::OPERATION_WRITE:
#ifdef USE_SWITCH
if (is_led(this->param_)) {
// A newer change is written anyway
if ((this->pending_writes_ & param_bit(this->param_)) != 0)
break;
// The LEDs cannot be read: show the state the module last accepted, or write this one again later
const uint8_t bit = 1U << led_switch(this->param_);
if ((this->led_known_ & bit) != 0) {
this->publish_switch_(led_switch(this->param_), (this->led_on_ & bit) != 0);
} else {
this->deferred_writes_ |= param_bit(this->param_);
this->deferred_ms_ = App.get_loop_component_start_time();
}
break;
}
#endif
// The entity shows a value the module never got; show the module's value again
this->pending_reads_ |= this->wanted_reads_() & param_bit(this->param_);
break;
case Operation::OPERATION_SAVE:
// The entities show the changes as kept, but unsaved they are lost at power off: save in a later sequence
this->save_pending_ = false;
this->deferred_save_ = true;
this->deferred_ms_ = App.get_loop_component_start_time();
break;
case Operation::OPERATION_READ:
this->pending_reads_ &= ~param_bit(this->param_);
break;
default:
break;
}
}
} // namespace esphome::dfrobot_sen0609
@@ -0,0 +1,213 @@
#pragma once
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
#include "esphome/core/helpers.h"
#ifdef USE_BINARY_SENSOR
#include "esphome/components/binary_sensor/binary_sensor.h"
#endif
#ifdef USE_SENSOR
#include "esphome/components/sensor/sensor.h"
#endif
#ifdef USE_TEXT_SENSOR
#include "esphome/components/text_sensor/text_sensor.h"
#endif
#ifdef USE_NUMBER
#include "esphome/components/number/number.h"
#endif
#ifdef USE_SWITCH
#include "esphome/components/switch/switch.h"
#endif
#ifdef USE_SELECT
#include "esphome/components/select/select.h"
#endif
#include "esphome/components/uart/uart.h"
#include <array>
namespace esphome::dfrobot_sen0609 {
static constexpr uint8_t MAX_LINE_LENGTH = 80;
static constexpr uint8_t MAX_COMMAND_LENGTH = 40;
static constexpr uint8_t MAX_RESPONSE_VALUES = 3;
enum class RunMode : uint8_t {
RUN_MODE_PRESENCE = 0,
RUN_MODE_SPEED_AND_DISTANCE = 1,
};
/// Settings shown as number entities.
enum NumberSetting : uint8_t {
NUMBER_MIN_RANGE,
NUMBER_MAX_RANGE,
NUMBER_TRIGGER_RANGE,
NUMBER_HOLD_SENSITIVITY,
NUMBER_TRIGGER_SENSITIVITY,
NUMBER_ON_LATENCY,
NUMBER_OFF_LATENCY,
NUMBER_INHIBIT_TIME,
NUMBER_THRESHOLD_FACTOR,
NUMBER_COUNT,
};
/// Settings shown as switch entities.
enum SwitchSetting : uint8_t {
SWITCH_MICRO_MOTION,
SWITCH_RUN_LED,
SWITCH_OUT_LED,
SWITCH_COUNT,
};
/// Module parameters; a write or read of each is one command. Pending work runs in this order.
enum Param : uint8_t {
PARAM_RUN_APP,
PARAM_UART_OUTPUT,
PARAM_RANGE,
PARAM_TRIG_RANGE,
PARAM_SENSITIVITY,
PARAM_LATENCY,
PARAM_INHIBIT,
PARAM_THR_FACTOR,
PARAM_MICRO_MOTION,
PARAM_GPIO_MODE,
PARAM_RUN_LED,
PARAM_OUT_LED,
PARAM_SW_VERSION,
PARAM_HW_VERSION,
PARAM_COUNT,
};
enum class Operation : uint8_t {
OPERATION_NONE,
OPERATION_STOP,
OPERATION_START,
OPERATION_WRITE,
OPERATION_SAVE,
OPERATION_READ,
};
class DFRobotSEN0609Component final : public Component, public uart::UARTDevice {
#ifdef USE_BINARY_SENSOR
SUB_BINARY_SENSOR(has_target)
#endif
#ifdef USE_SENSOR
SUB_SENSOR(target_distance)
SUB_SENSOR(target_speed)
SUB_SENSOR(target_energy)
#endif
#ifdef USE_TEXT_SENSOR
SUB_TEXT_SENSOR(version)
SUB_TEXT_SENSOR(hardware_version)
#endif
#ifdef USE_SELECT
SUB_SELECT(out_pin_level)
#endif
public:
explicit DFRobotSEN0609Component(RunMode mode);
void setup() override;
void loop() override;
void dump_config() override;
#ifdef USE_NUMBER
void set_number(NumberSetting setting, number::Number *number) { this->numbers_[setting] = number; }
void control_number(NumberSetting setting, float value);
#endif
#ifdef USE_SWITCH
void set_switch(SwitchSetting setting, switch_::Switch *sw) { this->switches_[setting] = sw; }
void control_switch(SwitchSetting setting, bool state);
#endif
#ifdef USE_SELECT
void control_out_pin_level(size_t index);
#endif
protected:
void readline_(uint8_t c);
void handle_line_(char *line);
void handle_frame_(char *line);
void log_bad_frame_(const LogString *problem, const char *line);
void parse_response_(const char *args);
void queue_configuration_();
uint16_t wanted_reads_() const;
void request_write_(Param param);
void run_sequencer_();
void check_frames_(uint32_t now);
bool build_command_(Operation operation, Param param);
bool apply_read_(Param param);
void finish_operation_();
void fail_operation_();
void hold_(uint32_t duration_ms);
bool is_aux_operation_() const {
return this->operation_ == Operation::OPERATION_STOP || this->operation_ == Operation::OPERATION_START;
}
#ifdef USE_NUMBER
void publish_number_(NumberSetting setting, float value);
void forget_number_changes_(Param param, bool unknown);
#endif
#ifdef USE_SWITCH
void publish_switch_(SwitchSetting setting, bool state);
#endif
#ifdef USE_SENSOR
void publish_target_(float distance, float speed, float energy);
Deduplicator<float> distance_dedup_;
Deduplicator<float> speed_dedup_;
Deduplicator<float> energy_dedup_;
#endif
#ifdef USE_NUMBER
std::array<number::Number *, NUMBER_COUNT> numbers_{};
std::array<float, NUMBER_COUNT> number_values_;
#endif
#ifdef USE_SWITCH
std::array<switch_::Switch *, SWITCH_COUNT> switches_{};
std::array<bool, SWITCH_COUNT> switch_states_{};
#endif
char line_[MAX_LINE_LENGTH];
char command_[MAX_COMMAND_LENGTH]{};
float response_[MAX_RESPONSE_VALUES]{};
uint32_t sent_ms_{0};
uint32_t hold_start_ms_{0};
uint32_t hold_ms_{0};
uint32_t last_frame_ms_{0};
uint32_t last_change_ms_{0};
uint32_t deferred_ms_{0};
uint16_t pending_writes_{0};
uint16_t pending_reads_{0};
// Reads made for a write whose paired value is unknown; the write is dropped when the read fails
uint16_t paired_reads_{0};
// Writes given up on that are tried again in a later sequence
uint16_t deferred_writes_{0};
#ifdef USE_NUMBER
// Numbers changed since their last write was sent, one bit per NumberSetting
uint16_t changed_numbers_{0};
#endif
RunMode mode_;
Operation operation_{Operation::OPERATION_NONE};
Param param_{PARAM_COUNT};
uint8_t line_pos_{0};
uint8_t response_count_{0};
uint8_t out_pin_level_{0};
uint8_t attempts_{0};
uint8_t aux_attempts_{0};
#ifdef USE_SWITCH
// The module cannot be asked for its LED states: the ones it last accepted, one bit per SwitchSetting
uint8_t led_known_{0};
uint8_t led_on_{0};
bool sent_led_on_{false};
#endif
bool line_overflow_{false};
bool read_failed_{false};
bool bad_frame_logged_{false};
bool echo_seen_{false};
bool version_received_{false};
bool stopped_{false};
bool save_pending_{false};
bool deferred_save_{false};
bool sequence_failed_{false};
};
} // namespace esphome::dfrobot_sen0609
@@ -0,0 +1,194 @@
from typing import NamedTuple
import esphome.codegen as cg
from esphome.components import number
import esphome.config_validation as cv
from esphome.const import (
CONF_ID,
CONF_MAX_RANGE,
CONF_MIN_RANGE,
CONF_MODEL,
DEVICE_CLASS_DISTANCE,
DEVICE_CLASS_DURATION,
ENTITY_CATEGORY_CONFIG,
ICON_MOTION_SENSOR,
ICON_RULER,
ICON_TIMELAPSE,
UNIT_METER,
UNIT_SECOND,
)
from esphome.core import CORE
from esphome.types import ConfigType
from .. import (
CONF_DFROBOT_SEN0609_ID,
MAX_RANGES,
MODE_PRESENCE,
MODE_SPEED_AND_DISTANCE,
DFRobotSEN0609Component,
dfrobot_sen0609_ns,
final_validate_mode,
get_hub_config,
)
DFRobotSEN0609Number = dfrobot_sen0609_ns.class_("DFRobotSEN0609Number", number.Number)
NumberSetting = dfrobot_sen0609_ns.enum("NumberSetting")
CONF_HOLD_SENSITIVITY = "hold_sensitivity"
CONF_INHIBIT_TIME = "inhibit_time"
CONF_OFF_LATENCY = "off_latency"
CONF_ON_LATENCY = "on_latency"
CONF_THRESHOLD_FACTOR = "threshold_factor"
CONF_TRIGGER_RANGE = "trigger_range"
CONF_TRIGGER_SENSITIVITY = "trigger_sensitivity"
class Setting(NamedTuple):
key: str
setting: cg.MockObj
min_value: float
max_value: float | None # None: the model's longest range
step: float
mode: str | None # None: both modes
schema: cv.Schema
def _distance(icon: str = ICON_RULER) -> cv.Schema:
return number.number_schema(
DFRobotSEN0609Number,
device_class=DEVICE_CLASS_DISTANCE,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=icon,
unit_of_measurement=UNIT_METER,
)
def _duration() -> cv.Schema:
return number.number_schema(
DFRobotSEN0609Number,
device_class=DEVICE_CLASS_DURATION,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_TIMELAPSE,
unit_of_measurement=UNIT_SECOND,
)
def _plain() -> cv.Schema:
return number.number_schema(
DFRobotSEN0609Number,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_MOTION_SENSOR,
)
SETTINGS = (
Setting(
CONF_MIN_RANGE,
NumberSetting.NUMBER_MIN_RANGE,
0.6,
None,
0.1,
None,
_distance(),
),
Setting(
CONF_MAX_RANGE,
NumberSetting.NUMBER_MAX_RANGE,
0.6,
None,
0.1,
None,
_distance(),
),
Setting(
CONF_TRIGGER_RANGE,
NumberSetting.NUMBER_TRIGGER_RANGE,
0.6,
None,
0.1,
MODE_PRESENCE,
_distance(ICON_MOTION_SENSOR),
),
Setting(
CONF_HOLD_SENSITIVITY,
NumberSetting.NUMBER_HOLD_SENSITIVITY,
0,
9,
1,
MODE_PRESENCE,
_plain(),
),
Setting(
CONF_TRIGGER_SENSITIVITY,
NumberSetting.NUMBER_TRIGGER_SENSITIVITY,
0,
9,
1,
MODE_PRESENCE,
_plain(),
),
Setting(
CONF_ON_LATENCY,
NumberSetting.NUMBER_ON_LATENCY,
0,
2,
0.01,
MODE_PRESENCE,
_duration(),
),
Setting(
CONF_OFF_LATENCY,
NumberSetting.NUMBER_OFF_LATENCY,
2,
1500,
0.5,
MODE_PRESENCE,
_duration(),
),
Setting(
CONF_INHIBIT_TIME,
NumberSetting.NUMBER_INHIBIT_TIME,
0.1,
60,
0.1,
MODE_PRESENCE,
_duration(),
),
Setting(
CONF_THRESHOLD_FACTOR,
NumberSetting.NUMBER_THRESHOLD_FACTOR,
0,
65535,
1,
MODE_SPEED_AND_DISTANCE,
_plain(),
),
)
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(CONF_ID): cv.declare_id(cg.EntityBase),
cv.GenerateID(CONF_DFROBOT_SEN0609_ID): cv.use_id(DFRobotSEN0609Component),
**{cv.Optional(s.key): s.schema for s in SETTINGS},
}
)
FINAL_VALIDATE_SCHEMA = final_validate_mode(
{s.key: s.mode for s in SETTINGS if s.mode is not None}
)
async def to_code(config: ConfigType) -> None:
hub_id = config[CONF_DFROBOT_SEN0609_ID]
hub = await cg.get_variable(hub_id)
max_range = MAX_RANGES[get_hub_config(CORE.config, hub_id)[CONF_MODEL]]
numbers = number.sub_numbers(config, parent=hub)
for s in SETTINGS:
await numbers(
s.key,
lambda n, setting=s.setting: hub.set_number(setting, n),
s.setting,
min_value=s.min_value,
max_value=max_range if s.max_value is None else s.max_value,
step=s.step,
)
@@ -0,0 +1,18 @@
#pragma once
#include "esphome/components/number/number.h"
#include "../dfrobot_sen0609.h"
namespace esphome::dfrobot_sen0609 {
class DFRobotSEN0609Number final : public number::Number, public Parented<DFRobotSEN0609Component> {
public:
explicit DFRobotSEN0609Number(NumberSetting setting) : setting_(setting) {}
protected:
void control(float value) override { this->parent_->control_number(this->setting_, value); }
NumberSetting setting_;
};
} // namespace esphome::dfrobot_sen0609
@@ -0,0 +1,29 @@
import esphome.codegen as cg
from esphome.components import select
from esphome.components.const import CONF_OUT_PIN_LEVEL
import esphome.config_validation as cv
from esphome.const import CONF_ID, ENTITY_CATEGORY_CONFIG, ICON_SCALE
from esphome.types import ConfigType
from .. import CONF_DFROBOT_SEN0609_ID, DFRobotSEN0609Component, dfrobot_sen0609_ns
OutPinLevelSelect = dfrobot_sen0609_ns.class_("OutPinLevelSelect", select.Select)
CONFIG_SCHEMA = {
cv.GenerateID(CONF_ID): cv.declare_id(cg.EntityBase),
cv.GenerateID(CONF_DFROBOT_SEN0609_ID): cv.use_id(DFRobotSEN0609Component),
cv.Optional(CONF_OUT_PIN_LEVEL): select.select_schema(
OutPinLevelSelect,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_SCALE,
),
}
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_DFROBOT_SEN0609_ID])
selects = select.sub_selects(config, parent=hub)
# The level the OUT pin has while a target is present; the index is the module's value
await selects(
CONF_OUT_PIN_LEVEL, hub.set_out_pin_level_select, options=["low", "high"]
)
@@ -0,0 +1,17 @@
#pragma once
#include "esphome/components/select/select.h"
#include "../dfrobot_sen0609.h"
namespace esphome::dfrobot_sen0609 {
class OutPinLevelSelect final : public select::Select, public Parented<DFRobotSEN0609Component> {
public:
// User provided, not "= default": `new(p) OutPinLevelSelect()` would zero-fill .bss that is already zero.
OutPinLevelSelect() {}
protected:
void control(size_t index) override { this->parent_->control_out_pin_level(index); }
};
} // namespace esphome::dfrobot_sen0609
@@ -0,0 +1,64 @@
import esphome.codegen as cg
from esphome.components import sensor
import esphome.config_validation as cv
from esphome.const import (
CONF_ID,
DEVICE_CLASS_DISTANCE,
DEVICE_CLASS_SPEED,
ICON_SIGNAL,
STATE_CLASS_MEASUREMENT,
UNIT_METER,
UNIT_METER_PER_SECOND,
)
from esphome.types import ConfigType
from . import (
CONF_DFROBOT_SEN0609_ID,
MODE_SPEED_AND_DISTANCE,
DFRobotSEN0609Component,
final_validate_mode,
)
DEPENDENCIES = ["dfrobot_sen0609"]
CONF_TARGET_DISTANCE = "target_distance"
CONF_TARGET_ENERGY = "target_energy"
CONF_TARGET_SPEED = "target_speed"
CONFIG_SCHEMA = {
cv.GenerateID(CONF_ID): cv.declare_id(cg.EntityBase),
cv.GenerateID(CONF_DFROBOT_SEN0609_ID): cv.use_id(DFRobotSEN0609Component),
cv.Optional(CONF_TARGET_DISTANCE): sensor.sensor_schema(
accuracy_decimals=2,
device_class=DEVICE_CLASS_DISTANCE,
state_class=STATE_CLASS_MEASUREMENT,
unit_of_measurement=UNIT_METER,
),
cv.Optional(CONF_TARGET_SPEED): sensor.sensor_schema(
accuracy_decimals=2,
device_class=DEVICE_CLASS_SPEED,
state_class=STATE_CLASS_MEASUREMENT,
unit_of_measurement=UNIT_METER_PER_SECOND,
),
cv.Optional(CONF_TARGET_ENERGY): sensor.sensor_schema(
accuracy_decimals=0,
icon=ICON_SIGNAL,
state_class=STATE_CLASS_MEASUREMENT,
),
}
FINAL_VALIDATE_SCHEMA = final_validate_mode(
{
CONF_TARGET_DISTANCE: MODE_SPEED_AND_DISTANCE,
CONF_TARGET_ENERGY: MODE_SPEED_AND_DISTANCE,
CONF_TARGET_SPEED: MODE_SPEED_AND_DISTANCE,
}
)
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_DFROBOT_SEN0609_ID])
sensors = sensor.sub_sensors(config)
await sensors(CONF_TARGET_DISTANCE, hub.set_target_distance_sensor)
await sensors(CONF_TARGET_SPEED, hub.set_target_speed_sensor)
await sensors(CONF_TARGET_ENERGY, hub.set_target_energy_sensor)
@@ -0,0 +1,68 @@
import esphome.codegen as cg
from esphome.components import switch
import esphome.config_validation as cv
from esphome.const import (
CONF_ID,
DEVICE_CLASS_SWITCH,
ENTITY_CATEGORY_CONFIG,
ICON_LIGHTBULB,
ICON_MOTION_SENSOR,
)
from esphome.types import ConfigType
from .. import (
CONF_DFROBOT_SEN0609_ID,
MODE_SPEED_AND_DISTANCE,
DFRobotSEN0609Component,
dfrobot_sen0609_ns,
final_validate_mode,
)
DFRobotSEN0609Switch = dfrobot_sen0609_ns.class_("DFRobotSEN0609Switch", switch.Switch)
SwitchSetting = dfrobot_sen0609_ns.enum("SwitchSetting")
CONF_MICRO_MOTION = "micro_motion"
CONF_OUT_LED = "out_led"
CONF_RUN_LED = "run_led"
def _led() -> cv.Schema:
# The module forgets the LED state at power off, so it is restored from here
return switch.switch_schema(
DFRobotSEN0609Switch,
default_restore_mode="RESTORE_DEFAULT_ON",
device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_LIGHTBULB,
)
CONFIG_SCHEMA = {
cv.GenerateID(CONF_ID): cv.declare_id(cg.EntityBase),
cv.GenerateID(CONF_DFROBOT_SEN0609_ID): cv.use_id(DFRobotSEN0609Component),
cv.Optional(CONF_MICRO_MOTION): switch.switch_schema(
DFRobotSEN0609Switch,
device_class=DEVICE_CLASS_SWITCH,
entity_category=ENTITY_CATEGORY_CONFIG,
icon=ICON_MOTION_SENSOR,
),
cv.Optional(CONF_RUN_LED): _led(),
cv.Optional(CONF_OUT_LED): _led(),
}
FINAL_VALIDATE_SCHEMA = final_validate_mode(
{CONF_MICRO_MOTION: MODE_SPEED_AND_DISTANCE}
)
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_DFROBOT_SEN0609_ID])
switches = switch.sub_switches(config, parent=hub)
for key, setting in (
(CONF_MICRO_MOTION, SwitchSetting.SWITCH_MICRO_MOTION),
(CONF_RUN_LED, SwitchSetting.SWITCH_RUN_LED),
(CONF_OUT_LED, SwitchSetting.SWITCH_OUT_LED),
):
await switches(
key, lambda s, setting=setting: hub.set_switch(setting, s), setting
)
@@ -0,0 +1,18 @@
#pragma once
#include "esphome/components/switch/switch.h"
#include "../dfrobot_sen0609.h"
namespace esphome::dfrobot_sen0609 {
class DFRobotSEN0609Switch final : public switch_::Switch, public Parented<DFRobotSEN0609Component> {
public:
explicit DFRobotSEN0609Switch(SwitchSetting setting) : setting_(setting) {}
protected:
void write_state(bool state) override { this->parent_->control_switch(this->setting_, state); }
SwitchSetting setting_;
};
} // namespace esphome::dfrobot_sen0609
@@ -0,0 +1,29 @@
import esphome.codegen as cg
from esphome.components import text_sensor
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_VERSION, ENTITY_CATEGORY_DIAGNOSTIC, ICON_CHIP
from esphome.types import ConfigType
from . import CONF_DFROBOT_SEN0609_ID, DFRobotSEN0609Component
DEPENDENCIES = ["dfrobot_sen0609"]
CONF_HARDWARE_VERSION = "hardware_version"
CONFIG_SCHEMA = {
cv.GenerateID(CONF_ID): cv.declare_id(cg.EntityBase),
cv.GenerateID(CONF_DFROBOT_SEN0609_ID): cv.use_id(DFRobotSEN0609Component),
cv.Optional(CONF_VERSION): text_sensor.text_sensor_schema(
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, icon=ICON_CHIP
),
cv.Optional(CONF_HARDWARE_VERSION): text_sensor.text_sensor_schema(
entity_category=ENTITY_CATEGORY_DIAGNOSTIC, icon=ICON_CHIP
),
}
async def to_code(config: ConfigType) -> None:
hub = await cg.get_variable(config[CONF_DFROBOT_SEN0609_ID])
text_sensors = text_sensor.sub_text_sensors(config)
await text_sensors(CONF_VERSION, hub.set_version_text_sensor)
await text_sensors(CONF_HARDWARE_VERSION, hub.set_hardware_version_text_sensor)
-1
View File
@@ -843,7 +843,6 @@ constexpr uint8_t TESTCARD_FONT[3][8] PROGMEM = {{0x41, 0x7F, 0x7F, 0x09, 0x19,
void Display::test_card() {
int w = get_width(), h = get_height(), image_w, image_h;
this->clear();
this->show_test_card_ = false;
image_w = std::min(w - 20, 310);
image_h = std::min(h - 20, 255);
int shift_x = (w - image_w) / 2;
+2 -1
View File
@@ -357,7 +357,8 @@ class Display : public PollingComponent {
* \param ptr A pointer to the start of the data to be copied
* \param order The ordering of the colors
* \param bitness Defines the number of bits and their format for each pixel
* \param big_endian True if 16 bit values are stored big-endian
* \param big_endian True if 16 bit values are stored big-endian. Ignored for 888, which is always b,g,r
* in memory for RGB order, as LVGL and ESPHome images store it.
* \param x_offset The initial x-offset into the source buffer.
* \param y_offset The initial y-offset into the source buffer.
* \param x_pad How many pixels are in each line after the end of the pixels to be copied.
@@ -23,9 +23,8 @@ class ColorUtil {
return (ptr[idx] << 8) + ptr[idx + 1];
return ptr[idx] + (ptr[idx + 1] << 8);
} else if constexpr (BITNESS == COLOR_BITNESS_888) {
// Always the LVGL RGB888 layout, b,g,r in memory for RGB order; byte order applies to 565 only
const size_t idx = index * 3;
if (big_endian)
return (ptr[idx + 0] << 16) + (ptr[idx + 1] << 8) + ptr[idx + 2];
return ptr[idx + 0] + (ptr[idx + 1] << 8) + (ptr[idx + 2] << 16);
} else {
return ptr[index];
+1
View File
@@ -203,6 +203,7 @@ def _final_validate(config) -> None:
config[CONF_UPDATE_INTERVAL] = update_interval("never")
else:
config[CONF_SHOW_TEST_CARD] = True
config[CONF_UPDATE_INTERVAL] = update_interval("1min")
elif CONF_UPDATE_INTERVAL not in config:
config[CONF_UPDATE_INTERVAL] = update_interval("1min")
+1 -1
View File
@@ -468,7 +468,7 @@ void HOT EPaperBase::draw_pixels_at(int x_start, int y_start, int w, int h, cons
ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) {
size_t contiguous_length;
uint8_t *contiguous = this->buffer_.get_span(0, contiguous_length);
if (!this->buffer_is_mono() || this->get_clipping().is_set() || bitness != COLOR_BITNESS_565 ||
if (!this->buffer_is_mono_() || this->get_clipping().is_set() || bitness != COLOR_BITNESS_565 ||
contiguous_length != this->buffer_length_) {
Display::draw_pixels_at(x_start, y_start, w, h, ptr, order, bitness, big_endian, x_offset, y_offset, x_pad);
return;
+13 -8
View File
@@ -38,15 +38,18 @@ class EPaperBase : public Display,
spi::DATA_RATE_2MHZ> {
public:
EPaperBase(const char *name, uint16_t width, uint16_t height, const uint8_t *init_sequence = nullptr,
size_t init_sequence_length = 0, DisplayType display_type = DISPLAY_TYPE_BINARY)
size_t init_sequence_length = 0, DisplayType display_type = DISPLAY_TYPE_BINARY,
uint8_t bits_per_pixel = 1, uint8_t planes = 1)
: name_(name),
width_(width),
row_width_((width * bits_per_pixel + 7) / 8),
height_(height),
bits_per_pixel_(bits_per_pixel),
planes_(planes),
init_sequence_(init_sequence),
init_sequence_length_(init_sequence_length),
display_type_(display_type) {
this->row_width_ = (this->width_ + 7) / 8; // width of a row in bytes
}
display_type_(display_type),
buffer_length_((size_t) this->row_width_ * height * planes) {}
void set_dc_pin(GPIOPin *dc_pin) { dc_pin_ = dc_pin; }
float get_setup_priority() const override;
void set_reset_pin(GPIOPin *reset) { this->reset_pin_ = reset; }
@@ -134,8 +137,8 @@ class EPaperBase : public Display,
bool rotate_coordinates_(int &x, int &y);
void draw_rgb565_block_(uint8_t *buffer, int x_start, int y_start, int w, int h, const uint8_t *ptr, ColorOrder order,
bool big_endian, int x_offset, int y_offset, int x_pad);
// Whether the buffer holds 1 bit per pixel as draw_pixel_at() writes it, so draw_pixels_at() can write it directly
virtual bool buffer_is_mono() const { return false; }
// Whether the buffer is a single plane of 1 bit per pixel, as draw_pixel_at() writes it
bool buffer_is_mono_() const { return this->bits_per_pixel_ == 1 && this->planes_ == 1; }
/**
* Methods that must be implemented by concrete classes to control the display
@@ -173,13 +176,15 @@ class EPaperBase : public Display,
// properties initialised in the constructor
const char *name_;
uint16_t width_;
uint16_t row_width_; // width of a row in bytes
uint16_t row_width_; // width of a row of one plane in bytes
uint16_t height_;
uint8_t bits_per_pixel_;
uint8_t planes_; // the buffer holds this many planes of row_width_ * height_ bytes, one after another
const uint8_t *init_sequence_;
size_t init_sequence_length_;
DisplayType display_type_;
size_t buffer_length_{};
size_t buffer_length_;
size_t current_data_index_{}; // used by data transfer to track progress
split_buffer::SplitBuffer buffer_{};
GPIOPin *dc_pin_{};
@@ -15,9 +15,7 @@ class EPaper4bpp : public EPaperBase {
public:
EPaper4bpp(const char *name, uint16_t width, uint16_t height, const uint8_t *init_sequence,
size_t init_sequence_length)
: EPaperBase(name, width, height, init_sequence, init_sequence_length, DISPLAY_TYPE_COLOR) {
this->buffer_length_ = width * height / 2; // 2 pixels per byte
}
: EPaperBase(name, width, height, init_sequence, init_sequence_length, DISPLAY_TYPE_COLOR, 4) {}
void fill(Color color) override;
void clear() override;
@@ -9,10 +9,7 @@ class EPaperInkplate2 final : public EPaperBase {
public:
EPaperInkplate2(const char *name, uint16_t width, uint16_t height, const uint8_t *init_sequence,
size_t init_sequence_length)
: EPaperBase(name, width, height, init_sequence, init_sequence_length, DISPLAY_TYPE_COLOR) {
// Dual-plane buffer: black/white plane followed by red plane, 1 bit per pixel each.
this->buffer_length_ = this->row_width_ * this->height_ * 2;
}
: EPaperBase(name, width, height, init_sequence, init_sequence_length, DISPLAY_TYPE_COLOR, 1, 2) {}
void fill(Color color) override;
void clear() override;
@@ -27,12 +27,9 @@ class EPaperJD79660 final : public EPaperBase {
public:
EPaperJD79660(const char *name, uint16_t width, uint16_t height, const uint8_t *init_sequence,
size_t init_sequence_length, const uint8_t *fast_update, uint16_t fast_update_length)
: EPaperBase(name, width, height, init_sequence, init_sequence_length, DISPLAY_TYPE_COLOR),
: EPaperBase(name, width, height, init_sequence, init_sequence_length, DISPLAY_TYPE_COLOR, 2),
fast_update_(fast_update),
fast_update_length_(fast_update_length) {
this->row_width_ = (width + 3) / 4; // Fix base class calc (2bpp instead of 1bpp)
this->buffer_length_ = this->row_width_ * height;
}
fast_update_length_(fast_update_length) {}
void fill(Color color) override;
@@ -9,13 +9,10 @@ namespace esphome::epaper_spi {
class EPaperMono : public EPaperBase {
public:
EPaperMono(const char *name, uint16_t width, uint16_t height, const uint8_t *init_sequence,
size_t init_sequence_length, DisplayType display_type = DISPLAY_TYPE_BINARY)
: EPaperBase(name, width, height, init_sequence, init_sequence_length, display_type) {
this->buffer_length_ = (width + 7) / 8 * height; // 8 pixels per byte, rounded up
}
size_t init_sequence_length, DisplayType display_type = DISPLAY_TYPE_BINARY, uint8_t bits_per_pixel = 1)
: EPaperBase(name, width, height, init_sequence, init_sequence_length, display_type, bits_per_pixel) {}
protected:
bool buffer_is_mono() const override { return true; }
void refresh_screen(bool partial) override;
void power_on() override {}
void power_off() override{};
@@ -58,9 +58,9 @@ bool HOT EPaperSSD1677::transfer_data() {
// A full update ignores 0x26, and the copy may not hold a real frame yet (first update after
// boot): send the new frame to both planes.
const bool send_copy = this->plane_ == 0 && this->update_count_ != 0;
// A mono buffer already holds the frame as a RAM plane does, so it can be sent as it is; a deeper
// buffer (the four-level model) returns false and packs the plane in plane_row().
const bool direct = send_copy || this->buffer_is_mono();
// A 1 bit per pixel buffer already holds the frame as a RAM plane does, so it can be sent as it is; a
// deeper buffer (the four-level model, 2 bits per pixel) packs the plane in plane_row() instead.
const bool direct = send_copy || this->buffer_is_mono_();
const auto &source = send_copy ? this->sent_ : this->buffer_;
const size_t row_length = this->plane_row_length_();
const size_t plane_length = row_length * this->height_;
@@ -23,8 +23,8 @@ namespace esphome::epaper_spi {
class EPaperSSD1677 : public EPaperMono {
public:
EPaperSSD1677(const char *name, uint16_t width, uint16_t height, const uint8_t *init_sequence,
size_t init_sequence_length, DisplayType display_type = DISPLAY_TYPE_BINARY)
: EPaperMono(name, width, height, init_sequence, init_sequence_length, display_type) {}
size_t init_sequence_length, DisplayType display_type = DISPLAY_TYPE_BINARY, uint8_t bits_per_pixel = 1)
: EPaperMono(name, width, height, init_sequence, init_sequence_length, display_type, bits_per_pixel) {}
void setup() override;
@@ -29,16 +29,12 @@ class EPaperSSD1677Gray4 : public EPaperSSD1677 {
public:
EPaperSSD1677Gray4(const char *name, uint16_t width, uint16_t height, const uint8_t *init_sequence,
size_t init_sequence_length)
: EPaperSSD1677(name, width, height, init_sequence, init_sequence_length, DISPLAY_TYPE_GRAYSCALE) {
this->row_width_ = (width + 3) / 4; // 4 pixels per byte
this->buffer_length_ = (size_t) this->row_width_ * height;
}
: EPaperSSD1677(name, width, height, init_sequence, init_sequence_length, DISPLAY_TYPE_GRAYSCALE, 2) {}
void fill(Color color) override;
void draw_pixel_at(int x, int y, Color color) override;
protected:
bool buffer_is_mono() const override { return false; }
// A partial update if partial updates are enabled and this is not a full one.
bool is_partial_push_() const { return this->update_count_ != 0 && this->sent_.is_valid(); }
void plane_row(size_t y, uint8_t *out) override;
@@ -22,9 +22,7 @@ class EPaperT133A01 : public EPaperBase {
public:
EPaperT133A01(const char *name, uint16_t width, uint16_t height, const uint8_t *init_sequence,
size_t init_sequence_length)
: EPaperBase(name, width, height, init_sequence, init_sequence_length, DISPLAY_TYPE_COLOR) {
this->buffer_length_ = (size_t) width * height / 2; // 2 pixels per byte at 4bpp
}
: EPaperBase(name, width, height, init_sequence, init_sequence_length, DISPLAY_TYPE_COLOR, 4) {}
void set_cs_pins(GPIOPin *cs, GPIOPin *cs1) {
this->cs_pin_ = cs;
@@ -32,12 +32,9 @@ class EPaperUC8179 final : public EPaperBase {
public:
EPaperUC8179(const char *name, uint16_t width, uint16_t height, const uint8_t *init_sequence,
size_t init_sequence_length)
: EPaperBase(name, width, height, init_sequence, init_sequence_length, DISPLAY_TYPE_BINARY) {
this->buffer_length_ = this->row_width_ * height;
}
: EPaperBase(name, width, height, init_sequence, init_sequence_length, DISPLAY_TYPE_BINARY) {}
protected:
bool buffer_is_mono() const override { return true; }
bool initialise(bool partial) override;
bool transfer_data() override;
void refresh_screen(bool partial) override;
@@ -22,9 +22,7 @@ class EPaperWaveshareBWR : public EPaperBase {
public:
EPaperWaveshareBWR(const char *name, uint16_t width, uint16_t height, const uint8_t *init_sequence,
size_t init_sequence_length)
: EPaperBase(name, width, height, init_sequence, init_sequence_length, DISPLAY_TYPE_BINARY) {
this->buffer_length_ = this->row_width_ * height * 2;
}
: EPaperBase(name, width, height, init_sequence, init_sequence_length, DISPLAY_TYPE_BINARY, 1, 2) {}
void fill(Color color) override;
@@ -18,9 +18,7 @@ class EPaperWeAct3C : public EPaperBase {
public:
EPaperWeAct3C(const char *name, uint16_t width, uint16_t height, const uint8_t *init_sequence,
size_t init_sequence_length)
: EPaperBase(name, width, height, init_sequence, init_sequence_length, DISPLAY_TYPE_BINARY) {
this->buffer_length_ = this->row_width_ * height * 2;
}
: EPaperBase(name, width, height, init_sequence, init_sequence_length, DISPLAY_TYPE_BINARY, 1, 2) {}
void fill(Color color) override;
void clear() override;
@@ -48,6 +48,7 @@ class BLEService final {
bool is_created() { return this->state_ == CREATED; }
bool is_running() { return this->state_ == RUNNING; }
bool is_starting() { return this->state_ == STARTING; }
bool is_stopped() { return this->state_ == STOPPED; }
bool is_deleted() { return this->state_ == DELETED; }
protected:
+2
View File
@@ -57,6 +57,7 @@ from .const import (
PREFERENCES_RTC_NORMAL_WORDS,
THROW_STUBS_HEADER,
check_preferences_fit,
check_timer1_users,
enable_serial,
enable_serial1,
esp8266_ns,
@@ -540,6 +541,7 @@ async def to_code(config: ConfigType) -> None:
CORE.add_job(finalize_waveform_config)
CORE.add_job(finalize_serial_config)
CORE.add_job(check_preferences_fit)
CORE.add_job(check_timer1_users)
@coroutine_with_priority(CoroPriority.WORKAROUNDS)
+23
View File
@@ -17,6 +17,7 @@ CONF_EARLY_PIN_INIT = "early_pin_init"
CONF_ENABLE_SERIAL = "enable_serial"
CONF_ENABLE_SERIAL1 = "enable_serial1"
KEY_WAVEFORM_REQUIRED = "waveform_required"
KEY_TIMER1_USERS = "timer1_users"
KEY_SERIAL_REQUIRED = "serial_required"
KEY_SERIAL1_REQUIRED = "serial1_required"
# Set for the native (non-PlatformIO) toolchain's build generator
@@ -129,6 +130,28 @@ def require_waveform() -> None:
require_waveform()
"""
CORE.data.setdefault(KEY_ESP8266, {})[KEY_WAVEFORM_REQUIRED] = True
use_timer1("the Arduino waveform generator (esp8266_pwm, ac_dimmer)")
def use_timer1(owner: str) -> None:
"""Record that a component drives hardware timer 1; check_timer1_users() rejects a second one.
The ESP8266 has one general purpose timer with one interrupt. Two components that program it
take it from each other, so the configuration is refused instead. Components that go through
the Arduino waveform generator share it and register through require_waveform().
"""
users = CORE.data.setdefault(KEY_ESP8266, {}).setdefault(KEY_TIMER1_USERS, set())
users.add(owner)
@coroutine_with_priority(CoroPriority.FINAL)
async def check_timer1_users() -> None:
"""Fail the build when more than one component drives hardware timer 1."""
users = sorted(CORE.data[KEY_ESP8266].get(KEY_TIMER1_USERS, ()))
if len(users) > 1:
raise EsphomeError(
f"{' and '.join(users)} each need hardware timer 1 of the ESP8266, which only one of them can drive"
)
def enable_serial() -> None:
@@ -2,6 +2,7 @@
#if defined(USE_ETHERNET) && defined(USE_ESP32)
#include "esphome/components/network/util.h"
#include "esphome/core/application.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
@@ -731,14 +732,7 @@ network::IPAddresses EthernetComponent::get_ip_addresses() {
addresses[0] = network::IPAddress(&ip.ip);
}
#if USE_NETWORK_IPV6
struct esp_ip6_addr if_ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
uint8_t count = 0;
count = esp_netif_get_all_ip6(this->eth_netif_, if_ip6s);
assert(count <= CONFIG_LWIP_IPV6_NUM_ADDRESSES);
assert(count < addresses.size());
for (int i = 0; i < count; i++) {
addresses[i + 1] = network::IPAddress(&if_ip6s[i]);
}
network::fill_ip6_addresses(this->eth_netif_, addresses);
#endif /* USE_NETWORK_IPV6 */
return addresses;
+95 -3
View File
@@ -1,19 +1,43 @@
from esphome import pins
from esphome import automation, pins
import esphome.codegen as cg
from esphome.components import i2c
from esphome.components import i2c, sensor, text_sensor
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_INTERRUPT_PIN
from esphome.const import (
CONF_CURRENT,
CONF_ID,
CONF_INTERRUPT_PIN,
CONF_ON_CONNECT,
CONF_ON_DISCONNECT,
CONF_ON_ERROR,
CONF_VOLTAGE,
DEVICE_CLASS_CURRENT,
DEVICE_CLASS_VOLTAGE,
ENTITY_CATEGORY_DIAGNOSTIC,
ICON_FLASH,
STATE_CLASS_MEASUREMENT,
UNIT_AMPERE,
UNIT_VOLT,
)
from esphome.types import ConfigType
AUTO_LOAD = ["sensor", "text_sensor"]
CODEOWNERS = ["@remcom"]
DEPENDENCIES = ["i2c"]
DOMAIN = "fusb302b"
CONF_CONTRACT = "contract"
CONF_ON_POWER_READY = "on_power_ready"
CONF_ON_REQUEST_FAILED = "on_request_failed"
CONF_REQUEST_VOLTAGE = "request_voltage"
fusb302b_ns = cg.esphome_ns.namespace("fusb302b")
FUSB302B = fusb302b_ns.class_("FUSB302B", cg.Component, i2c.I2CDevice)
ConnectForwarder = fusb302b_ns.class_("ConnectForwarder")
DisconnectForwarder = fusb302b_ns.class_("DisconnectForwarder")
PowerReadyForwarder = fusb302b_ns.class_("PowerReadyForwarder")
ErrorForwarder = fusb302b_ns.class_("ErrorForwarder")
# USB PD fixed supply voltages are 5, 9, 12, 15 and 20 V; the highest one that does not exceed the
# requested voltage is chosen.
validate_voltage = cv.int_range(min=5, max=20)
@@ -24,6 +48,29 @@ CONFIG_SCHEMA = cv.All(
cv.GenerateID(): cv.declare_id(FUSB302B),
cv.Required(CONF_INTERRUPT_PIN): pins.internal_gpio_input_pullup_pin_schema,
cv.Required(CONF_REQUEST_VOLTAGE): validate_voltage,
cv.Optional(CONF_CONTRACT): text_sensor.text_sensor_schema(
icon=ICON_FLASH,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
cv.Optional(CONF_VOLTAGE): sensor.sensor_schema(
unit_of_measurement=UNIT_VOLT,
accuracy_decimals=1,
device_class=DEVICE_CLASS_VOLTAGE,
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
cv.Optional(CONF_CURRENT): sensor.sensor_schema(
unit_of_measurement=UNIT_AMPERE,
accuracy_decimals=2,
device_class=DEVICE_CLASS_CURRENT,
state_class=STATE_CLASS_MEASUREMENT,
entity_category=ENTITY_CATEGORY_DIAGNOSTIC,
),
cv.Optional(CONF_ON_CONNECT): automation.validate_automation({}),
cv.Optional(CONF_ON_DISCONNECT): automation.validate_automation({}),
cv.Optional(CONF_ON_POWER_READY): automation.validate_automation({}),
cv.Optional(CONF_ON_ERROR): automation.validate_automation({}),
cv.Optional(CONF_ON_REQUEST_FAILED): automation.validate_automation({}),
}
)
.extend(cv.COMPONENT_SCHEMA)
@@ -31,6 +78,24 @@ CONFIG_SCHEMA = cv.All(
cv.only_on_esp32,
)
_CALLBACK_AUTOMATIONS = (
automation.CallbackAutomation(
CONF_ON_CONNECT, "add_on_state_callback", forwarder=ConnectForwarder
),
automation.CallbackAutomation(
CONF_ON_DISCONNECT, "add_on_state_callback", forwarder=DisconnectForwarder
),
automation.CallbackAutomation(
CONF_ON_POWER_READY, "add_on_state_callback", forwarder=PowerReadyForwarder
),
automation.CallbackAutomation(
CONF_ON_ERROR, "add_on_state_callback", forwarder=ErrorForwarder
),
automation.CallbackAutomation(
CONF_ON_REQUEST_FAILED, "add_on_request_failed_callback"
),
)
async def to_code(config: ConfigType) -> None:
interrupt_pin = await cg.gpio_pin_expression(config[CONF_INTERRUPT_PIN])
@@ -38,3 +103,30 @@ async def to_code(config: ConfigType) -> None:
await cg.register_component(var, config)
await i2c.register_i2c_device(var, config)
cg.add(var.set_request_voltage(config[CONF_REQUEST_VOLTAGE]))
sensors = sensor.sub_sensors(config)
await sensors(CONF_VOLTAGE, var.set_voltage_sensor)
await sensors(CONF_CURRENT, var.set_current_sensor)
text_sensors = text_sensor.sub_text_sensors(config)
await text_sensors(CONF_CONTRACT, var.set_contract_text_sensor)
await automation.build_callback_automations(var, config, _CALLBACK_AUTOMATIONS)
automation.register_apply_action(
"fusb302b.request_voltage",
cv.maybe_simple_value(
{
cv.GenerateID(): cv.use_id(FUSB302B),
cv.Required(CONF_VOLTAGE): cv.templatable(validate_voltage),
},
key=CONF_VOLTAGE,
),
automation.ApplyField(CONF_VOLTAGE, "request_voltage", cg.int_),
)
automation.register_apply_condition(
"fusb302b.is_connected",
automation.maybe_simple_id({cv.GenerateID(): cv.use_id(FUSB302B)}),
"is_connected()",
)
+55
View File
@@ -0,0 +1,55 @@
#pragma once
#include <type_traits>
#include "esphome/core/automation.h"
#include "pd.h"
namespace esphome::fusb302b {
/// Triggers when a source is attached, including after recovering from a communication error.
struct ConnectForwarder {
Automation<> *automation;
void operator()(PdState state, PdState previous) const {
if (is_connected_state(state) && !is_connected_state(previous))
this->automation->trigger();
}
};
/// Triggers when the source is removed, or is no longer known to be attached because communication failed.
struct DisconnectForwarder {
Automation<> *automation;
void operator()(PdState state, PdState previous) const {
if (!is_connected_state(state) && is_connected_state(previous))
this->automation->trigger();
}
};
/// Triggers when negotiation is finished: a new PD contract is in place, or the source does not speak PD.
struct PowerReadyForwarder {
Automation<> *automation;
void operator()(PdState state, PdState /*previous*/) const {
if (state == PdState::PD_STATE_EXPLICIT_CONTRACT || state == PdState::PD_STATE_PD_TIMEOUT)
this->automation->trigger();
}
};
/// Triggers when communication with the chip fails.
struct ErrorForwarder {
Automation<> *automation;
void operator()(PdState state, PdState previous) const {
if (state == PdState::PD_STATE_ERROR && previous != PdState::PD_STATE_ERROR)
this->automation->trigger();
}
};
static_assert(sizeof(ConnectForwarder) <= sizeof(void *));
static_assert(std::is_trivially_copyable_v<ConnectForwarder>);
static_assert(sizeof(DisconnectForwarder) <= sizeof(void *));
static_assert(std::is_trivially_copyable_v<DisconnectForwarder>);
static_assert(sizeof(PowerReadyForwarder) <= sizeof(void *));
static_assert(std::is_trivially_copyable_v<PowerReadyForwarder>);
static_assert(sizeof(ErrorForwarder) <= sizeof(void *));
static_assert(std::is_trivially_copyable_v<ErrorForwarder>);
} // namespace esphome::fusb302b
+173 -8
View File
@@ -2,6 +2,10 @@
#ifdef USE_ESP32
#include <algorithm>
#include <cstdio>
#include "esphome/core/application.h"
#include "esphome/core/log.h"
#include "fusb302b_registers.h"
@@ -13,6 +17,7 @@ ESPHOME_LOG_TAG(TAG, "fusb302b");
// Task notification bits
static constexpr uint32_t NOTIFY_IRQ = 1 << 0;
static constexpr uint32_t NOTIFY_STOP = 1 << 1;
static constexpr uint32_t NOTIFY_REQUEST = 1 << 2;
static constexpr uint32_t TASK_STACK_SIZE = 4096;
static constexpr UBaseType_t TASK_PRIORITY = 18;
@@ -71,6 +76,97 @@ void FUSB302B::setup() {
this->interrupt_pin_->attach_interrupt(&FUSB302B::gpio_intr, this, gpio::INTERRUPT_FALLING_EDGE);
}
void FUSB302B::queue_event_(uint8_t event) {
const uint8_t head = this->event_queue_head_.load(std::memory_order_relaxed);
if (static_cast<uint8_t>(head - this->event_queue_tail_.load(std::memory_order_acquire)) < EVENT_QUEUE_SIZE) {
this->event_queue_[head % EVENT_QUEUE_SIZE] = event;
this->event_queue_head_.store(head + 1, std::memory_order_release);
} else {
if (event == EVENT_REQUEST_FAILED)
this->dropped_request_failures_.fetch_add(1, std::memory_order_relaxed);
this->event_queue_overflow_ = true;
}
this->enable_loop_soon_any_context();
}
void FUSB302B::on_state_changed() {
const PdState state = this->state_;
if (state == this->queued_state_) {
// Only the contract changed
this->enable_loop_soon_any_context();
return;
}
this->queued_state_ = state;
this->queue_event_(static_cast<uint8_t>(state));
}
void FUSB302B::on_request_failed() { this->queue_event_(EVENT_REQUEST_FAILED); }
void FUSB302B::loop() {
const uint32_t contract = this->contract_;
if (contract != this->published_contract_) {
this->published_contract_ = contract;
this->publish_contract_(PackedContract{contract});
}
uint8_t tail = this->event_queue_tail_.load(std::memory_order_relaxed);
const uint8_t head = this->event_queue_head_.load(std::memory_order_acquire);
while (tail != head) {
const uint8_t event = this->event_queue_[tail % EVENT_QUEUE_SIZE];
this->event_queue_tail_.store(++tail, std::memory_order_release);
if (event == EVENT_REQUEST_FAILED) {
this->request_failed_callback_.call();
} else {
this->publish_state_(static_cast<PdState>(event));
}
}
if (this->event_queue_overflow_.exchange(false)) {
ESP_LOGW(TAG, "Events arrived faster than the main loop handled them; some state triggers were skipped");
// Drop the queued states before reading the state, so no older state is replayed after it. Request failures
// are only counted, so every one of them still fires.
uint8_t failures = this->dropped_request_failures_.exchange(0, std::memory_order_relaxed);
const uint8_t end = this->event_queue_head_.load(std::memory_order_acquire);
for (; tail != end; tail++) {
if (this->event_queue_[tail % EVENT_QUEUE_SIZE] == EVENT_REQUEST_FAILED)
failures++;
}
this->event_queue_tail_.store(end, std::memory_order_release);
this->publish_state_(this->state_);
for (; failures > 0; failures--)
this->request_failed_callback_.call();
}
// Re-enabled by on_state_changed() or on_request_failed() from the PHY task
this->disable_loop();
}
void FUSB302B::publish_state_(PdState state) {
if (state == this->published_state_)
return;
const PdState previous = this->published_state_;
this->published_state_ = state;
this->state_callback_.call(state, previous);
}
void FUSB302B::publish_contract_(PackedContract contract) {
#ifdef USE_SENSOR
if (this->voltage_sensor_ != nullptr)
this->voltage_sensor_->publish_state(contract.voltage());
if (this->current_sensor_ != nullptr)
this->current_sensor_->publish_state(contract.current());
#endif
#ifdef USE_TEXT_SENSOR
if (this->contract_text_sensor_ != nullptr) {
if (!contract.is_set()) {
this->contract_text_sensor_->publish_state("Detached");
} else {
char buf[24];
snprintf(buf, sizeof(buf), "%.2fA @ %.0fV", contract.current(), contract.voltage());
this->contract_text_sensor_->publish_state(buf);
}
}
#endif
}
void FUSB302B::dump_config() {
ESP_LOGCONFIG(TAG,
"FUSB302B:\n"
@@ -78,6 +174,13 @@ void FUSB302B::dump_config() {
this->get_request_voltage());
LOG_I2C_DEVICE(this);
LOG_PIN(" Interrupt Pin: ", this->interrupt_pin_);
#ifdef USE_SENSOR
LOG_SENSOR(" ", "Voltage", this->voltage_sensor_);
LOG_SENSOR(" ", "Current", this->current_sensor_);
#endif
#ifdef USE_TEXT_SENSOR
LOG_TEXT_SENSOR(" ", "Contract", this->contract_text_sensor_);
#endif
}
void FUSB302B::on_shutdown() {
@@ -87,6 +190,23 @@ void FUSB302B::on_shutdown() {
xTaskNotify(this->task_.get_handle(), NOTIFY_STOP, eSetBits);
}
void FUSB302B::request_voltage(int voltage) {
if (voltage < MIN_REQUEST_VOLTAGE || voltage > MAX_REQUEST_VOLTAGE) {
ESP_LOGW(TAG, "Requested %d V is outside %u-%u V, clamping", voltage, MIN_REQUEST_VOLTAGE, MAX_REQUEST_VOLTAGE);
}
this->set_request_voltage(static_cast<uint8_t>(std::clamp<int>(voltage, MIN_REQUEST_VOLTAGE, MAX_REQUEST_VOLTAGE)));
if (this->is_failed()) {
ESP_LOGW(TAG, "Cannot request %u V, the component failed", this->get_request_voltage());
// loop() no longer runs, so fire the trigger from the scheduler. Calling it here directly would recurse if the
// automation requests a voltage again.
App.scheduler.set_timeout(this, 0, [this]() { this->request_failed_callback_.call(); });
return;
}
// Before setup() the voltage is simply used at the first attach
if (this->task_.is_created())
xTaskNotify(this->task_.get_handle(), NOTIFY_REQUEST, eSetBits);
}
void FUSB302B::run_task_() {
// Give the source time to settle after power-up before the first CC measurement
vTaskDelay(pdMS_TO_TICKS(STARTUP_DELAY_MS));
@@ -94,7 +214,8 @@ void FUSB302B::run_task_() {
while (true) {
uint32_t wait_ms = CC_POLL_INTERVAL_MS;
if (this->attached_) {
const bool busy = this->waiting_for_source_caps_ || this->retry_request_pending_ || this->check_ams_();
const bool busy = this->waiting_for_source_caps_ || this->request_pending_ || this->retry_request_pending_ ||
this->check_ams_();
wait_ms = busy ? PD_POLL_INTERVAL_MS : IDLE_POLL_INTERVAL_MS;
}
uint32_t bits = 0;
@@ -104,6 +225,10 @@ void FUSB302B::run_task_() {
while (true)
vTaskSuspend(nullptr);
}
if (bits & NOTIFY_REQUEST) {
// When unattached, the new voltage is simply used at the next attach
this->request_pending_ = this->attached_;
}
// INT_N is held low until the interrupt registers are read, so a new event that arrives while it is
// already low has no falling edge: keep servicing while the pin stays asserted.
@@ -118,6 +243,11 @@ void FUSB302B::run_task_() {
}
this->check_transition_timeout_();
this->check_request_retry_();
// Waits for a pending Wait retry to end in a contract or a failure, so the two sequences never overlap
if (this->request_pending_ && !this->retry_request_pending_ && !this->check_ams_()) {
this->request_pending_ = false;
this->start_negotiation_();
}
this->check_source_caps_();
}
}
@@ -341,6 +471,7 @@ void FUSB302B::detach_() {
ESP_LOGD(TAG, "Detached (VBUS lost)");
this->attached_ = false;
this->waiting_for_source_caps_ = false;
this->request_pending_ = false;
this->reset_pd_();
this->clear_contract_();
this->set_state_(PdState::PD_STATE_DISCONNECTED);
@@ -348,7 +479,8 @@ void FUSB302B::detach_() {
void FUSB302B::handle_hard_reset_() {
ESP_LOGD(TAG, "Hard reset received");
// The source returns to Type-C default power and sends its capabilities again
// The source returns to Type-C default power and sends its capabilities again, using the current request voltage
this->request_pending_ = false;
// Also flushes the RX FIFO, so the caller has nothing left to read
if (!this->reset_pd_()) {
this->enter_error_();
@@ -364,17 +496,49 @@ void FUSB302B::enter_error_() {
ESP_LOGW(TAG, "Communication with the chip failed");
this->attached_ = false;
this->waiting_for_source_caps_ = false;
this->request_pending_ = false;
// Also stops a pending Wait retry, which would otherwise send its request to the failed chip
this->reset_protocol_();
this->clear_contract_();
this->set_state_(PdState::PD_STATE_ERROR);
}
void FUSB302B::start_negotiation_() {
ESP_LOGD(TAG, "Requesting source capabilities for %u V", this->get_request_voltage());
// send_message() logs a failure; the soft reset is the next step either way, so do not wait for an answer
if (!this->send_message(this->make_control_msg_(PD_CNTRL_GET_SOURCE_CAP))) {
this->send_soft_reset_();
return;
}
this->start_source_cap_wait_(SourceCapStep::SOURCE_CAP_STEP_GET_SENT);
}
void FUSB302B::send_soft_reset_() {
this->reset_pd_();
this->send_message(this->make_control_msg_(PD_CNTRL_SOFT_RESET));
if (!this->reset_pd_()) {
this->enter_error_();
return;
}
if (!this->send_message(this->make_control_msg_(PD_CNTRL_SOFT_RESET))) {
// No answer can come, so do not wait for one
this->source_caps_failed_();
return;
}
// The source answers a soft reset with new source capabilities
this->start_source_cap_wait_(SourceCapStep::SOURCE_CAP_STEP_SOFT_RESET_SENT);
}
void FUSB302B::source_caps_failed_() {
this->waiting_for_source_caps_ = false;
if (this->state_ == PdState::PD_STATE_DEFAULT_CONTRACT) {
ESP_LOGW(TAG, "PD negotiation failed, staying at 5 V");
this->set_state_(PdState::PD_STATE_PD_TIMEOUT);
} else {
// A request made at runtime got no answer. A soft reset keeps the power level, so the contract in place stays.
ESP_LOGW(TAG, "Renegotiation failed, keeping the current contract");
}
this->on_request_failed();
}
void FUSB302B::start_source_cap_wait_(SourceCapStep step) {
this->waiting_for_source_caps_ = true;
this->source_cap_step_ = step;
@@ -390,7 +554,10 @@ void FUSB302B::check_source_caps_() {
if (elapsed < SOURCE_CAP_WAIT_MS)
return;
ESP_LOGD(TAG, "No source capabilities received, requesting them");
this->send_message(this->make_control_msg_(PD_CNTRL_GET_SOURCE_CAP));
if (!this->send_message(this->make_control_msg_(PD_CNTRL_GET_SOURCE_CAP))) {
this->send_soft_reset_();
return;
}
this->source_cap_step_ = SourceCapStep::SOURCE_CAP_STEP_GET_SENT;
break;
case SourceCapStep::SOURCE_CAP_STEP_GET_SENT:
@@ -402,9 +569,7 @@ void FUSB302B::check_source_caps_() {
case SourceCapStep::SOURCE_CAP_STEP_SOFT_RESET_SENT:
if (elapsed < SOURCE_CAP_RETRY_MS)
return;
ESP_LOGW(TAG, "PD negotiation failed, staying at 5 V");
this->waiting_for_source_caps_ = false;
this->set_state_(PdState::PD_STATE_PD_TIMEOUT);
this->source_caps_failed_();
return;
}
this->source_cap_step_start_ = millis();
+71 -2
View File
@@ -2,6 +2,9 @@
#ifdef USE_ESP32
#include <array>
#include <atomic>
#include "esphome/components/i2c/i2c.h"
#include "esphome/core/component.h"
#include "esphome/core/defines.h"
@@ -9,6 +12,13 @@
#include "esphome/core/helpers.h"
#include "esphome/core/static_task.h"
#ifdef USE_SENSOR
#include "esphome/components/sensor/sensor.h"
#endif
#ifdef USE_TEXT_SENSOR
#include "esphome/components/text_sensor/text_sensor.h"
#endif
#include "pd.h"
namespace esphome::fusb302b {
@@ -16,16 +26,39 @@ namespace esphome::fusb302b {
/// FUSB302B USB Type-C / Power Delivery sink controller.
///
/// A dedicated task owns all I2C traffic to the chip so that PD replies meet the protocol timing
/// even while the main loop is busy.
/// even while the main loop is busy. The main loop only publishes state changes.
class FUSB302B : public PowerDelivery, public Component, public i2c::I2CDevice {
public:
explicit FUSB302B(InternalGPIOPin *interrupt_pin) : interrupt_pin_(interrupt_pin) {}
void setup() override;
void loop() override;
void dump_config() override;
void on_shutdown() override;
float get_setup_priority() const override { return setup_priority::HARDWARE; }
/// Negotiate a new contract with the highest source voltage that does not exceed `voltage`.
/// Values outside 5-20 V are clamped with a warning.
void request_voltage(int voltage);
/// Callbacks receive each new state in order, with the state before it.
template<typename F> void add_on_state_callback(F &&callback) {
this->state_callback_.add(std::forward<F>(callback));
}
/// Called when a voltage request was rejected, got no answer, or was dropped because the component failed.
/// A contract already in place stays valid.
template<typename F> void add_on_request_failed_callback(F &&callback) {
this->request_failed_callback_.add(std::forward<F>(callback));
}
#ifdef USE_SENSOR
void set_voltage_sensor(sensor::Sensor *sensor) { this->voltage_sensor_ = sensor; }
void set_current_sensor(sensor::Sensor *sensor) { this->current_sensor_ = sensor; }
#endif
#ifdef USE_TEXT_SENSOR
void set_contract_text_sensor(text_sensor::TextSensor *sensor) { this->contract_text_sensor_ = sensor; }
#endif
protected:
enum class SourceCapStep : uint8_t {
SOURCE_CAP_STEP_WAIT,
@@ -37,10 +70,11 @@ class FUSB302B : public PowerDelivery, public Component, public i2c::I2CDevice {
static void task_func(void *arg);
bool send_message(const PdMsg &msg) override;
void on_state_changed() override {}
void on_state_changed() override;
void on_soft_reset_received() override { this->start_source_cap_wait_(SourceCapStep::SOURCE_CAP_STEP_WAIT); }
void on_source_caps_received() override { this->waiting_for_source_caps_ = false; }
void on_transition_timeout() override { this->send_soft_reset_(); }
void on_request_failed() override;
// PHY task only
[[noreturn]] void run_task_();
@@ -54,10 +88,16 @@ class FUSB302B : public PowerDelivery, public Component, public i2c::I2CDevice {
void detach_();
void handle_hard_reset_();
void start_source_cap_wait_(SourceCapStep step);
void start_negotiation_();
void send_soft_reset_();
void source_caps_failed_();
void check_source_caps_();
void enter_error_();
void publish_contract_(PackedContract contract);
void publish_state_(PdState state);
void queue_event_(uint8_t event);
InternalGPIOPin *interrupt_pin_;
StaticTask task_;
@@ -67,6 +107,35 @@ class FUSB302B : public PowerDelivery, public Component, public i2c::I2CDevice {
bool attached_{false};
bool waiting_for_source_caps_{false};
bool cc_ambiguous_logged_{false};
bool request_pending_{false};
// Last state put in the queue
PdState queued_state_{PdState::PD_STATE_DISCONNECTED};
// State changes and request failures from the PHY task, replayed in order by loop() so no event is lost while the
// main loop is blocked. An entry is a PdState value or EVENT_REQUEST_FAILED. The PHY task only writes head, the
// main loop only writes tail.
static constexpr uint8_t EVENT_QUEUE_SIZE = 8;
static constexpr uint8_t EVENT_REQUEST_FAILED = 0xFF;
std::array<uint8_t, EVENT_QUEUE_SIZE> event_queue_{};
std::atomic<uint8_t> event_queue_head_{0};
std::atomic<uint8_t> event_queue_tail_{0};
std::atomic<bool> event_queue_overflow_{false};
// Request failures that did not fit in the queue
std::atomic<uint8_t> dropped_request_failures_{0};
// Main loop only
PdState published_state_{PdState::PD_STATE_DISCONNECTED};
uint32_t published_contract_{UINT32_MAX};
LazyCallbackManager<void(PdState, PdState)> state_callback_;
LazyCallbackManager<void()> request_failed_callback_;
#ifdef USE_SENSOR
sensor::Sensor *voltage_sensor_{nullptr};
sensor::Sensor *current_sensor_{nullptr};
#endif
#ifdef USE_TEXT_SENSOR
text_sensor::TextSensor *contract_text_sensor_{nullptr};
#endif
};
} // namespace esphome::fusb302b
+8 -1
View File
@@ -114,8 +114,14 @@ void PowerDelivery::set_ams_(bool active) {
}
bool PowerDelivery::check_ams_() {
if (this->active_ams_ && millis() - this->ams_start_ > AMS_TIMEOUT_MS)
if (this->active_ams_ && millis() - this->ams_start_ > AMS_TIMEOUT_MS) {
this->set_ams_(false);
// Every sequence carries a request. In TRANSITION, check_transition_timeout_() handles the missing PS_RDY.
if (this->state_ != PdState::PD_STATE_TRANSITION) {
ESP_LOGW(TAG, "Source did not complete the request in time");
this->request_failed_();
}
}
return this->active_ams_;
}
@@ -148,6 +154,7 @@ void PowerDelivery::request_failed_() {
// A contract already in place stays valid. Without one, report that negotiation is over at default power.
if (this->state_ == PdState::PD_STATE_DEFAULT_CONTRACT)
this->set_state_(PdState::PD_STATE_PD_TIMEOUT);
this->on_request_failed();
}
void PowerDelivery::check_transition_timeout_() {
+5 -1
View File
@@ -125,6 +125,9 @@ class PowerDelivery {
/// Called after an accepted request was not followed by PS_RDY in time and the state fell back to default power;
/// the PHY sends a soft reset so the source offers its capabilities again.
virtual void on_transition_timeout() = 0;
/// Called when a request ended without a new contract: it was rejected, the source kept answering Wait or did not
/// answer in time, or it could not be sent. A contract already in place stays valid.
virtual void on_request_failed() = 0;
void handle_message_(const PdMsg &msg);
/// Called by the PHY when the chip gave up on a message after its retries without receiving a GoodCRC.
@@ -137,7 +140,8 @@ class PowerDelivery {
/// contract.
void set_state_and_contract_(PdState state, const PdContract &contract);
void set_ams_(bool active);
/// Returns true while an atomic message sequence is in progress; ends it after a timeout.
/// Returns true while an atomic message sequence is in progress. Ends it after a timeout, which fails the
/// request unless the source already accepted a new contract.
bool check_ams_();
/// Falls back to default power when the source accepted a request but did not report the new supply ready in time.
void check_transition_timeout_();
+5
View File
@@ -103,6 +103,11 @@ CONFIG_SCHEMA = cv.All(
async def to_code(config: ConfigType) -> None:
if CORE.is_esp8266:
from esphome.components.esp8266.const import use_timer1
# The acknowledge bit is released from hardware timer 1
use_timer1(DOMAIN)
if CORE.is_esp32:
# The acknowledge bit is released from a general purpose timer, left out of the build by default
include_builtin_idf_component("esp_driver_gptimer")
@@ -0,0 +1,136 @@
#pragma once
#include <cctype>
#include "esphome/core/helpers.h"
#include "esphome/core/string_ref.h"
namespace esphome::homeassistant {
/// Emit the UTF-8 encoding of a code point. Rejects NUL, which would end the item early.
template<typename C> bool emit_utf8(uint32_t code_point, C &on_char) {
if (code_point == 0 || code_point > 0x10FFFF)
return false;
if (code_point < 0x80) {
on_char(static_cast<char>(code_point));
} else if (code_point < 0x800) {
on_char(static_cast<char>(0xC0 | (code_point >> 6)));
on_char(static_cast<char>(0x80 | (code_point & 0x3F)));
} else if (code_point < 0x10000) {
on_char(static_cast<char>(0xE0 | (code_point >> 12)));
on_char(static_cast<char>(0x80 | ((code_point >> 6) & 0x3F)));
on_char(static_cast<char>(0x80 | (code_point & 0x3F)));
} else {
on_char(static_cast<char>(0xF0 | (code_point >> 18)));
on_char(static_cast<char>(0x80 | ((code_point >> 12) & 0x3F)));
on_char(static_cast<char>(0x80 | ((code_point >> 6) & 0x3F)));
on_char(static_cast<char>(0x80 | (code_point & 0x3F)));
}
return true;
}
/** Walk a list attribute as Home Assistant sends it: the Python repr of a list of strings, such as
* `['Low', "It's high", 'caf\xe9']`, or of an enum, such as `[<ColorMode.HS: 'hs'>]`, where the
* quoted value stands for the item.
*
* Calls on_char for each decoded byte of an item and on_item at the end of each item. Returns false
* on malformed input; the callbacks may already have run by then.
*/
template<typename C, typename O> bool parse_python_list(StringRef src, C &&on_char, O &&on_item) {
const char *pos = src.c_str();
const char *const end = pos + src.size();
auto skip_spaces = [&]() {
while (pos < end && *pos == ' ')
pos++;
};
skip_spaces();
if (pos == end || *pos != '[')
return false;
pos++;
skip_spaces();
if (pos < end && *pos == ']') {
pos++;
} else {
while (true) {
skip_spaces();
// An enum member is shown as <Name.MEMBER: 'value'>; the quoted value is the item
const bool enum_member = pos < end && *pos == '<';
if (enum_member) {
pos++;
while (pos < end && (std::isalnum(static_cast<unsigned char>(*pos)) || *pos == '_' || *pos == '.'))
pos++;
if (pos == end || *pos != ':')
return false;
pos++;
skip_spaces();
}
if (pos == end || (*pos != '\'' && *pos != '"'))
return false;
const char quote = *pos++;
while (true) {
if (pos == end)
return false;
const char c = *pos++;
if (c == quote)
break;
if (c != '\\') {
on_char(c);
continue;
}
if (pos == end)
return false;
const char escape = *pos++;
switch (escape) {
case '\\':
case '\'':
case '"':
on_char(escape);
break;
case 'n':
on_char('\n');
break;
case 'r':
on_char('\r');
break;
case 't':
on_char('\t');
break;
case 'x':
case 'u':
case 'U': {
const size_t digits = escape == 'x' ? 2 : (escape == 'u' ? 4 : 8);
if (static_cast<size_t>(end - pos) < digits)
return false;
auto code_point = parse_hex<uint32_t>(pos, digits);
if (!code_point.has_value() || !emit_utf8(*code_point, on_char))
return false;
pos += digits;
break;
}
default:
return false;
}
}
if (enum_member) {
skip_spaces();
if (pos == end || *pos != '>')
return false;
pos++;
}
on_item();
skip_spaces();
if (pos == end)
return false;
const char separator = *pos++;
if (separator == ']')
break;
if (separator != ',')
return false;
}
}
skip_spaces();
return pos == end;
}
} // namespace esphome::homeassistant
@@ -13,6 +13,7 @@ from .. import (
CODEOWNERS = ["@jesserockz"]
DEPENDENCIES = ["api"]
AUTO_LOAD = ["homeassistant"]
CONF_MAX_OPTIONS = "max_options"
CONF_OPTIONS_BUFFER_SIZE = "options_buffer_size"
@@ -2,6 +2,8 @@
#include <cstring>
#include "../homeassistant.h"
#include "esphome/components/api/api_pb2.h"
#include "esphome/components/api/api_server.h"
#include "esphome/core/helpers.h"
@@ -13,118 +15,6 @@ namespace esphome::homeassistant {
ESPHOME_LOG_TAG(TAG, "homeassistant.select");
namespace {
/// Emit the UTF-8 encoding of a code point. Rejects NUL, which would end the option early.
template<typename C> bool emit_utf8(uint32_t code_point, C &on_char) {
if (code_point == 0 || code_point > 0x10FFFF)
return false;
if (code_point < 0x80) {
on_char(static_cast<char>(code_point));
} else if (code_point < 0x800) {
on_char(static_cast<char>(0xC0 | (code_point >> 6)));
on_char(static_cast<char>(0x80 | (code_point & 0x3F)));
} else if (code_point < 0x10000) {
on_char(static_cast<char>(0xE0 | (code_point >> 12)));
on_char(static_cast<char>(0x80 | ((code_point >> 6) & 0x3F)));
on_char(static_cast<char>(0x80 | (code_point & 0x3F)));
} else {
on_char(static_cast<char>(0xF0 | (code_point >> 18)));
on_char(static_cast<char>(0x80 | ((code_point >> 12) & 0x3F)));
on_char(static_cast<char>(0x80 | ((code_point >> 6) & 0x3F)));
on_char(static_cast<char>(0x80 | (code_point & 0x3F)));
}
return true;
}
/** Walk a list attribute as Home Assistant sends it: the Python repr of a list of strings, such as
* `['Low', "It's high", 'caf\xe9']`.
*
* Calls on_char for each decoded byte of an option and on_option at the end of each option. Returns false
* on malformed input; the callbacks may already have run by then.
*/
template<typename C, typename O> bool parse_option_list(StringRef src, C &&on_char, O &&on_option) {
const char *pos = src.c_str();
const char *const end = pos + src.size();
auto skip_spaces = [&]() {
while (pos < end && *pos == ' ')
pos++;
};
skip_spaces();
if (pos == end || *pos != '[')
return false;
pos++;
skip_spaces();
if (pos < end && *pos == ']') {
pos++;
} else {
while (true) {
skip_spaces();
if (pos == end || (*pos != '\'' && *pos != '"'))
return false;
const char quote = *pos++;
while (true) {
if (pos == end)
return false;
const char c = *pos++;
if (c == quote)
break;
if (c != '\\') {
on_char(c);
continue;
}
if (pos == end)
return false;
const char escape = *pos++;
switch (escape) {
case '\\':
case '\'':
case '"':
on_char(escape);
break;
case 'n':
on_char('\n');
break;
case 'r':
on_char('\r');
break;
case 't':
on_char('\t');
break;
case 'x':
case 'u':
case 'U': {
const size_t digits = escape == 'x' ? 2 : (escape == 'u' ? 4 : 8);
if (static_cast<size_t>(end - pos) < digits)
return false;
auto code_point = parse_hex<uint32_t>(pos, digits);
if (!code_point.has_value() || !emit_utf8(*code_point, on_char))
return false;
pos += digits;
break;
}
default:
return false;
}
}
on_option();
skip_spaces();
if (pos == end)
return false;
const char separator = *pos++;
if (separator == ']')
break;
if (separator != ',')
return false;
}
}
skip_spaces();
return pos == end;
}
} // namespace
void HomeassistantSelect::setup() {
this->options_buffer_ = std::make_unique<char[]>(this->options_buffer_size_);
this->option_list_ = std::make_unique<const char *[]>(this->max_options_);
@@ -146,7 +36,7 @@ void HomeassistantSelect::options_changed_(StringRef options) {
size_t match_len = 0;
bool matching = active != nullptr;
optional<size_t> new_active;
bool valid = parse_option_list(
bool valid = parse_python_list(
options,
[&](char c) {
bytes++;
@@ -183,7 +73,7 @@ void HomeassistantSelect::options_changed_(StringRef options) {
size_t offset = 0;
size_t option_start = 0;
size_t index = 0;
parse_option_list(
parse_python_list(
options, [&](char c) { buffer[offset++] = c; },
[&]() {
buffer[offset++] = '\0';
@@ -3,9 +3,8 @@ import esphome.codegen as cg
from esphome.components.ota import BASE_OTA_SCHEMA, OTAComponent, ota_to_code
import esphome.config_validation as cv
from esphome.const import CONF_ID, CONF_PASSWORD, CONF_URL, CONF_USERNAME
from esphome.core import ID, coroutine_with_priority
from esphome.core import coroutine_with_priority
from esphome.coroutine import CoroPriority
from esphome.cpp_generator import MockObj, TemplateArgsType
from esphome.types import ConfigType
from .. import CONF_HTTP_REQUEST_ID, HttpRequestComponent, http_request_ns
@@ -21,9 +20,6 @@ CONF_MD5_URL = "md5_url"
OtaHttpRequestComponent = http_request_ns.class_(
"OtaHttpRequestComponent", OTAComponent
)
OtaHttpRequestComponentFlashAction = http_request_ns.class_(
"OtaHttpRequestComponentFlashAction", automation.Action
)
CONFIG_SCHEMA = cv.All(
cv.Schema(
@@ -68,38 +64,13 @@ OTA_HTTP_REQUEST_FLASH_ACTION_SCHEMA = cv.All(
)
@automation.register_action(
automation.register_apply_action(
"ota.http_request.flash",
OtaHttpRequestComponentFlashAction,
OTA_HTTP_REQUEST_FLASH_ACTION_SCHEMA,
synchronous=True,
automation.ApplyField(CONF_MD5_URL, "set_md5_url", cg.std_string),
automation.ApplyField(CONF_MD5, "set_md5", cg.std_string),
automation.ApplyField(CONF_PASSWORD, "set_password", cg.std_string),
automation.ApplyField(CONF_USERNAME, "set_username", cg.std_string),
automation.ApplyField(CONF_URL, "set_url", cg.std_string),
automation.ApplyCall("flash()"),
)
async def ota_http_request_action_to_code(
config: ConfigType,
action_id: ID,
template_arg: cg.TemplateArguments,
args: TemplateArgsType,
) -> MockObj:
paren = await cg.get_variable(config[CONF_ID])
var = cg.new_Pvariable(action_id, template_arg, paren)
if md5_url := config.get(CONF_MD5_URL):
template_ = await cg.templatable(md5_url, args, cg.std_string)
cg.add(var.set_md5_url(template_))
if md5_str := config.get(CONF_MD5):
template_ = await cg.templatable(md5_str, args, cg.std_string)
cg.add(var.set_md5(template_))
if password_str := config.get(CONF_PASSWORD):
template_ = await cg.templatable(password_str, args, cg.std_string)
cg.add(var.set_password(template_))
if username_str := config.get(CONF_USERNAME):
template_ = await cg.templatable(username_str, args, cg.std_string)
cg.add(var.set_username(template_))
template_ = await cg.templatable(config[CONF_URL], args, cg.std_string)
cg.add(var.set_url(template_))
return var
@@ -1,40 +0,0 @@
#pragma once
#include "ota_http_request.h"
#include "esphome/core/automation.h"
namespace esphome::http_request {
template<typename... Ts> class OtaHttpRequestComponentFlashAction final : public Action<Ts...> {
public:
OtaHttpRequestComponentFlashAction(OtaHttpRequestComponent *parent) : parent_(parent) {}
TEMPLATABLE_VALUE(std::string, md5_url)
TEMPLATABLE_VALUE(std::string, md5)
TEMPLATABLE_VALUE(std::string, password)
TEMPLATABLE_VALUE(std::string, url)
TEMPLATABLE_VALUE(std::string, username)
void play(const Ts &...x) override {
if (this->md5_url_.has_value()) {
this->parent_->set_md5_url(this->md5_url_.value(x...));
}
if (this->md5_.has_value()) {
this->parent_->set_md5(this->md5_.value(x...));
}
if (this->password_.has_value()) {
this->parent_->set_password(this->password_.value(x...));
}
if (this->username_.has_value()) {
this->parent_->set_username(this->username_.value(x...));
}
this->parent_->set_url(this->url_.value(x...));
this->parent_->flash();
// Normally never reached due to reboot
}
protected:
OtaHttpRequestComponent *parent_;
};
} // namespace esphome::http_request
+4 -2
View File
@@ -160,7 +160,8 @@ void HOT HUB75Display::draw_pixels_at(int x_start, int y_start, int w, int h, co
// 24-bit: 3 bytes per pixel, tightly packed
format = Hub75PixelFormat::RGB888;
bytes_per_pixel = 3;
// Note: 24-bit is always RGB order in LVGL
// ESPHome RGB order 888 is b,g,r in memory, which hub75 calls BGR
color_order = (order == ColorOrder::COLOR_ORDER_RGB) ? Hub75ColorOrder::BGR : Hub75ColorOrder::RGB;
#else
ESP_LOGE(TAG, "Unsupported LV_COLOR_DEPTH: %d", LV_COLOR_DEPTH);
return;
@@ -169,7 +170,8 @@ void HOT HUB75Display::draw_pixels_at(int x_start, int y_start, int w, int h, co
// Non-LVGL mode: standard 24-bit RGB888
format = Hub75PixelFormat::RGB888;
bytes_per_pixel = 3;
color_order = (order == ColorOrder::COLOR_ORDER_RGB) ? Hub75ColorOrder::RGB : Hub75ColorOrder::BGR;
// ESPHome RGB order 888 is b,g,r in memory, which hub75 calls BGR
color_order = (order == ColorOrder::COLOR_ORDER_RGB) ? Hub75ColorOrder::BGR : Hub75ColorOrder::RGB;
#endif
} else {
ESP_LOGE(TAG, "Unsupported bitness: %d", static_cast<int>(bitness));
+19 -2
View File
@@ -144,10 +144,15 @@ def validate_config(config: ConfigType) -> ConfigType:
return config
def _host_is_linux() -> bool:
"""The host I2C bus needs Linux i2c-dev."""
return sys.platform.lower().startswith("linux")
def validate_host_config(config: ConfigType) -> ConfigType:
if CORE.is_host:
# Host I2C is currently only supported on Linux
if not sys.platform.lower().startswith("linux"):
if not _host_is_linux():
raise cv.Invalid(
"I2C is only supported on Linux for the host platform. "
f"Current platform: {sys.platform}"
@@ -446,7 +451,7 @@ def final_validate_device_schema(
)
FILTER_SOURCE_FILES = filter_source_files_from_platform(
_platform_filter = filter_source_files_from_platform(
{
"i2c_bus_arduino.cpp": {
PlatformFramework.ESP8266_ARDUINO,
@@ -463,3 +468,15 @@ FILTER_SOURCE_FILES = filter_source_files_from_platform(
"i2c_bus_host.cpp": {PlatformFramework.HOST_NATIVE},
}
)
def FILTER_SOURCE_FILES() -> list[str]:
excluded = _platform_filter()
# An empty list means the target isn't known yet. Otherwise the host bus is
# only kept for the host platform, and it needs Linux i2c-dev
# (validate_host_config rejects host I2C elsewhere). Leaving it out lets host
# builds without a bus, like the C++ unit tests with their fake bus, build on
# macOS too.
if excluded and "i2c_bus_host.cpp" not in excluded and not _host_is_linux():
excluded.append("i2c_bus_host.cpp")
return excluded
@@ -142,7 +142,8 @@ void ImprovBLEComponent::loop() {
this->set_status_indicator_state_(false);
if (this->should_start_ && this->setup_complete_) {
if (this->service_->is_created()) {
// A service that an earlier session stopped stays STOPPED and can be started again
if (this->service_->is_created() || this->service_->is_stopped()) {
this->service_->start();
} else if (this->service_->is_running()) {
// Start by advertising the device name first BEFORE setting any state
+1 -4
View File
@@ -987,10 +987,7 @@ void HOT IT8951Display::draw_pixels_at(int x_start, int y_start, int w, int h, c
}
case COLOR_BITNESS_888: {
const size_t i = source_idx * 3;
color_value =
big_endian
? (static_cast<uint32_t>(ptr[i]) << 16) | (static_cast<uint32_t>(ptr[i + 1]) << 8) | ptr[i + 2]
: ptr[i] | (static_cast<uint32_t>(ptr[i + 1]) << 8) | (static_cast<uint32_t>(ptr[i + 2]) << 16);
color_value = ptr[i] | (static_cast<uint32_t>(ptr[i + 1]) << 8) | (static_cast<uint32_t>(ptr[i + 2]) << 16);
break;
}
default:
+1 -1
View File
@@ -1,5 +1,6 @@
import esphome.codegen as cg
from esphome.components import select
from esphome.components.const import CONF_OUT_PIN_LEVEL
import esphome.config_validation as cv
from esphome.const import (
CONF_BAUD_RATE,
@@ -20,7 +21,6 @@ LightOutControlSelect = ld2410_ns.class_("LightOutControlSelect", select.Select)
CONF_DISTANCE_RESOLUTION = "distance_resolution"
CONF_LIGHT_FUNCTION = "light_function"
CONF_OUT_PIN_LEVEL = "out_pin_level"
CONFIG_SCHEMA = {
+1 -1
View File
@@ -1,5 +1,6 @@
import esphome.codegen as cg
from esphome.components import select
from esphome.components.const import CONF_OUT_PIN_LEVEL
import esphome.config_validation as cv
from esphome.const import (
CONF_BAUD_RATE,
@@ -20,7 +21,6 @@ LightOutControlSelect = LD2412_ns.class_("LightOutControlSelect", select.Select)
CONF_DISTANCE_RESOLUTION = "distance_resolution"
CONF_LIGHT_FUNCTION = "light_function"
CONF_OUT_PIN_LEVEL = "out_pin_level"
CONFIG_SCHEMA = {
@@ -11,6 +11,7 @@ from esphome.const import (
CONF_REPEAT,
CONF_WRITE_PIN,
)
from esphome.core import CORE
from esphome.cpp_helpers import gpio_pin_expression
from esphome.types import ConfigType
@@ -65,6 +66,10 @@ automation.register_apply_action(
async def to_code(config: ConfigType) -> None:
if CORE.is_esp8266:
from esphome.components.esp8266.const import use_timer1
use_timer1(DOMAIN)
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
+13 -6
View File
@@ -27,17 +27,23 @@ static const LogString *gyro_range_to_str(LSM6DSGyroRange range) {
}
}
static const LogString *chip_name_from_who_am_i(uint8_t who_am_i) {
const LogString *chip_name_from_who_am_i(uint8_t who_am_i) {
switch (who_am_i) {
case 0x69:
return LOG_STR("LSMDSO");
case 0x6A:
case LSM6DS_WHO_AM_I_LSM6DS3:
return LOG_STR("LSM6DS3");
case LSM6DS_WHO_AM_I_LSM6DS3TR_C:
return LOG_STR("LSM6DS3TR-C");
case LSM6DS_WHO_AM_I_LSM6DSO:
return LOG_STR("LSM6DSO");
default:
return nullptr;
}
}
uint16_t temperature_sensitivity_from_who_am_i(uint8_t who_am_i) {
return who_am_i == LSM6DS_WHO_AM_I_LSM6DS3 ? 16 : 256;
}
void LSM6DSComponent::setup() {
MotionComponent::setup();
uint8_t who_am_i = 0;
@@ -54,6 +60,7 @@ void LSM6DSComponent::setup() {
}
ESP_LOGD(TAG, "Found %s (WHO_AM_I = 0x%02X)", LOG_STR_ARG(chip_name), who_am_i);
this->chip_name_ = chip_name;
this->temperature_sensitivity_ = temperature_sensitivity_from_who_am_i(who_am_i);
// 2. Software reset — clears all registers to defaults
if (this->write_register(LSM6DS_REG_CTRL3_C, &CTRL3_C_SW_RESET, 1) != i2c::ERROR_OK) {
@@ -185,12 +192,12 @@ bool LSM6DSComponent::update_data(motion::MotionData &data) {
// Temperature (lazy — only read if a listener is registered)
// Kept as a separate 2-byte read to avoid extending the burst to 14 bytes when
// temperature is not needed.
// Formula: T(°C) = (raw / 256.0) + 25.0 (datasheet Table 90, OUT_TEMP register)
// Formula: T(°C) = raw / sensitivity + 25.0, sensitivity in LSB/°C per chip (0 LSB at 25 °C)
if (!this->temperature_callback_.empty()) {
uint8_t raw_t[2];
if (this->read_bytes(LSM6DS_REG_OUT_TEMP_L, raw_t, 2)) {
int16_t temp_raw = (int16_t) ((raw_t[1] << 8) | raw_t[0]);
float temperature = (temp_raw / 256.0f) + 25.0f;
float temperature = temp_raw / static_cast<float>(this->temperature_sensitivity_) + 25.0f;
this->temperature_callback_.call(temperature);
}
}
+19 -9
View File
@@ -9,15 +9,19 @@ namespace esphome::lsm6ds {
// ── Register map (datasheet DocID030071 Rev 3, Table 19) ────────────────────
static const uint8_t LSM6DS_REG_WHO_AM_I = 0x0F;
static const uint8_t LSM6DS_REG_CTRL1_XL = 0x10; // Accel ODR + FS
static const uint8_t LSM6DS_REG_CTRL2_G = 0x11; // Gyro ODR + FS
static const uint8_t LSM6DS_REG_CTRL3_C = 0x12; // SW_RESET, BDU, IF_INC
static const uint8_t LSM6DS_REG_CTRL6_C = 0x15; // Accel HP disable, Gyro LPF1
static const uint8_t LSM6DS_REG_CTRL7_G = 0x16; // Gyro HP disable
static const uint8_t LSM6DS_REG_STATUS = 0x1E; // XLDA, GDA, TDA
static const uint8_t LSM6DS_REG_OUT_TEMP_L = 0x20; // Temperature LSB
static const uint8_t LSM6DS_REG_OUTX_L_G = 0x22; // Gyro X LSB (burst start)
static const uint8_t LSM6DS_REG_OUTX_L_XL = 0x28; // Accel X LSB
// WHO_AM_I values; the LSM6DS3 (DocID026899) and LSM6DSO (DS12140) use the same registers as the TR-C here
static constexpr uint8_t LSM6DS_WHO_AM_I_LSM6DS3 = 0x69;
static constexpr uint8_t LSM6DS_WHO_AM_I_LSM6DS3TR_C = 0x6A;
static constexpr uint8_t LSM6DS_WHO_AM_I_LSM6DSO = 0x6C; // also the LSM6DSO32, whose accel ranges differ (unsupported)
static const uint8_t LSM6DS_REG_CTRL1_XL = 0x10; // Accel ODR + FS
static const uint8_t LSM6DS_REG_CTRL2_G = 0x11; // Gyro ODR + FS
static const uint8_t LSM6DS_REG_CTRL3_C = 0x12; // SW_RESET, BDU, IF_INC
static const uint8_t LSM6DS_REG_CTRL6_C = 0x15; // Accel HP disable, Gyro LPF1
static const uint8_t LSM6DS_REG_CTRL7_G = 0x16; // Gyro HP disable
static const uint8_t LSM6DS_REG_STATUS = 0x1E; // XLDA, GDA, TDA
static const uint8_t LSM6DS_REG_OUT_TEMP_L = 0x20; // Temperature LSB
static const uint8_t LSM6DS_REG_OUTX_L_G = 0x22; // Gyro X LSB (burst start)
static const uint8_t LSM6DS_REG_OUTX_L_XL = 0x28; // Accel X LSB
// Burst read from 0x22 to 0x2D inclusive: gyro XYZ (6 bytes) + accel XYZ (6 bytes)
static const uint8_t LSM6DS_BURST_LEN = 12;
@@ -83,6 +87,11 @@ enum LSM6DSGyroODR : uint8_t {
};
// ── Main component class ─────────────────────────────────────────────────────
/// Chip name for a WHO_AM_I value, or nullptr when the chip is not supported.
const LogString *chip_name_from_who_am_i(uint8_t who_am_i);
/// Temperature sensitivity in LSB/°C for a supported WHO_AM_I value, from the chip's datasheet.
uint16_t temperature_sensitivity_from_who_am_i(uint8_t who_am_i);
class LSM6DSComponent final : public motion::MotionComponent, public i2c::I2CDevice {
public:
void setup() override;
@@ -105,6 +114,7 @@ class LSM6DSComponent final : public motion::MotionComponent, public i2c::I2CDev
LSM6DSAccelODR accel_odr_{LSM6DS_ACCEL_ODR_104};
LSM6DSGyroRange gyro_range_{LSM6DS_GYRO_RANGE_2000};
LSM6DSGyroODR gyro_odr_{LSM6DS_GYRO_ODR_208};
uint16_t temperature_sensitivity_{256}; // LSB/°C: 16 on the LSM6DS3, 256 on the others
LazyCallbackManager<void(float)> temperature_callback_{};
};
+3
View File
@@ -112,6 +112,7 @@ from .widgets import (
set_obj_properties,
)
from .widgets.keyboard import attach_textareas
from .widgets.menu import add_root_back_button_triggers, validate_menu_pages
# Import only what we actually use directly in this file
from .widgets.msgbox import MSGBOX_SCHEMA, msgboxes_to_code
@@ -333,6 +334,7 @@ def final_validation(config_list):
path = global_config.get_path_for_id(name)
widget_conf = global_config.get_config_for_path(path[:-1])
widget_type.final_validate(name, conf, widget_conf, path[1:])
validate_menu_pages(global_config)
def lv_conf_path_flag(lv_conf_h_file: Path) -> str:
@@ -513,6 +515,7 @@ async def to_code(configs):
await finish_list_triggers()
await generate_triggers()
await add_root_back_button_triggers()
await generate_align_tos(configs[0])
await attach_textareas()
for config in configs:
+18
View File
@@ -25,6 +25,7 @@ KEY_LV_DEFINES = "lv_defines"
KEY_LV_FONTS_USED = "lv_fonts_used"
KEY_LV_IMAGES_USED = "lv_images_used"
KEY_LV_USES = "lv_uses"
KEY_MENU_SET_PAGE_ACTIONS = "menu_set_page_actions"
KEY_NAMED_STYLES = "named_styles"
KEY_OPTIONS = "options"
KEY_REFRESHED_WIDGETS = "refreshed_widgets"
@@ -172,6 +173,10 @@ def get_refreshed_widgets() -> set:
return _get_data(KEY_REFRESHED_WIDGETS, set())
def get_menu_set_page_actions() -> list:
return _get_data(KEY_MENU_SET_PAGE_ACTIONS, [])
def get_list_triggers() -> dict:
return _get_data(KEY_LIST_TRIGGERS, {})
@@ -682,6 +687,12 @@ LV_MENU_MODES = LvConstant(
"BOTTOM_FIXED",
)
LV_MENU_ROOT_BACK_BUTTON_MODES = LvConstant(
"LV_MENU_ROOT_BACK_BUTTON_",
"DISABLED",
"ENABLED",
)
LV_CHART_TYPES = (
"NONE",
"LINE",
@@ -762,6 +773,7 @@ CONF_GRID_ROW_ALIGN = "grid_row_align"
CONF_GRID_ROWS = "grid_rows"
CONF_HEADER_BUTTONS = "header_buttons"
CONF_HEADER_MODE = "header_mode"
CONF_HEADER_STYLE = "header_style"
CONF_HOME = "home"
CONF_IMAGE = "image"
CONF_INDICATORS = "indicators"
@@ -778,6 +790,7 @@ CONF_LVGL_ID = "lvgl_id"
CONF_LONG_MODE = "long_mode"
CONF_MAJOR_TICKS_STYLE = "major_ticks_style"
CONF_MAPPING = "mapping"
CONF_MENU = "menu"
CONF_MSGBOXES = "msgboxes"
CONF_OBJ = "obj"
CONF_ONE_CHECKED = "one_checked"
@@ -788,6 +801,7 @@ CONF_ON_LANDSCAPE = "on_landscape"
CONF_ON_PAUSE = "on_pause"
CONF_ON_PORTRAIT = "on_portrait"
CONF_ON_RESUME = "on_resume"
CONF_ON_ROOT_BACK_BUTTON_CLICK = "on_root_back_button_click"
CONF_ON_SELECT = "on_select"
CONF_OPA = "opa"
CONF_NEXT = "next"
@@ -810,6 +824,7 @@ CONF_RESUME_ON_INPUT = "resume_on_input"
CONF_RIGHT_BUTTON = "right_button"
CONF_ROLLOVER = "rollover"
CONF_ROOT_BACK_BTN = "root_back_btn"
CONF_ROOT_PAGE = "root_page"
CONF_SCALE = "scale"
CONF_SCALE_LINES = "scale_lines"
CONF_SCROLLBAR_MODE = "scrollbar_mode"
@@ -817,9 +832,12 @@ CONF_SCROLL_DIR = "scroll_dir"
CONF_SCROLLABLE = "scrollable"
CONF_SCROLL_SNAP_X = "scroll_snap_x"
CONF_SCROLL_SNAP_Y = "scroll_snap_y"
CONF_SECTIONS = "sections"
CONF_SELECTED_INDEX = "selected_index"
CONF_SELECTED_TEXT = "selected_text"
CONF_SHOW_SNOW = "show_snow"
CONF_SIDEBAR_PAGE = "sidebar_page"
CONF_SIDEBAR_HEADER_STYLE = "sidebar_header_style"
CONF_SPIN_TIME = "spin_time"
CONF_SRC = "src"
CONF_START_ANGLE = "start_angle"
+7
View File
@@ -1009,6 +1009,13 @@ lv_color_t lv_grad_calculate_color(const lv_grad_dsc_t *dsc, int32_t pos) {
}
#endif // USE_LVGL_GRADIENT
#ifdef USE_LVGL_MENU
bool lv_menu_back_button_is_root_click(lv_obj_t *menu, lv_event_t *e) {
return lv_event_get_code(e) == LV_EVENT_CLICKED &&
lv_menu_back_button_is_root(menu, static_cast<lv_obj_t *>(lv_event_get_target(e)));
}
#endif // USE_LVGL_MENU
lv_point_t LvglComponent::get_touch_relative_to_obj(lv_obj_t *obj) {
auto *indev = lv_indev_get_act();
if (indev == nullptr) {
+6
View File
@@ -143,6 +143,12 @@ lv_obj_t *lv_list_get_row_for_remove(lv_obj_t *list, int index);
lv_color_t lv_grad_calculate_color(const lv_grad_dsc_t *dsc, int32_t pos);
#endif // USE_LVGL_GRADIENT
#ifdef USE_LVGL_MENU
// True if `e` is a click on `menu`'s back button while already at the root page -- the one
// case LVGL's own docs say the app must handle itself (there is no default behavior).
bool lv_menu_back_button_is_root_click(lv_obj_t *menu, lv_event_t *e);
#endif // USE_LVGL_MENU
// Parent class for things that wrap an LVGL object
class LvCompound {
public:
+326
View File
@@ -0,0 +1,326 @@
from esphome import automation
from esphome.automation import Trigger
import esphome.codegen as cg
import esphome.config_validation as cv
from esphome.const import CONF_BUTTON, CONF_ID, CONF_PAGES, CONF_TRIGGER_ID
from esphome.final_validate import FinalValidateConfig
from esphome.types import ConfigType
from ..automation import action_to_code
from ..defines import (
CONF_HEADER_MODE,
CONF_HEADER_STYLE,
CONF_IMAGE,
CONF_MAIN,
CONF_MENU,
CONF_ON_ROOT_BACK_BUTTON_CLICK,
CONF_PAGE,
CONF_ROOT_BACK_BTN,
CONF_ROOT_PAGE,
CONF_SECTIONS,
CONF_SIDEBAR_HEADER_STYLE,
CONF_SIDEBAR_PAGE,
CONF_TITLE,
LV_MENU_MODES,
LV_MENU_ROOT_BACK_BUTTON_MODES,
TYPE_FLEX,
get_menu_set_page_actions,
get_widget_map,
literal,
)
from ..lv_validation import lv_text
from ..lvcode import (
EVENT_ARG,
LambdaContext,
LocalVariable,
LvConditional,
lv,
lv_add,
lv_expr,
lv_obj,
)
from ..schemas import container_schema, part_schema
from ..types import LV_EVENT, LvType, ObjUpdateAction, lv_obj_t, lv_obj_t_ptr, lvgl_ns
from . import (
Widget,
WidgetType,
get_widget_,
get_widgets,
set_obj_properties,
widget_to_code,
)
from .obj import obj_spec
# Local to the "menu" widget only -- not a shared config concept.
CONF_SIDEBAR = "sidebar"
# Tag types, only used to distinguish declare_id()/use_id() checks for pages and
# sections -- like tileview's lv_tileview_tile_t they carry no extra behaviour, but
# they must have their own names: id type checks compare the type name, so reusing
# `lv_obj_t` here would let any object be passed where a page is required. The
# generated code always declares the variable as `lv_obj_t *`.
lv_menu_page_t = LvType("lv_menu_page_t")
lv_menu_section_t = LvType("lv_menu_section_t")
lv_menu_t = LvType(
"lv_menu_t",
largs=[(lv_obj_t_ptr, "page")],
lvalue=lambda w: lv_expr.menu_get_cur_main_page(w.obj),
has_on_value=True,
)
class MenuSectionType(WidgetType):
"""
A grouped, bordered set of rows inside a menu page (lv_menu_section_create).
Only valid as a direct child of a menu page, so it's not exposed in the
generic `widgets:` menu -- reached only via a page's `sections:` key.
"""
def __init__(self):
super().__init__(
"menu_section", lv_menu_section_t, (CONF_MAIN,), {}, is_mock=True
)
async def obj_creator(self, parent: cg.MockObj, config: dict):
return lv_expr.menu_section_create(parent)
menu_section_spec = MenuSectionType()
MENU_PAGE_SCHEMA = cv.Schema(
{
cv.Optional(CONF_TITLE): lv_text,
cv.Optional(CONF_SECTIONS): cv.ensure_list(
container_schema(
menu_section_spec,
{cv.GenerateID(): cv.declare_id(lv_menu_section_t)},
)
),
}
)
class MenuPageType(WidgetType):
"""
A page of the menu (lv_menu_page_create), shown one at a time via
`lvgl.menu.set_page`. Like tabview's tabs/tileview's tiles, pages are only
ever declared via the menu's own `pages:` key, not the generic `widgets:`
menu -- reachable content, not a freestanding widget type.
"""
def __init__(self):
super().__init__(
"menu_page", lv_menu_page_t, (CONF_MAIN,), MENU_PAGE_SCHEMA, is_mock=True
)
async def obj_creator(self, parent: cg.MockObj, config: dict):
title = config.get(CONF_TITLE)
return lv_expr.menu_page_create(
parent, await lv_text.process(title) if title is not None else cg.nullptr
)
async def on_create(self, var: cg.MockObj, config: dict):
# Runs before the page's ungrouped `widgets:` are added, so sections come first.
for section_conf in config.get(CONF_SECTIONS, ()):
await widget_to_code(section_conf, menu_section_spec, var)
return var
menu_page_spec = MenuPageType()
MENU_SCHEMA = cv.Schema(
{
cv.Required(CONF_PAGES): cv.All(
cv.ensure_list(
container_schema(
menu_page_spec, {cv.GenerateID(): cv.declare_id(lv_menu_page_t)}
)
),
cv.Length(min=1),
),
cv.Required(CONF_ROOT_PAGE): cv.use_id(lv_menu_page_t),
cv.Optional(CONF_SIDEBAR_PAGE): cv.use_id(lv_menu_page_t),
cv.Optional(CONF_HEADER_MODE, default="top_fixed"): LV_MENU_MODES.one_of,
cv.Optional(
CONF_ROOT_BACK_BTN, default="disabled"
): LV_MENU_ROOT_BACK_BUTTON_MODES.one_of,
cv.Optional(CONF_HEADER_STYLE): part_schema(obj_spec.parts),
cv.Optional(CONF_SIDEBAR_HEADER_STYLE): part_schema(obj_spec.parts),
cv.Optional(CONF_ON_ROOT_BACK_BUTTON_CLICK): automation.validate_automation(
{cv.GenerateID(CONF_TRIGGER_ID): cv.declare_id(Trigger.template())}
),
}
)
class MenuType(WidgetType):
def __init__(self):
super().__init__(
CONF_MENU, lv_menu_t, (CONF_MAIN,), schema=MENU_SCHEMA, modify_schema={}
)
def get_uses(self):
return CONF_BUTTON, CONF_IMAGE, TYPE_FLEX
def validate(self, value: ConfigType) -> ConfigType:
# The sidebar header only exists once a sidebar page has been set.
if CONF_SIDEBAR_HEADER_STYLE in value and CONF_SIDEBAR_PAGE not in value:
raise cv.Invalid(
f"'{CONF_SIDEBAR_HEADER_STYLE}' requires '{CONF_SIDEBAR_PAGE}'",
path=[CONF_SIDEBAR_HEADER_STYLE],
)
# Runs before the schema, so the default has not been applied yet and an
# invalid value is left for the schema to report.
back_btn = (
str(value.get(CONF_ROOT_BACK_BTN, "disabled"))
.upper()
.removeprefix(LV_MENU_ROOT_BACK_BUTTON_MODES.prefix)
)
if CONF_ON_ROOT_BACK_BUTTON_CLICK in value and back_btn != "ENABLED":
raise cv.Invalid(
f"'{CONF_ON_ROOT_BACK_BUTTON_CLICK}' requires "
f"'{CONF_ROOT_BACK_BTN}: enabled'",
path=[CONF_ON_ROOT_BACK_BUTTON_CLICK],
)
return value
async def to_code(self, w: Widget, config: dict):
await w.set_property(
"mode_header", await LV_MENU_MODES.process(config[CONF_HEADER_MODE])
)
await w.set_property(
"mode_root_back_button",
await LV_MENU_ROOT_BACK_BUTTON_MODES.process(config[CONF_ROOT_BACK_BTN]),
)
for page_conf in config[CONF_PAGES]:
await widget_to_code(page_conf, menu_page_spec, w.obj)
root_page = await get_widget_(config[CONF_ROOT_PAGE])
lv.menu_set_page(w.obj, root_page.obj)
if sidebar_id := config.get(CONF_SIDEBAR_PAGE):
sidebar_page = await get_widget_(sidebar_id)
lv.menu_set_sidebar_page(w.obj, sidebar_page.obj)
if header_style := config.get(CONF_HEADER_STYLE):
with LocalVariable(
"menu_header", lv_obj_t, rhs=lv_expr.menu_get_main_header(w.obj)
) as hdr:
await set_obj_properties(Widget(hdr, obj_spec), header_style)
if sidebar_header_style := config.get(CONF_SIDEBAR_HEADER_STYLE):
with LocalVariable(
"menu_sidebar_header",
lv_obj_t,
rhs=lv_expr.menu_get_sidebar_header(w.obj),
) as hdr:
await set_obj_properties(Widget(hdr, obj_spec), sidebar_header_style)
menu_spec = MenuType()
def _menu_page_owners(global_config: FinalValidateConfig) -> dict[str, tuple]:
"""
Map every declared menu page id to the config path of the menu that owns it.
A page id is always declared inside its menu's `pages:` list, so the owning menu
is three steps up the path (`id` -> list index -> `pages`).
"""
return {
str(declared_id): tuple(path[:-3])
for declared_id, path in global_config.declare_ids
if declared_id.type is lv_menu_page_t
}
def validate_menu_pages(global_config: FinalValidateConfig) -> None:
"""
Check that a menu only ever refers to pages of its own.
`lv_menu_page_create()` makes a page a child of one specific menu, so handing a
page of another menu to `root_page:`, `sidebar_page:` or `lvgl.menu.set_page`
can only misbehave at run time. It also hangs code generation when the other
menu is defined later, since the waiting menu's page never appears.
"""
owners = _menu_page_owners(global_config)
for menu_path in set(owners.values()):
menu_config = global_config.get_config_for_path(list(menu_path))
for key in (CONF_ROOT_PAGE, CONF_SIDEBAR_PAGE):
page_id = menu_config.get(key)
if page_id is not None and owners.get(str(page_id)) != menu_path:
raise cv.Invalid(
f"'{page_id}' is not a page of this menu; "
f"'{key}' must name one of the menu's own 'pages:'",
# Paths reported from final validation are relative to the
# component config, so drop the leading domain name.
path=[*menu_path[1:], key],
)
for config in get_menu_set_page_actions():
menu_id = config[CONF_ID]
page_id = config[CONF_PAGE]
menu_path = tuple(global_config.get_path_for_id(menu_id)[:-1])
if owners.get(str(page_id)) != menu_path:
raise cv.Invalid(
f"'lvgl.menu.set_page' page '{page_id}' is not a page of menu '{menu_id}'"
)
async def add_root_back_button_triggers() -> None:
"""
Wire up on_root_back_button_click for every menu, once all widgets exist.
Actions like `lvgl.widget.hide` on the menu itself need the widget map fully
populated, so this must run as a deferred pass rather than inline in
MenuType.to_code -- doing it inline deadlocks the code-generation coroutine
scheduler.
"""
# Snapshot: build_automation() can create widgets (e.g. `lvgl.list.add`).
for w in list(get_widget_map().values()):
if not isinstance(w.type, MenuType):
continue
for conf in w.config.get(CONF_ON_ROOT_BACK_BUTTON_CLICK, ()):
trigger = cg.new_Pvariable(conf[CONF_TRIGGER_ID])
await automation.build_automation(trigger, [], conf)
async with LambdaContext(EVENT_ARG, where=conf[CONF_TRIGGER_ID]) as context:
with LvConditional(
lvgl_ns.lv_menu_back_button_is_root_click(w.obj, literal("event"))
):
lv_add(trigger.trigger())
lv_obj.add_event_cb(
w.obj, await context.get_lambda(), LV_EVENT.CLICKED, cg.nullptr
)
def _record_set_page_action(config: ConfigType) -> ConfigType:
"""Remember the action so validate_menu_pages() can check the page's owner once
the whole config is known."""
get_menu_set_page_actions().append(config)
return config
@automation.register_action(
"lvgl.menu.set_page",
ObjUpdateAction,
cv.All(
cv.Schema(
{
cv.Required(CONF_ID): cv.use_id(lv_menu_t),
cv.Required(CONF_PAGE): cv.use_id(lv_menu_page_t),
cv.Optional(CONF_SIDEBAR, default=False): cv.boolean,
}
),
_record_set_page_action,
),
synchronous=True,
)
async def menu_set_page(config, action_id, template_arg, args):
widget = await get_widgets(config)
page = await get_widget_(config[CONF_PAGE])
sidebar = config[CONF_SIDEBAR]
async def do_set_page(w: Widget):
if sidebar:
lv.menu_set_sidebar_page(w.obj, page.obj)
else:
lv.menu_set_page(w.obj, page.obj)
return await action_to_code(widget, do_set_page, action_id, template_arg, args)
+11 -4
View File
@@ -1,4 +1,5 @@
import esphome.codegen as cg
from esphome.components.const import CONF_BORDER_ROUTER
from esphome.components.esp32 import add_idf_component, add_idf_sdkconfig_option
from esphome.config_helpers import filter_source_files_from_platform, get_logger_level
import esphome.config_validation as cv
@@ -188,10 +189,16 @@ def enable_mdns_storage() -> None:
cg.add_define("USE_MDNS_STORE_SERVICES")
def _openthread_srp_registers_services() -> bool:
"""Without border_router, the OpenThread SRP client registers the services instead of mDNS."""
openthread_config = CORE.config.get(CONF_OPENTHREAD)
return openthread_config is not None and CONF_BORDER_ROUTER not in openthread_config
def request_service_enable_disable() -> bool:
"""Request MDNSComponent::set_service_enabled() support.
ESP32 only, not with OpenThread. Returns True when the
ESP32 only, not with the OpenThread SRP client. Returns True when the
USE_MDNS_SUPPORTS_ENABLE_DISABLE define was added; guard C++ usage with it.
Public API for external components. Do not remove.
@@ -201,7 +208,7 @@ def request_service_enable_disable() -> bool:
mdns_config is None
or mdns_config[CONF_DISABLED]
or not CORE.is_esp32
or CONF_OPENTHREAD in CORE.config
or _openthread_srp_registers_services()
):
return False
cg.add_define("USE_MDNS_SUPPORTS_ENABLE_DISABLE")
@@ -288,13 +295,13 @@ async def to_code(config: ConfigType) -> None:
await cg.register_component(var, config)
# With both wifi and ethernet, the _esphomelib "network" TXT value follows the
# connected interface. OpenThread hands its services to the SRP client once,
# connected interface. The OpenThread SRP client registers the services once,
# so it keeps the fixed value.
if (
CONF_API in CORE.config
and CONF_WIFI in CORE.config
and CONF_ETHERNET in CORE.config
and CONF_OPENTHREAD not in CORE.config
and not _openthread_srp_registers_services()
):
from esphome.components import network
+2 -2
View File
@@ -126,8 +126,8 @@ class MDNSComponent final : public Component
#ifndef USE_MDNS_STORE_SERVICES
#error "USE_MDNS_SUPPORTS_ENABLE_DISABLE requires USE_MDNS_STORE_SERVICES"
#endif
#ifdef USE_OPENTHREAD
#error "USE_MDNS_SUPPORTS_ENABLE_DISABLE is not supported with OpenThread"
#ifdef USE_OPENTHREAD_SRP
#error "USE_MDNS_SUPPORTS_ENABLE_DISABLE is not supported with the OpenThread SRP client"
#endif
/// Enable or disable a compiled-in service, matched by type and proto (e.g. "_sendspin", "_tcp").
/// Only valid once this component is ready. Re-enabling re-reads the port but keeps the boot-time TXT values.
+5 -5
View File
@@ -12,7 +12,7 @@ namespace esphome::mdns {
ESPHOME_LOG_TAG(TAG, "mdns");
#ifndef USE_OPENTHREAD
#ifndef USE_OPENTHREAD_SRP
static esp_err_t add_service(const MDNSService &service) {
// Stack buffer for up to 16 txt records, heap fallback for more
SmallBufferWithHeapFallback<16, mdns_txt_item_t> txt_records(service.txt_records.size());
@@ -30,7 +30,7 @@ static esp_err_t add_service(const MDNSService &service) {
#endif
static void register_esp32(MDNSComponent *comp, StaticVector<MDNSService, MDNS_SERVICE_COUNT> &services) {
#ifdef USE_OPENTHREAD
#ifdef USE_OPENTHREAD_SRP
// OpenThread handles service registration via SRP client
// Services are compiled by MDNSComponent::compile_records_() and consumed by OpenThreadSrpComponent
#else
@@ -62,7 +62,7 @@ static void register_esp32(MDNSComponent *comp, StaticVector<MDNSService, MDNS_S
#endif
}
#if defined(USE_MDNS_SUPPORTS_ENABLE_DISABLE) && !defined(USE_OPENTHREAD)
#if defined(USE_MDNS_SUPPORTS_ENABLE_DISABLE) && !defined(USE_OPENTHREAD_SRP)
bool MDNSComponent::set_service_enabled(const char *service_type, const char *proto, bool enabled) {
// services_ is compiled in setup()
if (!this->is_ready()) {
@@ -87,7 +87,7 @@ bool MDNSComponent::set_service_enabled(const char *service_type, const char *pr
ESP_LOGW(TAG, "Service %s not found", service_type);
return false;
}
#endif // USE_MDNS_SUPPORTS_ENABLE_DISABLE && !USE_OPENTHREAD
#endif // USE_MDNS_SUPPORTS_ENABLE_DISABLE && !USE_OPENTHREAD_SRP
#ifdef USE_MDNS_DYNAMIC_NETWORK_TXT
bool MDNSComponent::publish_network_txt_(const char *network) {
@@ -110,7 +110,7 @@ void MDNSComponent::setup() {
}
void MDNSComponent::on_shutdown() {
#ifndef USE_OPENTHREAD
#ifndef USE_OPENTHREAD_SRP
mdns_free();
delay(40); // Allow the mdns packets announcing service removal to be sent
#endif
+9 -21
View File
@@ -297,15 +297,10 @@ void MipiDsi::draw_pixel_at(int x, int y, Color color) {
break;
}
case display::COLOR_BITNESS_888:
if (this->color_mode_ == display::COLOR_ORDER_BGR) {
this->buffer_[pos * 3] = color.b;
this->buffer_[pos * 3 + 1] = color.g;
this->buffer_[pos * 3 + 2] = color.r;
} else {
this->buffer_[pos * 3] = color.r;
this->buffer_[pos * 3 + 1] = color.g;
this->buffer_[pos * 3 + 2] = color.b;
}
// The panel applies the colour order, so the buffer keeps the standard b,g,r layout
this->buffer_[pos * 3] = color.b;
this->buffer_[pos * 3 + 1] = color.g;
this->buffer_[pos * 3 + 2] = color.r;
break;
case display::COLOR_BITNESS_332:
break;
@@ -341,19 +336,12 @@ void MipiDsi::fill(Color color) {
}
case display::COLOR_BITNESS_888:
if (this->color_mode_ == display::COLOR_ORDER_BGR) {
for (size_t i = 0; i != this->width_ * this->height_; i++) {
this->buffer_[i * 3 + 0] = color.b;
this->buffer_[i * 3 + 1] = color.g;
this->buffer_[i * 3 + 2] = color.r;
}
} else {
for (size_t i = 0; i != this->width_ * this->height_; i++) {
this->buffer_[i * 3 + 0] = color.r;
this->buffer_[i * 3 + 1] = color.g;
this->buffer_[i * 3 + 2] = color.b;
}
for (size_t i = 0; i != this->width_ * this->height_; i++) {
this->buffer_[i * 3 + 0] = color.b;
this->buffer_[i * 3 + 1] = color.g;
this->buffer_[i * 3 + 2] = color.r;
}
break;
default:
break;
-3
View File
@@ -39,8 +39,6 @@ class MipiDsi final : public display::Display {
public:
MipiDsi(size_t width, size_t height, display::ColorBitness color_depth, uint8_t pixel_mode)
: width_(width), height_(height), color_depth_(color_depth), pixel_mode_(pixel_mode) {}
display::ColorOrder get_color_mode() { return this->color_mode_; }
void set_color_mode(display::ColorOrder color_mode) { this->color_mode_ = color_mode; }
void set_invert_colors(bool invert_colors) { this->invert_colors_ = invert_colors; }
display::DisplayType get_display_type() override { return display::DisplayType::DISPLAY_TYPE_COLOR; }
@@ -102,7 +100,6 @@ class MipiDsi final : public display::Display {
uint8_t lanes_{2}; // 1, 2, 3 or 4 lanes
bool invert_colors_{};
display::ColorOrder color_mode_{display::COLOR_ORDER_BGR};
display::ColorBitness color_depth_;
uint8_t pixel_mode_{};
+3 -3
View File
@@ -570,12 +570,12 @@ class MipiSpiBuffer
if (this->auto_clear_enabled_) {
this->clear();
}
if (this->page_ != nullptr) {
if (this->show_test_card_) {
this->test_card();
} else if (this->page_ != nullptr) {
this->page_->get_writer()(*this);
} else if (this->writer_.has_value()) {
(*this->writer_)(*this);
} else {
this->test_card();
}
#if ESPHOME_LOG_LEVEL == ESPHOME_LOG_LEVEL_VERBOSE
esph_log_v(TAG, "Drawing from line %d took %dms", this->start_line_, millis() - lap);
+30
View File
@@ -2,10 +2,40 @@
#include "esphome/core/application.h"
#include "esphome/core/defines.h"
#include "esphome/core/helpers.h"
#include "esphome/core/log.h"
#ifdef USE_NETWORK
namespace esphome::network {
#if defined(USE_ESP32) && USE_NETWORK_IPV6
ESPHOME_LOG_TAG(TAG, "network");
void fill_ip6_addresses(esp_netif_t *netif, IPAddresses &addresses) {
esp_ip6_addr_t ip6s[CONFIG_LWIP_IPV6_NUM_ADDRESSES];
size_t count = esp_netif_get_all_ip6(netif, ip6s);
// Slot 0 holds the IPv4 address
if (count > addresses.size() - 1) {
// Warn once per interface
static esp_netif_t *warned[3]{};
for (auto &slot : warned) {
if (slot == netif) {
break;
}
if (slot == nullptr) {
slot = netif;
ESP_LOGW(TAG, "%s has %zu IPv6 addresses, only %zu are reported", esp_netif_get_desc(netif), count,
addresses.size() - 1);
break;
}
}
count = addresses.size() - 1;
}
for (size_t i = 0; i < count; i++) {
addresses[i + 1] = IPAddress(&ip6s[i]);
}
}
#endif
// The order of the components is important: WiFi should come after any possible main interfaces (it may be used as
// an AP that uses a previous interface for NAT).

Some files were not shown because too many files have changed in this diff Show More