ESPHome 2026.11.0-dev
Loading...
Searching...
No Matches
ld24xx.h
Go to the documentation of this file.
1#pragma once
2
5
6#include <memory>
7#include <span>
8
9#ifdef USE_SENSOR
11#endif
12
13#define LOG_SENSOR_WITH_DEDUP_SAFE(tag, name, sensor) \
14 if ((sensor).has_sensor()) { \
15 LOG_SENSOR(tag, name, (sensor).get_sensor()); \
16 }
17
18#define SAFE_PUBLISH_SENSOR(sensor, value) (sensor).publish_state_if_not_dup(value)
19
20#define SAFE_PUBLISH_SENSOR_UNKNOWN(sensor) (sensor).publish_state_unknown()
21
22#define highbyte(val) (uint8_t) ((val) >> 8)
23#define lowbyte(val) (uint8_t) ((val) & 0xff)
24
25namespace esphome::ld24xx {
26
27// Helper to find index of value in constexpr array
28template<size_t N> optional<size_t> find_index(const uint32_t (&arr)[N], uint32_t value) {
29 for (size_t i = 0; i < N; i++) {
30 if (arr[i] == value)
31 return i;
32 }
33 return {};
34}
35
36static const char *const UNKNOWN_MAC = "unknown";
37static const char *const VERSION_FMT = "%u.%02X.%02X%02X%02X%02X";
38
39// Helper function to format MAC address with stack allocation
40// Returns pointer to UNKNOWN_MAC constant or formatted buffer
41inline const char *format_mac_str(const uint8_t *mac_address, std::span<char, MAC_ADDRESS_PRETTY_BUFFER_SIZE> buffer) {
42 if (mac_address_is_valid(mac_address)) {
43 format_mac_addr_upper(mac_address, buffer.data());
44 return buffer.data();
45 }
46 return UNKNOWN_MAC;
47}
48
49// Helper function to format firmware version with stack allocation
50// Buffer must be exactly 20 bytes (format: "x.xxXXXXXX" fits in 11 + null terminator, 20 for safety)
51inline void format_version_str(const uint8_t *version, std::span<char, 20> buffer) {
52 snprintf(buffer.data(), buffer.size(), VERSION_FMT, version[1], version[0], version[5], version[4], version[3],
53 version[2]);
54}
55
56#ifdef USE_SENSOR
57template<typename T> using SensorWithDedup = sensor::SensorWithDedup<T>;
58#endif
59} // namespace esphome::ld24xx
A sensor pointer that may be null plus the last value it published, stored inline.
uint32_t buffer
Definition gif.cpp:50
void format_version_str(const uint8_t *version, std::span< char, 20 > buffer)
Definition ld24xx.h:51
const char * format_mac_str(const uint8_t *mac_address, std::span< char, MAC_ADDRESS_PRETTY_BUFFER_SIZE > buffer)
Definition ld24xx.h:41
optional< size_t > find_index(const uint32_t(&arr)[N], uint32_t value)
Definition ld24xx.h:28
bool mac_address_is_valid(const uint8_t *mac)
Check if the MAC address is not all zeros or all ones.
Definition helpers.cpp:865
char * format_mac_addr_upper(const uint8_t *mac, char *output)
Format MAC address as XX:XX:XX:XX:XX:XX (uppercase, colon separators)
Definition helpers.h:1676
static void uint32_t