ESPHome 2026.5.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
12#define SUB_SENSOR_WITH_DEDUP(name, dedup_type) \
13 protected: \
14 ld24xx::SensorWithDedup<dedup_type> name##_sensor_{}; \
15\
16 public: \
17 void set_##name##_sensor(sensor::Sensor *sensor) { this->name##_sensor_.set_sensor(sensor); }
18#endif
19
20#define LOG_SENSOR_WITH_DEDUP_SAFE(tag, name, sensor) \
21 if ((sensor).has_sensor()) { \
22 LOG_SENSOR(tag, name, (sensor).get_sensor()); \
23 }
24
25#define SAFE_PUBLISH_SENSOR(sensor, value) (sensor).publish_state_if_not_dup(value)
26
27#define SAFE_PUBLISH_SENSOR_UNKNOWN(sensor) (sensor).publish_state_unknown()
28
29#define highbyte(val) (uint8_t)((val) >> 8)
30#define lowbyte(val) (uint8_t)((val) &0xff)
31
32namespace esphome::ld24xx {
33
34// Helper to find index of value in constexpr array
35template<size_t N> optional<size_t> find_index(const uint32_t (&arr)[N], uint32_t value) {
36 for (size_t i = 0; i < N; i++) {
37 if (arr[i] == value)
38 return i;
39 }
40 return {};
41}
42
43static const char *const UNKNOWN_MAC = "unknown";
44static const char *const VERSION_FMT = "%u.%02X.%02X%02X%02X%02X";
45
46// Helper function to format MAC address with stack allocation
47// Returns pointer to UNKNOWN_MAC constant or formatted buffer
48// Buffer must be exactly 18 bytes (17 for "XX:XX:XX:XX:XX:XX" + null terminator)
49inline const char *format_mac_str(const uint8_t *mac_address, std::span<char, 18> buffer) {
50 if (mac_address_is_valid(mac_address)) {
51 format_mac_addr_upper(mac_address, buffer.data());
52 return buffer.data();
53 }
54 return UNKNOWN_MAC;
55}
56
57// Helper function to format firmware version with stack allocation
58// Buffer must be exactly 20 bytes (format: "x.xxXXXXXX" fits in 11 + null terminator, 20 for safety)
59inline void format_version_str(const uint8_t *version, std::span<char, 20> buffer) {
60 snprintf(buffer.data(), buffer.size(), VERSION_FMT, version[1], version[0], version[5], version[4], version[3],
61 version[2]);
62}
63
64#ifdef USE_SENSOR
67template<typename T> class SensorWithDedup {
68 public:
70 this->sens_ = sens;
71 this->dedup_ = {};
72 }
73
75 if (this->sens_ != nullptr && this->dedup_.next(state)) {
76 this->sens_->publish_state(static_cast<float>(state));
77 }
78 }
79
81 if (this->sens_ != nullptr && this->dedup_.next_unknown()) {
82 this->sens_->publish_state(NAN);
83 }
84 }
85
86 bool has_sensor() const { return this->sens_ != nullptr; }
87 sensor::Sensor *get_sensor() const { return this->sens_; }
88
89 protected:
92};
93#endif
94} // namespace esphome::ld24xx
Helper class to deduplicate items in a series of values.
Definition helpers.h:1985
Sensor with deduplication — sensor may be null, null check is internal.
Definition ld24xx.h:67
sensor::Sensor * get_sensor() const
Definition ld24xx.h:87
void publish_state_if_not_dup(T state)
Definition ld24xx.h:74
Deduplicator< T > dedup_
Definition ld24xx.h:91
void set_sensor(sensor::Sensor *sens)
Definition ld24xx.h:69
Base-class for all sensors.
Definition sensor.h:47
void publish_state(float state)
Publish a new state to the front-end.
Definition sensor.cpp:68
bool state
Definition fan.h:2
void format_version_str(const uint8_t *version, std::span< char, 20 > buffer)
Definition ld24xx.h:59
const char * format_mac_str(const uint8_t *mac_address, std::span< char, 18 > buffer)
Definition ld24xx.h:49
optional< size_t > find_index(const uint32_t(&arr)[N], uint32_t value)
Definition ld24xx.h:35
bool mac_address_is_valid(const uint8_t *mac)
Check if the MAC address is not all zeros or all ones.
Definition helpers.cpp:884
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:1435
static void uint32_t